From 0ee7987c8968e2dbefe01f2dc6795806f501b6d2 Mon Sep 17 00:00:00 2001
From: Sai Pranav <rajasaipranav0@gmail.com>
Date: Fri, 20 Nov 2026 17:11:33 +0000
Subject: [PATCH v1] Honor disabled nodes when CLUSTER chooses between sort and
 index scan

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.

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

diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index bd4062d4a8..51a4f657b6 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -7240,7 +7240,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..e28d2eff04 100644
--- a/src/test/regress/expected/cluster.out
+++ b/src/test/regress/expected/cluster.out
@@ -923,4 +923,21 @@ DROP TABLE clstr_4;
 DROP TABLE clstr_expression;
 DROP TABLE clstrpart;
 DROP TABLE cluster_aborted_upd;
+-- CLUSTER must honor enable_sort 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
+\set VERBOSITY default
+RESET enable_sort;
+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..65e7a3d5a3 100644
--- a/src/test/regress/sql/cluster.sql
+++ b/src/test/regress/sql/cluster.sql
@@ -486,4 +486,18 @@ DROP TABLE clstr_expression;
 DROP TABLE clstrpart;
 DROP TABLE cluster_aborted_upd;
 
+-- CLUSTER must honor enable_sort 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;
+\set VERBOSITY default
+RESET enable_sort;
+RESET random_page_cost;
+DROP TABLE clstr_sort_test;
+
 DROP USER regress_clstr_user;
-- 
2.50.1 (Apple Git-155)

