pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control?
9+ messages / 2 participants
[nested] [flat]
* [pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control?
@ 2024-09-30 23:55 "kinghuang (@kinghuang)" <noreply+kinghuang@github.com>
0 siblings, 0 replies; 9+ messages in thread
From: kinghuang (@kinghuang) @ 2024-09-30 23:55 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
I have pgvector 0.7.2 installed and using hnsw indexes on several tables. Given a `hnsw.ef_search` value like 200, I've noticed that rows come back in noticeable "steps". That is, I get the first 6 rows almost right away, then another 25 rows 2 seconds later, then another 125 rows 8 seconds later, before finally reaching 200.
What causes the rows to be returned in such a pattern? Is it something I can control or optimize for? I've tried fiddling with `hnsw.ef_search` in queries and the `m` and `ef_construction` parameters in indexes, but I always get the bumps at 6, 25, 125, etc.
Edit: The return pattern actually appears to be 1, 5, 25, 125, …. The time between the first row and the next five is miniscule. Didn't notice it at first.
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control?
@ 2024-10-01 00:15 "ankane (@ankane)" <noreply+ankane@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: ankane (@ankane) @ 2024-10-01 00:15 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @kinghuang, what's the output of `EXPLAIN ANALYZE`?
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control?
@ 2024-10-01 00:50 "kinghuang (@kinghuang)" <noreply+kinghuang@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: kinghuang (@kinghuang) @ 2024-10-01 00:50 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
This is with `hnsw.ef_search = 200`.
```sql
select
e.doc_id,
e.ext_id,
e.source_id,
e.reference_datetime,
cs.dist as cs_dist
from dlph_doc__embedding_4 e
cross join lateral (
select embedding <=> dlph__ml.vertexai_embedding('example')::vector(768) dist
) cs
order by cs.dist;
```
```
Index Scan using dlph_doc__embedding_4_embedding_idx on dlph_doc__embedding_4 e (cost=27.01..11700.84 rows=38162 width=36) (actual time=152.395..12480.575 rows=207 loops=1)
Order By: (embedding <=> (dlph__ml.vertexai_embedding('example'::text, 'text-embedding-004'::text))::vector(768))
Planning Time: 0.129 ms
Execution Time: 12480.767 ms
```
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control?
@ 2024-10-01 01:32 "ankane (@ankane)" <noreply+ankane@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: ankane (@ankane) @ 2024-10-01 01:32 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Are you using a hosted Postgres service?
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control?
@ 2024-10-01 01:38 "kinghuang (@kinghuang)" <noreply+kinghuang@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: kinghuang (@kinghuang) @ 2024-10-01 01:38 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Yeah, I'm on Crunchy Bridge by Crunchy Data.
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control?
@ 2024-10-01 01:50 "ankane (@ankane)" <noreply+ankane@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: ankane (@ankane) @ 2024-10-01 01:50 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hmm, pgvector currently gets all rows from the index before the first one is returned, so I don't think the issue is there. Also, I'm not able to get that query to use an index when testing locally with Postgres 16 (even with `enable_seqscan = off`).
If you pass a vector instead of a function call, does it still happen? If not, I'd see if `vertexai_embedding` is marked as `VOLATILE` (the default).
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control?
@ 2024-10-01 02:56 "ankane (@ankane)" <noreply+ankane@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: ankane (@ankane) @ 2024-10-01 02:56 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
It looks like this is due to how the query is written and the function being `STABLE` (as it doesn't happen with `IMMUTABLE` and `VOLATILE` won't use the index).
```sql
CREATE OR REPLACE FUNCTION embed(t text) RETURNS vector AS $$
BEGIN
RAISE NOTICE 'embed: %', t;
RETURN '[3,1,2]';
END;
$$ LANGUAGE plpgsql STABLE;
CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]'), ('[7,8,9]');
SET enable_seqscan = off;
EXPLAIN ANALYZE SELECT * FROM items CROSS JOIN LATERAL (SELECT embedding <=> embed('text')::vector(3) dist) cs ORDER BY dist;
```
This will call the function (and print `embed: text`) 4 times - once for the initial query and then for each result.
If you change how the query is written, it'll only call the function once.
```sql
EXPLAIN ANALYZE SELECT * FROM items ORDER BY embedding <=> (SELECT embed('text')::vector(3)) LIMIT 5;
```
Also, I'd recommend adding `LIMIT` to the query (rather than relying on `ef_search` to limit results).
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control?
@ 2024-10-01 02:56 "ankane (@ankane)" <noreply+ankane@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: ankane (@ankane) @ 2024-10-01 02:56 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 9+ messages in thread
* Re: [pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control?
@ 2024-10-01 03:00 "kinghuang (@kinghuang)" <noreply+kinghuang@github.com>
7 siblings, 0 replies; 9+ messages in thread
From: kinghuang (@kinghuang) @ 2024-10-01 03:00 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Wow, you're totally right. Thank you so much for digging into this!
^ permalink raw [nested|flat] 9+ messages in thread
end of thread, other threads:[~2024-10-01 03:00 UTC | newest]
Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-09-30 23:55 [pgvector/pgvector] issue #687: Query returns indexed rows with noticeable "steps"; how to control? "kinghuang (@kinghuang)" <noreply+kinghuang@github.com>
2024-10-01 00:15 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-01 00:50 ` "kinghuang (@kinghuang)" <noreply+kinghuang@github.com>
2024-10-01 01:32 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-01 01:38 ` "kinghuang (@kinghuang)" <noreply+kinghuang@github.com>
2024-10-01 01:50 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-01 02:56 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-01 02:56 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-10-01 03:00 ` "kinghuang (@kinghuang)" <noreply+kinghuang@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