Skip to content
mentoor

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?

Choose one answer

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) and ef_construction (candidate list while building, default 64). Query option: hnsw.ef_search (default 40).
  • IVFFlat clusters vectors into lists groups 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 suggests lists of rows / 1000 up to 1 million rows.

How to tune

  1. Measure first. Take 100 real queries, get exact top-10 results with the index disabled, and treat them as ground truth.
  2. Run the same queries with the index and compute recall@10.
  3. Raise ef_search (or probes) until recall reaches the target, then check latency. Both rise together.
  4. Only if the dial cannot reach the target in the latency budget, rebuild with a higher m or ef_construction (or more lists).

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_ops is ignored by <=> queries.
  • Tuning the build before the dial. Rebuilding takes hours; changing ef_search takes 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_search or ivfflat.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?

Choose one answer

Check yourself · 3 of 3

Why must an IVFFlat index be created after the table holds data?

Choose one answer

Practice items, not counted towards your mastery.

Start here