pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: JoranDox (@JoranDox) <noreply+JoranDox@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering
Date: Thu, 24 Jul 2025 07:11:15 +0000
Message-ID: <gh-pgvector-pgvector-878-c3112313790@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-878@github.com>
References: <gh-pgvector-pgvector-878@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)

view thread (7+ messages)  latest in thread

Message-ID: <gh-pgvector-pgvector-878-c3112313790@github.com>
Permalink:  ../gh-pgvector-pgvector-878-c3112313790@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