From f933948b41d89c0b9b35a535bf8c23a9c0eef6f9 Mon Sep 17 00:00:00 2001
From: amitlan <amitlangote09@gmail.com>
Date: Thu, 18 Jun 2020 13:12:21 +0900
Subject: [PATCH v2 2/3] Do not set rootResultRelIndex unnecessarily

It's set in ModifyTable for all INSERT, UPDATE, and DELETE, however
only needed for the latter two operations.
---
 src/backend/optimizer/plan/setrefs.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/backend/optimizer/plan/setrefs.c b/src/backend/optimizer/plan/setrefs.c
index 3eb0fc1..5001966 100644
--- a/src/backend/optimizer/plan/setrefs.c
+++ b/src/backend/optimizer/plan/setrefs.c
@@ -935,8 +935,11 @@ set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
 				 * If the main target relation is a partitioned table, also
 				 * add the partition root's RT index to rootResultRelations,
 				 * and remember its index in that list in rootResultRelIndex.
+				 * We do this only for UPDATE/DELETE though, because in only
+				 * in their case do we need to process the root relation
+				 * separately from other result relations.
 				 */
-				if (splan->rootRelation)
+				if (splan->rootRelation && splan->operation != CMD_INSERT)
 				{
 					splan->rootResultRelIndex =
 						list_length(root->glob->rootResultRelations);
-- 
1.8.3.1

