From: ankane (@ankane) <noreply+ankane@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #678: Iterative index scans
Date: Sun, 22 Sep 2024 19:34:05 +0000
Message-ID: <gh-pgvector-pgvector-678@github.com> (raw)
Hi all, I wanted to share some work on iterative index scans to get feedback.
- [hnsw-streaming branch](https://github.com/pgvector/pgvector/compare/hnsw-streaming)
- [ivfflat-streaming branch](https://github.com/pgvector/pgvector/compare/ivfflat-streaming)
You can enable this functionality (naming TDB) with:
```sql
SET hnsw.streaming = on;
SET ivfflat.streaming = on;
```
For HNSW, it keeps track of discarded candidates at layer 0. When more tuples are needed, it calls `HnswSearchLayer` / Algorithm 2 with the nearest discarded candidates as entry points (in batches of `ef_search`). The scan terminates when enough tuples are found, `hnsw.ef_stream` elements are visited, or `work_mem` is exceeded.
For IVFFlat, it scans the next closest lists in groups of `ivfflat.probes`, up to `ivfflat.max_probes`.
---
One issue I'm having trouble addressing is how to terminate scans for queries with distance filters. In the query below, if only 9 records are within the distance, it'll continue scanning the index. I've tried using `xs_orderbyvals` on `IndexScanDesc`, but it doesn't seem to help.
```sql
SELECT * FROM items WHERE embedding <-> $1 < 0.1 ORDER BY embedding <-> $1 LIMIT 10;
```