diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index c32928d9bd..0d6326d1de 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -281,7 +281,7 @@ ExecInsert(ModifyTableState *mtstate, /* * get information on the (current) result relation */ - resultRelInfo = estate->es_result_relation_info; + saved_resultRelInfo = resultRelInfo = estate->es_result_relation_info; /* Determine the partition to heap_insert the tuple into */ if (mtstate->mt_partition_tuple_routing) @@ -408,7 +408,10 @@ ExecInsert(ModifyTableState *mtstate, slot = ExecBRInsertTriggers(estate, resultRelInfo, slot); if (slot == NULL) /* "do nothing" */ - return NULL; + { + result = NULL; + goto restore_estate_result_rel; + } /* trigger might have changed tuple */ tuple = ExecMaterializeSlot(slot); @@ -421,7 +424,10 @@ ExecInsert(ModifyTableState *mtstate, slot = ExecIRInsertTriggers(estate, resultRelInfo, slot); if (slot == NULL) /* "do nothing" */ - return NULL; + { + result = NULL; + goto restore_estate_result_rel; + } /* trigger might have changed tuple */ tuple = ExecMaterializeSlot(slot); @@ -439,7 +445,10 @@ ExecInsert(ModifyTableState *mtstate, planSlot); if (slot == NULL) /* "do nothing" */ - return NULL; + { + result = NULL; + goto restore_estate_result_rel; + } /* FDW might have changed tuple */ tuple = ExecMaterializeSlot(slot); @@ -495,7 +504,7 @@ ExecInsert(ModifyTableState *mtstate, * No need though if the tuple has been routed, and a BR trigger * doesn't exist. */ - if (saved_resultRelInfo != NULL && + if (saved_resultRelInfo != resultRelInfo && !(resultRelInfo->ri_TrigDesc && resultRelInfo->ri_TrigDesc->trig_insert_before_row)) check_partition_constr = false; @@ -544,7 +553,8 @@ ExecInsert(ModifyTableState *mtstate, estate, canSetTag, &returning)) { InstrCountFiltered2(&mtstate->ps, 1); - return returning; + result = returning; + goto restore_estate_result_rel; } else goto vlock; @@ -559,7 +569,8 @@ ExecInsert(ModifyTableState *mtstate, Assert(onconflict == ONCONFLICT_NOTHING); ExecCheckTIDVisible(estate, resultRelInfo, &conflictTid); InstrCountFiltered2(&mtstate->ps, 1); - return NULL; + result = NULL; + goto restore_estate_result_rel; } } @@ -686,6 +697,7 @@ ExecInsert(ModifyTableState *mtstate, if (resultRelInfo->ri_projectReturning) result = ExecProcessReturning(resultRelInfo, slot, planSlot); +restore_estate_result_rel: if (saved_resultRelInfo) estate->es_result_relation_info = saved_resultRelInfo;