@@ -111,6 +111,7 @@ static void MtmPrePrepareTransaction(MtmCurrentTrans* x);
111111static void MtmPostPrepareTransaction (MtmCurrentTrans * x );
112112static void MtmAbortPreparedTransaction (MtmCurrentTrans * x );
113113static void MtmEndTransaction (MtmCurrentTrans * x , bool commit );
114+ static bool MtmTwoPhaseCommit (MtmCurrentTrans * x );
114115static TransactionId MtmGetOldestXmin (Relation rel , bool ignoreVacuum );
115116static bool MtmXidInMVCCSnapshot (TransactionId xid , Snapshot snapshot );
116117static TransactionId MtmAdjustOldestXid (TransactionId xid );
@@ -588,6 +589,11 @@ MtmXactCallback(XactEvent event, void *arg)
588589 case XACT_EVENT_ABORT :
589590 MtmEndTransaction (& MtmTx , false);
590591 break ;
592+ case XACT_EVENT_COMMIT_COMMAND :
593+ if (!IsTransactionBlock ()) {
594+ MtmTwoPhaseCommit (& MtmTx );
595+ }
596+ break ;
591597 default :
592598 break ;
593599 }
@@ -1922,33 +1928,33 @@ MtmGenerateGid(char* gid)
19221928 sprintf (gid , "MTM-%d-%d-%d" , MtmNodeId , MyProcPid , ++ localCount );
19231929}
19241930
1925- static void MtmTwoPhaseCommit (char * completionTag )
1931+ static bool MtmTwoPhaseCommit (MtmCurrentTrans * x )
19261932{
1927- MtmGenerateGid (MtmTx .gid );
1928- if (!IsTransactionBlock ()) {
1929- elog (WARNING , "Start transaction block for %d" , MtmTx .xid );
1930- BeginTransactionBlock ();
1931- CommitTransactionCommand ();
1932- StartTransactionCommand ();
1933- }
1934- if (!PrepareTransactionBlock (MtmTx .gid ))
1935- {
1936- elog (WARNING , "Failed to prepare transaction %s" , MtmTx .gid );
1937- /* report unsuccessful commit in completionTag */
1938- if (completionTag ) {
1939- strcpy (completionTag , "ROLLBACK" );
1933+ if (x -> isDistributed && x -> containsDML ) {
1934+ MtmGenerateGid (x -> gid );
1935+ if (!IsTransactionBlock ()) {
1936+ elog (WARNING , "Start transaction block for %d" , x -> xid );
1937+ BeginTransactionBlock ();
1938+ CommitTransactionCommand ();
1939+ StartTransactionCommand ();
19401940 }
1941- /* ??? Should we do explicit rollback */
1942- } else {
1943- CommitTransactionCommand ();
1944- StartTransactionCommand ();
1945- if (MtmGetCurrentTransactionStatus () == TRANSACTION_STATUS_ABORTED ) {
1946- FinishPreparedTransaction (MtmTx .gid , false);
1947- elog (ERROR , "Transaction %s is aborted by DTM" , MtmTx .gid );
1948- } else {
1949- FinishPreparedTransaction (MtmTx .gid , true);
1941+ if (!PrepareTransactionBlock (x -> gid ))
1942+ {
1943+ elog (WARNING , "Failed to prepare transaction %s" , x -> gid );
1944+ /* ??? Should we do explicit rollback */
1945+ } else {
1946+ CommitTransactionCommand ();
1947+ StartTransactionCommand ();
1948+ if (MtmGetCurrentTransactionStatus () == TRANSACTION_STATUS_ABORTED ) {
1949+ FinishPreparedTransaction (x -> gid , false);
1950+ elog (ERROR , "Transaction %s is aborted by DTM" , x -> gid );
1951+ } else {
1952+ FinishPreparedTransaction (x -> gid , true);
1953+ }
19501954 }
1955+ return true;
19511956 }
1957+ return false;
19521958}
19531959
19541960static void MtmProcessUtility (Node * parsetree , const char * queryString ,
@@ -1965,8 +1971,7 @@ static void MtmProcessUtility(Node *parsetree, const char *queryString,
19651971 switch (stmt -> kind )
19661972 {
19671973 case TRANS_STMT_COMMIT :
1968- if (MtmTx .isDistributed && MtmTx .containsDML ) {
1969- MtmTwoPhaseCommit (completionTag );
1974+ if (MtmTwoPhaseCommit (& MtmTx )) {
19701975 return ;
19711976 }
19721977 break ;
@@ -2002,9 +2007,6 @@ static void MtmProcessUtility(Node *parsetree, const char *queryString,
20022007 if (MtmProcessDDLCommand (queryString )) {
20032008 return ;
20042009 }
2005- if (MtmTx .isDistributed && MtmTx .containsDML && !IsTransactionBlock ()) {
2006- MtmTwoPhaseCommit (completionTag );
2007- }
20082010 }
20092011 if (PreviousProcessUtilityHook != NULL )
20102012 {
@@ -2035,7 +2037,7 @@ MtmExecutorFinish(QueryDesc *queryDesc)
20352037 }
20362038 }
20372039 if (MtmTx .isDistributed && MtmTx .containsDML && !IsTransactionBlock ()) {
2038- MtmTwoPhaseCommit (NULL );
2040+ MtmTwoPhaseCommit (& MtmTx );
20392041 }
20402042 }
20412043 if (PreviousExecutorFinishHook != NULL )
0 commit comments