From 4f712ea389d7a0a31e05e82543d000315496183a Mon Sep 17 00:00:00 2001 From: Sai Pranav Date: Sat, 21 Nov 2026 05:58:59 +0000 Subject: [PATCH v3] Honor enable_sort and enable_seqscan in CLUSTER CLUSTER 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. Fix this by using compare_path_costs() to compare disabled-node counts before costs. 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. #Bug: 19881 Reported-by: Noah Author: Raja Sai Pranav Reviewed-by: Fujii Masao 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; -- 2.56.0