pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
11+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-11-24 f5ead25982  doc: Clarify scan method selection for REPACK and CLUSTER
  2026-11-24 927939e924 d51990fdba d7025bf715  Honor enable_sort and enable_seqscan in CLUSTER and REPACK
* BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
@ 2026-11-11 13:45 PG Bug reporting form <noreply@postgresql.org>
  2026-11-20 17:12 ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: PG Bug reporting form @ 2026-11-11 13:45 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: imchifan8@gmail.com

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.








^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
  2026-11-11 13:45 BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off PG Bug reporting form <noreply@postgresql.org>
@ 2026-11-20 17:12 ` Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 02:28   ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Raja Sai pranav @ 2026-11-20 17:12 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: imchifan8@gmail.com

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)
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
  2026-11-11 13:45 BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off PG Bug reporting form <noreply@postgresql.org>
  2026-11-20 17:12 ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
@ 2026-11-21 02:28   ` Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 06:00     ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Fujii Masao @ 2026-11-21 02:28 UTC (permalink / raw)
  To: Raja Sai pranav <rajasaipranav0@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; imchifan8@gmail.com

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






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
  2026-11-11 13:45 BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off PG Bug reporting form <noreply@postgresql.org>
  2026-11-20 17:12 ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 02:28   ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
@ 2026-11-21 06:00     ` Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 06:24       ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 06:54       ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 2 replies; 11+ messages in thread

From: Raja Sai pranav @ 2026-11-21 06:00 UTC (permalink / raw)
  To: masao.fujii@gmail.com; +Cc: pgsql-bugs@lists.postgresql.org; imchifan8@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)
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
  2026-11-11 13:45 BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off PG Bug reporting form <noreply@postgresql.org>
  2026-11-20 17:12 ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 02:28   ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 06:00     ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
@ 2026-11-21 06:24       ` Raja Sai pranav <rajasaipranav0@gmail.com>
  1 sibling, 0 replies; 11+ messages in thread

From: Raja Sai pranav @ 2026-11-21 06:24 UTC (permalink / raw)
  To: masao.fujii@gmail.com; +Cc: pgsql-bugs@lists.postgresql.org; imchifan8@gmail.com

Hi,

Here is the separate docs patch. The only place that mentions this is
"Notes on Resources" in repack.sgml, which the CLUSTER page points to. It
adds one sentence: with enable_indexscan off, a sequential scan and sort is
always used, and with enable_seqscan off, the index scan is preferred. Both
match what I saw in the 8-combination tests. The enable_seqscan part is
only true with the v2 code fix applied, so this goes on top of it.

I couldn't build the full docs locally because the DocBook DTD isn't
installed. I checked that the paragraph is well-formed and that the three
link targets exist in config.sgml.

Regards,
Sai Pranav

On Sat, Nov 21, 2026 11:30 AM, Raja Sai pranav <rajasaipranav0@gmail.com>
wrote:

> 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:

v1-0001-doc-Mention-enable_indexscan-and-enable_seqscan-i.patch (text/x-patch, 1.5K) download From 08b4dc8bf7aa2d91dbb7b9ee6123133a2bbbeba5 Mon Sep 17 00:00:00 2001 From: Sai Pranav <rajasaipranav0@gmail.com> Date: Sat, 21 Nov 2026 06:23:54 +0000 Subject: [PATCH v1] doc: Mention enable_indexscan and enable_seqscan in REPACK resource notes The notes said only that enable_sort can stop REPACK USING INDEX (and so CLUSTER) from choosing a sequential scan and sort. enable_indexscan and enable_seqscan also affect that choice, so say how. Suggested by Fujii Masao. Discussion: https://postgr.es/m/19881-cc00ace224a1fa3e@postgresql.org --- doc/src/sgml/ref/repack.sgml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/ref/repack.sgml b/doc/src/sgml/ref/repack.sgml index 4cd3cfa0b1..58ab8ac391 100644 --- a/doc/src/sgml/ref/repack.sgml +++ b/doc/src/sgml/ref/repack.sgml @@ -294,7 +294,11 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING requirement is intolerable, you can disable this choice by temporarily setting <xref linkend="guc-enable-sort"/> to <literal>off</literal>; or omitting <literal>USING INDEX</literal> if the table doesn't need to be - reordered. + reordered. The choice also follows <xref linkend="guc-enable-indexscan"/> + and <xref linkend="guc-enable-seqscan"/>: with + <varname>enable_indexscan</varname> off, a sequential scan and sort is + always used, and with <varname>enable_seqscan</varname> off, the index + scan is preferred. </para> <para> -- 2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
  2026-11-11 13:45 BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off PG Bug reporting form <noreply@postgresql.org>
  2026-11-20 17:12 ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 02:28   ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 06:00     ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
