All Projects → pgvector → pgvector

pgvector / pgvector

Licence: other
Open-source vector similarity search for Postgres

Programming Languages

c
50402 projects - #5 most used programming language
perl
6916 projects
Makefile
30231 projects
Dockerfile
14818 projects

Projects that are alternatives of or similar to pgvector

Milvus
An open-source vector database for embedding similarity search and AI applications.
Stars: ✭ 9,015 (+1770.33%)
Mutual labels:  nearest-neighbor-search, approximate-nearest-neighbor-search
annoy.rb
annoy-rb provides Ruby bindings for the Annoy (Approximate Nearest Neighbors Oh Yeah).
Stars: ✭ 23 (-95.23%)
Mutual labels:  nearest-neighbor-search, approximate-nearest-neighbor-search
Annoy
Approximate Nearest Neighbors in C++/Python optimized for memory usage and loading/saving to disk
Stars: ✭ 9,262 (+1821.58%)
Mutual labels:  nearest-neighbor-search, approximate-nearest-neighbor-search
lshensemble
LSH index for approximate set containment search
Stars: ✭ 48 (-90.04%)
Mutual labels:  nearest-neighbor-search, approximate-nearest-neighbor-search
scikit-hubness
A Python package for hubness analysis and high-dimensional data mining
Stars: ✭ 41 (-91.49%)
Mutual labels:  nearest-neighbor-search, approximate-nearest-neighbor-search
adventures-with-ann
All the code for a series of Medium articles on Approximate Nearest Neighbors
Stars: ✭ 40 (-91.7%)
Mutual labels:  nearest-neighbor-search, approximate-nearest-neighbor-search
pynanoflann
Unofficial python wrapper to the nanoflann k-d tree
Stars: ✭ 24 (-95.02%)
Mutual labels:  nearest-neighbor-search
NearestNeighborDescent.jl
Efficient approximate k-nearest neighbors graph construction and search in Julia
Stars: ✭ 34 (-92.95%)
Mutual labels:  approximate-nearest-neighbor-search
instant-distance
Fast approximate nearest neighbor searching in Rust, based on HNSW index
Stars: ✭ 140 (-70.95%)
Mutual labels:  approximate-nearest-neighbor-search
pqtable
Fast search algorithm for product-quantized codes via hash-tables
Stars: ✭ 48 (-90.04%)
Mutual labels:  nearest-neighbor-search
docarray
The data structure for unstructured data
Stars: ✭ 561 (+16.39%)
Mutual labels:  nearest-neighbor-search
kdtree
A k-d tree implementation in Go.
Stars: ✭ 98 (-79.67%)
Mutual labels:  nearest-neighbor-search
product-quantization
🙃Implementation of vector quantization algorithms, codes for Norm-Explicit Quantization: Improving Vector Quantization for Maximum Inner Product Search.
Stars: ✭ 40 (-91.7%)
Mutual labels:  approximate-nearest-neighbor-search
awesome-vector-search
Collections of vector search related libraries, service and research papers
Stars: ✭ 460 (-4.56%)
Mutual labels:  nearest-neighbor-search
Dolphinn
High Dimensional Approximate Near(est) Neighbor
Stars: ✭ 32 (-93.36%)
Mutual labels:  nearest-neighbor-search
spark-annoy
Building Annoy Index on Apache Spark
Stars: ✭ 73 (-84.85%)
Mutual labels:  approximate-nearest-neighbor-search
elasticsearch-approximate-nearest-neighbor
Plugin to integrate approximate nearest neighbor(ANN) search with Elasticsearch
Stars: ✭ 53 (-89%)
Mutual labels:  approximate-nearest-neighbor-search
wordvector be
Web服务:使用腾讯 800 万词向量模型和 spotify annoy 引擎得到相似关键词
Stars: ✭ 92 (-80.91%)
Mutual labels:  nearest-neighbor-search
Rayuela.jl
Code for my PhD thesis. Library of quantization-based methods for fast similarity search in high dimensions. Presented at ECCV 18.
Stars: ✭ 54 (-88.8%)
Mutual labels:  nearest-neighbor-search
ann-benchmarks
Benchmarking approximate nearest neighbors. Note: This is an archived version from our SISAP 2017 paper, see below.
Stars: ✭ 30 (-93.78%)
Mutual labels:  nearest-neighbor-search

pgvector

Open-source vector similarity search for Postgres

CREATE TABLE table (column vector(3));
CREATE INDEX ON table USING ivfflat (column vector_l2_ops);
SELECT * FROM table ORDER BY column <-> '[1,2,3]' LIMIT 5;

Supports L2 distance, inner product, and cosine distance

Build Status

Installation

Compile and install the extension (supports Postgres 9.6+)

git clone --branch v0.2.5 https://github.com/pgvector/pgvector.git
cd pgvector
make
make install # may need sudo

