pg.ddx.io  pgvector/pgvector GitHub issues and pull requests  
help / color / mirror / Atom feed
From: hinthornw (@hinthornw) <noreply+hinthornw@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] issue #722: Index support for filters beyond equivalence
Date: Mon, 02 Dec 2024 22:05:10 +0000
Message-ID: <gh-pgvector-pgvector-722-c2513037051@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-722@github.com>
References: <gh-pgvector-pgvector-722@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.

view thread (4+ messages)  latest in thread

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