From 483173460bb610882060c00f0d5006f950a2e964 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Thu, 10 Aug 2023 14:35:24 -0700 Subject: [PATCH] Improved concurrent inserts with empty entry point --- src/hnswinsert.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/hnswinsert.c b/src/hnswinsert.c index bfd2b5f..250fc78 100644 --- a/src/hnswinsert.c +++ b/src/hnswinsert.c @@ -392,7 +392,7 @@ HnswAddDuplicate(Relation index, HnswElement element, HnswElement dup) * Write changes to disk */ static void -WriteElement(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement element, int m, HnswElement dup, HnswElement entryPoint) +WriteElement(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement element, int m, int efConstruction, HnswElement dup, HnswElement entryPoint) { /* Try to add to existing page */ if (dup != NULL) @@ -407,7 +407,19 @@ WriteElement(Relation index, FmgrInfo *procinfo, Oid collation, HnswElement elem /* Update metapage if needed */ if (entryPoint == NULL || element->level > entryPoint->level) - HnswUpdateMetaPage(index, true, element, InvalidBlockNumber, MAIN_FORKNUM); + { + /* TODO Lock metapage for entire block */ + HnswElement newEntryPoint = HnswGetEntryPoint(index); + + if (entryPoint == NULL && newEntryPoint != NULL) + { + /* Try again with new entry point */ + HnswInsertElement(element, newEntryPoint, index, procinfo, collation, m, efConstruction, false); + UpdateNeighborPages(index, procinfo, collation, element, m); + } + else + HnswUpdateMetaPage(index, true, element, InvalidBlockNumber, MAIN_FORKNUM); + } } /* @@ -452,7 +464,7 @@ HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heap_ti dup = HnswFindDuplicate(element); /* Write to disk */ - WriteElement(index, procinfo, collation, element, m, dup, entryPoint); + WriteElement(index, procinfo, collation, element, m, efConstruction, dup, entryPoint); return true; }