pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: OmkarPalika (@OmkarPalika) <noreply+OmkarPalika@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #846: Unexpected use of Index Scan in ANN query without LIMIT or enable_indexscan
Date: Fri, 25 Sep 2026 20:20:13 +0000
Message-ID: <gh-pgvector-pgvector-846-c5839008636@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-846@github.com>
References: <gh-pgvector-pgvector-846@github.com>

@ankane, in May 2025 you said on this issue that you would want a framework in place for better testing cost estimation before changing the HNSW cost. I built something adjacent while benchmarking filtered recall, so I am posting the measurements in case they are useful here.

PostgreSQL 17, pgvector 0.8.6, 1M rows, HNSW `m=16, ef_construction=64`. Stepping filter selectivity in 0.1% increments to find where the planner switches between a `bitmap+sort` exact plan and HNSW:

| filter column | `pg_stats.correlation` | exact plan wins up to |
|---|---|---|
| `id` | 1.00 | ~9,670 rows |
| `bucket_corr` | 0.10 | ~1,000–2,000 rows |
| `bucket_multi` | -0.01 | ~1,000–2,000 rows |
| `bucket` | -0.00 | ~1,000–2,000 rows |

The threshold is not a row count, and not a fraction of the table. It tracks heap pages touched: 9,669 sequential ids occupy a short contiguous run, while 1,000 scattered rows touch about 970 pages, and the bitmap heap scan is priced accordingly.

`CLUSTER` on the filter column confirms that reading. `bucket_corr` moves to ~8,000–9,000 rows and `id` drops to ~1,522 — the two columns swap physical ordering and their thresholds swap with them, while the two untouched control columns do not move at all.

The part that may be relevant to #1031: on a filter that correlates with embedding position, the plan flip and a silent wrong answer are the same event. At 0.1% selectivity the exact plan returns a full 10 rows; at 0.2% the planner switches to HNSW and returns **0 rows**, as does every larger selectivity tested. No error. A `SELECT 1 FROM items WHERE <filter> LIMIT 1` existence probe returns in 0.175ms in exactly that case, so the matching rows are present and cheap to reach, just not through the graph.

So at least one part of the cost estimation problem is not about queries that need every row. It is that the HNSW cost does not reflect how many candidates survive the predicate, and the switch lands precisely where survival goes to zero. These queries all carry a `LIMIT`, so a `tuple_fraction` test would not catch them — not an argument against the PR, just a case it does not cover.

Harness and full data: https://github.com/OmkarPalika/pgvector-filtered-recall — every measurement asserts its plan with EXPLAIN before timing, and ground truth comes from an exact scan in the same database rather than a separate pipeline.

If a cost estimation test framework is still wanted, I am happy to run whatever configurations would be useful.


view thread (8+ messages)

Message-ID: <gh-pgvector-pgvector-846-c5839008636@github.com>
Permalink:  ../gh-pgvector-pgvector-846-c5839008636@github.com/

 · 


This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox