From 242a12b7d5afe1d631d1c6ff727a691353579d21 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 25 Sep 2024 15:33:57 -0700 Subject: [PATCH] Added same cost adjustment to HNSW as IVFFlat since TOAST not included in seq scan cost - #682 [skip ci] --- src/hnsw.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/hnsw.c b/src/hnsw.c index 86c0ebc..64ff080 100644 --- a/src/hnsw.c +++ b/src/hnsw.c @@ -12,6 +12,7 @@ #include "utils/float.h" #include "utils/guc.h" #include "utils/selfuncs.h" +#include "utils/spccache.h" #if PG_VERSION_NUM < 150000 #define MarkGUCPrefixReserved(x) EmitWarningsOnPlaceholders(x) @@ -103,6 +104,7 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, int layer0TuplesMax; double layer0Selectivity; double scalingFactor = 0.55; + double spc_seq_page_cost; Relation index; /* Never use index without order */ @@ -159,6 +161,23 @@ hnswcostestimate(PlannerInfo *root, IndexPath *path, double loop_count, genericcostestimate(root, path, loop_count, &costs); + get_tablespace_page_costs(path->indexinfo->reltablespace, NULL, &spc_seq_page_cost); + + /* Adjust cost if needed since TOAST not included in seq scan cost */ + if (costs.numIndexPages > path->indexinfo->rel->pages) + { + /* Change all page cost from random to sequential */ + costs.indexTotalCost -= costs.numIndexPages * (costs.spc_random_page_cost - spc_seq_page_cost); + + /* Remove cost of extra pages */ + costs.indexTotalCost -= (costs.numIndexPages - path->indexinfo->rel->pages) * spc_seq_page_cost; + } + else + { + /* Change some page cost from random to sequential */ + costs.indexTotalCost -= 0.5 * costs.numIndexPages * (costs.spc_random_page_cost - spc_seq_page_cost); + } + /* Use total cost since most work happens before first tuple is returned */ *indexStartupCost = costs.indexTotalCost; *indexTotalCost = costs.indexTotalCost;