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.

Reported-by: Noah <[email protected]>
Author: Raja Sai Pranav <[email protected]>
Reviewed-by: Fujii Masao <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Backpatch-through: 18

Branch
------
REL_18_STABLE

Details
-------
https://git.postgresql.org/pg/commitdiff/d7025bf715cf94f2c5d16aae47a04626397993d7

Modified Files
--------------
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(-)

Reply via email to