Improved HNSW graph repair - #239

This commit is contained in:
Andrew Kane
2023-08-26 14:06:03 -07:00
parent bb1e5ed98f
commit cfaa2ecd7f

View File

@@ -623,10 +623,6 @@ HnswSearchLayer(Datum q, List *ep, int ef, int lc, Relation index, FmgrInfo *pro
Assert(!e->element->deleted); Assert(!e->element->deleted);
/* Skip self for vacuuming update */
if (skipElement != NULL && e->element->blkno == skipElement->blkno && e->element->offno == skipElement->offno)
continue;
/* Make robust to issues */ /* Make robust to issues */
if (e->element->level < lc) if (e->element->level < lc)
continue; continue;
@@ -913,10 +909,10 @@ HnswUpdateConnection(HnswElement element, HnswCandidate * hc, int m, int lc, int
} }
/* /*
* Remove elements being deleted * Remove elements being deleted or skipped
*/ */
static List * static List *
RemoveElementsBeingDeleted(List *w) RemoveElements(List *w, HnswElement skipElement)
{ {
ListCell *lc2; ListCell *lc2;
List *w2 = NIL; List *w2 = NIL;
@@ -925,6 +921,10 @@ RemoveElementsBeingDeleted(List *w)
{ {
HnswCandidate *hc = (HnswCandidate *) lfirst(lc2); HnswCandidate *hc = (HnswCandidate *) lfirst(lc2);
/* Skip self for vacuuming update */
if (skipElement != NULL && hc->element->blkno == skipElement->blkno && hc->element->offno == skipElement->offno)
continue;
if (list_length(hc->element->heaptids) != 0) if (list_length(hc->element->heaptids) != 0)
w2 = lappend(w2, hc); w2 = lappend(w2, hc);
} }
@@ -963,20 +963,27 @@ HnswInsertElement(HnswElement element, HnswElement entryPoint, Relation index, F
if (level > entryLevel) if (level > entryLevel)
level = entryLevel; level = entryLevel;
/* Add one for existing element */
if (existing)
efConstruction++;
/* 2nd phase */ /* 2nd phase */
for (int lc = level; lc >= 0; lc--) for (int lc = level; lc >= 0; lc--)
{ {
int lm = HnswGetLayerM(m, lc); int lm = HnswGetLayerM(m, lc);
List *neighbors; List *neighbors;
List *lw;
w = HnswSearchLayer(q, ep, efConstruction, lc, index, procinfo, collation, true, skipElement); w = HnswSearchLayer(q, ep, efConstruction, lc, index, procinfo, collation, true, skipElement);
/* Elements being deleted can help with search */ /* Elements being deleted or skipped can help with search */
/* but should be removed before selecting neighbors */ /* but should be removed before selecting neighbors */
if (index != NULL) if (index != NULL)
w = RemoveElementsBeingDeleted(w); lw = RemoveElements(w, skipElement);
else
lw = w;
neighbors = SelectNeighbors(w, lm, lc, procinfo, collation, NULL); neighbors = SelectNeighbors(lw, lm, lc, procinfo, collation, NULL);
AddConnections(element, neighbors, lm, lc); AddConnections(element, neighbors, lm, lc);