From 5418ba6709e2a5f25f47dcc6c06ed4646a1b42af Mon Sep 17 00:00:00 2001
From: Amit Kapila <akapila@postgresql.org>
Date: Mon, 8 Jun 2020 11:36:28 +0530
Subject: [PATCH v27 3/3] Incremental patch for 0002 to test removal of
 duplicate invalidation processing.

---
 src/backend/replication/logical/decode.c        |  4 ++--
 src/backend/replication/logical/reorderbuffer.c | 22 +++++++++++++++++-----
 src/include/replication/reorderbuffer.h         |  3 +++
 3 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/src/backend/replication/logical/decode.c b/src/backend/replication/logical/decode.c
index a1d8745..7f1385c 100644
--- a/src/backend/replication/logical/decode.c
+++ b/src/backend/replication/logical/decode.c
@@ -596,10 +596,10 @@ DecodeCommit(LogicalDecodingContext *ctx, XLogRecordBuffer *buf,
 	 */
 	if (parsed->nmsgs > 0)
 	{
-		if (!ctx->fast_forward)
+		/*if (!ctx->fast_forward)
 			ReorderBufferAddInvalidations(ctx->reorder, xid, buf->origptr,
 										  parsed->nmsgs, parsed->msgs);
-		ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);
+		ReorderBufferXidSetCatalogChanges(ctx->reorder, xid, buf->origptr);*/
 	}
 
 	SnapBuildCommitTxn(ctx->snapshot_builder, buf->origptr, xid,
diff --git a/src/backend/replication/logical/reorderbuffer.c b/src/backend/replication/logical/reorderbuffer.c
index b889edf..42067f1 100644
--- a/src/backend/replication/logical/reorderbuffer.c
+++ b/src/backend/replication/logical/reorderbuffer.c
@@ -864,6 +864,9 @@ ReorderBufferAssignChild(ReorderBuffer *rb, TransactionId xid,
 	subtxn->txn_flags |= RBTXN_IS_SUBXACT;
 	subtxn->toplevel_xid = xid;
 	Assert(subtxn->nsubtxns == 0);
+        
+        /* set the reference to top-level transaction */
+        subtxn->toptxn = txn;
 
 	/* add to subtransaction list */
 	dlist_push_tail(&txn->subtxns, &subtxn->node);
@@ -1878,8 +1881,8 @@ ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
 		AbortCurrentTransaction();
 
 		/* make sure there's no cache pollution */
-		ReorderBufferExecuteInvalidations(txn->ninvalidations,
-										  txn->invalidations);
+		/*ReorderBufferExecuteInvalidations(txn->ninvalidations,
+										  txn->invalidations);*/
 
 		if (using_subtxn)
 			RollbackAndReleaseCurrentSubTransaction();
@@ -1905,8 +1908,8 @@ ReorderBufferCommit(ReorderBuffer *rb, TransactionId xid,
 		AbortCurrentTransaction();
 
 		/* make sure there's no cache pollution */
-		ReorderBufferExecuteInvalidations(txn->ninvalidations,
-										  txn->invalidations);
+		/*ReorderBufferExecuteInvalidations(txn->ninvalidations,
+										  txn->invalidations);*/
 
 		if (using_subtxn)
 			RollbackAndReleaseCurrentSubTransaction();
@@ -2237,7 +2240,7 @@ ReorderBufferAddInvalidation(ReorderBuffer *rb, TransactionId xid,
 						   sizeof(SharedInvalidationMessage) * nmsgs);
 	memcpy(change->data.inval.invalidations, msgs,
 		   sizeof(SharedInvalidationMessage) * nmsgs);
-
+        
 	ReorderBufferQueueChange(rb, xid, lsn, change);
 
 	MemoryContextSwitchTo(oldcontext);
@@ -2295,6 +2298,15 @@ ReorderBufferXidSetCatalogChanges(ReorderBuffer *rb, TransactionId xid,
 	txn = ReorderBufferTXNByXid(rb, xid, true, NULL, lsn, true);
 
 	txn->txn_flags |= RBTXN_HAS_CATALOG_CHANGES;
+        
+        /*
+	 * Mark top-level transaction as having catalog changes too if one of its
+	 * children has so that the ReorderBufferBuildTupleCidHash can conveniently
+	 * check just top-level transaction and decide whether to build the hash
+	 * table or not.
+	 */
+        if (txn->toptxn != NULL)
+            txn->toptxn->txn_flags |= RBTXN_HAS_CATALOG_CHANGES;
 }
 
 /*
diff --git a/src/include/replication/reorderbuffer.h b/src/include/replication/reorderbuffer.h
index af35287..e582ceb 100644
--- a/src/include/replication/reorderbuffer.h
+++ b/src/include/replication/reorderbuffer.h
@@ -228,6 +228,9 @@ typedef struct ReorderBufferTXN
 	 * LSN pointing to the end of the commit record + 1.
 	 */
 	XLogRecPtr	end_lsn;
+        
+        /* Toplevel transaction for this subxact (NULL for top-level). */
+        struct ReorderBufferTXN *toptxn;
 
 	/*
 	 * LSN of the last lsn at which snapshot information reside, so we can
-- 
1.8.3.1

