pgvecto.rs
pgvecto.rs is a Postgres extension that provides vector similarity search functions. It is written in Rust and based on pgrx.
- cosine_distance: cosine distance
- dot_product_distance: dot product distance
- square_euclidean_distance: square Euclidean distance
cargo install cargo-pgrx
cargo pgrx init
cargo pgrx run
-- install the extension
DROP EXTENSION IF EXISTS vectors;
CREATE EXTENSION vectors;
-- check the extension related functions
\df+
-- call the distance function through operators
-- square Euclidean distance
SELECT array[1, 2, 3] <-> array[3, 2, 1];
-- dot product distance
SELECT array[1, 2, 3] <#> array[3, 2, 1];
-- cosine distance
SELECT array[1, 2, 3] <=> array[3, 2, 1];
-- create table
CREATE TABLE items (id bigserial PRIMARY KEY, emb numeric[]);
-- insert values
INSERT INTO items (emb) VALUES (ARRAY[1,2,3]), (ARRAY[4,5,6]);
-- query the similar embeddings
SELECT * FROM items ORDER BY emb <-> ARRAY[3,2,1] LIMIT 5;
-- query the neighbors within a certain distance
SELECT * FROM items WHERE emb <-> ARRAY[3,2,1] < 5;
See the open issues
Thanks goes to these wonderful people (emoji key):
Ce Gao πΌ π π |
Keming π π» π π€ π |
Jinjing Zhou π¨ π€ π |
||||
Add your contributions |
This project follows the all-contributors specification. Contributions of any kind welcome!
Thanks to the following projects: