@@ -110,6 +110,7 @@ static void MtmXactCallback(XactEvent event, void *arg);
110110static void MtmBeginTransaction (MtmCurrentTrans * x );
111111static void MtmPrePrepareTransaction (MtmCurrentTrans * x );
112112static void MtmPostPrepareTransaction (MtmCurrentTrans * x );
113+ static void MtmAbortPreparedTransaction (MtmCurrentTrans * x );
113114static void MtmEndTransaction (MtmCurrentTrans * x , bool commit );
114115static TransactionId MtmGetOldestXmin (Relation rel , bool ignoreVacuum );
115116static bool MtmXidInMVCCSnapshot (TransactionId xid , Snapshot snapshot );
@@ -496,6 +497,9 @@ MtmXactCallback(XactEvent event, void *arg)
496497 case XACT_EVENT_POST_PREPARE :
497498 MtmPostPrepareTransaction (& dtmTx );
498499 break ;
500+ case XACT_EVENT_ABORT_PREPARED :
501+ MtmAbortPreparedTransaction (& dtmTx );
502+ break ;
499503 case XACT_EVENT_COMMIT :
500504 MtmEndTransaction (& dtmTx , true);
501505 break ;
@@ -522,6 +526,7 @@ MtmResetTransaction(MtmCurrentTrans* x)
522526 x -> snapshot = INVALID_CSN ;
523527 x -> xid = InvalidTransactionId ;
524528 x -> gtid .xid = InvalidTransactionId ;
529+ x -> isDistributed = false;
525530}
526531
527532static void
@@ -620,14 +625,16 @@ static void
620625MtmPostPrepareTransaction (MtmCurrentTrans * x )
621626{
622627 MtmTransState * ts ;
628+ MtmTransMap * tm ;
623629
624630 MtmLock (LW_EXCLUSIVE );
625631 ts = hash_search (MtmXid2State , & x -> xid , HASH_FIND , NULL );
626632 Assert (ts != NULL );
633+ tm = (MtmTransMap * )hash_search (MtmGid2State , x -> gid , HASH_ENTER , NULL );
634+ Assert (x -> gid [0 ]);
635+ tm -> state = ts ;
636+
627637 if (!MtmIsCoordinator (ts )) {
628- MtmTransMap * tm = (MtmTransMap * )hash_search (MtmGid2State , x -> gid , HASH_ENTER , NULL );
629- Assert (x -> gid [0 ]);
630- tm -> state = ts ;
631638 MtmSendNotificationMessage (ts , MSG_READY ); /* send notification to coordinator */
632639 MtmUnlock ();
633640 MtmResetTransaction (x );
@@ -646,6 +653,20 @@ MtmPostPrepareTransaction(MtmCurrentTrans* x)
646653}
647654
648655
656+ static void
657+ MtmAbortPreparedTransaction (MtmCurrentTrans * x )
658+ {
659+ MtmTransMap * tm ;
660+
661+ MtmLock (LW_EXCLUSIVE );
662+ tm = (MtmTransMap * )hash_search (MtmGid2State , x -> gid , HASH_REMOVE , NULL );
663+ Assert (tm != NULL );
664+ tm -> state -> status = TRANSACTION_STATUS_ABORTED ;
665+ MtmAdjustSubtransactions (tm -> state );
666+ MtmUnlock ();
667+ MtmResetTransaction (x );
668+ }
669+
649670static void
650671MtmEndTransaction (MtmCurrentTrans * x , bool commit )
651672{
@@ -695,9 +716,7 @@ MtmEndTransaction(MtmCurrentTrans* x, bool commit)
695716 }
696717 MtmUnlock ();
697718 }
698- x -> snapshot = INVALID_CSN ;
699- x -> xid = InvalidTransactionId ;
700- x -> gtid .xid = InvalidTransactionId ;
719+ MtmResetTransaction (x );
701720 MtmCheckSlots ();
702721}
703722
@@ -1735,17 +1754,16 @@ MtmGenerateGid(char* gid)
17351754
17361755static void MtmTwoPhaseCommit (char * completionTag )
17371756{
1738- char gid [MULTIMASTER_MAX_GID_SIZE ];
1739- MtmGenerateGid (gid );
1757+ MtmGenerateGid (dtmTx .gid );
17401758 if (!IsTransactionBlock ()) {
17411759 elog (WARNING , "Start transaction block for %d" , dtmTx .xid );
17421760 BeginTransactionBlock ();
17431761 CommitTransactionCommand ();
17441762 StartTransactionCommand ();
17451763 }
1746- if (!PrepareTransactionBlock (gid ))
1764+ if (!PrepareTransactionBlock (dtmTx . gid ))
17471765 {
1748- elog (WARNING , "Failed to prepare transaction %s" , gid );
1766+ elog (WARNING , "Failed to prepare transaction %s" , dtmTx . gid );
17491767 /* report unsuccessful commit in completionTag */
17501768 if (completionTag ) {
17511769 strcpy (completionTag , "ROLLBACK" );
@@ -1755,10 +1773,10 @@ static void MtmTwoPhaseCommit(char *completionTag)
17551773 CommitTransactionCommand ();
17561774 StartTransactionCommand ();
17571775 if (MtmGetCurrentTransactionStatus () == TRANSACTION_STATUS_ABORTED ) {
1758- FinishPreparedTransaction (gid , false);
1759- elog (ERROR , "Transaction %s is aborted by DTM" , gid );
1776+ FinishPreparedTransaction (dtmTx . gid , false);
1777+ elog (ERROR , "Transaction %s is aborted by DTM" , dtmTx . gid );
17601778 } else {
1761- FinishPreparedTransaction (gid , true);
1779+ FinishPreparedTransaction (dtmTx . gid , true);
17621780 }
17631781 }
17641782}
0 commit comments