pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #916: Unintuitive Query Latency When Filtering
5+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #916: Unintuitive Query Latency When Filtering
@ 2025-10-26 19:58 "igm503 (@igm503)" <noreply+igm503@github.com>
0 siblings, 0 replies; 5+ messages in thread
From: igm503 (@igm503) @ 2025-10-26 19:58 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I'm curious about why postgresql with pgvector makes the decisions it does when doing a vector search with a filter condition.
I have a table with about 3 million vectors. I'm searching for similar items to a query item with a filter on how recently the searched-for items were added to the table. I've noticed the following unexpected pattern in query latencies:
| Date Range | LIMIT | Rows Scanned | Execution Time | Items in Filter |
|------------|-------|--------------|----------------|------------------|
| 1 month | 5 | 1,054 | 1,885 ms | 17,570 |
| 1 month | 40 | N/A* | 235 ms | 17,570 |
| 3 months | 5 | 125 | 438 ms | 82,583 |
| 3 months | 40 | 390 | 1,220 ms | 82,583 |
*Used parallel sequential scan instead of HNSW index
As expected, the bigger filter (3 months) takes longer with a bigger limit (40 vs 5). However, in the case of the 1 month filter query, I get the unexpected result that the query is faster when searching for the top 40 than when searching for the top 5. From what I can tell, this is because postgres, in the 1-month/top-5 case elects to NOT use the hnsw index, and instead do a parallel sequential scan on the vectors, which turns out to be better in this case, perhaps because of how restrictive the filter is.
But if this is true, then why not do the parallel sequential scan for the LIMIT 5 case as well, where it would be faster (~200ms vs ~2000ms). Any insights on why this happens, and if there's anything that can be done to improve latency with heavy filters like these?
I've included explain analyze outputs for the 4 queries mentioned below.
Note: example results below are from different paper ids (otherwise I get faster, cached results that break the comparison), but, but these numbers match the typical query latencies I see across most query ids.
## Top 5 from Past Month (gallery would be 17570 items after filtering)
```SQL
arxiv=# EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM papers_embedding
WHERE paper_id IN (SELECT id FROM papers_paper WHERE created >= NOW() - INTERVAL '1 month')
ORDER BY vector <-> (SELECT vector FROM papers_embedding WHERE id=0)
LIMIT 5;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=3037.40..10956.24 rows=5 width=80) (actual time=607.213..1884.950 rows=5 loops=1)
Buffers: shared hit=3552 read=15254
InitPlan 1
-> Index Scan using papers_embedding_pkey on papers_embedding papers_embedding_1 (cost=0.43..8.45 rows=1 width=18) (actual time=0.022..0.024 rows=1 loops=1)
Index Cond: (id = 2)
Buffers: shared hit=4
-> Nested Loop (cost=3028.95..24177685.44 rows=15264 width=80) (actual time=607.211..1884.944 rows=5 loops=1)
Buffers: shared hit=3552 read=15254
-> Index Scan using papers_embedding_vector_hnsw_idx on papers_embedding (cost=3028.52..21120796.15 rows=2859894 width=72) (actual time=159.400..1875.445 rows=1054 loops=1)
Order By: (vector <-> (InitPlan 1).col1)
Buffers: shared hit=1229 read=13321
-> Index Scan using papers_paper_pkey on papers_paper (cost=0.43..1.07 rows=1 width=8) (actual time=0.009..0.009 rows=0 loops=1054)
Index Cond: (id = papers_embedding.paper_id)
Filter: (created >= (now() - '1 mon'::interval))
Rows Removed by Filter: 1
Buffers: shared hit=2293 read=1923
Planning:
Buffers: shared hit=23
Planning Time: 0.718 ms
Execution Time: 1885.184 ms
(20 rows)
Time: 1886.508 ms (00:01.887)
```
## Top 40 from Past Month (gallery would be 17570 items after filtering)
```SQL
arxiv=# EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM papers_embedding
WHERE paper_id IN (SELECT id FROM papers_paper WHERE created >= NOW() - INTERVAL '1 month')
ORDER BY vector <-> (SELECT vector FROM papers_embedding WHERE id=1)
LIMIT 40;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=61782.75..61787.41 rows=40 width=80) (actual time=233.576..235.380 rows=40 loops=1)
Buffers: shared hit=130992 read=58713
InitPlan 1
-> Index Scan using papers_embedding_pkey on papers_embedding papers_embedding_1 (cost=0.43..8.45 rows=1 width=18) (actual time=0.011..0.012 rows=1 loops=1)
Index Cond: (id = 3)
Buffers: shared hit=4
-> Gather Merge (cost=61774.30..63258.40 rows=12720 width=80) (actual time=233.575..235.367 rows=40 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=130992 read=58713
-> Sort (cost=60774.27..60790.17 rows=6360 width=80) (actual time=221.682..221.684 rows=31 loops=3)
Sort Key: ((papers_embedding.vector <-> (InitPlan 1).col1))
Sort Method: top-N heapsort Memory: 34kB
Buffers: shared hit=130988 read=58713
Worker 0: Sort Method: top-N heapsort Memory: 34kB
Worker 1: Sort Method: top-N heapsort Memory: 33kB
-> Hash Join (cost=7377.10..60573.24 rows=6360 width=80) (actual time=109.677..220.883 rows=5857 loops=3)
Hash Cond: (papers_embedding.paper_id = papers_paper.id)
Buffers: shared hit=130975 read=58710
-> Parallel Seq Scan on papers_embedding (cost=0.00..50052.23 rows=1191622 width=72) (actual time=0.208..79.075 rows=953251 loops=3)
Buffers: shared hit=522 read=37614
-> Hash (cost=7186.82..7186.82 rows=15223 width=8) (actual time=23.910..23.910 rows=17570 loops=3)
Buckets: 32768 (originally 16384) Batches: 1 (originally 1) Memory Usage: 943kB
Buffers: shared hit=7532 read=3309
-> Index Scan using idx_papers_created on papers_paper (cost=0.43..7186.82 rows=15223 width=8) (actual time=0.202..20.866 rows=17570 loops=3)
Index Cond: (created >= (now() - '1 mon'::interval))
Buffers: shared hit=7532 read=3309
Planning:
Buffers: shared hit=23
Planning Time: 0.478 ms
Execution Time: 235.421 ms
(31 rows)
Time: 236.286 ms
```
## Top 5 from Past 3 Months (gallery would be 82583 items after filtering)
```SQL
arxiv=# EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM papers_embedding
WHERE paper_id IN (SELECT id FROM papers_paper WHERE created >= NOW() - INTERVAL '3 months')
ORDER BY vector <-> (SELECT vector FROM papers_embedding WHERE id=2)
LIMIT 5;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=3037.40..4693.09 rows=5 width=80) (actual time=109.116..437.816 rows=5 loops=1)
Buffers: shared hit=479 read=3332
InitPlan 1
-> Index Scan using papers_embedding_pkey on papers_embedding papers_embedding_1 (cost=0.43..8.45 rows=1 width=18) (actual time=1.291..1.292 rows=0 loops=1)
Index Cond: (id = 0)
Buffers: shared read=3
-> Nested Loop (cost=3028.95..24177829.79 rows=73005 width=80) (actual time=109.114..437.812 rows=5 loops=1)
Buffers: shared hit=479 read=3332
-> Index Scan using papers_embedding_vector_hnsw_idx on papers_embedding (cost=3028.52..21120796.15 rows=2859894 width=72) (actual time=108.892..436.378 rows=125 loops=1)
Order By: (vector <-> (InitPlan 1).col1)
Buffers: shared hit=189 read=3122
-> Index Scan using papers_paper_pkey on papers_paper (cost=0.43..1.07 rows=1 width=8) (actual time=0.011..0.011 rows=0 loops=125)
Index Cond: (id = papers_embedding.paper_id)
Filter: (created >= (now() - '3 mons'::interval))
Rows Removed by Filter: 1
Buffers: shared hit=290 read=210
Planning:
Buffers: shared hit=23
Planning Time: 0.721 ms
Execution Time: 438.001 ms
(20 rows)
Time: 439.351 ms
```
## Top 40 from Past 3 Months (gallery would be 82583 items after filtering)
```SQL
arxiv=# EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM papers_embedding
WHERE paper_id IN (SELECT id FROM papers_paper WHERE created >= NOW() - INTERVAL '3 months')
ORDER BY vector <-> (SELECT vector FROM papers_embedding WHERE id=3)
LIMIT 40;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=3037.40..16282.96 rows=40 width=80) (actual time=157.051..1220.181 rows=40 loops=1)
Buffers: shared hit=1642 read=8212
InitPlan 1
-> Index Scan using papers_embedding_pkey on papers_embedding papers_embedding_1 (cost=0.43..8.45 rows=1 width=18) (actual time=0.041..0.042 rows=1 loops=1)
Index Cond: (id = 1)
Buffers: shared hit=3 read=1
-> Nested Loop (cost=3028.95..24177829.79 rows=73005 width=80) (actual time=157.050..1220.175 rows=40 loops=1)
Buffers: shared hit=1642 read=8212
-> Index Scan using papers_embedding_vector_hnsw_idx on papers_embedding (cost=3028.52..21120796.15 rows=2859894 width=72) (actual time=157.004..1209.768 rows=390 loops=1)
Order By: (vector <-> (InitPlan 1).col1)
Buffers: shared hit=511 read=7463
-> Index Scan using papers_paper_pkey on papers_paper (cost=0.43..1.07 rows=1 width=8) (actual time=0.008..0.008 rows=0 loops=390)
Index Cond: (id = papers_embedding.paper_id)
Filter: (created >= (now() - '3 mons'::interval))
Rows Removed by Filter: 1
Buffers: shared hit=885 read=675
Planning:
Buffers: shared hit=23
Planning Time: 0.697 ms
Execution Time: 1220.321 ms
(20 rows)
Time: 1221.505 ms (00:01.222)
```
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #916: Unintuitive Query Latency When Filtering
@ 2025-10-28 17:07 ` "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2025-10-28 17:07 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @igm503, it looks like very little of the HNSW index is in memory while a decent percentage of the table is in memory (buffers `hit` vs `read`), making the table scan faster (and cost estimation won't account for this).
A few things you can try:
1. Make sure `shared_buffers` is [set correctly](https://github.com/pgvector/pgvector?tab=readme-ov-file#tuning)
2. Add a B-tree index on `paper_id`
3. Use the `halfvec` type (and/or binary quantization with re-ranking) so more of the HNSW index fits into memory
4. Increase memory
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #916: Unintuitive Query Latency When Filtering
@ 2025-10-28 17:07 ` "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2025-10-28 17:07 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #916: Unintuitive Query Latency When Filtering
@ 2025-11-02 17:17 ` "igm503 (@igm503)" <noreply+igm503@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: igm503 (@igm503) @ 2025-11-02 17:17 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane Thanks for the response.
1. This helped overall speed, but it remains the case that the query planner still makes very bad decisions about whether to use HNSW or a naive scan when the filter set is ~1% of the total gallery (that is, I still see HNSW being outperformed by naive scan even when much more of the index is in memory than in the previous example)
2. I've got that.
3. I've since tried going down to Binary, and still see the same problem! (it just shifts what gallery % and n_returned parameters where the "bad" decisions happen)
Is there any predictable way I can intervene in query execution to force it to force postgres not to use the HNSW index for some queries? That would kind of solve my problem. I've done some searching, and haven't found a way to do this.
^ permalink raw [nested|flat] 5+ messages in thread
* Re: [pgvector/pgvector] issue #916: Unintuitive Query Latency When Filtering
@ 2025-11-04 19:06 ` "ankane (@ankane)" <noreply+ankane@github.com>
3 siblings, 0 replies; 5+ messages in thread
From: ankane (@ankane) @ 2025-11-04 19:06 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
You can add `+ 0` to the `ORDER BY` clause to avoid an index scan (or use the [pg_hint_plan extension](https://github.com/ossc-db/pg_hint_plan)).
^ permalink raw [nested|flat] 5+ messages in thread
end of thread, other threads:[~2025-11-04 19:06 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-10-26 19:58 [pgvector/pgvector] issue #916: Unintuitive Query Latency When Filtering "igm503 (@igm503)" <noreply+igm503@github.com>
2025-10-28 17:07 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-10-28 17:07 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-11-02 17:17 ` "igm503 (@igm503)" <noreply+igm503@github.com>
2025-11-04 19:06 ` "ankane (@ankane)" <noreply+ankane@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