From a69ec8bc9bd49fc4ad313ebe0cad3271ce4ab194 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 18 Nov 2026 01:43:04 -0800 Subject: [PATCH] Added ivfflat.scan_mem_multiplier [skip ci] --- src/ivfflat.c | 6 ++++++ src/ivfflat.h | 1 + src/ivfscan.c | 3 ++- test/expected/ivfflat_vector.out | 20 ++++++++++++++++++++ test/sql/ivfflat_vector.sql | 10 ++++++++++ 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/ivfflat.c b/src/ivfflat.c index e9659582..3c641f10 100644 --- a/src/ivfflat.c +++ b/src/ivfflat.c @@ -33,6 +33,7 @@ int ivfflat_probes; int ivfflat_iterative_scan; int ivfflat_max_probes; +double ivfflat_scan_mem_multiplier; static relopt_kind ivfflat_relopt_kind; static const struct config_enum_entry ivfflat_iterative_scan_options[] = { @@ -64,6 +65,11 @@ IvfflatInit(void) NULL, &ivfflat_max_probes, IVFFLAT_MAX_LISTS, IVFFLAT_MIN_LISTS, IVFFLAT_MAX_LISTS, PGC_USERSET, 0, NULL, NULL, NULL); + /* Same range as hash_mem_multiplier */ + DefineCustomRealVariable("ivfflat.scan_mem_multiplier", "Sets the multiple of work_mem to use for scans", + NULL, &ivfflat_scan_mem_multiplier, + 1, 1, 1000, PGC_USERSET, 0, NULL, NULL, NULL); + MarkGUCPrefixReserved("ivfflat"); } diff --git a/src/ivfflat.h b/src/ivfflat.h index 00891a55..7686be40 100644 --- a/src/ivfflat.h +++ b/src/ivfflat.h @@ -103,6 +103,7 @@ typedef Pointer Item; extern int ivfflat_probes; extern int ivfflat_iterative_scan; extern int ivfflat_max_probes; +extern double ivfflat_scan_mem_multiplier; typedef enum IvfflatIterativeScanMode { diff --git a/src/ivfscan.c b/src/ivfscan.c index 3d18b36f..545c42fc 100644 --- a/src/ivfscan.c +++ b/src/ivfscan.c @@ -621,7 +621,8 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) so->listsHeap = pairingheap_allocate(CompareListsMaxHeap, NULL); so->useHeap = true; - so->maxHeapItems = ComputeMaxHeapItems(mul_size(work_mem, 1024)); + /* TODO Improve multiplier pattern */ + so->maxHeapItems = ComputeMaxHeapItems(mul_size(work_mem, 1024 * ivfflat_scan_mem_multiplier)); BinaryHeapInit(&so->itemsHeap, Min(32 * 1024, so->maxHeapItems)); so->sortstate = NULL; diff --git a/test/expected/ivfflat_vector.out b/test/expected/ivfflat_vector.out index 462ea23c..f524d7a0 100644 --- a/test/expected/ivfflat_vector.out +++ b/test/expected/ivfflat_vector.out @@ -181,6 +181,16 @@ SET ivfflat.max_probes = 0; ERROR: 0 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32768) SET ivfflat.max_probes = 32769; ERROR: 32769 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32768) +SHOW ivfflat.scan_mem_multiplier; + ivfflat.scan_mem_multiplier +----------------------------- + 1 +(1 row) + +SET ivfflat.scan_mem_multiplier = 0; +ERROR: 0 is outside the valid range for parameter "ivfflat.scan_mem_multiplier" (1 .. 1000) +SET ivfflat.scan_mem_multiplier = 1001; +ERROR: 1001 is outside the valid range for parameter "ivfflat.scan_mem_multiplier" (1 .. 1000) -- dimensions CREATE TABLE t (val vector(2000)); CREATE INDEX ON t USING ivfflat (val vector_l2_ops); @@ -273,6 +283,16 @@ DEBUG: ivfflat scan no longer fits into work_mem after 1 tuples [1,2,3] | [1,2,3] (3 rows) +SET ivfflat.scan_mem_multiplier = 2; +SELECT * FROM t ORDER BY val <-> '[3,3,3]'; + val +--------- + [1,2,3] + [1,1,1] + [0,0,0] +(3 rows) + +RESET ivfflat.scan_mem_multiplier; RESET client_min_messages; DROP TABLE t; RESET work_mem; diff --git a/test/sql/ivfflat_vector.sql b/test/sql/ivfflat_vector.sql index 44b3aefd..cb56fdae 100644 --- a/test/sql/ivfflat_vector.sql +++ b/test/sql/ivfflat_vector.sql @@ -95,6 +95,10 @@ SHOW ivfflat.max_probes; SET ivfflat.max_probes = 0; SET ivfflat.max_probes = 32769; +SHOW ivfflat.scan_mem_multiplier; +SET ivfflat.scan_mem_multiplier = 0; +SET ivfflat.scan_mem_multiplier = 1001; + -- dimensions CREATE TABLE t (val vector(2000)); @@ -142,8 +146,14 @@ INSERT INTO t (val) VALUES ('[0,0,0]'), ('[1,2,3]'), ('[1,1,1]'); CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 1); SET client_min_messages = DEBUG; + SELECT * FROM t ORDER BY val <-> '[3,3,3]'; SELECT * FROM t CROSS JOIN LATERAL (SELECT * FROM t t2 ORDER BY val <-> t.val LIMIT 1) t2 ORDER BY t.val; + +SET ivfflat.scan_mem_multiplier = 2; +SELECT * FROM t ORDER BY val <-> '[3,3,3]'; +RESET ivfflat.scan_mem_multiplier; + RESET client_min_messages; DROP TABLE t;