Do not hold the XidGenLock while obtaining an XID from the GTM
authorPavan Deolasee <[email protected]>
Fri, 29 Jan 2016 09:13:52 +0000 (14:43 +0530)
committerPavan Deolasee <[email protected]>
Mon, 1 Feb 2016 09:14:43 +0000 (10:14 +0100)
This was an oversight when on-demand GXID work was committed. Mason reported
that this patch significantly improves performance on his tests and I can
also confirm that with my own tests.

Report and patch by Mason Sharp, with some changes from me.

src/backend/access/transam/varsup.c

index 4a6d3f615293e6f39f68200b734840a2a5f1020c..1e601a68ca28eee05a9c24ae098c15a413bd9578 100644 (file)
@@ -206,11 +206,7 @@ GetNewTransactionId(bool isSubXact)
                }
                *timestamp_received = true;
        }
-#endif /* PGXC */
-
-       LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
 
-#ifdef PGXC
        /*
         * Unless we are running initdb (which sets useLocalXid to true), we must
         * have either got a valid global XID, either from the coordinator/datanode
@@ -222,8 +218,6 @@ GetNewTransactionId(bool isSubXact)
                {
                        xid = next_xid;
                        next_xid = InvalidTransactionId;
-                       if (TransactionIdFollowsOrEquals(xid, ShmemVariableCache->nextXid))
-                               ShmemVariableCache->nextXid = xid;
                }
                else if ((IsConnFromCoord() || IsConnFromDatanode()) &&
                        MyCoordId != InvalidOid && MyCoordPid != 0 &&
@@ -279,10 +273,17 @@ GetNewTransactionId(bool isSubXact)
                                }
                        }
                }
+               LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
+               if (TransactionIdFollowsOrEquals(xid, ShmemVariableCache->nextXid))
+                               ShmemVariableCache->nextXid = xid;
        }
        else
+       {
+               LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
                xid = ShmemVariableCache->nextXid;
+       }
 #else
+       LWLockAcquire(XidGenLock, LW_EXCLUSIVE);
        xid = ShmemVariableCache->nextXid;
 #endif /* PGXC */
 
@@ -442,8 +443,11 @@ GetNewTransactionId(bool isSubXact)
         */
        if (increment_xid || !IsPostmasterEnvironment)
        {
-               ShmemVariableCache->nextXid = xid;
-               TransactionIdAdvance(ShmemVariableCache->nextXid);
+               if (TransactionIdFollowsOrEquals(xid, ShmemVariableCache->nextXid))
+               {
+                       ShmemVariableCache->nextXid = xid;
+                       TransactionIdAdvance(ShmemVariableCache->nextXid);
+               }
        }
 #else
        TransactionIdAdvance(ShmemVariableCache->nextXid);