From 0aac0943362080b29657a7539644a4716023b2e0 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Tue, 7 May 2024 13:32:47 -0400 Subject: [PATCH 4/4] When enable_gathermerge=false, don't generate gather merge paths. Previously, we generated them, and then added disable_cost to the startup cost of each one. Not generating them at all is cheaper. --- src/backend/optimizer/path/allpaths.c | 25 ++++++++++++++++--------- src/backend/optimizer/path/costsize.c | 4 ++-- src/backend/optimizer/plan/planner.c | 6 +++++- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/backend/optimizer/path/allpaths.c b/src/backend/optimizer/path/allpaths.c index aa78c0af0c..a1d1fefa05 100644 --- a/src/backend/optimizer/path/allpaths.c +++ b/src/backend/optimizer/path/allpaths.c @@ -3090,18 +3090,21 @@ generate_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_rows) * For each useful ordering, we can consider an order-preserving Gather * Merge. */ - foreach(lc, rel->partial_pathlist) + if (enable_gathermerge) { - Path *subpath = (Path *) lfirst(lc); - GatherMergePath *path; + foreach(lc, rel->partial_pathlist) + { + Path *subpath = (Path *) lfirst(lc); + GatherMergePath *path; - if (subpath->pathkeys == NIL) - continue; + if (subpath->pathkeys == NIL) + continue; - rows = subpath->rows * subpath->parallel_workers; - path = create_gather_merge_path(root, rel, subpath, rel->reltarget, - subpath->pathkeys, NULL, rowsp); - add_path(rel, &path->path); + rows = subpath->rows * subpath->parallel_workers; + path = create_gather_merge_path(root, rel, subpath, rel->reltarget, + subpath->pathkeys, NULL, rowsp); + add_path(rel, &path->path); + } } } @@ -3214,6 +3217,10 @@ generate_useful_gather_paths(PlannerInfo *root, RelOptInfo *rel, bool override_r /* generate the regular gather (merge) paths */ generate_gather_paths(root, rel, override_rows); + /* beyond this point, we only create gather merge paths */ + if (!enable_gathermerge) + return; + /* consider incremental sort for interesting orderings */ useful_pathkeys_list = get_useful_pathkeys_for_relation(root, rel, true); diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c index f56b6efe34..8b58e71fc7 100644 --- a/src/backend/optimizer/path/costsize.c +++ b/src/backend/optimizer/path/costsize.c @@ -490,8 +490,8 @@ cost_gather_merge(GatherMergePath *path, PlannerInfo *root, else path->path.rows = rel->rows; - if (!enable_gathermerge) - startup_cost += disable_cost; + /* shouldn't reach here if enable_gathermerge = false */ + Assert(enable_gathermerge); /* * Add one to the number of workers to account for the leader. This might diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 032818423f..5c4157afba 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -5231,7 +5231,7 @@ create_ordered_paths(PlannerInfo *root, * to the required output order and then use Gather Merge. */ if (ordered_rel->consider_parallel && root->sort_pathkeys != NIL && - input_rel->partial_pathlist != NIL) + input_rel->partial_pathlist != NIL && enable_gathermerge) { Path *cheapest_partial_path; @@ -7426,6 +7426,10 @@ gather_grouping_paths(PlannerInfo *root, RelOptInfo *rel) /* Try Gather for unordered paths and Gather Merge for ordered ones. */ generate_useful_gather_paths(root, rel, true); + /* beyond this point, we only create gather merge paths */ + if (!enable_gathermerge) + return; + cheapest_partial_path = linitial(rel->partial_pathlist); /* XXX Shouldn't this also consider the group-key-reordering? */ -- 2.39.3 (Apple Git-145)