1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-14 08:21:07 +03:00

Avoid inserting Result nodes that only compute identity projections.

The planner sometimes inserts Result nodes to perform column projections
(ie, arbitrary scalar calculations) above plan nodes that lack projection
logic of their own.  However, we did that even if the lower plan node was
in fact producing the required column set already; which is a pretty common
case given the popularity of "SELECT * FROM ...".  Measurements show that
the useless plan node adds non-negligible overhead, especially when there
are many columns in the result.  So add a check to avoid inserting a Result
node unless there's something useful for it to do.

There are a couple of remaining places where unnecessary Result nodes
could get inserted, but they are (a) much less performance-critical,
and (b) coded in such a way that it's hard to avoid inserting a Result,
because the desired tlist is changed on-the-fly in subsequent logic.
We'll leave those alone for now.

Kyotaro Horiguchi; reviewed and further hacked on by Amit Kapila and
Tom Lane.
This commit is contained in:
Tom Lane
2013-03-14 13:42:51 -04:00
parent a5ff502fce
commit 4387cf956b
7 changed files with 163 additions and 128 deletions

View File

@ -1379,10 +1379,12 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
{
/*
* If the top-level plan node is one that cannot do expression
* evaluation, we must insert a Result node to project the
* evaluation and its existing target list isn't already what
* we need, we must insert a Result node to project the
* desired tlist.
*/
if (!is_projection_capable_plan(result_plan))
if (!is_projection_capable_plan(result_plan) &&
!tlist_same_exprs(sub_tlist, result_plan->targetlist))
{
result_plan = (Plan *) make_result(root,
sub_tlist,
@ -1542,10 +1544,13 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)
* If the top-level plan node is one that cannot do expression
* evaluation, we must insert a Result node to project the desired
* tlist. (In some cases this might not really be required, but
* it's not worth trying to avoid it.) Note that on second and
* subsequent passes through the following loop, the top-level
* node will be a WindowAgg which we know can project; so we only
* need to check once.
* it's not worth trying to avoid it. In particular, think not to
* skip adding the Result if the initial window_tlist matches the
* top-level plan node's output, because we might change the tlist
* inside the following loop.) Note that on second and subsequent
* passes through the following loop, the top-level node will be a
* WindowAgg which we know can project; so we only need to check
* once.
*/
if (!is_projection_capable_plan(result_plan))
{