pgvector.git / summary / log / commit / refs
commit 2f7507eb7aab397725bdf4bb77bc2f4000acac66
Author: Andrew Kane <andrew@ankane.org>
Date: Sat Oct 31 20:12:29 2026 +0000
Fixed possible error with iterative index scans during HNSW vacuuming
CHANGELOG.md | 1 +
src/hnswvacuum.c | 125 ++++++++++++++++++++++++++++++++++++-----
test/t/046_hnsw_vacuum_scan.pl | 2 +-
3 files changed, 112 insertions(+), 16 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b82e658d..a724b193 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,7 @@
- Improved performance of HNSW inserts with `sparsevec`
- Improved `amvalidate` for indexes
- Improved property testing for indexes
+- Fixed possible error with iterative index scans during HNSW vacuuming
- Dropped support for Postgres 13 and 14
## 0.8.7 (2026-10-01)
diff --git a/src/hnswvacuum.c b/src/hnswvacuum.c
index 69020a36..507ed00f 100644
--- a/src/hnswvacuum.c
+++ b/src/hnswvacuum.c
@@ -502,6 +502,108 @@ RepairGraph(HnswVacuumState * vacuumstate)
}
}
+/*
+ * Repair deleting neighbors
+ */
+static void
+RepairDeletingNeighbors(HnswVacuumState * vacuumstate, ItemPointer neighbortid)
+{
+ Relation index = vacuumstate->index;
+ BufferAccessStrategy bas = vacuumstate->bas;
+ Buffer nbuf;
+ Page npage;
+ GenericXLogState *state;
+ HnswNeighborTuple ntup;
+
+ nbuf = ReadBufferExtended(index, MAIN_FORKNUM, ItemPointerGetBlockNumber(neighbortid), RBM_NORMAL, bas);
+ LockBuffer(nbuf, BUFFER_LOCK_EXCLUSIVE);
+ state = GenericXLogStart(index);
+ npage = GenericXLogRegisterBuffer(state, nbuf, 0);
+ ntup = (HnswNeighborTuple) PageGetItem(npage, PageGetItemId(npage, ItemPointerGetOffsetNumber(neighbortid)));
+
+ /* Overwrite neighbors */
+ for (int i = 0; i < ntup->count; i++)
+ ItemPointerSetInvalid(&ntup->indextids[i]);
+
+ /*
+ * We modified the tuple in place, no need to call PageIndexTupleOverwrite
+ */
+
+ /* Commit */
+ GenericXLogFinish(state);
+ UnlockReleaseBuffer(nbuf);
+}
+
+/*
+ * Repair deleting
+ */
+static void
+RepairDeleting(HnswVacuumState * vacuumstate)
+{
+ Relation index = vacuumstate->index;
+ BufferAccessStrategy bas = vacuumstate->bas;
+ BlockNumber blkno = HNSW_HEAD_BLKNO;
+
+ /*
+ * Wait for inserts and index scans to complete. Inserts and scans started
+ * before this point may visit tuples about to be deleted. Inserts and
+ * scans started after this point will not, since the graph has been
+ * repaired. However, resumed iterative scans may still visit.
+ */
+ LockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock);
+ UnlockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock);
+
+ LockPage(index, HNSW_SCAN_LOCK, ExclusiveLock);
+ UnlockPage(index, HNSW_SCAN_LOCK, ExclusiveLock);
+
+ while (BlockNumberIsValid(blkno))
+ {
+ Buffer buf;
+ Page page;
+ OffsetNumber offno;
+ OffsetNumber maxoffno;
+ ItemPointerData neighbortids[MaxOffsetNumber];
+ int neighbortidsLength = 0;
+
+ vacuum_delay_point();
+
+ buf = ReadBufferExtended(index, MAIN_FORKNUM, blkno, RBM_NORMAL, bas);
+ LockBuffer(buf, BUFFER_LOCK_SHARE);
+ page = BufferGetPage(buf);
+ maxoffno = PageGetMaxOffsetNumber(page);
+
+ /* Load items into memory to minimize locking */
+ for (offno = FirstOffsetNumber; offno <= maxoffno; offno = OffsetNumberNext(offno))
+ {
+ HnswElementTuple etup = (HnswElementTuple) PageGetItem(page, PageGetItemId(page, offno));
+ ItemPointer neighbortid;
+
+ /* Skip neighbor tuples */
+ if (!HnswIsElementTuple(etup))
+ continue;
+
+ /* Skip deleted tuples */
+ if (etup->deleted)
+ continue;
+
+ /* Skip live tuples */
+ if (ItemPointerIsValid(&etup->heaptids[0]))
+ continue;
+
+ /* Get neighbor TID */
+ neighbortid = &neighbortids[neighbortidsLength++];
+ ItemPointerCopy(&etup->neighbortid, neighbortid);
+ }
+
+ blkno = HnswPageGetOpaque(page)->nextblkno;
+
+ UnlockReleaseBuffer(buf);
+
+ for (int i = 0; i < neighbortidsLength; i++)
+ RepairDeletingNeighbors(vacuumstate, &neighbortids[i]);
+ }
+}
+
/*
* Confirm graph was repaired
*/
@@ -544,10 +646,6 @@ ConfirmRepaired(HnswVacuumState * vacuumstate)
if (etup->deleted)
continue;
- /* Skip if being deleted */
- if (!ItemPointerIsValid(&etup->heaptids[0]))
- continue;
-
/* Get neighbor page */
neighborPage = ItemPointerGetBlockNumber(&etup->neighbortid);
neighborOffno = ItemPointerGetOffsetNumber(&etup->neighbortid);
@@ -601,16 +699,7 @@ MarkDeleted(HnswVacuumState * vacuumstate)
BufferAccessStrategy bas = vacuumstate->bas;
bool useFsm = HnswGetTypeInfo(index)->useFsm;
- /*
- * Wait for inserts and index scans to complete. Inserts and scans before
- * this point may visit tuples about to be deleted. Inserts and scans
- * after this point will not, since the graph has been repaired.
- */
- LockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock);
- UnlockPage(index, HNSW_UPDATE_LOCK, ExclusiveLock);
-
- ConfirmRepaired(vacuumstate);
-
+ /* Wait for iterative scan batch to complete */
LockPage(index, HNSW_SCAN_LOCK, ExclusiveLock);
UnlockPage(index, HNSW_SCAN_LOCK, ExclusiveLock);
@@ -803,7 +892,13 @@ hnswbulkdelete(IndexVacuumInfo *info, IndexBulkDeleteResult *stats,
/* Pass 2: Repair graph */
HnswBench("RepairGraph", RepairGraph(&vacuumstate));
- /* Passes 3 and 4: Confirm repaired and mark as deleted */
+ /* Pass 3: Repair deleting */
+ HnswBench("RepairDeleting", RepairDeleting(&vacuumstate));
+
+ /* Pass 4: Confirm repaired */
+ HnswBench("ConfirmRepaired", ConfirmRepaired(&vacuumstate));
+
+ /* Pass 5: Mark as deleted */
HnswBench("MarkDeleted", MarkDeleted(&vacuumstate));
FreeVacuumState(&vacuumstate);
diff --git a/test/t/046_hnsw_vacuum_scan.pl b/test/t/046_hnsw_vacuum_scan.pl
index cfdfa74d..909b8209 100644
--- a/test/t/046_hnsw_vacuum_scan.pl
+++ b/test/t/046_hnsw_vacuum_scan.pl
@@ -30,7 +30,7 @@ $node->pgbench(
[qr{^$}],
"concurrent SELECTs and VACUUM",
{
- "046_hnsw_vacuum_scan_select\@1000" => "SELECT i FROM tst ORDER BY v <-> '[0,0,0]' LIMIT 10;",
+ "046_hnsw_vacuum_scan_select\@1000" => "SET hnsw.iterative_scan = strict_order; SELECT i FROM tst ORDER BY v <-> '[0,0,0]' LIMIT 10;",
"046_hnsw_vacuum_scan_vacuum\@1" => "VACUUM tst;"
}
);
[parent: 8a3ca2ed589b]