pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #263: No results when using index
17+ messages / 8 participants
[nested] [flat]
* [pgvector/pgvector] issue #263: No results when using index
@ 2023-09-12 19:08 "vincenzon (@vincenzon)" <noreply+vincenzon@github.com>
0 siblings, 0 replies; 17+ messages in thread
From: vincenzon (@vincenzon) @ 2023-09-12 19:08 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I have a table of quotes with vector embeddings. There are two columns in the table, one is indexed one is not, they are otherwise identical. When I query against the unindexed column for the 5 nearest neighbors to a given embedding, I get 5 results. However, when I run the same query using the indexed column I get no results.
```
conn = psycopg2.connect(connectionStr)
register_vector(conn)
embedding = model.encode(["I can't do that"])
with conn.cursor() as cur:
t0 = time.time()
cur.execute("""
select movie_line
from quotes
where character = 'hal'
and movie_line like '%%Dave%%'
order by (embedding <=> %s)
limit 1
""", embedding)
rows = cur.fetchall()
t1 = time.time()
print(f"Time: {t1 - t0}s")
for r in rows:
print(r)
conn.close()
```
Produces:
```
Time: 4.286221981048584s
("I'm sorry, Dave, but in accordance with sub-routine C1532/4, quote, When the crew are dead or incapacitated, the computer must assume control, unquote. I must, therefore, override your authority now since you are not in any condition to intel- ligently exercise it.",)
```
But changing to use the indexed column, embedding2, in the order by clause, and setting the probes parameter
```
cur.execute("set ivfflat.probes = 28;")
...
order by (embedding2 <=> %s)
...
```
Returns no results.
How can using an index in an order by clause lead to no results being returned?
This is entirely reproducible, so if there is any debugging I can do to help diagnose the issue, let me know.
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2023-09-12 19:09 "jkatz (@jkatz)" <noreply+jkatz@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: jkatz (@jkatz) @ 2023-09-12 19:09 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@vincenzon How many rows are in the table, and how many `lists` did you use to create the index?
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2023-09-12 21:16 "vincenzon (@vincenzon)" <noreply+vincenzon@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: vincenzon (@vincenzon) @ 2023-09-12 21:16 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@jkatz there are 890K rows, there are 800 lists in the IVFFlat index, and I am querying with 28 probes.
```
Table "public.quotes"
Column | Type | Collation | Nullable | Default
-------------+-------------+-----------+----------+---------
id | integer | | not null |
movie_title | text | | |
movie_line | text | | |
character | text | | |
embedding | vector(768) | | |
embedding2 | vector(768) | | |
Indexes:
"quotes_pkey" PRIMARY KEY, btree (id)
"quotes_embedding2_idx" ivfflat (embedding2 vector_cosine_ops) WITH (lists='800')
```
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2023-09-13 03:39 "fanfuxiaoran (@fanfuxiaoran)" <noreply+fanfuxiaoran@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: fanfuxiaoran (@fanfuxiaoran) @ 2023-09-13 03:39 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
That's interesting, could you try bigger ivfflat.probes or a different vector here`(embedding <=> %s)` to see if it can return any results?
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2023-09-13 11:01 "vincenzon (@vincenzon)" <noreply+vincenzon@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: vincenzon (@vincenzon) @ 2023-09-13 11:01 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@fanfuxiaoran it doesn't matter how many probes I use, although, it is definitely the case that fewer probes return faster (still with no results). So it must be "doing the query" internally, it is just not returning anything.
Changing the query vector does return results. I guess I "knew" it was specific to this query string since I have other queries in my set up that work just fine.
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2023-09-13 13:12 "fanfuxiaoran (@fanfuxiaoran)" <noreply+fanfuxiaoran@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: fanfuxiaoran (@fanfuxiaoran) @ 2023-09-13 13:12 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@vincenzon I guess I know the cause, I reproduced it by the following steps
```
create table t5(id int, embedding vector(3));
insert into t5 values(1, '[1,3,5]');
insert into t5 values(2, '[4,5,3]');
insert into t5 values(3, '[7,0,4]');
insert into t5 values(4, '[3,6,9]');
CREATE INDEX ON t5 USING ivfflat (embedding vector_ip_ops) WITH (lists = 4);
set ivfflat.probes=1;
set enable_seqscan=false;
select * from t5 where id = 3 order by embedding <#> '[1,3,5]' limit 2;
```
The above query returns nothing as well.
The ivfflat index searches the cluster '[1,3,5]', but there is no tuple matching the condition 'id=3'.
I think the result is reasonable.
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2023-09-13 14:12 "vincenzon (@vincenzon)" <noreply+vincenzon@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: vincenzon (@vincenzon) @ 2023-09-13 14:12 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@fanfuxiaoran I guess I disagree with the conclusion that this is reasonable. I would assume that the default value of:
```
embedding <#> '[1,3,5]'
```
would be 1.0 if no vectors were found in the cluster. The result in this case would be random order (or whatever would result when the cosine metric ties). I think returning nothing is an abstraction leak. I expect the nearest neighbor to be approximate not ... whatever this is.
Do you see my point?
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2023-09-13 17:05 "leo-mangold (@leo-mangold)" <noreply+leo-mangold@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: leo-mangold (@leo-mangold) @ 2023-09-13 17:05 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I also have this issue with `hnsw` for certain input query strings. It also happed to me when I used `ivfflat`.
```
SELECT * FROM table WHERE date_start <= $1 AND (date_end IS NULL OR date_end >= $1) ORDER BY embedding <#> $2 LIMIT $3
```
I have about 300k rows in the entire set and after filtering for date it still leaves me with 15k rows, so i would expect there to be some matches.
When disabling the index I get 53 results for exactly the same query :(
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2023-09-14 07:34 "fanfuxiaoran (@fanfuxiaoran)" <noreply+fanfuxiaoran@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: fanfuxiaoran (@fanfuxiaoran) @ 2023-09-14 07:34 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@vincenzon Yes, I know what you mean. You expect the index scan to return the same result as the seq scan. Looks like this issue is similar to https://github.com/pgvector/pgvector/issues/259.
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2023-09-20 22:06 "pashkinelfe (@pashkinelfe)" <noreply+pashkinelfe@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: pashkinelfe (@pashkinelfe) @ 2023-09-20 22:06 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I suppose the case when post-filtering depletes all (or most) of ann-found tuples is completely legit. Though considering how often are the related complaints I'd suggest it to be mentioned in readme/manual explicitly. Maybe both filtering by other attribute or "filtering" due to dead heap tuples could be mentioned both.
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2023-11-07 04:30 "ankane (@ankane)" <noreply+ankane@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: ankane (@ankane) @ 2023-11-07 04:30 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I think we should probably try to update the cost estimate so the index won't be used in cases where few tuples are expected to be returned due to attribute filtering (if possible).
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2024-01-21 21:05 "ankane (@ankane)" <noreply+ankane@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: ankane (@ankane) @ 2024-01-21 21:05 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Pushed one way to address this in #424 (essentially, the queries in this thread are not a good fit for a vector index, so the PR makes sure one isn't used).
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2024-06-01 10:05 "kareldonk (@kareldonk)" <noreply+kareldonk@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: kareldonk (@kareldonk) @ 2024-06-01 10:05 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I also faced a situation where I was using the HNSW index type with `vector_cosine_ops` and I would get no results returned for certain queries. This happened especially when using `order by (table.embedding <=> 'vectors here'` in my SQL queries in combination with `limit 10`. If I removed the ordering and/or the limit, depending on the case, then I would get results for the same query.
What seems to have resolved the problem for me was recreating the index with `with (m = 16, ef_construction = 1000)` instead of the default `ef_construction` value. I also had to do `SET hnsw.ef_search = 100;` instead of the default.
Seems like if the table with embeddings gets large enough the default value is not enough. But my table has 16640 records now, which is relatively small in my opinion.
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2024-06-02 10:30 "david-gang (@david-gang)" <noreply+david-gang@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: david-gang (@david-gang) @ 2024-06-02 10:30 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
> I also faced a situation where I was using the HNSW index type with `vector_cosine_ops` and I would get no results returned for certain queries. This happened especially when using `order by (table.embedding <=> 'vectors here'` in my SQL queries in combination with `limit 10`. If I removed the ordering and/or the limit, depending on the case, then I would get results for the same query.
>
> What seems to have resolved the problem for me was recreating the index with `with (m = 16, ef_construction = 1000)` instead of the default `ef_construction` value. I also had to do `SET hnsw.ef_search = 100;` instead of the default.
>
> Seems like if the table with embeddings gets large enough the default value is not enough. But my table has 16640 records now, which is relatively small in my opinion.
BTW: If you have just 16640 records it would make sense to run it without index and see what the times are.
(This does not solve the general use case which can just be solved by something like hqann)
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2024-09-22 21:55 "ankane (@ankane)" <noreply+ankane@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: ankane (@ankane) @ 2024-09-22 21:55 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
There are now branches to address this in #678 for anyone who would like to give feedback / test (in a non-production environment).
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2024-10-30 22:14 "ankane (@ankane)" <noreply+ankane@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: ankane (@ankane) @ 2024-10-30 22:14 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
0.8.0 supports [iterative index scans](https://github.com/pgvector/pgvector?tab=readme-ov-file#iterative-index-scans) and has improved cost estimation, which should completely eliminate this issue for IVFFlat and greatly improve it for HNSW.
^ permalink raw [nested|flat] 17+ messages in thread
* Re: [pgvector/pgvector] issue #263: No results when using index
@ 2024-10-30 22:14 "ankane (@ankane)" <noreply+ankane@github.com>
15 siblings, 0 replies; 17+ messages in thread
From: ankane (@ankane) @ 2024-10-30 22:14 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 17+ messages in thread
end of thread, other threads:[~2024-10-30 22:14 UTC | newest]
Thread overview: 17+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-09-12 19:08 [pgvector/pgvector] issue #263: No results when using index "vincenzon (@vincenzon)" <noreply+vincenzon@github.com>
2023-09-12 19:09 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2023-09-12 21:16 ` "vincenzon (@vincenzon)" <noreply+vincenzon@github.com>
2023-09-13 03:39 ` "fanfuxiaoran (@fanfuxiaoran)" <noreply+fanfuxiaoran@github.com>
2023-09-13 11:01 ` "vincenzon (@vincenzon)" <noreply+vincenzon@github.com>
2023-09-13 13:12 ` "fanfuxiaoran (@fanfuxiaoran)" <noreply+fanfuxiaoran@github.com>
2023-09-13 14:12 ` "vincenzon (@vincenzon)" <noreply+vincenzon@github.com>
2023-09-13 17:05 ` "leo-mangold (@leo-mangold)" <noreply+leo-mangold@github.com>
2023-09-14 07:34 ` "fanfuxiaoran (@fanfuxiaoran)" <noreply+fanfuxiaoran@github.com>
2023-09-20 22:06 ` "pashkinelfe (@pashkinelfe)" <noreply+pashkinelfe@github.com>
2023-11-07 04:30 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-01-21 21:05 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-06-01 10:05 ` "kareldonk (@kareldonk)" <noreply+kareldonk@github.com>
2024-06-02 10:30 ` "david-gang (@david-gang)" <noreply+david-gang@github.com>
2024-09-22 21:55 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-30 22:14 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-30 22:14 ` "ankane (@ankane)" <noreply+ankane@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