All posts

pgvector vs a dedicated vector database: when Postgres is enough for RAG

Should your AI product use pgvector or a dedicated vector database? The real decision points: scale, filtering, operations and migration cost, with limits taken from the pgvector docs.

CodeKrypt 5 min read

Every RAG project hits the same early question: do we add a vector database, or put embeddings in the Postgres we already run? The honest answer for most teams starting out is that pgvector vs a dedicated vector database is not a performance contest yet. It is an operations and risk decision, and the performance question only becomes real at a scale most products reach later than they expect.

This post lays out what pgvector actually gives you, where its documented limits are, and the signals that justify moving to a dedicated engine. Facts about pgvector come from its official README. Where I describe vendor benchmarks, I do not quote their numbers, because they are run on specific hardware by parties who sell one side of the comparison.

What does pgvector give you out of the box?

pgvector adds vector types and nearest-neighbour search to Postgres. Per its README it supports two approximate index types, HNSW and IVFFlat, and several vector types:

TypeMax dimensions you can indexUse
vector2,000Standard float32 embeddings
halfvec4,000Half precision, smaller index
bit64,000Binary quantization
sparsevec1,000 non-zero elements (HNSW only)Sparse embeddings

Vectors can be stored up to 16,000 dimensions, but only the sizes above can be indexed. Most mainstream embedding models fall well inside that, though you should check the dimension of the model you choose.

The practical benefit is not speed. It is that your embeddings sit in the same database as the rows they describe. One backup, one set of credentials, one migration tool, and a write that updates a document and its embedding in one transaction. For a team without dedicated infrastructure staff, that removes a whole system from the pager rotation.

Where does pgvector get hard?

Filtered search

This is the limit that surprises people most. The README states that with approximate indexes, filtering is applied after the index is scanned. Its own example: if your WHERE clause matches 10% of rows, HNSW with the default hnsw.ef_search of 40 returns about 4 matching rows on average, not 40.

That matters enormously for multi-tenant products, where every query is filtered by customer. Version 0.8.0 added iterative index scans, which keep scanning until enough results are found or a cap is hit (hnsw.max_scan_tuples defaults to 20,000). The README also suggests partial indexes for filters with few distinct values and partitioning for many. If your filters are highly selective, test this on real data first. Our post on multi-tenant RAG data isolation covers the isolation side of the same problem.

Memory and index builds

HNSW indexes perform best when they fit in memory, so index size and shared_buffers become capacity-planning inputs. Quantized types (halfvec, binary with re-ranking against the original vectors, as the README recommends) reduce size at some cost in recall. None of this is exotic, but it is Postgres tuning, so someone has to own it.

Everything shares one database

Heavy vector queries compete with your transactional workload for CPU, memory and I/O. A read replica dedicated to retrieval is the usual answer, and it is also the point where you are already running a second database system, just a familiar one.

What does a dedicated vector database add?

Dedicated engines such as Pinecone, Qdrant, Weaviate or Milvus are built around the vector workload, so they typically offer:

The costs are real too: another system to deploy, secure and monitor; data duplicated from your source of truth, with sync logic that can drift; and, for proprietary managed services, a migration project if you ever leave.

How should you decide?

SituationLean towards
Already on Postgres, early product, modest corpuspgvector
Small team, no one to run another datastorepgvector or a fully managed service
Strict per-tenant filters on every query, large corpusTest pgvector with iterative scans; consider a dedicated engine if recall suffers
Corpus growing past what one Postgres node serves comfortablyDedicated engine or sharded setup
Need vector search to scale independently of your app databaseDedicated engine or a retrieval read replica
Strong data-residency or self-hosting requirementpgvector or a self-hosted engine such as Qdrant

The deciding evidence is always a measurement on your own data. Build a set of representative queries with known relevant documents (the same evaluation set you need for hybrid search and reranking), then record recall and latency under the filters your product really uses. A vendor chart tells you about the vendor's dataset.

How do you avoid getting locked in either way?

Three habits keep the decision reversible:

  1. Keep source text and embeddings in your own storage. Treat the vector index as a rebuildable cache, not the system of record.
  2. Put retrieval behind one interface in your code: search(query, filters, k). Swapping the backend then touches one module.
  3. Record the embedding model and version with every vector. When you change models you must re-embed everything, regardless of which database you use. The same discipline applies to generation models, as covered in planning an LLM model migration.

What to do next

Start with pgvector if you already run Postgres, and write down the trigger that would make you move: for example, filtered recall below a target you set, p95 retrieval latency above your budget, or index size that no longer fits your memory plan. Having that trigger written down turns a vague worry into a measurable condition.

If you are choosing a retrieval architecture for an AI product and want a second opinion before committing, see how we approach AI SaaS development and get in touch.

CodeKrypt avatar

CodeKrypt

Drafted by CodeKrypt's AI writing pipeline, built and maintained by our engineers, under fixed rules on sources and claims. How we write

Frequently asked questions

Do I need a dedicated vector database for RAG?
Often not at the start. If you already run Postgres, pgvector gives you HNSW and IVFFlat indexes next to your relational data, with one backup, one access model and transactional writes. Move when a measured limit, not a hunch, says so.
How many vectors can pgvector handle?
There is no single number. It depends on dimensions, memory, index settings and the recall you need. Published benchmarks are mostly run by vendors on specific hardware, so load a realistic sample of your own data and measure recall and latency.
Why is filtered vector search tricky in pgvector?
With approximate indexes the filter is applied after the index is scanned, so a selective WHERE clause can return fewer rows than you asked for. Iterative index scans, added in pgvector 0.8.0, scan further until enough results are found.
Is switching vector databases later hard?
It is manageable if embeddings and source text stay in your own storage and retrieval sits behind one interface. It is painful if the vector store is the only place the data lives.

Related reading

Chat on WhatsApp