From 9f973cd95f5ac3aab6ccb8ebdfb048188e4025e4 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Mon, 16 Nov 2026 18:10:19 -0800 Subject: [PATCH] Fixed broken HOT chain handling in parallel index builds Co-authored-by: Lukas Fittl --- src/hnsw.h | 1 + src/hnswbuild.c | 8 ++++++-- src/ivfbuild.c | 8 ++++++-- src/ivfflat.h | 1 + 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/hnsw.h b/src/hnsw.h index adbeef4a..663d90ce 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -266,6 +266,7 @@ typedef struct HnswShared /* Mutable state */ int nparticipantsdone; double reltuples; + bool brokenhotchain; HnswGraph graphData; } HnswShared; diff --git a/src/hnswbuild.c b/src/hnswbuild.c index 01a634b3..94fb3f4f 100644 --- a/src/hnswbuild.c +++ b/src/hnswbuild.c @@ -754,7 +754,7 @@ FreeBuildState(HnswBuildState * buildstate) * Within leader, wait for end of heap scan */ static double -ParallelHeapScan(HnswBuildState * buildstate) +ParallelHeapScan(HnswBuildState * buildstate, bool *brokenhotchain) { HnswShared *hnswshared = buildstate->hnswleader->hnswshared; int nparticipanttuplesorts; @@ -769,6 +769,7 @@ ParallelHeapScan(HnswBuildState * buildstate) buildstate->graph = &hnswshared->graphData; buildstate->hnswarea = buildstate->hnswleader->hnswarea; reltuples = hnswshared->reltuples; + *brokenhotchain = hnswshared->brokenhotchain; SpinLockRelease(&hnswshared->mutex); break; } @@ -815,6 +816,8 @@ HnswParallelScanAndInsert(Relation heapRel, Relation indexRel, HnswShared * hnsw SpinLockAcquire(&hnswshared->mutex); hnswshared->nparticipantsdone++; hnswshared->reltuples += reltuples; + if (indexInfo->ii_BrokenHotChain) + hnswshared->brokenhotchain = true; SpinLockRelease(&hnswshared->mutex); /* Log statistics */ @@ -1025,6 +1028,7 @@ HnswBeginParallel(HnswBuildState * buildstate, bool isconcurrent, int request) /* Initialize mutable state */ hnswshared->nparticipantsdone = 0; hnswshared->reltuples = 0; + hnswshared->brokenhotchain = false; table_parallelscan_initialize(buildstate->heap, ParallelTableScanFromHnswShared(hnswshared), snapshot); @@ -1127,7 +1131,7 @@ BuildGraph(HnswBuildState * buildstate) if (buildstate->heap != NULL) { if (buildstate->hnswleader) - buildstate->reltuples = ParallelHeapScan(buildstate); + buildstate->reltuples = ParallelHeapScan(buildstate, &buildstate->indexInfo->ii_BrokenHotChain); else buildstate->reltuples = table_index_build_scan(buildstate->heap, buildstate->index, buildstate->indexInfo, true, true, BuildCallback, (void *) buildstate, NULL); diff --git a/src/ivfbuild.c b/src/ivfbuild.c index 1e85698d..7c7a008d 100644 --- a/src/ivfbuild.c +++ b/src/ivfbuild.c @@ -612,7 +612,7 @@ InitBuildSortState(TupleDesc tupdesc, int memory, SortCoordinate coordinate) * Within leader, wait for end of heap scan */ static double -ParallelHeapScan(IvfflatBuildState * buildstate) +ParallelHeapScan(IvfflatBuildState * buildstate, bool *brokenhotchain) { IvfflatShared *ivfshared = buildstate->ivfleader->ivfshared; int nparticipanttuplesorts; @@ -626,6 +626,7 @@ ParallelHeapScan(IvfflatBuildState * buildstate) { buildstate->indtuples = ivfshared->indtuples; reltuples = ivfshared->reltuples; + *brokenhotchain = ivfshared->brokenhotchain; #ifdef IVFFLAT_KMEANS_DEBUG buildstate->inertia = ivfshared->inertia; #endif @@ -687,6 +688,8 @@ IvfflatParallelScanAndSort(IvfflatSpool * ivfspool, IvfflatShared * ivfshared, S ivfshared->nparticipantsdone++; ivfshared->reltuples += reltuples; ivfshared->indtuples += buildstate.indtuples; + if (indexInfo->ii_BrokenHotChain) + ivfshared->brokenhotchain = true; #ifdef IVFFLAT_KMEANS_DEBUG ivfshared->inertia += buildstate.inertia; #endif @@ -924,6 +927,7 @@ IvfflatBeginParallel(IvfflatBuildState * buildstate, bool isconcurrent, int requ ivfshared->nparticipantsdone = 0; ivfshared->reltuples = 0; ivfshared->indtuples = 0; + ivfshared->brokenhotchain = false; #ifdef IVFFLAT_KMEANS_DEBUG ivfshared->inertia = 0; #endif @@ -1028,7 +1032,7 @@ AssignTuples(IvfflatBuildState * buildstate) if (buildstate->heap != NULL) { if (buildstate->ivfleader) - buildstate->reltuples = ParallelHeapScan(buildstate); + buildstate->reltuples = ParallelHeapScan(buildstate, &buildstate->indexInfo->ii_BrokenHotChain); else buildstate->reltuples = table_index_build_scan(buildstate->heap, buildstate->index, buildstate->indexInfo, true, true, BuildCallback, (void *) buildstate, NULL); diff --git a/src/ivfflat.h b/src/ivfflat.h index f5ec4099..f1f0754f 100644 --- a/src/ivfflat.h +++ b/src/ivfflat.h @@ -161,6 +161,7 @@ typedef struct IvfflatShared int nparticipantsdone; double reltuples; double indtuples; + bool brokenhotchain; #ifdef IVFFLAT_KMEANS_DEBUG double inertia;