mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-11 15:16:54 +08:00
Improved HNSW vacuum performance
This commit is contained in:
@@ -268,11 +268,12 @@ void HnswSetNeighborTuple(HnswNeighborTuple ntup, HnswElement e, int m);
|
|||||||
void HnswAddHeapTid(HnswElement element, ItemPointer heaptid);
|
void HnswAddHeapTid(HnswElement element, ItemPointer heaptid);
|
||||||
void HnswInitNeighbors(HnswElement element, int m);
|
void HnswInitNeighbors(HnswElement element, int m);
|
||||||
bool HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid, Relation heapRel);
|
bool HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heap_tid, Relation heapRel);
|
||||||
void UpdateNeighborPages(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement e, int m);
|
void UpdateNeighborPages(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement e, int m, HTAB *deleted);
|
||||||
void HnswLoadElement(HnswElement element, float *distance, Datum *q, Relation index, FmgrInfo *procinfo, Oid collation, bool loadVec);
|
void HnswLoadElement(HnswElement element, float *distance, Datum *q, Relation index, FmgrInfo *procinfo, Oid collation, bool loadVec);
|
||||||
void HnswSetElementTuple(HnswElementTuple etup, HnswElement element);
|
void HnswSetElementTuple(HnswElementTuple etup, HnswElement element);
|
||||||
void HnswUpdateConnection(HnswElement element, HnswCandidate * hc, int m, int lc, int *updateIdx, Relation index, FmgrInfo *procinfo, Oid collation);
|
void HnswUpdateConnection(HnswElement element, HnswCandidate * hc, int m, int lc, int *updateIdx, Relation index, FmgrInfo *procinfo, Oid collation);
|
||||||
void HnswLoadNeighbors(HnswElement element, Relation index);
|
void HnswLoadNeighbors(HnswElement element, Relation index);
|
||||||
|
bool HnswDeletedContains(HTAB *deleted, ItemPointer indextid);
|
||||||
|
|
||||||
/* Index access methods */
|
/* Index access methods */
|
||||||
IndexBuildResult *hnswbuild(Relation heap, Relation index, IndexInfo *indexInfo);
|
IndexBuildResult *hnswbuild(Relation heap, Relation index, IndexInfo *indexInfo);
|
||||||
|
|||||||
@@ -283,11 +283,33 @@ WriteNewElementPages(Relation index, HnswElement e, int m, BlockNumber insertPag
|
|||||||
*updatedInsertPage = newInsertPage;
|
*updatedInsertPage = newInsertPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Get index of deleted neighbor
|
||||||
|
*/
|
||||||
|
static int
|
||||||
|
GetDeletedIndex(HnswElement element, HTAB *deleted, int lc)
|
||||||
|
{
|
||||||
|
HnswNeighborArray *neighbors = &element->neighbors[lc];
|
||||||
|
|
||||||
|
for (int i = 0; i < neighbors->length; i++)
|
||||||
|
{
|
||||||
|
HnswCandidate *hc = &neighbors->items[i];
|
||||||
|
ItemPointerData ipData;
|
||||||
|
|
||||||
|
ItemPointerSet(&ipData, hc->element->blkno, hc->element->offno);
|
||||||
|
|
||||||
|
if (HnswDeletedContains(deleted, &ipData))
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Update neighbors
|
* Update neighbors
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
UpdateNeighborPages(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement e, int m)
|
UpdateNeighborPages(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement e, int m, HTAB *deleted)
|
||||||
{
|
{
|
||||||
for (int lc = e->level; lc >= 0; lc--)
|
for (int lc = e->level; lc >= 0; lc--)
|
||||||
{
|
{
|
||||||
@@ -311,8 +333,13 @@ UpdateNeighborPages(Relation index, FmgrInfo *procinfo, Oid collation, HnswEleme
|
|||||||
/* Do not lock yet since selecting neighbors can take time */
|
/* Do not lock yet since selecting neighbors can take time */
|
||||||
HnswLoadNeighbors(hc->element, index);
|
HnswLoadNeighbors(hc->element, index);
|
||||||
|
|
||||||
|
/* Find deleted index without loading elements if possible */
|
||||||
|
if (deleted != NULL)
|
||||||
|
idx = GetDeletedIndex(hc->element, deleted, lc);
|
||||||
|
|
||||||
/* Select neighbors */
|
/* Select neighbors */
|
||||||
HnswUpdateConnection(e, hc, lm, lc, &idx, index, procinfo, collation);
|
if (idx == -1)
|
||||||
|
HnswUpdateConnection(e, hc, lm, lc, &idx, index, procinfo, collation);
|
||||||
|
|
||||||
/* New element was not selected as a neighbor */
|
/* New element was not selected as a neighbor */
|
||||||
if (idx == -1)
|
if (idx == -1)
|
||||||
@@ -484,7 +511,7 @@ WriteElement(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement elem
|
|||||||
HnswUpdateMetaPage(index, false, NULL, newInsertPage, MAIN_FORKNUM);
|
HnswUpdateMetaPage(index, false, NULL, newInsertPage, MAIN_FORKNUM);
|
||||||
|
|
||||||
/* Update neighbors */
|
/* Update neighbors */
|
||||||
UpdateNeighborPages(index, procinfo, collation, element, m);
|
UpdateNeighborPages(index, procinfo, collation, element, m, NULL);
|
||||||
|
|
||||||
/* Update metapage if needed */
|
/* Update metapage if needed */
|
||||||
if (element->level > entryPoint->level)
|
if (element->level > entryPoint->level)
|
||||||
|
|||||||
@@ -10,8 +10,8 @@
|
|||||||
/*
|
/*
|
||||||
* Check if deleted list contains an index tid
|
* Check if deleted list contains an index tid
|
||||||
*/
|
*/
|
||||||
static bool
|
bool
|
||||||
DeletedContains(HTAB *deleted, ItemPointer indextid)
|
HnswDeletedContains(HTAB *deleted, ItemPointer indextid)
|
||||||
{
|
{
|
||||||
bool found;
|
bool found;
|
||||||
|
|
||||||
@@ -167,7 +167,7 @@ NeedsUpdated(HnswVacuumState * vacuumstate, HnswElement element)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* Check if in deleted list */
|
/* Check if in deleted list */
|
||||||
if (DeletedContains(vacuumstate->deleted, indextid))
|
if (HnswDeletedContains(vacuumstate->deleted, indextid))
|
||||||
{
|
{
|
||||||
needsUpdated = true;
|
needsUpdated = true;
|
||||||
break;
|
break;
|
||||||
@@ -255,7 +255,7 @@ RepairGraphElement(HnswVacuumState * vacuumstate, HnswElement element)
|
|||||||
UnlockReleaseBuffer(buf);
|
UnlockReleaseBuffer(buf);
|
||||||
|
|
||||||
/* Update neighbors */
|
/* Update neighbors */
|
||||||
UpdateNeighborPages(index, procinfo, collation, element, m);
|
UpdateNeighborPages(index, procinfo, collation, element, m, vacuumstate->deleted);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -293,7 +293,7 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate)
|
|||||||
|
|
||||||
ItemPointerSet(&epData, entryPoint->blkno, entryPoint->offno);
|
ItemPointerSet(&epData, entryPoint->blkno, entryPoint->offno);
|
||||||
|
|
||||||
if (DeletedContains(vacuumstate->deleted, &epData))
|
if (HnswDeletedContains(vacuumstate->deleted, &epData))
|
||||||
HnswUpdateMetaPage(index, true, highestPoint, InvalidBlockNumber, MAIN_FORKNUM);
|
HnswUpdateMetaPage(index, true, highestPoint, InvalidBlockNumber, MAIN_FORKNUM);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user