SuperAPI AI Directory

pgvector

Extension that turns PostgreSQL into a vector database — no separate DB needed, uses familiar SQL

Open website

About this tool

pgvector is an open-source vector similarity search extension for PostgreSQL, with the goal of storing vectors together with the rest of the data in Postgres, thereby eliminating the need to deploy a separate vector database. It supports exact and approximate nearest neighbor search, supports single-precision, half-precision, binary, and sparse vectors, supports L2 distance, inner product, cosine distance, L1 distance, Hamming distance, and Jaccard distance, and can be used with any language that has a Postgres client. At the same time, it preserves Postgres features such as ACID compliance, point-in-time recovery, and JOINs.

Main capabilities:

  • Vector types: vector (up to 2,000 dimensions), halfvec (up to 4,000 dimensions), bit (up to 64,000 dimensions), sparsevec (up to 1,000 non-zero elements).
  • Distance operators: <-> L2 distance, <#> negative inner product, <=> cosine distance, <+> L1 distance, <~> Hamming distance (binary vectors), <%> Jaccard distance (binary vectors).
  • Index types: HNSW and IVFFlat, used for approximate nearest neighbor search; exact nearest neighbor search is performed by default.
  • Supports quantization to scale, supports half-precision indexing, binary quantization, subvector indexing, and hybrid search (combined with Postgres full-text search, with results mergeable using Reciprocal Rank Fusion or a cross-encoder).
  • Supports iterative index scans (since 0.8.0), used for approximate queries with filter conditions.

Usage:

  • Compile and install the extension, supporting Postgres 13+; it can also be installed via Docker, Homebrew, PGXN, APT, Yum, pkg, APK, conda-forge, and is preinstalled in Postgres.app and many managed providers.
  • Enable the extension in each database where it will be used: CREATE EXTENSION vector;.
  • Create a vector column, for example CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));.
  • Insert or bulk load vectors (COPY can be used).
  • Query nearest neighbors, for example SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;.
  • Add an index to use approximate search, such as HNSW or IVFFlat, and create a separate index for each distance function.
  • HNSW parameters (m, ef_construction, hnsw.ef_search) and IVFFlat parameters (lists, ivfflat.probes) can be tuned.

Applicable scenarios:

  • Applications that need to perform vector similarity search directly in Postgres, such as semantic search, recommendations, retrieval-augmented generation, etc.
  • Teams that want to leverage the existing Postgres ecosystem (JOINs, ACID, backup and recovery, replication, sharding tools such as Citus, PgDog).
  • Multi-tenant scenarios can achieve tenant isolation through list partitioning or separate tables.

Limitations and considerations:

  • Approximate indexes sacrifice some recall in exchange for speed; after adding an approximate index, query results may differ from exact search.
  • Approximate queries with WHERE filters may return fewer results; iterative index scans can be enabled; when the proportion of rows matching the filter condition is high, an approximate index may be more suitable.
  • Vector elements must be finite values (NaN, Infinity, -Infinity are not allowed); vector supports up to 16,000 dimensions, but index dimensions are limited by type.
  • Index build time is affected by maintenance_work_mem, the number of parallel workers, and index options; HNSW vacuum may be slow, and reindexing first can help.
  • In production environments, it is recommended to create indexes concurrently to avoid blocking writes.
  • Specific performance tuning needs to be combined with data scale and hardware; the material does not provide fixed benchmarks or pricing information.