mirror of
https://github.com/postgres/postgres.git
synced 2025-07-08 11:42:09 +03:00
Add a Gather Merge executor node.
Like Gather, we spawn multiple workers and run the same plan in each one; however, Gather Merge is used when each worker produces the same output ordering and we want to preserve that output ordering while merging together the streams of tuples from various workers. (In a way, Gather Merge is like a hybrid of Gather and MergeAppend.) This works out to a win if it saves us from having to perform an expensive Sort. In cases where only a small amount of data would need to be sorted, it may actually be faster to use a regular Gather node and then sort the results afterward, because Gather Merge sometimes needs to wait synchronously for tuples whereas a pure Gather generally doesn't. But if this avoids an expensive sort then it's a win. Rushabh Lathia, reviewed and tested by Amit Kapila, Thomas Munro, and Neha Sharma, and reviewed and revised by me. Discussion: http://postgr.es/m/CAGPqQf09oPX-cQRpBKS0Gq49Z+m6KBxgxd_p9gX8CKk_d75HoQ@mail.gmail.com
This commit is contained in:
@ -2084,39 +2084,51 @@ set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
|
||||
|
||||
/*
|
||||
* generate_gather_paths
|
||||
* Generate parallel access paths for a relation by pushing a Gather on
|
||||
* top of a partial path.
|
||||
* Generate parallel access paths for a relation by pushing a Gather or
|
||||
* Gather Merge on top of a partial path.
|
||||
*
|
||||
* This must not be called until after we're done creating all partial paths
|
||||
* for the specified relation. (Otherwise, add_partial_path might delete a
|
||||
* path that some GatherPath has a reference to.)
|
||||
* path that some GatherPath or GatherMergePath has a reference to.)
|
||||
*/
|
||||
void
|
||||
generate_gather_paths(PlannerInfo *root, RelOptInfo *rel)
|
||||
{
|
||||
Path *cheapest_partial_path;
|
||||
Path *simple_gather_path;
|
||||
ListCell *lc;
|
||||
|
||||
/* If there are no partial paths, there's nothing to do here. */
|
||||
if (rel->partial_pathlist == NIL)
|
||||
return;
|
||||
|
||||
/*
|
||||
* The output of Gather is currently always unsorted, so there's only one
|
||||
* partial path of interest: the cheapest one. That will be the one at
|
||||
* the front of partial_pathlist because of the way add_partial_path
|
||||
* works.
|
||||
*
|
||||
* Eventually, we should have a Gather Merge operation that can merge
|
||||
* multiple tuple streams together while preserving their ordering. We
|
||||
* could usefully generate such a path from each partial path that has
|
||||
* non-NIL pathkeys.
|
||||
* The output of Gather is always unsorted, so there's only one partial
|
||||
* path of interest: the cheapest one. That will be the one at the front
|
||||
* of partial_pathlist because of the way add_partial_path works.
|
||||
*/
|
||||
cheapest_partial_path = linitial(rel->partial_pathlist);
|
||||
simple_gather_path = (Path *)
|
||||
create_gather_path(root, rel, cheapest_partial_path, rel->reltarget,
|
||||
NULL, NULL);
|
||||
add_path(rel, simple_gather_path);
|
||||
|
||||
/*
|
||||
* For each useful ordering, we can consider an order-preserving Gather
|
||||
* Merge.
|
||||
*/
|
||||
foreach (lc, rel->partial_pathlist)
|
||||
{
|
||||
Path *subpath = (Path *) lfirst(lc);
|
||||
GatherMergePath *path;
|
||||
|
||||
if (subpath->pathkeys == NIL)
|
||||
continue;
|
||||
|
||||
path = create_gather_merge_path(root, rel, subpath, rel->reltarget,
|
||||
subpath->pathkeys, NULL, NULL);
|
||||
add_path(rel, &path->path);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -126,6 +126,7 @@ bool enable_nestloop = true;
|
||||
bool enable_material = true;
|
||||
bool enable_mergejoin = true;
|
||||
bool enable_hashjoin = true;
|
||||
bool enable_gathermerge = true;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@ -372,6 +373,73 @@ cost_gather(GatherPath *path, PlannerInfo *root,
|
||||
path->path.total_cost = (startup_cost + run_cost);
|
||||
}
|
||||
|
||||
/*
|
||||
* cost_gather_merge
|
||||
* Determines and returns the cost of gather merge path.
|
||||
*
|
||||
* GatherMerge merges several pre-sorted input streams, using a heap that at
|
||||
* any given instant holds the next tuple from each stream. If there are N
|
||||
* streams, we need about N*log2(N) tuple comparisons to construct the heap at
|
||||
* startup, and then for each output tuple, about log2(N) comparisons to
|
||||
* replace the top heap entry with the next tuple from the same stream.
|
||||
*/
|
||||
void
|
||||
cost_gather_merge(GatherMergePath *path, PlannerInfo *root,
|
||||
RelOptInfo *rel, ParamPathInfo *param_info,
|
||||
Cost input_startup_cost, Cost input_total_cost,
|
||||
double *rows)
|
||||
{
|
||||
Cost startup_cost = 0;
|
||||
Cost run_cost = 0;
|
||||
Cost comparison_cost;
|
||||
double N;
|
||||
double logN;
|
||||
|
||||
/* Mark the path with the correct row estimate */
|
||||
if (rows)
|
||||
path->path.rows = *rows;
|
||||
else if (param_info)
|
||||
path->path.rows = param_info->ppi_rows;
|
||||
else
|
||||
path->path.rows = rel->rows;
|
||||
|
||||
if (!enable_gathermerge)
|
||||
startup_cost += disable_cost;
|
||||
|
||||
/*
|
||||
* Add one to the number of workers to account for the leader. This might
|
||||
* be overgenerous since the leader will do less work than other workers
|
||||
* in typical cases, but we'll go with it for now.
|
||||
*/
|
||||
Assert(path->num_workers > 0);
|
||||
N = (double) path->num_workers + 1;
|
||||
logN = LOG2(N);
|
||||
|
||||
/* Assumed cost per tuple comparison */
|
||||
comparison_cost = 2.0 * cpu_operator_cost;
|
||||
|
||||
/* Heap creation cost */
|
||||
startup_cost += comparison_cost * N * logN;
|
||||
|
||||
/* Per-tuple heap maintenance cost */
|
||||
run_cost += path->path.rows * comparison_cost * logN;
|
||||
|
||||
/* small cost for heap management, like cost_merge_append */
|
||||
run_cost += cpu_operator_cost * path->path.rows;
|
||||
|
||||
/*
|
||||
* Parallel setup and communication cost. Since Gather Merge, unlike
|
||||
* Gather, requires us to block until a tuple is available from every
|
||||
* worker, we bump the IPC cost up a little bit as compared with Gather.
|
||||
* For lack of a better idea, charge an extra 5%.
|
||||
*/
|
||||
startup_cost += parallel_setup_cost;
|
||||
run_cost += parallel_tuple_cost * path->path.rows * 1.05;
|
||||
|
||||
path->path.startup_cost = startup_cost + input_startup_cost;
|
||||
path->path.total_cost = (startup_cost + run_cost + input_total_cost);
|
||||
}
|
||||
|
||||
/*
|
||||
* cost_index
|
||||
* Determines and returns the cost of scanning a relation using an index.
|
||||
|
@ -277,6 +277,8 @@ static ModifyTable *make_modifytable(PlannerInfo *root,
|
||||
List *resultRelations, List *subplans,
|
||||
List *withCheckOptionLists, List *returningLists,
|
||||
List *rowMarks, OnConflictExpr *onconflict, int epqParam);
|
||||
static GatherMerge *create_gather_merge_plan(PlannerInfo *root,
|
||||
GatherMergePath *best_path);
|
||||
|
||||
|
||||
/*
|
||||
@ -475,6 +477,10 @@ create_plan_recurse(PlannerInfo *root, Path *best_path, int flags)
|
||||
(LimitPath *) best_path,
|
||||
flags);
|
||||
break;
|
||||
case T_GatherMerge:
|
||||
plan = (Plan *) create_gather_merge_plan(root,
|
||||
(GatherMergePath *) best_path);
|
||||
break;
|
||||
default:
|
||||
elog(ERROR, "unrecognized node type: %d",
|
||||
(int) best_path->pathtype);
|
||||
@ -1451,6 +1457,86 @@ create_gather_plan(PlannerInfo *root, GatherPath *best_path)
|
||||
return gather_plan;
|
||||
}
|
||||
|
||||
/*
|
||||
* create_gather_merge_plan
|
||||
*
|
||||
* Create a Gather Merge plan for 'best_path' and (recursively)
|
||||
* plans for its subpaths.
|
||||
*/
|
||||
static GatherMerge *
|
||||
create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path)
|
||||
{
|
||||
GatherMerge *gm_plan;
|
||||
Plan *subplan;
|
||||
List *pathkeys = best_path->path.pathkeys;
|
||||
int numsortkeys;
|
||||
AttrNumber *sortColIdx;
|
||||
Oid *sortOperators;
|
||||
Oid *collations;
|
||||
bool *nullsFirst;
|
||||
|
||||
/* As with Gather, it's best to project away columns in the workers. */
|
||||
subplan = create_plan_recurse(root, best_path->subpath, CP_EXACT_TLIST);
|
||||
|
||||
/* See create_merge_append_plan for why there's no make_xxx function */
|
||||
gm_plan = makeNode(GatherMerge);
|
||||
gm_plan->plan.targetlist = subplan->targetlist;
|
||||
gm_plan->num_workers = best_path->num_workers;
|
||||
copy_generic_path_info(&gm_plan->plan, &best_path->path);
|
||||
|
||||
/* Gather Merge is pointless with no pathkeys; use Gather instead. */
|
||||
Assert(pathkeys != NIL);
|
||||
|
||||
/* Compute sort column info, and adjust GatherMerge tlist as needed */
|
||||
(void) prepare_sort_from_pathkeys(&gm_plan->plan, pathkeys,
|
||||
best_path->path.parent->relids,
|
||||
NULL,
|
||||
true,
|
||||
&gm_plan->numCols,
|
||||
&gm_plan->sortColIdx,
|
||||
&gm_plan->sortOperators,
|
||||
&gm_plan->collations,
|
||||
&gm_plan->nullsFirst);
|
||||
|
||||
|
||||
/* Compute sort column info, and adjust subplan's tlist as needed */
|
||||
subplan = prepare_sort_from_pathkeys(subplan, pathkeys,
|
||||
best_path->subpath->parent->relids,
|
||||
gm_plan->sortColIdx,
|
||||
false,
|
||||
&numsortkeys,
|
||||
&sortColIdx,
|
||||
&sortOperators,
|
||||
&collations,
|
||||
&nullsFirst);
|
||||
|
||||
/* As for MergeAppend, check that we got the same sort key information. */
|
||||
Assert(numsortkeys == gm_plan->numCols);
|
||||
if (memcmp(sortColIdx, gm_plan->sortColIdx,
|
||||
numsortkeys * sizeof(AttrNumber)) != 0)
|
||||
elog(ERROR, "GatherMerge child's targetlist doesn't match GatherMerge");
|
||||
Assert(memcmp(sortOperators, gm_plan->sortOperators,
|
||||
numsortkeys * sizeof(Oid)) == 0);
|
||||
Assert(memcmp(collations, gm_plan->collations,
|
||||
numsortkeys * sizeof(Oid)) == 0);
|
||||
Assert(memcmp(nullsFirst, gm_plan->nullsFirst,
|
||||
numsortkeys * sizeof(bool)) == 0);
|
||||
|
||||
/* Now, insert a Sort node if subplan isn't sufficiently ordered */
|
||||
if (!pathkeys_contained_in(pathkeys, best_path->subpath->pathkeys))
|
||||
subplan = (Plan *) make_sort(subplan, numsortkeys,
|
||||
sortColIdx, sortOperators,
|
||||
collations, nullsFirst);
|
||||
|
||||
/* Now insert the subplan under GatherMerge. */
|
||||
gm_plan->plan.lefttree = subplan;
|
||||
|
||||
/* use parallel mode for parallel plans. */
|
||||
root->glob->parallelModeNeeded = true;
|
||||
|
||||
return gm_plan;
|
||||
}
|
||||
|
||||
/*
|
||||
* create_projection_plan
|
||||
*
|
||||
|
@ -3663,8 +3663,7 @@ create_grouping_paths(PlannerInfo *root,
|
||||
|
||||
/*
|
||||
* Now generate a complete GroupAgg Path atop of the cheapest partial
|
||||
* path. We need only bother with the cheapest path here, as the
|
||||
* output of Gather is never sorted.
|
||||
* path. We can do this using either Gather or Gather Merge.
|
||||
*/
|
||||
if (grouped_rel->partial_pathlist)
|
||||
{
|
||||
@ -3711,6 +3710,70 @@ create_grouping_paths(PlannerInfo *root,
|
||||
parse->groupClause,
|
||||
(List *) parse->havingQual,
|
||||
dNumGroups));
|
||||
|
||||
/*
|
||||
* The point of using Gather Merge rather than Gather is that it
|
||||
* can preserve the ordering of the input path, so there's no
|
||||
* reason to try it unless (1) it's possible to produce more than
|
||||
* one output row and (2) we want the output path to be ordered.
|
||||
*/
|
||||
if (parse->groupClause != NIL && root->group_pathkeys != NIL)
|
||||
{
|
||||
foreach(lc, grouped_rel->partial_pathlist)
|
||||
{
|
||||
Path *subpath = (Path *) lfirst(lc);
|
||||
Path *gmpath;
|
||||
double total_groups;
|
||||
|
||||
/*
|
||||
* It's useful to consider paths that are already properly
|
||||
* ordered for Gather Merge, because those don't need a
|
||||
* sort. It's also useful to consider the cheapest path,
|
||||
* because sorting it in parallel and then doing Gather
|
||||
* Merge may be better than doing an unordered Gather
|
||||
* followed by a sort. But there's no point in
|
||||
* considering non-cheapest paths that aren't already
|
||||
* sorted correctly.
|
||||
*/
|
||||
if (path != subpath &&
|
||||
!pathkeys_contained_in(root->group_pathkeys,
|
||||
subpath->pathkeys))
|
||||
continue;
|
||||
|
||||
total_groups = subpath->rows * subpath->parallel_workers;
|
||||
|
||||
gmpath = (Path *)
|
||||
create_gather_merge_path(root,
|
||||
grouped_rel,
|
||||
subpath,
|
||||
NULL,
|
||||
root->group_pathkeys,
|
||||
NULL,
|
||||
&total_groups);
|
||||
|
||||
if (parse->hasAggs)
|
||||
add_path(grouped_rel, (Path *)
|
||||
create_agg_path(root,
|
||||
grouped_rel,
|
||||
gmpath,
|
||||
target,
|
||||
parse->groupClause ? AGG_SORTED : AGG_PLAIN,
|
||||
AGGSPLIT_FINAL_DESERIAL,
|
||||
parse->groupClause,
|
||||
(List *) parse->havingQual,
|
||||
&agg_final_costs,
|
||||
dNumGroups));
|
||||
else
|
||||
add_path(grouped_rel, (Path *)
|
||||
create_group_path(root,
|
||||
grouped_rel,
|
||||
gmpath,
|
||||
target,
|
||||
parse->groupClause,
|
||||
(List *) parse->havingQual,
|
||||
dNumGroups));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -3808,6 +3871,16 @@ create_grouping_paths(PlannerInfo *root,
|
||||
/* Now choose the best path(s) */
|
||||
set_cheapest(grouped_rel);
|
||||
|
||||
/*
|
||||
* We've been using the partial pathlist for the grouped relation to hold
|
||||
* partially aggregated paths, but that's actually a little bit bogus
|
||||
* because it's unsafe for later planning stages -- like ordered_rel ---
|
||||
* to get the idea that they can use these partial paths as if they didn't
|
||||
* need a FinalizeAggregate step. Zap the partial pathlist at this stage
|
||||
* so we don't get confused.
|
||||
*/
|
||||
grouped_rel->partial_pathlist = NIL;
|
||||
|
||||
return grouped_rel;
|
||||
}
|
||||
|
||||
@ -4275,6 +4348,56 @@ create_ordered_paths(PlannerInfo *root,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* generate_gather_paths() will have already generated a simple Gather
|
||||
* path for the best parallel path, if any, and the loop above will have
|
||||
* considered sorting it. Similarly, generate_gather_paths() will also
|
||||
* have generated order-preserving Gather Merge plans which can be used
|
||||
* without sorting if they happen to match the sort_pathkeys, and the loop
|
||||
* above will have handled those as well. However, there's one more
|
||||
* possibility: it may make sense to sort the cheapest partial path
|
||||
* according to the required output order and then use Gather Merge.
|
||||
*/
|
||||
if (ordered_rel->consider_parallel && root->sort_pathkeys != NIL &&
|
||||
input_rel->partial_pathlist != NIL)
|
||||
{
|
||||
Path *cheapest_partial_path;
|
||||
|
||||
cheapest_partial_path = linitial(input_rel->partial_pathlist);
|
||||
|
||||
/*
|
||||
* If cheapest partial path doesn't need a sort, this is redundant
|
||||
* with what's already been tried.
|
||||
*/
|
||||
if (!pathkeys_contained_in(root->sort_pathkeys,
|
||||
cheapest_partial_path->pathkeys))
|
||||
{
|
||||
Path *path;
|
||||
double total_groups;
|
||||
|
||||
path = (Path *) create_sort_path(root,
|
||||
ordered_rel,
|
||||
cheapest_partial_path,
|
||||
root->sort_pathkeys,
|
||||
limit_tuples);
|
||||
|
||||
total_groups = cheapest_partial_path->rows *
|
||||
cheapest_partial_path->parallel_workers;
|
||||
path = (Path *)
|
||||
create_gather_merge_path(root, ordered_rel,
|
||||
path,
|
||||
target, root->sort_pathkeys, NULL,
|
||||
&total_groups);
|
||||
|
||||
/* Add projection step if needed */
|
||||
if (path->pathtarget != target)
|
||||
path = apply_projection_to_path(root, ordered_rel,
|
||||
path, target);
|
||||
|
||||
add_path(ordered_rel, path);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* If there is an FDW that's responsible for all baserels of the query,
|
||||
* let it consider adding ForeignPaths.
|
||||
|
@ -616,6 +616,7 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
|
||||
break;
|
||||
|
||||
case T_Gather:
|
||||
case T_GatherMerge:
|
||||
set_upper_references(root, plan, rtoffset);
|
||||
break;
|
||||
|
||||
|
@ -2700,6 +2700,7 @@ finalize_plan(PlannerInfo *root, Plan *plan, Bitmapset *valid_params,
|
||||
case T_Sort:
|
||||
case T_Unique:
|
||||
case T_Gather:
|
||||
case T_GatherMerge:
|
||||
case T_SetOp:
|
||||
case T_Group:
|
||||
/* no node-type-specific fields need fixing */
|
||||
|
@ -1627,6 +1627,66 @@ create_unique_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
|
||||
return pathnode;
|
||||
}
|
||||
|
||||
/*
|
||||
* create_gather_merge_path
|
||||
*
|
||||
* Creates a path corresponding to a gather merge scan, returning
|
||||
* the pathnode.
|
||||
*/
|
||||
GatherMergePath *
|
||||
create_gather_merge_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath,
|
||||
PathTarget *target, List *pathkeys,
|
||||
Relids required_outer, double *rows)
|
||||
{
|
||||
GatherMergePath *pathnode = makeNode(GatherMergePath);
|
||||
Cost input_startup_cost = 0;
|
||||
Cost input_total_cost = 0;
|
||||
|
||||
Assert(subpath->parallel_safe);
|
||||
Assert(pathkeys);
|
||||
|
||||
pathnode->path.pathtype = T_GatherMerge;
|
||||
pathnode->path.parent = rel;
|
||||
pathnode->path.param_info = get_baserel_parampathinfo(root, rel,
|
||||
required_outer);
|
||||
pathnode->path.parallel_aware = false;
|
||||
|
||||
pathnode->subpath = subpath;
|
||||
pathnode->num_workers = subpath->parallel_workers;
|
||||
pathnode->path.pathkeys = pathkeys;
|
||||
pathnode->path.pathtarget = target ? target : rel->reltarget;
|
||||
pathnode->path.rows += subpath->rows;
|
||||
|
||||
if (pathkeys_contained_in(pathkeys, subpath->pathkeys))
|
||||
{
|
||||
/* Subpath is adequately ordered, we won't need to sort it */
|
||||
input_startup_cost += subpath->startup_cost;
|
||||
input_total_cost += subpath->total_cost;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* We'll need to insert a Sort node, so include cost for that */
|
||||
Path sort_path; /* dummy for result of cost_sort */
|
||||
|
||||
cost_sort(&sort_path,
|
||||
root,
|
||||
pathkeys,
|
||||
subpath->total_cost,
|
||||
subpath->rows,
|
||||
subpath->pathtarget->width,
|
||||
0.0,
|
||||
work_mem,
|
||||
-1);
|
||||
input_startup_cost += sort_path.startup_cost;
|
||||
input_total_cost += sort_path.total_cost;
|
||||
}
|
||||
|
||||
cost_gather_merge(pathnode, root, rel, pathnode->path.param_info,
|
||||
input_startup_cost, input_total_cost, rows);
|
||||
|
||||
return pathnode;
|
||||
}
|
||||
|
||||
/*
|
||||
* translate_sub_tlist - get subquery column numbers represented by tlist
|
||||
*
|
||||
|
Reference in New Issue
Block a user