From: jkatz (@jkatz) <noreply+jkatz@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] PR #682: Update HNSW cost estimatation to utilize search and index info
Date: Wed, 25 Sep 2024 18:15:43 +0000
Message-ID: <gh-pgvector-pgvector-682-c2374840373@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-682@github.com>
References: <gh-pgvector-pgvector-682@github.com>
Note: I've fixed a previous test failure (it was a TODO that's now resolved with the updated cost estimation model :smile:). However, the active failure is interesting. The failing query is:
```sql
EXPLAIN ANALYZE SELECT i FROM tst WHERE v <-> '$query' < 1 ORDER BY v <-> '$query';
```
The dataset size is 10K vectors, and to explicitly state, this is a query with a filter that is ordering the vectors but doesn't have an explicit limit. `hnsw.ef_search` is `40`.
Previously, the costing was such that PostgreSQL estimated the sequential scan was cheaper and opted for that.
With the current patch, PostgreSQL estimates the index scan is cheaper (by almost less than a single cost unit 😨) and opts to use that. This may very well be the fastest scan path, but (untested theory) in streaming mode would this end up using the full index scan, which would certainly be more expensive than a sequential scan?
I have a locally modified version of the patch that borrows similar selectivity logic to IVFFlat (`MIN(our estimate, PostgreSQL's selectivity estimate)`) but PostgreSQL still opted for the sequential scan.
Primarily posting this as a FYI. I'm still pulling a few threads.