@ 2026-11-21 06:54       ` Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 07:14         ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  1 sibling, 1 reply; 11+ messages in thread

From: Fujii Masao @ 2026-11-21 06:54 UTC (permalink / raw)
  To: Raja Sai pranav <rajasaipranav0@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; imchifan8@gmail.com

On Sat, Nov 21, 2026 at 3:00 PM Raja Sai pranav
<rajasaipranav0@gmail.com> wrote:
>
>  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.

It's helpful if you provide the patches for old stable versions.

BTW, regarding the discussion on the list, could you avoid top-posting?
Bottom-posting is the preferred style on this mailing list.

Regards,

-- 
Fujii Masao






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
  2026-11-11 13:45 BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off PG Bug reporting form <noreply@postgresql.org>
  2026-11-20 17:12 ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 02:28   ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 06:00     ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 06:54       ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
@ 2026-11-21 07:14         ` Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 14:00           ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Raja Sai pranav @ 2026-11-21 07:14 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; imchifan8@gmail.com

On Sat, Nov 21, 2026 at 3:54 PM Fujii Masao wrote:
> It's helpful if you provide the patches for old stable versions.

I sent the REL_18_STABLE version a few minutes ago, but my mail client
didn't keep it in this thread, so the archive shows it as a separate thread:
https://www.postgresql.org/message-id/nh-3b9619a3-ca8e-4e6f-b78b-6dcec89a9d7d%40google.com

Sorry for the noise.

Regards,
Sai Pranav

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
  2026-11-11 13:45 BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off PG Bug reporting form <noreply@postgresql.org>
  2026-11-20 17:12 ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 02:28   ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 06:00     ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 06:54       ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 07:14         ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
