From 0094c9784eef914324a465c2dd96ba01cb2943ba Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Fri, 4 Dec 2026 04:30:24 -0800 Subject: [PATCH] Switched to item pointers for HNSW vacuuming to reduce uninitialized fields [skip ci] --- src/hnsw.h | 4 ++-- src/hnswvacuum.c | 51 ++++++++++++++++++++++-------------------------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/hnsw.h b/src/hnsw.h index d7cfff1d..baae1b10 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -476,8 +476,8 @@ typedef struct HnswVacuumState struct tidhash_hash *deleting; BufferAccessStrategy bas; HnswNeighborTuple ntup; - HnswElementData highestPoint; - HnswElementData fallbackPoint; + ItemPointerData highestPoint; + ItemPointerData fallbackPoint; /* Memory */ MemoryContext tmpCtx; diff --git a/src/hnswvacuum.c b/src/hnswvacuum.c index 112b6fd2..46d1f886 100644 --- a/src/hnswvacuum.c +++ b/src/hnswvacuum.c @@ -42,22 +42,15 @@ static void RemoveHeapTids(HnswVacuumState * vacuumstate) { BlockNumber blkno = HNSW_HEAD_BLKNO; - HnswElement highestPoint = &vacuumstate->highestPoint; - HnswElement fallbackPoint = &vacuumstate->fallbackPoint; + ItemPointer highestPoint = &vacuumstate->highestPoint; + ItemPointer fallbackPoint = &vacuumstate->fallbackPoint; Relation index = vacuumstate->index; BufferAccessStrategy bas = vacuumstate->bas; IndexBulkDeleteResult *stats = vacuumstate->stats; - /* Store separately since HnswElement level is uint8 */ int highestLevel = -1; int fallbackLevel = -1; - /* Initialize highest point and fallback point */ - highestPoint->blkno = InvalidBlockNumber; - highestPoint->offno = InvalidOffsetNumber; - fallbackPoint->blkno = InvalidBlockNumber; - fallbackPoint->offno = InvalidOffsetNumber; - while (BlockNumberIsValid(blkno)) { Buffer buf; @@ -138,27 +131,21 @@ RemoveHeapTids(HnswVacuumState * vacuumstate) } else if (etup->level > highestLevel) { - if (BlockNumberIsValid(highestPoint->blkno)) + if (ItemPointerIsValid(highestPoint)) { /* Current highest point becomes fallback */ - fallbackPoint->blkno = highestPoint->blkno; - fallbackPoint->offno = highestPoint->offno; - fallbackPoint->level = highestPoint->level; + ItemPointerCopy(highestPoint, fallbackPoint); fallbackLevel = highestLevel; } /* Keep track of highest point */ - highestPoint->blkno = blkno; - highestPoint->offno = offno; - highestPoint->level = etup->level; + ItemPointerSet(highestPoint, blkno, offno); highestLevel = etup->level; } else if (etup->level > fallbackLevel) { /* Keep track of second highest point */ - fallbackPoint->blkno = blkno; - fallbackPoint->offno = offno; - fallbackPoint->level = etup->level; + ItemPointerSet(fallbackPoint, blkno, offno); fallbackLevel = etup->level; } } @@ -293,6 +280,18 @@ RepairGraphElement(HnswVacuumState * vacuumstate, HnswElement element, HnswEleme MemoryContextReset(vacuumstate->repairCtx); } +/* + * Try to initialize an element from an index TID + */ +static HnswElement +TryElementFromTid(ItemPointer tid) +{ + if (!ItemPointerIsValid(tid)) + return NULL; + + return HnswInitElementFromBlock(ItemPointerGetBlockNumber(tid), ItemPointerGetOffsetNumber(tid)); +} + /* * Repair graph entry point */ @@ -301,12 +300,11 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate) { Relation index = vacuumstate->index; HnswSupport *support = &vacuumstate->support; - HnswElement highestPoint = &vacuumstate->highestPoint; + HnswElement highestPoint; HnswElement entryPoint; MemoryContext oldCtx = MemoryContextSwitchTo(vacuumstate->tmpCtx); - if (!BlockNumberIsValid(highestPoint->blkno)) - highestPoint = NULL; + highestPoint = TryElementFromTid(&vacuumstate->highestPoint); /* * Repair graph for highest non-entry point. Highest point may be outdated @@ -322,12 +320,7 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate) /* Use fallback point if highest point is entry point */ if (entryPoint != NULL && entryPoint->blkno == highestPoint->blkno && entryPoint->offno == highestPoint->offno) - { - highestPoint = &vacuumstate->fallbackPoint; - - if (!BlockNumberIsValid(highestPoint->blkno)) - highestPoint = NULL; - } + highestPoint = TryElementFromTid(&vacuumstate->fallbackPoint); if (highestPoint != NULL) { @@ -896,6 +889,8 @@ InitVacuumState(HnswVacuumState * vacuumstate, IndexVacuumInfo *info, IndexBulkD vacuumstate->efConstruction = HnswGetEfConstruction(index); vacuumstate->bas = info->strategy; vacuumstate->ntup = palloc0(HNSW_TUPLE_ALLOC_SIZE); + ItemPointerSetInvalid(&vacuumstate->highestPoint); + ItemPointerSetInvalid(&vacuumstate->fallbackPoint); vacuumstate->tmpCtx = AllocSetContextCreate(CurrentMemoryContext, "Hnsw vacuum temporary context", ALLOCSET_DEFAULT_SIZES);