Skip to main content

What is pgvector, and when should you use it for AI applications?

Summary

  • pgvector is an open-source Postgres extension for vector similarity search. It adds a vector column type, distance operators and approximate nearest neighbor indexes, so embeddings live in ordinary tables next to the data they describe.
  • Use pgvector when an AI application's embeddings belong to rows it already keeps in Postgres: documents for retrieval-augmented generation, an agent's past interactions, product catalogs, support tickets.
  • Choose HNSW for the better speed-recall tradeoff and IVFFlat for faster builds and less memory, and add filtering, partitioning or iterative scans when queries carry WHERE clauses.
  • For agent workloads, the place to run pgvector is Lakebase, the serverless Postgres database in the Databricks Data + AI Platform. It supports pgvector, and Lakebase Search (generally available) adds lakebase_vector, which is drop-in compatible with pgvector.

What is pgvector, and when should you use it for AI applications?

pgvector is an open-source extension that adds vector similarity search to Postgres. You enable it with CREATE EXTENSION vector, add a vector(n) column to a table, and query nearest neighbors with SQL operators such as <=> for cosine distance. Use it when your AI application's embeddings describe data you already store in Postgres, because a similarity search can then join, filter and run inside the same transaction as the rest of your queries. For AI agents, run it on Lakebase, the serverless Postgres database in the Databricks Data + AI Platform.

How pgvector works

By default pgvector performs exact nearest neighbor search, which the project describes as giving "perfect recall." Adding an index switches to approximate search, which trades some recall for speed.

Feature What pgvector provides
Vector types vector (single precision), halfvec (half precision), bit (binary) and sparsevec (sparse)
Distance operators <-> L2, <#> negative inner product, <=> cosine, <+> L1, plus Hamming and Jaccard for binary vectors
HNSW index A multilayer graph with the better speed-recall tradeoff; slower to build, uses more memory, needs no training step
IVFFlat index Divides vectors into lists and searches the closest ones; faster to build and lighter on memory; create it after the table has data
Filtering Standard Postgres indexes on filter columns, partial indexes, partitioning, and iterative index scans for approximate indexes
Scaling Half-precision indexing and binary quantization for smaller indexes; replicas for read scaling

pgvector is released under the PostgreSQL License and works from any language with a Postgres client. Because it writes through the write-ahead log, its README notes it supports replication and point-in-time recovery like any other Postgres data.

When to use pgvector for AI applications

pgvector fits best when retrieval is part of an application rather than a separate product:

  • Retrieval-augmented generation over application data. Chunks, their embeddings and their source metadata sit in one schema, so a query can restrict results to a customer, a date range or a document type in the same statement.
  • Recall of past interactions. An agent keeps summaries of earlier interactions with an embedding column and looks up similar ones before acting.
  • Semantic search inside a product. Catalogs, tickets and knowledge bases get search by meaning without leaving the database that serves them.
  • Multitenant AI features. The README recommends list partitioning or separate tables when tenants need isolation, so one tenant's vectors don't affect another's recall.

Two signals suggest adding more than plain pgvector. If exact terms such as product codes or names matter, pair it with keyword search; the README points to Postgres full-text search combined through Reciprocal Rank Fusion. If indexes grow large, its own guidance is half-precision vectors, binary quantization and re-ranking.

How Lakebase approaches pgvector

On Lakebase, vector is a listed, supported extension, so every statement on this page works on a Lakebase database as written.
When you want an index built for Lakebase, enable Lakebase Search, generally available since September 2026. Its lakebase_vector extension is drop-in compatible with pgvector: same types, operators and query syntax. CREATE EXTENSION lakebase_vector CASCADE pulls in pgvector as a dependency, and a vector column can take a lakebase_ann index, as in the Lakebase Search docs. The companion lakebase_text extension covers the keyword half with BM25 ranking.
The database around the extension is what makes Lakebase the pick for agents:

  • Spin up per tenant or agent. The README's isolation advice, separate tables per tenant, extends naturally to separate databases or branches, which Lakebase's generally available API, CLI and SDKs can create from code.
  • Spin down safely. Idle compute scales to zero, and Lakebase Search indexes "are storage-backed and survive scale-to-zero without warmup."
  • Branch before a model switch. A copy-on-write branch holds an isolated copy of production for trying a new embedding model, and a branch reset returns it to the parent's current state.
  • Connect back to the lakehouse. Synced tables bring Unity Catalog tables into Lakebase and can map an embedding column to vector(n), so embeddings computed in the lakehouse become searchable next to operational data.

FAQs

Is pgvector a vector database?

It's a Postgres extension that gives Postgres vector database capabilities. Vectors are a column type, so they share Postgres transactions, backups and access control with the rest of the data.

Should I use HNSW or IVFFlat?

The pgvector README says HNSW has the better speed-recall tradeoff but slower build times and higher memory use, while IVFFlat builds faster and uses less memory. Build an IVFFlat index only after the table has data.

Does Lakebase support pgvector?

Yes. The Lakebase product page states that "Lakebase supports extensions like PostGIS and pgvector," and vector appears in the supported extensions list. Lakebase Search's lakebase_vector is a drop-in compatible option for the same queries.

The information provided herein is for general informational purposes only and may not reflect the most current product capabilities or configurations.