Thank you both for the review.

I updated the comment in ExecSetupPartitionTupleRouting considering the
point both of you raised.

About renaming es_leaf_result_relations to
es_tuple_routing_result_relations, I will defer that to committer.  But on
second though, maybe we don't need to make this patch larger than it has
to be.

Thanks,
Amit
From d001de96ab452e48b011db7f34840a3e6b8999c9 Mon Sep 17 00:00:00 2001
From: amit <amitlangot...@gmail.com>
Date: Tue, 6 Feb 2018 10:40:45 +0900
Subject: [PATCH v3] Fix trigger behavior with update tuple routing

Trigger stats shown by EXPLAIN ANALYZE may contain duplicate
entries, because leaf partitions' ResultRelInfo is redundantly
present in multiple EState lists.

Reported by: Etsuro Fujita
---
 src/backend/executor/execPartition.c | 10 +++++++---
 src/include/nodes/execnodes.h        |  5 ++++-
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/backend/executor/execPartition.c 
b/src/backend/executor/execPartition.c
index 106a96d910..8094dbc614 100644
--- a/src/backend/executor/execPartition.c
+++ b/src/backend/executor/execPartition.c
@@ -178,6 +178,13 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate,
                                                          resultRTindex,
                                                          rel,
                                                          
estate->es_instrument);
+
+                       /*
+                        * Since we're newly creating this ResultRelInfo, add 
it to
+                        * someplace where others could find it.
+                        */
+                       estate->es_leaf_result_relations =
+                               lappend(estate->es_leaf_result_relations, 
leaf_part_rri);
                }
 
                part_tupdesc = RelationGetDescr(partrel);
@@ -210,9 +217,6 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate,
                                                        mtstate != NULL &&
                                                        mtstate->mt_onconflict 
!= ONCONFLICT_NONE);
 
-               estate->es_leaf_result_relations =
-                       lappend(estate->es_leaf_result_relations, 
leaf_part_rri);
-
                proute->partitions[i] = leaf_part_rri;
                i++;
        }
diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h
index a2a2a9f3d4..1915b53b2f 100644
--- a/src/include/nodes/execnodes.h
+++ b/src/include/nodes/execnodes.h
@@ -466,7 +466,10 @@ typedef struct EState
        ResultRelInfo *es_root_result_relations;        /* array of 
ResultRelInfos */
        int                     es_num_root_result_relations;   /* length of 
the array */
 
-       /* Info about leaf partitions of partitioned table(s) for insert 
queries: */
+       /*
+        * The following list contains ResultRelInfo's created by the tuple
+        * routing code for partitions that don't already have one.
+        */
        List       *es_leaf_result_relations;   /* List of ResultRelInfos */
 
        /* Stuff used for firing triggers: */
-- 
2.11.0

Reply via email to