Build the pgvector IVFFlat index after loading your vectors. Start with rows / 1000 lists through one million rows or sqrt(rows) above that, then test probes around sqrt(lists). Verify the query plans and choose the lowest probes setting that meets both your recall and p95 latency targets. pgvector v0.8.2 README
What do lists and probes actually control?
An IVFFlat index groups vectors into lists during the build. The lists option sets how many groups the index will have. At query time, ivfflat.probes sets how many of the closest lists to search and defaults to one. More probes usually improve recall at the cost of more work per query. pgvector v0.8.2 README
Starting values by table size
Round the sizing formulas to whole numbers. The supported range is 1 to 32,768 lists, with 100 as the default if you omit the option. At one million rows, the formula gives 1,000 lists and the square root of 1,000 rounds to 32 probes. pgvector IVFFlat list limits
| Rows | Lists | Starting probes | Approx. rows searched per query |
|---|---|---|---|
| 100,000 | 100 | 10 | 10,000 |
| 500,000 | 500 | 22 | 22,000 |
| 1,000,000 | 1,000 | 32 | 32,000 |
| 5,000,000 | 2,236 | 47 | 105,000 |
| 10,000,000 | 3,162 | 56 | 177,000 |
| 100,000,000 | 10,000 | 100 | 1,000,000 |
The last column estimates rows × probes / lists assuming equally sized lists. Actual lists can vary in size.
Wiring it up
This example uses a loaded items table with an id and an embedding vector column. Use a query vector with the same dimensions as your column. The cosine IVFFlat index uses vector_cosine_ops, which matches the <=> operator in the ascending ORDER BY with a LIMIT. pgvector v0.8.2 README
CREATE INDEX items_embedding_ivf
ON items USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 1000);
SELECT id, embedding <=> '[3,1,2]' AS distance
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 10;
A regular index build blocks writes while it runs. If the table must stay writable, use CREATE INDEX CONCURRENTLY outside a transaction block. PostgreSQL 18 CREATE INDEX
Verify exact and indexed plans
Start with normal planner settings and use the same fixed corpus, query vectors, and LIMIT throughout. For this cosine example, use a corpus of non-null, nonzero embeddings with at least ten eligible rows. pgvector excludes null and zero vectors from cosine indexes. pgvector v0.8.2 README
Start with exact search. Look for a sequential scan on items and a sort by cosine distance below the limit. A parallel sequential scan can also provide exact ground truth. Save the IDs and distances for every query vector after confirming the plan. pgvector v0.8.2 README · PostgreSQL 18 Using EXPLAIN
BEGIN;
SET LOCAL enable_indexscan = off;
EXPLAIN (COSTS OFF)
SELECT id, embedding <=> '[3,1,2]' AS distance
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 10;
SELECT id, embedding <=> '[3,1,2]' AS distance
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 10;
COMMIT;
For the indexed pass, run the same query in a new transaction for each probes setting. For 1,000 lists, try 16, 32 and 64 as example settings. Keep iterative scans off so this sweep varies only the initial probes count. pgvector IVFFlat query settings
BEGIN;
SET LOCAL ivfflat.probes = 32;
SET LOCAL ivfflat.iterative_scan = off;
EXPLAIN (COSTS OFF)
SELECT id, embedding <=> '[3,1,2]' AS distance
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 10;
SELECT id, embedding <=> '[3,1,2]' AS distance
FROM items
ORDER BY embedding <=> '[3,1,2]'
LIMIT 10;
COMMIT;
For each query and probes setting, look for Index Scan using items_embedding_ivf on items. A sequential scan or a different index is a fallback. Mark it in the plan column and exclude that setting from the IVFFlat comparison if any query falls back. PostgreSQL 18 EXPLAIN
Both passes use transaction-local settings. At COMMIT, PostgreSQL restores the session settings for the next pass. PostgreSQL 18 SET
Compare recall@k and p95
- Recall@k. For each query, divide the number of matching exact and indexed IDs by
k, then average the scores. For example, seven matching IDs atLIMIT 10give that query a recall@10 of 0.70. If distances tie at the cutoff, count all returned rows at or within the exact kth distance. Recall benchmark guide - p95 latency. In
psql, enable\timing onand record the time for each plainSELECT. Sort the timings for each setting from fastest to slowest. Use the value at rankceil(0.95 × number of timings), counting from one. PostgreSQL 18 psql · PostgreSQL 18 aggregate functions - Short results. Count how many queries return fewer than
krows. Keep them in the recall calculation with the same denominator. Low probes, too little data at build time and post-scan filtering can all cause short results. pgvector v0.8.2 README
| Probes | Plan and index name | Mean recall@k | p95 (ms) | Short results (count) |
|---|---|---|---|---|
Pick the lowest tested probes value whose verified runs meet both targets. If 16 fails and 32 passes, test values between them to narrow the choice. If no setting qualifies, use the checks below before retuning. For broader tradeoffs, see HNSW vs IVF vector indexes.
When PostgreSQL bypasses IVFFlat
- Check the query shape. The cosine operator and operator class must match. Order by the raw distance in ascending order and keep the
LIMIT. Sorting by a computed cosine similarity instead prevents the distance index from supplying the order. pgvector v0.8.2 README - Check probes against lists. Keep probes below lists in the approximate sweep. At the full list count, pgvector documents that the planner skips the index. pgvector v0.8.2 README
- Check the planner’s choice. A small table can be faster to scan. In a separate diagnostic transaction, add
SET LOCAL enable_seqscan = off;before the sameEXPLAINto see whether the intended index is available to the planner. This discourages sequential scans, so still check the index name. Keep this diagnostic separate from the sweep with normal planner settings. pgvector v0.8.2 README · PostgreSQL 18 query planning
When should you rebuild an IVFFlat index?
Repeat the verified sweep if recall falls below target after the corpus changes. Consider rebuilding if the index was trained before enough data was loaded. If growth changes the starting list count materially, try a replacement index and run the same sweep against it. pgvector v0.8.2 README
To rebuild at the existing list count, run this outside a transaction block. PostgreSQL 18 REINDEX
REINDEX INDEX CONCURRENTLY items_embedding_ivf;
To try a different list count, create a replacement under a new name. Run this outside a transaction block. PostgreSQL 18 CREATE INDEX
CREATE INDEX CONCURRENTLY items_embedding_ivf_new
ON items USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 2236);
Keep the old index until EXPLAIN names items_embedding_ivf_new and the replacement meets your targets. If the planner keeps choosing the old index, test the replacement on a copy of the table with only the new index. Once verified on the target table, drop the old index outside a transaction block. PostgreSQL 18 EXPLAIN · PostgreSQL 18 DROP INDEX
DROP INDEX CONCURRENTLY items_embedding_ivf;
The SQL follows the pgvector v0.8.2 README and PostgreSQL 18 documentation. The examples were not executed.
Related across the network
- RAG Pipeline Architecture: Components and Build Order — ragstackguide.com
- RAG Chunking Strategy: Picking Chunk Size and Overlap — ragstackguide.com