From 5ba62fca84b0c3e1eb2693493cf825f80a4a7b74 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 14 Feb 2024 17:13:30 -0800 Subject: [PATCH] Fixed crash with shared_preload_libraries - fixes #460 --- src/hnsw.c | 6 ++++-- src/hnsw.h | 1 + src/hnswbuild.c | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/hnsw.c b/src/hnsw.c index 9689b17..fa08cf5 100644 --- a/src/hnsw.c +++ b/src/hnsw.c @@ -8,6 +8,7 @@ #include "commands/progress.h" #include "commands/vacuum.h" #include "hnsw.h" +#include "miscadmin.h" #include "utils/guc.h" #include "utils/selfuncs.h" @@ -28,7 +29,7 @@ static relopt_kind hnsw_relopt_kind; * this grows bigger, we should use a shmem_request_hook and * RequestAddinShmemSpace() to pre-reserve space for this. */ -static void +void HnswInitLockTranche(void) { int *tranche_ids; @@ -53,7 +54,8 @@ HnswInitLockTranche(void) void HnswInit(void) { - HnswInitLockTranche(); + if (!process_shared_preload_libraries_in_progress) + HnswInitLockTranche(); hnsw_relopt_kind = add_reloption_kind(); add_int_reloption(hnsw_relopt_kind, "m", "Max number of connections", diff --git a/src/hnsw.h b/src/hnsw.h index 09e90f3..470e24e 100644 --- a/src/hnsw.h +++ b/src/hnsw.h @@ -389,6 +389,7 @@ void HnswLoadElement(HnswElement element, float *distance, Datum *q, Relation i void HnswSetElementTuple(char *base, HnswElementTuple etup, HnswElement element); void HnswUpdateConnection(char *base, HnswElement element, HnswCandidate * hc, int lm, int lc, int *updateIdx, Relation index, FmgrInfo *procinfo, Oid collation); void HnswLoadNeighbors(HnswElement element, Relation index, int m); +void HnswInitLockTranche(void); PGDLLEXPORT void HnswParallelBuildMain(dsm_segment *seg, shm_toc *toc); /* Index access methods */ diff --git a/src/hnswbuild.c b/src/hnswbuild.c index 1cc0049..7b6026a 100644 --- a/src/hnswbuild.c +++ b/src/hnswbuild.c @@ -601,6 +601,9 @@ BuildCallback(Relation index, CALLBACK_ITEM_POINTER, Datum *values, static void InitGraph(HnswGraph * graph, char *base, long memoryTotal) { + /* Initialize the lock tranche if needed */ + HnswInitLockTranche(); + HnswPtrStore(base, graph->head, (HnswElement) NULL); HnswPtrStore(base, graph->entryPoint, (HnswElement) NULL); graph->memoryUsed = 0;