From e718eb8da41e586cc58a9849993e25d1bb32b2b2 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Mon, 21 Oct 2024 20:38:50 -0700 Subject: [PATCH] Updated range and defaults for iterative search parameters --- src/hnsw.c | 4 ++-- src/hnswscan.c | 2 +- src/ivfflat.c | 4 ++-- src/ivfscan.c | 12 +----------- test/expected/hnsw_vector.out | 6 +++--- test/expected/ivfflat_vector.out | 15 ++++----------- test/sql/hnsw_vector.sql | 2 +- test/sql/ivfflat_vector.sql | 5 +---- test/t/043_hnsw_iterative_search.pl | 3 ++- 9 files changed, 17 insertions(+), 36 deletions(-) diff --git a/src/hnsw.c b/src/hnsw.c index 5c747b5..2a678dd 100644 --- a/src/hnsw.c +++ b/src/hnsw.c @@ -84,8 +84,8 @@ HnswInit(void) /* This is approximate and does not apply to the initial scan */ DefineCustomIntVariable("hnsw.max_search_tuples", "Sets the max number of candidates to visit for iterative search", - "-1 means no limit", &hnsw_max_search_tuples, - -1, -1, INT_MAX, PGC_USERSET, 0, NULL, NULL, NULL); + NULL, &hnsw_max_search_tuples, + 20000, 1, INT_MAX, PGC_USERSET, 0, NULL, NULL, NULL); MarkGUCPrefixReserved("hnsw"); } diff --git a/src/hnswscan.c b/src/hnswscan.c index ba3dea8..367723c 100644 --- a/src/hnswscan.c +++ b/src/hnswscan.c @@ -241,7 +241,7 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir) break; /* Reached max number of tuples */ - if (hnsw_max_search_tuples != -1 && so->tuples >= hnsw_max_search_tuples) + if (so->tuples >= hnsw_max_search_tuples) { if (pairingheap_is_empty(so->discarded)) break; diff --git a/src/ivfflat.c b/src/ivfflat.c index be29dd5..730651b 100644 --- a/src/ivfflat.c +++ b/src/ivfflat.c @@ -47,8 +47,8 @@ IvfflatInit(void) /* If this is less than probes, probes is used */ DefineCustomIntVariable("ivfflat.max_probes", "Sets the max number of probes for iterative search", - "-1 means no limit", &ivfflat_max_probes, - -1, -1, IVFFLAT_MAX_LISTS, PGC_USERSET, 0, NULL, NULL, NULL); + NULL, &ivfflat_max_probes, + IVFFLAT_MAX_LISTS, IVFFLAT_MIN_LISTS, IVFFLAT_MAX_LISTS, PGC_USERSET, 0, NULL, NULL, NULL); MarkGUCPrefixReserved("ivfflat"); } diff --git a/src/ivfscan.c b/src/ivfscan.c index 251f70f..af232d3 100644 --- a/src/ivfscan.c +++ b/src/ivfscan.c @@ -264,17 +264,7 @@ ivfflatbeginscan(Relation index, int nkeys, int norderbys) IvfflatGetMetaPageInfo(index, &lists, &dimensions); if (ivfflat_iterative_search != IVFFLAT_ITERATIVE_SEARCH_OFF) - { - maxProbes = ivfflat_max_probes; - - if (maxProbes < 0) - maxProbes = lists; - else if (maxProbes < probes) - { - /* TODO Show notice */ - maxProbes = probes; - } - } + maxProbes = Max(ivfflat_max_probes, probes); else maxProbes = probes; diff --git a/test/expected/hnsw_vector.out b/test/expected/hnsw_vector.out index 60eb011..134292b 100644 --- a/test/expected/hnsw_vector.out +++ b/test/expected/hnsw_vector.out @@ -177,9 +177,9 @@ HINT: Available values: off, relaxed_order, strict_order. SHOW hnsw.max_search_tuples; hnsw.max_search_tuples ------------------------ - -1 + 20000 (1 row) -SET hnsw.max_search_tuples = -2; -ERROR: -2 is outside the valid range for parameter "hnsw.max_search_tuples" (-1 .. 2147483647) +SET hnsw.max_search_tuples = 0; +ERROR: 0 is outside the valid range for parameter "hnsw.max_search_tuples" (1 .. 2147483647) DROP TABLE t; diff --git a/test/expected/ivfflat_vector.out b/test/expected/ivfflat_vector.out index 8a80ea3..729eb58 100644 --- a/test/expected/ivfflat_vector.out +++ b/test/expected/ivfflat_vector.out @@ -95,13 +95,6 @@ SELECT * FROM t ORDER BY val <-> '[3,3,3]'; [0,0,0] (3 rows) -SET ivfflat.max_probes = 0; -SELECT * FROM t ORDER BY val <-> '[3,3,3]'; - val ---------- - [1,2,3] -(1 row) - SET ivfflat.max_probes = 1; SELECT * FROM t ORDER BY val <-> '[3,3,3]'; val @@ -163,11 +156,11 @@ HINT: Available values: off, relaxed_order. SHOW ivfflat.max_probes; ivfflat.max_probes -------------------- - -1 + 32768 (1 row) -SET ivfflat.max_probes = -2; -ERROR: -2 is outside the valid range for parameter "ivfflat.max_probes" (-1 .. 32768) +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) +ERROR: 32769 is outside the valid range for parameter "ivfflat.max_probes" (1 .. 32768) DROP TABLE t; diff --git a/test/sql/hnsw_vector.sql b/test/sql/hnsw_vector.sql index ba8d4aa..3281758 100644 --- a/test/sql/hnsw_vector.sql +++ b/test/sql/hnsw_vector.sql @@ -104,6 +104,6 @@ SET hnsw.iterative_search = on; SHOW hnsw.max_search_tuples; -SET hnsw.max_search_tuples = -2; +SET hnsw.max_search_tuples = 0; DROP TABLE t; diff --git a/test/sql/ivfflat_vector.sql b/test/sql/ivfflat_vector.sql index 9e20060..4c3ac1c 100644 --- a/test/sql/ivfflat_vector.sql +++ b/test/sql/ivfflat_vector.sql @@ -53,9 +53,6 @@ CREATE INDEX ON t USING ivfflat (val vector_l2_ops) WITH (lists = 3); SET ivfflat.iterative_search = relaxed_order; SELECT * FROM t ORDER BY val <-> '[3,3,3]'; -SET ivfflat.max_probes = 0; -SELECT * FROM t ORDER BY val <-> '[3,3,3]'; - SET ivfflat.max_probes = 1; SELECT * FROM t ORDER BY val <-> '[3,3,3]'; @@ -93,7 +90,7 @@ SET ivfflat.iterative_search = on; SHOW ivfflat.max_probes; -SET ivfflat.max_probes = -2; +SET ivfflat.max_probes = 0; SET ivfflat.max_probes = 32769; DROP TABLE t; diff --git a/test/t/043_hnsw_iterative_search.pl b/test/t/043_hnsw_iterative_search.pl index 8e1aa1e..ea48010 100644 --- a/test/t/043_hnsw_iterative_search.pl +++ b/test/t/043_hnsw_iterative_search.pl @@ -27,6 +27,7 @@ $node->safe_psql("postgres", qq( my $count = $node->safe_psql("postgres", qq( SET enable_seqscan = off; SET hnsw.iterative_search = relaxed_order; + SET hnsw.max_search_tuples = 100000; SET work_mem = '8MB'; SELECT COUNT(*) FROM (SELECT v FROM tst WHERE i % 10000 = 0 ORDER BY v <-> (SELECT v FROM tst LIMIT 1) LIMIT 11) t; )); @@ -59,7 +60,7 @@ my ($ret, $stdout, $stderr) = $node->psql("postgres", qq( SET enable_seqscan = off; SET hnsw.iterative_search = relaxed_order; SET client_min_messages = debug1; - SET work_mem = '2MB'; + SET work_mem = '1MB'; SELECT COUNT(*) FROM (SELECT v FROM tst WHERE i % 10000 = 0 ORDER BY v <-> (SELECT v FROM tst LIMIT 1) LIMIT 11) t; )); like($stderr, qr/hnsw index scan exceeded work_mem after \d+ tuples/);