From ac79f79637ae3f1050be6498d82ba80a63c071ff Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sat, 20 Jan 2024 17:07:36 -0800 Subject: [PATCH] Allow override --- src/hnsw.c | 21 ++++++++++++++++----- test/t/015_hnsw_duplicates.pl | 5 ++--- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/hnsw.c b/src/hnsw.c index 255d06f..b498bad 100644 --- a/src/hnsw.c +++ b/src/hnsw.c @@ -83,11 +83,8 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, List *qinfos; #endif - /* - * Never use index without order or limit, or if limit + offset > - * ef_search - */ - if (path->indexorderbys == NULL || root->limit_tuples < 0 || root->limit_tuples > hnsw_ef_search) + /* Never use index without order or limit */ + if (path->indexorderbys == NULL || root->limit_tuples < 0) { *indexStartupCost = DBL_MAX; *indexTotalCost = DBL_MAX; @@ -97,6 +94,20 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, return; } + /* + * Do not use index if limit + offset > ef_search unless enable_seqscan = + * off + */ + if (root->limit_tuples > hnsw_ef_search) + { + *indexStartupCost = 1.0e10 - 1; + *indexTotalCost = 1.0e10 - 1; + *indexSelectivity = 0; + *indexCorrelation = 0; + *indexPages = 0; + return; + } + MemSet(&costs, 0, sizeof(costs)); index = index_open(path->indexinfo->indexoid, NoLock); diff --git a/test/t/015_hnsw_duplicates.pl b/test/t/015_hnsw_duplicates.pl index 4cc7f03..874ec43 100644 --- a/test/t/015_hnsw_duplicates.pl +++ b/test/t/015_hnsw_duplicates.pl @@ -23,11 +23,10 @@ sub insert_vectors sub test_duplicates { - # TODO Improve my $res = $node->safe_psql("postgres", qq( SET enable_seqscan = off; - SET hnsw.ef_search = 10; - SELECT COUNT(*) FROM (SELECT * FROM tst ORDER BY v <-> '[1,1,1]' LIMIT 10) t; + SET hnsw.ef_search = 1; + SELECT COUNT(*) FROM (SELECT * FROM tst ORDER BY v <-> '[1,1,1]' LIMIT 20) t; )); is($res, 10); }