🧭 · Models

pgvector

pgvector: store and search embeddings as a native column right beside your relational data in Postgres.

In one line

pgvector turns a Postgres column into a searchable vector store, so similarity search lives in the same database as your rows, filters, and joins.

ConceptWhat it is

pgvector is an open-source extension for PostgreSQL that adds a native vector data type plus distance operators and approximate-nearest-neighbor indexes. Instead of standing up a separate vector database, you run CREATE EXTENSION vector, add a vector column to an existing table, and query embeddings with ordinary SQL. It exists because most teams already run Postgres for their application data, and moving embeddings into that same engine means similarity search, relational filters, and joins happen in one place with one set of transactions, backups, and access controls.

The core idea is co-location: a document's embedding sits in the same row as its metadata (owner, tenant, tier, timestamps), so a semantic query can be constrained by a normal WHERE clause and combined with Postgres full-text search for hybrid retrieval. It supports multiple distance metrics (cosine, inner product, L2) and two index types, HNSW for high recall and IVFFlat for lower memory, letting you trade accuracy against cost.

How it worksThe mechanics

You embed your text with a model, store the resulting vector in a vector(n) column alongside the source row, and build an HNSW or IVFFlat index over that column for the chosen distance metric. At query time you embed the user's question, then run a single SQL statement that orders rows by the distance operator (for example ORDER BY embedding <=> $1 LIMIT k) while applying any WHERE filters and joins you need; the planner uses the approximate index to return the top-k neighbors, and recall is tuned through parameters like lists/probes for IVFFlat or m/ef_construction/ef_search for HNSW. New rows are embedded and inserted continuously, and an HNSW index updates incrementally as part of normal writes, while IVFFlat benefits from periodic reindexing to keep recall high.

At a glanceSee it

pgvector diagram

When to use itWhere it fits

  • You already run Postgres and want embeddings to live next to the relational data they describe, avoiding a second datastore to operate and sync.
  • Retrieval must be filtered or joined by exact business rules, such as tenant, permission, locale, or price, expressed cleanly as SQL WHERE clauses.
  • Corpus size is small-to-medium (thousands to low millions of vectors) where a single Postgres instance comfortably holds the index in memory.
  • You want transactional consistency, so an embedding and its source row are inserted, updated, or deleted atomically together.

When NOT to use itLimits & anti-patterns

  • You need to serve hundreds of millions or billions of vectors at very high query throughput, where a purpose-built engine's sharding and compression pay off.
  • Your team has no Postgres operational muscle, so a fully managed vector service would be less to run than tuning an index-heavy database.
  • You require advanced ANN features out of the box, such as native multi-vector reranking, learned quantization, or per-namespace autoscaling.
  • Write and reindex volume is so high that HNSW build times and memory pressure would degrade the same instance your app depends on.

Trade-offsAdvantages & costs

Advantages
  • One database for vectors and relational data means shared backups, replication, security, and a single transaction boundary.
  • Genuinely open source under the PostgreSQL license, self-hostable, and offered by every major managed Postgres provider, so there is no vendor lock-in.
  • Rich, exact filtering and joins for free, because retrieval is just SQL over your existing schema and full-text indexes.
  • Low incremental cost and learning curve when Postgres is already part of the stack.
Trade-offs & costs
  • Not purpose-built for extreme scale; performance and memory become the bottleneck well before dedicated vector engines do.
  • HNSW indexes are memory-hungry and slow to build, competing for resources with your transactional workload on the same box.
  • Recall and latency depend on hand-tuned index parameters, and getting lists, probes, or ef_search wrong quietly returns worse results.
  • Combining an ANN index with selective filters can force the planner into slower paths, so query plans need attention as data grows.

ExampleIn the real world

A support team stores knowledge-base articles in a Postgres table that already holds columns for product line, locale, and the customer plan tier each article applies to. They add an embedding vector(1536) column, backfill it by embedding each article, and build an HNSW index for cosine distance. For retrieval-augmented answers, the app embeds the incoming ticket and runs one query: SELECT id, body FROM articles WHERE locale = $1 AND plan_tier <= $2 ORDER BY embedding <=> $3 LIMIT 5. Because the filter and the similarity search execute together, an enterprise-only article never leaks to a free-tier user, and the same query can join usage tables to prefer articles about features the customer actually has. No separate vector service, sync job, or second consistency model is involved.

ToolsHow to implement it

  • pgvectorthe extension itself, providing the vector type, distance operators, and HNSW/IVFFlat indexes.
  • pgvectorscalea companion extension from Timescale adding a disk-friendly StreamingDiskANN index for larger corpora.
  • Managed Postgres hosts such as Supabase, Neon, AWS RDS/Aurora, and Google Cloud SQL, which ship pgvector preinstalled.
  • LangChainand LlamaIndex, whose PGVector integrations wire embedding and retrieval into a RAG pipeline.

Cost & effortWhat it takes

The software is free and open source, so the real cost is the Postgres instance that hosts it: CPU and, especially, RAM to keep HNSW indexes resident. Effort is low to adopt if you already operate Postgres, since it is one extension and a column, but meaningful tuning effort lives in choosing the index type and its build and search parameters, sizing memory, and watching query plans as the corpus grows. Expect the cost curve to stay flat and cheap at small-to-medium scale and then rise sharply, at which point either pgvectorscale or a migration to a purpose-built vector store becomes the economical move.

A living map of modern AI — kept current every morning