From b565386600cbd84087dc43b0ea56dc038b2562fe Mon Sep 17 00:00:00 2001
From: csteam <mplageman@pivotal.io>
Date: Tue, 16 Jul 2019 18:12:29 -0700
Subject: [PATCH v1 3/3] Avoid adding returningList for invalid result_relation
 number

When result_relation is 0, this loop will add all Vars from the
returningList to the targetlist, which seems incorrect.
---
 src/backend/optimizer/prep/preptlist.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/optimizer/prep/preptlist.c b/src/backend/optimizer/prep/preptlist.c
index 792ae393d9..24da3bfb6c 100644
--- a/src/backend/optimizer/prep/preptlist.c
+++ b/src/backend/optimizer/prep/preptlist.c
@@ -192,7 +192,7 @@ preprocess_targetlist(PlannerInfo *root)
 	 * belong to the result rel don't need to be added, because they will be
 	 * made to refer to the actual heap tuple.
 	 */
-	if (parse->returningList && list_length(parse->rtable) > 1)
+	if (result_relation && parse->returningList && list_length(parse->rtable) > 1)
 	{
 		List	   *vars;
 		ListCell   *l;
-- 
2.22.0

