From a2408e60fa48b492efb5526cc18ba72841b61b37 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Fri, 11 Oct 2024 14:57:57 -0700 Subject: [PATCH] Revert "Added IndexTuple to HNSW elements (first step to support multiple attributes)" This reverts commit 53a8734bacc96dd33eb2193c6560f07d93246ef3. --- src/hnsw.h | 8 ++------ src/hnswbuild.c | 29 ++++++++++++----------------- src/hnswinsert.c | 15 ++++++--------- src/hnswutils.c | 22 +++------------------- 4 files changed, 23 insertions(+), 51 deletions(-) diff --git a/src/hnsw.h b/src/hnsw.h index fd34de1..6b184ec 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -133,7 +133,6 @@ HnswPtrDeclare(HnswElementData, HnswElementRelptr, HnswElementPtr); HnswPtrDeclare(HnswNeighborArray, HnswNeighborArrayRelptr, HnswNeighborArrayPtr); HnswPtrDeclare(HnswNeighborArrayPtr, HnswNeighborsRelptr, HnswNeighborsPtr); HnswPtrDeclare(char, DatumRelptr, DatumPtr); -HnswPtrDeclare(IndexTupleData, IndexTupleRelptr, IndexTuplePtr); struct HnswElementData { @@ -150,7 +149,6 @@ struct HnswElementData OffsetNumber neighborOffno; BlockNumber neighborPage; DatumPtr value; - IndexTuplePtr itup; LWLock lock; }; @@ -290,7 +288,6 @@ typedef struct HnswBuildState HnswGraph *graph; double ml; int maxLevel; - TupleDesc tupdesc; /* Memory */ MemoryContext graphCtx; @@ -431,12 +428,11 @@ void HnswSetNeighborTuple(char *base, HnswNeighborTuple ntup, HnswElement e, in void HnswAddHeapTid(HnswElement element, ItemPointer heaptid); HnswNeighborArray *HnswInitNeighborArray(int lm, HnswAllocator * allocator); void HnswInitNeighbors(char *base, HnswElement element, int m, HnswAllocator * alloc); -bool HnswInsertTupleOnDisk(Relation index, HnswSupport * support, TupleDesc tupdesc, IndexTuple itup, ItemPointer heaptid, bool building); +bool HnswInsertTupleOnDisk(Relation index, HnswSupport * support, Datum value, ItemPointer heaptid, bool building); void HnswUpdateNeighborsOnDisk(Relation index, HnswSupport * support, HnswElement e, int m, bool checkExisting, bool building); void HnswLoadElementFromTuple(HnswElement element, HnswElementTuple etup, bool loadHeaptids, bool loadVec); void HnswLoadElement(HnswElement element, double *distance, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec, double *maxDistance); -TupleDesc HnswTupleDesc(Relation index); -bool HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support, TupleDesc tupdesc); +bool HnswFormIndexValue(Datum *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support); void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element); void HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newElement, float distance, int lm, int *updateIdx, Relation index, HnswSupport * support); bool HnswLoadNeighborTids(HnswElement element, ItemPointerData *indextids, Relation index, int m, int lm, int lc); diff --git a/src/hnswbuild.c b/src/hnswbuild.c index 85fc060..b667478 100644 --- a/src/hnswbuild.c +++ b/src/hnswbuild.c @@ -476,20 +476,18 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn HnswElement element; HnswAllocator *allocator = &buildstate->allocator; HnswSupport *support = &buildstate->support; + Size valueSize; + Pointer valuePtr; LWLock *flushLock = &graph->flushLock; char *base = buildstate->hnswarea; - TupleDesc tupdesc = buildstate->tupdesc; - IndexTuple itup; - Size itupSize; - IndexTuple itupShared; - bool unused; + Datum value; /* Form index value */ - if (!HnswFormIndexTuple(&itup, values, isnull, buildstate->typeInfo, support, tupdesc)) + if (!HnswFormIndexValue(&value, values, isnull, buildstate->typeInfo, support)) return false; - /* Get tuple size */ - itupSize = IndexTupleSize(itup); + /* Get datum size */ + valueSize = VARSIZE_ANY(DatumGetPointer(value)); /* Ensure graph not flushed when inserting */ LWLockAcquire(flushLock, LW_SHARED); @@ -499,7 +497,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn { LWLockRelease(flushLock); - return HnswInsertTupleOnDisk(index, support, tupdesc, itup, heaptid, true); + return HnswInsertTupleOnDisk(index, support, value, heaptid, true); } /* @@ -531,12 +529,12 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn LWLockRelease(flushLock); - return HnswInsertTupleOnDisk(index, support, tupdesc, itup, heaptid, true); + return HnswInsertTupleOnDisk(index, support, value, heaptid, true); } /* Ok, we can proceed to allocate the element */ element = HnswInitElement(base, heaptid, buildstate->m, buildstate->ml, buildstate->maxLevel, allocator); - itupShared = HnswAlloc(allocator, itupSize); + valuePtr = HnswAlloc(allocator, valueSize); /* * We have now allocated the space needed for the element, so we don't @@ -545,10 +543,9 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn */ LWLockRelease(&graph->allocatorLock); - /* Copy the tuple */ - memcpy(itupShared, itup, itupSize); - HnswPtrStore(base, element->itup, itupShared); - HnswPtrStore(base, element->value, DatumGetPointer(index_getattr(itupShared, 1, tupdesc, &unused))); + /* Copy the datum */ + memcpy(valuePtr, DatumGetPointer(value), valueSize); + HnswPtrStore(base, element->value, valuePtr); /* Create a lock for the element */ LWLockInitialize(&element->lock, hnsw_lock_tranche_id); @@ -701,7 +698,6 @@ InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, Index buildstate->graph = &buildstate->graphData; buildstate->ml = HnswGetMl(buildstate->m); buildstate->maxLevel = HnswGetMaxLevel(buildstate->m); - buildstate->tupdesc = HnswTupleDesc(index); buildstate->graphCtx = GenerationContextCreate(CurrentMemoryContext, "Hnsw build graph context", @@ -726,7 +722,6 @@ InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, Index static void FreeBuildState(HnswBuildState * buildstate) { - pfree(buildstate->tupdesc); MemoryContextDelete(buildstate->graphCtx); MemoryContextDelete(buildstate->tmpCtx); } diff --git a/src/hnswinsert.c b/src/hnswinsert.c index a1447e4..a5fac4e 100644 --- a/src/hnswinsert.c +++ b/src/hnswinsert.c @@ -687,7 +687,7 @@ UpdateGraphOnDisk(Relation index, HnswSupport * support, HnswElement element, in * Insert a tuple into the index */ bool -HnswInsertTupleOnDisk(Relation index, HnswSupport * support, TupleDesc tupdesc, IndexTuple itup, ItemPointer heaptid, bool building) +HnswInsertTupleOnDisk(Relation index, HnswSupport * support, Datum value, ItemPointer heaptid, bool building) { HnswElement entryPoint; HnswElement element; @@ -695,7 +695,6 @@ HnswInsertTupleOnDisk(Relation index, HnswSupport * support, TupleDesc tupdesc, int efConstruction = HnswGetEfConstruction(index); LOCKMODE lockmode = ShareLock; char *base = NULL; - bool unused; /* * Get a shared lock. This allows vacuum to ensure no in-flight inserts @@ -709,8 +708,7 @@ HnswInsertTupleOnDisk(Relation index, HnswSupport * support, TupleDesc tupdesc, /* Create an element */ element = HnswInitElement(base, heaptid, m, HnswGetMl(m), HnswGetMaxLevel(m), NULL); - HnswPtrStore(base, element->itup, itup); - HnswPtrStore(base, element->value, DatumGetPointer(index_getattr(itup, 1, tupdesc, &unused))); + HnswPtrStore(base, element->value, DatumGetPointer(value)); /* Prevent concurrent inserts when likely updating entry point */ if (entryPoint == NULL || element->level > entryPoint->level) @@ -744,18 +742,17 @@ HnswInsertTupleOnDisk(Relation index, HnswSupport * support, TupleDesc tupdesc, static void HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid) { - IndexTuple itup; + Datum value; const HnswTypeInfo *typeInfo = HnswGetTypeInfo(index); HnswSupport support; - TupleDesc tupdesc = HnswTupleDesc(index); HnswInitSupport(&support, index); - /* Form index tuple */ - if (!HnswFormIndexTuple(&itup, values, isnull, typeInfo, &support, tupdesc)) + /* Form index value */ + if (!HnswFormIndexValue(&value, values, isnull, typeInfo, &support)) return; - HnswInsertTupleOnDisk(index, &support, tupdesc, itup, heaptid, false); + HnswInsertTupleOnDisk(index, &support, value, heaptid, false); } /* diff --git a/src/hnswutils.c b/src/hnswutils.c index ad3edfa..e84561e 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -395,24 +395,10 @@ HnswUpdateMetaPage(Relation index, int updateEntry, HnswElement entryPoint, Bloc } /* - * Get the tuple descriptor - */ -TupleDesc -HnswTupleDesc(Relation index) -{ - TupleDesc tupdesc = CreateTupleDescCopyConstr(RelationGetDescr(index)); - - /* Prevent compression */ - TupleDescAttr(tupdesc, 0)->attstorage = TYPSTORAGE_PLAIN; - - return tupdesc; -} - -/* - * Form index tuple + * Form index value */ bool -HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support, TupleDesc tupdesc) +HnswFormIndexValue(Datum *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support) { /* Detoast once for all calls */ Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0])); @@ -430,9 +416,7 @@ HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeI value = HnswNormValue(typeInfo, support->collation, value); } - /* TODO Combine value with values to support multiple attributes */ - Assert(tupdesc->natts == 1); - *out = index_form_tuple(tupdesc, &value, isnull); + *out = value; return true; }