Then load it in databases where you want to use it

CREATE EXTENSION vector;

You can also install it with Docker, Homebrew, or PGXN

Getting Started

Create a vector column with 3 dimensions (replace table and column with non-reserved names)

CREATE TABLE table (column vector(3));

Insert values

INSERT INTO table VALUES ('[1,2,3]'), ('[4,5,6]');

Get the nearest neighbor by L2 distance

SELECT * FROM table ORDER BY column <-> '[3,1,2]' LIMIT 1;

Also supports inner product (<#>) and cosine distance (<=>)

Note: <#> returns the negative inner product since Postgres only supports ASC order index scans on operators

Indexing

Speed up queries with an approximate index. Add an index for each distance function you want to use.

L2 distance

CREATE INDEX ON table USING ivfflat (column vector_l2_ops);

Inner product

CREATE INDEX ON table USING ivfflat (column vector_ip_ops);

Cosine distance

CREATE INDEX ON table USING ivfflat (column vector_cosine_ops);

Indexes should be created after the table has some data for optimal clustering. Also, unlike typical indexes which only affect performance, you may see different results for queries after adding an approximate index.

Index Options

Specify the number of inverted lists (100 by default)

CREATE INDEX ON table USING ivfflat (column opclass) WITH (lists = 100);

A good place to start is 4 * sqrt(rows)

Query Options

Specify the number of probes (1 by default)

SET ivfflat.probes = 1;

A higher value improves recall at the cost of speed.

Use SET LOCAL inside a transaction to set it for a single query

BEGIN;
SET LOCAL ivfflat.probes = 1;
SELECT ...
COMMIT;

Indexing Progress

Check indexing progress with Postgres 12+

SELECT phase, tuples_done, tuples_total FROM pg_stat_progress_create_index;

The phases are:

  1. initializing
  2. sampling table
  3. performing k-means
  4. sorting tuples
  5. loading tuples

Note: tuples_done and tuples_total are only populated during the loading tuples phase

Partial Indexes

Consider partial indexes for queries with a WHERE clause

CREATE INDEX ON table USING ivfflat (column opclass) WHERE (other_column = 123);

To index many different values of other_column, consider partitioning on other_column.

Performance

To speed up queries without an index, increase max_parallel_workers_per_gather.

SET max_parallel_workers_per_gather = 4;

To speed up queries with an index, increase the number of inverted lists (at the expense of recall).

CREATE INDEX ON table USING ivfflat (column opclass) WITH (lists = 1000);

Reference

Vector Type

Each vector takes 4 * dimensions + 8 bytes of storage. Each element is a float, and all elements must be finite (no NaN, Infinity or -Infinity). Vectors can have up to 1024 dimensions.

Vector Operators

Operator Description
+ element-wise addition
- element-wise subtraction
<-> Euclidean distance
<#> negative inner product
<=> cosine distance

Vector Functions

Function Description
cosine_distance(vector, vector) cosine distance
inner_product(vector, vector) inner product
l2_distance(vector, vector) Euclidean distance
vector_dims(vector) number of dimensions
vector_norm(vector) Euclidean norm

Libraries

Libraries that use pgvector:

Frequently Asked Questions

How many vectors can be stored in a single table?

A non-partitioned table has a limit of 32 TB by default in Postgres. A partitioned table can have thousands of partitions of that size.

Is replication supported?

Yes, pgvector uses the write-ahead log (WAL), which allows for replication and point-in-time recovery.

What if my data has more than 1024 dimensions?

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/vector.h

Additional Installation Methods

Docker

Get the Docker image with:

docker pull ankane/pgvector

This adds pgvector to the Postgres image.

You can also build the image manually

git clone --branch v0.2.5 https://github.com/pgvector/pgvector.git
cd pgvector
docker build -t pgvector .

Homebrew

On Mac with Homebrew Postgres, you can use:

brew install pgvector/brew/pgvector

PGXN

Install from the PostgreSQL Extension Network with:

pgxn install vector

Hosted Postgres

Some Postgres providers only support specific extensions. To request a new extension:

  • Amazon RDS - follow the instructions on this page
  • Google Cloud SQL - follow the instructions on this page
  • DigitalOcean Managed Databases - vote or comment on this page
  • Azure Database for PostgreSQL - follow the instructions on this page

Upgrading

Install the latest version and run:

ALTER EXTENSION vector UPDATE;

Thanks

Thanks to:

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development:

git clone https://github.com/pgvector/pgvector.git
cd pgvector
make
make install

To run all tests:

make installcheck        # regression tests
make prove_installcheck  # TAP tests

To run single tests:

make installcheck REGRESS=functions                    # regression test
make prove_installcheck PROVE_TESTS=test/t/001_wal.pl  # TAP test

To enable benchmarking:

make clean && PG_CFLAGS=-DIVFFLAT_BENCH make && make install

Resources for contributors

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].