pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: mauriciocirelli (@mauriciocirelli) <noreply+mauriciocirelli@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #259: Understanding HNSW + filtering
Date: Tue, 07 May 2024 11:18:10 +0000
Message-ID: <gh-pgvector-pgvector-259-c2098167511@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-259@github.com>
References: <gh-pgvector-pgvector-259@github.com>

Another thing that I am seeing when having more filters on the query is the behavior of the LIMIT clause. This table is partitioned by category and we have an index on owner_id and another index on the embedding (HSNW).

```sql

WITH pre_filtered_docs AS (
	SELECT * FROM documents
	WHERE
	((category = ('cat-a')) AND (owner_id IN (1, 2, 3)))
	OR
	((category = ('cat-b')) AND (owner_id IN (4, 5, 6)))
)
SELECT category, owner_id, text, embedding <=> '[vector to query]' as distance
FROM pre_filtered_docs
ORDER BY distance
LIMIT 10;
```

```
QUERY PLAN
Limit  (cost=257.04..296.29 rows=10 width=48)
  ->  Merge Append  (cost=257.04..833.95 rows=147 width=48)
        Sort Key: ((documents.embedding <=> '[...]'::vector))"
        ->  Index Scan using documents_cat_a_embedding_idx on documents_cat_a documents_1  (cost=128.51..279.53 rows=14 width=48)
              Order By: (embedding <=> '[...]'::vector)
              Filter: ((((category)::text = 'cat-a'::text) AND (owner_id = ANY ('{1,2,3}'::integer[]))) OR (((category)::text = 'cat-b'::text) AND (owner_id = ANY ('{4,5,6}'::integer[]))))
        ->  Index Scan using documents_cat_b_embedding_idx on documents_cat_b documents_2  (cost=128.51..552.95 rows=133 width=48)
              Order By: (embedding <=> '[...]'::vector)
              Filter: ((((category)::text = 'cat-a'::text) AND (owner_id = ANY ('{1,2,3}'::integer[]))) OR (((category)::text = 'cat-b'::text) AND (owner_id = ANY ('{4,5,6}'::integer[]))))
```

Since I have an OR condition on the subquery, I would expect that this query would bring at least the same number of documents it brings without that OR. However, I see that if I remove one of those conditions, I get 10 docs. If I keep them both, I get less docs.

How is it possible? It should be an additive filter (OR) and return at least the same number of docs.

Interestingly, this is happening only on PG v15 (our production deployment). I have PG v16 on a docker container in my local machine for development and it works as expected.

The query planner is ver different:

```
QUERY PLAN
Limit  (cost=382.12..382.14 rows=10 width=48)
  ->  Sort  (cost=382.12..382.48 rows=146 width=48)
        Sort Key: ((documents.embedding <=> '[...]'::vector))
        ->  Append  (cost=36.32..378.96 rows=146 width=48)
              ->  Bitmap Heap Scan on documents_cat_a documents_1  (cost=36.32..56.99 rows=14 width=48)
                    Recheck Cond: ((owner_id = ANY ('{1,2,3}'::integer[])) OR ((category)::text = 'cat-b'::text))
                    Filter: ((((category)::text = 'cat-a'::text) AND (owner_id = ANY ('{1,2,3}'::integer[]))) OR (((category)::text = 'cat-b'::text) AND (owner_id = ANY ('{4,5,6}'::integer[]))))
                    ->  BitmapOr  (cost=36.28..36.28 rows=14 width=0)
                          ->  Bitmap Index Scan on documents_cat_a_owner_id_idx  (cost=0.00..31.99 rows=14 width=0)
                                Index Cond: (owner_id = ANY ('{1,2,3}'::integer[]))
                          ->  Bitmap Index Scan on documents_cat_a_pkey  (cost=0.00..4.29 rows=1 width=0)
                                Index Cond: ((category)::text = 'cat-b'::text)
              ->  Seq Scan on documents_cat_b documents_2  (cost=0.04..321.24 rows=132 width=48)
                    Filter: ((((category)::text = 'cat-a'::text) AND (owner_id = ANY ('{1,2,3}'::integer[]))) OR (((category)::text = 'cat-b'::text) AND (owner_id = ANY ('{4,5,6}'::integer[]))))

```

view thread (90+ messages)  latest in thread

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