@ 2026-11-21 14:00           ` Fujii Masao <masao.fujii@gmail.com>
  2026-11-22 12:29             ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Fujii Masao @ 2026-11-21 14:00 UTC (permalink / raw)
  To: Raja Sai pranav <rajasaipranav0@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; imchifan8@gmail.com

On Sat, Nov 21, 2026 at 4:14 PM Raja Sai pranav
<rajasaipranav0@gmail.com> wrote:
> I sent the REL_18_STABLE version a few minutes ago, but my mail client didn't keep it in this thread, so the archive shows it as a separate thread:

Ok. Anyway, thanks for the v18 and docs patches!

Attached are updated versions of all three patches.

For master and v19, I changed the tests to use REPACK, which exercises
the same code as CLUSTER, and moved them before the final cleanup section.

For v18, I added a test case for enable_seqscan and moved the tests before
cleanup to keep the test structure consistent across branches.

For the docs, I moved the GUC descriptions into the B-tree scan method
discussion to clarify their scope. I also clarified that enable_indexscan = off
takes precedence over the other two settings.

I've updated the commit messages as well. The code fix will be backpatched
to v18, while the docs change is for master only.

Barring any objections, I will commit them.

Regards,

-- 
Fujii Masao

Attachments: download all (3 files, .tar.gz)

v3-0001-Honor-enable_sort-and-enable_seqscan-in-CLUSTER-a.patch (application/octet-stream, 5.4K) download From 29684fce1f81298cebb5f79df36bf682ade5f37c Mon Sep 17 00:00:00 2001 From: Sai Pranav <rajasaipranav0@gmail.com> Date: Sat, 21 Nov 2026 05:58:59 +0000 Subject: [PATCH v3 1/2] Honor enable_sort and enable_seqscan in CLUSTER and REPACK CLUSTER and REPACK USING INDEX could choose a sequential scan and sort even when enable_sort or enable_seqscan was off and an enabled index scan was available. This could also cause unexpected temporary disk usage for sorting. The cause was that plan_cluster_use_sort() compared only total costs, ignoring the disabled-node counts used to enforce these settings. In v19 and later, it also left default_pgs_mask at zero, marking both scan paths as disabled regardless of enable_seqscan. Fix this by using compare_path_costs() to compare disabled-node counts before costs, and initializing the strategy mask for the scan paths. An enabled index scan is now preferred when either enable_sort or enable_seqscan is off, regardless of the estimated costs. Backpatch to v18, where the cost comparison regression was introduced. #Bug: 19881 Reported-by: Noah <imchifan8@gmail.com> Author: Raja Sai Pranav <rajasaipranav0@gmail.com> Reviewed-by: Fujii Masao <masao.fujii@gmail.com> Discussion: https://postgr.es/m/19881-cc00ace224a1fa3e@postgresql.org Backpatch-through: 18 --- src/backend/optimizer/plan/planner.c | 12 +++++++++++- src/test/regress/expected/cluster.out | 22 ++++++++++++++++++++++ src/test/regress/sql/cluster.sql | 17 +++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index bd4062d4a8f..568a1b2a346 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); + /* + * Initialize the strategies needed for the two non-partial scan paths we + * compare below. Index scans are enabled, as checked above. + */ + 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 708db970012..2fc706563f5 100644 --- a/src/test/regress/expected/cluster.out +++ b/src/test/regress/expected/cluster.out @@ -914,6 +914,27 @@ SELECT * FROM cluster_aborted_upd; 1 | 3 (1 row) +-- REPACK must honor enable_sort and enable_seqscan when choosing a scan method. +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 +REPACK (VERBOSE) clstr_sort_test USING INDEX 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; +REPACK (VERBOSE) clstr_sort_test USING INDEX 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; +REPACK (VERBOSE) clstr_sort_test USING INDEX 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; -- clean up DROP TABLE clustertest; DROP TABLE clstr_1; @@ -923,4 +944,5 @@ DROP TABLE clstr_4; DROP TABLE clstr_expression; DROP TABLE clstrpart; DROP TABLE cluster_aborted_upd; +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 12b351bbb05..c6d3b570ea2 100644 --- a/src/test/regress/sql/cluster.sql +++ b/src/test/regress/sql/cluster.sql @@ -476,6 +476,22 @@ COMMIT; SELECT count(*) FROM cluster_aborted_upd; SELECT * FROM cluster_aborted_upd; +-- REPACK must honor enable_sort and enable_seqscan when choosing a scan method. +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 +REPACK (VERBOSE) clstr_sort_test USING INDEX clstr_sort_test_idx; +SET enable_sort = off; +REPACK (VERBOSE) clstr_sort_test USING INDEX clstr_sort_test_idx; +RESET enable_sort; +SET enable_seqscan = off; +REPACK (VERBOSE) clstr_sort_test USING INDEX clstr_sort_test_idx; +\set VERBOSITY default +RESET enable_seqscan; +RESET random_page_cost; + -- clean up DROP TABLE clustertest; DROP TABLE clstr_1; @@ -485,5 +501,6 @@ DROP TABLE clstr_4; DROP TABLE clstr_expression; DROP TABLE clstrpart; DROP TABLE cluster_aborted_upd; +DROP TABLE clstr_sort_test; DROP USER regress_clstr_user; -- 2.56.0
v3-0002-doc-Clarify-scan-method-selection-for-REPACK-and-.patch (application/octet-stream, 3.0K) download From 72a6406329c86df60ec0025292e03c8b2712a92d Mon Sep 17 00:00:00 2001 From: Sai Pranav <rajasaipranav0@gmail.com> Date: Sat, 21 Nov 2026 06:23:54 +0000 Subject: [PATCH v3 2/2] doc: Clarify scan method selection for REPACK and CLUSTER The documentation described using enable_sort to avoid sorting during clustering, but did not explain how enable_indexscan and enable_seqscan affected that choice. This left the conditions for avoiding sorting unclear. Document these settings together with the scan method selection rules for B-tree indexes. Explain that enable_indexscan = off forces a sequential scan and sort regardless of the other two settings. Otherwise, disabling enable_sort or enable_seqscan favors an index scan, provided the index is usable. Suggested-by: Fujii Masao <masao.fujii@gmail.com> Author: Raja Sai Pranav <rajasaipranav0@gmail.com> Reviewed-by: Fujii Masao <masao.fujii@gmail.com> Discussion: https://postgr.es/m/CAHGQGwGhA-tzFGPV+oBQF1jK==tSkmwM=z_z9p+bhb-yYCAzLA@mail.gmail.com --- doc/src/sgml/ref/repack.sgml | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/doc/src/sgml/ref/repack.sgml b/doc/src/sgml/ref/repack.sgml index 4cd3cfa0b11..0649e033aa1 100644 --- a/doc/src/sgml/ref/repack.sgml +++ b/doc/src/sgml/ref/repack.sgml @@ -258,8 +258,17 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING the table using either an index scan on the specified index, or a sequential scan followed by sorting. It will attempt to choose the method that will be faster, based on planner cost parameters and available - statistical information. When clustering on an index of an access method - other than B-Tree, <command>REPACK</command> always uses an index scan. + statistical information. Setting <xref linkend="guc-enable-indexscan"/> + to <literal>off</literal> forces a sequential scan and sort, regardless + of <xref linkend="guc-enable-sort"/> and <xref linkend="guc-enable-seqscan"/>. + Otherwise, setting either <varname>enable_sort</varname> or + <varname>enable_seqscan</varname> to <literal>off</literal> favors an + index scan, provided the index is usable. + </para> + + <para> + When clustering on an index of an access method other than B-Tree, + <command>REPACK</command> always uses an index scan. </para> <para> @@ -292,7 +301,9 @@ REPACK [ ( <replaceable class="parameter">option</replaceable> [, ...] ) ] USING file of up to the size of the table is needed in addition to the above. This method is usually faster than an index scan, but if the disk space requirement is intolerable, you can disable this choice by temporarily - setting <xref linkend="guc-enable-sort"/> to <literal>off</literal>; or + setting <xref linkend="guc-enable-sort"/> to <literal>off</literal> + (subject to the conditions described in + <xref linkend="sql-repack-notes-on-clustering"/>); or omitting <literal>USING INDEX</literal> if the table doesn't need to be reordered. </para> -- 2.56.0
nocfbot-v3-0001-Honor-enable_sort-and-enable_seqscan-in-CLUSTER-v18.patch (application/octet-stream, 4.7K) download From 4f712ea389d7a0a31e05e82543d000315496183a Mon Sep 17 00:00:00 2001 From: Sai Pranav <rajasaipranav0@gmail.com> Date: Sat, 21 Nov 2026 05:58:59 +0000 Subject: [PATCH v3] Honor enable_sort and enable_seqscan in CLUSTER CLUSTER could choose a sequential scan and sort even when enable_sort or enable_seqscan was off and an enabled index scan was available. This could also cause unexpected temporary disk usage for sorting. The cause was that plan_cluster_use_sort() compared only total costs, ignoring the disabled-node counts used to enforce these settings. Fix this by using compare_path_costs() to compare disabled-node counts before costs. An enabled index scan is now preferred when either enable_sort or enable_seqscan is off, regardless of the estimated costs. Backpatch to v18, where the cost comparison regression was introduced. #Bug: 19881 Reported-by: Noah <imchifan8@gmail.com> Author: Raja Sai Pranav <rajasaipranav0@gmail.com> Reviewed-by: Fujii Masao <masao.fujii@gmail.com> Discussion: https://postgr.es/m/19881-cc00ace224a1fa3e@postgresql.org Backpatch-through: 18 --- src/backend/optimizer/plan/planner.c | 4 +++- src/test/regress/expected/cluster.out | 22 ++++++++++++++++++++++ src/test/regress/sql/cluster.sql | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index f997d191097..4bbbecd71a8 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.out index 277b092fd43..eca4e697e5f 100644 --- a/src/test/regress/expected/cluster.out +++ b/src/test/regress/expected/cluster.out @@ -692,6 +692,27 @@ SELECT * FROM cluster_aborted_upd; 1 | 3 (1 row) +-- CLUSTER must honor enable_sort and enable_seqscan when choosing a scan method. +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 +RESET enable_sort; +SET enable_seqscan = 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_seqscan; +RESET random_page_cost; -- clean up DROP TABLE clustertest; DROP TABLE clstr_1; @@ -700,4 +721,5 @@ DROP TABLE clstr_3; DROP TABLE clstr_4; DROP TABLE clstr_expression; DROP TABLE cluster_aborted_upd; +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 082f694fb10..de5f17db54f 100644 --- a/src/test/regress/sql/cluster.sql +++ b/src/test/regress/sql/cluster.sql @@ -331,6 +331,22 @@ COMMIT; SELECT count(*) FROM cluster_aborted_upd; SELECT * FROM cluster_aborted_upd; +-- CLUSTER must honor enable_sort and enable_seqscan when choosing a scan method. +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; + -- clean up DROP TABLE clustertest; DROP TABLE clstr_1; @@ -339,5 +355,6 @@ DROP TABLE clstr_3; DROP TABLE clstr_4; DROP TABLE clstr_expression; DROP TABLE cluster_aborted_upd; +DROP TABLE clstr_sort_test; DROP USER regress_clstr_user; -- 2.56.0
^ permalink raw reply [nested|flat] 11+ messages in thread

* Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
  2026-11-11 13:45 BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off PG Bug reporting form <noreply@postgresql.org>
  2026-11-20 17:12 ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 02:28   ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 06:00     ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 06:54       ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 07:14         ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 14:00           ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
@ 2026-11-22 12:29             ` Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-24 02:43               ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 1 reply; 11+ messages in thread

