Skip to content

Commit a2408e6

Browse files
committed
Revert "Added IndexTuple to HNSW elements (first step to support multiple attributes)"
This reverts commit 53a8734.
1 parent 53a8734 commit a2408e6

File tree

4 files changed

+23
-51
lines changed

4 files changed

+23
-51
lines changed

src/hnsw.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ HnswPtrDeclare(HnswElementData, HnswElementRelptr, HnswElementPtr);
133133
HnswPtrDeclare(HnswNeighborArray, HnswNeighborArrayRelptr, HnswNeighborArrayPtr);
134134
HnswPtrDeclare(HnswNeighborArrayPtr, HnswNeighborsRelptr, HnswNeighborsPtr);
135135
HnswPtrDeclare(char, DatumRelptr, DatumPtr);
136-
HnswPtrDeclare(IndexTupleData, IndexTupleRelptr, IndexTuplePtr);
137136

138137
struct HnswElementData
139138
{
@@ -150,7 +149,6 @@ struct HnswElementData
150149
OffsetNumber neighborOffno;
151150
BlockNumber neighborPage;
152151
DatumPtr value;
153-
IndexTuplePtr itup;
154152
LWLock lock;
155153
};
156154

@@ -290,7 +288,6 @@ typedef struct HnswBuildState
290288
HnswGraph *graph;
291289
double ml;
292290
int maxLevel;
293-
TupleDesc tupdesc;
294291

295292
/* Memory */
296293
MemoryContext graphCtx;
@@ -431,12 +428,11 @@ void HnswSetNeighborTuple(char *base, HnswNeighborTuple ntup, HnswElement e, in
431428
void HnswAddHeapTid(HnswElement element, ItemPointer heaptid);
432429
HnswNeighborArray *HnswInitNeighborArray(int lm, HnswAllocator * allocator);
433430
void HnswInitNeighbors(char *base, HnswElement element, int m, HnswAllocator * alloc);
434-
bool HnswInsertTupleOnDisk(Relation index, HnswSupport * support, TupleDesc tupdesc, IndexTuple itup, ItemPointer heaptid, bool building);
431+
bool HnswInsertTupleOnDisk(Relation index, HnswSupport * support, Datum value, ItemPointer heaptid, bool building);
435432
void HnswUpdateNeighborsOnDisk(Relation index, HnswSupport * support, HnswElement e, int m, bool checkExisting, bool building);
436433
void HnswLoadElementFromTuple(HnswElement element, HnswElementTuple etup, bool loadHeaptids, bool loadVec);
437434
void HnswLoadElement(HnswElement element, double *distance, HnswQuery * q, Relation index, HnswSupport * support, bool loadVec, double *maxDistance);
438-
TupleDesc HnswTupleDesc(Relation index);
439-
bool HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support, TupleDesc tupdesc);
435+
bool HnswFormIndexValue(Datum *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support);
440436
void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element);
441437
void HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newElement, float distance, int lm, int *updateIdx, Relation index, HnswSupport * support);
442438
bool HnswLoadNeighborTids(HnswElement element, ItemPointerData *indextids, Relation index, int m, int lm, int lc);

src/hnswbuild.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -476,20 +476,18 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
476476
HnswElement element;
477477
HnswAllocator *allocator = &buildstate->allocator;
478478
HnswSupport *support = &buildstate->support;
479+
Size valueSize;
480+
Pointer valuePtr;
479481
LWLock *flushLock = &graph->flushLock;
480482
char *base = buildstate->hnswarea;
481-
TupleDesc tupdesc = buildstate->tupdesc;
482-
IndexTuple itup;
483-
Size itupSize;
484-
IndexTuple itupShared;
485-
bool unused;
483+
Datum value;
486484

487485
/* Form index value */
488-
if (!HnswFormIndexTuple(&itup, values, isnull, buildstate->typeInfo, support, tupdesc))
486+
if (!HnswFormIndexValue(&value, values, isnull, buildstate->typeInfo, support))
489487
return false;
490488

491-
/* Get tuple size */
492-
itupSize = IndexTupleSize(itup);
489+
/* Get datum size */
490+
valueSize = VARSIZE_ANY(DatumGetPointer(value));
493491

494492
/* Ensure graph not flushed when inserting */
495493
LWLockAcquire(flushLock, LW_SHARED);
@@ -499,7 +497,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
499497
{
500498
LWLockRelease(flushLock);
501499

502-
return HnswInsertTupleOnDisk(index, support, tupdesc, itup, heaptid, true);
500+
return HnswInsertTupleOnDisk(index, support, value, heaptid, true);
503501
}
504502

505503
/*
@@ -531,12 +529,12 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn
531529

532530
LWLockRelease(flushLock);
533531

534-
return HnswInsertTupleOnDisk(index, support, tupdesc, itup, heaptid, true);
532+
return HnswInsertTupleOnDisk(index, support, value, heaptid, true);
535533
}
536534

537535
/* Ok, we can proceed to allocate the element */
538536
element = HnswInitElement(base, heaptid, buildstate->m, buildstate->ml, buildstate->maxLevel, allocator);
539-
itupShared = HnswAlloc(allocator, itupSize);
537+
valuePtr = HnswAlloc(allocator, valueSize);
540538

541539
/*
542540
* 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
545543
*/
546544
LWLockRelease(&graph->allocatorLock);
547545

