From b4482ca85151298f086a52e3977be8f949741bac Mon Sep 17 00:00:00 2001 From: Masahiko Sawada Date: Sat, 21 Oct 2017 15:50:22 +0900 Subject: [PATCH 1/3] Keep track of local writes. --- src/backend/access/heap/heapam.c | 10 ++++++++++ src/backend/access/transam/xact.c | 27 +++++++++++++++++++++++++++ src/include/access/xact.h | 3 +++ 3 files changed, 40 insertions(+) diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c index 52dda41..fafed0c 100644 --- a/src/backend/access/heap/heapam.c +++ b/src/backend/access/heap/heapam.c @@ -2496,6 +2496,9 @@ heap_insert(Relation relation, HeapTuple tup, CommandId cid, */ CheckForSerializableConflictIn(relation, NULL, InvalidBuffer); + /* Remember to write on local node for foreign transaction */ + RegisterTransactionLocalNode(); + /* NO EREPORT(ERROR) from here till changes are logged */ START_CRIT_SECTION(); @@ -3305,6 +3308,9 @@ l1: */ MultiXactIdSetOldestMember(); + /* Remember to write on local node for foreign transaction */ + RegisterTransactionLocalNode(); + compute_new_xmax_infomask(HeapTupleHeaderGetRawXmax(tp.t_data), tp.t_data->t_infomask, tp.t_data->t_infomask2, xid, LockTupleExclusive, true, @@ -4210,6 +4216,10 @@ l2: */ CheckForSerializableConflictIn(relation, &oldtup, buffer); + + /* Remember to write on local node for foreign transaction */ + RegisterTransactionLocalNode(); + /* * At this point newbuf and buffer are both pinned and locked, and newbuf * has enough space for the new tuple. If they are the same buffer, only diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index 8203388..52e1286 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -117,6 +117,9 @@ TransactionId *ParallelCurrentXids; */ int MyXactFlags; +/* Transaction do the write on local node */ +bool XactWriteLocalNode = false; + /* * transaction states - transaction state from server perspective */ @@ -2150,6 +2153,8 @@ CommitTransaction(void) XactTopTransactionId = InvalidTransactionId; nParallelCurrentXids = 0; + ForgetTransactionLocalNode(); + /* * done with commit processing, set current transaction state back to * default @@ -2427,6 +2432,8 @@ PrepareTransaction(void) XactTopTransactionId = InvalidTransactionId; nParallelCurrentXids = 0; + ForgetTransactionLocalNode(); + /* * done with 1st phase commit processing, set current transaction state * back to default @@ -2611,6 +2618,8 @@ AbortTransaction(void) pgstat_report_xact_timestamp(0); } + ForgetTransactionLocalNode(); + /* * State remains TRANS_ABORT until CleanupTransaction(). */ @@ -4441,6 +4450,24 @@ AbortOutOfAnyTransaction(void) } /* + * RegisterTransactionLocalNode --- remember to write on local node + */ +void +RegisterTransactionLocalNode(void) +{ + XactWriteLocalNode = true; +} + +/* + * ForgetTransactionLocalNode --- forget to write on local node + */ +void +ForgetTransactionLocalNode(void) +{ + XactWriteLocalNode = false; +} + +/* * IsTransactionBlock --- are we within a transaction block? */ bool diff --git a/src/include/access/xact.h b/src/include/access/xact.h index f2c10f9..3e7b054 100644 --- a/src/include/access/xact.h +++ b/src/include/access/xact.h @@ -91,6 +91,7 @@ extern int MyXactFlags; */ #define XACT_FLAGS_ACQUIREDACCESSEXCLUSIVELOCK (1U << 1) +extern bool XactWriteLocalNode; /* * start- and end-of-transaction callbacks for dynamically loaded modules @@ -377,6 +378,8 @@ extern void RegisterXactCallback(XactCallback callback, void *arg); extern void UnregisterXactCallback(XactCallback callback, void *arg); extern void RegisterSubXactCallback(SubXactCallback callback, void *arg); extern void UnregisterSubXactCallback(SubXactCallback callback, void *arg); +extern void RegisterTransactionLocalNode(void); +extern void ForgetTransactionLocalNode(void); extern int xactGetCommittedChildren(TransactionId **ptr); -- 1.8.3.1