diff --git a/src/hnsw.h b/src/hnsw.h index a12f727..7748ec0 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -243,6 +243,13 @@ typedef struct HnswTypeInfo void (*checkValue) (Pointer v); } HnswTypeInfo; +typedef struct HnswSupport +{ + FmgrInfo *procinfo[2]; + FmgrInfo *normprocinfo; + Oid *collation; +} HnswSupport; + typedef struct HnswBuildState { /* Info */ @@ -262,9 +269,7 @@ typedef struct HnswBuildState double reltuples; /* Support functions */ - FmgrInfo *procinfo[2]; - FmgrInfo *normprocinfo; - Oid *collation; + HnswSupport support; /* Variables */ HnswGraph graphData; @@ -341,9 +346,7 @@ typedef struct HnswScanOpaqueData MemoryContext tmpCtx; /* Support functions */ - FmgrInfo *procinfo[2]; - FmgrInfo *normprocinfo; - Oid *collation; + HnswSupport support; } HnswScanOpaqueData; typedef HnswScanOpaqueData * HnswScanOpaque; @@ -361,8 +364,7 @@ typedef struct HnswVacuumState int efConstruction; /* Support functions */ - FmgrInfo *procinfo[2]; - Oid *collation; + HnswSupport support; /* Variables */ struct tidhash_hash *deleted; @@ -378,32 +380,32 @@ typedef struct HnswVacuumState int HnswGetM(Relation index); int HnswGetEfConstruction(Relation index); FmgrInfo *HnswOptionalProcInfo(Relation index, uint16 procnum); -void HnswSetProcinfo(Relation index, FmgrInfo **procinfo, FmgrInfo **normprocinfo, Oid **collation); +void HnswInitSupport(HnswSupport * support, Relation index); Datum HnswNormValue(const HnswTypeInfo * typeInfo, Oid collation, Datum value); -bool HnswCheckNorm(FmgrInfo *procinfo, Oid collation, Datum value); +bool HnswCheckNorm(HnswSupport * support, Datum value); Buffer HnswNewBuffer(Relation index, ForkNumber forkNum); void HnswInitPage(Buffer buf, Page page); void HnswInit(void); -List *HnswSearchLayer(char *base, Datum q, IndexTuple qtup, ScanKeyData *keyData, List *ep, int ef, int lc, Relation index, FmgrInfo **procinfo, Oid *collation, int m, bool inserting, HnswElement skipElement, bool inMemory); +List *HnswSearchLayer(char *base, Datum q, IndexTuple qtup, ScanKeyData *keyData, List *ep, int ef, int lc, Relation index, HnswSupport * support, int m, bool inserting, HnswElement skipElement, bool inMemory); HnswElement HnswGetEntryPoint(Relation index); void HnswGetMetaPageInfo(Relation index, int *m, HnswElement * entryPoint); void *HnswAlloc(HnswAllocator * allocator, Size size); HnswElement HnswInitElement(char *base, ItemPointer tid, int m, double ml, int maxLevel, HnswAllocator * alloc); HnswElement HnswInitElementFromBlock(BlockNumber blkno, OffsetNumber offno); -void HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint, Relation index, FmgrInfo **procinfo, Oid *collation, int m, int efConstruction, bool existing, bool inMemory); -HnswSearchCandidate *HnswEntryCandidate(char *base, HnswElement em, Datum q, IndexTuple qtup, ScanKeyData *keyData, Relation rel, FmgrInfo **procinfo, Oid *collation, bool loadVec, bool inMemory); +void HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint, Relation index, HnswSupport * support, int m, int efConstruction, bool existing, bool inMemory); +HnswSearchCandidate *HnswEntryCandidate(char *base, HnswElement em, Datum q, IndexTuple qtup, ScanKeyData *keyData, Relation rel, HnswSupport * support, bool loadVec, bool inMemory); void HnswUpdateMetaPage(Relation index, int updateEntry, HnswElement entryPoint, BlockNumber insertPage, ForkNumber forkNum, bool building); void HnswSetNeighborTuple(char *base, HnswNeighborTuple ntup, HnswElement e, int m); 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, FmgrInfo **procinfo, Oid *collation, IndexTuple itup, ItemPointer heaptid, bool building); -void HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo **procinfo, Oid *collation, HnswElement e, int m, bool checkExisting, bool building); +bool HnswInsertTupleOnDisk(Relation index, HnswSupport * support, IndexTuple itup, 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, Relation index); -void HnswLoadElement(HnswElement element, double *distance, bool *matches, Datum *q, IndexTuple qtup, ScanKeyData *keyData, Relation index, FmgrInfo **procinfo, Oid *collation, bool loadVec, double *maxDistance); +void HnswLoadElement(HnswElement element, double *distance, bool *matches, Datum *q, IndexTuple qtup, ScanKeyData *keyData, Relation index, HnswSupport * support, bool loadVec, double *maxDistance); void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element, bool useIndexTuple); -void HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newElement, float distance, int lm, int *updateIdx, Relation index, FmgrInfo **procinfo, Oid *collation); -bool HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, FmgrInfo *normprocinfo, Oid collation, TupleDesc tupdesc); +void HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newElement, float distance, int lm, int *updateIdx, Relation index, HnswSupport * support); +bool HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support, TupleDesc tupdesc); bool HnswLoadNeighborTids(HnswElement element, ItemPointerData *indextids, Relation index, int m, int lm, int lc); void HnswInitLockTranche(void); const HnswTypeInfo *HnswGetTypeInfo(Relation index); diff --git a/src/hnswbuild.c b/src/hnswbuild.c index fd07de4..3493659 100644 --- a/src/hnswbuild.c +++ b/src/hnswbuild.c @@ -365,7 +365,7 @@ AddElementInMemory(char *base, HnswGraph * graph, HnswElement element) * Update neighbors */ static void -UpdateNeighborsInMemory(char *base, Relation index, FmgrInfo **procinfo, Oid *collation, HnswElement e, int m) +UpdateNeighborsInMemory(char *base, Relation index, HnswSupport * support, HnswElement e, int m) { for (int lc = e->level; lc >= 0; lc--) { @@ -387,7 +387,7 @@ UpdateNeighborsInMemory(char *base, Relation index, FmgrInfo **procinfo, Oid *co Assert(neighborElement); LWLockAcquire(&neighborElement->lock, LW_EXCLUSIVE); - HnswUpdateConnection(base, HnswGetNeighbors(base, neighborElement, lc), e, hc->distance, lm, NULL, index, procinfo, collation); + HnswUpdateConnection(base, HnswGetNeighbors(base, neighborElement, lc), e, hc->distance, lm, NULL, index, support); LWLockRelease(&neighborElement->lock); } } @@ -397,7 +397,7 @@ UpdateNeighborsInMemory(char *base, Relation index, FmgrInfo **procinfo, Oid *co * Update graph in memory */ static void -UpdateGraphInMemory(FmgrInfo **procinfo, Oid *collation, HnswElement element, int m, int efConstruction, HnswElement entryPoint, HnswBuildState * buildstate) +UpdateGraphInMemory(HnswSupport * support, HnswElement element, int m, int efConstruction, HnswElement entryPoint, HnswBuildState * buildstate) { HnswGraph *graph = buildstate->graph; char *base = buildstate->hnswarea; @@ -410,7 +410,7 @@ UpdateGraphInMemory(FmgrInfo **procinfo, Oid *collation, HnswElement element, in AddElementInMemory(base, graph, element); /* Update neighbors */ - UpdateNeighborsInMemory(base, buildstate->index, procinfo, collation, element, m); + UpdateNeighborsInMemory(base, buildstate->index, support, element, m); /* Update entry point if needed (already have lock) */ if (entryPoint == NULL || element->level > entryPoint->level) @@ -424,9 +424,8 @@ static void InsertTupleInMemory(HnswBuildState * buildstate, HnswElement element) { Relation index = buildstate->index; - FmgrInfo **procinfo = buildstate->procinfo; - Oid *collation = buildstate->collation; HnswGraph *graph = buildstate->graph; + HnswSupport *support = &buildstate->support; HnswElement entryPoint; LWLock *entryLock = &graph->entryLock; LWLock *entryWaitLock = &graph->entryWaitLock; @@ -458,10 +457,10 @@ InsertTupleInMemory(HnswBuildState * buildstate, HnswElement element) } /* Find neighbors for element */ - HnswFindElementNeighbors(base, element, entryPoint, index, procinfo, collation, m, efConstruction, false, true); + HnswFindElementNeighbors(base, element, entryPoint, index, support, m, efConstruction, false, true); /* Update graph in memory */ - UpdateGraphInMemory(procinfo, collation, element, m, efConstruction, entryPoint, buildstate); + UpdateGraphInMemory(support, element, m, efConstruction, entryPoint, buildstate); /* Release entry lock */ LWLockRelease(entryLock); @@ -476,8 +475,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn HnswGraph *graph = buildstate->graph; HnswElement element; HnswAllocator *allocator = &buildstate->allocator; - FmgrInfo **procinfo = buildstate->procinfo; - Oid *collation = buildstate->collation; + HnswSupport *support = &buildstate->support; LWLock *flushLock = &graph->flushLock; char *base = buildstate->hnswarea; TupleDesc tupdesc = buildstate->tupdesc; @@ -487,7 +485,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn bool unused; /* Form index tuple */ - if (!HnswFormIndexTuple(&itup, values, isnull, buildstate->typeInfo, buildstate->normprocinfo, collation[0], tupdesc)) + if (!HnswFormIndexTuple(&itup, values, isnull, buildstate->typeInfo, support, tupdesc)) return false; /* Get tuple size */ @@ -501,7 +499,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn { LWLockRelease(flushLock); - return HnswInsertTupleOnDisk(index, procinfo, collation, itup, heaptid, true); + return HnswInsertTupleOnDisk(index, support, itup, heaptid, true); } /* @@ -533,7 +531,7 @@ InsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid, Hn LWLockRelease(flushLock); - return HnswInsertTupleOnDisk(index, procinfo, collation, itup, heaptid, true); + return HnswInsertTupleOnDisk(index, support, itup, heaptid, true); } /* Ok, we can proceed to allocate the element */ @@ -710,7 +708,7 @@ InitBuildState(HnswBuildState * buildstate, Relation heap, Relation index, Index buildstate->indtuples = 0; /* Get support functions */ - HnswSetProcinfo(index, buildstate->procinfo, &buildstate->normprocinfo, &buildstate->collation); + HnswInitSupport(&buildstate->support, index); InitGraph(&buildstate->graphData, NULL, (Size) maintenance_work_mem * 1024L); buildstate->graph = &buildstate->graphData; diff --git a/src/hnswinsert.c b/src/hnswinsert.c index 14698f9..b3cf9a3 100644 --- a/src/hnswinsert.c +++ b/src/hnswinsert.c @@ -369,7 +369,7 @@ HnswLoadNeighbors(HnswElement element, Relation index, int m, int lm, int lc) * Load elements for insert */ static void -LoadElementsForInsert(HnswNeighborArray * neighbors, Datum q, IndexTuple qtup, int *idx, Relation index, FmgrInfo **procinfo, Oid *collation) +LoadElementsForInsert(HnswNeighborArray * neighbors, Datum q, IndexTuple qtup, int *idx, Relation index, HnswSupport * support) { char *base = NULL; @@ -380,7 +380,7 @@ LoadElementsForInsert(HnswNeighborArray * neighbors, Datum q, IndexTuple qtup, i double distance; bool matches; - HnswLoadElement(element, &distance, &matches, &q, qtup, NULL, index, procinfo, collation, true, NULL); + HnswLoadElement(element, &distance, &matches, &q, qtup, NULL, index, support, true, NULL); hc->distance = distance; /* Prune element if being deleted */ @@ -396,7 +396,7 @@ LoadElementsForInsert(HnswNeighborArray * neighbors, Datum q, IndexTuple qtup, i * Get update index */ static int -GetUpdateIndex(HnswElement element, HnswElement newElement, float distance, int m, int lm, int lc, Relation index, FmgrInfo **procinfo, Oid *collation, MemoryContext updateCtx) +GetUpdateIndex(HnswElement element, HnswElement newElement, float distance, int m, int lm, int lc, Relation index, HnswSupport * support, MemoryContext updateCtx) { char *base = NULL; int idx = -1; @@ -424,10 +424,10 @@ GetUpdateIndex(HnswElement element, HnswElement newElement, float distance, int Datum q = HnswGetValue(base, element); IndexTuple qtup = HnswPtrAccess(base, element->itup);; - LoadElementsForInsert(neighbors, q, qtup, &idx, index, procinfo, collation); + LoadElementsForInsert(neighbors, q, qtup, &idx, index, support); if (idx == -1) - HnswUpdateConnection(base, neighbors, newElement, distance, lm, &idx, index, procinfo, collation); + HnswUpdateConnection(base, neighbors, newElement, distance, lm, &idx, index, support); } MemoryContextSwitchTo(oldCtx); @@ -532,7 +532,7 @@ UpdateNeighborOnDisk(HnswElement element, HnswElement newElement, int idx, int m * Update neighbors */ void -HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo **procinfo, Oid *collation, HnswElement e, int m, bool checkExisting, bool building) +HnswUpdateNeighborsOnDisk(Relation index, HnswSupport * support, HnswElement e, int m, bool checkExisting, bool building) { char *base = NULL; @@ -555,7 +555,7 @@ HnswUpdateNeighborsOnDisk(Relation index, FmgrInfo **procinfo, Oid *collation, H HnswElement neighborElement = HnswPtrAccess(base, hc->element); int idx; - idx = GetUpdateIndex(neighborElement, e, hc->distance, m, lm, lc, index, procinfo, collation, updateCtx); + idx = GetUpdateIndex(neighborElement, e, hc->distance, m, lm, lc, index, support, updateCtx); /* New element was not selected as a neighbor */ if (idx == -1) @@ -665,7 +665,7 @@ FindDuplicateOnDisk(Relation index, HnswElement element, bool building) * Update graph on disk */ static void -UpdateGraphOnDisk(Relation index, FmgrInfo **procinfo, Oid *collation, HnswElement element, int m, int efConstruction, HnswElement entryPoint, bool building) +UpdateGraphOnDisk(Relation index, HnswSupport * support, HnswElement element, int m, int efConstruction, HnswElement entryPoint, bool building) { BlockNumber newInsertPage = InvalidBlockNumber; @@ -681,7 +681,7 @@ UpdateGraphOnDisk(Relation index, FmgrInfo **procinfo, Oid *collation, HnswEleme HnswUpdateMetaPage(index, 0, NULL, newInsertPage, MAIN_FORKNUM, building); /* Update neighbors */ - HnswUpdateNeighborsOnDisk(index, procinfo, collation, element, m, false, building); + HnswUpdateNeighborsOnDisk(index, support, element, m, false, building); /* Update entry point if needed */ if (entryPoint == NULL || element->level > entryPoint->level) @@ -692,7 +692,7 @@ UpdateGraphOnDisk(Relation index, FmgrInfo **procinfo, Oid *collation, HnswEleme * Insert a tuple into the index */ bool -HnswInsertTupleOnDisk(Relation index, FmgrInfo **procinfo, Oid *collation, IndexTuple itup, ItemPointer heaptid, bool building) +HnswInsertTupleOnDisk(Relation index, HnswSupport * support, IndexTuple itup, ItemPointer heaptid, bool building) { HnswElement entryPoint; HnswElement element; @@ -733,10 +733,10 @@ HnswInsertTupleOnDisk(Relation index, FmgrInfo **procinfo, Oid *collation, Index } /* Find neighbors for element */ - HnswFindElementNeighbors(base, element, entryPoint, index, procinfo, collation, m, efConstruction, false, false); + HnswFindElementNeighbors(base, element, entryPoint, index, support, m, efConstruction, false, false); /* Update graph on disk */ - UpdateGraphOnDisk(index, procinfo, collation, element, m, efConstruction, entryPoint, building); + UpdateGraphOnDisk(index, support, element, m, efConstruction, entryPoint, building); /* Release lock */ UnlockPage(index, HNSW_UPDATE_LOCK, lockmode); @@ -753,17 +753,15 @@ HnswInsertTuple(Relation index, Datum *values, bool *isnull, ItemPointer heaptid IndexTuple itup; const HnswTypeInfo *typeInfo = HnswGetTypeInfo(index); TupleDesc tupdesc = RelationGetDescr(index); - FmgrInfo *procinfo[2]; - FmgrInfo *normprocinfo; - Oid *collation; + HnswSupport support; - HnswSetProcinfo(index, procinfo, &normprocinfo, &collation); + HnswInitSupport(&support, index); /* Form index tuple */ - if (!HnswFormIndexTuple(&itup, values, isnull, typeInfo, normprocinfo, collation[0], tupdesc)) + if (!HnswFormIndexTuple(&itup, values, isnull, typeInfo, &support, tupdesc)) return; - HnswInsertTupleOnDisk(index, procinfo, collation, itup, heaptid, false); + HnswInsertTupleOnDisk(index, &support, itup, heaptid, false); } /* diff --git a/src/hnswscan.c b/src/hnswscan.c index 2f807a3..3bdc494 100644 --- a/src/hnswscan.c +++ b/src/hnswscan.c @@ -15,8 +15,7 @@ GetScanItems(IndexScanDesc scan, Datum q) { HnswScanOpaque so = (HnswScanOpaque) scan->opaque; Relation index = scan->indexRelation; - FmgrInfo **procinfo = so->procinfo; - Oid *collation = so->collation; + HnswSupport *support = &so->support; List *ep; List *w; int m; @@ -31,15 +30,15 @@ GetScanItems(IndexScanDesc scan, Datum q) if (entryPoint == NULL) return NIL; - ep = list_make1(HnswEntryCandidate(base, entryPoint, q, NULL, keyData, index, procinfo, collation, false, inMemory)); + ep = list_make1(HnswEntryCandidate(base, entryPoint, q, NULL, keyData, index, support, false, inMemory)); for (int lc = entryPoint->level; lc >= 1; lc--) { - w = HnswSearchLayer(base, q, NULL, keyData, ep, 1, lc, index, procinfo, collation, m, false, NULL, inMemory); + w = HnswSearchLayer(base, q, NULL, keyData, ep, 1, lc, index, support, m, false, NULL, inMemory); ep = w; } - return HnswSearchLayer(base, q, NULL, keyData, ep, hnsw_ef_search, 0, index, procinfo, collation, m, false, NULL, inMemory); + return HnswSearchLayer(base, q, NULL, keyData, ep, hnsw_ef_search, 0, index, support, m, false, NULL, inMemory); } /* @@ -62,8 +61,8 @@ GetScanValue(IndexScanDesc scan) Assert(!VARATT_IS_EXTENDED(DatumGetPointer(value))); /* Normalize if needed */ - if (so->normprocinfo != NULL) - value = HnswNormValue(so->typeInfo, so->collation[0], value); + if (so->support.normprocinfo != NULL) + value = HnswNormValue(so->typeInfo, so->support.collation[0], value); } return value; @@ -88,7 +87,7 @@ hnswbeginscan(Relation index, int nkeys, int norderbys) ALLOCSET_DEFAULT_SIZES); /* Set support functions */ - HnswSetProcinfo(index, so->procinfo, &so->normprocinfo, &so->collation); + HnswInitSupport(&so->support, index); scan->opaque = so; diff --git a/src/hnswutils.c b/src/hnswutils.c index ea94155..f33e064 100644 --- a/src/hnswutils.c +++ b/src/hnswutils.c @@ -154,20 +154,18 @@ HnswOptionalProcInfo(Relation index, uint16 procnum) } /* - * Set procinfo + * Init support functions */ void -HnswSetProcinfo(Relation index, FmgrInfo **procinfo, FmgrInfo **normprocinfo, Oid **collation) +HnswInitSupport(HnswSupport * support, Relation index) { - procinfo[0] = index_getprocinfo(index, 1, HNSW_DISTANCE_PROC); + support->procinfo[0] = index_getprocinfo(index, 1, HNSW_DISTANCE_PROC); if (IndexRelationGetNumberOfKeyAttributes(index) > 1) - procinfo[1] = index_getprocinfo(index, 2, HNSW_ATTRIBUTE_DISTANCE_PROC); + support->procinfo[1] = index_getprocinfo(index, 2, HNSW_ATTRIBUTE_DISTANCE_PROC); - *collation = index->rd_indcollation; - - if (normprocinfo != NULL) - *normprocinfo = HnswOptionalProcInfo(index, HNSW_NORM_PROC); + support->collation = index->rd_indcollation; + support->normprocinfo = HnswOptionalProcInfo(index, HNSW_NORM_PROC); } /* @@ -207,9 +205,9 @@ HnswNormValue(const HnswTypeInfo * typeInfo, Oid collation, Datum value) * Check if non-zero norm */ bool -HnswCheckNorm(FmgrInfo *procinfo, Oid collation, Datum value) +HnswCheckNorm(HnswSupport * support, Datum value) { - return DatumGetFloat8(FunctionCall1Coll(procinfo, collation, value)) > 0; + return DatumGetFloat8(FunctionCall1Coll(support->normprocinfo, support->collation[0], value)) > 0; } /* @@ -472,7 +470,7 @@ HnswUpdateMetaPage(Relation index, int updateEntry, HnswElement entryPoint, Bloc * Form index tuple */ bool -HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, FmgrInfo *normprocinfo, Oid collation, TupleDesc tupdesc) +HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeInfo * typeInfo, HnswSupport * support, TupleDesc tupdesc) { Datum newValues[2]; @@ -484,12 +482,12 @@ HnswFormIndexTuple(IndexTuple *out, Datum *values, bool *isnull, const HnswTypeI typeInfo->checkValue(DatumGetPointer(value)); /* Normalize if needed */ - if (normprocinfo != NULL) + if (support->normprocinfo != NULL) { - if (!HnswCheckNorm(normprocinfo, collation, value)) + if (!HnswCheckNorm(support, value)) return false; - value = HnswNormValue(typeInfo, collation, value); + value = HnswNormValue(typeInfo, support->collation[0], value); } newValues[0] = value; @@ -629,14 +627,14 @@ AttributeDistance(double e) * Calculate the distance between values */ static double -HnswGetDistance(IndexTuple itup, Datum vec, Datum q, IndexTuple qtup, ScanKeyData *keyData, Relation index, FmgrInfo **procinfo, Oid *collation, bool *matches) +HnswGetDistance(IndexTuple itup, Datum vec, Datum q, IndexTuple qtup, ScanKeyData *keyData, Relation index, HnswSupport * support, bool *matches) { double g; if (DatumGetPointer(q) == NULL) g = 0; else - g = DatumGetFloat8(FunctionCall2Coll(procinfo[0], collation[0], q, vec)); + g = DatumGetFloat8(FunctionCall2Coll(support->procinfo[0], support->collation[0], q, vec)); Assert(PointerIsValid(matches)); *matches = true; @@ -669,7 +667,7 @@ HnswGetDistance(IndexTuple itup, Datum vec, Datum q, IndexTuple qtup, ScanKeyDat } else if (!DatumGetBool(FunctionCall2Coll(&key->sk_func, key->sk_collation, value, key->sk_argument))) { - double ei = fabs(DatumGetFloat8(FunctionCall2Coll(procinfo[key->sk_attno - 1], collation[key->sk_attno - 1], value, key->sk_argument))); + double ei = fabs(DatumGetFloat8(FunctionCall2Coll(support->procinfo[key->sk_attno - 1], support->collation[key->sk_attno - 1], value, key->sk_argument))); if (ei > 0) e += ei; @@ -700,7 +698,7 @@ HnswGetDistance(IndexTuple itup, Datum vec, Datum q, IndexTuple qtup, ScanKeyDat e += 1000; } else - e += fabs(DatumGetFloat8(FunctionCall2Coll(procinfo[i + 1], collation[i + 1], value, value2))); + e += fabs(DatumGetFloat8(FunctionCall2Coll(support->procinfo[i + 1], support->collation[i + 1], value, value2))); } return w * g + AttributeDistance(e); @@ -714,7 +712,7 @@ HnswGetDistance(IndexTuple itup, Datum vec, Datum q, IndexTuple qtup, ScanKeyDat * Load an element and optionally get its distance from q */ static void -HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, bool *matches, Datum *q, IndexTuple qtup, ScanKeyData *keyData, Relation index, FmgrInfo **procinfo, Oid *collation, bool loadVec, double *maxDistance, HnswElement * element) +HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, bool *matches, Datum *q, IndexTuple qtup, ScanKeyData *keyData, Relation index, HnswSupport * support, bool loadVec, double *maxDistance, HnswElement * element) { Buffer buf; Page page; @@ -748,7 +746,7 @@ HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, boo value = PointerGetDatum(&etup->data); } - *distance = HnswGetDistance(itup, value, *q, qtup, keyData, index, procinfo, collation, matches); + *distance = HnswGetDistance(itup, value, *q, qtup, keyData, index, support, matches); } /* Load element */ @@ -767,36 +765,36 @@ HnswLoadElementImpl(BlockNumber blkno, OffsetNumber offno, double *distance, boo * Load an element and optionally get its distance from q */ void -HnswLoadElement(HnswElement element, double *distance, bool *matches, Datum *q, IndexTuple qtup, ScanKeyData *keyData, Relation index, FmgrInfo **procinfo, Oid *collation, bool loadVec, double *maxDistance) +HnswLoadElement(HnswElement element, double *distance, bool *matches, Datum *q, IndexTuple qtup, ScanKeyData *keyData, Relation index, HnswSupport * support, bool loadVec, double *maxDistance) { - HnswLoadElementImpl(element->blkno, element->offno, distance, matches, q, qtup, keyData, index, procinfo, collation, loadVec, maxDistance, &element); + HnswLoadElementImpl(element->blkno, element->offno, distance, matches, q, qtup, keyData, index, support, loadVec, maxDistance, &element); } /* * Get the distance for an element */ static double -GetElementDistance(char *base, HnswElement element, bool *matches, Datum q, IndexTuple qtup, ScanKeyData *keyData, Relation index, FmgrInfo **procinfo, Oid *collation) +GetElementDistance(char *base, HnswElement element, bool *matches, Datum q, IndexTuple qtup, ScanKeyData *keyData, Relation index, HnswSupport * support) { Datum value = HnswGetValue(base, element); IndexTuple itup = HnswPtrAccess(base, element->itup); - return HnswGetDistance(itup, value, q, qtup, keyData, index, procinfo, collation, matches); + return HnswGetDistance(itup, value, q, qtup, keyData, index, support, matches); } /* * Create a candidate for the entry point */ HnswSearchCandidate * -HnswEntryCandidate(char *base, HnswElement entryPoint, Datum q, IndexTuple qtup, ScanKeyData *keyData, Relation index, FmgrInfo **procinfo, Oid *collation, bool loadVec, bool inMemory) +HnswEntryCandidate(char *base, HnswElement entryPoint, Datum q, IndexTuple qtup, ScanKeyData *keyData, Relation index, HnswSupport * support, bool loadVec, bool inMemory) { HnswSearchCandidate *sc = palloc(sizeof(HnswSearchCandidate)); HnswPtrStore(base, sc->element, entryPoint); if (inMemory) - sc->distance = GetElementDistance(base, entryPoint, &sc->matches, q, qtup, keyData, index, procinfo, collation); + sc->distance = GetElementDistance(base, entryPoint, &sc->matches, q, qtup, keyData, index, support); else - HnswLoadElement(entryPoint, &sc->distance, &sc->matches, &q, qtup, keyData, index, procinfo, collation, loadVec, NULL); + HnswLoadElement(entryPoint, &sc->distance, &sc->matches, &q, qtup, keyData, index, support, loadVec, NULL); return sc; } @@ -985,7 +983,7 @@ HnswLoadUnvisitedFromDisk(HnswElement element, HnswUnvisited * unvisited, int *u * Algorithm 2 from paper */ List * -HnswSearchLayer(char *base, Datum q, IndexTuple qtup, ScanKeyData *keyData, List *ep, int ef, int lc, Relation index, FmgrInfo **procinfo, Oid *collation, int m, bool inserting, HnswElement skipElement, bool inMemory) +HnswSearchLayer(char *base, Datum q, IndexTuple qtup, ScanKeyData *keyData, List *ep, int ef, int lc, Relation index, HnswSupport * support, int m, bool inserting, HnswElement skipElement, bool inMemory) { List *w = NIL; pairingheap *C = pairingheap_allocate(CompareNearestCandidates, NULL); @@ -1063,7 +1061,7 @@ HnswSearchLayer(char *base, Datum q, IndexTuple qtup, ScanKeyData *keyData, List if (inMemory) { eElement = unvisited[i].element; - eDistance = GetElementDistance(base, eElement, &eMatches, q, qtup, keyData, index, procinfo, collation); + eDistance = GetElementDistance(base, eElement, &eMatches, q, qtup, keyData, index, support); } else { @@ -1073,7 +1071,7 @@ HnswSearchLayer(char *base, Datum q, IndexTuple qtup, ScanKeyData *keyData, List /* Avoid any allocations if not adding */ eElement = NULL; - HnswLoadElementImpl(blkno, offno, &eDistance, &eMatches, &q, qtup, keyData, index, procinfo, collation, inserting, alwaysAdd ? NULL : &f->distance, &eElement); + HnswLoadElementImpl(blkno, offno, &eDistance, &eMatches, &q, qtup, keyData, index, support, inserting, alwaysAdd ? NULL : &f->distance, &eElement); if (eElement == NULL) continue; @@ -1179,7 +1177,7 @@ CompareCandidateDistancesOffset(const ListCell *a, const ListCell *b) * Check if an element is closer to q than any element from R */ static bool -CheckElementCloser(char *base, HnswCandidate * e, List *r, Relation index, FmgrInfo **procinfo, Oid *collation) +CheckElementCloser(char *base, HnswCandidate * e, List *r, Relation index, HnswSupport * support) { HnswElement eElement = HnswPtrAccess(base, e->element); Datum eValue = HnswGetValue(base, eElement); @@ -1193,7 +1191,7 @@ CheckElementCloser(char *base, HnswCandidate * e, List *r, Relation index, FmgrI Datum riValue = HnswGetValue(base, riElement); IndexTuple ritup = HnswPtrAccess(base, riElement->itup); bool matches; - float distance = HnswGetDistance(etup, eValue, riValue, ritup, NULL, index, procinfo, collation, &matches); + float distance = HnswGetDistance(etup, eValue, riValue, ritup, NULL, index, support, &matches); if (distance <= e->distance) return false; @@ -1206,7 +1204,7 @@ CheckElementCloser(char *base, HnswCandidate * e, List *r, Relation index, FmgrI * Algorithm 4 from paper */ static List * -SelectNeighbors(char *base, List *c, int lm, Relation index, FmgrInfo **procinfo, Oid *collation, bool *closerSet, HnswCandidate * newCandidate, HnswCandidate * *pruned, bool sortCandidates) +SelectNeighbors(char *base, List *c, int lm, Relation index, HnswSupport * support, bool *closerSet, HnswCandidate * newCandidate, HnswCandidate * *pruned, bool sortCandidates) { List *r = NIL; List *w = list_copy(c); @@ -1240,7 +1238,7 @@ SelectNeighbors(char *base, List *c, int lm, Relation index, FmgrInfo **procinfo /* Use previous state of r and wd to skip work when possible */ if (mustCalculate) - e->closer = CheckElementCloser(base, e, r, index, procinfo, collation); + e->closer = CheckElementCloser(base, e, r, index, support); else if (list_length(added) > 0) { /* Keep Valgrind happy for in-memory, parallel builds */ @@ -1253,8 +1251,7 @@ SelectNeighbors(char *base, List *c, int lm, Relation index, FmgrInfo **procinfo */ if (e->closer) { - e->closer = CheckElementCloser(base, e, added, index, procinfo, collation); - + e->closer = CheckElementCloser(base, e, added, index, support); if (!e->closer) removedAny = true; } @@ -1266,7 +1263,7 @@ SelectNeighbors(char *base, List *c, int lm, Relation index, FmgrInfo **procinfo */ if (removedAny) { - e->closer = CheckElementCloser(base, e, r, index, procinfo, collation); + e->closer = CheckElementCloser(base, e, r, index, support); if (e->closer) added = lappend(added, e); } @@ -1274,7 +1271,7 @@ SelectNeighbors(char *base, List *c, int lm, Relation index, FmgrInfo **procinfo } else if (e == newCandidate) { - e->closer = CheckElementCloser(base, e, r, index, procinfo, collation); + e->closer = CheckElementCloser(base, e, r, index, support); if (e->closer) added = lappend(added, e); } @@ -1325,7 +1322,7 @@ AddConnections(char *base, HnswElement element, List *neighbors, int lc) * Update connections */ void -HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newElement, float distance, int lm, int *updateIdx, Relation index, FmgrInfo **procinfo, Oid *collation) +HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newElement, float distance, int lm, int *updateIdx, Relation index, HnswSupport * support) { HnswCandidate newHc; @@ -1351,7 +1348,7 @@ HnswUpdateConnection(char *base, HnswNeighborArray * neighbors, HnswElement newE c = lappend(c, &neighbors->items[i]); c = lappend(c, &newHc); - SelectNeighbors(base, c, lm, index, procinfo, collation, &neighbors->closerSet, &newHc, &pruned, true); + SelectNeighbors(base, c, lm, index, support, &neighbors->closerSet, &newHc, &pruned, true); /* Should not happen */ if (pruned == NULL) @@ -1422,7 +1419,7 @@ PrecomputeHash(char *base, HnswElement element) * Algorithm 1 from paper */ void -HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint, Relation index, FmgrInfo **procinfo, Oid *collation, int m, int efConstruction, bool existing, bool inMemory) +HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint, Relation index, HnswSupport * support, int m, int efConstruction, bool existing, bool inMemory) { List *ep; List *w; @@ -1442,13 +1439,13 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint return; /* Get entry point and level */ - ep = list_make1(HnswEntryCandidate(base, entryPoint, q, qtup, keyData, index, procinfo, collation, true, inMemory)); + ep = list_make1(HnswEntryCandidate(base, entryPoint, q, qtup, keyData, index, support, true, inMemory)); entryLevel = entryPoint->level; /* 1st phase: greedy search to insert level */ for (int lc = entryLevel; lc >= level + 1; lc--) { - w = HnswSearchLayer(base, q, qtup, keyData, ep, 1, lc, index, procinfo, collation, m, true, skipElement, inMemory); + w = HnswSearchLayer(base, q, qtup, keyData, ep, 1, lc, index, support, m, true, skipElement, inMemory); ep = w; } @@ -1467,7 +1464,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint List *lw = NIL; ListCell *lc2; - w = HnswSearchLayer(base, q, qtup, keyData, ep, efConstruction, lc, index, procinfo, collation, m, true, skipElement, inMemory); + w = HnswSearchLayer(base, q, qtup, keyData, ep, efConstruction, lc, index, support, m, true, skipElement, inMemory); /* Convert search candidates to candidates */ foreach(lc2, w) @@ -1491,7 +1488,7 @@ HnswFindElementNeighbors(char *base, HnswElement element, HnswElement entryPoint * sortCandidates to true for in-memory builds to enable closer * caching, but there does not seem to be a difference in performance. */ - neighbors = SelectNeighbors(base, lw, lm, index, procinfo, collation, &HnswGetNeighbors(base, element, lc)->closerSet, NULL, NULL, false); + neighbors = SelectNeighbors(base, lw, lm, index, support, &HnswGetNeighbors(base, element, lc)->closerSet, NULL, NULL, false); AddConnections(base, element, neighbors, lc); diff --git a/src/hnswvacuum.c b/src/hnswvacuum.c index d8ad357..bef5fbf 100644 --- a/src/hnswvacuum.c +++ b/src/hnswvacuum.c @@ -184,13 +184,12 @@ static void RepairGraphElement(HnswVacuumState * vacuumstate, HnswElement element, HnswElement entryPoint) { Relation index = vacuumstate->index; + HnswSupport *support = &vacuumstate->support; Buffer buf; Page page; GenericXLogState *state; int m = vacuumstate->m; int efConstruction = vacuumstate->efConstruction; - FmgrInfo **procinfo = vacuumstate->procinfo; - Oid *collation = vacuumstate->collation; BufferAccessStrategy bas = vacuumstate->bas; HnswNeighborTuple ntup = vacuumstate->ntup; Size ntupSize = HNSW_NEIGHBOR_TUPLE_SIZE(element->level, m); @@ -205,7 +204,7 @@ RepairGraphElement(HnswVacuumState * vacuumstate, HnswElement element, HnswEleme element->heaptidsLength = 0; /* Find neighbors for element, skipping itself */ - HnswFindElementNeighbors(base, element, entryPoint, index, procinfo, collation, m, efConstruction, true, false); + HnswFindElementNeighbors(base, element, entryPoint, index, support, m, efConstruction, true, false); /* Zero memory for each element */ MemSet(ntup, 0, HNSW_TUPLE_ALLOC_SIZE); @@ -229,7 +228,7 @@ RepairGraphElement(HnswVacuumState * vacuumstate, HnswElement element, HnswEleme UnlockReleaseBuffer(buf); /* Update neighbors */ - HnswUpdateNeighborsOnDisk(index, procinfo, collation, element, m, true, false); + HnswUpdateNeighborsOnDisk(index, support, element, m, true, false); } /* @@ -239,6 +238,7 @@ static void RepairGraphEntryPoint(HnswVacuumState * vacuumstate) { Relation index = vacuumstate->index; + HnswSupport *support = &vacuumstate->support; HnswElement highestPoint = &vacuumstate->highestPoint; HnswElement entryPoint; MemoryContext oldCtx = MemoryContextSwitchTo(vacuumstate->tmpCtx); @@ -256,7 +256,7 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate) LockPage(index, HNSW_UPDATE_LOCK, ShareLock); /* Load element */ - HnswLoadElement(highestPoint, NULL, NULL, NULL, NULL, NULL, index, vacuumstate->procinfo, vacuumstate->collation, true, NULL); + HnswLoadElement(highestPoint, NULL, NULL, NULL, NULL, NULL, index, support, true, NULL); /* Repair if needed */ if (NeedsUpdated(vacuumstate, highestPoint)) @@ -294,7 +294,7 @@ RepairGraphEntryPoint(HnswVacuumState * vacuumstate) * is outdated, this can remove connections at higher levels in * the graph until they are repaired, but this should be fine. */ - HnswLoadElement(entryPoint, NULL, NULL, NULL, NULL, NULL, index, vacuumstate->procinfo, vacuumstate->collation, true, NULL); + HnswLoadElement(entryPoint, NULL, NULL, NULL, NULL, NULL, index, support, true, NULL); if (NeedsUpdated(vacuumstate, entryPoint)) { @@ -586,7 +586,7 @@ InitVacuumState(HnswVacuumState * vacuumstate, IndexVacuumInfo *info, IndexBulkD "Hnsw vacuum temporary context", ALLOCSET_DEFAULT_SIZES); - HnswSetProcinfo(index, vacuumstate->procinfo, NULL, &vacuumstate->collation); + HnswInitSupport(&vacuumstate->support, index); /* Get m from metapage */ HnswGetMetaPageInfo(index, &vacuumstate->m, NULL);