pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Raja Sai pranav <rajasaipranav0@gmail.com>
To: 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 11:12:34 -0600
Message-ID: <nh-a8ecf828-5d53-4cf4-b046-4f25bb4207b7@google.com> (raw)
In-Reply-To: <19881-cc00ace224a1fa3e@postgresql.org>
References: <19881-cc00ace224a1fa3e@postgresql.org>

Hi,

I reproduced this on master (4a26e36f87) and attached a patch.

plan_cluster_use_sort() compares only total_cost of the seqscan + sort path
against the index scan path. enable_sort = off no longer adds a big cost to
the sort; it adds a disabled node, which this comparison ignores. The patch
uses compare_path_costs(), which looks at disabled nodes first, like the
rest of the planner.

With your settings (random_page_cost = 100000, seq_page_cost = 0.01) I
tried all four combinations of enable_sort and enable_indexscan. Only
enable_sort = off with enable_indexscan = on changes, and it now uses the
index scan. With both off, it falls back to comparing cost, as before.

The patch adds a test to cluster.sql that checks the CLUSTER (VERBOSE)
message ("using index scan" vs "using sequential scan and sort"). It uses
an empty table and VERBOSITY terse, so the output has no page counts or
timings. The test fails without the fix, and the regression tests pass with
it. REL_18_STABLE has the same code.

Regards,
Sai Pranav

On Fri, Nov 20, 2026 10:41 PM, PG Bug reporting form <noreply@postgresql.org>
wrote:

> The following bug has been logged on the website:
>
> Bug reference:      19881
> Logged by:          Noah
> Email address:      imchifan8@gmail.com
> PostgreSQL version: 18.6
> Operating system:   Linux/amd64
> Description:
>
> CLUSTER can choose a sequential scan plus explicit sort even when
> enable_sort is off and an enabled index-scan alternative exists. In the
> case
> below, high random_page_cost makes the sort route numerically cheaper, but
> its additional disabled-node count should take precedence over total cost
> and cause the index route to be selected.
>
> With enable_sort on, trace_sort reports "begin tuple sort", as expected.
> With enable_sort off, the same message still appears, showing that CLUSTER
> selected the disabled explicit-sort route.
>
> How to repeat:
>
> Save the following as setup.sql:
>
> ```
> DROP TABLE IF EXISTS cluster_control;
> DROP TABLE IF EXISTS cluster_test;
> CREATE TABLE cluster_control (a integer, filler text);
> CREATE TABLE cluster_test (a integer, filler text);
> INSERT INTO cluster_control
> SELECT g, repeat('x', 80) FROM generate_series(1, 50000) AS g;
> INSERT INTO cluster_test
> SELECT * FROM cluster_control;
> CREATE INDEX cluster_control_idx ON cluster_control (a);
> CREATE INDEX cluster_test_idx ON cluster_test (a);
> ANALYZE cluster_control;
> ANALYZE cluster_test;
> ```
>
> Save the control as control.sql:
>
> ```
> SET client_min_messages = log;
> SET trace_sort = on;
> SET random_page_cost = 100000;
> SET seq_page_cost = 0.01;
> SET enable_sort = on;
> CLUSTER cluster_control USING cluster_control_idx;
> ```
>
> Save the test as test.sql:
>
> ```
> SET client_min_messages = log;
> SET trace_sort = on;
> SET random_page_cost = 100000;
> SET seq_page_cost = 0.01;
> SET enable_sort = off;
> CLUSTER cluster_test USING cluster_test_idx;
> ```
>
> Run the files in order, with the control and test in separate sessions:
>
> ```
> psql -X -d postgres -f setup.sql
> psql -X -d postgres -f control.sql
> psql -X -d postgres -f test.sql
> ```
>
> Both CLUSTER commands complete. The decisive recorded output for the
> control
> includes:
>
> ```
> SET
> SET
> SET
> SET
> SET
> CLUSTER
> LOG:  begin tuple sort: nkeys = 1, workMem = 65536, randomAccess = f
> ```
>
> The test produces the same strategy discriminator despite enable_sort being
> off:
>
> ```
> SET
> SET
> SET
> SET
> SET
> CLUSTER
> LOG:  begin tuple sort: nkeys = 1, workMem = 65536, randomAccess = f
> ```
>
> Other trace_sort messages report index sorts used while rebuilding indexes;
> the "begin tuple sort" message identifies the explicit sort used to
> implement CLUSTER itself.
>
> This was reproduced on PostgreSQL 18.6 on Linux/amd64. The behavior
> suggests
> that CLUSTER's manual comparison of its alternatives uses numeric cost
> without first honoring the disabled-node count. This occurs when CLUSTER
> chooses between an index scan and a numerically cheaper sequential scan
> plus
> sort while explicit sorts are disabled.
>
>
>
>
>
>
>

Attachments:

v1-0001-Honor-disabled-nodes-when-CLUSTER-chooses-between.patch (text/x-patch, 3.6K) download 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)

view thread (11+ messages)  latest in thread

Message-ID: <nh-a8ecf828-5d53-4cf4-b046-4f25bb4207b7@google.com>
Permalink:  ../nh-a8ecf828-5d53-4cf4-b046-4f25bb4207b7@google.com/
Also on:    postgresql.org/message-id/nh-a8ecf828-5d53-4cf4-b046-4f25bb4207b7@google.com

 ·  · 

reply

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, imchifan8@gmail.com
  Subject: Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
  In-Reply-To: <nh-a8ecf828-5d53-4cf4-b046-4f25bb4207b7@google.com>
  References: <19881-cc00ace224a1fa3e@postgresql.org> <nh-a8ecf828-5d53-4cf4-b046-4f25bb4207b7@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