pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering
7+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering
@ 2025-07-23 08:47 "JoranDox (@JoranDox)" <noreply+JoranDox@github.com>
0 siblings, 0 replies; 7+ messages in thread
From: JoranDox (@JoranDox) @ 2025-07-23 08:47 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
So I want an index for search on a subset (somewhere between 0 and 10%) of my table, with an easy boolean column for filtering. I thought this was a perfect case for using a partial index with definition:
`CREATE INDEX ON my_table USING hnsw (embedding vector_cosine_ops) WHERE (handy_bool = true)`
(table relevant columns are embedding, item_id and handy_bool)
and then querying it like
```sql
WITH
aggregated_embeddings AS (
SELECT AVG(embedding) AS user_embedding
FROM my_table
WHERE item_id IN ('123', '234', '345') -- some list of items from (potentially) the whole table
),
distances AS (
SELECT item_id,
embedding <=> (SELECT user_embedding FROM aggregated_embeddings) AS distance
FROM my_table
WHERE handy_bool = TRUE
ORDER BY distance ASC
)
SELECT item_id, distance
FROM distances
WHERE distance > 0
LIMIT 10
;
```
there's two issues here:
- looking at explain analyze, it's choosing to use the index on handy_bool instead of the partial index (basically I have a generic index on every column for other reasons)
- if I force the usage of the partial index by setting things like
```sql
SET LOCAL enable_seqscan = off;
SET LOCAL enable_bitmapscan = off;
SET LOCAL enable_indexscan = off;
```
it's way faster (~1.5ms vs 15 ms without), BUT for larger queries (where the limit is a big part of the whole set, i.e. I have 300 entries where handy_bool is true, and I'm asking for > 50 rows in the result), I seem to only get partial results (14 rows in one case)
note that I'm setting `SET LOCAL hnsw.ef_search = 200;`
I thought this was only an effect of using a hnsw index and then filtering? with the partial index I'm prefiltering, and this should be no problem, right?
Please advise, and thanks in advance!
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering
@ 2025-07-23 11:56 ` "JoranDox (@JoranDox)" <noreply+JoranDox@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: JoranDox (@JoranDox) @ 2025-07-23 11:56 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
After more testing I think the filtering is a red herring because I see the same effect on using the not-partitioned HNSW index on the embedding column, so do I understand correctly that HNSW is not useful in cases where the LIMIT on your query is a sizeable chunk of the total candidates? I.e. it's fine for a top-10 search, but sucks in a top-100 search in 300 candidates?
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering
@ 2025-07-23 11:58 ` "JoranDox (@JoranDox)" <noreply+JoranDox@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: JoranDox (@JoranDox) @ 2025-07-23 11:58 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
And the question remains how I'm supposed to get postgres to use the (partial) hnsw index over the sequential scan on the subset (but maybe that should be a different issue?) because we're talking 10x faster in my tests
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering
@ 2025-07-23 22:51 ` "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2025-07-23 22:51 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @JoranDox,
1. Have you run `ANALYZE` on the table?
2. Is `random_page_cost` set correctly?
3. What's the output of `\d user_embedding` in `psql`?
4. What's the output of `EXPLAIN (ANALYZE, SETTINGS, BUFFERS)` for the default plan, as well as when the partial index is used?
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering
@ 2025-07-24 07:11 ` "JoranDox (@JoranDox)" <noreply+JoranDox@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: JoranDox (@JoranDox) @ 2025-07-24 07:11 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi Andrew, thanks for looking into this!
1. I haven't manually, but autovacuum is turned on so as far as I understand it, it should get analyzed periodically. I've run it manually once now and tried again but no change in behaviour.
2. Surprisingly random_page_cost is set to 2 instead of the default of 4, but unless it set it to a really low number (somewhere between 0.5 and 0, depending on the specific table (I have multiple similar tables for other reasons)) postgres won't use the partial HNSW index, and will happily use the slower sequential scan on the boolean index. Setting random_page_cost very low is a substitute for turning off seqscan etc, but again feels "wrong". I'd like to tune the numbers in some way (or understand it better at least) to make postgres make the "right" decisions on its own.
3. I'm not sure how to do `\d` on a CTE, but the initial table "my_table" in the example looks like this:
```
postgres=> \d my_table
Table "public.my_table"
Column | Type | Collation | Nullable | Default
----------------------+-----------------------------+-----------+----------+---------
item_id | text | | not null |
embedding | vector(1536) | | |
handy_bool | boolean | | |
Indexes:
"my_table_pkey" PRIMARY KEY, btree (item_id)
"my_table_embedding_hnsw_partial_idx" hnsw (embedding vector_cosine_ops) WHERE handy_bool = true
"my_table_embedding_idx" hnsw (embedding vector_cosine_ops)
"my_table_handy_bool_idx" btree (handy_bool)
"my_table_item_id_idx" btree (item_id)
```
and the aggregated_embeddings CTE looks like a single column "user_embedding" with a single row, containing the combined embedding of the input items
4. good question:
<details>
<summary>query used each time</summary>
```sql
BEGIN;
-- settings go here
EXPLAIN (ANALYZE, SETTINGS, BUFFERS)
WITH
aggregated_embeddings AS (
SELECT AVG(embedding) AS user_embedding
FROM my_table
WHERE item_id IN ('123','234','345')
limit 5;
)
SELECT item_id,
embedding <=> (SELECT user_embedding FROM aggregated_embeddings) AS distance
FROM my_table
WHERE handy_bool = TRUE
ORDER BY distance ASC
LIMIT 10
;
ROLLBACK;
```
</details>
<details>
<summary>No settings</summary>
gives
```
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=395.24..395.27 rows=10 width=18) (actual time=11.043..11.046 rows=10 loops=1)
Buffers: shared hit=8589
InitPlan 1 (returns $0)
-> Aggregate (cost=12.14..12.15 rows=1 width=32) (actual time=0.086..0.087 rows=1 loops=1)
Buffers: shared hit=27
-> Index Scan using my_table_item_id_idx on my_table my_table_1 (cost=0.42..12.13 rows=3 width=18) (actual time=0.011..0.019 rows=3 loops=1)
Index Cond: (item_id = ANY ('{123,234,345}'::text[]))
Buffers: shared hit=12
-> Sort (cost=383.09..387.09 rows=1600 width=18) (actual time=11.043..11.043 rows=10 loops=1)
Sort Key: ((my_table.embedding <=> $0))
Sort Method: top-N heapsort Memory: 26kB
Buffers: shared hit=8589
-> Index Scan using my_table_handy_bool_idx on my_table (cost=0.29..348.52 rows=1600 width=18) (actual time=0.101..10.631 rows=1614 loops=1)
Index Cond: (handy_bool = true)
Buffers: shared hit=8589
Settings: effective_cache_size = '48GB', jit = 'off', random_page_cost = '2'
Planning:
Buffers: shared hit=2
Planning Time: 0.141 ms
Execution Time: 11.076 ms
(20 rows)
```
</details>
<details>
<summary>Forcing the index usage through random_page_cost = 0</summary>
`SET LOCAL random_page_cost = 0`;
```
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=4.57..4.75 rows=10 width=18) (actual time=0.646..0.707 rows=10 loops=1)
Buffers: shared hit=560
InitPlan 1 (returns $0)
-> Aggregate (cost=1.32..1.33 rows=1 width=32) (actual time=0.089..0.090 rows=1 loops=1)
Buffers: shared hit=27
-> Index Scan using my_table_item_id_idx on my_table my_table_1 (cost=0.42..1.31 rows=3 width=18) (actual time=0.013..0.022 rows=3 loops=1)
Index Cond: (item_id = ANY ('{123,234,345}'::text[]))
Buffers: shared hit=12
-> Index Scan using my_table_embedding_hnsw_partial_idx on my_table (cost=3.23..32.00 rows=1600 width=18) (actual time=0.645..0.705 rows=10 loops=1)
Order By: (embedding <=> $0)
Buffers: shared hit=560
Settings: effective_cache_size = '48GB', jit = 'off', random_page_cost = '0'
Planning:
Buffers: shared hit=2
Planning Time: 0.137 ms
Execution Time: 0.736 ms
(16 rows)
```
(same plan if I add `SET LOCAL hnsw.ef_search = 200;` and/or `SET LOCAL hnsw.iterative_scan = relaxed_order;`)
</details>
<details>
<summary>Almost the same if forcing the index usage through turning off all index scans, just different cost estimates.
</summary>
`SET LOCAL enable_seqscan = off;`
`SET LOCAL enable_bitmapscan = off;`
`SET LOCAL enable_indexscan = off;`
```
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=20000002096.07..20000002116.14 rows=10 width=18) (actual time=1.709..1.782 rows=10 loops=1)
Buffers: shared hit=1213
InitPlan 1 (returns $0)
-> Aggregate (cost=10000000012.14..10000000012.15 rows=1 width=32) (actual time=0.095..0.096 rows=1 loops=1)
Buffers: shared hit=27
-> Index Scan using my_table_item_id_idx on my_table my_table_1 (cost=10000000000.42..10000000012.13 rows=3 width=18) (actual time=0.015..0.024 rows=3 loops=1)
Index Cond: (item_id = ANY ('{123,234,345}'::text[]))
Buffers: shared hit=12
-> Index Scan using my_table_embedding_hnsw_partial_idx on my_table (cost=10000002083.91..10000005296.00 rows=1600 width=18) (actual time=1.708..1.779 rows=10 loops=1)
Order By: (embedding <=> $0)
Buffers: shared hit=1213
Settings: effective_cache_size = '48GB', jit = 'off', random_page_cost = '2', enable_seqscan = 'off', enable_bitmapscan = 'off', enable_indexscan = 'off'
Planning:
Buffers: shared hit=2
Planning Time: 0.155 ms
Execution Time: 1.814 ms
(16 rows)
```
</details>
(I've renamed some things like the index names and table name, in case that wasn't clear, so if I missed something or something seems off, that could just be me, I've also spotted and fixed a mistake like that in the OP)
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering
@ 2025-07-25 16:35 ` "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2025-07-25 16:35 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Thanks.
From what I can tell, Postgres underestimates the cost of the `my_table_handy_bool_idx` index scan since it doesn't take [TOAST](https://www.postgresql.org/docs/current/storage-toast.html) into account.
With the specific setup above and `random_page_cost = 1.1` (the recommended value when using an SSD), this causes the planner to choose a suboptimal plan with ~50k rows (but not 100k+).
Two ways to address this are:
1. Use `PLAIN` storage for the `embedding` column, which avoids TOAST
2. Use [pg_hint_plan](https://github.com/ossc-db/pg_hint_plan) to help the planner
^ permalink raw [nested|flat] 7+ messages in thread
* Re: [pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering
@ 2025-07-25 16:35 ` "ankane (@ankane)" <noreply+ankane@github.com>
5 siblings, 0 replies; 7+ messages in thread
From: ankane (@ankane) @ 2025-07-25 16:35 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 7+ messages in thread
end of thread, other threads:[~2025-07-25 16:35 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-07-23 08:47 [pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering "JoranDox (@JoranDox)" <noreply+JoranDox@github.com>
2025-07-23 11:56 ` "JoranDox (@JoranDox)" <noreply+JoranDox@github.com>
2025-07-23 11:58 ` "JoranDox (@JoranDox)" <noreply+JoranDox@github.com>
2025-07-23 22:51 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-07-24 07:11 ` "JoranDox (@JoranDox)" <noreply+JoranDox@github.com>
2025-07-25 16:35 ` "ankane (@ankane)" <noreply+ankane@github.com>
2025-07-25 16:35 ` "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