From 5a2ca9baf3852eb422e016e6deae9c166664bf7b Mon Sep 17 00:00:00 2001
From: Hari Babu <kommi.haribabu@gmail.com>
Date: Wed, 14 Feb 2018 16:21:10 +1100
Subject: [PATCH 14/14] ExecARUpdateTriggers is updated to accept slot instead
 of tuple

The After record update trigger function is changed to accept
slot instead of newtuple, thus is reduces the need of TableTuple
variable in the callers.
---
 src/backend/commands/trigger.c         |  4 ++--
 src/backend/executor/execReplication.c |  5 +----
 src/backend/executor/nodeModifyTable.c | 21 ++++++---------------
 src/include/commands/trigger.h         |  2 +-
 src/pl/tcl/pltcl.c                     |  2 +-
 5 files changed, 11 insertions(+), 23 deletions(-)

diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index 2322040919..3df9ce12b9 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -2846,7 +2846,7 @@ void
 ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
 					 ItemPointer tupleid,
 					 HeapTuple fdw_trigtuple,
-					 HeapTuple newtuple,
+					 TupleTableSlot *slot,
 					 List *recheckIndexes,
 					 TransitionCaptureState *transition_capture)
 {
@@ -2858,7 +2858,7 @@ ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
 		  transition_capture->tcs_update_new_table)))
 	{
 		HeapTuple	trigtuple;
-
+		HeapTuple	newtuple = slot ? ExecHeapifySlot(slot) : NULL;
 		/*
 		 * Note: if the UPDATE is converted into a DELETE+INSERT as part of
 		 * update-partition-key operation, then this function is also called
diff --git a/src/backend/executor/execReplication.c b/src/backend/executor/execReplication.c
index 3ab6db5902..48661eb2d6 100644
--- a/src/backend/executor/execReplication.c
+++ b/src/backend/executor/execReplication.c
@@ -391,7 +391,6 @@ ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate,
 						 TupleTableSlot *searchslot, TupleTableSlot *slot)
 {
 	bool		skip_tuple = false;
-	TableTuple tuple;
 	ResultRelInfo *resultRelInfo = estate->es_result_relation_info;
 	Relation	rel = resultRelInfo->ri_RelationDesc;
 	ItemPointer tid = &(searchslot->tts_tid);
@@ -427,12 +426,10 @@ ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate,
 		table_update(rel, tid, slot, estate, GetCurrentCommandId(true), InvalidSnapshot,
 					   true, &hufd, &lockmode, IndexFunc, &recheckIndexes);
 
-		tuple = ExecHeapifySlot(slot);
-
 		/* AFTER ROW UPDATE Triggers */
 		ExecARUpdateTriggers(estate, resultRelInfo,
 							 tid,
-							 NULL, tuple, recheckIndexes, NULL);
+							 NULL, slot, recheckIndexes, NULL);
 
 		list_free(recheckIndexes);
 	}
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c
index b6b4b153b0..a0d050d19e 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -628,10 +628,9 @@ ExecInsert(ModifyTableState *mtstate,
 	if (mtstate->operation == CMD_UPDATE && mtstate->mt_transition_capture
 		&& mtstate->mt_transition_capture->tcs_update_new_table)
 	{
-		tuple = ExecHeapifySlot(slot);
 		ExecARUpdateTriggers(estate, resultRelInfo, NULL,
 							 NULL,
-							 tuple,
+							 slot,
 							 NULL,
 							 mtstate->mt_transition_capture);
 
@@ -1018,7 +1017,6 @@ ExecUpdate(ModifyTableState *mtstate,
 		   EState *estate,
 		   bool canSetTag)
 {
-	TableTuple tuple;
 	ResultRelInfo *resultRelInfo;
 	Relation	resultRelationDesc;
 	HTSU_Result result;
@@ -1036,7 +1034,7 @@ ExecUpdate(ModifyTableState *mtstate,
 	 * get the heap tuple out of the tuple table slot, making sure we have a
 	 * writable copy
 	 */
-	tuple = ExecHeapifySlot(slot);
+	ExecMaterializeSlot(slot);
 
 	/*
 	 * get information on the (current) result relation
@@ -1053,9 +1051,6 @@ ExecUpdate(ModifyTableState *mtstate,
 
 		if (slot == NULL)		/* "do nothing" */
 			return NULL;
-
-		/* trigger might have changed tuple */
-		tuple = ExecHeapifySlot(slot);
 	}
 
 	/* INSTEAD OF ROW UPDATE Triggers */
@@ -1067,9 +1062,6 @@ ExecUpdate(ModifyTableState *mtstate,
 
 		if (slot == NULL)		/* "do nothing" */
 			return NULL;
-
-		/* trigger might have changed tuple */
-		tuple = ExecHeapifySlot(slot);
 	}
 	else if (resultRelInfo->ri_FdwRoutine)
 	{
@@ -1089,9 +1081,6 @@ ExecUpdate(ModifyTableState *mtstate,
 		 * tableoid column, so initialize t_tableOid before evaluating them.
 		 */
 		ExecSlotUpdateTupleTableoid(slot, RelationGetRelid(resultRelationDesc));
-
-		/* FDW might have changed tuple */
-		tuple = ExecHeapifySlot(slot);
 	}
 	else
 	{
@@ -1148,6 +1137,7 @@ lreplace:;
 			PartitionTupleRouting *proute = mtstate->mt_partition_tuple_routing;
 			int			map_index;
 			TupleConversionMap *tupconv_map;
+			HeapTuple	tuple = ExecHeapifySlot(slot);
 
 			/*
 			 * When an UPDATE is run on a leaf partition, we will not have
@@ -1269,6 +1259,8 @@ lreplace:;
 
 		if (result == HeapTupleUpdated && !IsolationUsesXactSnapshot())
 		{
+			TableTuple tuple;
+
 			result = table_lock_tuple(resultRelationDesc, tupleid,
 										estate->es_snapshot,
 										&tuple, estate->es_output_cid,
@@ -1292,7 +1284,6 @@ lreplace:;
 					return NULL;
 				}
 				slot = ExecFilterJunk(resultRelInfo->ri_junkFilter, epqslot);
-				tuple = ExecHeapifySlot(slot);
 				goto lreplace;
 			}
 		}
@@ -1364,7 +1355,7 @@ lreplace:;
 		(estate->es_processed)++;
 
 	/* AFTER ROW UPDATE Triggers */
-	ExecARUpdateTriggers(estate, resultRelInfo, tupleid, oldtuple, tuple,
+	ExecARUpdateTriggers(estate, resultRelInfo, tupleid, oldtuple, slot,
 						 recheckIndexes,
 						 mtstate->operation == CMD_INSERT ?
 						 mtstate->mt_oc_transition_capture :
diff --git a/src/include/commands/trigger.h b/src/include/commands/trigger.h
index 093d1ae112..fe67d72084 100644
--- a/src/include/commands/trigger.h
+++ b/src/include/commands/trigger.h
@@ -229,7 +229,7 @@ extern void ExecARUpdateTriggers(EState *estate,
 					 ResultRelInfo *relinfo,
 					 ItemPointer tupleid,
 					 HeapTuple fdw_trigtuple,
-					 HeapTuple newtuple,
+					 TupleTableSlot *slot,
 					 List *recheckIndexes,
 					 TransitionCaptureState *transition_capture);
 extern TupleTableSlot *ExecIRUpdateTriggers(EState *estate,
diff --git a/src/pl/tcl/pltcl.c b/src/pl/tcl/pltcl.c
index 5df4dfdf55..ba703e20f0 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -2438,7 +2438,7 @@ pltcl_process_SPI_result(Tcl_Interp *interp,
 {
 	int			my_rc = TCL_OK;
 	int			loop_rc;
-	HeapTuple  *tuples;
+	TableTuple *tuples;
 	TupleDesc	tupdesc;
 
 	switch (spi_rc)
-- 
2.16.1.windows.4

