postgres.git / summary / log / commit / refs

commit    d51990fdba1f3afb267eb6051b32283a20de6387
Author:   Fujii Masao <fujii@postgresql.org>
Date:     Tue Nov 24 02:28:40 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 | 12 +++++++++++- src/test/regress/expected/cluster.out | 22 ++++++++++++++++++++++ src/test/regress/sql/cluster.sql | 17 +++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 36a57f24148..83f8f93b7c9 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -7164,6 +7164,14 @@ plan_cluster_use_sort(Oid tableOid, Oid indexOid) glob = makeNode(PlannerGlobal); + /* + * Initialize the strategies needed for the two non-partial scan paths we + * compare below. Index scans are enabled, as checked above. + */ + glob->default_pgs_mask = PGS_CONSIDER_NONPARTIAL | PGS_INDEXSCAN; + if (enable_seqscan) + glob->default_pgs_mask |= PGS_SEQSCAN; + root = makeNode(PlannerInfo); root->parse = query; root->glob = glob; @@ -7240,7 +7248,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 45759d3eccb..26c4a2d52e9 100644 --- a/src/test/regress/expected/cluster.out +++ b/src/test/regress/expected/cluster.out @@ -878,6 +878,27 @@ SELECT * FROM cluster_aborted_upd; 1 | 3 (1 row) +-- REPACK 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 +REPACK (VERBOSE) clstr_sort_test USING INDEX clstr_sort_test_idx; +INFO: repacking "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; +REPACK (VERBOSE) clstr_sort_test USING INDEX clstr_sort_test_idx; +INFO: repacking "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; +REPACK (VERBOSE) clstr_sort_test USING INDEX clstr_sort_test_idx; +INFO: repacking "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; @@ -887,4 +908,5 @@ DROP TABLE clstr_4; DROP TABLE clstr_expression; DROP TABLE clstrpart; 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 af72bf43f7a..263f777c358 100644 --- a/src/test/regress/sql/cluster.sql +++ b/src/test/regress/sql/cluster.sql @@ -448,6 +448,22 @@ COMMIT; SELECT count(*) FROM cluster_aborted_upd; SELECT * FROM cluster_aborted_upd; +-- REPACK 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 +REPACK (VERBOSE) clstr_sort_test USING INDEX clstr_sort_test_idx; +SET enable_sort = off; +REPACK (VERBOSE) clstr_sort_test USING INDEX clstr_sort_test_idx; +RESET enable_sort; +SET enable_seqscan = off; +REPACK (VERBOSE) clstr_sort_test USING INDEX clstr_sort_test_idx; +\set VERBOSITY default +RESET enable_seqscan; +RESET random_page_cost; + -- clean up DROP TABLE clustertest; DROP TABLE clstr_1; @@ -457,5 +473,6 @@ DROP TABLE clstr_4; DROP TABLE clstr_expression; DROP TABLE clstrpart; DROP TABLE cluster_aborted_upd; +DROP TABLE clstr_sort_test; DROP USER regress_clstr_user; [parent: 492bc90572bb]