Topic 2 of 5 · 6 min read
Approximate indexes: HNSW and IVF
Goal: Tune an HNSW or IVF index setting to hit a stated recall and latency target and explain the trade-off made.
An approximate index trades a little recall for a large speed gain, and the query-time setting (hnsw.ef_search or ivfflat.probes) is the dial you turn to buy recall back.
Check yourself · 1 of 3
What does recall mean for an approximate vector index?
Why it matters
An exact scan compares the query to every vector. At 50,000 chunks that is fast enough. At 5 million it is not. Approximate nearest-neighbour (ANN) indexes skip most vectors, so they can miss a true neighbour. Recall is the share of the true top-k that the index returns. A RAG system with 90% recall silently loses one relevant chunk in ten before the model ever sees it.
The two index types in pgvector
- HNSW builds a layered graph. A query walks the graph toward the neighbourhood of the query vector. It has better speed-recall behaviour and can be built on an empty table, but builds slower and uses more memory. Build options:
m(links per node, default 16) andef_construction(candidate list while building, default 64). Query option:hnsw.ef_search(default 40). - IVFFlat clusters vectors into
listsgroups and searches only the closest few. It builds faster and is smaller, but it must be built after the table holds data, because the clusters come from the data. Query option:ivfflat.probes(default 1). The README suggestslistsof rows / 1000 up to 1 million rows.
How to tune
- Measure first. Take 100 real queries, get exact top-10 results with the index disabled, and treat them as ground truth.
- Run the same queries with the index and compute recall@10.
- Raise
ef_search(orprobes) until recall reaches the target, then check latency. Both rise together. - Only if the dial cannot reach the target in the latency budget, rebuild with a higher
moref_construction(or morelists).
Before and after
-- Before: IVFFlat built on an empty table, default probes
CREATE INDEX ON chunks USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
Built before loading data, the clusters are meaningless, and probes = 1 searches one of 100. Recall is poor. After loading data:
CREATE INDEX ON chunks USING hnsw (embedding vector_cosine_ops) WITH (m = 16, ef_construction = 64);
SET hnsw.ef_search = 100;
What goes wrong
- No ground truth. "It feels fine" hides a recall of 80%.
- Wrong operator class. An index on
vector_l2_opsis ignored by<=>queries. - Tuning the build before the dial. Rebuilding takes hours; changing
ef_searchtakes one line.
Examples
Hitting 95% recall
Exact top-10 for 100 queries is the ground truth. With ef_search = 40 recall@10 is 0.88 at 6 ms. At 100 it is 0.96 at 11 ms. Choose 100: the target is met and 11 ms fits the budget. No rebuild needed.
The index that never gets used
A team builds USING hnsw (embedding vector_l2_ops), but queries use <=>. EXPLAIN shows a sequential scan. The fix is to rebuild with vector_cosine_ops, or to query with <->, whichever matches the embedding model.
Key points
- Approximate indexes can miss true neighbours; recall is the share of the exact top-k they return.
- HNSW is slower to build and larger but gives better recall at a given speed; IVFFlat builds faster but needs data in the table first.
- The query-time dial is
hnsw.ef_searchorivfflat.probes: higher means better recall and higher latency. - Measure recall against an exact scan of real queries before and after any change.
- The index operator class must match the query operator, or Postgres falls back to an exact scan.
Common mistakes
- Creating an IVFFlat index on an empty or tiny table.
- Reporting latency without reporting recall.
- Rebuilding the index for every tuning idea instead of changing the query-time setting first.
- Assuming an index was used without reading
EXPLAIN.
Check yourself · 2 of 3
An HNSW index returns recall@10 of 0.85 and the target is 0.95, with latency headroom. What is the first thing to try?
Check yourself · 3 of 3
Why must an IVFFlat index be created after the table holds data?
Practice items, not counted towards your mastery.
Start here
- pgvector: open-source vector similarity search for Postgres (opens in a new tab)ToolFree
The README lists the column type, operators, operator classes, HNSW and IVFFlat options and their defaults.
- Efficient and robust approximate nearest neighbor search using HNSW graphs (opens in a new tab)PaperFree
The original HNSW paper: how the layered graph works and why M and the search width trade recall for speed.
- Filtering (Qdrant documentation) (opens in a new tab)DocsFree
Shows how a dedicated vector database applies payload filters inside graph search.