pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #722: Index support for filters beyond equivalence
4+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #722: Index support for filters beyond equivalence
@ 2024-12-02 20:32 "hinthornw (@hinthornw)" <noreply+hinthornw@github.com>
0 siblings, 0 replies; 4+ messages in thread
From: hinthornw (@hinthornw) @ 2024-12-02 20:32 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Great work in 0.8.0 adding the iterative scan to resolve issues like [#678](https://github.com/pgvector/pgvector/issues/678), [#259](https://github.com/pgvector/pgvector/issues/259), and [#524](https://github.com/pgvector/pgvector/pull/524). I may be mistaken, but it seems the current index usage is still restricted to equivalence filters(?).
1. Is support for LIKE expressions (or other filters) in pre-filtering currently possible?
2. If not, is this feature planned for future releases (or something you'd invite a community contribution for)?
## Context
We want to support vector searches within namespaces. We have a btree index on a namespace/prefix column and want to retrieve rows by similarity within a namespace. When using the LIKE operator, the index is not used (a Sequential Scan is performed).
An analogy would be if you wanted to model a filesystem in postgres and wanted to search for documents in all subdirectories of `/Users/<user-id>` (Note: this is just one example; if it were limited to this then I'd create a partial index or partition by user-id).
```sql
SELECT *
FROM items
WHERE prefix LIKE 'prefix%'
ORDER BY embedding <-> query_vector ASC
LIMIT 10;
```
## Example Schema
```sql
CREATE TABLE items (
id SERIAL PRIMARY KEY,
prefix TEXT,
embedding vector(384)
);
CREATE INDEX IF NOT EXISTS items_prefix_idx ON items USING btree (prefix text_pattern_ops);
CREATE INDEX vector_idx ON items USING ivfflat (embedding vector_cosine_ops);
```
## Desired Behavior
The query should use both indices:
1. btree index for LIKE-based pre-filtering
2. vector index for similarity search
At present, I believe the recommendation is to either omit the index and just do a sequential scan after filtering by prefix or to use the vector index and then filter by prefix, depending on how the data is structured, though I could be missing something obvious here. Neither approach is that appetizing.
## Additional Information
- pgvector version: 0.8.0
- PostgreSQL version: 15, 16
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #722: Index support for filters beyond equivalence
@ 2024-12-02 21:18 "jkatz (@jkatz)" <noreply+jkatz@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: jkatz (@jkatz) @ 2024-12-02 21:18 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
You're not limited in index operations for other PostgreSQL index types. Do you have example query plans?
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #722: Index support for filters beyond equivalence
@ 2024-12-02 22:05 "hinthornw (@hinthornw)" <noreply+hinthornw@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: hinthornw (@hinthornw) @ 2024-12-02 22:05 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Oh amazing likely my naïveté then; apologies for the noise! I'll debug my case further before re-opening anything since it seems to be user error.
For context I was running things like the following query:
```sql
WITH scored AS (
SELECT s.prefix, s.key, s.value, s.created_at, s.updated_at, iv.embedding <-> %s::%s AS neg_score
FROM items s
JOIN item_vectors iv ON s.prefix = sv.prefix AND s.key = sv.key
WHERE s.prefix LIKE %s
ORDER BY iv.embedding <-> %s::%s ASC
LIMIT %s
)
SELECT * FROM (
SELECT DISTINCT ON (prefix, key)
prefix, key, value, created_at, updated_at, 1 - (scored.neg_score) as score
FROM scored
ORDER BY prefix, key, score DESC
) AS unique_docs
ORDER BY score DESC
LIMIT %s
OFFSET %s
```
And getting plans of the sort:
```
Limit (cost=20.97..20.97 rows=2 width=120)
-> Sort (cost=20.97..20.97 rows=2 width=120)
Sort Key: ((- scored.neg_score)) DESC
-> Unique (cost=20.94..20.96 rows=2 width=120)
-> Sort (cost=20.94..20.95 rows=2 width=120)
Sort Key: scored.prefix, scored.key, ((- scored.neg_score)) DESC
-> Subquery Scan on scored (cost=20.90..20.93 rows=2 width=120)
-> Limit (cost=20.90..20.91 rows=2 width=128)
-> Sort (cost=20.90..20.91 rows=2 width=128)
Sort Key: ((sv.embedding <#> '[0.03918457,0.04940796,0.006565094,0.03567505,0.09698486,0.0390625,0.06878662,-0.064819336,-0.031829834,-0.032226562,-0.114868164,0.003162384,0.059753418,-0.022979736,0.008384705,-0.03665161,-0.021102905,0.012245178,-0.051330566,0.0
-> Nested Loop (cost=8.34..20.89 rows=2 width=128)
Join Filter: (s.key = sv.key)
-> Bitmap Heap Scan on store s (cost=4.17..11.28 rows=3 width=112)
Recheck Cond: (prefix = 'test'::text)
-> Bitmap Index Scan on store_prefix_idx (cost=0.00..4.17 rows=3 width=0)
Index Cond: (prefix = 'test'::text)
-> Materialize (cost=4.16..9.51 rows=2 width=96)
-> Bitmap Heap Scan on store_vectors sv (cost=4.16..9.50 rows=2 width=96)
Recheck Cond: (prefix = 'test'::text)
-> Bitmap Index Scan on store_vectors_pkey (cost=0.00..4.16 rows=2 width=0)
Index Cond: (prefix = 'test'::text)
```
If I remove the WHERE clause there, it returns somethign more like;
```
Limit (cost=36.47..36.50 rows=10 width=120)
-> Sort (cost=36.47..36.62 rows=61 width=120)
Sort Key: ((- scored.neg_score)) DESC
-> Unique (cost=34.70..35.15 rows=61 width=120)
-> Sort (cost=34.70..34.85 rows=61 width=120)
Sort Key: scored.prefix, scored.key, ((- scored.neg_score)) DESC
-> Subquery Scan on scored (cost=9.10..32.89 rows=61 width=120)
-> Limit (cost=9.10..32.13 rows=61 width=128)
-> Nested Loop (cost=9.10..186.48 rows=470 width=128)
-> Index Scan using store_vectors_embedding_idx on store_vectors sv (cost=8.95..56.23 rows=470 width=96)
Order By: (embedding <#> '[-0.006450653,-0.0077438354,-0.0049858093,0.031433105,-0.0057144165,-0.06707764,0.014884949,-0.01197052,-0.009712219,0.0051879883,0.010398865,0.052093506,0.029403687,0.0049476624,-0.033050537,-0.045440674,0.0110321045,0.05871582,0
-> Index Scan using store_pkey on store s (cost=0.15..0.27 rows=1 width=112)
Index Cond: ((prefix = sv.prefix) AND (key = sv.key))
```
Since this was toggled by whether the WHERE clause was included, I had assumed it was a limitation.
Note that in this test case, the filter matches 100% of rows. I think what's happening is maybe the number of rows I have total is too small for it to use but will try to verify.
^ permalink raw [nested|flat] 4+ messages in thread
* Re: [pgvector/pgvector] issue #722: Index support for filters beyond equivalence
@ 2024-12-02 22:05 "hinthornw (@hinthornw)" <noreply+hinthornw@github.com>
2 siblings, 0 replies; 4+ messages in thread
From: hinthornw (@hinthornw) @ 2024-12-02 22:05 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@hinthornw closed this.
^ permalink raw [nested|flat] 4+ messages in thread
end of thread, other threads:[~2024-12-02 22:05 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-12-02 20:32 [pgvector/pgvector] issue #722: Index support for filters beyond equivalence "hinthornw (@hinthornw)" <noreply+hinthornw@github.com>
2024-12-02 21:18 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-12-02 22:05 ` "hinthornw (@hinthornw)" <noreply+hinthornw@github.com>
2024-12-02 22:05 ` "hinthornw (@hinthornw)" <noreply+hinthornw@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