From fe071949a7e85fde8805acc0ccdeb1d64de7d63a Mon Sep 17 00:00:00 2001
From: Hari Babu <kommi.haribabu@gmail.com>
Date: Thu, 29 Mar 2018 16:17:54 +1100
Subject: [PATCH 14/16] 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 | 22 ++++++----------------
 src/include/commands/trigger.h         |  2 +-
 src/pl/tcl/pltcl.c                     |  2 +-
 5 files changed, 11 insertions(+), 24 deletions(-)

diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c
index 75deabe0fe..5c6c7067fe 100644
--- a/src/backend/commands/trigger.c
+++ b/src/backend/commands/trigger.c
@@ -3073,7 +3073,7 @@ void
 ExecARUpdateTriggers(EState *estate, ResultRelInfo *relinfo,
 					 ItemPointer tupleid,
 					 HeapTuple fdw_trigtuple,
-					 HeapTuple newtuple,
+					 TupleTableSlot *slot,
 					 List *recheckIndexes,
 					 TransitionCaptureState *transition_capture)
 {
@@ -3085,7 +3085,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 3297fd7392..e83ab88c81 100644
--- a/src/backend/executor/execReplication.c
+++ b/src/backend/executor/execReplication.c
@@ -405,7 +405,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);
@@ -443,12 +442,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 9560a2fa51..baa08a91b6 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -272,7 +272,6 @@ ExecInsert(ModifyTableState *mtstate,
 		   EState *estate,
 		   bool canSetTag)
 {
-	TableTuple tuple;
 	ResultRelInfo *resultRelInfo;
 	Relation	resultRelationDesc;
 	Oid			newId;
@@ -541,10 +540,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);
 
@@ -930,7 +928,6 @@ ExecUpdate(ModifyTableState *mtstate,
 		   EState *estate,
 		   bool canSetTag)
 {
-	TableTuple tuple;
 	ResultRelInfo *resultRelInfo;
 	Relation	resultRelationDesc;
 	HTSU_Result result;
@@ -948,7 +945,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
@@ -965,9 +962,6 @@ ExecUpdate(ModifyTableState *mtstate,
 
 		if (slot == NULL)		/* "do nothing" */
 			return NULL;
-
-		/* trigger might have changed tuple */
-		tuple = ExecHeapifySlot(slot);
 	}
 
 	/* INSTEAD OF ROW UPDATE Triggers */
@@ -979,9 +973,6 @@ ExecUpdate(ModifyTableState *mtstate,
 
 		if (slot == NULL)		/* "do nothing" */
 			return NULL;
-
-		/* trigger might have changed tuple */
-		tuple = ExecHeapifySlot(slot);
 	}
 	else if (resultRelInfo->ri_FdwRoutine)
 	{
@@ -1001,9 +992,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
 	{
@@ -1060,6 +1048,7 @@ lreplace:;
 			PartitionTupleRouting *proute = mtstate->mt_partition_tuple_routing;
 			int			map_index;
 			TupleConversionMap *tupconv_map;
+			HeapTuple	tuple = ExecHeapifySlot(slot);
 
 			/*
 			 * Disallow an INSERT ON CONFLICT DO UPDATE that causes the
@@ -1189,6 +1178,8 @@ lreplace:;
 
 		if (result == HeapTupleUpdated && !IsolationUsesXactSnapshot())
 		{
+			TableTuple tuple;
+
 			result = table_lock_tuple(resultRelationDesc, tupleid,
 										estate->es_snapshot,
 										&tuple, estate->es_output_cid,
@@ -1212,7 +1203,6 @@ lreplace:;
 					return NULL;
 				}
 				slot = ExecFilterJunk(resultRelInfo->ri_junkFilter, epqslot);
-				tuple = ExecHeapifySlot(slot);
 				goto lreplace;
 			}
 		}
@@ -1284,7 +1274,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 2fe7ed33a5..7cac03d469 100644
--- a/src/include/commands/trigger.h
+++ b/src/include/commands/trigger.h
@@ -230,7 +230,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 12f7b13780..55319e3da8 100644
--- a/src/pl/tcl/pltcl.c
+++ b/src/pl/tcl/pltcl.c
@@ -2432,7 +2432,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

