From: Raja Sai pranav <rajasaipranav0@gmail.com>
To: masao.fujii@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Cc: imchifan8@gmail.com
Subject: Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
Date: Fri, 20 Nov 2026 23:05:57 -0800
Message-ID: <nh-3b9619a3-ca8e-4e6f-b78b-6dcec89a9d7d@google.com> (raw)
On Sat, Nov 21, 2026 at 3:54 PM Fujii Masao <masao.fujii@gmail.com> wrote:
> It's helpful if you provide the patches for old stable versions.
Attached is a version for REL_18_STABLE. It has only the
compare_path_costs() part, since 18 has no default_pgs_mask, and the test
checks only enable_sort. I ran it on REL_18_STABLE: the regression tests
pass, and without the code change the new test fails ("using sequential
scan and sort" instead of "using index scan").
REL_17_STABLE and older don't need it. They still add disable_cost to a
disabled sort, so comparing total_cost already avoids it there. v2 applies
to REL_19_STABLE as is.
> BTW, regarding the discussion on the list, could you avoid top-posting?
> Bottom-posting is the preferred style on this mailing list.
Sorry about that, I'll bottom-post from now on.
Regards,
Sai Pranav
Attachments:REL_18-v2-0001-Honor-enable_sort-in-CLUSTER-s-choice-of-plan.patch (text/x-patch, 3.6K) download
From 9f3b71b8708d4d6071edf1f56bce526aa84fd999 Mon Sep 17 00:00:00 2001
From: Sai Pranav <rajasaipranav0@gmail.com>
Date: Sat, 21 Nov 2026 07:05:17 +0000
Subject: [PATCH v2 REL_18] Honor enable_sort 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.
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.cindex f997d19109..4bbbecd71a 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.outindex 277b092fd4..1bb1383920 100644--- a/src/test/regress/expected/cluster.out+++ b/src/test/regress/expected/cluster.out@@ -700,4 +700,21 @@ DROP TABLE clstr_3;
DROP TABLE clstr_4;
DROP TABLE clstr_expression;
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: 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+\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.sqlindex 082f694fb1..2ce30e55d6 100644--- a/src/test/regress/sql/cluster.sql+++ b/src/test/regress/sql/cluster.sql@@ -340,4 +340,18 @@ DROP TABLE clstr_4;
DROP TABLE clstr_expression;
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)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: rajasaipranav0@gmail.com, masao.fujii@gmail.com, imchifan8@gmail.com
Subject: Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
In-Reply-To: <nh-3b9619a3-ca8e-4e6f-b78b-6dcec89a9d7d@google.com>
References: <nh-3b9619a3-ca8e-4e6f-b78b-6dcec89a9d7d@google.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox