From: igm503 (@igm503) <noreply+igm503@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #916: Unintuitive Query Latency When Filtering
Date: Sun, 26 Oct 2025 19:58:25 +0000
Message-ID: <gh-pgvector-pgvector-916@github.com> (raw)
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)
```