mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-06 20:50:57 +08:00
Fixed possible index corruption with HNSW vacuuming - resolves #988
Co-authored-by: Bhagyesh Chaturvedi <bhagyeshc@google.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
## 0.8.3 (unreleased)
|
## 0.8.3 (unreleased)
|
||||||
|
|
||||||
|
- Fixed possible index corruption with HNSW vacuuming
|
||||||
- Fixed performance regression with Hamming distance and Jaccard distance with Postgres 18
|
- Fixed performance regression with Hamming distance and Jaccard distance with Postgres 18
|
||||||
|
|
||||||
## 0.8.2 (2026-02-25)
|
## 0.8.2 (2026-02-25)
|
||||||
|
|||||||
@@ -412,6 +412,7 @@ typedef struct HnswVacuumState
|
|||||||
BufferAccessStrategy bas;
|
BufferAccessStrategy bas;
|
||||||
HnswNeighborTuple ntup;
|
HnswNeighborTuple ntup;
|
||||||
HnswElementData highestPoint;
|
HnswElementData highestPoint;
|
||||||
|
HnswElementData fallbackPoint;
|
||||||
|
|
||||||
/* Memory */
|
/* Memory */
|
||||||
MemoryContext tmpCtx;
|
MemoryContext tmpCtx;
|
||||||
|
|||||||
@@ -37,17 +37,20 @@ RemoveHeapTids(HnswVacuumState * vacuumstate)
|
|||||||
{
|
{
|
||||||
BlockNumber blkno = HNSW_HEAD_BLKNO;
|
BlockNumber blkno = HNSW_HEAD_BLKNO;
|
||||||
HnswElement highestPoint = &vacuumstate->highestPoint;
|
HnswElement highestPoint = &vacuumstate->highestPoint;
|
||||||
|
HnswElement fallbackPoint = &vacuumstate->fallbackPoint;
|
||||||
Relation index = vacuumstate->index;
|
Relation index = vacuumstate->index;
|
||||||
BufferAccessStrategy bas = vacuumstate->bas;
|
BufferAccessStrategy bas = vacuumstate->bas;
|
||||||
HnswElement entryPoint = HnswGetEntryPoint(vacuumstate->index);
|
|
||||||
IndexBulkDeleteResult *stats = vacuumstate->stats;
|
IndexBulkDeleteResult *stats = vacuumstate->stats;
|
||||||
|
|
||||||
/* Store separately since highestPoint.level is uint8 */
|
/* Store separately since HnswElement level is uint8 */
|
||||||
int highestLevel = -1;
|
int highestLevel = -1;
|
||||||
|
int fallbackLevel = -1;
|
||||||
|
|
||||||
/* Initialize highest point */
|
/* Initialize highest point and fallback point */
|
||||||
highestPoint->blkno = InvalidBlockNumber;
|
highestPoint->blkno = InvalidBlockNumber;
|
||||||
highestPoint->offno = InvalidOffsetNumber;
|
highestPoint->offno = InvalidOffsetNumber;
|
||||||
|
fallbackPoint->blkno = InvalidBlockNumber;
|
||||||
|
fallbackPoint->offno = InvalidOffsetNumber;
|
||||||
|
|
||||||
while (BlockNumberIsValid(blkno))
|
while (BlockNumberIsValid(blkno))
|
||||||
{
|
{
|
||||||
@@ -119,14 +122,31 @@ RemoveHeapTids(HnswVacuumState * vacuumstate)
|
|||||||
tidhash_insert(vacuumstate->deleted, ip, &found);
|
tidhash_insert(vacuumstate->deleted, ip, &found);
|
||||||
Assert(!found);
|
Assert(!found);
|
||||||
}
|
}
|
||||||
else if (etup->level > highestLevel && !(entryPoint != NULL && blkno == entryPoint->blkno && offno == entryPoint->offno))
|
else if (etup->level > highestLevel)
|
||||||
{
|
{
|
||||||
/* Keep track of highest non-entry point */
|
if (BlockNumberIsValid(highestPoint->blkno))
|
||||||
|
{
|
||||||
|
/* Current highest point becomes fallback */
|
||||||
|
fallbackPoint->blkno = highestPoint->blkno;
|
||||||
|
fallbackPoint->offno = highestPoint->offno;
|
||||||
|
fallbackPoint->level = highestPoint->level;
|
||||||
|
fallbackLevel = highestLevel;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Keep track of highest point */
|
||||||
highestPoint->blkno = blkno;
|
highestPoint->blkno = blkno;
|
||||||
highestPoint->offno = offno;
|
highestPoint->offno = offno;
|
||||||
highestPoint->level = etup->level;
|
highestPoint->level = etup->level;
|
||||||
highestLevel = etup->level;
|
highestLevel = etup->level;
|
||||||
}
|
}
|
||||||
|
else if (etup->level > fallbackLevel)
|
||||||
|
{
|
||||||
|
/* Keep track of second highest point */
|
||||||
|
fallbackPoint->blkno = blkno;
|
||||||
|
fallbackPoint->offno = offno;
|
||||||
|
fallbackPoint->level = etup->level;
|
||||||
|
fallbackLevel = etup->level;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
blkno = HnswPageGetOpaque(page)->nextblkno;
|
blkno = HnswPageGetOpaque(page)->nextblkno;
|
||||||
@@ -269,12 +289,27 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate)
|
|||||||
/* Get a shared lock */
|
/* Get a shared lock */
|
||||||
LockPage(index, HNSW_UPDATE_LOCK, ShareLock);
|
LockPage(index, HNSW_UPDATE_LOCK, ShareLock);
|
||||||
|
|
||||||
/* Load element */
|
/* Get latest entry point */
|
||||||
HnswLoadElement(highestPoint, NULL, NULL, index, support, true, NULL);
|
entryPoint = HnswGetEntryPoint(index);
|
||||||
|
|
||||||
/* Repair if needed */
|
/* Use fallback point if highest point is entry point */
|
||||||
if (NeedsUpdated(vacuumstate, highestPoint))
|
if (entryPoint != NULL && entryPoint->blkno == highestPoint->blkno && entryPoint->offno == highestPoint->offno)
|
||||||
RepairGraphElement(vacuumstate, highestPoint, HnswGetEntryPoint(index));
|
{
|
||||||
|
highestPoint = &vacuumstate->fallbackPoint;
|
||||||
|
|
||||||
|
if (!BlockNumberIsValid(highestPoint->blkno))
|
||||||
|
highestPoint = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (highestPoint != NULL)
|
||||||
|
{
|
||||||
|
/* Load element */
|
||||||
|
HnswLoadElement(highestPoint, NULL, NULL, index, support, true, NULL);
|
||||||
|
|
||||||
|
/* Repair if needed */
|
||||||
|
if (NeedsUpdated(vacuumstate, highestPoint))
|
||||||
|
RepairGraphElement(vacuumstate, highestPoint, entryPoint);
|
||||||
|
}
|
||||||
|
|
||||||
/* Release lock */
|
/* Release lock */
|
||||||
UnlockPage(index, HNSW_UPDATE_LOCK, ShareLock);
|
UnlockPage(index, HNSW_UPDATE_LOCK, ShareLock);
|
||||||
|
|||||||
Reference in New Issue
Block a user