mirror of
https://github.com/pgvector/pgvector.git
synced 2026-12-05 16:58:38 +00:00
Switched to item pointers for HNSW vacuuming to reduce uninitialized fields [skip ci]
This commit is contained in:
1 parent
a2cf46fe49
commit
0094c9784e
2 files changed
+25
-30
No files matched your search
+2
-2
@@ -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;
|
||||
|
||||
+23
-28
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user