548-
/* Copy the tuple */
549-
memcpy(itupShared, itup, itupSize);
550-
HnswPtrStore(base, element->itup, itupShared);
551-
HnswPtrStore(base, element->value, DatumGetPointer(index_getattr(itupShared, 1, tupdesc, &unused)));
546+
/* Copy the datum */
547+
memcpy(valuePtr, DatumGetPointer(value), valueSize);
548+
HnswPtrStore(base, element->value, valuePtr);
552549

553550
/* Create a lock for the element */
554551
LWLockInitialize(&element->lock, hnsw_lock_tranche_id);
@@ -701,7 +698,6 @@ InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, Index
701698
buildstate->graph = &buildstate->graphData;
702699
buildstate->ml = HnswGetMl(buildstate->m);
703700
buildstate->maxLevel = HnswGetMaxLevel(buildstate->m);
704-
buildstate->tupdesc = HnswTupleDesc(index);
705701

706702
buildstate->graphCtx = GenerationContextCreate(CurrentMemoryContext,
707703
"Hnsw build graph context",
@@ -726,7 +722,6 @@ InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, Index
726722
static void
727723
FreeBuildState(HnswBuildState * buildstate)
728724
{
729-
pfree(buildstate->tupdesc);
730725
MemoryContextDelete(buildstate->graphCtx);
731726
MemoryContextDelete(buildstate->tmpCtx);
732727
}

src/hnswinsert.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -687,15 +687,14 @@ UpdateGraphOnDisk(Relation index, HnswSupport * support, HnswElement element, in
687687
* Insert a tuple into the index
688688
*/
689689
bool
690-
HnswInsertTupleOnDisk(Relation index, HnswSupport * support, TupleDesc tupdesc, IndexTuple itup, ItemPointer heaptid, bool building)
690+
HnswInsertTupleOnDisk(Relation index, HnswSupport * support, Datum value, ItemPointer heaptid, bool building)
691691
{
692692
HnswElement entryPoint;
693693
HnswElement element;
694694
int m;
695695
int efConstruction = HnswGetEfConstruction(index);
696696
LOCKMODE lockmode = ShareLock;
697697
char *base = NULL;
698-
bool unused;
699698

700699
/*
701700
* Get a shared lock. This allows vacuum to ensure no in-flight inserts
@@ -709,8 +708,7 @@ HnswInsertTupleOnDisk(Relation index, HnswSupport * support, TupleDesc tupdesc,
709708

710709
/* Create an element */
711710
element = HnswInitElement(base, heaptid, m, HnswGetMl(m), HnswGetMaxLevel(m), NULL);
712-
HnswPtrStore(base, element->itup, itup);
713-
HnswPtrStore(base, element->value, DatumGetPointer(index_getattr(itup, 1, tupdesc, &unused)));
711+
HnswPtrStore(base, element->value, DatumGetPointer(value));
714712

715713
/* Prevent concurrent inserts when likely updating entry point */
716714
if (entryPoint == NULL || element->level > entryPoint->level)
@@ -744,18 +742,17 @@ HnswInsertTupleOnDisk(Relation index, HnswSupport * support, TupleDesc tupdesc,
744742
static void
745743
HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid)
746744
{
747-
IndexTuple itup;
745+
Datum value;
748746
const HnswTypeInfo *typeInfo = HnswGetTypeInfo(index);
749747
HnswSupport support;
750-
TupleDesc tupdesc = HnswTupleDesc(index);
751748

752749
HnswInitSupport(&support, index);
753750

754-
/* Form index tuple */
755-
if (!HnswFormIndexTuple(&itup, values, isnull, typeInfo, &support, tupdesc))
751+
/* Form index value */
752+
if (!HnswFormIndexValue(&value, values, isnull, typeInfo, &support))
756753
return;
757754

758-
HnswInsertTupleOnDisk(index, &support, tupdesc, itup, heaptid, false);
755+
HnswInsertTupleOnDisk(index, &support, value, heaptid, false);
759756
}
760757

761758
/*

src/hnswutils.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -395,24 +395,10 @@ HnswUpdateMetaPage(Relation index, int updateEntry, HnswElement entryPoint, Bloc
395395
}
396396

397397
/*
398-
* Get the tuple descriptor
399-
*/
400-
TupleDesc
401-
HnswTupleDesc(Relation index)
402-
{
403-
TupleDesc tupdesc = CreateTupleDescCopyConstr(RelationGetDescr(index));
404-
405-
/* Prevent compression */
406-
TupleDescAttr(tupdesc, 0)->attstorage = TYPSTORAGE_PLAIN;
407-
408-
return tupdesc;
409-
}
410-
411-
/*
412-
* Form index tuple
398+
* Form index value
413399
*/
414400
bool
415-
HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support, TupleDesc tupdesc)
401+
HnswFormIndexValue(Datum *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support)
416402
{
417403
/* Detoast once for all calls */
418404
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
@@ -430,9 +416,7 @@ HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeI
430416
value = HnswNormValue(typeInfo, support->collation, value);
431417
}
432418

433-
/* TODO Combine value with values to support multiple attributes */
434-
Assert(tupdesc->natts == 1);
435-
*out = index_form_tuple(tupdesc, &value, isnull);
419+
*out = value;
436420

437421
return true;
438422
}

0 commit comments

Comments
 (0)