pgvecto.rs
pgvecto.rs is a Postgres extension that provides vector similarity search functions. It is written in Rust and based on pgrx.
Features
- cosine_distance: cosine distance
- dot_product_distance: dot product distance
- square_euclidean_distance: square Euclidean distance
Build from source
cargo install cargo-pgrx
cargo pgrx init
cargo pgrx run
Getting Started
-- 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;
Roadmap
See the open issues
Contributors
β¨
Thanks goes to these wonderful people (emoji key):
Ce Gao |
Keming |
Jinjing Zhou |
Alex Chi |
|||
Add your contributions |
This project follows the all-contributors specification. Contributions of any kind welcome!
Acknowledgements
Thanks to the following projects: