postgres.git / summary / log / commit / refs
commit d7025bf715cf94f2c5d16aae47a04626397993d7
Author: Fujii Masao <fujii@postgresql.org>
Date: Tue Nov 24 02:29:31 2026 +0000
Honor enable_sort and enable_seqscan in CLUSTER and REPACK
CLUSTER and REPACK USING INDEX could choose a sequential scan and sort
even when enable_sort or enable_seqscan was off and an enabled index
scan was available. This could also cause unexpected temporary disk
usage for sorting.
The cause was that plan_cluster_use_sort() compared only total costs,
ignoring the disabled-node counts used to enforce these settings.
In v19 and later, it also left default_pgs_mask at zero, marking both
scan paths as disabled regardless of enable_seqscan.
Fix this by using compare_path_costs() to compare disabled-node counts
before costs, and initializing the strategy mask for the scan paths.
An enabled index scan is now preferred when either enable_sort or
enable_seqscan is off, regardless of the estimated costs.
Backpatch to v18, where the cost comparison regression was introduced.
Reported-by: Noah <imchifan8@gmail.com>
Author: Raja Sai Pranav <rajasaipranav0@gmail.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/19881-cc00ace224a1fa3e@postgresql.org
Backpatch-through: 18
src/backend/optimizer/plan/planner.c | 4 +++-
src/test/regress/expected/cluster.out | 22 ++++++++++++++++++++++
src/test/regress/sql/cluster.sql | 17 +++++++++++++++++
3 files changed, 42 insertions(+), 1 deletion(-)
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index f997d191097..4bbbecd71a8 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -7197,7 +7197,9 @@ plan_cluster_use_sort(Oid tableOid, Oid indexOid)
ForwardScanDirection, false,
NULL, 1.0, false);
- return (seqScanAndSortPath.total_cost < indexScanPath->path.total_cost);
+ /* Compare disabled nodes first, so that enable_sort etc. are honored */
+ return compare_path_costs(&seqScanAndSortPath, &indexScanPath->path,
+ TOTAL_COST) < 0;
}
/*
diff --git a/src/test/regress/expected/cluster.out b/src/test/regress/expected/cluster.out
index 277b092fd43..eca4e697e5f 100644
--- a/src/test/regress/expected/cluster.out
+++ b/src/test/regress/expected/cluster.out
@@ -692,6 +692,27 @@ SELECT * FROM cluster_aborted_upd;
1 | 3
(1 row)
+-- CLUSTER must honor enable_sort and enable_seqscan when choosing a scan method.
+CREATE TABLE clstr_sort_test (a int);
+CREATE INDEX clstr_sort_test_idx ON clstr_sort_test (a);
+ANALYZE clstr_sort_test;
+SET random_page_cost = 100000; -- make seqscan + sort the cheaper choice
+\set VERBOSITY terse
+CLUSTER (VERBOSE) clstr_sort_test USING clstr_sort_test_idx;
+INFO: clustering "public.clstr_sort_test" using sequential scan and sort
+INFO: "public.clstr_sort_test": found 0 removable, 0 nonremovable row versions in 0 pages
+SET enable_sort = off;
+CLUSTER (VERBOSE) clstr_sort_test USING clstr_sort_test_idx;
+INFO: clustering "public.clstr_sort_test" using index scan on "clstr_sort_test_idx"
+INFO: "public.clstr_sort_test": found 0 removable, 0 nonremovable row versions in 0 pages
+RESET enable_sort;
+SET enable_seqscan = off;
+CLUSTER (VERBOSE) clstr_sort_test USING clstr_sort_test_idx;
+INFO: clustering "public.clstr_sort_test" using index scan on "clstr_sort_test_idx"
+INFO: "public.clstr_sort_test": found 0 removable, 0 nonremovable row versions in 0 pages
+\set VERBOSITY default
+RESET enable_seqscan;
+RESET random_page_cost;
-- clean up
DROP TABLE clustertest;
DROP TABLE clstr_1;
@@ -700,4 +721,5 @@ DROP TABLE clstr_3;
DROP TABLE clstr_4;
DROP TABLE clstr_expression;
DROP TABLE cluster_aborted_upd;
+DROP TABLE clstr_sort_test;
DROP USER regress_clstr_user;
diff --git a/src/test/regress/sql/cluster.sql b/src/test/regress/sql/cluster.sql
index 082f694fb10..de5f17db54f 100644
--- a/src/test/regress/sql/cluster.sql
+++ b/src/test/regress/sql/cluster.sql
@@ -331,6 +331,22 @@ COMMIT;
SELECT count(*) FROM cluster_aborted_upd;
SELECT * FROM cluster_aborted_upd;
+-- CLUSTER must honor enable_sort and enable_seqscan when choosing a scan method.
+CREATE TABLE clstr_sort_test (a int);
+CREATE INDEX clstr_sort_test_idx ON clstr_sort_test (a);
+ANALYZE clstr_sort_test;
+SET random_page_cost = 100000; -- make seqscan + sort the cheaper choice
+\set VERBOSITY terse
+CLUSTER (VERBOSE) clstr_sort_test USING clstr_sort_test_idx;
+SET enable_sort = off;
+CLUSTER (VERBOSE) clstr_sort_test USING clstr_sort_test_idx;
+RESET enable_sort;
+SET enable_seqscan = off;
+CLUSTER (VERBOSE) clstr_sort_test USING clstr_sort_test_idx;
+\set VERBOSITY default
+RESET enable_seqscan;
+RESET random_page_cost;
+
-- clean up
DROP TABLE clustertest;
DROP TABLE clstr_1;
@@ -339,5 +355,6 @@ DROP TABLE clstr_3;
DROP TABLE clstr_4;
DROP TABLE clstr_expression;
DROP TABLE cluster_aborted_upd;
+DROP TABLE clstr_sort_test;
DROP USER regress_clstr_user;
[parent: 6b90b651a7bc]