From e44dd84325c277fd031b9ef486c51a0946c7d3a0 Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Thu, 8 Feb 2018 14:29:05 -0500 Subject: [PATCH] Avoid listing the same ResultRelInfo in more than one EState list. Doing so causes EXPLAIN ANALYZE to show trigger statistics multiple times. Commit 2f178441044be430f6b4d626e4dae68a9a6f6cec seems to be to blame for this. Amit Langote, revieed by Amit Khandekar, Etsuro Fujita, and me. --- src/backend/commands/explain.c | 11 +++++++---- src/backend/executor/execMain.c | 7 +++++-- src/backend/executor/execPartition.c | 12 +++++++++--- src/backend/executor/execUtils.c | 2 +- src/include/nodes/execnodes.h | 7 +++++-- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c index 41cd47e8bc..900fa74e85 100644 --- a/src/backend/commands/explain.c +++ b/src/backend/commands/explain.c @@ -652,15 +652,18 @@ ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc) bool show_relname; int numrels = queryDesc->estate->es_num_result_relations; int numrootrels = queryDesc->estate->es_num_root_result_relations; - List *leafrels = queryDesc->estate->es_leaf_result_relations; - List *targrels = queryDesc->estate->es_trig_target_relations; + List *routerels; + List *targrels; int nr; ListCell *l; + routerels = queryDesc->estate->es_tuple_routing_result_relations; + targrels = queryDesc->estate->es_trig_target_relations; + ExplainOpenGroup("Triggers", "Triggers", false, es); show_relname = (numrels > 1 || numrootrels > 0 || - leafrels != NIL || targrels != NIL); + routerels != NIL || targrels != NIL); rInfo = queryDesc->estate->es_result_relations; for (nr = 0; nr < numrels; rInfo++, nr++) report_triggers(rInfo, show_relname, es); @@ -669,7 +672,7 @@ ExplainPrintTriggers(ExplainState *es, QueryDesc *queryDesc) for (nr = 0; nr < numrootrels; rInfo++, nr++) report_triggers(rInfo, show_relname, es); - foreach(l, leafrels) + foreach(l, routerels) { rInfo = (ResultRelInfo *) lfirst(l); report_triggers(rInfo, show_relname, es); diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index 410921cc40..5d3e923cca 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -1413,8 +1413,11 @@ ExecGetTriggerResultRel(EState *estate, Oid relid) rInfo++; nr--; } - /* Third, search through the leaf result relations, if any */ - foreach(l, estate->es_leaf_result_relations) + /* + * Third, search through the result relations that were created during + * tuple routing, if any. + */ + foreach(l, estate->es_tuple_routing_result_relations) { rInfo = (ResultRelInfo *) lfirst(l); if (RelationGetRelid(rInfo->ri_RelationDesc) == relid) diff --git a/src/backend/executor/execPartition.c b/src/backend/executor/execPartition.c index ba6b52c32c..4048c3ebc6 100644 --- a/src/backend/executor/execPartition.c +++ b/src/backend/executor/execPartition.c @@ -178,6 +178,15 @@ ExecSetupPartitionTupleRouting(ModifyTableState *mtstate, resultRTindex, rel, estate->es_instrument); + + /* + * Since we've just initialized this ResultRelInfo, it's not in + * any list attached to the estate as yet. Add it, so that it can + * be found later. + */ + estate->es_tuple_routing_result_relations = + lappend(estate->es_tuple_routing_result_relations, + leaf_part_rri); } part_tupdesc = RelationGetDescr(partrel); @@ -210,9 +219,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/backend/executor/execUtils.c b/src/backend/executor/execUtils.c index e29f7aaf7b..50b6edce63 100644 --- a/src/backend/executor/execUtils.c +++ b/src/backend/executor/execUtils.c @@ -119,7 +119,7 @@ CreateExecutorState(void) estate->es_root_result_relations = NULL; estate->es_num_root_result_relations = 0; - estate->es_leaf_result_relations = NIL; + estate->es_tuple_routing_result_relations = NIL; estate->es_trig_target_relations = NIL; estate->es_trig_tuple_slot = NULL; diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 54ce63f147..286d55be03 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -466,8 +466,11 @@ 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: */ - List *es_leaf_result_relations; /* List of ResultRelInfos */ + /* + * The following list contains ResultRelInfos created by the tuple + * routing code for partitions that don't already have one. + */ + List *es_tuple_routing_result_relations; /* Stuff used for firing triggers: */ List *es_trig_target_relations; /* trigger-only ResultRelInfos */ -- 2.39.5