From: Raja Sai pranav @ 2026-11-22 12:29 UTC (permalink / raw)
  To: Fujii Masao <masao.fujii@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; imchifan8@gmail.com

On Sat, Nov 21, 2026 at 7:30 PM Fujii Masao <masao.fujii@gmail.com> wrote:
> Barring any objections, I will commit them.

Thanks! I applied v3 on master and on REL_18_STABLE. The regression tests
pass, and with the planner.c change reverted the new tests fail on both
branches, so they catch the bug. No objections from me.

Regards,
Sai Pranav

^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
  2026-11-11 13:45 BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off PG Bug reporting form <noreply@postgresql.org>
  2026-11-20 17:12 ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 02:28   ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 06:00     ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 06:54       ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  2026-11-21 07:14         ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
  2026-11-21 14:00           ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Fujii Masao <masao.fujii@gmail.com>
  2026-11-22 12:29             ` Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>
@ 2026-11-24 02:43               ` Fujii Masao <masao.fujii@gmail.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Fujii Masao @ 2026-11-24 02:43 UTC (permalink / raw)
  To: Raja Sai pranav <rajasaipranav0@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org; imchifan8@gmail.com

On Sun, Nov 22, 2026 at 9:30 PM Raja Sai pranav
<rajasaipranav0@gmail.com> wrote:
> Thanks! I applied v3 on master and on REL_18_STABLE. The regression tests pass, and with the planner.c change reverted the new tests fail on both branches, so they catch the bug. No objections from me.

