diff options
author | Robert Haas <rhaas@postgresql.org> | 2016-03-18 09:46:40 -0400 |
---|---|---|
committer | Robert Haas <rhaas@postgresql.org> | 2016-03-18 09:50:05 -0400 |
commit | 992b5ba30dcafdc222341505b072a6b009b248a7 (patch) | |
tree | a05302f0aa1af5273f799221e18db5dbadaf8f7c /src/backend/optimizer/util/pathnode.c | |
parent | 2d8a1e22b109680204cb015a30e5a733a233ed64 (diff) | |
download | postgresql-992b5ba30dcafdc222341505b072a6b009b248a7.tar.gz postgresql-992b5ba30dcafdc222341505b072a6b009b248a7.zip |
Push scan/join target list beneath Gather when possible.
This means that, for example, "SELECT expensive_func(a) FROM bigtab
WHERE something" can compute expensive_func(a) in the workers rather
than the leader if it happens to be parallel-safe, which figures to be
a big win in some practical cases.
Currently, we can only do this if the entire target list is
parallel-safe. If we worked harder, we might be able to evaluate
parallel-safe targets in the worker and any parallel-restricted
targets in the leader, but that would be more complicated, and there
aren't that many parallel-restricted functions that people are likely
to use in queries anyway. I think. So just do the simple thing for
the moment.
Robert Haas, Amit Kapila, and Tom Lane
Diffstat (limited to 'src/backend/optimizer/util/pathnode.c')
-rw-r--r-- | src/backend/optimizer/util/pathnode.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c index b8ea3168a84..541f7790ab0 100644 --- a/src/backend/optimizer/util/pathnode.c +++ b/src/backend/optimizer/util/pathnode.c @@ -2222,6 +2222,36 @@ apply_projection_to_path(PlannerInfo *root, path->total_cost += target->cost.startup - oldcost.startup + (target->cost.per_tuple - oldcost.per_tuple) * path->rows; + /* + * If the path happens to be a Gather path, we'd like to arrange for the + * subpath to return the required target list so that workers can help + * project. But if there is something that is not parallel-safe in the + * target expressions, then we can't. + */ + if (IsA(path, GatherPath) && + !has_parallel_hazard((Node *) target->exprs, false)) + { + GatherPath *gpath = (GatherPath *) path; + + /* + * We always use create_projection_path here, even if the subpath is + * projection-capable, so as to avoid modifying the subpath in place. + * It seems unlikely at present that there could be any other + * references to the subpath anyway, but better safe than sorry. + * (create_projection_plan will only insert a Result node if the + * subpath is not projection-capable, so we only include the cost of + * that node if it will actually be inserted. This is a bit grotty + * but we can improve it later if it seems important.) + */ + if (!is_projection_capable_path(gpath->subpath)) + gpath->path.total_cost += cpu_tuple_cost * gpath->subpath->rows; + gpath->subpath = (Path *) + create_projection_path(root, + gpath->subpath->parent, + gpath->subpath, + target); + } + return path; } |