pgvecto.rs is a Postgres extension that provides vector similarity search functions. It is written in Rust and based on pgrx. It is currently in the beta status, we invite you to try it out in production and provide us with feedback. Read more at ๐our launch blog.
- ๐ Easy to use: pgvecto.rs is a Postgres extension, which means that you can use it directly within your existing database. This makes it easy to integrate into your existing workflows and applications.
- ๐ Async indexing: pgvecto.rs's index is asynchronously constructed by the background threads and does not block insertions and always ready for new queries.
- ๐ฅ Filtering: pgvecto.rs supports filtering. You can set conditions when searching or retrieving points. This is the missing feature of other postgres extensions.
- ๐งฎ Quantization: pgvecto.rs supports scalar quantization and product qutization up to 64x.
- ๐ฆ Rewrite in Rust: Rust's strict compile-time checks ensure memory safety, reducing the risk of bugs and security issues commonly associated with C extensions.
pgvecto.rs | pgvector | |
---|---|---|
Transaction support | โ | |
Sufficient Result with Delete/Update/Filter | โ | |
Vector Dimension Limit | 65535 | 2000 |
Prefilter on HNSW | โ | โ |
Parallel HNSW Index build | โก๏ธ Linearly faster with more cores | ๐ Only single core used |
Async Index build | Ready for queries anytime and do not block insertions. | โ |
Quantization | Scalar/Product Quantization | โ |
More details at pgvecto.rs vs. pgvector
- Getting Started
- Usage
- Administration
- Developers
For new users, we recommend using the Docker image to get started quickly.
docker run \
--name pgvecto-rs-demo \
-e POSTGRES_PASSWORD=mysecretpassword \
-p 5432:5432 \
-d tensorchord/pgvecto-rs:pg16-v0.1.13
Then you can connect to the database using the psql
command line tool. The default username is postgres
, and the default password is mysecretpassword
.
psql -h localhost -p 5432 -U postgres
Run the following SQL to ensure the extension is enabled.
DROP EXTENSION IF EXISTS vectors;
CREATE EXTENSION vectors;
pgvecto.rs introduces a new data type vector(n)
denoting an n-dimensional vector. The n
within the brackets signifies the dimensions of the vector.
You could create a table with the following SQL.
-- create table with a vector column
CREATE TABLE items (
id bigserial PRIMARY KEY,
embedding vector(3) NOT NULL -- 3 dimensions
);
Tip
vector(n)
is a valid data type only if vector(3)
of vector
is also a valid data type. However, you cannot still put vector
for a column or there is some values mismatched with dimension denoted by the column, you won't able to create an index on it.
You can then populate the table with vector data as follows.
-- insert values
INSERT INTO items (embedding)
VALUES ('[1,2,3]'), ('[4,5,6]');
-- or insert values using a casting from array to vector
INSERT INTO items (embedding)
VALUES (ARRAY[1, 2, 3]::real[]), (ARRAY[4, 5, 6]::real[]);
We support three operators to calculate the distance between two vectors.
-
<->
: squared Euclidean distance, defined as$\Sigma (x_i - y_i) ^ 2$ . -
<#>
: negative dot product, defined as$- \Sigma x_iy_i$ . -
<=>
: cosine distance, defined as$1 - \frac{\Sigma x_iy_i}{\sqrt{\Sigma x_i^2 \Sigma y_i^2}}$ .
-- call the distance function through operators
-- squared Euclidean distance
SELECT '[1, 2, 3]'::vector <-> '[3, 2, 1]'::vector;
-- negative dot product
SELECT '[1, 2, 3]'::vector <#> '[3, 2, 1]'::vector;
-- cosine distance
SELECT '[1, 2, 3]'::vector <=> '[3, 2, 1]'::vector;
You can search for a vector simply like this.
-- query the similar embeddings
SELECT * FROM items ORDER BY embedding <-> '[3,2,1]' LIMIT 5;
vecf16
type is the same with vector
in anything but the scalar type. It stores 16-bit floating point numbers. If you want to reduce the memory usage to get better performance, you can try to replace vector
type with vecf16
type.
Please check out ROADMAP. Want to jump in? Welcome discussions and contributions!
- Chat with us on ๐ฌ Discord
- Have a look at
good first issue ๐
issues!
We welcome all kinds of contributions from the open-source community, individuals, and partners.
- Join our discord community!
- To build from the source, please read our contributing documentation and development tutorial.
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
Thanks to the following projects: