diff options
Diffstat (limited to 'src/backend/optimizer/plan')
-rw-r--r-- | src/backend/optimizer/plan/createplan.c | 35 | ||||
-rw-r--r-- | src/backend/optimizer/plan/planagg.c | 3 | ||||
-rw-r--r-- | src/backend/optimizer/plan/planner.c | 54 |
3 files changed, 32 insertions, 60 deletions
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index ab8df76a6ed..b2db6e8d035 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -1409,8 +1409,9 @@ create_gather_plan(PlannerInfo *root, GatherPath *best_path) /* * create_projection_plan * - * Create a Result node to do a projection step and (recursively) plans - * for its subpaths. + * Create a plan tree to do a projection step and (recursively) plans + * for its subpaths. We may need a Result node for the projection, + * but sometimes we can just let the subplan do the work. */ static Plan * create_projection_plan(PlannerInfo *root, ProjectionPath *best_path) @@ -1425,31 +1426,37 @@ create_projection_plan(PlannerInfo *root, ProjectionPath *best_path) tlist = build_path_tlist(root, &best_path->path); /* - * We might not really need a Result node here. There are several ways - * that this can happen. For example, MergeAppend doesn't project, so we - * would have thought that we needed a projection to attach resjunk sort - * columns to its output ... but create_merge_append_plan might have added - * those same resjunk sort columns to both MergeAppend and its children. - * Alternatively, apply_projection_to_path might have created a projection - * path as the subpath of a Gather node even though the subpath was - * projection-capable. So, if the subpath is capable of projection or the - * desired tlist is the same expression-wise as the subplan's, just jam it - * in there. We'll have charged for a Result that doesn't actually appear - * in the plan, but that's better than having a Result we don't need. + * We might not really need a Result node here, either because the subplan + * can project or because it's returning the right list of expressions + * anyway. Usually create_projection_path will have detected that and set + * dummypp if we don't need a Result; but its decision can't be final, + * because some createplan.c routines change the tlists of their nodes. + * (An example is that create_merge_append_plan might add resjunk sort + * columns to a MergeAppend.) So we have to recheck here. If we do + * arrive at a different answer than create_projection_path did, we'll + * have made slightly wrong cost estimates; but label the plan with the + * cost estimates we actually used, not "corrected" ones. (XXX this could + * be cleaned up if we moved more of the sortcolumn setup logic into Path + * creation, but that would add expense to creating Paths we might end up + * not using.) */ if (is_projection_capable_path(best_path->subpath) || tlist_same_exprs(tlist, subplan->targetlist)) { + /* Don't need a separate Result, just assign tlist to subplan */ plan = subplan; plan->targetlist = tlist; - /* Adjust cost to match what we thought during planning */ + /* Label plan with the estimated costs we actually used */ plan->startup_cost = best_path->path.startup_cost; plan->total_cost = best_path->path.total_cost; + plan->plan_rows = best_path->path.rows; + plan->plan_width = best_path->path.pathtarget->width; /* ... but be careful not to munge subplan's parallel-aware flag */ } else { + /* We need a Result node */ plan = (Plan *) make_result(tlist, NULL, subplan); copy_generic_path_info(plan, (Path *) best_path); diff --git a/src/backend/optimizer/plan/planagg.c b/src/backend/optimizer/plan/planagg.c index 0434a5a8e73..cefec7bdf10 100644 --- a/src/backend/optimizer/plan/planagg.c +++ b/src/backend/optimizer/plan/planagg.c @@ -465,8 +465,7 @@ build_minmax_path(PlannerInfo *root, MinMaxAggInfo *mminfo, * cheapest path.) */ sorted_path = apply_projection_to_path(subroot, final_rel, sorted_path, - create_pathtarget(subroot, tlist), - false); + create_pathtarget(subroot, tlist)); /* * Determine cost to get just the first row of the presorted path. diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 094c7082b14..2372311d409 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1500,7 +1500,6 @@ grouping_planner(PlannerInfo *root, bool inheritance_update, PathTarget *grouping_target; PathTarget *scanjoin_target; bool have_grouping; - bool scanjoin_target_parallel_safe = false; WindowFuncLists *wflists = NULL; List *activeWindows = NIL; List *rollup_lists = NIL; @@ -1731,14 +1730,6 @@ grouping_planner(PlannerInfo *root, bool inheritance_update, scanjoin_target = grouping_target; /* - * Check whether scan/join target is parallel safe ... unless there - * are no partial paths, in which case we don't care. - */ - if (current_rel->partial_pathlist && - !has_parallel_hazard((Node *) scanjoin_target->exprs, false)) - scanjoin_target_parallel_safe = true; - - /* * Forcibly apply scan/join target to all the Paths for the scan/join * rel. * @@ -1756,8 +1747,7 @@ grouping_planner(PlannerInfo *root, bool inheritance_update, Assert(subpath->param_info == NULL); path = apply_projection_to_path(root, current_rel, - subpath, scanjoin_target, - scanjoin_target_parallel_safe); + subpath, scanjoin_target); /* If we had to add a Result, path is different from subpath */ if (path != subpath) { @@ -1774,15 +1764,13 @@ grouping_planner(PlannerInfo *root, bool inheritance_update, * partial pathlist will expect partial paths for that rel to produce * the same output as complete paths ... and we just changed the * output for the complete paths, so we'll need to do the same thing - * for partial paths. + * for partial paths. But only parallel-safe expressions can be + * computed by partial paths. */ - if (scanjoin_target_parallel_safe) + if (current_rel->partial_pathlist && + !has_parallel_hazard((Node *) scanjoin_target->exprs, false)) { - /* - * Apply the scan/join target to each partial path. Otherwise, - * anything that attempts to use the partial paths for further - * upper planning may go wrong. - */ + /* Apply the scan/join target to each partial path */ foreach(lc, current_rel->partial_pathlist) { Path *subpath = (Path *) lfirst(lc); @@ -1792,36 +1780,14 @@ grouping_planner(PlannerInfo *root, bool inheritance_update, Assert(subpath->param_info == NULL); /* - * We can't use apply_projection_to_path() here, because there - * could already be pointers to these paths, and therefore we - * dare not modify them in place. Instead, we must use - * create_projection_path() unconditionally. + * Don't use apply_projection_to_path() here, because there + * could be other pointers to these paths, and therefore we + * mustn't modify them in place. */ newpath = (Path *) create_projection_path(root, current_rel, subpath, scanjoin_target); - - /* - * Although create_projection_path() inserts a ProjectionPath - * unconditionally, create_projection_plan() will only insert - * a Result node if the subpath is not projection-capable, so - * we should discount the cost of that node if it will not - * actually get inserted. (This is pretty grotty but we can - * improve it later if it seems important.) - */ - if (equal(scanjoin_target->exprs, subpath->pathtarget->exprs)) - { - /* at most we need a relabeling of the subpath */ - newpath->startup_cost = subpath->startup_cost; - newpath->total_cost = subpath->total_cost; - } - else if (is_projection_capable_path(subpath)) - { - /* need to project, but we don't need a Result */ - newpath->total_cost -= cpu_tuple_cost * subpath->rows; - } - lfirst(lc) = newpath; } } @@ -4231,7 +4197,7 @@ create_ordered_paths(PlannerInfo *root, /* Add projection step if needed */ if (path->pathtarget != target) path = apply_projection_to_path(root, ordered_rel, - path, target, false); + path, target); add_path(ordered_rel, path); } |