From fcfe75755c81ee3cb4357b4639072b08d281ea8f Mon Sep 17 00:00:00 2001
From: Sai Pranav <rajasaipranav0@gmail.com>
Date: Sat, 21 Nov 2026 05:58:59 +0000
Subject: [PATCH v2] Honor enable_sort and enable_seqscan in CLUSTER's choice
 of plan

plan_cluster_use_sort() compared only the total cost of a seqscan plus
sort against an index scan.  Settings like enable_sort no longer add a
large cost; they count disabled nodes instead, so CLUSTER could still
choose an explicit sort with enable_sort off.  Use compare_path_costs(),
which compares disabled nodes first, like the rest of the planner.

Also, the dummy PlannerGlobal left default_pgs_mask at zero, so both scan
paths were treated as disabled and enable_seqscan had no effect.  Set the
mask from enable_seqscan; the index scan is allowed here, since we have
already returned if enable_indexscan is off.  Problem with the mask and
fix for it by Fujii Masao.

Discussion: https://postgr.es/m/19881-cc00ace224a1fa3e@postgresql.org
---
 src/backend/optimizer/plan/planner.c  | 12 +++++++++++-
 src/test/regress/expected/cluster.out | 23 +++++++++++++++++++++++
 src/test/regress/sql/cluster.sql      | 18 ++++++++++++++++++
 3 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index bd4062d4a8..74ef651965 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);
 
+	/*
+	 * Without this, both scans would be treated as disabled.  We returned
+	 * above if enable_indexscan is off, so only enable_seqscan is checked.
+	 */
+	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 708db97001..e0d4a43f5a 100644
--- a/src/test/regress/expected/cluster.out
+++ b/src/test/regress/expected/cluster.out
@@ -923,4 +923,27 @@ DROP TABLE clstr_4;
 DROP TABLE clstr_expression;
 DROP TABLE clstrpart;
 DROP TABLE cluster_aborted_upd;
+-- CLUSTER must honor enable_sort and enable_seqscan when choosing between
+-- index scan and sort
+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:  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;
+CLUSTER (VERBOSE) clstr_sort_test USING 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;
+CLUSTER (VERBOSE) clstr_sort_test USING 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;
+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 12b351bbb0..cc97f54973 100644
--- a/src/test/regress/sql/cluster.sql
+++ b/src/test/regress/sql/cluster.sql
@@ -486,4 +486,22 @@ DROP TABLE clstr_expression;
 DROP TABLE clstrpart;
 DROP TABLE cluster_aborted_upd;
 
+-- CLUSTER must honor enable_sort and enable_seqscan when choosing between
+-- index scan and sort
+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;
+DROP TABLE clstr_sort_test;
+
 DROP USER regress_clstr_user;
-- 
2.50.1 (Apple Git-155)

