pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
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 22:00:07 -0800
Message-ID: <nh-8a23e327-61fa-40b1-b814-4d8997269fef@google.com> (raw)
In-Reply-To: <CAHGQGwGhA-tzFGPV+oBQF1jK==tSkmwM=z_z9p+bhb-yYCAzLA@mail.gmail.com>
References: <CAHGQGwGhA-tzFGPV+oBQF1jK==tSkmwM=z_z9p+bhb-yYCAzLA@mail.gmail.com>

Hi Fujii-san,

Thanks for looking. You are right: with default_pgs_mask at zero, both
scans count as disabled, so they tie and enable_seqscan does nothing. v2
attached adds your mask initialization as you wrote it. I first tried
making PGS_INDEXSCAN depend on enable_indexscan, but the function already
returns early when enable_indexscan is off, so your version is enough.

I ran all 8 combinations of enable_sort, enable_seqscan and
enable_indexscan on master, once with seqscan + sort cheaper
(random_page_cost = 100000) and once with the index scan cheaper
(seq_page_cost = 1000). Compared with v1, the only change is enable_seqscan
= off with sort cheaper: it now uses the index scan.

The test in cluster.sql now also checks enable_seqscan = off. It fails if
either part of the fix is removed, and the regression tests pass. v2
applies cleanly to REL_19_STABLE. REL_18_STABLE has no default_pgs_mask, so
it would need only the compare_path_costs() part; I can send a separate
version for 18 if that helps.

I agree about the docs. I'll send that as a separate patch.

Regards,
Sai Pranav

On Sat, Nov 21, 2026 07:58 AM, Fujii Masao <masao.fujii@gmail.com> wrote:

> On Sat, Nov 21, 2026 at 2:12 AM Raja Sai pranav
> <rajasaipranav0@gmail.com> wrote:
> > The patch uses compare_path_costs(), which looks at disabled nodes
> first, like the rest of the planner.
>
> Thanks for the patch!
>
> There seems another related issue: enable_seqscan = off still has no
> effect,
> even with this patch. The cause seems to be that plan_cluster_use_sort()
> leaves glob->default_pgs_mask at zero, effectively disabling both scan
> types.
>
> We should initialize the default_pgs_mask before calling
> build_simple_rel()?
> For example:
>
> -------------------------------
>   glob = makeNode(PlannerGlobal);
>
> + glob->default_pgs_mask = PGS_CONSIDER_NONPARTIAL | PGS_INDEXSCAN;
> + if (enable_seqscan)
> + glob->default_pgs_mask |= PGS_SEQSCAN;
> -------------------------------
>
> This fix seems necessary for both v19 and master.
>
> BTW, separately, I found that the CLUSTER/REPACK docs mention only
> enable_sort, although enable_seqscan and enable_indexscan also affect
> the choice. Perhaps we could document those in a separate patch.
>
> Regards,
>
> --
> Fujii Masao
>

Attachments:

v2-0001-Honor-enable_sort-and-enable_seqscan-in-CLUSTER-s.patch (text/x-patch, 4.8K) download 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)

view thread (11+ messages)  latest in thread

Message-ID: <nh-8a23e327-61fa-40b1-b814-4d8997269fef@google.com>
Permalink:  ../nh-8a23e327-61fa-40b1-b814-4d8997269fef@google.com/
Also on:    postgresql.org/message-id/nh-8a23e327-61fa-40b1-b814-4d8997269fef@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, 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-8a23e327-61fa-40b1-b814-4d8997269fef@google.com>
  References: <CAHGQGwGhA-tzFGPV+oBQF1jK==tSkmwM=z_z9p+bhb-yYCAzLA@mail.gmail.com> <nh-8a23e327-61fa-40b1-b814-4d8997269fef@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