pg.ddx.io pgvector/pgvector GitHub issues and pull requests
help / color / mirror / Atom feed[pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
13+ messages / 3 participants
[nested] [flat]
* [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-11 12:06 "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
0 siblings, 0 replies; 13+ messages in thread
From: vinodhbalasubramanian (@vinodhbalasubramanian) @ 2024-09-11 12:06 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Postgres gives inconsistent result count when it uses the HNSW index vs index not being used.
I am using PGVector HNSW USING hnsw (embedding vector_cosine_ops) WITH (m='16', ef_construction='32');
IFacing an issue with the below sample query
WITH filtered_opportunities AS (SELECT sf.notice_id FROM opportunity_filter sf
WHERE
sf.full_parent_path_name IN ('ABC SERVICE')
//and has_related_award=true // line 2
)
SELECT sv.notice_id FROM semantic_vector sv JOIN filtered_opportunities fo ON fo.notice_id = sv.notice_id
ORDER BY sv.embedding <=> cast('[-0.5078125]’) limit 200
From the above query, It gives me a result count of 30 by having one filter condition, if I add another AND condition (uncomment line 2) it is giving me 200 records (ideally adding more where condition should reduce the resultset).
The difference in above is, with the first query, it uses HNSW index, so postgres fetches 1000 records from vector table & then apply filter on top of it which reduces the resultset, in the second query postgres doesn't uses HNSW index, so it fetches more records while applying the filter at same time. This is a major issue in our application, where the filters behaves differently based on HNSW index used/not used.
Is this is a Postgres bug? any way to solve this?
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-11 13:58 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: jkatz (@jkatz) @ 2024-09-11 13:58 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
There is a long discussion about this here: https://github.com/pgvector/pgvector/issues/244 - in short, what's happening is that the index is returning rows that don't match the filters. In addition to ongoing development, there are a number of whats to handle this, including:
1. Use a different index for the filtering (e.g. B-tree [the default PostgreSQL index] / GIN etc.) - based on selectivity (the number of results returned), this may be faster than the vector index, and also provide 100% recall
2. Set `hnsw.ef_search` to a higher value to allow more results ot be returned
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-11 16:39 ` "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: vinodhbalasubramanian (@vinodhbalasubramanian) @ 2024-09-11 16:39 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@jkatz Thanks for your reply.
1. Is there a development on going to fix this bug? any approximate timeframe when we could expect this fix?
2. Do you mean to use Btree/GIN index for the other two fields (full_parent_path_name & has_related_award) ? It is already BTree indexed .
3. We are already using hnsw.ef_search=1000 which is maximum and (m='16', ef_construction='64')
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-16 14:07 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: jkatz (@jkatz) @ 2024-09-16 14:07 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@vinodhbalasubramanian This isn't a bug, but an active area of research. Please see #244.
If your data is already B-tree indexed, and you're using those columns in the filters, I would suggest using that instead of a HNSW index to see if you can achieve your query latency target. The benefit of that is you'll also have 100% recall.
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-18 07:23 ` "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: vinodhbalasubramanian (@vinodhbalasubramanian) @ 2024-09-18 07:23 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@jkatz Could you please explain why this is not considered as a bug? I run my query with one filter, it gives me less records & when I add another filter, it gives me more records, as a developer it is not a code logic bug, this discrepancy occurs based on Postgres query plan usage (HNSW index vs filter index), we could not control Postgres query plan.
Also your suggestion to use B-tree index & avoid HNSW index, it is not possible because:
1. Whenever we don't apply the filter conditions then we need to use HNSW index for faster retrieval
2. In some conditions Postgres doesn't use the B-tree index Ex: if the filter parameter has a condition that pulls heavy data (Ex: amount>1 and amount <100000), in this case Postgres doesn't use any of the B-tree indexes even though amount parameter is B-Tree index.
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-18 13:56 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: jkatz (@jkatz) @ 2024-09-18 13:56 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@vinodhbalasubramanian As mentioned previously, this has to do with how the HNSW index is searching and returning rows. The search occurs on vector similarity, and it doesn't account for the filters. The filters are applied after the index is scanned. You can increase the number of results returned from the index by increasing `hnsw.ef_search`.
However, as stated (and discussed in #244), this is in general an area of active research around considering filters as part of a vector similarity search. If you have examples of the types of filters that you're running on your similarity search queries, along with overall dataset size and how much data you expect to be returned by a filter, that would help with determining how we want to proceed with supporting in-index filtering (aka "prefiltering").
> In some conditions Postgres doesn't use the B-tree index Ex: if the filter parameter has a condition that pulls heavy data (Ex: amount>1 and amount <100000), in this case Postgres doesn't use any of the B-tree indexes even though amount parameter is B-Tree index.
This may be because the PostgreSQL query planner believes a sequential scan is faster than an index scan for this case (and it very may well be, without knowing anything about your data).
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-19 16:38 ` "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: vinodhbalasubramanian (@vinodhbalasubramanian) @ 2024-09-19 16:38 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@jkatz Already our hnsw.ef_search is set to 1000 I hope it is maximum in PGVector. If we apply on top of this 1000 records then we are loosing records count significantly. Any approax timeline when we are trying to implement this HNSW fix to consider the filters?
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-19 20:59 ` "ankane (@ankane)" <noreply+ankane@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: ankane (@ankane) @ 2024-09-19 20:59 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane closed this.
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-19 20:59 ` "ankane (@ankane)" <noreply+ankane@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: ankane (@ankane) @ 2024-09-19 20:59 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Hi @vinodhbalasubramanian, there isn't a timeline right now.
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-20 01:01 ` "ankane (@ankane)" <noreply+ankane@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: ankane (@ankane) @ 2024-09-20 01:01 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Spoke too soon, see #677 for the current plan.
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-20 06:33 ` "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: vinodhbalasubramanian (@vinodhbalasubramanian) @ 2024-09-20 06:33 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane Thanks for your response. reg. #677 "Filtering improvements-iterative index scans for HNSW". Will this change scans the embeddings along with the filters? will this change resolves our issue? i.e. HNSW scans along with applying filters at the same time?
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-20 06:49 ` "ankane (@ankane)" <noreply+ankane@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: ankane (@ankane) @ 2024-09-20 06:49 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
See https://github.com/pgvector/pgvector/issues/259#issuecomment-2362500744 for more info
^ permalink raw [nested|flat] 13+ messages in thread
* Re: [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused)
@ 2024-09-20 07:30 ` "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
11 siblings, 0 replies; 13+ messages in thread
From: vinodhbalasubramanian (@vinodhbalasubramanian) @ 2024-09-20 07:30 UTC (permalink / raw)
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
@ankane Thanks, I hope this "iterative index scans" will return us more records with filters. We will wait for this October release and see.
^ permalink raw [nested|flat] 13+ messages in thread
end of thread, other threads:[~2024-09-20 07:30 UTC | newest]
Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-09-11 12:06 [pgvector/pgvector] issue #671: HNSW + Filter inconsistent result issue (based on HNSW index used vs unused) "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
2024-09-11 13:58 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-09-11 16:39 ` "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
2024-09-16 14:07 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-09-18 07:23 ` "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
2024-09-18 13:56 ` "jkatz (@jkatz)" <noreply+jkatz@github.com>
2024-09-19 16:38 ` "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
2024-09-19 20:59 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-09-19 20:59 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-09-20 01:01 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-09-20 06:33 ` "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@github.com>
2024-09-20 06:49 ` "ankane (@ankane)" <noreply+ankane@github.com>
2024-09-20 07:30 ` "vinodhbalasubramanian (@vinodhbalasubramanian)" <noreply+vinodhbalasubramanian@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