Steve Singer ssinger at ca.afilias.info
Fri Apr 14 17:28:58 PDT 2017
Add in support where the logApply trigger inserst data into a table that has  trigger
,which runs on the replica,that then inserts data into another replicated table.

The logApply trigger might have already been called in this transaction but the
logTrigger is then called in the same transaction.  We then need to setup
the plans for inserting data into sl_log_1 or sl_log_2
---
 src/backend/slony1_funcs.c | 58 +++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 49 insertions(+), 9 deletions(-)

diff --git a/src/backend/slony1_funcs.c b/src/backend/slony1_funcs.c
index 657589e..f4db2d8 100644
--- a/src/backend/slony1_funcs.c
+++ b/src/backend/slony1_funcs.c
@@ -151,7 +151,9 @@ typedef struct slony_I_cluster_status
 	text	   *cmdtype_I;
 	text	   *cmdtype_U;
 	text	   *cmdtype_D;
-	bool	   event_txn;
+	bool		event_txn;
+	bool		apply_init;
+	bool		log_init;
 	
 	struct slony_I_cluster_status *next;
 }	Slony_I_ClusterStatus;
@@ -255,10 +257,19 @@ versionFunc(createEvent) (PG_FUNCTION_ARGS)
 	/*
 	 * Do the following only once per transaction.
 	 */
-	if (!TransactionIdEquals(cs->currentXid, newXid))
+	if (!TransactionIdEquals(cs->currentXid, newXid)  )
 	{
 		cs->currentXid = newXid;
 		cs->event_txn = true;
+		cs->apply_init = false;
+		cs->log_init = false;
+	}
+	else if (!cs->log_init)
+	{
+		/**
+		 * This transaction has been setup for apply but not logging.
+		 */
+		cs->event_txn = true;
 	}
 
 	if(!cs->event_txn)
@@ -436,10 +447,28 @@ versionFunc(logTrigger) (PG_FUNCTION_ARGS)
 	 */
 	cs = getClusterStatus(cluster_name, PLAN_INSERT_LOG_STATUS);
 
+	bool initRequired = false;
+	if(!TransactionIdEquals(cs->currentXid, newXid))
+	{
+		initRequired = true;
+		cs->apply_init = false;
+		cs->event_txn=false;
+		cs->log_init = true;
+	}
+	else if (!cs->log_init)
+	{
+		initRequired = true;
+	}
+
+	if(cs->event_txn)
+	{
+		elog(ERROR,"Slony-I: log trigger called in an event transaction");
+	}
+	
 	/*
 	 * Do the following only once per transaction.
 	 */
-	if (!TransactionIdEquals(cs->currentXid, newXid))
+	if (initRequired)
 	{
 		int32		log_status;
 		bool		isnull;
@@ -474,13 +503,11 @@ versionFunc(logTrigger) (PG_FUNCTION_ARGS)
 		}
 
 		cs->currentXid = newXid;
-		cs->event_txn=false;
+		cs->event_txn = false;
+		cs->log_init = true;
 	}
 
-	if(cs->event_txn)
-	{
-		elog(ERROR,"Slony-I: log trigger called in an event transaction");
-	}
+
 	/*
 	 * Save the current datestyle setting and switch to ISO (if not already)
 	 */
@@ -931,7 +958,19 @@ versionFunc(logApply) (PG_FUNCTION_ARGS)
 	/*
 	 * Do the following only once per transaction.
 	 */
-	if (!TransactionIdEquals(cs->currentXid, newXid))
+	bool planInitRequired = false;
+	if(!TransactionIdEquals(cs->currentXid, newXid))
+	{
+		planInitRequired = true;
+		cs->log_init = false;
+		cs->event_txn=false;
+	}
+	else if (!cs->apply_init)
+	{
+		planInitRequired = true;
+	}
+	
+	if (planInitRequired)
 	{
 		HASHCTL		hctl;
 
@@ -992,6 +1031,7 @@ versionFunc(logApply) (PG_FUNCTION_ARGS)
 		apply_num_evict = 0;
 
 		cs->currentXid = newXid;
+		cs->apply_init = true;
 	}
 
 	/*
-- 
2.1.4


--------------060106080008080705060703
Content-Type: text/x-diff;
 name="0001-Explicitly-disallow-createEvent-and-data-changes-in-.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename*0="0001-Explicitly-disallow-createEvent-and-data-changes-in-.pa";
 filename*1="tch"



More information about the Slony1-hackers mailing list