// what happens
pgvector's README, in its Filtering section:
hnsw.ef_search of 40, only 4 rows will match on average."An HNSW search keeps a dynamic candidate list of hnsw.ef_search entries, "40 by default". Those are approximate nearest neighbours, not a guaranteed top forty. Postgres then applies your WHERE clause to that list. If one row in ten passes the filter, about four survive on average (assuming the filter has nothing to do with vector closeness), however large your LIMIT is. The pickier the filter, the fewer you get.
It only happens when the planner picks the vector index for the query. If it uses a B-tree on the filter column instead, the search is exact.
The fix: iterative index scans
From the README: "Starting with 0.8.0, you can enable iterative index scans, which will automatically scan more of the index until enough results are found (or it reaches hnsw.max_scan_tuples or ivfflat.max_probes)." The CHANGELOG dates 0.8.0 to 2024-10-30. In the latest release, 0.8.7 (2026-10-01), src/hnsw.c still registers hnsw.iterative_scan with the boot value HNSW_ITERATIVE_SCAN_OFF. It is off by default.
Two modes. "Strict ensures results are in the exact order by distance." "Relaxed allows results to be slightly out of order by distance, but provides better recall." Relaxed is a trade, not an upgrade. The README shows how to restore exact order with a materialized CTE. IVFFlat supports relaxed_order only.
The scan is capped: "Specify the max number of tuples to visit (20,000 by default)" with hnsw.max_scan_tuples, because "scanning a large portion of an approximate index is expensive". A very selective filter can still hit the cap and come back short. If you run on a managed host, check which pgvector version it has: iterative scans need 0.8 or newer.
Filtering per customer? Index or partition.
Iterative scans are not the sturdiest fix for a filter like WHERE customer_id = $1. The README's advice: "A good place to start is creating an index on the filter column. This can provide fast, exact nearest neighbor search in many cases." If you filter by only a few distinct values, consider a partial HNSW index per value. "If filtering by many different values, consider partitioning."
And for multi-tenant apps: "sharing an approximate index between tenants means vectors from one tenant can affect recall (and speed) for other tenants. For tenant isolation, use list partitioning or separate tables."
Other reasons you get fewer rows
The filter isn't the only cause. The README's troubleshooting entry: results are limited by hnsw.ef_search, and "there may be even less results due to dead tuples or filtering conditions in the query". NULL vectors are not indexed, nor are zero vectors for cosine distance. A LIMIT above ef_search comes back short even without a filter. You can raise hnsw.ef_search for "better recall at the cost of speed".
The four-line cheat sheet
ef_search = 40).2. Pickier filter = fewer results.
3. Turn on iterative scans (0.8+, default off).
4. Per-customer: index the column or partition.
The earlier comparison this follows up: vector DB, or just Postgres?
Sources: pgvector README (Filtering, Multitenancy, Iterative Index Scans, Troubleshooting) at the v0.8.7 tag; pgvector CHANGELOG; pgvector src/hnsw.c and src/hnsw.h at v0.8.7; the pgvector 0.8.0 announcement on postgresql.org. All verified 2026-10-16.
One concept a week. Free.
The deeper, copy-paste version of each ToolCall short — in your inbox.
// total: 0.00 · spam: void · unsubscribe: one click
