Sub topic 12
Tuning m, ef_construction, ef_search, lists, and probes
This chapter assumes you can already create an HNSW or IVFFlat index and run an ORDER BY embedding <=> ... LIMIT k query. It also assumes you know that approximate indexes can miss true neighbors. Here you will learn which settings are fixed when an index is built, which can change on every query, and how to move each one deliberately.
Build-time and query-time parameters
pgvector exposes two kinds of settings.
- Build-time parameters are stored in the index. Changing them means running
CREATE INDEXagain. HNSW hasmandef_construction. IVFFlat haslists. - Query-time parameters are ordinary session settings. You can change them with
SETand no rebuild. They arehnsw.ef_searchandivfflat.probes.
| Parameter | Index | Set when | Default | Larger value means |
|---|---|---|---|---|
| `m` | HNSW | Build | 16 | More links per node, better recall, bigger index, slower build |
| `ef_construction` | HNSW | Build | 64 | Better graph quality, slower build |
| `hnsw.ef_search` | HNSW | Query | 40 | Better recall, slower query |
| `lists` | IVFFlat | Build | 100 in the docs' examples (you must choose) | Smaller clusters, faster scans per probe, lower recall at a fixed probe count |
| `ivfflat.probes` | IVFFlat | Query | 1 | Better recall, slower query |
Tune the query-time settings first. They are cheap to change and show how much recall you can gain before you rebuild anything.
Tuning HNSW
HNSW builds a layered graph. Each row becomes a node linked to nearby nodes. A query walks the graph from the top layer down to the bottom.
m
m is the maximum number of links each node keeps per layer. A higher m gives the search more routes to the true neighbors, which matters most for high-dimensional or clustered data. The cost is a larger index and a slower build. Values between 16 and 48 cover most workloads. Start with the default of 16 and increase it only if raising ef_search stops improving recall.
ef_construction
ef_construction is the size of the candidate list used while inserting each row into the graph. A larger list finds better links, so the finished graph has better recall at any ef_search. It slows the build and every later insert. It must be at least twice m, and the maximum is 1000.
CREATE INDEX items_embedding_hnsw
ON items USING hnsw (embedding vector_cosine_ops)
WITH (m = 24, ef_construction = 128);hnsw.ef_search
hnsw.ef_search is the size of the candidate list during a query. Raising it makes the search explore more of the graph. The range is 1 to 1000.
SHOW hnsw.ef_search; -- 40 by default
SET hnsw.ef_search = 100; -- applies to the rest of this sessionAn index scan returns at most ef_search rows. A query with LIMIT 100 against the default of 40 can therefore return only 40 rows, and no error tells you so. Keep ef_search at or above your largest LIMIT.
Tuning IVFFlat
IVFFlat runs k-means clustering over the rows that exist at build time and stores one centroid per cluster. Each cluster is a list. A query finds the closest centroids and scans only those lists.
lists
lists is the number of clusters. The pgvector documentation suggests these starting points:
- Up to about 1 million rows:
rows / 1000. - Above 1 million rows:
sqrt(rows).
More lists mean fewer rows per list, so each probe is faster, but a true neighbor is more likely to sit in a list you did not probe. Because the centroids come from the data present at build time, build the index after loading a representative set of rows. An index built on an empty or tiny table has poor centroids and gives poor recall, and the only fix is to rebuild it.
ivfflat.probes
ivfflat.probes is how many lists a query scans. The default of 1 is fast and often has poor recall. A reasonable starting value is sqrt(lists). Setting probes equal to lists scans every list, which is an exact search that is slower than a sequential scan.
CREATE INDEX items_embedding_ivfflat
ON items USING ivfflat (embedding vector_l2_ops)
WITH (lists = 316);
SET ivfflat.probes = 18;Scoping settings to one query
A plain SET lasts for the whole session. In a connection pool, that leaks into the next client that borrows the connection. Use SET LOCAL inside a transaction so the value ends with the transaction.
BEGIN;
SET LOCAL hnsw.ef_search = 200;
SELECT id, content
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 10;
COMMIT;This lets you give a high-recall setting to an important query and keep a cheaper setting for everything else.
A repeatable tuning procedure
- Pick a sample of 100 or more real query vectors.
- Compute the exact top-k for each sample, as the recall chapter shows, by running with
SET LOCAL enable_indexscan = off. - Run the same queries through the index at several settings, for example
ef_searchof 40, 80, 160 and 320. - Record recall (overlap with the exact results) and median and 95th percentile latency for each setting.
- Choose the lowest setting that meets your recall target. If no setting meets both recall and latency targets, rebuild with larger build-time parameters and repeat.
To see the effect on one query, compare plan timing:
BEGIN;
SET LOCAL hnsw.ef_search = 200;
EXPLAIN (ANALYZE, BUFFERS)
SELECT id FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 10;
COMMIT;The plan should contain an Index Scan using items_embedding_hnsw. If it shows a sequential scan with a sort, the planner ignored the index, so the settings you are tuning have no effect.
Interactions and edge cases
- Filters. With a
WHEREclause, the index scan returns its candidates first and PostgreSQL filters them afterward. A selective filter can leave fewer thanLIMITrows. Raisingef_searchorprobesgives the filter more candidates to work with. Recent pgvector versions also offer iterative scans (hnsw.iterative_scanandivfflat.iterative_scan) that keep scanning until enough rows pass the filter. Check which version you run before relying on them. - Build resources. HNSW builds are much faster when the graph fits in
maintenance_work_mem. PostgreSQL logs a notice when it does not. Parallel builds usemax_parallel_maintenance_workers. The operations chapter covers these in detail. - Data drift for IVFFlat. Centroids do not move after the build. If the data distribution changes a lot, recall falls and you need to run
REINDEX. HNSW has no centroids, so it degrades less from drift. - Latency is not linear. Doubling
ef_searchdoes not double query time. Measure at your own scale, since the curve depends on dimensions, row count, and cache state.
Exercise
A table has 2 million rows and an IVFFlat index built with lists = 100 and the default probes. Queries are fast but users report missing obvious matches. What two changes would you try, and in what order?
Show answer
First raise ivfflat.probes (for example to about 10, then higher), because it needs no rebuild and the default of 1 scans only one list out of 100. Measure recall against exact results at each value. If recall is good only at probes values that make queries too slow, rebuild with more lists, around sqrt(2,000,000), which is about 1,414, and set probes to roughly sqrt(lists), about 38, as a starting point.
Exercise
You run SET hnsw.ef_search = 40 (the default) and a query with LIMIT 100 on an HNSW index. How many rows can you expect, and how do you fix it?
Show answer
At most 40 rows, because the index scan returns no more than efsearch candidates. Set hnsw.efsearch to at least 100, using SET LOCAL inside a transaction if you want to keep the change to that query.