Alle Engineering Notes

Unsere Engineering Notes erscheinen auf Englisch und Niederländisch. Unten sehen Sie die englische Ausgabe.

Data · PostgreSQL & Retrieval7 Min. Lesezeit

Stay on pgvector or run a dedicated vector database?

Most teams adopt a dedicated vector database twelve months too early — inheriting dual-write drift, a second backup pipeline, and ACL mismatches for a workload PostgreSQL could serve in single-digit milliseconds.

The hidden tax of introducing a second state engine

Whenever an engineering team starts building retrieval-augmented generation (RAG) or semantic search, the default impulse is to provision a brand-new vector database alongside PostgreSQL. Benchmarks look effortless on a static million-row dataset — until real production mutations arrive.

The moment embeddings live in a separate store from your source-of-truth relational rows, you own distributed state synchronisation: outbox workers or CDC pipelines, tombstone cleanup when documents are deleted under GDPR erasure requests, a second backup and point-in-time recovery procedure, and — worst of all — permission drift when a user loses row-level access in PostgreSQL while stale embeddings remain queryable in the vector index.

Where pgvector HNSW wins — and the exact SQL pattern where it loses

Up to roughly five to twenty million embeddings (depending on dimensionality and quantization), PostgreSQL with pgvector HNSW indexes keeps transactional metadata, row-level security, and vector similarity inside a single ACID boundary. One backup, one connection pool, one set of roles.

Where teams get burned on pgvector is selective metadata filtering combined with unindexed HNSW memory eviction. Consider a multi-tenant retrieval query where one customer owns 0.2% of the table:

Diagnosing filtered HNSW recall & buffer hits in PostgreSQL
SET LOCAL hnsw.ef_search = 80;
SET LOCAL hnsw.iterative_scan = 'relaxed_order';

EXPLAIN (ANALYZE, BUFFERS)
SELECT d.id, d.title, (c.embedding <=> $1::halfvec(1024)) AS distance
FROM document_chunks c
JOIN documents d ON d.id = c.document_id
WHERE c.tenant_id = $2
  AND d.access_scope = ANY($3)
ORDER BY c.embedding <=> $1::halfvec(1024)
LIMIT 20;
  • Use halfvec (float16) or scalar quantization before buying bigger RAM. Storing 10 million 1,024-dimension vectors in float32 consumes ~40 GB of raw vector pages before HNSW graph links (~16–24 GB extra). Switching to halfvec(1024) cuts the index footprint roughly in half with negligible recall loss on modern embedding models, keeping the HNSW graph resident in shared_buffers / OS page cache.
  • Enable iterative scans (pgvector 0.8+) or partition by tenant/domain. Without iterative scans, a top-k HNSW traversal with a selective WHERE filter can return fewer than k rows — or force Postgres into a slow sequential scan. Partitioning hot multi-tenant tables by tenant bucket or enabling hnsw.iterative_scan prevents silent recall collapse.
  • Watch write amplification and autovacuum. HNSW graph inserts and frequent document re-chunking generate heavy WAL volume and dead tuples. If autovacuum_vacuum_scale_factor and maintenance_work_mem stay at install defaults, index bloat and read latency climb week over week.

Check Elasticsearch and Redis before adding a fourth system

Start with what your team already operates at 3 a.m. If you already run Elasticsearch for full-text search, its native HNSW vector fields and reciprocal rank fusion (BM25 lexical + dense vector hybrid search) often outperform pure vector similarity on exact product SKUs, legal clause numbers, and error codes without introducing a new cluster. Likewise, if Redis is already in your stack for low-latency session or feature state, bounded vector caches can live there cleanly.

The architectural cut-line: when Qdrant, Milvus, or Weaviate earn their place

Dedicated vector engines are genuinely strong engineering — once your workload hits architectural limits that justify owning a separate stateful cluster:

Decision matrix: keep retrieval in PostgreSQL vs. split into a dedicated vector cluster
ConstraintStay on PostgreSQL + pgvectorMove to Qdrant / Milvus / Weaviate
Corpus scaleUnder ~10M–20M vectors (with halfvec / partitioning)50M–500M+ vectors requiring distributed sharding
Filtering & ACLsRelational JOINs + strict transactional row-level ACLsHigh-QPS payload-indexed filtering decoupled from OLTP locks
Write patternModerate ingestion; scheduled re-embedding batchesContinuous high-throughput streaming upserts saturating OLTP WAL
Memory budgetHNSW index fits in RAM alongside OLTP working setRequires disk-backed mmap / PQ / binary quantization tiers

Measure your real query plans, buffer hit ratios, and write churn first. Tuning an existing PostgreSQL or Elasticsearch cluster takes days; unwinding a premature distributed database takes quarters.

Slow vector retrieval or a database migration nobody wants to touch?

Our 1-day Database Bottleneck Diagnosis delivers EXPLAIN (ANALYZE, BUFFERS) evidence, engine parameter diffs, and a written Tune, Scale, or Migrate decision in git.