BUG #19881: CLUSTER uses a disabled sort path when enable_sort is off
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.
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:
Show quoted text
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.
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
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:
Show quoted text
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
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:
Show quoted text
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 PranavOn 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
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
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:
/messages/by-id/nh-3b9619a3-ca8e-4e6f-b78b-6dcec89a9d7d@google.com
Sorry for the noise.
Regards,
Sai Pranav
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:
t258495_8v3-0001-Honor-enable_sort-and-enable_seqscan-in-CLUSTER-a.patchapplication/octet-stream; name=v3-0001-Honor-enable_sort-and-enable_seqscan-in-CLUSTER-a.patchDownload+50-2
v3-0002-doc-Clarify-scan-method-selection-for-REPACK-and-.patchapplication/octet-stream; name=v3-0002-doc-Clarify-scan-method-selection-for-REPACK-and-.patchDownload+14-4
nocfbot-v3-0001-Honor-enable_sort-and-enable_seqscan-in-CLUSTER-v18.patchapplication/octet-stream; name=nocfbot-v3-0001-Honor-enable_sort-and-enable_seqscan-in-CLUSTER-v18.patchDownload+42-2
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
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