summaryrefslogtreecommitdiff
path: root/src/backend/optimizer/plan
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/optimizer/plan')
-rw-r--r--src/backend/optimizer/plan/createplan.c8
-rw-r--r--src/backend/optimizer/plan/planner.c17
2 files changed, 16 insertions, 9 deletions
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index d668128ccb..65a2e38fc3 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -937,10 +937,12 @@ create_unique_plan(PlannerInfo *root, UniquePath *best_path)
if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
{
/*
- * If the top plan node can't do projections, we need to add a Result
- * node to help it along.
+ * If the top plan node can't do projections and its existing target
+ * list isn't already what we need, we need to add a Result node to
+ * help it along.
*/
- if (!is_projection_capable_plan(subplan))
+ if (!is_projection_capable_plan(subplan) &&
+ !tlist_same_exprs(newtlist, subplan->targetlist))
subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
else
subplan->targetlist = newtlist;
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 0847e787c3..670776b032 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -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))
{