From: developerayuva (@developerayuva) <noreply+developerayuva@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #845: Question about ef_search behavior and row scan distribution in partitioned tables
Date: Sun, 25 May 2025 06:13:36 +0000
Message-ID: <gh-pgvector-pgvector-845@github.com> (raw)
Hi team,
I'm using pgvector with a partitioned table setup (4 partitions, total ~100k rows). I'm seeing some interesting behavior with how rows are returned when I run a query with different LIMIT values, and I had a couple of questions.
Here's the query I ran:
```
-- This returned 160 rows (40 from each partition), which matches the default ef_search = 40.
-- But when I reduce the LIMIT to 100:
vector100k_part=> EXPLAIN ANALYZE SELECT id
FROM vector_items
ORDER BY embedding <-> '[0.5, 0.5, 0.5, 0.5, 0.5]'::vector(5) limit 1000;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=398.95..619.16 rows=1000 width=12) (actual time=1.566..1.795 rows=160 loops=1)
-> Merge Append (cost=398.95..22420.04 rows=100000 width=12) (actual time=1.565..1.775 rows=160 loops=1)
Sort Key: ((vector_items.embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5)))
-> Index Scan using vector_items_p0_embedding_idx on vector_items_p0 vector_items_1 (cost=99.65..5250.52 rows=25126 width=12) (actual time=0.419..0.466 rows=40 loops=1)
Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
-> Index Scan using vector_items_p1_embedding_idx on vector_items_p1 vector_items_2 (cost=99.68..5223.56 rows=24978 width=12) (actual time=0.375..0.422 rows=40 loops=1)
Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
-> Index Scan using vector_items_p2_embedding_idx on vector_items_p2 vector_items_3 (cost=99.80..5227.42 rows=24971 width=12) (actual time=0.379..0.423 rows=40 loops=1)
Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
-> Index Scan using vector_items_p3_embedding_idx on vector_items_p3 vector_items_4 (cost=99.77..5218.50 rows=24925 width=12) (actual time=0.390..0.435 rows=40 loops=1)
Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
Planning Time: 0.149 ms
Execution Time: 1.865 ms
(13 rows)
-- I see fewer rows scanned per partition (e.g., 26, 29, 27, 21).
vector100k_part=> EXPLAIN ANALYZE SELECT id
FROM vector_items
ORDER BY embedding <-> '[0.5, 0.5, 0.5, 0.5, 0.5]'::vector(5) limit 100;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=398.95..420.97 rows=100 width=12) (actual time=1.518..1.654 rows=100 loops=1)
-> Merge Append (cost=398.95..22420.04 rows=100000 width=12) (actual time=1.517..1.644 rows=100 loops=1)
Sort Key: ((vector_items.embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5)))
-> Index Scan using vector_items_p0_embedding_idx on vector_items_p0 vector_items_1 (cost=99.65..5250.52 rows=25126 width=12) (actual time=0.408..0.436 rows=26 loops=1)
Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
-> Index Scan using vector_items_p1_embedding_idx on vector_items_p1 vector_items_2 (cost=99.68..5223.56 rows=24978 width=12) (actual time=0.365..0.397 rows=29 loops=1)
Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
-> Index Scan using vector_items_p2_embedding_idx on vector_items_p2 vector_items_3 (cost=99.80..5227.42 rows=24971 width=12) (actual time=0.362..0.391 rows=27 loops=1)
Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
-> Index Scan using vector_items_p3_embedding_idx on vector_items_p3 vector_items_4 (cost=99.77..5218.50 rows=24925 width=12) (actual time=0.380..0.404 rows=21 loops=1)
Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
Planning Time: 0.146 ms
Execution Time: 1.703 ms
(13 rows)
```
This leads me to a few questions:
1. Why is `ef_search` not honored uniformly across all partitions for the smaller limit?
2. Let's say one partition has most of the nearest neighbors—shouldn't it be better for recall if each partition still returns up to `ef_search` candidates, and the final top-N (LIMIT) results are chosen after merging all those?
3. Is there a way to tune or force this behavior for better recall in partitioned setups?
Thanks