@@ -262,7 +262,8 @@ ExecInsert(ModifyTableState *mtstate,
262262 Relation resultRelationDesc ;
263263 Oid newId ;
264264 List * recheckIndexes = NIL ;
265- TupleTableSlot * oldslot = slot ;
265+ TupleTableSlot * oldslot = slot ,
266+ * result = NULL ;
266267
267268 /*
268269 * get the heap tuple out of the tuple table slot, making sure we have a
@@ -574,12 +575,6 @@ ExecInsert(ModifyTableState *mtstate,
574575
575576 list_free (recheckIndexes );
576577
577- if (saved_resultRelInfo )
578- {
579- resultRelInfo = saved_resultRelInfo ;
580- estate -> es_result_relation_info = resultRelInfo ;
581- }
582-
583578 /*
584579 * Check any WITH CHECK OPTION constraints from parent views. We are
585580 * required to do this after testing all constraints and uniqueness
@@ -597,9 +592,12 @@ ExecInsert(ModifyTableState *mtstate,
597592
598593 /* Process RETURNING if present */
599594 if (resultRelInfo -> ri_projectReturning )
600- return ExecProcessReturning (resultRelInfo , slot , planSlot );
595+ result = ExecProcessReturning (resultRelInfo , slot , planSlot );
601596
602- return NULL ;
597+ if (saved_resultRelInfo )
598+ estate -> es_result_relation_info = saved_resultRelInfo ;
599+
600+ return result ;
603601}
604602
605603/* ----------------------------------------------------------------
@@ -1786,6 +1784,7 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
17861784 {
17871785 TupleTableSlot * slot ;
17881786 ExprContext * econtext ;
1787+ List * returningList ;
17891788
17901789 /*
17911790 * Initialize result tuple slot and assign its rowtype using the first
@@ -1818,6 +1817,32 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
18181817 resultRelInfo -> ri_RelationDesc -> rd_att );
18191818 resultRelInfo ++ ;
18201819 }
1820+
1821+ /*
1822+ * Build a projection for each leaf partition rel. Note that we
1823+ * didn't build the returningList for each partition within the
1824+ * planner, but simple translation of the varattnos for each
1825+ * partition will suffice. This only occurs for the INSERT case;
1826+ * UPDATE/DELETE are handled above.
1827+ */
1828+ resultRelInfo = mtstate -> mt_partitions ;
1829+ returningList = linitial (node -> returningLists );
1830+ for (i = 0 ; i < mtstate -> mt_num_partitions ; i ++ )
1831+ {
1832+ Relation partrel = resultRelInfo -> ri_RelationDesc ;
1833+ List * rlist ,
1834+ * rliststate ;
1835+
1836+ /* varno = node->nominalRelation */
1837+ rlist = map_partition_varattnos (returningList ,
1838+ node -> nominalRelation ,
1839+ partrel , rel );
1840+ rliststate = (List * ) ExecInitExpr ((Expr * ) rlist , & mtstate -> ps );
1841+ resultRelInfo -> ri_projectReturning =
1842+ ExecBuildProjectionInfo (rliststate , econtext , slot ,
1843+ resultRelInfo -> ri_RelationDesc -> rd_att );
1844+ resultRelInfo ++ ;
1845+ }
18211846 }
18221847 else
18231848 {
0 commit comments