Added hnsw.search_mem_multiplier option

This commit is contained in:
Andrew Kane
2024-10-24 18:02:20 -07:00
parent 67eff41c44
commit ac6576e53a
4 changed files with 15 additions and 6 deletions

View File

@@ -28,6 +28,7 @@ static const struct config_enum_entry hnsw_iterative_search_options[] = {
int hnsw_ef_search;
int hnsw_iterative_search;
int hnsw_max_search_tuples;
double hnsw_search_mem_multiplier;
int hnsw_lock_tranche_id;
static relopt_kind hnsw_relopt_kind;
@@ -87,6 +88,11 @@ HnswInit(void)
NULL, &hnsw_max_search_tuples,
20000, 1, INT_MAX, PGC_USERSET, 0, NULL, NULL, NULL);
/* Same range and default as hash_mem_multiplier */
DefineCustomRealVariable("hnsw.search_mem_multiplier", "Sets the multiple of work_mem to use for iterative search",
NULL, &hnsw_search_mem_multiplier,
2, 1, 1000, PGC_USERSET, 0, NULL, NULL, NULL);
MarkGUCPrefixReserved("hnsw");
}

View File

@@ -111,6 +111,7 @@
extern int hnsw_ef_search;
extern int hnsw_iterative_search;
extern int hnsw_max_search_tuples;
extern double hnsw_search_mem_multiplier;
extern int hnsw_lock_tranche_id;
typedef enum HnswIterativeSearchMode
@@ -372,6 +373,7 @@ typedef struct HnswScanOpaqueData
int m;
int64 tuples;
double previousDistance;
Size maxMemory;
MemoryContext tmpCtx;
/* Support functions */

View File

@@ -138,6 +138,8 @@ hnswbeginscan(Relation index, int nkeys, int norderbys)
/* Set support functions */
HnswInitSupport(&so->support, index);
so->maxMemory = (Size) (work_mem * 1024.0 * hnsw_search_mem_multiplier);
scan->opaque = so;
return scan;
@@ -244,13 +246,13 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir)
so->w = lappend(so->w, HnswGetSearchCandidate(w_node, pairingheap_remove_first(so->discarded)));
}
/* Prevent scans from consuming too much memory */
else if (MemoryContextMemAllocated(so->tmpCtx, false) > (Size) work_mem * 1024L)
else if (MemoryContextMemAllocated(so->tmpCtx, false) >= so->maxMemory)
{
if (pairingheap_is_empty(so->discarded))
{
ereport(DEBUG1,
(errmsg("hnsw index scan exceeded work_mem after " INT64_FORMAT " tuples", so->tuples),
errhint("Increase work_mem to scan more tuples.")));
(errmsg("hnsw index scan reached memory limit after " INT64_FORMAT " tuples", so->tuples),
errhint("Increase hnsw.search_mem_multiplier to scan more tuples.")));
break;
}