Thanks for the review! I've pushed the patch.

Regards,

-- 
Fujii Masao






^ permalink  raw  reply  [nested|flat] 11+ messages in thread

* Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
@ 2026-11-21 07:05 Raja Sai pranav <rajasaipranav0@gmail.com>
  0 siblings, 0 replies; 11+ messages in thread

From: Raja Sai pranav @ 2026-11-21 07:05 UTC (permalink / raw)
  To: masao.fujii@gmail.com; +Cc: pgsql-bugs@lists.postgresql.org; imchifan8@gmail.com

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.c index 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.out index 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.sql index 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)
^ permalink raw reply [nested|flat] 11+ messages in thread


end of thread, other threads:[~2026-11-24 02:43 UTC | newest]

Thread overview: 11+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-11 13:45 BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off PG Bug reporting form <noreply@postgresql.org>
2026-11-20 17:12 ` Raja Sai pranav <rajasaipranav0@gmail.com>
2026-11-21 02:28   ` Fujii Masao <masao.fujii@gmail.com>
2026-11-21 06:00     ` Raja Sai pranav <rajasaipranav0@gmail.com>
2026-11-21 06:24       ` Raja Sai pranav <rajasaipranav0@gmail.com>
2026-11-21 06:54       ` Fujii Masao <masao.fujii@gmail.com>
2026-11-21 07:14         ` Raja Sai pranav <rajasaipranav0@gmail.com>
2026-11-21 14:00           ` Fujii Masao <masao.fujii@gmail.com>
2026-11-22 12:29             ` Raja Sai pranav <rajasaipranav0@gmail.com>
2026-11-24 02:43               ` Fujii Masao <masao.fujii@gmail.com>
2026-11-21 07:05 Re: BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off Raja Sai pranav <rajasaipranav0@gmail.com>

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