|
|
|
|
@@ -2,11 +2,7 @@
|
|
|
|
|
|
|
|
|
|
Open-source vector similarity search for Postgres
|
|
|
|
|
|
|
|
|
|
Supports
|
|
|
|
|
|
|
|
|
|
- exact and approximate nearest neighbor search
|
|
|
|
|
- L2 distance, inner product, and cosine distance
|
|
|
|
|
- any [language](#languages) with a Postgres client
|
|
|
|
|
Supports exact and approximate nearest neighbor search for L2 distance, inner product, and cosine distance
|
|
|
|
|
|
|
|
|
|
[](https://github.com/pgvector/pgvector/actions)
|
|
|
|
|
|
|
|
|
|
@@ -40,7 +36,7 @@ Create a vector column with 3 dimensions
|
|
|
|
|
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Insert vectors
|
|
|
|
|
Insert values
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
|
|
|
|
|
@@ -128,7 +124,7 @@ SELECT embedding <-> '[3,1,2]' AS distance FROM items;
|
|
|
|
|
For inner product, multiply by -1 (since `<#>` returns the negative inner product)
|
|
|
|
|
|
|
|
|
|
```sql
|
|
|
|
|
SELECT (embedding <#> '[3,1,2]') * -1 AS inner_product FROM items;
|
|
|
|
|
SELECT -1 * (embedding <#> '[3,1,2]') AS inner_product FROM items;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
For cosine similarity, use 1 - cosine distance
|
|
|
|
|
@@ -137,7 +133,7 @@ For cosine similarity, use 1 - cosine distance
|
|
|
|
|
SELECT 1 - (embedding <=> '[3,1,2]') AS cosine_similarity FROM items;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### Aggregates
|
|
|
|
|
#### Averaging
|
|
|
|
|
|
|
|
|
|
Average vectors
|
|
|
|
|
|
|
|
|
|
@@ -160,8 +156,8 @@ You can add an index to use approximate nearest neighbor search, which trades so
|
|
|
|
|
Three keys to achieving good recall are:
|
|
|
|
|
|
|
|
|
|
1. Create the index *after* the table has some data
|
|
|
|
|
2. Choose an appropriate number of lists - a good place to start is `rows / 1000` for up to 1M rows and `sqrt(rows)` for over 1M rows
|
|
|
|
|
3. When querying, specify an appropriate number of [probes](#query-options) (higher is better for recall, lower is better for speed) - a good place to start is `lists / 10` for up to 1M rows and `sqrt(lists)` for over 1M rows
|
|
|
|
|
2. Choose an appropriate number of lists (a good place to start is `rows / 1000` for up to 1M rows and `sqrt(rows)` for over 1M rows)
|
|
|
|
|
3. When querying, specify an appropriate number of [probes](#query-options) (higher is better for recall, lower is better for speed)
|
|
|
|
|
|
|
|
|
|
Add an index for each distance function you want to use.
|
|
|
|
|
|
|
|
|
|
@@ -279,10 +275,8 @@ Language | Libraries / Examples
|
|
|
|
|
--- | ---
|
|
|
|
|
C++ | [pgvector-cpp](https://github.com/pgvector/pgvector-cpp)
|
|
|
|
|
C# | [pgvector-dotnet](https://github.com/pgvector/pgvector-dotnet)
|
|
|
|
|
Crystal | [pgvector-crystal](https://github.com/pgvector/pgvector-crystal)
|
|
|
|
|
Elixir | [pgvector-elixir](https://github.com/pgvector/pgvector-elixir)
|
|
|
|
|
Go | [pgvector-go](https://github.com/pgvector/pgvector-go)
|
|
|
|
|
Haskell | [pgvector-haskell](https://github.com/pgvector/pgvector-haskell)
|
|
|
|
|
Java, Scala | [pgvector-java](https://github.com/pgvector/pgvector-java)
|
|
|
|
|
Julia | [pgvector-julia](https://github.com/pgvector/pgvector-julia)
|
|
|
|
|
Lua | [pgvector-lua](https://github.com/pgvector/pgvector-lua)
|
|
|
|
|
@@ -293,7 +287,6 @@ Python | [pgvector-python](https://github.com/pgvector/pgvector-python)
|
|
|
|
|
R | [pgvector-r](https://github.com/pgvector/pgvector-r)
|
|
|
|
|
Ruby | [pgvector-ruby](https://github.com/pgvector/pgvector-ruby), [Neighbor](https://github.com/ankane/neighbor)
|
|
|
|
|
Rust | [pgvector-rust](https://github.com/pgvector/pgvector-rust)
|
|
|
|
|
Swift | [pgvector-swift](https://github.com/pgvector/pgvector-swift)
|
|
|
|
|
|
|
|
|
|
## Frequently Asked Questions
|
|
|
|
|
|
|
|
|
|
@@ -312,10 +305,6 @@ Two things you can try are:
|
|
|
|
|
1. use dimensionality reduction
|
|
|
|
|
2. compile Postgres with a larger block size (`./configure --with-blocksize=32`) and edit the limit in `src/ivfflat.h`
|
|
|
|
|
|
|
|
|
|
#### Why am I seeing less results after adding an index?
|
|
|
|
|
|
|
|
|
|
The index was likely created with too little data for the number of lists. Drop the index until the table has more data.
|
|
|
|
|
|
|
|
|
|
## Reference
|
|
|
|
|
|
|
|
|
|
### Vector Type
|
|
|
|
|
@@ -452,7 +441,7 @@ To request a new extension on other providers:
|
|
|
|
|
- Google Cloud SQL - vote or comment on [this page](https://issuetracker.google.com/issues/265172065)
|
|
|
|
|
- Azure Database - vote or comment on [this page](https://feedback.azure.com/d365community/idea/7b423322-6189-ed11-a81b-000d3ae49307)
|
|
|
|
|
- DigitalOcean Managed Databases - vote or comment on [this page](https://ideas.digitalocean.com/app-framework-services/p/pgvector-extension-for-postgresql)
|
|
|
|
|
- Heroku Postgres - vote or comment on [this page](https://github.com/heroku/roadmap/issues/156)
|
|
|
|
|
- Render - vote or comment on [this page](https://feedback.render.com/features/p/add-pgvector-extension-to-postgresql)
|
|
|
|
|
|
|
|
|
|
## Upgrading
|
|
|
|
|
|
|
|
|
|
|