mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-14 00:26:55 +08:00
Improved locking for HNSW vacuum
This commit is contained in:
@@ -374,14 +374,32 @@ RepairGraph(HnswVacuumState * vacuumstate)
|
|||||||
{
|
{
|
||||||
HnswElement element = (HnswElement) lfirst(lc2);
|
HnswElement element = (HnswElement) lfirst(lc2);
|
||||||
HnswElement entryPoint;
|
HnswElement entryPoint;
|
||||||
|
LOCKMODE lockmode = ShareLock;
|
||||||
|
|
||||||
/* Check if any neighbors point to deleted values */
|
/* Check if any neighbors point to deleted values */
|
||||||
if (!NeedsUpdated(vacuumstate, element))
|
if (!NeedsUpdated(vacuumstate, element))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
/* Get a shared lock */
|
||||||
|
LockPage(index, HNSW_UPDATE_LOCK, lockmode);
|
||||||
|
|
||||||
/* Refresh entry point for each element */
|
/* Refresh entry point for each element */
|
||||||
entryPoint = HnswGetEntryPoint(index);
|
entryPoint = HnswGetEntryPoint(index);
|
||||||
|
|
||||||
|
/* Prevent concurrent inserts when likely updating entry point */
|
||||||
|
if (entryPoint == NULL || element->level > entryPoint->level)
|
||||||
|
{
|
||||||
|
/* Release shared lock */
|
||||||
|
UnlockPage(index, HNSW_UPDATE_LOCK, lockmode);
|
||||||
|
|
||||||
|
/* Get exclusive lock */
|
||||||
|
lockmode = ExclusiveLock;
|
||||||
|
LockPage(index, HNSW_UPDATE_LOCK, lockmode);
|
||||||
|
|
||||||
|
/* Get latest entry point after lock is acquired */
|
||||||
|
entryPoint = HnswGetEntryPoint(index);
|
||||||
|
}
|
||||||
|
|
||||||
/* Repair connections */
|
/* Repair connections */
|
||||||
RepairGraphElement(vacuumstate, element, entryPoint);
|
RepairGraphElement(vacuumstate, element, entryPoint);
|
||||||
|
|
||||||
@@ -391,6 +409,9 @@ RepairGraph(HnswVacuumState * vacuumstate)
|
|||||||
*/
|
*/
|
||||||
if (entryPoint == NULL || element->level > entryPoint->level)
|
if (entryPoint == NULL || element->level > entryPoint->level)
|
||||||
HnswUpdateMetaPage(index, HNSW_UPDATE_ENTRY_GREATER, element, InvalidBlockNumber, MAIN_FORKNUM);
|
HnswUpdateMetaPage(index, HNSW_UPDATE_ENTRY_GREATER, element, InvalidBlockNumber, MAIN_FORKNUM);
|
||||||
|
|
||||||
|
/* Release lock */
|
||||||
|
UnlockPage(index, HNSW_UPDATE_LOCK, lockmode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Reset memory context */
|
/* Reset memory context */
|
||||||
|
|||||||
Reference in New Issue
Block a user