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: [pgvector/pgvector] issue #878: partial index "USING hnsw (embedding vector_cosine_ops) WHERE (some_bool = true)" gives partial results as if filtering
Date: Wed, 23 Jul 2025 08:47:42 +0000
Message-ID: <gh-pgvector-pgvector-878@github.com> (raw)

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!



view thread (7+ messages)  latest in thread

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