Why Did HNSW Return Fewer Than k Rows?

“The category filter matches plenty of rows, but LIMIT 10 gives me six as soon as HNSW is involved.”

That is a believable user report. It is not, by itself, proof that HNSW lost four rows. LIMIT 10 is a ceiling, and several mechanisms can make the result stop below it. The fastest route to an explanation is to keep competing hypotheses alive long enough to disprove them.

Start a hypothesis ledger

Write the result count and the exact query at the top. Underneath, give each plausible cause one confirming observation and one way it could be ruled out. This keeps a tuning change from becoming the diagnosis.

H1 — The predicate does not have ten indexable rows. A relational count may show ten matching records while some have NULL vectors. With cosine distance, zero vectors are also excluded from the index. Count predicate matches, then separately count rows whose vectors can participate in this distance search. H1 weakens only when both counts support the requested k.

H2 — The query is not taking the HNSW path being blamed. Capture EXPLAIN for the exact distance operator, filter, order, and limit. The existence of an index is not evidence that PostgreSQL chose it. If the plan is exact, an approximate-search explanation does not fit this run.

H3 — A bounded HNSW candidate set is being thinned by the filter. This becomes credible when enough indexable rows satisfy the predicate, the plan uses HNSW, and a larger exploration budget raises the surviving count. It becomes weaker when an exact control also returns fewer than k or when count changes track data eligibility rather than search breadth.

H4 — Iterative scanning is unavailable or reaches its ceiling. Version matters. Iterative scans arrived in pgvector 0.8.0, and even then they stop when enough filtered rows are found or a configured scan limit is reached. “Iterative” is not “unbounded.”

H5 — Maintenance is reducing the available candidates. The documentation identifies dead tuples as another reason an HNSW query can return fewer rows. Check the dead-tuple and maintenance context before attributing the short set to search breadth.

The ledger should include the pgvector version, chosen plan, predicate count, indexable-row count, returned count, query latency, and relevant HNSW settings. Add one row per experiment. Do not erase a hypothesis when it loses; mark what observation weakened it.

Why H3 is plausible

The current pgvector documentation says that filtering with an approximate index happens after the index scan. HNSW explores candidates, then PostgreSQL applies the predicate. If only a fraction of explored candidates pass, the final set can be smaller than k even though more qualifying rows exist elsewhere in the table.

The documentation gives a deliberately compact illustration: when a condition matches 10 percent of rows and hnsw.ef_search is at its documented default of 40, about four matches survive on average. That is intuition, not a capacity formula. Graph structure, distribution, dead tuples, and the actual predicate still matter.

hnsw.ef_search controls the dynamic candidate-list size. Increasing it can improve recall at a speed cost. A transaction-scoped experiment with SET LOCAL hnsw.ef_search = ... is useful because it changes one run without silently changing every session. The value should be chosen as a test variable, not copied from an example and promoted to a production answer.

An informative comparison holds the query, data snapshot, and predicate constant. Capture the plan, returned count, and latency at the baseline and at one larger ef_search. If the result grows, H3 gains evidence. If it does not, resist the urge to keep doubling the number; H1, H2, H4, and H5 are still open.

Iterative scans change the experiment, not the need for one

From pgvector 0.8.0 onward, an iterative HNSW scan can continue until it finds enough filtered results or hits a limit. Strict ordering preserves exact distance order. Relaxed ordering can explore for better recall with slight ordering differences; the documented materialized-CTE pattern can restore a final sort, with a PostgreSQL 17-specific + 0 detail.

That choice belongs beside the product requirement. If exact distance order is contractual, relaxed order is not a free recall switch. If a small amount of disorder is acceptable, test it explicitly and sort as required rather than letting an engine setting redefine the response unnoticed.

The ceiling deserves its own line in the ledger. hnsw.max_scan_tuples and hnsw.scan_mem_multiplier bound how far the scan can go and how much memory it can use. A run that stops below k after exhausting a budget has not shown that neighbors do not exist; it has shown that this search did not reach them within that budget.

Keep the conclusion smaller than the experiment

“Higher ef_search returned ten rows once” is a result, not yet an explanation. A stronger conclusion names the mechanism it supports, the alternatives still open, and the cost observed in the same run. It also keeps the exact-search control nearby: pgvector is exact by default, while an approximate index exchanges some recall for speed.

The useful output is not a magic setting. It is a reproducible account of why this predicate, plan, version, and exploration budget produced fewer than k rows.

Next test — hypothesis: ______ | observation: ______ | elimination condition: ______

I would separate the diagnostic experiment from the production control. A larger
ef_search value or an iterative scan can be useful evidence, but making it a
global default also changes latency and resource use for query classes that may
not have the filtered-candidate problem.

For a bounded rollout, identify the query shape first and record requested_k,
returned_count, the chosen plan, filter identity, pgvector version, relevant
HNSW settings, latency, and whether a comparable exact control found at least k
indexable matches. Compare like with like: a change in predicate selectivity or
data snapshot should start a new series rather than count as a tuning result.

Predeclare two stop conditions. Stop or roll back when the deeper search crosses
the service’s latency or resource boundary. Also stop increasing the budget when
the short-result rate does not improve under comparable conditions; that result
points back to vector eligibility, plan choice, dead tuples, or another open
hypothesis. The useful control is therefore not “keep raising ef_search until
ten rows appear.” It is “spend a measured search budget on the affected query
class while the observed recall signal improves within the service boundary.”