From: ron-marcus (@ron-marcus) <noreply+ron-marcus@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: [pgvector/pgvector] issue #637: Update neighbors tuple atomically
Date: Tue, 06 Aug 2024 12:09:29 +0000
Message-ID: <gh-pgvector-pgvector-637@github.com> (raw)
Hi,
I would like to inquire about a potential concurrency issue in hnswUpdateNeighborsOnDisk().
From my understanding, updating an adjacency list should be an atomic read-modify-write (RMW) operation performed under a lock. However, I noticed that the current implementation reads the list, updates it locally, and only then acquires a lock to update on disk. The list is read under a lock, but the lock is released once the read is done.
In a scenario where two new vectors are added concurrently and both processes attempt to modify the same neighbor tuple, it appears that both may read the same original neighbor tuple when calling hnswUpdateNeighborsOnDisk(). This could result in only one update being stored on disk, potentially overwriting the other.
There is a comment stating, "Do not lock yet since selecting neighbors can take time." Could you please explain why it is acceptable not to acquire the lock immediately? Am I missing something? Is this a tradeoff between correctness and performance?
Thanks!