From c226d4f9be64787c411cb0dce59ba1acf72f4efa Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Thu, 3 Dec 2026 16:44:32 -0800 Subject: [PATCH] Updated default probes [skip ci] --- src/ivfflat.c | 12 +++++++++--- src/ivfflat.h | 3 ++- src/ivfscan.c | 7 ++++++- src/ivfutils.c | 14 ++++++++++++++ test/expected/ivfflat_vector.out | 10 +++++++--- test/sql/ivfflat_vector.sql | 3 +++ 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/ivfflat.c b/src/ivfflat.c index fec66a9b..391f5908 100644 --- a/src/ivfflat.c +++ b/src/ivfflat.c @@ -42,6 +42,12 @@ static const struct config_enum_entry ivfflat_iterative_scan_options[] = { {NULL, 0, false} }; +static bool +CheckProbes(int *newval, void **extra, GucSource source) +{ + return *newval != 0; +} + /* * Initialize index options and variables */ @@ -53,8 +59,8 @@ IvfflatInit(void) IVFFLAT_DEFAULT_LISTS, IVFFLAT_MIN_LISTS, IVFFLAT_MAX_LISTS, AccessExclusiveLock); DefineCustomIntVariable("ivfflat.probes", "Sets the number of probes", - "Valid range is 1..lists.", &ivfflat_probes, - IVFFLAT_DEFAULT_PROBES, IVFFLAT_MIN_LISTS, IVFFLAT_MAX_LISTS, PGC_USERSET, GUC_EXPLAIN, NULL, NULL, NULL); + "Valid range is 1..lists or -1.", &ivfflat_probes, + IVFFLAT_DEFAULT_PROBES, -1, IVFFLAT_MAX_LISTS, PGC_USERSET, GUC_EXPLAIN, CheckProbes, NULL, NULL); DefineCustomEnumVariable("ivfflat.iterative_scan", "Sets the mode for iterative scans", NULL, &ivfflat_iterative_scan, @@ -140,7 +146,7 @@ ivfflatcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, lists = IVFFLAT_DEFAULT_LISTS; /* Get the ratio of lists that we need to visit */ - ratio = ((double) ivfflat_probes) / lists; + ratio = (double) IvfflatGetProbes(lists) / lists; if (ratio > 1.0) ratio = 1.0; diff --git a/src/ivfflat.h b/src/ivfflat.h index 1eae4c63..06b3c41b 100644 --- a/src/ivfflat.h +++ b/src/ivfflat.h @@ -61,7 +61,7 @@ typedef Pointer Item; #define IVFFLAT_DEFAULT_LISTS 100 #define IVFFLAT_MIN_LISTS 1 #define IVFFLAT_MAX_LISTS 32768 -#define IVFFLAT_DEFAULT_PROBES 1 +#define IVFFLAT_DEFAULT_PROBES -1 /* Build phases */ /* PROGRESS_CREATEIDX_SUBPHASE_INITIALIZE is 1 */ @@ -397,6 +397,7 @@ bool IvfflatCheckNorm(FmgrInfo *procinfo, Oid collation, Datum value); void IvfflatNormVectors(const IvfflatTypeInfo * typeInfo, Oid collation, VectorArray arr, int dimensions, MemoryContext tmpCtx); void IvfflatCheckMemoryUsage(Size totalSize); int IvfflatGetLists(Relation index); +int IvfflatGetProbes(int lists); void IvfflatGetMetaPageInfo(Relation index, int *lists, int *dimensions); void IvfflatUpdateList(Relation index, ListInfo listInfo, BlockNumber insertPage, BlockNumber originalInsertPage, BlockNumber startPage, ForkNumber forkNum); void IvfflatCommitBuffer(Buffer buf, GenericXLogState *state); diff --git a/src/ivfscan.c b/src/ivfscan.c index d1d092a6..84fa3238 100644 --- a/src/ivfscan.c +++ b/src/ivfscan.c @@ -583,7 +583,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) IvfflatScanOpaque so; int lists; int dimensions; - int probes = ivfflat_probes; + int probes; int maxProbes; MemoryContext oldCtx; @@ -592,6 +592,11 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) /* Get lists and dimensions from metapage */ IvfflatGetMetaPageInfo(index, &lists, &dimensions); + probes = IvfflatGetProbes(lists); + + if (probes <= 0) + elog(ERROR, "No probes"); + if (ivfflat_iterative_scan != IVFFLAT_ITERATIVE_SCAN_OFF) maxProbes = Max(ivfflat_max_probes, probes); else diff --git a/src/ivfutils.c b/src/ivfutils.c index 6909ad63..2f3be62f 100644 --- a/src/ivfutils.c +++ b/src/ivfutils.c @@ -65,6 +65,20 @@ IvfflatGetLists(Relation index) return IVFFLAT_DEFAULT_LISTS; } +/* + * Get the number of probes + */ +int +IvfflatGetProbes(int lists) +{ + int probes = ivfflat_probes; + + if (probes == -1) + return round(sqrt(lists)); + + return probes; +} + /* * Get proc */ diff --git a/test/expected/ivfflat_vector.out b/test/expected/ivfflat_vector.out index e8779aa5..80d45f7c 100644 --- a/test/expected/ivfflat_vector.out +++ b/test/expected/ivfflat_vector.out @@ -95,6 +95,7 @@ CREATE TABLE t (val vector(3)); INSERT INTO t (val) VALUES ('[0,0,0]'), ('[1,2,3]'), ('[1,1,1]'), (NULL); CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 3); SET ivfflat.iterative_scan = relaxed_order; +SET ivfflat.probes = 1; SELECT * FROM t ORDER BY val <-> '[3,3,3]'; val --------- @@ -128,6 +129,7 @@ SELECT * FROM t ORDER BY val <-> '[3,3,3]'; (0 rows) RESET ivfflat.iterative_scan; +RESET ivfflat.probes; RESET ivfflat.max_probes; DROP TABLE t; -- unlogged @@ -155,13 +157,15 @@ DROP TABLE t; SHOW ivfflat.probes; ivfflat.probes ---------------- - 1 + -1 (1 row) +SET ivfflat.probes = -2; +ERROR: -2 is outside the valid range for parameter "ivfflat.probes" (-1 .. 32768) SET ivfflat.probes = 0; -ERROR: 0 is outside the valid range for parameter "ivfflat.probes" (1 .. 32768) +ERROR: invalid value for parameter "ivfflat.probes": 0 SET ivfflat.probes = 32769; -ERROR: 32769 is outside the valid range for parameter "ivfflat.probes" (1 .. 32768) +ERROR: 32769 is outside the valid range for parameter "ivfflat.probes" (-1 .. 32768) SHOW ivfflat.iterative_scan; ivfflat.iterative_scan ------------------------ diff --git a/test/sql/ivfflat_vector.sql b/test/sql/ivfflat_vector.sql index 615f4fc7..6080387f 100644 --- a/test/sql/ivfflat_vector.sql +++ b/test/sql/ivfflat_vector.sql @@ -52,6 +52,7 @@ INSERT INTO t (val) VALUES ('[0,0,0]'), ('[1,2,3]'), ('[1,1,1]'), (NULL); CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 3); SET ivfflat.iterative_scan = relaxed_order; +SET ivfflat.probes = 1; SELECT * FROM t ORDER BY val <-> '[3,3,3]'; SET ivfflat.max_probes = 1; @@ -64,6 +65,7 @@ TRUNCATE t; SELECT * FROM t ORDER BY val <-> '[3,3,3]'; RESET ivfflat.iterative_scan; +RESET ivfflat.probes; RESET ivfflat.max_probes; DROP TABLE t; @@ -85,6 +87,7 @@ CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 32769); DROP TABLE t; SHOW ivfflat.probes; +SET ivfflat.probes = -2; SET ivfflat.probes = 0; SET ivfflat.probes = 32769;