Optimize PredicateLockTuple().
authorThomas Munro <[email protected]>
Mon, 11 Nov 2019 03:34:01 +0000 (16:34 +1300)
committerThomas Munro <[email protected]>
Mon, 11 Nov 2019 04:06:59 +0000 (17:06 +1300)
PredicateLockTuple() has a fast exit if tuple was written by the current
transaction, as in that case it already has a lock.  This check can be
performed using TransactionIdIsCurrentTransactionId() instead of
SubTransGetTopmostTransaction(), to avoid any chance of having to hit the
disk.

Author: Ashwin Agrawal, based on a suggestion from Andres Freund
Reviewed-by: Thomas Munro
Discussion: https://postgr.es/m/CALfoeiv0k3hkEb3Oqk%3DziWqtyk2Jys1UOK5hwRBNeANT_yX%2Bng%40mail.gmail.com

src/backend/storage/lmgr/predicate.c

index 78fb90fb1bd6e655fc8b701ca3ed1cde60688c3a..86b46d68b9933cb373e3b61af4053b9ab908538c 100644 (file)
@@ -2549,7 +2549,6 @@ PredicateLockTuple(Relation relation, HeapTuple tuple, Snapshot snapshot)
 {
        PREDICATELOCKTARGETTAG tag;
        ItemPointer tid;
-       TransactionId targetxmin;
 
        if (!SerializationNeededForRead(relation, snapshot))
                return;
@@ -2559,24 +2558,9 @@ PredicateLockTuple(Relation relation, HeapTuple tuple, Snapshot snapshot)
         */
        if (relation->rd_index == NULL)
        {
-               TransactionId myxid;
-
-               targetxmin = HeapTupleHeaderGetXmin(tuple->t_data);
-
-               myxid = GetTopTransactionIdIfAny();
-               if (TransactionIdIsValid(myxid))
-               {
-                       if (TransactionIdFollowsOrEquals(targetxmin, TransactionXmin))
-                       {
-                               TransactionId xid = SubTransGetTopmostTransaction(targetxmin);
-
-                               if (TransactionIdEquals(xid, myxid))
-                               {
-                                       /* We wrote it; we already have a write lock. */
-                                       return;
-                               }
-                       }
-               }
+               /* If we wrote it; we already have a write lock. */
+               if (TransactionIdIsCurrentTransactionId(HeapTupleHeaderGetXmin(tuple->t_data)))
+                       return;
        }
 
        /*