pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
[pgvector/pgvector] issue #845: Question about ef_search behavior and row scan distribution in partitioned tables
7+ messages / 2 participants
[nested] [flat]

* [pgvector/pgvector] issue #845: Question about ef_search behavior and row scan distribution in partitioned tables
@ 2025-05-25 06:13  "developerayuva (@developerayuva)" <noreply+developerayuva@github.com>
  0 siblings, 0 replies; 7+ messages in thread

From: developerayuva (@developerayuva) @ 2025-05-25 06:13 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

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

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #845: Question about ef_search behavior and row scan distribution in partitioned tables
@ 2025-05-25 17:30  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2025-05-25 17:30 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Hi @developerayuva, `ef_search` is used for each partition.

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #845: Question about ef_search behavior and row scan distribution in partitioned tables
@ 2025-05-25 17:30  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2025-05-25 17:30 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

@ankane closed this.

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #845: Question about ef_search behavior and row scan distribution in partitioned tables
@ 2025-05-25 18:10  "developerayuva (@developerayuva)" <noreply+developerayuva@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: developerayuva (@developerayuva) @ 2025-05-25 18:10 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Thanks @ankane for the explanation! Just curious—why does the second query with `LIMIT 100` return fewer than 40 rows per partition, even though `ef_search` is set to 40?


^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #845: Question about ef_search behavior and row scan distribution in partitioned tables
@ 2025-05-25 18:26  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2025-05-25 18:26 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

The `EXPLAIN` output won't include all tuples fetched by the index (only those returned to the executor, which requests them one at a time until the query is satisfied).

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #845: Question about ef_search behavior and row scan distribution in partitioned tables
@ 2025-05-25 18:35  "ankane (@ankane)" <noreply+ankane@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: ankane (@ankane) @ 2025-05-25 18:35 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Try setting the limit to 1, then 2, then 3 to get a better idea for how this works with partitions.

^ permalink  raw  [nested|flat] 7+ messages in thread

* Re: [pgvector/pgvector] issue #845: Question about ef_search behavior and row scan distribution in partitioned tables
@ 2025-05-25 20:31  "developerayuva (@developerayuva)" <noreply+developerayuva@github.com>
  5 siblings, 0 replies; 7+ messages in thread

From: developerayuva (@developerayuva) @ 2025-05-25 20:31 UTC (permalink / raw)
  To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>

Got it—thanks! The reason we don’t always see `ef_search` (e.g., 40) rows returned from each index scan seems to be because the LIMIT clause is satisfied before all partitions are fully scanned. As shown in the examples below, the planner stops fetching more rows once it has enough to meet the limit at the top level. 

```
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 1;
                                                                                    QUERY PLAN                                                                                     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=398.95..399.17 rows=1 width=12) (actual time=1.633..1.635 rows=1 loops=1)
   ->  Merge Append  (cost=398.95..22420.04 rows=100000 width=12) (actual time=1.632..1.633 rows=1 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.444..0.445 rows=1 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.388..0.388 rows=1 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.386..0.386 rows=1 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.411..0.411 rows=1 loops=1)
               Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
 Planning Time: 0.150 ms
 Execution Time: 1.716 ms
(13 rows)

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 2;
                                                                                    QUERY PLAN                                                                                     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=398.95..399.39 rows=2 width=12) (actual time=1.624..1.630 rows=2 loops=1)
   ->  Merge Append  (cost=398.95..22420.04 rows=100000 width=12) (actual time=1.624..1.628 rows=2 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.429..0.429 rows=1 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.402..0.405 rows=2 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.384..0.384 rows=1 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.407..0.408 rows=1 loops=1)
               Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
 Planning Time: 0.143 ms
 Execution Time: 1.687 ms
(13 rows)

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 3;
                                                                                    QUERY PLAN                                                                                     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=398.95..399.61 rows=3 width=12) (actual time=1.605..1.612 rows=3 loops=1)
   ->  Merge Append  (cost=398.95..22420.04 rows=100000 width=12) (actual time=1.604..1.610 rows=3 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.428..0.428 rows=1 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.377..0.380 rows=2 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.392..0.394 rows=2 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.404..0.404 rows=1 loops=1)
               Order By: (embedding <-> '[0.5,0.5,0.5,0.5,0.5]'::vector(5))
 Planning Time: 0.161 ms
 Execution Time: 1.658 ms
(13 rows)
```

^ permalink  raw  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2025-05-25 20:31 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-05-25 06:13 [pgvector/pgvector] issue #845: Question about ef_search behavior and row scan distribution in partitioned tables "developerayuva (@developerayuva)" <noreply+developerayuva@github.com>
2025-05-25 17:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-05-25 17:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-05-25 18:10 ` "developerayuva (@developerayuva)" <noreply+developerayuva@github.com>
2025-05-25 18:26 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-05-25 18:35 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-05-25 20:31 ` "developerayuva (@developerayuva)" <noreply+developerayuva@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