From a18bea24b8cd3c3c3084373f99ffe434c60b368c Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Thu, 10 Aug 2023 15:07:28 -0700 Subject: [PATCH] Improved concurrent inserts with low number of elements --- src/hnswinsert.c | 16 ++++++++++++++-- src/hnswutils.c | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/hnswinsert.c b/src/hnswinsert.c index 250fc78..0bccfb4 100644 --- a/src/hnswinsert.c +++ b/src/hnswinsert.c @@ -290,6 +290,7 @@ UpdateNeighborPages(Relation index, FmgrInfo *procinfo, Oid collation, HnswEleme HnswNeighborTuple ntup; Size ntupSize; int idx = -1; + int startIdx; OffsetNumber offno = hc->element->neighborOffno; /* Get latest neighbors */ @@ -312,10 +313,21 @@ UpdateNeighborPages(Relation index, FmgrInfo *procinfo, Oid collation, HnswEleme ntupSize = ItemIdGetLength(itemid); /* Calculate index for update */ - idx += (hc->element->level - lc) * m; + startIdx = (hc->element->level - lc) * m; + + if (idx == -2) + { + /* Find free offset if still exists */ + /* TODO Retry updating connections if not */ + for (int i = 0; i < lm; i++) + if (!ItemPointerIsValid(&ntup->indextids[startIdx + i])) + idx = startIdx + i; + } + else + idx += startIdx; /* Make robust to issues */ - if (idx < ntup->count) + if (idx >= 0 && idx < ntup->count) { ItemPointer indextid = &ntup->indextids[idx]; diff --git a/src/hnswutils.c b/src/hnswutils.c index b763647..9e4dbdc 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -818,7 +818,7 @@ HnswUpdateConnection(HnswElement element, HnswCandidate * hc, int m, int lc, int /* Track update */ if (updateIdx != NULL) - *updateIdx = currentNeighbors->length - 1; + *updateIdx = -2; } else {