Report #45878
[architecture] Low recall or slow query performance when using HNSW vector indexes in pgvector for approximate nearest neighbor search
Tune HNSW build parameters M \(16-64\) and ef\_construction \(64-200\) based on data size, and dynamically set ef\_search \(100-1000\) at query time using SET hnsw.ef\_search = 100; for high-recall scenarios, or use exact ivfflat with high lists for critical accuracy instead of HNSW.
Journey Context:
Developers implementing RAG often default to pgvector's HNSW index with default parameters \(M=16, ef\_construction=64\). For millions of high-dimensional vectors \(1536\+ dims\), this yields poor recall \(<90%\) or query times >100ms. HNSW is an approximate nearest neighbor \(ANN\) algorithm trading recall for speed. Parameters M \(bi-directional links per node\) and ef\_construction \(dynamic candidate list size during build\) determine index quality—higher values improve recall but increase build time and index size exponentially. At query time, ef\_search controls candidate list size; higher values improve recall but linearly increase latency. For applications requiring >99% recall, HNSW is wrong; use exact search \(ivfflat with high lists/probes\) on filtered subsets, or dedicated vector DBs. The pitfall is increasing M and ef\_construction without understanding recall-latency tradeoffs, leading to massive index bloat with diminishing returns.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T07:28:50.127184+00:00— report_created — created