From: knizhnik (@knizhnik) <noreply+knizhnik@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] PR #282: Hnsw iterator
Date: Wed, 25 Sep 2024 12:01:42 +0000
Message-ID: <gh-pgvector-pgvector-282-c2373886689@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-282@github.com>
References: <gh-pgvector-pgvector-282@github.com>
> Hi @knizhnik, two things that can happen with the approach in this PR related to vacuuming are:
>
> 1. An element can be replaced between reading the element tuple and reading the neighbor tuple, giving an incorrect list of neighbors.
> 2. When reading neighbors, one of the elements may be replaced between reading the neighbor tuple and reading the element tuple, giving an incorrect element.
>
> #678 addresses this by:
>
> 1. Adding a version to each element
> 2. Using `HNSW_SCAN_LOCK` (which prevents both 1 and 2 in the non-iterative approach). This ensures vacuuming waits for current scan iterations to complete before marking elements as deleted (at which point an element can be immediately replaced). Iterations that start after vacuuming begins marking elements as deleted won't see any of those elements in neighbor tuples, avoiding this issue. Another approach (requiring a bit more space) would be to save the element version of each neighbor in the neighbor tuple.
Thank you for clarification. But then I have few questions:
1. How element version can preserve consistency? Right now if you find element with unexpected version you just stop traversal. But it means that scan may return less results than expected, may not it?
2. Is it be possible to hold `HNSW_SCAN_LOCK` until the end of the scan and also obtain in in index append? In this case not concurrent modification of index are osible during scan and there is no need to track element's versions. Yes certainly in this case we can not perform concurrent updates and select from the index. But it is really critical? I think that in most HNSW use cases data is loaded and then is accessed by queries.
3. What will happen if some long query is not finished during 16 vacuum iterations? Very unlikely but...
4. Why versioning and locking is used only for vacuum and scan? Why for index inserts synchronisation is not needed?