LCOV - code coverage report
Current view: top level - src/backend/storage/lmgr - predicate.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 1028 1371 75.0 %
Date: 2021-12-05 02:08:31 Functions: 65 72 90.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * predicate.c
       4             :  *    POSTGRES predicate locking
       5             :  *    to support full serializable transaction isolation
       6             :  *
       7             :  *
       8             :  * The approach taken is to implement Serializable Snapshot Isolation (SSI)
       9             :  * as initially described in this paper:
      10             :  *
      11             :  *  Michael J. Cahill, Uwe Röhm, and Alan D. Fekete. 2008.
      12             :  *  Serializable isolation for snapshot databases.
      13             :  *  In SIGMOD '08: Proceedings of the 2008 ACM SIGMOD
      14             :  *  international conference on Management of data,
      15             :  *  pages 729-738, New York, NY, USA. ACM.
      16             :  *  http://doi.acm.org/10.1145/1376616.1376690
      17             :  *
      18             :  * and further elaborated in Cahill's doctoral thesis:
      19             :  *
      20             :  *  Michael James Cahill. 2009.
      21             :  *  Serializable Isolation for Snapshot Databases.
      22             :  *  Sydney Digital Theses.
      23             :  *  University of Sydney, School of Information Technologies.
      24             :  *  http://hdl.handle.net/2123/5353
      25             :  *
      26             :  *
      27             :  * Predicate locks for Serializable Snapshot Isolation (SSI) are SIREAD
      28             :  * locks, which are so different from normal locks that a distinct set of
      29             :  * structures is required to handle them.  They are needed to detect
      30             :  * rw-conflicts when the read happens before the write.  (When the write
      31             :  * occurs first, the reading transaction can check for a conflict by
      32             :  * examining the MVCC data.)
      33             :  *
      34             :  * (1)  Besides tuples actually read, they must cover ranges of tuples
      35             :  *      which would have been read based on the predicate.  This will
      36             :  *      require modelling the predicates through locks against database
      37             :  *      objects such as pages, index ranges, or entire tables.
      38             :  *
      39             :  * (2)  They must be kept in RAM for quick access.  Because of this, it
      40             :  *      isn't possible to always maintain tuple-level granularity -- when
      41             :  *      the space allocated to store these approaches exhaustion, a
      42             :  *      request for a lock may need to scan for situations where a single
      43             :  *      transaction holds many fine-grained locks which can be coalesced
      44             :  *      into a single coarser-grained lock.
      45             :  *
      46             :  * (3)  They never block anything; they are more like flags than locks
      47             :  *      in that regard; although they refer to database objects and are
      48             :  *      used to identify rw-conflicts with normal write locks.
      49             :  *
      50             :  * (4)  While they are associated with a transaction, they must survive
      51             :  *      a successful COMMIT of that transaction, and remain until all
      52             :  *      overlapping transactions complete.  This even means that they
      53             :  *      must survive termination of the transaction's process.  If a
      54             :  *      top level transaction is rolled back, however, it is immediately
      55             :  *      flagged so that it can be ignored, and its SIREAD locks can be
      56             :  *      released any time after that.
      57             :  *
      58             :  * (5)  The only transactions which create SIREAD locks or check for
      59             :  *      conflicts with them are serializable transactions.
      60             :  *
      61             :  * (6)  When a write lock for a top level transaction is found to cover
      62             :  *      an existing SIREAD lock for the same transaction, the SIREAD lock
      63             :  *      can be deleted.
      64             :  *
      65             :  * (7)  A write from a serializable transaction must ensure that an xact
      66             :  *      record exists for the transaction, with the same lifespan (until
      67             :  *      all concurrent transaction complete or the transaction is rolled
      68             :  *      back) so that rw-dependencies to that transaction can be
      69             :  *      detected.
      70             :  *
      71             :  * We use an optimization for read-only transactions. Under certain
      72             :  * circumstances, a read-only transaction's snapshot can be shown to
      73             :  * never have conflicts with other transactions.  This is referred to
      74             :  * as a "safe" snapshot (and one known not to be is "unsafe").
      75             :  * However, it can't be determined whether a snapshot is safe until
      76             :  * all concurrent read/write transactions complete.
      77             :  *
      78             :  * Once a read-only transaction is known to have a safe snapshot, it
      79             :  * can release its predicate locks and exempt itself from further
      80             :  * predicate lock tracking. READ ONLY DEFERRABLE transactions run only
      81             :  * on safe snapshots, waiting as necessary for one to be available.
      82             :  *
      83             :  *
      84             :  * Lightweight locks to manage access to the predicate locking shared
      85             :  * memory objects must be taken in this order, and should be released in
      86             :  * reverse order:
      87             :  *
      88             :  *  SerializableFinishedListLock
      89             :  *      - Protects the list of transactions which have completed but which
      90             :  *          may yet matter because they overlap still-active transactions.
      91             :  *
      92             :  *  SerializablePredicateListLock
      93             :  *      - Protects the linked list of locks held by a transaction.  Note
      94             :  *          that the locks themselves are also covered by the partition
      95             :  *          locks of their respective lock targets; this lock only affects
      96             :  *          the linked list connecting the locks related to a transaction.
      97             :  *      - All transactions share this single lock (with no partitioning).
      98             :  *      - There is never a need for a process other than the one running
      99             :  *          an active transaction to walk the list of locks held by that
     100             :  *          transaction, except parallel query workers sharing the leader's
     101             :  *          transaction.  In the parallel case, an extra per-sxact lock is
     102             :  *          taken; see below.
     103             :  *      - It is relatively infrequent that another process needs to
     104             :  *          modify the list for a transaction, but it does happen for such
     105             :  *          things as index page splits for pages with predicate locks and
     106             :  *          freeing of predicate locked pages by a vacuum process.  When
     107             :  *          removing a lock in such cases, the lock itself contains the
     108             :  *          pointers needed to remove it from the list.  When adding a
     109             :  *          lock in such cases, the lock can be added using the anchor in
     110             :  *          the transaction structure.  Neither requires walking the list.
     111             :  *      - Cleaning up the list for a terminated transaction is sometimes
     112             :  *          not done on a retail basis, in which case no lock is required.
     113             :  *      - Due to the above, a process accessing its active transaction's
     114             :  *          list always uses a shared lock, regardless of whether it is
     115             :  *          walking or maintaining the list.  This improves concurrency
     116             :  *          for the common access patterns.
     117             :  *      - A process which needs to alter the list of a transaction other
     118             :  *          than its own active transaction must acquire an exclusive
     119             :  *          lock.
     120             :  *
     121             :  *  SERIALIZABLEXACT's member 'perXactPredicateListLock'
     122             :  *      - Protects the linked list of predicate locks held by a transaction.
     123             :  *          Only needed for parallel mode, where multiple backends share the
     124             :  *          same SERIALIZABLEXACT object.  Not needed if
     125             :  *          SerializablePredicateListLock is held exclusively.
     126             :  *
     127             :  *  PredicateLockHashPartitionLock(hashcode)
     128             :  *      - The same lock protects a target, all locks on that target, and
     129             :  *          the linked list of locks on the target.
     130             :  *      - When more than one is needed, acquire in ascending address order.
     131             :  *      - When all are needed (rare), acquire in ascending index order with
     132             :  *          PredicateLockHashPartitionLockByIndex(index).
     133             :  *
     134             :  *  SerializableXactHashLock
     135             :  *      - Protects both PredXact and SerializableXidHash.
     136             :  *
     137             :  *
     138             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
     139             :  * Portions Copyright (c) 1994, Regents of the University of California
     140             :  *
     141             :  *
     142             :  * IDENTIFICATION
     143             :  *    src/backend/storage/lmgr/predicate.c
     144             :  *
     145             :  *-------------------------------------------------------------------------
     146             :  */
     147             : /*
     148             :  * INTERFACE ROUTINES
     149             :  *
     150             :  * housekeeping for setting up shared memory predicate lock structures
     151             :  *      InitPredicateLocks(void)
     152             :  *      PredicateLockShmemSize(void)
     153             :  *
     154             :  * predicate lock reporting
     155             :  *      GetPredicateLockStatusData(void)
     156             :  *      PageIsPredicateLocked(Relation relation, BlockNumber blkno)
     157             :  *
     158             :  * predicate lock maintenance
     159             :  *      GetSerializableTransactionSnapshot(Snapshot snapshot)
     160             :  *      SetSerializableTransactionSnapshot(Snapshot snapshot,
     161             :  *                                         VirtualTransactionId *sourcevxid)
     162             :  *      RegisterPredicateLockingXid(void)
     163             :  *      PredicateLockRelation(Relation relation, Snapshot snapshot)
     164             :  *      PredicateLockPage(Relation relation, BlockNumber blkno,
     165             :  *                      Snapshot snapshot)
     166             :  *      PredicateLockTID(Relation relation, ItemPointer tid, Snapshot snapshot,
     167             :  *                       TransactionId insert_xid)
     168             :  *      PredicateLockPageSplit(Relation relation, BlockNumber oldblkno,
     169             :  *                             BlockNumber newblkno)
     170             :  *      PredicateLockPageCombine(Relation relation, BlockNumber oldblkno,
     171             :  *                               BlockNumber newblkno)
     172             :  *      TransferPredicateLocksToHeapRelation(Relation relation)
     173             :  *      ReleasePredicateLocks(bool isCommit, bool isReadOnlySafe)
     174             :  *
     175             :  * conflict detection (may also trigger rollback)
     176             :  *      CheckForSerializableConflictOut(Relation relation, TransactionId xid,
     177             :  *                                      Snapshot snapshot)
     178             :  *      CheckForSerializableConflictIn(Relation relation, ItemPointer tid,
     179             :  *                                     BlockNumber blkno)
     180             :  *      CheckTableForSerializableConflictIn(Relation relation)
     181             :  *
     182             :  * final rollback checking
     183             :  *      PreCommit_CheckForSerializationFailure(void)
     184             :  *
     185             :  * two-phase commit support
     186             :  *      AtPrepare_PredicateLocks(void);
     187             :  *      PostPrepare_PredicateLocks(TransactionId xid);
     188             :  *      PredicateLockTwoPhaseFinish(TransactionId xid, bool isCommit);
     189             :  *      predicatelock_twophase_recover(TransactionId xid, uint16 info,
     190             :  *                                     void *recdata, uint32 len);
     191             :  */
     192             : 
     193             : #include "postgres.h"
     194             : 
     195             : #include "access/parallel.h"
     196             : #include "access/slru.h"
     197             : #include "access/subtrans.h"
     198             : #include "access/transam.h"
     199             : #include "access/twophase.h"
     200             : #include "access/twophase_rmgr.h"
     201             : #include "access/xact.h"
     202             : #include "access/xlog.h"
     203             : #include "miscadmin.h"
     204             : #include "pgstat.h"
     205             : #include "storage/bufmgr.h"
     206             : #include "storage/predicate.h"
     207             : #include "storage/predicate_internals.h"
     208             : #include "storage/proc.h"
     209             : #include "storage/procarray.h"
     210             : #include "utils/rel.h"
     211             : #include "utils/snapmgr.h"
     212             : 
     213             : /* Uncomment the next line to test the graceful degradation code. */
     214             : /* #define TEST_SUMMARIZE_SERIAL */
     215             : 
     216             : /*
     217             :  * Test the most selective fields first, for performance.
     218             :  *
     219             :  * a is covered by b if all of the following hold:
     220             :  *  1) a.database = b.database
     221             :  *  2) a.relation = b.relation
     222             :  *  3) b.offset is invalid (b is page-granularity or higher)
     223             :  *  4) either of the following:
     224             :  *      4a) a.offset is valid (a is tuple-granularity) and a.page = b.page
     225             :  *   or 4b) a.offset is invalid and b.page is invalid (a is
     226             :  *          page-granularity and b is relation-granularity
     227             :  */
     228             : #define TargetTagIsCoveredBy(covered_target, covering_target)           \
     229             :     ((GET_PREDICATELOCKTARGETTAG_RELATION(covered_target) == /* (2) */  \
     230             :       GET_PREDICATELOCKTARGETTAG_RELATION(covering_target))             \
     231             :      && (GET_PREDICATELOCKTARGETTAG_OFFSET(covering_target) ==          \
     232             :          InvalidOffsetNumber)                                /* (3) */  \
     233             :      && (((GET_PREDICATELOCKTARGETTAG_OFFSET(covered_target) !=         \
     234             :            InvalidOffsetNumber)                              /* (4a) */ \
     235             :           && (GET_PREDICATELOCKTARGETTAG_PAGE(covering_target) ==       \
     236             :               GET_PREDICATELOCKTARGETTAG_PAGE(covered_target)))         \
     237             :          || ((GET_PREDICATELOCKTARGETTAG_PAGE(covering_target) ==       \
     238             :               InvalidBlockNumber)                            /* (4b) */ \
     239             :              && (GET_PREDICATELOCKTARGETTAG_PAGE(covered_target)        \
     240             :                  != InvalidBlockNumber)))                               \
     241             :      && (GET_PREDICATELOCKTARGETTAG_DB(covered_target) ==    /* (1) */  \
     242             :          GET_PREDICATELOCKTARGETTAG_DB(covering_target)))
     243             : 
     244             : /*
     245             :  * The predicate locking target and lock shared hash tables are partitioned to
     246             :  * reduce contention.  To determine which partition a given target belongs to,
     247             :  * compute the tag's hash code with PredicateLockTargetTagHashCode(), then
     248             :  * apply one of these macros.
     249             :  * NB: NUM_PREDICATELOCK_PARTITIONS must be a power of 2!
     250             :  */
     251             : #define PredicateLockHashPartition(hashcode) \
     252             :     ((hashcode) % NUM_PREDICATELOCK_PARTITIONS)
     253             : #define PredicateLockHashPartitionLock(hashcode) \
     254             :     (&MainLWLockArray[PREDICATELOCK_MANAGER_LWLOCK_OFFSET + \
     255             :         PredicateLockHashPartition(hashcode)].lock)
     256             : #define PredicateLockHashPartitionLockByIndex(i) \
     257             :     (&MainLWLockArray[PREDICATELOCK_MANAGER_LWLOCK_OFFSET + (i)].lock)
     258             : 
     259             : #define NPREDICATELOCKTARGETENTS() \
     260             :     mul_size(max_predicate_locks_per_xact, add_size(MaxBackends, max_prepared_xacts))
     261             : 
     262             : #define SxactIsOnFinishedList(sxact) (!SHMQueueIsDetached(&((sxact)->finishedLink)))
     263             : 
     264             : /*
     265             :  * Note that a sxact is marked "prepared" once it has passed
     266             :  * PreCommit_CheckForSerializationFailure, even if it isn't using
     267             :  * 2PC. This is the point at which it can no longer be aborted.
     268             :  *
     269             :  * The PREPARED flag remains set after commit, so SxactIsCommitted
     270             :  * implies SxactIsPrepared.
     271             :  */
     272             : #define SxactIsCommitted(sxact) (((sxact)->flags & SXACT_FLAG_COMMITTED) != 0)
     273             : #define SxactIsPrepared(sxact) (((sxact)->flags & SXACT_FLAG_PREPARED) != 0)
     274             : #define SxactIsRolledBack(sxact) (((sxact)->flags & SXACT_FLAG_ROLLED_BACK) != 0)
     275             : #define SxactIsDoomed(sxact) (((sxact)->flags & SXACT_FLAG_DOOMED) != 0)
     276             : #define SxactIsReadOnly(sxact) (((sxact)->flags & SXACT_FLAG_READ_ONLY) != 0)
     277             : #define SxactHasSummaryConflictIn(sxact) (((sxact)->flags & SXACT_FLAG_SUMMARY_CONFLICT_IN) != 0)
     278             : #define SxactHasSummaryConflictOut(sxact) (((sxact)->flags & SXACT_FLAG_SUMMARY_CONFLICT_OUT) != 0)
     279             : /*
     280             :  * The following macro actually means that the specified transaction has a
     281             :  * conflict out *to a transaction which committed ahead of it*.  It's hard
     282             :  * to get that into a name of a reasonable length.
     283             :  */
     284             : #define SxactHasConflictOut(sxact) (((sxact)->flags & SXACT_FLAG_CONFLICT_OUT) != 0)
     285             : #define SxactIsDeferrableWaiting(sxact) (((sxact)->flags & SXACT_FLAG_DEFERRABLE_WAITING) != 0)
     286             : #define SxactIsROSafe(sxact) (((sxact)->flags & SXACT_FLAG_RO_SAFE) != 0)
     287             : #define SxactIsROUnsafe(sxact) (((sxact)->flags & SXACT_FLAG_RO_UNSAFE) != 0)
     288             : #define SxactIsPartiallyReleased(sxact) (((sxact)->flags & SXACT_FLAG_PARTIALLY_RELEASED) != 0)
     289             : 
     290             : /*
     291             :  * Compute the hash code associated with a PREDICATELOCKTARGETTAG.
     292             :  *
     293             :  * To avoid unnecessary recomputations of the hash code, we try to do this
     294             :  * just once per function, and then pass it around as needed.  Aside from
     295             :  * passing the hashcode to hash_search_with_hash_value(), we can extract
     296             :  * the lock partition number from the hashcode.
     297             :  */
     298             : #define PredicateLockTargetTagHashCode(predicatelocktargettag) \
     299             :     get_hash_value(PredicateLockTargetHash, predicatelocktargettag)
     300             : 
     301             : /*
     302             :  * Given a predicate lock tag, and the hash for its target,
     303             :  * compute the lock hash.
     304             :  *
     305             :  * To make the hash code also depend on the transaction, we xor the sxid
     306             :  * struct's address into the hash code, left-shifted so that the
     307             :  * partition-number bits don't change.  Since this is only a hash, we
     308             :  * don't care if we lose high-order bits of the address; use an
     309             :  * intermediate variable to suppress cast-pointer-to-int warnings.
     310             :  */
     311             : #define PredicateLockHashCodeFromTargetHashCode(predicatelocktag, targethash) \
     312             :     ((targethash) ^ ((uint32) PointerGetDatum((predicatelocktag)->myXact)) \
     313             :      << LOG2_NUM_PREDICATELOCK_PARTITIONS)
     314             : 
     315             : 
     316             : /*
     317             :  * The SLRU buffer area through which we access the old xids.
     318             :  */
     319             : static SlruCtlData SerialSlruCtlData;
     320             : 
     321             : #define SerialSlruCtl           (&SerialSlruCtlData)
     322             : 
     323             : #define SERIAL_PAGESIZE         BLCKSZ
     324             : #define SERIAL_ENTRYSIZE            sizeof(SerCommitSeqNo)
     325             : #define SERIAL_ENTRIESPERPAGE   (SERIAL_PAGESIZE / SERIAL_ENTRYSIZE)
     326             : 
     327             : /*
     328             :  * Set maximum pages based on the number needed to track all transactions.
     329             :  */
     330             : #define SERIAL_MAX_PAGE         (MaxTransactionId / SERIAL_ENTRIESPERPAGE)
     331             : 
     332             : #define SerialNextPage(page) (((page) >= SERIAL_MAX_PAGE) ? 0 : (page) + 1)
     333             : 
     334             : #define SerialValue(slotno, xid) (*((SerCommitSeqNo *) \
     335             :     (SerialSlruCtl->shared->page_buffer[slotno] + \
     336             :     ((((uint32) (xid)) % SERIAL_ENTRIESPERPAGE) * SERIAL_ENTRYSIZE))))
     337             : 
     338             : #define SerialPage(xid) (((uint32) (xid)) / SERIAL_ENTRIESPERPAGE)
     339             : 
     340             : typedef struct SerialControlData
     341             : {
     342             :     int         headPage;       /* newest initialized page */
     343             :     TransactionId headXid;      /* newest valid Xid in the SLRU */
     344             :     TransactionId tailXid;      /* oldest xmin we might be interested in */
     345             : }           SerialControlData;
     346             : 
     347             : typedef struct SerialControlData *SerialControl;
     348             : 
     349             : static SerialControl serialControl;
     350             : 
     351             : /*
     352             :  * When the oldest committed transaction on the "finished" list is moved to
     353             :  * SLRU, its predicate locks will be moved to this "dummy" transaction,
     354             :  * collapsing duplicate targets.  When a duplicate is found, the later
     355             :  * commitSeqNo is used.
     356             :  */
     357             : static SERIALIZABLEXACT *OldCommittedSxact;
     358             : 
     359             : 
     360             : /*
     361             :  * These configuration variables are used to set the predicate lock table size
     362             :  * and to control promotion of predicate locks to coarser granularity in an
     363             :  * attempt to degrade performance (mostly as false positive serialization
     364             :  * failure) gracefully in the face of memory pressure.
     365             :  */
     366             : int         max_predicate_locks_per_xact;   /* set by guc.c */
     367             : int         max_predicate_locks_per_relation;   /* set by guc.c */
     368             : int         max_predicate_locks_per_page;   /* set by guc.c */
     369             : 
     370             : /*
     371             :  * This provides a list of objects in order to track transactions
     372             :  * participating in predicate locking.  Entries in the list are fixed size,
     373             :  * and reside in shared memory.  The memory address of an entry must remain
     374             :  * fixed during its lifetime.  The list will be protected from concurrent
     375             :  * update externally; no provision is made in this code to manage that.  The
     376             :  * number of entries in the list, and the size allowed for each entry is
     377             :  * fixed upon creation.
     378             :  */
     379             : static PredXactList PredXact;
     380             : 
     381             : /*
     382             :  * This provides a pool of RWConflict data elements to use in conflict lists
     383             :  * between transactions.
     384             :  */
     385             : static RWConflictPoolHeader RWConflictPool;
     386             : 
     387             : /*
     388             :  * The predicate locking hash tables are in shared memory.
     389             :  * Each backend keeps pointers to them.
     390             :  */
     391             : static HTAB *SerializableXidHash;
     392             : static HTAB *PredicateLockTargetHash;
     393             : static HTAB *PredicateLockHash;
     394             : static SHM_QUEUE *FinishedSerializableTransactions;
     395             : 
     396             : /*
     397             :  * Tag for a dummy entry in PredicateLockTargetHash. By temporarily removing
     398             :  * this entry, you can ensure that there's enough scratch space available for
     399             :  * inserting one entry in the hash table. This is an otherwise-invalid tag.
     400             :  */
     401             : static const PREDICATELOCKTARGETTAG ScratchTargetTag = {0, 0, 0, 0};
     402             : static uint32 ScratchTargetTagHash;
     403             : static LWLock *ScratchPartitionLock;
     404             : 
     405             : /*
     406             :  * The local hash table used to determine when to combine multiple fine-
     407             :  * grained locks into a single courser-grained lock.
     408             :  */
     409             : static HTAB *LocalPredicateLockHash = NULL;
     410             : 
     411             : /*
     412             :  * Keep a pointer to the currently-running serializable transaction (if any)
     413             :  * for quick reference. Also, remember if we have written anything that could
     414             :  * cause a rw-conflict.
     415             :  */
     416             : static SERIALIZABLEXACT *MySerializableXact = InvalidSerializableXact;
     417             : static bool MyXactDidWrite = false;
     418             : 
     419             : /*
     420             :  * The SXACT_FLAG_RO_UNSAFE optimization might lead us to release
     421             :  * MySerializableXact early.  If that happens in a parallel query, the leader
     422             :  * needs to defer the destruction of the SERIALIZABLEXACT until end of
     423             :  * transaction, because the workers still have a reference to it.  In that
     424             :  * case, the leader stores it here.
     425             :  */
     426             : static SERIALIZABLEXACT *SavedSerializableXact = InvalidSerializableXact;
     427             : 
     428             : /* local functions */
     429             : 
     430             : static SERIALIZABLEXACT *CreatePredXact(void);
     431             : static void ReleasePredXact(SERIALIZABLEXACT *sxact);
     432             : static SERIALIZABLEXACT *FirstPredXact(void);
     433             : static SERIALIZABLEXACT *NextPredXact(SERIALIZABLEXACT *sxact);
     434             : 
     435             : static bool RWConflictExists(const SERIALIZABLEXACT *reader, const SERIALIZABLEXACT *writer);
     436             : static void SetRWConflict(SERIALIZABLEXACT *reader, SERIALIZABLEXACT *writer);
     437             : static void SetPossibleUnsafeConflict(SERIALIZABLEXACT *roXact, SERIALIZABLEXACT *activeXact);
     438             : static void ReleaseRWConflict(RWConflict conflict);
     439             : static void FlagSxactUnsafe(SERIALIZABLEXACT *sxact);
     440             : 
     441             : static bool SerialPagePrecedesLogically(int page1, int page2);
     442             : static void SerialInit(void);
     443             : static void SerialAdd(TransactionId xid, SerCommitSeqNo minConflictCommitSeqNo);
     444             : static SerCommitSeqNo SerialGetMinConflictCommitSeqNo(TransactionId xid);
     445             : static void SerialSetActiveSerXmin(TransactionId xid);
     446             : 
     447             : static uint32 predicatelock_hash(const void *key, Size keysize);
     448             : static void SummarizeOldestCommittedSxact(void);
     449             : static Snapshot GetSafeSnapshot(Snapshot snapshot);
     450             : static Snapshot GetSerializableTransactionSnapshotInt(Snapshot snapshot,
     451             :                                                       VirtualTransactionId *sourcevxid,
     452             :                                                       int sourcepid);
     453             : static bool PredicateLockExists(const PREDICATELOCKTARGETTAG *targettag);
     454             : static bool GetParentPredicateLockTag(const PREDICATELOCKTARGETTAG *tag,
     455             :                                       PREDICATELOCKTARGETTAG *parent);
     456             : static bool CoarserLockCovers(const PREDICATELOCKTARGETTAG *newtargettag);
     457             : static void RemoveScratchTarget(bool lockheld);
     458             : static void RestoreScratchTarget(bool lockheld);
     459             : static void RemoveTargetIfNoLongerUsed(PREDICATELOCKTARGET *target,
     460             :                                        uint32 targettaghash);
     461             : static void DeleteChildTargetLocks(const PREDICATELOCKTARGETTAG *newtargettag);
     462             : static int  MaxPredicateChildLocks(const PREDICATELOCKTARGETTAG *tag);
     463             : static bool CheckAndPromotePredicateLockRequest(const PREDICATELOCKTARGETTAG *reqtag);
     464             : static void DecrementParentLocks(const PREDICATELOCKTARGETTAG *targettag);
     465             : static void CreatePredicateLock(const PREDICATELOCKTARGETTAG *targettag,
     466             :                                 uint32 targettaghash,
     467             :                                 SERIALIZABLEXACT *sxact);
     468             : static void DeleteLockTarget(PREDICATELOCKTARGET *target, uint32 targettaghash);
     469             : static bool TransferPredicateLocksToNewTarget(PREDICATELOCKTARGETTAG oldtargettag,
     470             :                                               PREDICATELOCKTARGETTAG newtargettag,
     471             :                                               bool removeOld);
     472             : static void PredicateLockAcquire(const PREDICATELOCKTARGETTAG *targettag);
     473             : static void DropAllPredicateLocksFromTable(Relation relation,
     474             :                                            bool transfer);
     475             : static void SetNewSxactGlobalXmin(void);
     476             : static void ClearOldPredicateLocks(void);
     477             : static void ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial,
     478             :                                        bool summarize);
     479             : static bool XidIsConcurrent(TransactionId xid);
     480             : static void CheckTargetForConflictsIn(PREDICATELOCKTARGETTAG *targettag);
     481             : static void FlagRWConflict(SERIALIZABLEXACT *reader, SERIALIZABLEXACT *writer);
     482             : static void OnConflict_CheckForSerializationFailure(const SERIALIZABLEXACT *reader,
     483             :                                                     SERIALIZABLEXACT *writer);
     484             : static void CreateLocalPredicateLockHash(void);
     485             : static void ReleasePredicateLocksLocal(void);
     486             : 
     487             : 
     488             : /*------------------------------------------------------------------------*/
     489             : 
     490             : /*
     491             :  * Does this relation participate in predicate locking? Temporary and system
     492             :  * relations are exempt, as are materialized views.
     493             :  */
     494             : static inline bool
     495      256422 : PredicateLockingNeededForRelation(Relation relation)
     496             : {
     497      367820 :     return !(relation->rd_id < FirstUnpinnedObjectId ||
     498      111398 :              RelationUsesLocalBuffers(relation) ||
     499      111382 :              relation->rd_rel->relkind == RELKIND_MATVIEW);
     500             : }
     501             : 
     502             : /*
     503             :  * When a public interface method is called for a read, this is the test to
     504             :  * see if we should do a quick return.
     505             :  *
     506             :  * Note: this function has side-effects! If this transaction has been flagged
     507             :  * as RO-safe since the last call, we release all predicate locks and reset
     508             :  * MySerializableXact. That makes subsequent calls to return quickly.
     509             :  *
     510             :  * This is marked as 'inline' to eliminate the function call overhead in the
     511             :  * common case that serialization is not needed.
     512             :  */
     513             : static inline bool
     514   431384956 : SerializationNeededForRead(Relation relation, Snapshot snapshot)
     515             : {
     516             :     /* Nothing to do if this is not a serializable transaction */
     517   431384956 :     if (MySerializableXact == InvalidSerializableXact)
     518   431135428 :         return false;
     519             : 
     520             :     /*
     521             :      * Don't acquire locks or conflict when scanning with a special snapshot.
     522             :      * This excludes things like CLUSTER and REINDEX. They use the wholesale
     523             :      * functions TransferPredicateLocksToHeapRelation() and
     524             :      * CheckTableForSerializableConflictIn() to participate in serialization,
     525             :      * but the scans involved don't need serialization.
     526             :      */
     527      249528 :     if (!IsMVCCSnapshot(snapshot))
     528        2602 :         return false;
     529             : 
     530             :     /*
     531             :      * Check if we have just become "RO-safe". If we have, immediately release
     532             :      * all locks as they're not needed anymore. This also resets
     533             :      * MySerializableXact, so that subsequent calls to this function can exit
     534             :      * quickly.
     535             :      *
     536             :      * A transaction is flagged as RO_SAFE if all concurrent R/W transactions
     537             :      * commit without having conflicts out to an earlier snapshot, thus
     538             :      * ensuring that no conflicts are possible for this transaction.
     539             :      */
     540      246926 :     if (SxactIsROSafe(MySerializableXact))
     541             :     {
     542          62 :         ReleasePredicateLocks(false, true);
     543          62 :         return false;
     544             :     }
     545             : 
     546             :     /* Check if the relation doesn't participate in predicate locking */
     547      246864 :     if (!PredicateLockingNeededForRelation(relation))
     548      144460 :         return false;
     549             : 
     550      102404 :     return true;                /* no excuse to skip predicate locking */
     551             : }
     552             : 
     553             : /*
     554             :  * Like SerializationNeededForRead(), but called on writes.
     555             :  * The logic is the same, but there is no snapshot and we can't be RO-safe.
     556             :  */
     557             : static inline bool
     558    35450448 : SerializationNeededForWrite(Relation relation)
     559             : {
     560             :     /* Nothing to do if this is not a serializable transaction */
     561    35450448 :     if (MySerializableXact == InvalidSerializableXact)
     562    35441156 :         return false;
     563             : 
     564             :     /* Check if the relation doesn't participate in predicate locking */
     565        9292 :     if (!PredicateLockingNeededForRelation(relation))
     566         540 :         return false;
     567             : 
     568        8752 :     return true;                /* no excuse to skip predicate locking */
     569             : }
     570             : 
     571             : 
     572             : /*------------------------------------------------------------------------*/
     573             : 
     574             : /*
     575             :  * These functions are a simple implementation of a list for this specific
     576             :  * type of struct.  If there is ever a generalized shared memory list, we
     577             :  * should probably switch to that.
     578             :  */
     579             : static SERIALIZABLEXACT *
     580        6008 : CreatePredXact(void)
     581             : {
     582             :     PredXactListElement ptle;
     583             : 
     584             :     ptle = (PredXactListElement)
     585        6008 :         SHMQueueNext(&PredXact->availableList,
     586        6008 :                      &PredXact->availableList,
     587             :                      offsetof(PredXactListElementData, link));
     588        6008 :     if (!ptle)
     589           0 :         return NULL;
     590             : 
     591        6008 :     SHMQueueDelete(&ptle->link);
     592        6008 :     SHMQueueInsertBefore(&PredXact->activeList, &ptle->link);
     593        6008 :     return &ptle->sxact;
     594             : }
     595             : 
     596             : static void
     597        3114 : ReleasePredXact(SERIALIZABLEXACT *sxact)
     598             : {
     599             :     PredXactListElement ptle;
     600             : 
     601             :     Assert(ShmemAddrIsValid(sxact));
     602             : 
     603        3114 :     ptle = (PredXactListElement)
     604             :         (((char *) sxact)
     605             :          - offsetof(PredXactListElementData, sxact)
     606             :          + offsetof(PredXactListElementData, link));
     607        3114 :     SHMQueueDelete(&ptle->link);
     608        3114 :     SHMQueueInsertBefore(&PredXact->availableList, &ptle->link);
     609        3114 : }
     610             : 
     611             : static SERIALIZABLEXACT *
     612       13810 : FirstPredXact(void)
     613             : {
     614             :     PredXactListElement ptle;
     615             : 
     616             :     ptle = (PredXactListElement)
     617       13810 :         SHMQueueNext(&PredXact->activeList,
     618       13810 :                      &PredXact->activeList,
     619             :                      offsetof(PredXactListElementData, link));
     620       13810 :     if (!ptle)
     621           0 :         return NULL;
     622             : 
     623       13810 :     return &ptle->sxact;
     624             : }
     625             : 
     626             : static SERIALIZABLEXACT *
     627       17348 : NextPredXact(SERIALIZABLEXACT *sxact)
     628             : {
     629             :     PredXactListElement ptle;
     630             : 
     631             :     Assert(ShmemAddrIsValid(sxact));
     632             : 
     633       17348 :     ptle = (PredXactListElement)
     634             :         (((char *) sxact)
     635             :          - offsetof(PredXactListElementData, sxact)
     636             :          + offsetof(PredXactListElementData, link));
     637             :     ptle = (PredXactListElement)
     638       17348 :         SHMQueueNext(&PredXact->activeList,
     639       17348 :                      &ptle->link,
     640             :                      offsetof(PredXactListElementData, link));
     641       17348 :     if (!ptle)
     642       13676 :         return NULL;
     643             : 
     644        3672 :     return &ptle->sxact;
     645             : }
     646             : 
     647             : /*------------------------------------------------------------------------*/
     648             : 
     649             : /*
     650             :  * These functions manage primitive access to the RWConflict pool and lists.
     651             :  */
     652             : static bool
     653        3650 : RWConflictExists(const SERIALIZABLEXACT *reader, const SERIALIZABLEXACT *writer)
     654             : {
     655             :     RWConflict  conflict;
     656             : 
     657             :     Assert(reader != writer);
     658             : 
     659             :     /* Check the ends of the purported conflict first. */
     660        3650 :     if (SxactIsDoomed(reader)
     661        3650 :         || SxactIsDoomed(writer)
     662        3650 :         || SHMQueueEmpty(&reader->outConflicts)
     663        1110 :         || SHMQueueEmpty(&writer->inConflicts))
     664        2620 :         return false;
     665             : 
     666             :     /* A conflict is possible; walk the list to find out. */
     667             :     conflict = (RWConflict)
     668        1030 :         SHMQueueNext(&reader->outConflicts,
     669             :                      &reader->outConflicts,
     670             :                      offsetof(RWConflictData, outLink));
     671        1062 :     while (conflict)
     672             :     {
     673        1030 :         if (conflict->sxactIn == writer)
     674         998 :             return true;
     675             :         conflict = (RWConflict)
     676          32 :             SHMQueueNext(&reader->outConflicts,
     677          32 :                          &conflict->outLink,
     678             :                          offsetof(RWConflictData, outLink));
     679             :     }
     680             : 
     681             :     /* No conflict found. */
     682          32 :     return false;
     683             : }
     684             : 
     685             : static void
     686        1524 : SetRWConflict(SERIALIZABLEXACT *reader, SERIALIZABLEXACT *writer)
     687             : {
     688             :     RWConflict  conflict;
     689             : 
     690             :     Assert(reader != writer);
     691             :     Assert(!RWConflictExists(reader, writer));
     692             : 
     693             :     conflict = (RWConflict)
     694        1524 :         SHMQueueNext(&RWConflictPool->availableList,
     695        1524 :                      &RWConflictPool->availableList,
     696             :                      offsetof(RWConflictData, outLink));
     697        1524 :     if (!conflict)
     698           0 :         ereport(ERROR,
     699             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
     700             :                  errmsg("not enough elements in RWConflictPool to record a read/write conflict"),
     701             :                  errhint("You might need to run fewer transactions at a time or increase max_connections.")));
     702             : 
     703        1524 :     SHMQueueDelete(&conflict->outLink);
     704             : 
     705        1524 :     conflict->sxactOut = reader;
     706        1524 :     conflict->sxactIn = writer;
     707        1524 :     SHMQueueInsertBefore(&reader->outConflicts, &conflict->outLink);
     708        1524 :     SHMQueueInsertBefore(&writer->inConflicts, &conflict->inLink);
     709        1524 : }
     710             : 
     711             : static void
     712         260 : SetPossibleUnsafeConflict(SERIALIZABLEXACT *roXact,
     713             :                           SERIALIZABLEXACT *activeXact)
     714             : {
     715             :     RWConflict  conflict;
     716             : 
     717             :     Assert(roXact != activeXact);
     718             :     Assert(SxactIsReadOnly(roXact));
     719             :     Assert(!SxactIsReadOnly(activeXact));
     720             : 
     721             :     conflict = (RWConflict)
     722         260 :         SHMQueueNext(&RWConflictPool->availableList,
     723         260 :                      &RWConflictPool->availableList,
     724             :                      offsetof(RWConflictData, outLink));
     725         260 :     if (!conflict)
     726           0 :         ereport(ERROR,
     727             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
     728             :                  errmsg("not enough elements in RWConflictPool to record a potential read/write conflict"),
     729             :                  errhint("You might need to run fewer transactions at a time or increase max_connections.")));
     730             : 
     731         260 :     SHMQueueDelete(&conflict->outLink);
     732             : 
     733         260 :     conflict->sxactOut = activeXact;
     734         260 :     conflict->sxactIn = roXact;
     735         260 :     SHMQueueInsertBefore(&activeXact->possibleUnsafeConflicts,
     736             :                          &conflict->outLink);
     737         260 :     SHMQueueInsertBefore(&roXact->possibleUnsafeConflicts,
     738             :                          &conflict->inLink);
     739         260 : }
     740             : 
     741             : static void
     742        1784 : ReleaseRWConflict(RWConflict conflict)
     743             : {
     744        1784 :     SHMQueueDelete(&conflict->inLink);
     745        1784 :     SHMQueueDelete(&conflict->outLink);
     746        1784 :     SHMQueueInsertBefore(&RWConflictPool->availableList, &conflict->outLink);
     747        1784 : }
     748             : 
     749             : static void
     750           6 : FlagSxactUnsafe(SERIALIZABLEXACT *sxact)
     751             : {
     752             :     RWConflict  conflict,
     753             :                 nextConflict;
     754             : 
     755             :     Assert(SxactIsReadOnly(sxact));
     756             :     Assert(!SxactIsROSafe(sxact));
     757             : 
     758           6 :     sxact->flags |= SXACT_FLAG_RO_UNSAFE;
     759             : 
     760             :     /*
     761             :      * We know this isn't a safe snapshot, so we can stop looking for other
     762             :      * potential conflicts.
     763             :      */
     764             :     conflict = (RWConflict)
     765           6 :         SHMQueueNext(&sxact->possibleUnsafeConflicts,
     766           6 :                      &sxact->possibleUnsafeConflicts,
     767             :                      offsetof(RWConflictData, inLink));
     768          12 :     while (conflict)
     769             :     {
     770             :         nextConflict = (RWConflict)
     771           6 :             SHMQueueNext(&sxact->possibleUnsafeConflicts,
     772           6 :                          &conflict->inLink,
     773             :                          offsetof(RWConflictData, inLink));
     774             : 
     775             :         Assert(!SxactIsReadOnly(conflict->sxactOut));
     776             :         Assert(sxact == conflict->sxactIn);
     777             : 
     778           6 :         ReleaseRWConflict(conflict);
     779             : 
     780           6 :         conflict = nextConflict;
     781             :     }
     782           6 : }
     783             : 
     784             : /*------------------------------------------------------------------------*/
     785             : 
     786             : /*
     787             :  * Decide whether a Serial page number is "older" for truncation purposes.
     788             :  * Analogous to CLOGPagePrecedes().
     789             :  */
     790             : static bool
     791           0 : SerialPagePrecedesLogically(int page1, int page2)
     792             : {
     793             :     TransactionId xid1;
     794             :     TransactionId xid2;
     795             : 
     796           0 :     xid1 = ((TransactionId) page1) * SERIAL_ENTRIESPERPAGE;
     797           0 :     xid1 += FirstNormalTransactionId + 1;
     798           0 :     xid2 = ((TransactionId) page2) * SERIAL_ENTRIESPERPAGE;
     799           0 :     xid2 += FirstNormalTransactionId + 1;
     800             : 
     801           0 :     return (TransactionIdPrecedes(xid1, xid2) &&
     802           0 :             TransactionIdPrecedes(xid1, xid2 + SERIAL_ENTRIESPERPAGE - 1));
     803             : }
     804             : 
     805             : #ifdef USE_ASSERT_CHECKING
     806             : static void
     807             : SerialPagePrecedesLogicallyUnitTests(void)
     808             : {
     809             :     int         per_page = SERIAL_ENTRIESPERPAGE,
     810             :                 offset = per_page / 2;
     811             :     int         newestPage,
     812             :                 oldestPage,
     813             :                 headPage,
     814             :                 targetPage;
     815             :     TransactionId newestXact,
     816             :                 oldestXact;
     817             : 
     818             :     /* GetNewTransactionId() has assigned the last XID it can safely use. */
     819             :     newestPage = 2 * SLRU_PAGES_PER_SEGMENT - 1;    /* nothing special */
     820             :     newestXact = newestPage * per_page + offset;
     821             :     Assert(newestXact / per_page == newestPage);
     822             :     oldestXact = newestXact + 1;
     823             :     oldestXact -= 1U << 31;
     824             :     oldestPage = oldestXact / per_page;
     825             : 
     826             :     /*
     827             :      * In this scenario, the SLRU headPage pertains to the last ~1000 XIDs
     828             :      * assigned.  oldestXact finishes, ~2B XIDs having elapsed since it
     829             :      * started.  Further transactions cause us to summarize oldestXact to
     830             :      * tailPage.  Function must return false so SerialAdd() doesn't zero
     831             :      * tailPage (which may contain entries for other old, recently-finished
     832             :      * XIDs) and half the SLRU.  Reaching this requires burning ~2B XIDs in
     833             :      * single-user mode, a negligible possibility.
     834             :      */
     835             :     headPage = newestPage;
     836             :     targetPage = oldestPage;
     837             :     Assert(!SerialPagePrecedesLogically(headPage, targetPage));
     838             : 
     839             :     /*
     840             :      * In this scenario, the SLRU headPage pertains to oldestXact.  We're
     841             :      * summarizing an XID near newestXact.  (Assume few other XIDs used
     842             :      * SERIALIZABLE, hence the minimal headPage advancement.  Assume
     843             :      * oldestXact was long-running and only recently reached the SLRU.)
     844             :      * Function must return true to make SerialAdd() create targetPage.
     845             :      *
     846             :      * Today's implementation mishandles this case, but it doesn't matter
     847             :      * enough to fix.  Verify that the defect affects just one page by
     848             :      * asserting correct treatment of its prior page.  Reaching this case
     849             :      * requires burning ~2B XIDs in single-user mode, a negligible
     850             :      * possibility.  Moreover, if it does happen, the consequence would be
     851             :      * mild, namely a new transaction failing in SimpleLruReadPage().
     852             :      */
     853             :     headPage = oldestPage;
     854             :     targetPage = newestPage;
     855             :     Assert(SerialPagePrecedesLogically(headPage, targetPage - 1));
     856             : #if 0
     857             :     Assert(SerialPagePrecedesLogically(headPage, targetPage));
     858             : #endif
     859             : }
     860             : #endif
     861             : 
     862             : /*
     863             :  * Initialize for the tracking of old serializable committed xids.
     864             :  */
     865             : static void
     866        2894 : SerialInit(void)
     867             : {
     868             :     bool        found;
     869             : 
     870             :     /*
     871             :      * Set up SLRU management of the pg_serial data.
     872             :      */
     873        2894 :     SerialSlruCtl->PagePrecedes = SerialPagePrecedesLogically;
     874        2894 :     SimpleLruInit(SerialSlruCtl, "Serial",
     875        2894 :                   NUM_SERIAL_BUFFERS, 0, SerialSLRULock, "pg_serial",
     876             :                   LWTRANCHE_SERIAL_BUFFER, SYNC_HANDLER_NONE);
     877             : #ifdef USE_ASSERT_CHECKING
     878             :     SerialPagePrecedesLogicallyUnitTests();
     879             : #endif
     880             :     SlruPagePrecedesUnitTests(SerialSlruCtl, SERIAL_ENTRIESPERPAGE);
     881             : 
     882             :     /*
     883             :      * Create or attach to the SerialControl structure.
     884             :      */
     885        2894 :     serialControl = (SerialControl)
     886        2894 :         ShmemInitStruct("SerialControlData", sizeof(SerialControlData), &found);
     887             : 
     888             :     Assert(found == IsUnderPostmaster);
     889        2894 :     if (!found)
     890             :     {
     891             :         /*
     892             :          * Set control information to reflect empty SLRU.
     893             :          */
     894        2894 :         serialControl->headPage = -1;
     895        2894 :         serialControl->headXid = InvalidTransactionId;
     896        2894 :         serialControl->tailXid = InvalidTransactionId;
     897             :     }
     898        2894 : }
     899             : 
     900             : /*
     901             :  * Record a committed read write serializable xid and the minimum
     902             :  * commitSeqNo of any transactions to which this xid had a rw-conflict out.
     903             :  * An invalid commitSeqNo means that there were no conflicts out from xid.
     904             :  */
     905             : static void
     906           0 : SerialAdd(TransactionId xid, SerCommitSeqNo minConflictCommitSeqNo)
     907             : {
     908             :     TransactionId tailXid;
     909             :     int         targetPage;
     910             :     int         slotno;
     911             :     int         firstZeroPage;
     912             :     bool        isNewPage;
     913             : 
     914             :     Assert(TransactionIdIsValid(xid));
     915             : 
     916           0 :     targetPage = SerialPage(xid);
     917             : 
     918           0 :     LWLockAcquire(SerialSLRULock, LW_EXCLUSIVE);
     919             : 
     920             :     /*
     921             :      * If no serializable transactions are active, there shouldn't be anything
     922             :      * to push out to the SLRU.  Hitting this assert would mean there's
     923             :      * something wrong with the earlier cleanup logic.
     924             :      */
     925           0 :     tailXid = serialControl->tailXid;
     926             :     Assert(TransactionIdIsValid(tailXid));
     927             : 
     928             :     /*
     929             :      * If the SLRU is currently unused, zero out the whole active region from
     930             :      * tailXid to headXid before taking it into use. Otherwise zero out only
     931             :      * any new pages that enter the tailXid-headXid range as we advance
     932             :      * headXid.
     933             :      */
     934           0 :     if (serialControl->headPage < 0)
     935             :     {
     936           0 :         firstZeroPage = SerialPage(tailXid);
     937           0 :         isNewPage = true;
     938             :     }
     939             :     else
     940             :     {
     941           0 :         firstZeroPage = SerialNextPage(serialControl->headPage);
     942           0 :         isNewPage = SerialPagePrecedesLogically(serialControl->headPage,
     943             :                                                 targetPage);
     944             :     }
     945             : 
     946           0 :     if (!TransactionIdIsValid(serialControl->headXid)
     947           0 :         || TransactionIdFollows(xid, serialControl->headXid))
     948           0 :         serialControl->headXid = xid;
     949           0 :     if (isNewPage)
     950           0 :         serialControl->headPage = targetPage;
     951             : 
     952           0 :     if (isNewPage)
     953             :     {
     954             :         /* Initialize intervening pages. */
     955           0 :         while (firstZeroPage != targetPage)
     956             :         {
     957           0 :             (void) SimpleLruZeroPage(SerialSlruCtl, firstZeroPage);
     958           0 :             firstZeroPage = SerialNextPage(firstZeroPage);
     959             :         }
     960           0 :         slotno = SimpleLruZeroPage(SerialSlruCtl, targetPage);
     961             :     }
     962             :     else
     963           0 :         slotno = SimpleLruReadPage(SerialSlruCtl, targetPage, true, xid);
     964             : 
     965           0 :     SerialValue(slotno, xid) = minConflictCommitSeqNo;
     966           0 :     SerialSlruCtl->shared->page_dirty[slotno] = true;
     967             : 
     968           0 :     LWLockRelease(SerialSLRULock);
     969           0 : }
     970             : 
     971             : /*
     972             :  * Get the minimum commitSeqNo for any conflict out for the given xid.  For
     973             :  * a transaction which exists but has no conflict out, InvalidSerCommitSeqNo
     974             :  * will be returned.
     975             :  */
     976             : static SerCommitSeqNo
     977          42 : SerialGetMinConflictCommitSeqNo(TransactionId xid)
     978             : {
     979             :     TransactionId headXid;
     980             :     TransactionId tailXid;
     981             :     SerCommitSeqNo val;
     982             :     int         slotno;
     983             : 
     984             :     Assert(TransactionIdIsValid(xid));
     985             : 
     986          42 :     LWLockAcquire(SerialSLRULock, LW_SHARED);
     987          42 :     headXid = serialControl->headXid;
     988          42 :     tailXid = serialControl->tailXid;
     989          42 :     LWLockRelease(SerialSLRULock);
     990             : 
     991          42 :     if (!TransactionIdIsValid(headXid))
     992          42 :         return 0;
     993             : 
     994             :     Assert(TransactionIdIsValid(tailXid));
     995             : 
     996           0 :     if (TransactionIdPrecedes(xid, tailXid)
     997           0 :         || TransactionIdFollows(xid, headXid))
     998           0 :         return 0;
     999             : 
    1000             :     /*
    1001             :      * The following function must be called without holding SerialSLRULock,
    1002             :      * but will return with that lock held, which must then be released.
    1003             :      */
    1004           0 :     slotno = SimpleLruReadPage_ReadOnly(SerialSlruCtl,
    1005             :                                         SerialPage(xid), xid);
    1006           0 :     val = SerialValue(slotno, xid);
    1007           0 :     LWLockRelease(SerialSLRULock);
    1008           0 :     return val;
    1009             : }
    1010             : 
    1011             : /*
    1012             :  * Call this whenever there is a new xmin for active serializable
    1013             :  * transactions.  We don't need to keep information on transactions which
    1014             :  * precede that.  InvalidTransactionId means none active, so everything in
    1015             :  * the SLRU can be discarded.
    1016             :  */
    1017             : static void
    1018        3144 : SerialSetActiveSerXmin(TransactionId xid)
    1019             : {
    1020        3144 :     LWLockAcquire(SerialSLRULock, LW_EXCLUSIVE);
    1021             : 
    1022             :     /*
    1023             :      * When no sxacts are active, nothing overlaps, set the xid values to
    1024             :      * invalid to show that there are no valid entries.  Don't clear headPage,
    1025             :      * though.  A new xmin might still land on that page, and we don't want to
    1026             :      * repeatedly zero out the same page.
    1027             :      */
    1028        3144 :     if (!TransactionIdIsValid(xid))
    1029             :     {
    1030        1554 :         serialControl->tailXid = InvalidTransactionId;
    1031        1554 :         serialControl->headXid = InvalidTransactionId;
    1032        1554 :         LWLockRelease(SerialSLRULock);
    1033        1554 :         return;
    1034             :     }
    1035             : 
    1036             :     /*
    1037             :      * When we're recovering prepared transactions, the global xmin might move
    1038             :      * backwards depending on the order they're recovered. Normally that's not
    1039             :      * OK, but during recovery no serializable transactions will commit, so
    1040             :      * the SLRU is empty and we can get away with it.
    1041             :      */
    1042        1590 :     if (RecoveryInProgress())
    1043             :     {
    1044             :         Assert(serialControl->headPage < 0);
    1045           0 :         if (!TransactionIdIsValid(serialControl->tailXid)
    1046           0 :             || TransactionIdPrecedes(xid, serialControl->tailXid))
    1047             :         {
    1048           0 :             serialControl->tailXid = xid;
    1049             :         }
    1050           0 :         LWLockRelease(SerialSLRULock);
    1051           0 :         return;
    1052             :     }
    1053             : 
    1054             :     Assert(!TransactionIdIsValid(serialControl->tailXid)
    1055             :            || TransactionIdFollows(xid, serialControl->tailXid));
    1056             : 
    1057        1590 :     serialControl->tailXid = xid;
    1058             : 
    1059        1590 :     LWLockRelease(SerialSLRULock);
    1060             : }
    1061             : 
    1062             : /*
    1063             :  * Perform a checkpoint --- either during shutdown, or on-the-fly
    1064             :  *
    1065             :  * We don't have any data that needs to survive a restart, but this is a
    1066             :  * convenient place to truncate the SLRU.
    1067             :  */
    1068             : void
    1069        4142 : CheckPointPredicate(void)
    1070             : {
    1071             :     int         tailPage;
    1072             : 
    1073        4142 :     LWLockAcquire(SerialSLRULock, LW_EXCLUSIVE);
    1074             : 
    1075             :     /* Exit quickly if the SLRU is currently not in use. */
    1076        4142 :     if (serialControl->headPage < 0)
    1077             :     {
    1078        4142 :         LWLockRelease(SerialSLRULock);
    1079        4142 :         return;
    1080             :     }
    1081             : 
    1082           0 :     if (TransactionIdIsValid(serialControl->tailXid))
    1083             :     {
    1084             :         /* We can truncate the SLRU up to the page containing tailXid */
    1085           0 :         tailPage = SerialPage(serialControl->tailXid);
    1086             :     }
    1087             :     else
    1088             :     {
    1089             :         /*----------
    1090             :          * The SLRU is no longer needed. Truncate to head before we set head
    1091             :          * invalid.
    1092             :          *
    1093             :          * XXX: It's possible that the SLRU is not needed again until XID
    1094             :          * wrap-around has happened, so that the segment containing headPage
    1095             :          * that we leave behind will appear to be new again. In that case it
    1096             :          * won't be removed until XID horizon advances enough to make it
    1097             :          * current again.
    1098             :          *
    1099             :          * XXX: This should happen in vac_truncate_clog(), not in checkpoints.
    1100             :          * Consider this scenario, starting from a system with no in-progress
    1101             :          * transactions and VACUUM FREEZE having maximized oldestXact:
    1102             :          * - Start a SERIALIZABLE transaction.
    1103             :          * - Start, finish, and summarize a SERIALIZABLE transaction, creating
    1104             :          *   one SLRU page.
    1105             :          * - Consume XIDs to reach xidStopLimit.
    1106             :          * - Finish all transactions.  Due to the long-running SERIALIZABLE
    1107             :          *   transaction, earlier checkpoints did not touch headPage.  The
    1108             :          *   next checkpoint will change it, but that checkpoint happens after
    1109             :          *   the end of the scenario.
    1110             :          * - VACUUM to advance XID limits.
    1111             :          * - Consume ~2M XIDs, crossing the former xidWrapLimit.
    1112             :          * - Start, finish, and summarize a SERIALIZABLE transaction.
    1113             :          *   SerialAdd() declines to create the targetPage, because headPage
    1114             :          *   is not regarded as in the past relative to that targetPage.  The
    1115             :          *   transaction instigating the summarize fails in
    1116             :          *   SimpleLruReadPage().
    1117             :          */
    1118           0 :         tailPage = serialControl->headPage;
    1119           0 :         serialControl->headPage = -1;
    1120             :     }
    1121             : 
    1122           0 :     LWLockRelease(SerialSLRULock);
    1123             : 
    1124             :     /* Truncate away pages that are no longer required */
    1125           0 :     SimpleLruTruncate(SerialSlruCtl, tailPage);
    1126             : 
    1127             :     /*
    1128             :      * Write dirty SLRU pages to disk
    1129             :      *
    1130             :      * This is not actually necessary from a correctness point of view. We do
    1131             :      * it merely as a debugging aid.
    1132             :      *
    1133             :      * We're doing this after the truncation to avoid writing pages right
    1134             :      * before deleting the file in which they sit, which would be completely
    1135             :      * pointless.
    1136             :      */
    1137           0 :     SimpleLruWriteAll(SerialSlruCtl, true);
    1138             : }
    1139             : 
    1140             : /*------------------------------------------------------------------------*/
    1141             : 
    1142             : /*
    1143             :  * InitPredicateLocks -- Initialize the predicate locking data structures.
    1144             :  *
    1145             :  * This is called from CreateSharedMemoryAndSemaphores(), which see for
    1146             :  * more comments.  In the normal postmaster case, the shared hash tables
    1147             :  * are created here.  Backends inherit the pointers
    1148             :  * to the shared tables via fork().  In the EXEC_BACKEND case, each
    1149             :  * backend re-executes this code to obtain pointers to the already existing
    1150             :  * shared hash tables.
    1151             :  */
    1152             : void
    1153        2894 : InitPredicateLocks(void)
    1154             : {
    1155             :     HASHCTL     info;
    1156             :     long        max_table_size;
    1157             :     Size        requestSize;
    1158             :     bool        found;
    1159             : 
    1160             : #ifndef EXEC_BACKEND
    1161             :     Assert(!IsUnderPostmaster);
    1162             : #endif
    1163             : 
    1164             :     /*
    1165             :      * Compute size of predicate lock target hashtable. Note these
    1166             :      * calculations must agree with PredicateLockShmemSize!
    1167             :      */
    1168        2894 :     max_table_size = NPREDICATELOCKTARGETENTS();
    1169             : 
    1170             :     /*
    1171             :      * Allocate hash table for PREDICATELOCKTARGET structs.  This stores
    1172             :      * per-predicate-lock-target information.
    1173             :      */
    1174        2894 :     info.keysize = sizeof(PREDICATELOCKTARGETTAG);
    1175        2894 :     info.entrysize = sizeof(PREDICATELOCKTARGET);
    1176        2894 :     info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
    1177             : 
    1178        2894 :     PredicateLockTargetHash = ShmemInitHash("PREDICATELOCKTARGET hash",
    1179             :                                             max_table_size,
    1180             :                                             max_table_size,
    1181             :                                             &info,
    1182             :                                             HASH_ELEM | HASH_BLOBS |
    1183             :                                             HASH_PARTITION | HASH_FIXED_SIZE);
    1184             : 
    1185             :     /*
    1186             :      * Reserve a dummy entry in the hash table; we use it to make sure there's
    1187             :      * always one entry available when we need to split or combine a page,
    1188             :      * because running out of space there could mean aborting a
    1189             :      * non-serializable transaction.
    1190             :      */
    1191        2894 :     if (!IsUnderPostmaster)
    1192             :     {
    1193        2894 :         (void) hash_search(PredicateLockTargetHash, &ScratchTargetTag,
    1194             :                            HASH_ENTER, &found);
    1195             :         Assert(!found);
    1196             :     }
    1197             : 
    1198             :     /* Pre-calculate the hash and partition lock of the scratch entry */
    1199        2894 :     ScratchTargetTagHash = PredicateLockTargetTagHashCode(&ScratchTargetTag);
    1200        2894 :     ScratchPartitionLock = PredicateLockHashPartitionLock(ScratchTargetTagHash);
    1201             : 
    1202             :     /*
    1203             :      * Allocate hash table for PREDICATELOCK structs.  This stores per
    1204             :      * xact-lock-of-a-target information.
    1205             :      */
    1206        2894 :     info.keysize = sizeof(PREDICATELOCKTAG);
    1207        2894 :     info.entrysize = sizeof(PREDICATELOCK);
    1208        2894 :     info.hash = predicatelock_hash;
    1209        2894 :     info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
    1210             : 
    1211             :     /* Assume an average of 2 xacts per target */
    1212        2894 :     max_table_size *= 2;
    1213             : 
    1214        2894 :     PredicateLockHash = ShmemInitHash("PREDICATELOCK hash",
    1215             :                                       max_table_size,
    1216             :                                       max_table_size,
    1217             :                                       &info,
    1218             :                                       HASH_ELEM | HASH_FUNCTION |
    1219             :                                       HASH_PARTITION | HASH_FIXED_SIZE);
    1220             : 
    1221             :     /*
    1222             :      * Compute size for serializable transaction hashtable. Note these
    1223             :      * calculations must agree with PredicateLockShmemSize!
    1224             :      */
    1225        2894 :     max_table_size = (MaxBackends + max_prepared_xacts);
    1226             : 
    1227             :     /*
    1228             :      * Allocate a list to hold information on transactions participating in
    1229             :      * predicate locking.
    1230             :      *
    1231             :      * Assume an average of 10 predicate locking transactions per backend.
    1232             :      * This allows aggressive cleanup while detail is present before data must
    1233             :      * be summarized for storage in SLRU and the "dummy" transaction.
    1234             :      */
    1235        2894 :     max_table_size *= 10;
    1236             : 
    1237        2894 :     PredXact = ShmemInitStruct("PredXactList",
    1238             :                                PredXactListDataSize,
    1239             :                                &found);
    1240             :     Assert(found == IsUnderPostmaster);
    1241        2894 :     if (!found)
    1242             :     {
    1243             :         int         i;
    1244             : 
    1245        2894 :         SHMQueueInit(&PredXact->availableList);
    1246        2894 :         SHMQueueInit(&PredXact->activeList);
    1247        2894 :         PredXact->SxactGlobalXmin = InvalidTransactionId;
    1248        2894 :         PredXact->SxactGlobalXminCount = 0;
    1249        2894 :         PredXact->WritableSxactCount = 0;
    1250        2894 :         PredXact->LastSxactCommitSeqNo = FirstNormalSerCommitSeqNo - 1;
    1251        2894 :         PredXact->CanPartialClearThrough = 0;
    1252        2894 :         PredXact->HavePartialClearedThrough = 0;
    1253        2894 :         requestSize = mul_size((Size) max_table_size,
    1254             :                                PredXactListElementDataSize);
    1255        2894 :         PredXact->element = ShmemAlloc(requestSize);
    1256             :         /* Add all elements to available list, clean. */
    1257        2894 :         memset(PredXact->element, 0, requestSize);
    1258     3045774 :         for (i = 0; i < max_table_size; i++)
    1259             :         {
    1260     3042880 :             LWLockInitialize(&PredXact->element[i].sxact.perXactPredicateListLock,
    1261             :                              LWTRANCHE_PER_XACT_PREDICATE_LIST);
    1262     3042880 :             SHMQueueInsertBefore(&(PredXact->availableList),
    1263     3042880 :                                  &(PredXact->element[i].link));
    1264             :         }
    1265        2894 :         PredXact->OldCommittedSxact = CreatePredXact();
    1266        2894 :         SetInvalidVirtualTransactionId(PredXact->OldCommittedSxact->vxid);
    1267        2894 :         PredXact->OldCommittedSxact->prepareSeqNo = 0;
    1268        2894 :         PredXact->OldCommittedSxact->commitSeqNo = 0;
    1269        2894 :         PredXact->OldCommittedSxact->SeqNo.lastCommitBeforeSnapshot = 0;
    1270        2894 :         SHMQueueInit(&PredXact->OldCommittedSxact->outConflicts);
    1271        2894 :         SHMQueueInit(&PredXact->OldCommittedSxact->inConflicts);
    1272        2894 :         SHMQueueInit(&PredXact->OldCommittedSxact->predicateLocks);
    1273        2894 :         SHMQueueInit(&PredXact->OldCommittedSxact->finishedLink);
    1274        2894 :         SHMQueueInit(&PredXact->OldCommittedSxact->possibleUnsafeConflicts);
    1275        2894 :         PredXact->OldCommittedSxact->topXid = InvalidTransactionId;
    1276        2894 :         PredXact->OldCommittedSxact->finishedBefore = InvalidTransactionId;
    1277        2894 :         PredXact->OldCommittedSxact->xmin = InvalidTransactionId;
    1278        2894 :         PredXact->OldCommittedSxact->flags = SXACT_FLAG_COMMITTED;
    1279        2894 :         PredXact->OldCommittedSxact->pid = 0;
    1280             :     }
    1281             :     /* This never changes, so let's keep a local copy. */
    1282        2894 :     OldCommittedSxact = PredXact->OldCommittedSxact;
    1283             : 
    1284             :     /*
    1285             :      * Allocate hash table for SERIALIZABLEXID structs.  This stores per-xid
    1286             :      * information for serializable transactions which have accessed data.
    1287             :      */
    1288        2894 :     info.keysize = sizeof(SERIALIZABLEXIDTAG);
    1289        2894 :     info.entrysize = sizeof(SERIALIZABLEXID);
    1290             : 
    1291        2894 :     SerializableXidHash = ShmemInitHash("SERIALIZABLEXID hash",
    1292             :                                         max_table_size,
    1293             :                                         max_table_size,
    1294             :                                         &info,
    1295             :                                         HASH_ELEM | HASH_BLOBS |
    1296             :                                         HASH_FIXED_SIZE);
    1297             : 
    1298             :     /*
    1299             :      * Allocate space for tracking rw-conflicts in lists attached to the
    1300             :      * transactions.
    1301             :      *
    1302             :      * Assume an average of 5 conflicts per transaction.  Calculations suggest
    1303             :      * that this will prevent resource exhaustion in even the most pessimal
    1304             :      * loads up to max_connections = 200 with all 200 connections pounding the
    1305             :      * database with serializable transactions.  Beyond that, there may be
    1306             :      * occasional transactions canceled when trying to flag conflicts. That's
    1307             :      * probably OK.
    1308             :      */
    1309        2894 :     max_table_size *= 5;
    1310             : 
    1311        2894 :     RWConflictPool = ShmemInitStruct("RWConflictPool",
    1312             :                                      RWConflictPoolHeaderDataSize,
    1313             :                                      &found);
    1314             :     Assert(found == IsUnderPostmaster);
    1315        2894 :     if (!found)
    1316             :     {
    1317             :         int         i;
    1318             : 
    1319        2894 :         SHMQueueInit(&RWConflictPool->availableList);
    1320        2894 :         requestSize = mul_size((Size) max_table_size,
    1321             :                                RWConflictDataSize);
    1322        2894 :         RWConflictPool->element = ShmemAlloc(requestSize);
    1323             :         /* Add all elements to available list, clean. */
    1324        2894 :         memset(RWConflictPool->element, 0, requestSize);
    1325    15217294 :         for (i = 0; i < max_table_size; i++)
    1326             :         {
    1327    15214400 :             SHMQueueInsertBefore(&(RWConflictPool->availableList),
    1328    15214400 :                                  &(RWConflictPool->element[i].outLink));
    1329             :         }
    1330             :     }
    1331             : 
    1332             :     /*
    1333             :      * Create or attach to the header for the list of finished serializable
    1334             :      * transactions.
    1335             :      */
    1336        2894 :     FinishedSerializableTransactions = (SHM_QUEUE *)
    1337        2894 :         ShmemInitStruct("FinishedSerializableTransactions",
    1338             :                         sizeof(SHM_QUEUE),
    1339             :                         &found);
    1340             :     Assert(found == IsUnderPostmaster);
    1341        2894 :     if (!found)
    1342        2894 :         SHMQueueInit(FinishedSerializableTransactions);
    1343             : 
    1344             :     /*
    1345             :      * Initialize the SLRU storage for old committed serializable
    1346             :      * transactions.
    1347             :      */
    1348        2894 :     SerialInit();
    1349        2894 : }
    1350             : 
    1351             : /*
    1352             :  * Estimate shared-memory space used for predicate lock table
    1353             :  */
    1354             : Size
    1355        3842 : PredicateLockShmemSize(void)
    1356             : {
    1357        3842 :     Size        size = 0;
    1358             :     long        max_table_size;
    1359             : 
    1360             :     /* predicate lock target hash table */
    1361        3842 :     max_table_size = NPREDICATELOCKTARGETENTS();
    1362        3842 :     size = add_size(size, hash_estimate_size(max_table_size,
    1363             :                                              sizeof(PREDICATELOCKTARGET)));
    1364             : 
    1365             :     /* predicate lock hash table */
    1366        3842 :     max_table_size *= 2;
    1367        3842 :     size = add_size(size, hash_estimate_size(max_table_size,
    1368             :                                              sizeof(PREDICATELOCK)));
    1369             : 
    1370             :     /*
    1371             :      * Since NPREDICATELOCKTARGETENTS is only an estimate, add 10% safety
    1372             :      * margin.
    1373             :      */
    1374        3842 :     size = add_size(size, size / 10);
    1375             : 
    1376             :     /* transaction list */
    1377        3842 :     max_table_size = MaxBackends + max_prepared_xacts;
    1378        3842 :     max_table_size *= 10;
    1379        3842 :     size = add_size(size, PredXactListDataSize);
    1380        3842 :     size = add_size(size, mul_size((Size) max_table_size,
    1381             :                                    PredXactListElementDataSize));
    1382             : 
    1383             :     /* transaction xid table */
    1384        3842 :     size = add_size(size, hash_estimate_size(max_table_size,
    1385             :                                              sizeof(SERIALIZABLEXID)));
    1386             : 
    1387             :     /* rw-conflict pool */
    1388        3842 :     max_table_size *= 5;
    1389        3842 :     size = add_size(size, RWConflictPoolHeaderDataSize);
    1390        3842 :     size = add_size(size, mul_size((Size) max_table_size,
    1391             :                                    RWConflictDataSize));
    1392             : 
    1393             :     /* Head for list of finished serializable transactions. */
    1394        3842 :     size = add_size(size, sizeof(SHM_QUEUE));
    1395             : 
    1396             :     /* Shared memory structures for SLRU tracking of old committed xids. */
    1397        3842 :     size = add_size(size, sizeof(SerialControlData));
    1398        3842 :     size = add_size(size, SimpleLruShmemSize(NUM_SERIAL_BUFFERS, 0));
    1399             : 
    1400        3842 :     return size;
    1401             : }
    1402             : 
    1403             : 
    1404             : /*
    1405             :  * Compute the hash code associated with a PREDICATELOCKTAG.
    1406             :  *
    1407             :  * Because we want to use just one set of partition locks for both the
    1408             :  * PREDICATELOCKTARGET and PREDICATELOCK hash tables, we have to make sure
    1409             :  * that PREDICATELOCKs fall into the same partition number as their
    1410             :  * associated PREDICATELOCKTARGETs.  dynahash.c expects the partition number
    1411             :  * to be the low-order bits of the hash code, and therefore a
    1412             :  * PREDICATELOCKTAG's hash code must have the same low-order bits as the
    1413             :  * associated PREDICATELOCKTARGETTAG's hash code.  We achieve this with this
    1414             :  * specialized hash function.
    1415             :  */
    1416             : static uint32
    1417           0 : predicatelock_hash(const void *key, Size keysize)
    1418             : {
    1419           0 :     const PREDICATELOCKTAG *predicatelocktag = (const PREDICATELOCKTAG *) key;
    1420             :     uint32      targethash;
    1421             : 
    1422             :     Assert(keysize == sizeof(PREDICATELOCKTAG));
    1423             : 
    1424             :     /* Look into the associated target object, and compute its hash code */
    1425           0 :     targethash = PredicateLockTargetTagHashCode(&predicatelocktag->myTarget->tag);
    1426             : 
    1427           0 :     return PredicateLockHashCodeFromTargetHashCode(predicatelocktag, targethash);
    1428             : }
    1429             : 
    1430             : 
    1431             : /*
    1432             :  * GetPredicateLockStatusData
    1433             :  *      Return a table containing the internal state of the predicate
    1434             :  *      lock manager for use in pg_lock_status.
    1435             :  *
    1436             :  * Like GetLockStatusData, this function tries to hold the partition LWLocks
    1437             :  * for as short a time as possible by returning two arrays that simply
    1438             :  * contain the PREDICATELOCKTARGETTAG and SERIALIZABLEXACT for each lock
    1439             :  * table entry. Multiple copies of the same PREDICATELOCKTARGETTAG and
    1440             :  * SERIALIZABLEXACT will likely appear.
    1441             :  */
    1442             : PredicateLockData *
    1443         260 : GetPredicateLockStatusData(void)
    1444             : {
    1445             :     PredicateLockData *data;
    1446             :     int         i;
    1447             :     int         els,
    1448             :                 el;
    1449             :     HASH_SEQ_STATUS seqstat;
    1450             :     PREDICATELOCK *predlock;
    1451             : 
    1452         260 :     data = (PredicateLockData *) palloc(sizeof(PredicateLockData));
    1453             : 
    1454             :     /*
    1455             :      * To ensure consistency, take simultaneous locks on all partition locks
    1456             :      * in ascending order, then SerializableXactHashLock.
    1457             :      */
    1458        4420 :     for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
    1459        4160 :         LWLockAcquire(PredicateLockHashPartitionLockByIndex(i), LW_SHARED);
    1460         260 :     LWLockAcquire(SerializableXactHashLock, LW_SHARED);
    1461             : 
    1462             :     /* Get number of locks and allocate appropriately-sized arrays. */
    1463         260 :     els = hash_get_num_entries(PredicateLockHash);
    1464         260 :     data->nelements = els;
    1465         260 :     data->locktags = (PREDICATELOCKTARGETTAG *)
    1466         260 :         palloc(sizeof(PREDICATELOCKTARGETTAG) * els);
    1467         260 :     data->xacts = (SERIALIZABLEXACT *)
    1468         260 :         palloc(sizeof(SERIALIZABLEXACT) * els);
    1469             : 
    1470             : 
    1471             :     /* Scan through PredicateLockHash and copy contents */
    1472         260 :     hash_seq_init(&seqstat, PredicateLockHash);
    1473             : 
    1474         260 :     el = 0;
    1475             : 
    1476         264 :     while ((predlock = (PREDICATELOCK *) hash_seq_search(&seqstat)))
    1477             :     {
    1478           4 :         data->locktags[el] = predlock->tag.myTarget->tag;
    1479           4 :         data->xacts[el] = *predlock->tag.myXact;
    1480           4 :         el++;
    1481             :     }
    1482             : 
    1483             :     Assert(el == els);
    1484             : 
    1485             :     /* Release locks in reverse order */
    1486         260 :     LWLockRelease(SerializableXactHashLock);
    1487        4420 :     for (i = NUM_PREDICATELOCK_PARTITIONS - 1; i >= 0; i--)
    1488        4160 :         LWLockRelease(PredicateLockHashPartitionLockByIndex(i));
    1489             : 
    1490         260 :     return data;
    1491             : }
    1492             : 
    1493             : /*
    1494             :  * Free up shared memory structures by pushing the oldest sxact (the one at
    1495             :  * the front of the SummarizeOldestCommittedSxact queue) into summary form.
    1496             :  * Each call will free exactly one SERIALIZABLEXACT structure and may also
    1497             :  * free one or more of these structures: SERIALIZABLEXID, PREDICATELOCK,
    1498             :  * PREDICATELOCKTARGET, RWConflictData.
    1499             :  */
    1500             : static void
    1501           0 : SummarizeOldestCommittedSxact(void)
    1502             : {
    1503             :     SERIALIZABLEXACT *sxact;
    1504             : 
    1505           0 :     LWLockAcquire(SerializableFinishedListLock, LW_EXCLUSIVE);
    1506             : 
    1507             :     /*
    1508             :      * This function is only called if there are no sxact slots available.
    1509             :      * Some of them must belong to old, already-finished transactions, so
    1510             :      * there should be something in FinishedSerializableTransactions list that
    1511             :      * we can summarize. However, there's a race condition: while we were not
    1512             :      * holding any locks, a transaction might have ended and cleaned up all
    1513             :      * the finished sxact entries already, freeing up their sxact slots. In
    1514             :      * that case, we have nothing to do here. The caller will find one of the
    1515             :      * slots released by the other backend when it retries.
    1516             :      */
    1517           0 :     if (SHMQueueEmpty(FinishedSerializableTransactions))
    1518             :     {
    1519           0 :         LWLockRelease(SerializableFinishedListLock);
    1520           0 :         return;
    1521             :     }
    1522             : 
    1523             :     /*
    1524             :      * Grab the first sxact off the finished list -- this will be the earliest
    1525             :      * commit.  Remove it from the list.
    1526             :      */
    1527             :     sxact = (SERIALIZABLEXACT *)
    1528           0 :         SHMQueueNext(FinishedSerializableTransactions,
    1529             :                      FinishedSerializableTransactions,
    1530             :                      offsetof(SERIALIZABLEXACT, finishedLink));
    1531           0 :     SHMQueueDelete(&(sxact->finishedLink));
    1532             : 
    1533             :     /* Add to SLRU summary information. */
    1534           0 :     if (TransactionIdIsValid(sxact->topXid) && !SxactIsReadOnly(sxact))
    1535           0 :         SerialAdd(sxact->topXid, SxactHasConflictOut(sxact)
    1536             :                   ? sxact->SeqNo.earliestOutConflictCommit : InvalidSerCommitSeqNo);
    1537             : 
    1538             :     /* Summarize and release the detail. */
    1539           0 :     ReleaseOneSerializableXact(sxact, false, true);
    1540             : 
    1541           0 :     LWLockRelease(SerializableFinishedListLock);
    1542             : }
    1543             : 
    1544             : /*
    1545             :  * GetSafeSnapshot
    1546             :  *      Obtain and register a snapshot for a READ ONLY DEFERRABLE
    1547             :  *      transaction. Ensures that the snapshot is "safe", i.e. a
    1548             :  *      read-only transaction running on it can execute serializably
    1549             :  *      without further checks. This requires waiting for concurrent
    1550             :  *      transactions to complete, and retrying with a new snapshot if
    1551             :  *      one of them could possibly create a conflict.
    1552             :  *
    1553             :  *      As with GetSerializableTransactionSnapshot (which this is a subroutine
    1554             :  *      for), the passed-in Snapshot pointer should reference a static data
    1555             :  *      area that can safely be passed to GetSnapshotData.
    1556             :  */
    1557             : static Snapshot
    1558           8 : GetSafeSnapshot(Snapshot origSnapshot)
    1559             : {
    1560             :     Snapshot    snapshot;
    1561             : 
    1562             :     Assert(XactReadOnly && XactDeferrable);
    1563             : 
    1564             :     while (true)
    1565             :     {
    1566             :         /*
    1567             :          * GetSerializableTransactionSnapshotInt is going to call
    1568             :          * GetSnapshotData, so we need to provide it the static snapshot area
    1569             :          * our caller passed to us.  The pointer returned is actually the same
    1570             :          * one passed to it, but we avoid assuming that here.
    1571             :          */
    1572           8 :         snapshot = GetSerializableTransactionSnapshotInt(origSnapshot,
    1573             :                                                          NULL, InvalidPid);
    1574             : 
    1575           8 :         if (MySerializableXact == InvalidSerializableXact)
    1576           4 :             return snapshot;    /* no concurrent r/w xacts; it's safe */
    1577             : 
    1578           4 :         LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    1579             : 
    1580             :         /*
    1581             :          * Wait for concurrent transactions to finish. Stop early if one of
    1582             :          * them marked us as conflicted.
    1583             :          */
    1584           4 :         MySerializableXact->flags |= SXACT_FLAG_DEFERRABLE_WAITING;
    1585          10 :         while (!(SHMQueueEmpty(&MySerializableXact->possibleUnsafeConflicts) ||
    1586           6 :                  SxactIsROUnsafe(MySerializableXact)))
    1587             :         {
    1588           6 :             LWLockRelease(SerializableXactHashLock);
    1589           6 :             ProcWaitForSignal(WAIT_EVENT_SAFE_SNAPSHOT);
    1590           6 :             LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    1591             :         }
    1592           4 :         MySerializableXact->flags &= ~SXACT_FLAG_DEFERRABLE_WAITING;
    1593             : 
    1594           4 :         if (!SxactIsROUnsafe(MySerializableXact))
    1595             :         {
    1596           2 :             LWLockRelease(SerializableXactHashLock);
    1597           2 :             break;              /* success */
    1598             :         }
    1599             : 
    1600           2 :         LWLockRelease(SerializableXactHashLock);
    1601             : 
    1602             :         /* else, need to retry... */
    1603           2 :         ereport(DEBUG2,
    1604             :                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    1605             :                  errmsg_internal("deferrable snapshot was unsafe; trying a new one")));
    1606           2 :         ReleasePredicateLocks(false, false);
    1607             :     }
    1608             : 
    1609             :     /*
    1610             :      * Now we have a safe snapshot, so we don't need to do any further checks.
    1611             :      */
    1612             :     Assert(SxactIsROSafe(MySerializableXact));
    1613           2 :     ReleasePredicateLocks(false, true);
    1614             : 
    1615           2 :     return snapshot;
    1616             : }
    1617             : 
    1618             : /*
    1619             :  * GetSafeSnapshotBlockingPids
    1620             :  *      If the specified process is currently blocked in GetSafeSnapshot,
    1621             :  *      write the process IDs of all processes that it is blocked by
    1622             :  *      into the caller-supplied buffer output[].  The list is truncated at
    1623             :  *      output_size, and the number of PIDs written into the buffer is
    1624             :  *      returned.  Returns zero if the given PID is not currently blocked
    1625             :  *      in GetSafeSnapshot.
    1626             :  */
    1627             : int
    1628       12010 : GetSafeSnapshotBlockingPids(int blocked_pid, int *output, int output_size)
    1629             : {
    1630       12010 :     int         num_written = 0;
    1631             :     SERIALIZABLEXACT *sxact;
    1632             : 
    1633       12010 :     LWLockAcquire(SerializableXactHashLock, LW_SHARED);
    1634             : 
    1635             :     /* Find blocked_pid's SERIALIZABLEXACT by linear search. */
    1636       24124 :     for (sxact = FirstPredXact(); sxact != NULL; sxact = NextPredXact(sxact))
    1637             :     {
    1638       12248 :         if (sxact->pid == blocked_pid)
    1639         134 :             break;
    1640             :     }
    1641             : 
    1642             :     /* Did we find it, and is it currently waiting in GetSafeSnapshot? */
    1643       12010 :     if (sxact != NULL && SxactIsDeferrableWaiting(sxact))
    1644             :     {
    1645             :         RWConflict  possibleUnsafeConflict;
    1646             : 
    1647             :         /* Traverse the list of possible unsafe conflicts collecting PIDs. */
    1648             :         possibleUnsafeConflict = (RWConflict)
    1649           4 :             SHMQueueNext(&sxact->possibleUnsafeConflicts,
    1650           4 :                          &sxact->possibleUnsafeConflicts,
    1651             :                          offsetof(RWConflictData, inLink));
    1652             : 
    1653           8 :         while (possibleUnsafeConflict != NULL && num_written < output_size)
    1654             :         {
    1655           4 :             output[num_written++] = possibleUnsafeConflict->sxactOut->pid;
    1656             :             possibleUnsafeConflict = (RWConflict)
    1657           4 :                 SHMQueueNext(&sxact->possibleUnsafeConflicts,
    1658           4 :                              &possibleUnsafeConflict->inLink,
    1659             :                              offsetof(RWConflictData, inLink));
    1660             :         }
    1661             :     }
    1662             : 
    1663       12010 :     LWLockRelease(SerializableXactHashLock);
    1664             : 
    1665       12010 :     return num_written;
    1666             : }
    1667             : 
    1668             : /*
    1669             :  * Acquire a snapshot that can be used for the current transaction.
    1670             :  *
    1671             :  * Make sure we have a SERIALIZABLEXACT reference in MySerializableXact.
    1672             :  * It should be current for this process and be contained in PredXact.
    1673             :  *
    1674             :  * The passed-in Snapshot pointer should reference a static data area that
    1675             :  * can safely be passed to GetSnapshotData.  The return value is actually
    1676             :  * always this same pointer; no new snapshot data structure is allocated
    1677             :  * within this function.
    1678             :  */
    1679             : Snapshot
    1680        3112 : GetSerializableTransactionSnapshot(Snapshot snapshot)
    1681             : {
    1682             :     Assert(IsolationIsSerializable());
    1683             : 
    1684             :     /*
    1685             :      * Can't use serializable mode while recovery is still active, as it is,
    1686             :      * for example, on a hot standby.  We could get here despite the check in
    1687             :      * check_XactIsoLevel() if default_transaction_isolation is set to
    1688             :      * serializable, so phrase the hint accordingly.
    1689             :      */
    1690        3112 :     if (RecoveryInProgress())
    1691           0 :         ereport(ERROR,
    1692             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1693             :                  errmsg("cannot use serializable mode in a hot standby"),
    1694             :                  errdetail("\"default_transaction_isolation\" is set to \"serializable\"."),
    1695             :                  errhint("You can use \"SET default_transaction_isolation = 'repeatable read'\" to change the default.")));
    1696             : 
    1697             :     /*
    1698             :      * A special optimization is available for SERIALIZABLE READ ONLY
    1699             :      * DEFERRABLE transactions -- we can wait for a suitable snapshot and
    1700             :      * thereby avoid all SSI overhead once it's running.
    1701             :      */
    1702        3112 :     if (XactReadOnly && XactDeferrable)
    1703           6 :         return GetSafeSnapshot(snapshot);
    1704             : 
    1705        3106 :     return GetSerializableTransactionSnapshotInt(snapshot,
    1706             :                                                  NULL, InvalidPid);
    1707             : }
    1708             : 
    1709             : /*
    1710             :  * Import a snapshot to be used for the current transaction.
    1711             :  *
    1712             :  * This is nearly the same as GetSerializableTransactionSnapshot, except that
    1713             :  * we don't take a new snapshot, but rather use the data we're handed.
    1714             :  *
    1715             :  * The caller must have verified that the snapshot came from a serializable
    1716             :  * transaction; and if we're read-write, the source transaction must not be
    1717             :  * read-only.
    1718             :  */
    1719             : void
    1720          10 : SetSerializableTransactionSnapshot(Snapshot snapshot,
    1721             :                                    VirtualTransactionId *sourcevxid,
    1722             :                                    int sourcepid)
    1723             : {
    1724             :     Assert(IsolationIsSerializable());
    1725             : 
    1726             :     /*
    1727             :      * If this is called by parallel.c in a parallel worker, we don't want to
    1728             :      * create a SERIALIZABLEXACT just yet because the leader's
    1729             :      * SERIALIZABLEXACT will be installed with AttachSerializableXact().  We
    1730             :      * also don't want to reject SERIALIZABLE READ ONLY DEFERRABLE in this
    1731             :      * case, because the leader has already determined that the snapshot it
    1732             :      * has passed us is safe.  So there is nothing for us to do.
    1733             :      */
    1734          10 :     if (IsParallelWorker())
    1735          10 :         return;
    1736             : 
    1737             :     /*
    1738             :      * We do not allow SERIALIZABLE READ ONLY DEFERRABLE transactions to
    1739             :      * import snapshots, since there's no way to wait for a safe snapshot when
    1740             :      * we're using the snap we're told to.  (XXX instead of throwing an error,
    1741             :      * we could just ignore the XactDeferrable flag?)
    1742             :      */
    1743           0 :     if (XactReadOnly && XactDeferrable)
    1744           0 :         ereport(ERROR,
    1745             :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    1746             :                  errmsg("a snapshot-importing transaction must not be READ ONLY DEFERRABLE")));
    1747             : 
    1748           0 :     (void) GetSerializableTransactionSnapshotInt(snapshot, sourcevxid,
    1749             :                                                  sourcepid);
    1750             : }
    1751             : 
    1752             : /*
    1753             :  * Guts of GetSerializableTransactionSnapshot
    1754             :  *
    1755             :  * If sourcevxid is valid, this is actually an import operation and we should
    1756             :  * skip calling GetSnapshotData, because the snapshot contents are already
    1757             :  * loaded up.  HOWEVER: to avoid race conditions, we must check that the
    1758             :  * source xact is still running after we acquire SerializableXactHashLock.
    1759             :  * We do that by calling ProcArrayInstallImportedXmin.
    1760             :  */
    1761             : static Snapshot
    1762        3114 : GetSerializableTransactionSnapshotInt(Snapshot snapshot,
    1763             :                                       VirtualTransactionId *sourcevxid,
    1764             :                                       int sourcepid)
    1765             : {
    1766             :     PGPROC     *proc;
    1767             :     VirtualTransactionId vxid;
    1768             :     SERIALIZABLEXACT *sxact,
    1769             :                *othersxact;
    1770             : 
    1771             :     /* We only do this for serializable transactions.  Once. */
    1772             :     Assert(MySerializableXact == InvalidSerializableXact);
    1773             : 
    1774             :     Assert(!RecoveryInProgress());
    1775             : 
    1776             :     /*
    1777             :      * Since all parts of a serializable transaction must use the same
    1778             :      * snapshot, it is too late to establish one after a parallel operation
    1779             :      * has begun.
    1780             :      */
    1781        3114 :     if (IsInParallelMode())
    1782           0 :         elog(ERROR, "cannot establish serializable snapshot during a parallel operation");
    1783             : 
    1784        3114 :     proc = MyProc;
    1785             :     Assert(proc != NULL);
    1786        3114 :     GET_VXID_FROM_PGPROC(vxid, *proc);
    1787             : 
    1788             :     /*
    1789             :      * First we get the sxact structure, which may involve looping and access
    1790             :      * to the "finished" list to free a structure for use.
    1791             :      *
    1792             :      * We must hold SerializableXactHashLock when taking/checking the snapshot
    1793             :      * to avoid race conditions, for much the same reasons that
    1794             :      * GetSnapshotData takes the ProcArrayLock.  Since we might have to
    1795             :      * release SerializableXactHashLock to call SummarizeOldestCommittedSxact,
    1796             :      * this means we have to create the sxact first, which is a bit annoying
    1797             :      * (in particular, an elog(ERROR) in procarray.c would cause us to leak
    1798             :      * the sxact).  Consider refactoring to avoid this.
    1799             :      */
    1800             : #ifdef TEST_SUMMARIZE_SERIAL
    1801             :     SummarizeOldestCommittedSxact();
    1802             : #endif
    1803        3114 :     LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    1804             :     do
    1805             :     {
    1806        3114 :         sxact = CreatePredXact();
    1807             :         /* If null, push out committed sxact to SLRU summary & retry. */
    1808        3114 :         if (!sxact)
    1809             :         {
    1810           0 :             LWLockRelease(SerializableXactHashLock);
    1811           0 :             SummarizeOldestCommittedSxact();
    1812           0 :             LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    1813             :         }
    1814        3114 :     } while (!sxact);
    1815             : 
    1816             :     /* Get the snapshot, or check that it's safe to use */
    1817        3114 :     if (!sourcevxid)
    1818        3114 :         snapshot = GetSnapshotData(snapshot);
    1819           0 :     else if (!ProcArrayInstallImportedXmin(snapshot->xmin, sourcevxid))
    1820             :     {
    1821           0 :         ReleasePredXact(sxact);
    1822           0 :         LWLockRelease(SerializableXactHashLock);
    1823           0 :         ereport(ERROR,
    1824             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
    1825             :                  errmsg("could not import the requested snapshot"),
    1826             :                  errdetail("The source process with PID %d is not running anymore.",
    1827             :                            sourcepid)));
    1828             :     }
    1829             : 
    1830             :     /*
    1831             :      * If there are no serializable transactions which are not read-only, we
    1832             :      * can "opt out" of predicate locking and conflict checking for a
    1833             :      * read-only transaction.
    1834             :      *
    1835             :      * The reason this is safe is that a read-only transaction can only become
    1836             :      * part of a dangerous structure if it overlaps a writable transaction
    1837             :      * which in turn overlaps a writable transaction which committed before
    1838             :      * the read-only transaction started.  A new writable transaction can
    1839             :      * overlap this one, but it can't meet the other condition of overlapping
    1840             :      * a transaction which committed before this one started.
    1841             :      */
    1842        3114 :     if (XactReadOnly && PredXact->WritableSxactCount == 0)
    1843             :     {
    1844         220 :         ReleasePredXact(sxact);
    1845         220 :         LWLockRelease(SerializableXactHashLock);
    1846         220 :         return snapshot;
    1847             :     }
    1848             : 
    1849             :     /* Maintain serializable global xmin info. */
    1850        2894 :     if (!TransactionIdIsValid(PredXact->SxactGlobalXmin))
    1851             :     {
    1852             :         Assert(PredXact->SxactGlobalXminCount == 0);
    1853        1554 :         PredXact->SxactGlobalXmin = snapshot->xmin;
    1854        1554 :         PredXact->SxactGlobalXminCount = 1;
    1855        1554 :         SerialSetActiveSerXmin(snapshot->xmin);
    1856             :     }
    1857        1340 :     else if (TransactionIdEquals(snapshot->xmin, PredXact->SxactGlobalXmin))
    1858             :     {
    1859             :         Assert(PredXact->SxactGlobalXminCount > 0);
    1860        1270 :         PredXact->SxactGlobalXminCount++;
    1861             :     }
    1862             :     else
    1863             :     {
    1864             :         Assert(TransactionIdFollows(snapshot->xmin, PredXact->SxactGlobalXmin));
    1865             :     }
    1866             : 
    1867             :     /* Initialize the structure. */
    1868        2894 :     sxact->vxid = vxid;
    1869        2894 :     sxact->SeqNo.lastCommitBeforeSnapshot = PredXact->LastSxactCommitSeqNo;
    1870        2894 :     sxact->prepareSeqNo = InvalidSerCommitSeqNo;
    1871        2894 :     sxact->commitSeqNo = InvalidSerCommitSeqNo;
    1872        2894 :     SHMQueueInit(&(sxact->outConflicts));
    1873        2894 :     SHMQueueInit(&(sxact->inConflicts));
    1874        2894 :     SHMQueueInit(&(sxact->possibleUnsafeConflicts));
    1875        2894 :     sxact->topXid = GetTopTransactionIdIfAny();
    1876        2894 :     sxact->finishedBefore = InvalidTransactionId;
    1877        2894 :     sxact->xmin = snapshot->xmin;
    1878        2894 :     sxact->pid = MyProcPid;
    1879        2894 :     SHMQueueInit(&(sxact->predicateLocks));
    1880        2894 :     SHMQueueElemInit(&(sxact->finishedLink));
    1881        2894 :     sxact->flags = 0;
    1882        2894 :     if (XactReadOnly)
    1883             :     {
    1884         210 :         sxact->flags |= SXACT_FLAG_READ_ONLY;
    1885             : 
    1886             :         /*
    1887             :          * Register all concurrent r/w transactions as possible conflicts; if
    1888             :          * all of them commit without any outgoing conflicts to earlier
    1889             :          * transactions then this snapshot can be deemed safe (and we can run
    1890             :          * without tracking predicate locks).
    1891             :          */
    1892         918 :         for (othersxact = FirstPredXact();
    1893             :              othersxact != NULL;
    1894         708 :              othersxact = NextPredXact(othersxact))
    1895             :         {
    1896         708 :             if (!SxactIsCommitted(othersxact)
    1897         470 :                 && !SxactIsDoomed(othersxact)
    1898         470 :                 && !SxactIsReadOnly(othersxact))
    1899             :             {
    1900         260 :                 SetPossibleUnsafeConflict(sxact, othersxact);
    1901             :             }
    1902             :         }
    1903             :     }
    1904             :     else
    1905             :     {
    1906        2684 :         ++(PredXact->WritableSxactCount);
    1907             :         Assert(PredXact->WritableSxactCount <=
    1908             :                (MaxBackends + max_prepared_xacts));
    1909             :     }
    1910             : 
    1911        2894 :     MySerializableXact = sxact;
    1912        2894 :     MyXactDidWrite = false;     /* haven't written anything yet */
    1913             : 
    1914        2894 :     LWLockRelease(SerializableXactHashLock);
    1915             : 
    1916        2894 :     CreateLocalPredicateLockHash();
    1917             : 
    1918        2894 :     return snapshot;
    1919             : }
    1920             : 
    1921             : static void
    1922        2904 : CreateLocalPredicateLockHash(void)
    1923             : {
    1924             :     HASHCTL     hash_ctl;
    1925             : 
    1926             :     /* Initialize the backend-local hash table of parent locks */
    1927             :     Assert(LocalPredicateLockHash == NULL);
    1928        2904 :     hash_ctl.keysize = sizeof(PREDICATELOCKTARGETTAG);
    1929        2904 :     hash_ctl.entrysize = sizeof(LOCALPREDICATELOCK);
    1930        2904 :     LocalPredicateLockHash = hash_create("Local predicate lock",
    1931             :                                          max_predicate_locks_per_xact,
    1932             :                                          &hash_ctl,
    1933             :                                          HASH_ELEM | HASH_BLOBS);
    1934        2904 : }
    1935             : 
    1936             : /*
    1937             :  * Register the top level XID in SerializableXidHash.
    1938             :  * Also store it for easy reference in MySerializableXact.
    1939             :  */
    1940             : void
    1941      454892 : RegisterPredicateLockingXid(TransactionId xid)
    1942             : {
    1943             :     SERIALIZABLEXIDTAG sxidtag;
    1944             :     SERIALIZABLEXID *sxid;
    1945             :     bool        found;
    1946             : 
    1947             :     /*
    1948             :      * If we're not tracking predicate lock data for this transaction, we
    1949             :      * should ignore the request and return quickly.
    1950             :      */
    1951      454892 :     if (MySerializableXact == InvalidSerializableXact)
    1952      452428 :         return;
    1953             : 
    1954             :     /* We should have a valid XID and be at the top level. */
    1955             :     Assert(TransactionIdIsValid(xid));
    1956             : 
    1957        2464 :     LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    1958             : 
    1959             :     /* This should only be done once per transaction. */
    1960             :     Assert(MySerializableXact->topXid == InvalidTransactionId);
    1961             : 
    1962        2464 :     MySerializableXact->topXid = xid;
    1963             : 
    1964        2464 :     sxidtag.xid = xid;
    1965        2464 :     sxid = (SERIALIZABLEXID *) hash_search(SerializableXidHash,
    1966             :                                            &sxidtag,
    1967             :                                            HASH_ENTER, &found);
    1968             :     Assert(!found);
    1969             : 
    1970             :     /* Initialize the structure. */
    1971        2464 :     sxid->myXact = MySerializableXact;
    1972        2464 :     LWLockRelease(SerializableXactHashLock);
    1973             : }
    1974             : 
    1975             : 
    1976             : /*
    1977             :  * Check whether there are any predicate locks held by any transaction
    1978             :  * for the page at the given block number.
    1979             :  *
    1980             :  * Note that the transaction may be completed but not yet subject to
    1981             :  * cleanup due to overlapping serializable transactions.  This must
    1982             :  * return valid information regardless of transaction isolation level.
    1983             :  *
    1984             :  * Also note that this doesn't check for a conflicting relation lock,
    1985             :  * just a lock specifically on the given page.
    1986             :  *
    1987             :  * One use is to support proper behavior during GiST index vacuum.
    1988             :  */
    1989             : bool
    1990           0 : PageIsPredicateLocked(Relation relation, BlockNumber blkno)
    1991             : {
    1992             :     PREDICATELOCKTARGETTAG targettag;
    1993             :     uint32      targettaghash;
    1994             :     LWLock     *partitionLock;
    1995             :     PREDICATELOCKTARGET *target;
    1996             : 
    1997           0 :     SET_PREDICATELOCKTARGETTAG_PAGE(targettag,
    1998             :                                     relation->rd_node.dbNode,
    1999             :                                     relation->rd_id,
    2000             :                                     blkno);
    2001             : 
    2002           0 :     targettaghash = PredicateLockTargetTagHashCode(&targettag);
    2003           0 :     partitionLock = PredicateLockHashPartitionLock(targettaghash);
    2004           0 :     LWLockAcquire(partitionLock, LW_SHARED);
    2005             :     target = (PREDICATELOCKTARGET *)
    2006           0 :         hash_search_with_hash_value(PredicateLockTargetHash,
    2007             :                                     &targettag, targettaghash,
    2008             :                                     HASH_FIND, NULL);
    2009           0 :     LWLockRelease(partitionLock);
    2010             : 
    2011           0 :     return (target != NULL);
    2012             : }
    2013             : 
    2014             : 
    2015             : /*
    2016             :  * Check whether a particular lock is held by this transaction.
    2017             :  *
    2018             :  * Important note: this function may return false even if the lock is
    2019             :  * being held, because it uses the local lock table which is not
    2020             :  * updated if another transaction modifies our lock list (e.g. to
    2021             :  * split an index page). It can also return true when a coarser
    2022             :  * granularity lock that covers this target is being held. Be careful
    2023             :  * to only use this function in circumstances where such errors are
    2024             :  * acceptable!
    2025             :  */
    2026             : static bool
    2027      153648 : PredicateLockExists(const PREDICATELOCKTARGETTAG *targettag)
    2028             : {
    2029             :     LOCALPREDICATELOCK *lock;
    2030             : 
    2031             :     /* check local hash table */
    2032      153648 :     lock = (LOCALPREDICATELOCK *) hash_search(LocalPredicateLockHash,
    2033             :                                               targettag,
    2034             :                                               HASH_FIND, NULL);
    2035             : 
    2036      153648 :     if (!lock)
    2037       59868 :         return false;
    2038             : 
    2039             :     /*
    2040             :      * Found entry in the table, but still need to check whether it's actually
    2041             :      * held -- it could just be a parent of some held lock.
    2042             :      */
    2043       93780 :     return lock->held;
    2044             : }
    2045             : 
    2046             : /*
    2047             :  * Return the parent lock tag in the lock hierarchy: the next coarser
    2048             :  * lock that covers the provided tag.
    2049             :  *
    2050             :  * Returns true and sets *parent to the parent tag if one exists,
    2051             :  * returns false if none exists.
    2052             :  */
    2053             : static bool
    2054       89838 : GetParentPredicateLockTag(const PREDICATELOCKTARGETTAG *tag,
    2055             :                           PREDICATELOCKTARGETTAG *parent)
    2056             : {
    2057       89838 :     switch (GET_PREDICATELOCKTARGETTAG_TYPE(*tag))
    2058             :     {
    2059       19386 :         case PREDLOCKTAG_RELATION:
    2060             :             /* relation locks have no parent lock */
    2061       19386 :             return false;
    2062             : 
    2063       16802 :         case PREDLOCKTAG_PAGE:
    2064             :             /* parent lock is relation lock */
    2065       16802 :             SET_PREDICATELOCKTARGETTAG_RELATION(*parent,
    2066             :                                                 GET_PREDICATELOCKTARGETTAG_DB(*tag),
    2067             :                                                 GET_PREDICATELOCKTARGETTAG_RELATION(*tag));
    2068             : 
    2069       16802 :             return true;
    2070             : 
    2071       53650 :         case PREDLOCKTAG_TUPLE:
    2072             :             /* parent lock is page lock */
    2073       53650 :             SET_PREDICATELOCKTARGETTAG_PAGE(*parent,
    2074             :                                             GET_PREDICATELOCKTARGETTAG_DB(*tag),
    2075             :                                             GET_PREDICATELOCKTARGETTAG_RELATION(*tag),
    2076             :                                             GET_PREDICATELOCKTARGETTAG_PAGE(*tag));
    2077       53650 :             return true;
    2078             :     }
    2079             : 
    2080             :     /* not reachable */
    2081             :     Assert(false);
    2082           0 :     return false;
    2083             : }
    2084             : 
    2085             : /*
    2086             :  * Check whether the lock we are considering is already covered by a
    2087             :  * coarser lock for our transaction.
    2088             :  *
    2089             :  * Like PredicateLockExists, this function might return a false
    2090             :  * negative, but it will never return a false positive.
    2091             :  */
    2092             : static bool
    2093       51764 : CoarserLockCovers(const PREDICATELOCKTARGETTAG *newtargettag)
    2094             : {
    2095             :     PREDICATELOCKTARGETTAG targettag,
    2096             :                 parenttag;
    2097             : 
    2098       51764 :     targettag = *newtargettag;
    2099             : 
    2100             :     /* check parents iteratively until no more */
    2101       62512 :     while (GetParentPredicateLockTag(&targettag, &parenttag))
    2102             :     {
    2103       54196 :         targettag = parenttag;
    2104       54196 :         if (PredicateLockExists(&targettag))
    2105       43448 :             return true;
    2106             :     }
    2107             : 
    2108             :     /* no more parents to check; lock is not covered */
    2109        8316 :     return false;
    2110             : }
    2111             : 
    2112             : /*
    2113             :  * Remove the dummy entry from the predicate lock target hash, to free up some
    2114             :  * scratch space. The caller must be holding SerializablePredicateListLock,
    2115             :  * and must restore the entry with RestoreScratchTarget() before releasing the
    2116             :  * lock.
    2117             :  *
    2118             :  * If lockheld is true, the caller is already holding the partition lock
    2119             :  * of the partition containing the scratch entry.
    2120             :  */
    2121             : static void
    2122          52 : RemoveScratchTarget(bool lockheld)
    2123             : {
    2124             :     bool        found;
    2125             : 
    2126             :     Assert(LWLockHeldByMe(SerializablePredicateListLock));
    2127             : 
    2128          52 :     if (!lockheld)
    2129           0 :         LWLockAcquire(ScratchPartitionLock, LW_EXCLUSIVE);
    2130          52 :     hash_search_with_hash_value(PredicateLockTargetHash,
    2131             :                                 &ScratchTargetTag,
    2132             :                                 ScratchTargetTagHash,
    2133             :                                 HASH_REMOVE, &found);
    2134             :     Assert(found);
    2135          52 :     if (!lockheld)
    2136           0 :         LWLockRelease(ScratchPartitionLock);
    2137          52 : }
    2138             : 
    2139             : /*
    2140             :  * Re-insert the dummy entry in predicate lock target hash.
    2141             :  */
    2142             : static void
    2143          52 : RestoreScratchTarget(bool lockheld)
    2144             : {
    2145             :     bool        found;
    2146             : 
    2147             :     Assert(LWLockHeldByMe(SerializablePredicateListLock));
    2148             : 
    2149          52 :     if (!lockheld)
    2150           0 :         LWLockAcquire(ScratchPartitionLock, LW_EXCLUSIVE);
    2151          52 :     hash_search_with_hash_value(PredicateLockTargetHash,
    2152             :                                 &ScratchTargetTag,
    2153             :                                 ScratchTargetTagHash,
    2154             :                                 HASH_ENTER, &found);
    2155             :     Assert(!found);
    2156          52 :     if (!lockheld)
    2157           0 :         LWLockRelease(ScratchPartitionLock);
    2158          52 : }
    2159             : 
    2160             : /*
    2161             :  * Check whether the list of related predicate locks is empty for a
    2162             :  * predicate lock target, and remove the target if it is.
    2163             :  */
    2164             : static void
    2165        8312 : RemoveTargetIfNoLongerUsed(PREDICATELOCKTARGET *target, uint32 targettaghash)
    2166             : {
    2167             :     PREDICATELOCKTARGET *rmtarget PG_USED_FOR_ASSERTS_ONLY;
    2168             : 
    2169             :     Assert(LWLockHeldByMe(SerializablePredicateListLock));
    2170             : 
    2171             :     /* Can't remove it until no locks at this target. */
    2172        8312 :     if (!SHMQueueEmpty(&target->predicateLocks))
    2173        1892 :         return;
    2174             : 
    2175             :     /* Actually remove the target. */
    2176        6420 :     rmtarget = hash_search_with_hash_value(PredicateLockTargetHash,
    2177        6420 :                                            &target->tag,
    2178             :                                            targettaghash,
    2179             :                                            HASH_REMOVE, NULL);
    2180             :     Assert(rmtarget == target);
    2181             : }
    2182             : 
    2183             : /*
    2184             :  * Delete child target locks owned by this process.
    2185             :  * This implementation is assuming that the usage of each target tag field
    2186             :  * is uniform.  No need to make this hard if we don't have to.
    2187             :  *
    2188             :  * We acquire an LWLock in the case of parallel mode, because worker
    2189             :  * backends have access to the leader's SERIALIZABLEXACT.  Otherwise,
    2190             :  * we aren't acquiring LWLocks for the predicate lock or lock
    2191             :  * target structures associated with this transaction unless we're going
    2192             :  * to modify them, because no other process is permitted to modify our
    2193             :  * locks.
    2194             :  */
    2195             : static void
    2196        4592 : DeleteChildTargetLocks(const PREDICATELOCKTARGETTAG *newtargettag)
    2197             : {
    2198             :     SERIALIZABLEXACT *sxact;
    2199             :     PREDICATELOCK *predlock;
    2200             : 
    2201        4592 :     LWLockAcquire(SerializablePredicateListLock, LW_SHARED);
    2202        4592 :     sxact = MySerializableXact;
    2203        4592 :     if (IsInParallelMode())
    2204           8 :         LWLockAcquire(&sxact->perXactPredicateListLock, LW_EXCLUSIVE);
    2205             :     predlock = (PREDICATELOCK *)
    2206        4592 :         SHMQueueNext(&(sxact->predicateLocks),
    2207        4592 :                      &(sxact->predicateLocks),
    2208             :                      offsetof(PREDICATELOCK, xactLink));
    2209       15308 :     while (predlock)
    2210             :     {
    2211             :         SHM_QUEUE  *predlocksxactlink;
    2212             :         PREDICATELOCK *nextpredlock;
    2213             :         PREDICATELOCKTAG oldlocktag;
    2214             :         PREDICATELOCKTARGET *oldtarget;
    2215             :         PREDICATELOCKTARGETTAG oldtargettag;
    2216             : 
    2217       10716 :         predlocksxactlink = &(predlock->xactLink);
    2218             :         nextpredlock = (PREDICATELOCK *)
    2219       10716 :             SHMQueueNext(&(sxact->predicateLocks),
    2220             :                          predlocksxactlink,
    2221             :                          offsetof(PREDICATELOCK, xactLink));
    2222             : 
    2223       10716 :         oldlocktag = predlock->tag;
    2224             :         Assert(oldlocktag.myXact == sxact);
    2225       10716 :         oldtarget = oldlocktag.myTarget;
    2226       10716 :         oldtargettag = oldtarget->tag;
    2227             : 
    2228       10716 :         if (TargetTagIsCoveredBy(oldtargettag, *newtargettag))
    2229             :         {
    2230             :             uint32      oldtargettaghash;
    2231             :             LWLock     *partitionLock;
    2232             :             PREDICATELOCK *rmpredlock PG_USED_FOR_ASSERTS_ONLY;
    2233             : 
    2234        1992 :             oldtargettaghash = PredicateLockTargetTagHashCode(&oldtargettag);
    2235        1992 :             partitionLock = PredicateLockHashPartitionLock(oldtargettaghash);
    2236             : 
    2237        1992 :             LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    2238             : 
    2239        1992 :             SHMQueueDelete(predlocksxactlink);
    2240        1992 :             SHMQueueDelete(&(predlock->targetLink));
    2241        1992 :             rmpredlock = hash_search_with_hash_value
    2242             :                 (PredicateLockHash,
    2243             :                  &oldlocktag,
    2244        1992 :                  PredicateLockHashCodeFromTargetHashCode(&oldlocktag,
    2245             :                                                          oldtargettaghash),
    2246             :                  HASH_REMOVE, NULL);
    2247             :             Assert(rmpredlock == predlock);
    2248             : 
    2249        1992 :             RemoveTargetIfNoLongerUsed(oldtarget, oldtargettaghash);
    2250             : 
    2251        1992 :             LWLockRelease(partitionLock);
    2252             : 
    2253        1992 :             DecrementParentLocks(&oldtargettag);
    2254             :         }
    2255             : 
    2256       10716 :         predlock = nextpredlock;
    2257             :     }
    2258        4592 :     if (IsInParallelMode())
    2259           8 :         LWLockRelease(&sxact->perXactPredicateListLock);
    2260        4592 :     LWLockRelease(SerializablePredicateListLock);
    2261        4592 : }
    2262             : 
    2263             : /*
    2264             :  * Returns the promotion limit for a given predicate lock target.  This is the
    2265             :  * max number of descendant locks allowed before promoting to the specified
    2266             :  * tag. Note that the limit includes non-direct descendants (e.g., both tuples
    2267             :  * and pages for a relation lock).
    2268             :  *
    2269             :  * Currently the default limit is 2 for a page lock, and half of the value of
    2270             :  * max_pred_locks_per_transaction - 1 for a relation lock, to match behavior
    2271             :  * of earlier releases when upgrading.
    2272             :  *
    2273             :  * TODO SSI: We should probably add additional GUCs to allow a maximum ratio
    2274             :  * of page and tuple locks based on the pages in a relation, and the maximum
    2275             :  * ratio of tuple locks to tuples in a page.  This would provide more
    2276             :  * generally "balanced" allocation of locks to where they are most useful,
    2277             :  * while still allowing the absolute numbers to prevent one relation from
    2278             :  * tying up all predicate lock resources.
    2279             :  */
    2280             : static int
    2281       10748 : MaxPredicateChildLocks(const PREDICATELOCKTARGETTAG *tag)
    2282             : {
    2283       10748 :     switch (GET_PREDICATELOCKTARGETTAG_TYPE(*tag))
    2284             :     {
    2285        7024 :         case PREDLOCKTAG_RELATION:
    2286        7024 :             return max_predicate_locks_per_relation < 0
    2287             :                 ? (max_predicate_locks_per_xact
    2288        7024 :                    / (-max_predicate_locks_per_relation)) - 1
    2289        7024 :                 : max_predicate_locks_per_relation;
    2290             : 
    2291        3724 :         case PREDLOCKTAG_PAGE:
    2292        3724 :             return max_predicate_locks_per_page;
    2293             : 
    2294           0 :         case PREDLOCKTAG_TUPLE:
    2295             : 
    2296             :             /*
    2297             :              * not reachable: nothing is finer-granularity than a tuple, so we
    2298             :              * should never try to promote to it.
    2299             :              */
    2300             :             Assert(false);
    2301           0 :             return 0;
    2302             :     }
    2303             : 
    2304             :     /* not reachable */
    2305             :     Assert(false);
    2306           0 :     return 0;
    2307             : }
    2308             : 
    2309             : /*
    2310             :  * For all ancestors of a newly-acquired predicate lock, increment
    2311             :  * their child count in the parent hash table. If any of them have
    2312             :  * more descendants than their promotion threshold, acquire the
    2313             :  * coarsest such lock.
    2314             :  *
    2315             :  * Returns true if a parent lock was acquired and false otherwise.
    2316             :  */
    2317             : static bool
    2318        8316 : CheckAndPromotePredicateLockRequest(const PREDICATELOCKTARGETTAG *reqtag)
    2319             : {
    2320             :     PREDICATELOCKTARGETTAG targettag,
    2321             :                 nexttag,
    2322             :                 promotiontag;
    2323             :     LOCALPREDICATELOCK *parentlock;
    2324             :     bool        found,
    2325             :                 promote;
    2326             : 
    2327        8316 :     promote = false;
    2328             : 
    2329        8316 :     targettag = *reqtag;
    2330             : 
    2331             :     /* check parents iteratively */
    2332       19064 :     while (GetParentPredicateLockTag(&targettag, &nexttag))
    2333             :     {
    2334       10748 :         targettag = nexttag;
    2335       10748 :         parentlock = (LOCALPREDICATELOCK *) hash_search(LocalPredicateLockHash,
    2336             :                                                         &targettag,
    2337             :                                                         HASH_ENTER,
    2338             :                                                         &found);
    2339       10748 :         if (!found)
    2340             :         {
    2341        6636 :             parentlock->held = false;
    2342        6636 :             parentlock->childLocks = 1;
    2343             :         }
    2344             :         else
    2345        4112 :             parentlock->childLocks++;
    2346             : 
    2347       10748 :         if (parentlock->childLocks >
    2348       10748 :             MaxPredicateChildLocks(&targettag))
    2349             :         {
    2350             :             /*
    2351             :              * We should promote to this parent lock. Continue to check its
    2352             :              * ancestors, however, both to get their child counts right and to
    2353             :              * check whether we should just go ahead and promote to one of
    2354             :              * them.
    2355             :              */
    2356         664 :             promotiontag = targettag;
    2357         664 :             promote = true;
    2358             :         }
    2359             :     }
    2360             : 
    2361        8316 :     if (promote)
    2362             :     {
    2363             :         /* acquire coarsest ancestor eligible for promotion */
    2364         664 :         PredicateLockAcquire(&promotiontag);
    2365         664 :         return true;
    2366             :     }
    2367             :     else
    2368        7652 :         return false;
    2369             : }
    2370             : 
    2371             : /*
    2372             :  * When releasing a lock, decrement the child count on all ancestor
    2373             :  * locks.
    2374             :  *
    2375             :  * This is called only when releasing a lock via
    2376             :  * DeleteChildTargetLocks (i.e. when a lock becomes redundant because
    2377             :  * we've acquired its parent, possibly due to promotion) or when a new
    2378             :  * MVCC write lock makes the predicate lock unnecessary. There's no
    2379             :  * point in calling it when locks are released at transaction end, as
    2380             :  * this information is no longer needed.
    2381             :  */
    2382             : static void
    2383        2754 : DecrementParentLocks(const PREDICATELOCKTARGETTAG *targettag)
    2384             : {
    2385             :     PREDICATELOCKTARGETTAG parenttag,
    2386             :                 nexttag;
    2387             : 
    2388        2754 :     parenttag = *targettag;
    2389             : 
    2390        8262 :     while (GetParentPredicateLockTag(&parenttag, &nexttag))
    2391             :     {
    2392             :         uint32      targettaghash;
    2393             :         LOCALPREDICATELOCK *parentlock,
    2394             :                    *rmlock PG_USED_FOR_ASSERTS_ONLY;
    2395             : 
    2396        5508 :         parenttag = nexttag;
    2397        5508 :         targettaghash = PredicateLockTargetTagHashCode(&parenttag);
    2398             :         parentlock = (LOCALPREDICATELOCK *)
    2399        5508 :             hash_search_with_hash_value(LocalPredicateLockHash,
    2400             :                                         &parenttag, targettaghash,
    2401             :                                         HASH_FIND, NULL);
    2402             : 
    2403             :         /*
    2404             :          * There's a small chance the parent lock doesn't exist in the lock
    2405             :          * table. This can happen if we prematurely removed it because an
    2406             :          * index split caused the child refcount to be off.
    2407             :          */
    2408        5508 :         if (parentlock == NULL)
    2409           0 :             continue;
    2410             : 
    2411        5508 :         parentlock->childLocks--;
    2412             : 
    2413             :         /*
    2414             :          * Under similar circumstances the parent lock's refcount might be
    2415             :          * zero. This only happens if we're holding that lock (otherwise we
    2416             :          * would have removed the entry).
    2417             :          */
    2418        5508 :         if (parentlock->childLocks < 0)
    2419             :         {
    2420             :             Assert(parentlock->held);
    2421           0 :             parentlock->childLocks = 0;
    2422             :         }
    2423             : 
    2424        5508 :         if ((parentlock->childLocks == 0) && (!parentlock->held))
    2425             :         {
    2426             :             rmlock = (LOCALPREDICATELOCK *)
    2427        1500 :                 hash_search_with_hash_value(LocalPredicateLockHash,
    2428             :                                             &parenttag, targettaghash,
    2429             :                                             HASH_REMOVE, NULL);
    2430             :             Assert(rmlock == parentlock);
    2431             :         }
    2432             :     }
    2433        2754 : }
    2434             : 
    2435             : /*
    2436             :  * Indicate that a predicate lock on the given target is held by the
    2437             :  * specified transaction. Has no effect if the lock is already held.
    2438             :  *
    2439             :  * This updates the lock table and the sxact's lock list, and creates
    2440             :  * the lock target if necessary, but does *not* do anything related to
    2441             :  * granularity promotion or the local lock table. See
    2442             :  * PredicateLockAcquire for that.
    2443             :  */
    2444             : static void
    2445        8316 : CreatePredicateLock(const PREDICATELOCKTARGETTAG *targettag,
    2446             :                     uint32 targettaghash,
    2447             :                     SERIALIZABLEXACT *sxact)
    2448             : {
    2449             :     PREDICATELOCKTARGET *target;
    2450             :     PREDICATELOCKTAG locktag;
    2451             :     PREDICATELOCK *lock;
    2452             :     LWLock     *partitionLock;
    2453             :     bool        found;
    2454             : 
    2455        8316 :     partitionLock = PredicateLockHashPartitionLock(targettaghash);
    2456             : 
    2457        8316 :     LWLockAcquire(SerializablePredicateListLock, LW_SHARED);
    2458        8316 :     if (IsInParallelMode())
    2459          12 :         LWLockAcquire(&sxact->perXactPredicateListLock, LW_EXCLUSIVE);
    2460        8316 :     LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    2461             : 
    2462             :     /* Make sure that the target is represented. */
    2463             :     target = (PREDICATELOCKTARGET *)
    2464        8316 :         hash_search_with_hash_value(PredicateLockTargetHash,
    2465             :                                     targettag, targettaghash,
    2466             :                                     HASH_ENTER_NULL, &found);
    2467        8316 :     if (!target)
    2468           0 :         ereport(ERROR,
    2469             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
    2470             :                  errmsg("out of shared memory"),
    2471             :                  errhint("You might need to increase max_pred_locks_per_transaction.")));
    2472        8316 :     if (!found)
    2473        6420 :         SHMQueueInit(&(target->predicateLocks));
    2474             : 
    2475             :     /* We've got the sxact and target, make sure they're joined. */
    2476        8316 :     locktag.myTarget = target;
    2477        8316 :     locktag.myXact = sxact;
    2478             :     lock = (PREDICATELOCK *)
    2479        8316 :         hash_search_with_hash_value(PredicateLockHash, &locktag,
    2480        8316 :                                     PredicateLockHashCodeFromTargetHashCode(&locktag, targettaghash),
    2481             :                                     HASH_ENTER_NULL, &found);
    2482        8316 :     if (!lock)
    2483           0 :         ereport(ERROR,
    2484             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
    2485             :                  errmsg("out of shared memory"),
    2486             :                  errhint("You might need to increase max_pred_locks_per_transaction.")));
    2487             : 
    2488        8316 :     if (!found)
    2489             :     {
    2490        8312 :         SHMQueueInsertBefore(&(target->predicateLocks), &(lock->targetLink));
    2491        8312 :         SHMQueueInsertBefore(&(sxact->predicateLocks),
    2492             :                              &(lock->xactLink));
    2493        8312 :         lock->commitSeqNo = InvalidSerCommitSeqNo;
    2494             :     }
    2495             : 
    2496        8316 :     LWLockRelease(partitionLock);
    2497        8316 :     if (IsInParallelMode())
    2498          12 :         LWLockRelease(&sxact->perXactPredicateListLock);
    2499        8316 :     LWLockRelease(SerializablePredicateListLock);
    2500        8316 : }
    2501             : 
    2502             : /*
    2503             :  * Acquire a predicate lock on the specified target for the current
    2504             :  * connection if not already held. This updates the local lock table
    2505             :  * and uses it to implement granularity promotion. It will consolidate
    2506             :  * multiple locks into a coarser lock if warranted, and will release
    2507             :  * any finer-grained locks covered by the new one.
    2508             :  */
    2509             : static void
    2510       52146 : PredicateLockAcquire(const PREDICATELOCKTARGETTAG *targettag)
    2511             : {
    2512             :     uint32      targettaghash;
    2513             :     bool        found;
    2514             :     LOCALPREDICATELOCK *locallock;
    2515             : 
    2516             :     /* Do we have the lock already, or a covering lock? */
    2517       52146 :     if (PredicateLockExists(targettag))
    2518       43830 :         return;
    2519             : 
    2520       51764 :     if (CoarserLockCovers(targettag))
    2521       43448 :         return;
    2522             : 
    2523             :     /* the same hash and LW lock apply to the lock target and the local lock. */
    2524        8316 :     targettaghash = PredicateLockTargetTagHashCode(targettag);
    2525             : 
    2526             :     /* Acquire lock in local table */
    2527             :     locallock = (LOCALPREDICATELOCK *)
    2528        8316 :         hash_search_with_hash_value(LocalPredicateLockHash,
    2529             :                                     targettag, targettaghash,
    2530             :                                     HASH_ENTER, &found);
    2531        8316 :     locallock->held = true;
    2532        8316 :     if (!found)
    2533        7652 :         locallock->childLocks = 0;
    2534             : 
    2535             :     /* Actually create the lock */
    2536        8316 :     CreatePredicateLock(targettag, targettaghash, MySerializableXact);
    2537             : 
    2538             :     /*
    2539             :      * Lock has been acquired. Check whether it should be promoted to a
    2540             :      * coarser granularity, or whether there are finer-granularity locks to
    2541             :      * clean up.
    2542             :      */
    2543        8316 :     if (CheckAndPromotePredicateLockRequest(targettag))
    2544             :     {
    2545             :         /*
    2546             :          * Lock request was promoted to a coarser-granularity lock, and that
    2547             :          * lock was acquired. It will delete this lock and any of its
    2548             :          * children, so we're done.
    2549             :          */
    2550             :     }
    2551             :     else
    2552             :     {
    2553             :         /* Clean up any finer-granularity locks */
    2554        7652 :         if (GET_PREDICATELOCKTARGETTAG_TYPE(*targettag) != PREDLOCKTAG_TUPLE)
    2555        4592 :             DeleteChildTargetLocks(targettag);
    2556             :     }
    2557             : }
    2558             : 
    2559             : 
    2560             : /*
    2561             :  *      PredicateLockRelation
    2562             :  *
    2563             :  * Gets a predicate lock at the relation level.
    2564             :  * Skip if not in full serializable transaction isolation level.
    2565             :  * Skip if this is a temporary table.
    2566             :  * Clear any finer-grained predicate locks this session has on the relation.
    2567             :  */
    2568             : void
    2569     1863224 : PredicateLockRelation(Relation relation, Snapshot snapshot)
    2570             : {
    2571             :     PREDICATELOCKTARGETTAG tag;
    2572             : 
    2573     1863224 :     if (!SerializationNeededForRead(relation, snapshot))
    2574     1861916 :         return;
    2575             : 
    2576        1308 :     SET_PREDICATELOCKTARGETTAG_RELATION(tag,
    2577             :                                         relation->rd_node.dbNode,
    2578             :                                         relation->rd_id);
    2579        1308 :     PredicateLockAcquire(&tag);
    2580             : }
    2581             : 
    2582             : /*
    2583             :  *      PredicateLockPage
    2584             :  *
    2585             :  * Gets a predicate lock at the page level.
    2586             :  * Skip if not in full serializable transaction isolation level.
    2587             :  * Skip if this is a temporary table.
    2588             :  * Skip if a coarser predicate lock already covers this page.
    2589             :  * Clear any finer-grained predicate locks this session has on the relation.
    2590             :  */
    2591             : void
    2592    12899530 : PredicateLockPage(Relation relation, BlockNumber blkno, Snapshot snapshot)
    2593             : {
    2594             :     PREDICATELOCKTARGETTAG tag;
    2595             : 
    2596    12899530 :     if (!SerializationNeededForRead(relation, snapshot))
    2597    12896662 :         return;
    2598             : 
    2599        2868 :     SET_PREDICATELOCKTARGETTAG_PAGE(tag,
    2600             :                                     relation->rd_node.dbNode,
    2601             :                                     relation->rd_id,
    2602             :                                     blkno);
    2603        2868 :     PredicateLockAcquire(&tag);
    2604             : }
    2605             : 
    2606             : /*
    2607             :  *      PredicateLockTID
    2608             :  *
    2609             :  * Gets a predicate lock at the tuple level.
    2610             :  * Skip if not in full serializable transaction isolation level.
    2611             :  * Skip if this is a temporary table.
    2612             :  */
    2613             : void
    2614    30417818 : PredicateLockTID(Relation relation, ItemPointer tid, Snapshot snapshot,
    2615             :                  TransactionId tuple_xid)
    2616             : {
    2617             :     PREDICATELOCKTARGETTAG tag;
    2618             : 
    2619    30417818 :     if (!SerializationNeededForRead(relation, snapshot))
    2620    30370512 :         return;
    2621             : 
    2622             :     /*
    2623             :      * Return if this xact wrote it.
    2624             :      */
    2625       47306 :     if (relation->rd_index == NULL)
    2626             :     {
    2627             :         /* If we wrote it; we already have a write lock. */
    2628       47306 :         if (TransactionIdIsCurrentTransactionId(tuple_xid))
    2629           0 :             return;
    2630             :     }
    2631             : 
    2632             :     /*
    2633             :      * Do quick-but-not-definitive test for a relation lock first.  This will
    2634             :      * never cause a return when the relation is *not* locked, but will
    2635             :      * occasionally let the check continue when there really *is* a relation
    2636             :      * level lock.
    2637             :      */
    2638       47306 :     SET_PREDICATELOCKTARGETTAG_RELATION(tag,
    2639             :                                         relation->rd_node.dbNode,
    2640             :                                         relation->rd_id);
    2641       47306 :     if (PredicateLockExists(&tag))
    2642           0 :         return;
    2643             : 
    2644       47306 :     SET_PREDICATELOCKTARGETTAG_TUPLE(tag,
    2645             :                                      relation->rd_node.dbNode,
    2646             :                                      relation->rd_id,
    2647             :                                      ItemPointerGetBlockNumber(tid),
    2648             :                                      ItemPointerGetOffsetNumber(tid));
    2649       47306 :     PredicateLockAcquire(&tag);
    2650             : }
    2651             : 
    2652             : 
    2653             : /*
    2654             :  *      DeleteLockTarget
    2655             :  *
    2656             :  * Remove a predicate lock target along with any locks held for it.
    2657             :  *
    2658             :  * Caller must hold SerializablePredicateListLock and the
    2659             :  * appropriate hash partition lock for the target.
    2660             :  */
    2661             : static void
    2662           0 : DeleteLockTarget(PREDICATELOCKTARGET *target, uint32 targettaghash)
    2663             : {
    2664             :     PREDICATELOCK *predlock;
    2665             :     SHM_QUEUE  *predlocktargetlink;
    2666             :     PREDICATELOCK *nextpredlock;
    2667             :     bool        found;
    2668             : 
    2669             :     Assert(LWLockHeldByMeInMode(SerializablePredicateListLock,
    2670             :                                 LW_EXCLUSIVE));
    2671             :     Assert(LWLockHeldByMe(PredicateLockHashPartitionLock(targettaghash)));
    2672             : 
    2673             :     predlock = (PREDICATELOCK *)
    2674           0 :         SHMQueueNext(&(target->predicateLocks),
    2675           0 :                      &(target->predicateLocks),
    2676             :                      offsetof(PREDICATELOCK, targetLink));
    2677           0 :     LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    2678           0 :     while (predlock)
    2679             :     {
    2680           0 :         predlocktargetlink = &(predlock->targetLink);
    2681             :         nextpredlock = (PREDICATELOCK *)
    2682           0 :             SHMQueueNext(&(target->predicateLocks),
    2683             :                          predlocktargetlink,
    2684             :                          offsetof(PREDICATELOCK, targetLink));
    2685             : 
    2686           0 :         SHMQueueDelete(&(predlock->xactLink));
    2687           0 :         SHMQueueDelete(&(predlock->targetLink));
    2688             : 
    2689           0 :         hash_search_with_hash_value
    2690             :             (PredicateLockHash,
    2691           0 :              &predlock->tag,
    2692           0 :              PredicateLockHashCodeFromTargetHashCode(&predlock->tag,
    2693             :                                                      targettaghash),
    2694             :              HASH_REMOVE, &found);
    2695             :         Assert(found);
    2696             : 
    2697           0 :         predlock = nextpredlock;
    2698             :     }
    2699           0 :     LWLockRelease(SerializableXactHashLock);
    2700             : 
    2701             :     /* Remove the target itself, if possible. */
    2702           0 :     RemoveTargetIfNoLongerUsed(target, targettaghash);
    2703           0 : }
    2704             : 
    2705             : 
    2706             : /*
    2707             :  *      TransferPredicateLocksToNewTarget
    2708             :  *
    2709             :  * Move or copy all the predicate locks for a lock target, for use by
    2710             :  * index page splits/combines and other things that create or replace
    2711             :  * lock targets. If 'removeOld' is true, the old locks and the target
    2712             :  * will be removed.
    2713             :  *
    2714             :  * Returns true on success, or false if we ran out of shared memory to
    2715             :  * allocate the new target or locks. Guaranteed to always succeed if
    2716             :  * removeOld is set (by using the scratch entry in PredicateLockTargetHash
    2717             :  * for scratch space).
    2718             :  *
    2719             :  * Warning: the "removeOld" option should be used only with care,
    2720             :  * because this function does not (indeed, can not) update other
    2721             :  * backends' LocalPredicateLockHash. If we are only adding new
    2722             :  * entries, this is not a problem: the local lock table is used only
    2723             :  * as a hint, so missing entries for locks that are held are
    2724             :  * OK. Having entries for locks that are no longer held, as can happen
    2725             :  * when using "removeOld", is not in general OK. We can only use it
    2726             :  * safely when replacing a lock with a coarser-granularity lock that
    2727             :  * covers it, or if we are absolutely certain that no one will need to
    2728             :  * refer to that lock in the future.
    2729             :  *
    2730             :  * Caller must hold SerializablePredicateListLock exclusively.
    2731             :  */
    2732             : static bool
    2733         174 : TransferPredicateLocksToNewTarget(PREDICATELOCKTARGETTAG oldtargettag,
    2734             :                                   PREDICATELOCKTARGETTAG newtargettag,
    2735             :                                   bool removeOld)
    2736             : {
    2737             :     uint32      oldtargettaghash;
    2738             :     LWLock     *oldpartitionLock;
    2739             :     PREDICATELOCKTARGET *oldtarget;
    2740             :     uint32      newtargettaghash;
    2741             :     LWLock     *newpartitionLock;
    2742             :     bool        found;
    2743         174 :     bool        outOfShmem = false;
    2744             : 
    2745             :     Assert(LWLockHeldByMeInMode(SerializablePredicateListLock,
    2746             :                                 LW_EXCLUSIVE));
    2747             : 
    2748         174 :     oldtargettaghash = PredicateLockTargetTagHashCode(&oldtargettag);
    2749         174 :     newtargettaghash = PredicateLockTargetTagHashCode(&newtargettag);
    2750         174 :     oldpartitionLock = PredicateLockHashPartitionLock(oldtargettaghash);
    2751         174 :     newpartitionLock = PredicateLockHashPartitionLock(newtargettaghash);
    2752             : 
    2753         174 :     if (removeOld)
    2754             :     {
    2755             :         /*
    2756             :          * Remove the dummy entry to give us scratch space, so we know we'll
    2757             :          * be able to create the new lock target.
    2758             :          */
    2759           0 :         RemoveScratchTarget(false);
    2760             :     }
    2761             : 
    2762             :     /*
    2763             :      * We must get the partition locks in ascending sequence to avoid
    2764             :      * deadlocks. If old and new partitions are the same, we must request the
    2765             :      * lock only once.
    2766             :      */
    2767         174 :     if (oldpartitionLock < newpartitionLock)
    2768             :     {
    2769          76 :         LWLockAcquire(oldpartitionLock,
    2770          76 :                       (removeOld ? LW_EXCLUSIVE : LW_SHARED));
    2771          76 :         LWLockAcquire(newpartitionLock, LW_EXCLUSIVE);
    2772             :     }
    2773          98 :     else if (oldpartitionLock > newpartitionLock)
    2774             :     {
    2775          88 :         LWLockAcquire(newpartitionLock, LW_EXCLUSIVE);
    2776          88 :         LWLockAcquire(oldpartitionLock,
    2777          88 :                       (removeOld ? LW_EXCLUSIVE : LW_SHARED));
    2778             :     }
    2779             :     else
    2780          10 :         LWLockAcquire(newpartitionLock, LW_EXCLUSIVE);
    2781             : 
    2782             :     /*
    2783             :      * Look for the old target.  If not found, that's OK; no predicate locks
    2784             :      * are affected, so we can just clean up and return. If it does exist,
    2785             :      * walk its list of predicate locks and move or copy them to the new
    2786             :      * target.
    2787             :      */
    2788         174 :     oldtarget = hash_search_with_hash_value(PredicateLockTargetHash,
    2789             :                                             &oldtargettag,
    2790             :                                             oldtargettaghash,
    2791             :                                             HASH_FIND, NULL);
    2792             : 
    2793         174 :     if (oldtarget)
    2794             :     {
    2795             :         PREDICATELOCKTARGET *newtarget;
    2796             :         PREDICATELOCK *oldpredlock;
    2797             :         PREDICATELOCKTAG newpredlocktag;
    2798             : 
    2799           0 :         newtarget = hash_search_with_hash_value(PredicateLockTargetHash,
    2800             :                                                 &newtargettag,
    2801             :                                                 newtargettaghash,
    2802             :                                                 HASH_ENTER_NULL, &found);
    2803             : 
    2804           0 :         if (!newtarget)
    2805             :         {
    2806             :             /* Failed to allocate due to insufficient shmem */
    2807           0 :             outOfShmem = true;
    2808           0 :             goto exit;
    2809             :         }
    2810             : 
    2811             :         /* If we created a new entry, initialize it */
    2812           0 :         if (!found)
    2813           0 :             SHMQueueInit(&(newtarget->predicateLocks));
    2814             : 
    2815           0 :         newpredlocktag.myTarget = newtarget;
    2816             : 
    2817             :         /*
    2818             :          * Loop through all the locks on the old target, replacing them with
    2819             :          * locks on the new target.
    2820             :          */
    2821             :         oldpredlock = (PREDICATELOCK *)
    2822           0 :             SHMQueueNext(&(oldtarget->predicateLocks),
    2823           0 :                          &(oldtarget->predicateLocks),
    2824             :                          offsetof(PREDICATELOCK, targetLink));
    2825           0 :         LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    2826           0 :         while (oldpredlock)
    2827             :         {
    2828             :             SHM_QUEUE  *predlocktargetlink;
    2829             :             PREDICATELOCK *nextpredlock;
    2830             :             PREDICATELOCK *newpredlock;
    2831           0 :             SerCommitSeqNo oldCommitSeqNo = oldpredlock->commitSeqNo;
    2832             : 
    2833           0 :             predlocktargetlink = &(oldpredlock->targetLink);
    2834             :             nextpredlock = (PREDICATELOCK *)
    2835           0 :                 SHMQueueNext(&(oldtarget->predicateLocks),
    2836             :                              predlocktargetlink,
    2837             :                              offsetof(PREDICATELOCK, targetLink));
    2838           0 :             newpredlocktag.myXact = oldpredlock->tag.myXact;
    2839             : 
    2840           0 :             if (removeOld)
    2841             :             {
    2842           0 :                 SHMQueueDelete(&(oldpredlock->xactLink));
    2843           0 :                 SHMQueueDelete(&(oldpredlock->targetLink));
    2844             : 
    2845           0 :                 hash_search_with_hash_value
    2846             :                     (PredicateLockHash,
    2847           0 :                      &oldpredlock->tag,
    2848           0 :                      PredicateLockHashCodeFromTargetHashCode(&oldpredlock->tag,
    2849             :                                                              oldtargettaghash),
    2850             :                      HASH_REMOVE, &found);
    2851             :                 Assert(found);
    2852             :             }
    2853             : 
    2854             :             newpredlock = (PREDICATELOCK *)
    2855           0 :                 hash_search_with_hash_value(PredicateLockHash,
    2856             :                                             &newpredlocktag,
    2857           0 :                                             PredicateLockHashCodeFromTargetHashCode(&newpredlocktag,
    2858             :                                                                                     newtargettaghash),
    2859             :                                             HASH_ENTER_NULL,
    2860             :                                             &found);
    2861           0 :             if (!newpredlock)
    2862             :             {
    2863             :                 /* Out of shared memory. Undo what we've done so far. */
    2864           0 :                 LWLockRelease(SerializableXactHashLock);
    2865           0 :                 DeleteLockTarget(newtarget, newtargettaghash);
    2866           0 :                 outOfShmem = true;
    2867           0 :                 goto exit;
    2868             :             }
    2869           0 :             if (!found)
    2870             :             {
    2871           0 :                 SHMQueueInsertBefore(&(newtarget->predicateLocks),
    2872             :                                      &(newpredlock->targetLink));
    2873           0 :                 SHMQueueInsertBefore(&(newpredlocktag.myXact->predicateLocks),
    2874             :                                      &(newpredlock->xactLink));
    2875           0 :                 newpredlock->commitSeqNo = oldCommitSeqNo;
    2876             :             }
    2877             :             else
    2878             :             {
    2879           0 :                 if (newpredlock->commitSeqNo < oldCommitSeqNo)
    2880           0 :                     newpredlock->commitSeqNo = oldCommitSeqNo;
    2881             :             }
    2882             : 
    2883             :             Assert(newpredlock->commitSeqNo != 0);
    2884             :             Assert((newpredlock->commitSeqNo == InvalidSerCommitSeqNo)
    2885             :                    || (newpredlock->tag.myXact == OldCommittedSxact));
    2886             : 
    2887           0 :             oldpredlock = nextpredlock;
    2888             :         }
    2889           0 :         LWLockRelease(SerializableXactHashLock);
    2890             : 
    2891           0 :         if (removeOld)
    2892             :         {
    2893             :             Assert(SHMQueueEmpty(&oldtarget->predicateLocks));
    2894           0 :             RemoveTargetIfNoLongerUsed(oldtarget, oldtargettaghash);
    2895             :         }
    2896             :     }
    2897             : 
    2898             : 
    2899         174 : exit:
    2900             :     /* Release partition locks in reverse order of acquisition. */
    2901         174 :     if (oldpartitionLock < newpartitionLock)
    2902             :     {
    2903          76 :         LWLockRelease(newpartitionLock);
    2904          76 :         LWLockRelease(oldpartitionLock);
    2905             :     }
    2906          98 :     else if (oldpartitionLock > newpartitionLock)
    2907             :     {
    2908          88 :         LWLockRelease(oldpartitionLock);
    2909          88 :         LWLockRelease(newpartitionLock);
    2910             :     }
    2911             :     else
    2912          10 :         LWLockRelease(newpartitionLock);
    2913             : 
    2914         174 :     if (removeOld)
    2915             :     {
    2916             :         /* We shouldn't run out of memory if we're moving locks */
    2917             :         Assert(!outOfShmem);
    2918             : 
    2919             :         /* Put the scratch entry back */
    2920           0 :         RestoreScratchTarget(false);
    2921             :     }
    2922             : 
    2923         174 :     return !outOfShmem;
    2924             : }
    2925             : 
    2926             : /*
    2927             :  * Drop all predicate locks of any granularity from the specified relation,
    2928             :  * which can be a heap relation or an index relation.  If 'transfer' is true,
    2929             :  * acquire a relation lock on the heap for any transactions with any lock(s)
    2930             :  * on the specified relation.
    2931             :  *
    2932             :  * This requires grabbing a lot of LW locks and scanning the entire lock
    2933             :  * target table for matches.  That makes this more expensive than most
    2934             :  * predicate lock management functions, but it will only be called for DDL
    2935             :  * type commands that are expensive anyway, and there are fast returns when
    2936             :  * no serializable transactions are active or the relation is temporary.
    2937             :  *
    2938             :  * We don't use the TransferPredicateLocksToNewTarget function because it
    2939             :  * acquires its own locks on the partitions of the two targets involved,
    2940             :  * and we'll already be holding all partition locks.
    2941             :  *
    2942             :  * We can't throw an error from here, because the call could be from a
    2943             :  * transaction which is not serializable.
    2944             :  *
    2945             :  * NOTE: This is currently only called with transfer set to true, but that may
    2946             :  * change.  If we decide to clean up the locks from a table on commit of a
    2947             :  * transaction which executed DROP TABLE, the false condition will be useful.
    2948             :  */
    2949             : static void
    2950       21762 : DropAllPredicateLocksFromTable(Relation relation, bool transfer)
    2951             : {
    2952             :     HASH_SEQ_STATUS seqstat;
    2953             :     PREDICATELOCKTARGET *oldtarget;
    2954             :     PREDICATELOCKTARGET *heaptarget;
    2955             :     Oid         dbId;
    2956             :     Oid         relId;
    2957             :     Oid         heapId;
    2958             :     int         i;
    2959             :     bool        isIndex;
    2960             :     bool        found;
    2961             :     uint32      heaptargettaghash;
    2962             : 
    2963             :     /*
    2964             :      * Bail out quickly if there are no serializable transactions running.
    2965             :      * It's safe to check this without taking locks because the caller is
    2966             :      * holding an ACCESS EXCLUSIVE lock on the relation.  No new locks which
    2967             :      * would matter here can be acquired while that is held.
    2968             :      */
    2969       21762 :     if (!TransactionIdIsValid(PredXact->SxactGlobalXmin))
    2970       21710 :         return;
    2971             : 
    2972          68 :     if (!PredicateLockingNeededForRelation(relation))
    2973          16 :         return;
    2974             : 
    2975          52 :     dbId = relation->rd_node.dbNode;
    2976          52 :     relId = relation->rd_id;
    2977          52 :     if (relation->rd_index == NULL)
    2978             :     {
    2979           0 :         isIndex = false;
    2980           0 :         heapId = relId;
    2981             :     }
    2982             :     else
    2983             :     {
    2984          52 :         isIndex = true;
    2985          52 :         heapId = relation->rd_index->indrelid;
    2986             :     }
    2987             :     Assert(heapId != InvalidOid);
    2988             :     Assert(transfer || !isIndex);   /* index OID only makes sense with
    2989             :                                      * transfer */
    2990             : 
    2991             :     /* Retrieve first time needed, then keep. */
    2992          52 :     heaptargettaghash = 0;
    2993          52 :     heaptarget = NULL;
    2994             : 
    2995             :     /* Acquire locks on all lock partitions */
    2996          52 :     LWLockAcquire(SerializablePredicateListLock, LW_EXCLUSIVE);
    2997         884 :     for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
    2998         832 :         LWLockAcquire(PredicateLockHashPartitionLockByIndex(i), LW_EXCLUSIVE);
    2999          52 :     LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    3000             : 
    3001             :     /*
    3002             :      * Remove the dummy entry to give us scratch space, so we know we'll be
    3003             :      * able to create the new lock target.
    3004             :      */
    3005          52 :     if (transfer)
    3006          52 :         RemoveScratchTarget(true);
    3007             : 
    3008             :     /* Scan through target map */
    3009          52 :     hash_seq_init(&seqstat, PredicateLockTargetHash);
    3010             : 
    3011         144 :     while ((oldtarget = (PREDICATELOCKTARGET *) hash_seq_search(&seqstat)))
    3012             :     {
    3013             :         PREDICATELOCK *oldpredlock;
    3014             : 
    3015             :         /*
    3016             :          * Check whether this is a target which needs attention.
    3017             :          */
    3018          92 :         if (GET_PREDICATELOCKTARGETTAG_RELATION(oldtarget->tag) != relId)
    3019          92 :             continue;           /* wrong relation id */
    3020           0 :         if (GET_PREDICATELOCKTARGETTAG_DB(oldtarget->tag) != dbId)
    3021           0 :             continue;           /* wrong database id */
    3022           0 :         if (transfer && !isIndex
    3023           0 :             && GET_PREDICATELOCKTARGETTAG_TYPE(oldtarget->tag) == PREDLOCKTAG_RELATION)
    3024           0 :             continue;           /* already the right lock */
    3025             : 
    3026             :         /*
    3027             :          * If we made it here, we have work to do.  We make sure the heap
    3028             :          * relation lock exists, then we walk the list of predicate locks for
    3029             :          * the old target we found, moving all locks to the heap relation lock
    3030             :          * -- unless they already hold that.
    3031             :          */
    3032             : 
    3033             :         /*
    3034             :          * First make sure we have the heap relation target.  We only need to
    3035             :          * do this once.
    3036             :          */
    3037           0 :         if (transfer && heaptarget == NULL)
    3038             :         {
    3039             :             PREDICATELOCKTARGETTAG heaptargettag;
    3040             : 
    3041           0 :             SET_PREDICATELOCKTARGETTAG_RELATION(heaptargettag, dbId, heapId);
    3042           0 :             heaptargettaghash = PredicateLockTargetTagHashCode(&heaptargettag);
    3043           0 :             heaptarget = hash_search_with_hash_value(PredicateLockTargetHash,
    3044             :                                                      &heaptargettag,
    3045             :                                                      heaptargettaghash,
    3046             :                                                      HASH_ENTER, &found);
    3047           0 :             if (!found)
    3048           0 :                 SHMQueueInit(&heaptarget->predicateLocks);
    3049             :         }
    3050             : 
    3051             :         /*
    3052             :          * Loop through all the locks on the old target, replacing them with
    3053             :          * locks on the new target.
    3054             :          */
    3055             :         oldpredlock = (PREDICATELOCK *)
    3056           0 :             SHMQueueNext(&(oldtarget->predicateLocks),
    3057           0 :                          &(oldtarget->predicateLocks),
    3058             :                          offsetof(PREDICATELOCK, targetLink));
    3059           0 :         while (oldpredlock)
    3060             :         {
    3061             :             PREDICATELOCK *nextpredlock;
    3062             :             PREDICATELOCK *newpredlock;
    3063             :             SerCommitSeqNo oldCommitSeqNo;
    3064             :             SERIALIZABLEXACT *oldXact;
    3065             : 
    3066             :             nextpredlock = (PREDICATELOCK *)
    3067           0 :                 SHMQueueNext(&(oldtarget->predicateLocks),
    3068           0 :                              &(oldpredlock->targetLink),
    3069             :                              offsetof(PREDICATELOCK, targetLink));
    3070             : 
    3071             :             /*
    3072             :              * Remove the old lock first. This avoids the chance of running
    3073             :              * out of lock structure entries for the hash table.
    3074             :              */
    3075           0 :             oldCommitSeqNo = oldpredlock->commitSeqNo;
    3076           0 :             oldXact = oldpredlock->tag.myXact;
    3077             : 
    3078           0 :             SHMQueueDelete(&(oldpredlock->xactLink));
    3079             : 
    3080             :             /*
    3081             :              * No need for retail delete from oldtarget list, we're removing
    3082             :              * the whole target anyway.
    3083             :              */
    3084           0 :             hash_search(PredicateLockHash,
    3085           0 :                         &oldpredlock->tag,
    3086             :                         HASH_REMOVE, &found);
    3087             :             Assert(found);
    3088             : 
    3089           0 :             if (transfer)
    3090             :             {
    3091             :                 PREDICATELOCKTAG newpredlocktag;
    3092             : 
    3093           0 :                 newpredlocktag.myTarget = heaptarget;
    3094           0 :                 newpredlocktag.myXact = oldXact;
    3095             :                 newpredlock = (PREDICATELOCK *)
    3096           0 :                     hash_search_with_hash_value(PredicateLockHash,
    3097             :                                                 &newpredlocktag,
    3098           0 :                                                 PredicateLockHashCodeFromTargetHashCode(&newpredlocktag,
    3099             :                                                                                         heaptargettaghash),
    3100             :                                                 HASH_ENTER,
    3101             :                                                 &found);
    3102           0 :                 if (!found)
    3103             :                 {
    3104           0 :                     SHMQueueInsertBefore(&(heaptarget->predicateLocks),
    3105             :                                          &(newpredlock->targetLink));
    3106           0 :                     SHMQueueInsertBefore(&(newpredlocktag.myXact->predicateLocks),
    3107             :                                          &(newpredlock->xactLink));
    3108           0 :                     newpredlock->commitSeqNo = oldCommitSeqNo;
    3109             :                 }
    3110             :                 else
    3111             :                 {
    3112           0 :                     if (newpredlock->commitSeqNo < oldCommitSeqNo)
    3113           0 :                         newpredlock->commitSeqNo = oldCommitSeqNo;
    3114             :                 }
    3115             : 
    3116             :                 Assert(newpredlock->commitSeqNo != 0);
    3117             :                 Assert((newpredlock->commitSeqNo == InvalidSerCommitSeqNo)
    3118             :                        || (newpredlock->tag.myXact == OldCommittedSxact));
    3119             :             }
    3120             : 
    3121           0 :             oldpredlock = nextpredlock;
    3122             :         }
    3123             : 
    3124           0 :         hash_search(PredicateLockTargetHash, &oldtarget->tag, HASH_REMOVE,
    3125             :                     &found);
    3126             :         Assert(found);
    3127             :     }
    3128             : 
    3129             :     /* Put the scratch entry back */
    3130          52 :     if (transfer)
    3131          52 :         RestoreScratchTarget(true);
    3132             : 
    3133             :     /* Release locks in reverse order */
    3134          52 :     LWLockRelease(SerializableXactHashLock);
    3135         884 :     for (i = NUM_PREDICATELOCK_PARTITIONS - 1; i >= 0; i--)
    3136         832 :         LWLockRelease(PredicateLockHashPartitionLockByIndex(i));
    3137          52 :     LWLockRelease(SerializablePredicateListLock);
    3138             : }
    3139             : 
    3140             : /*
    3141             :  * TransferPredicateLocksToHeapRelation
    3142             :  *      For all transactions, transfer all predicate locks for the given
    3143             :  *      relation to a single relation lock on the heap.
    3144             :  */
    3145             : void
    3146       21762 : TransferPredicateLocksToHeapRelation(Relation relation)
    3147             : {
    3148       21762 :     DropAllPredicateLocksFromTable(relation, true);
    3149       21762 : }
    3150             : 
    3151             : 
    3152             : /*
    3153             :  *      PredicateLockPageSplit
    3154             :  *
    3155             :  * Copies any predicate locks for the old page to the new page.
    3156             :  * Skip if this is a temporary table or toast table.
    3157             :  *
    3158             :  * NOTE: A page split (or overflow) affects all serializable transactions,
    3159             :  * even if it occurs in the context of another transaction isolation level.
    3160             :  *
    3161             :  * NOTE: This currently leaves the local copy of the locks without
    3162             :  * information on the new lock which is in shared memory.  This could cause
    3163             :  * problems if enough page splits occur on locked pages without the processes
    3164             :  * which hold the locks getting in and noticing.
    3165             :  */
    3166             : void
    3167       57286 : PredicateLockPageSplit(Relation relation, BlockNumber oldblkno,
    3168             :                        BlockNumber newblkno)
    3169             : {
    3170             :     PREDICATELOCKTARGETTAG oldtargettag;
    3171             :     PREDICATELOCKTARGETTAG newtargettag;
    3172             :     bool        success;
    3173             : 
    3174             :     /*
    3175             :      * Bail out quickly if there are no serializable transactions running.
    3176             :      *
    3177             :      * It's safe to do this check without taking any additional locks. Even if
    3178             :      * a serializable transaction starts concurrently, we know it can't take
    3179             :      * any SIREAD locks on the page being split because the caller is holding
    3180             :      * the associated buffer page lock. Memory reordering isn't an issue; the
    3181             :      * memory barrier in the LWLock acquisition guarantees that this read
    3182             :      * occurs while the buffer page lock is held.
    3183             :      */
    3184       57286 :     if (!TransactionIdIsValid(PredXact->SxactGlobalXmin))
    3185       57112 :         return;
    3186             : 
    3187         198 :     if (!PredicateLockingNeededForRelation(relation))
    3188          24 :         return;
    3189             : 
    3190             :     Assert(oldblkno != newblkno);
    3191             :     Assert(BlockNumberIsValid(oldblkno));
    3192             :     Assert(BlockNumberIsValid(newblkno));
    3193             : 
    3194         174 :     SET_PREDICATELOCKTARGETTAG_PAGE(oldtargettag,
    3195             :                                     relation->rd_node.dbNode,
    3196             :                                     relation->rd_id,
    3197             :                                     oldblkno);
    3198         174 :     SET_PREDICATELOCKTARGETTAG_PAGE(newtargettag,
    3199             :                                     relation->rd_node.dbNode,
    3200             :                                     relation->rd_id,
    3201             :                                     newblkno);
    3202             : 
    3203         174 :     LWLockAcquire(SerializablePredicateListLock, LW_EXCLUSIVE);
    3204             : 
    3205             :     /*
    3206             :      * Try copying the locks over to the new page's tag, creating it if
    3207             :      * necessary.
    3208             :      */
    3209         174 :     success = TransferPredicateLocksToNewTarget(oldtargettag,
    3210             :                                                 newtargettag,
    3211             :                                                 false);
    3212             : 
    3213         174 :     if (!success)
    3214             :     {
    3215             :         /*
    3216             :          * No more predicate lock entries are available. Failure isn't an
    3217             :          * option here, so promote the page lock to a relation lock.
    3218             :          */
    3219             : 
    3220             :         /* Get the parent relation lock's lock tag */
    3221           0 :         success = GetParentPredicateLockTag(&oldtargettag,
    3222             :                                             &newtargettag);
    3223             :         Assert(success);
    3224             : 
    3225             :         /*
    3226             :          * Move the locks to the parent. This shouldn't fail.
    3227             :          *
    3228             :          * Note that here we are removing locks held by other backends,
    3229             :          * leading to a possible inconsistency in their local lock hash table.
    3230             :          * This is OK because we're replacing it with a lock that covers the
    3231             :          * old one.
    3232             :          */
    3233           0 :         success = TransferPredicateLocksToNewTarget(oldtargettag,
    3234             :                                                     newtargettag,
    3235             :                                                     true);
    3236             :         Assert(success);
    3237             :     }
    3238             : 
    3239         174 :     LWLockRelease(SerializablePredicateListLock);
    3240             : }
    3241             : 
    3242             : /*
    3243             :  *      PredicateLockPageCombine
    3244             :  *
    3245             :  * Combines predicate locks for two existing pages.
    3246             :  * Skip if this is a temporary table or toast table.
    3247             :  *
    3248             :  * NOTE: A page combine affects all serializable transactions, even if it
    3249             :  * occurs in the context of another transaction isolation level.
    3250             :  */
    3251             : void
    3252        4066 : PredicateLockPageCombine(Relation relation, BlockNumber oldblkno,
    3253             :                          BlockNumber newblkno)
    3254             : {
    3255             :     /*
    3256             :      * Page combines differ from page splits in that we ought to be able to
    3257             :      * remove the locks on the old page after transferring them to the new
    3258             :      * page, instead of duplicating them. However, because we can't edit other
    3259             :      * backends' local lock tables, removing the old lock would leave them
    3260             :      * with an entry in their LocalPredicateLockHash for a lock they're not
    3261             :      * holding, which isn't acceptable. So we wind up having to do the same
    3262             :      * work as a page split, acquiring a lock on the new page and keeping the
    3263             :      * old page locked too. That can lead to some false positives, but should
    3264             :      * be rare in practice.
    3265             :      */
    3266        4066 :     PredicateLockPageSplit(relation, oldblkno, newblkno);
    3267        4066 : }
    3268             : 
    3269             : /*
    3270             :  * Walk the list of in-progress serializable transactions and find the new
    3271             :  * xmin.
    3272             :  */
    3273             : static void
    3274        1590 : SetNewSxactGlobalXmin(void)
    3275             : {
    3276             :     SERIALIZABLEXACT *sxact;
    3277             : 
    3278             :     Assert(LWLockHeldByMe(SerializableXactHashLock));
    3279             : 
    3280        1590 :     PredXact->SxactGlobalXmin = InvalidTransactionId;
    3281        1590 :     PredXact->SxactGlobalXminCount = 0;
    3282             : 
    3283        6116 :     for (sxact = FirstPredXact(); sxact != NULL; sxact = NextPredXact(sxact))
    3284             :     {
    3285        4526 :         if (!SxactIsRolledBack(sxact)
    3286        3964 :             && !SxactIsCommitted(sxact)
    3287          36 :             && sxact != OldCommittedSxact)
    3288             :         {
    3289             :             Assert(sxact->xmin != InvalidTransactionId);
    3290          36 :             if (!TransactionIdIsValid(PredXact->SxactGlobalXmin)
    3291           0 :                 || TransactionIdPrecedes(sxact->xmin,
    3292           0 :                                          PredXact->SxactGlobalXmin))
    3293             :             {
    3294          36 :                 PredXact->SxactGlobalXmin = sxact->xmin;
    3295          36 :                 PredXact->SxactGlobalXminCount = 1;
    3296             :             }
    3297           0 :             else if (TransactionIdEquals(sxact->xmin,
    3298             :                                          PredXact->SxactGlobalXmin))
    3299           0 :                 PredXact->SxactGlobalXminCount++;
    3300             :         }
    3301             :     }
    3302             : 
    3303        1590 :     SerialSetActiveSerXmin(PredXact->SxactGlobalXmin);
    3304        1590 : }
    3305             : 
    3306             : /*
    3307             :  *      ReleasePredicateLocks
    3308             :  *
    3309             :  * Releases predicate locks based on completion of the current transaction,
    3310             :  * whether committed or rolled back.  It can also be called for a read only
    3311             :  * transaction when it becomes impossible for the transaction to become
    3312             :  * part of a dangerous structure.
    3313             :  *
    3314             :  * We do nothing unless this is a serializable transaction.
    3315             :  *
    3316             :  * This method must ensure that shared memory hash tables are cleaned
    3317             :  * up in some relatively timely fashion.
    3318             :  *
    3319             :  * If this transaction is committing and is holding any predicate locks,
    3320             :  * it must be added to a list of completed serializable transactions still
    3321             :  * holding locks.
    3322             :  *
    3323             :  * If isReadOnlySafe is true, then predicate locks are being released before
    3324             :  * the end of the transaction because MySerializableXact has been determined
    3325             :  * to be RO_SAFE.  In non-parallel mode we can release it completely, but it
    3326             :  * in parallel mode we partially release the SERIALIZABLEXACT and keep it
    3327             :  * around until the end of the transaction, allowing each backend to clear its
    3328             :  * MySerializableXact variable and benefit from the optimization in its own
    3329             :  * time.
    3330             :  */
    3331             : void
    3332      728982 : ReleasePredicateLocks(bool isCommit, bool isReadOnlySafe)
    3333             : {
    3334             :     bool        needToClear;
    3335             :     RWConflict  conflict,
    3336             :                 nextConflict,
    3337             :                 possibleUnsafeConflict;
    3338             :     SERIALIZABLEXACT *roXact;
    3339             : 
    3340             :     /*
    3341             :      * We can't trust XactReadOnly here, because a transaction which started
    3342             :      * as READ WRITE can show as READ ONLY later, e.g., within
    3343             :      * subtransactions.  We want to flag a transaction as READ ONLY if it
    3344             :      * commits without writing so that de facto READ ONLY transactions get the
    3345             :      * benefit of some RO optimizations, so we will use this local variable to
    3346             :      * get some cleanup logic right which is based on whether the transaction
    3347             :      * was declared READ ONLY at the top level.
    3348             :      */
    3349             :     bool        topLevelIsDeclaredReadOnly;
    3350             : 
    3351             :     /* We can't be both committing and releasing early due to RO_SAFE. */
    3352             :     Assert(!(isCommit && isReadOnlySafe));
    3353             : 
    3354             :     /* Are we at the end of a transaction, that is, a commit or abort? */
    3355      728982 :     if (!isReadOnlySafe)
    3356             :     {
    3357             :         /*
    3358             :          * Parallel workers mustn't release predicate locks at the end of
    3359             :          * their transaction.  The leader will do that at the end of its
    3360             :          * transaction.
    3361             :          */
    3362      728918 :         if (IsParallelWorker())
    3363             :         {
    3364        5070 :             ReleasePredicateLocksLocal();
    3365        5070 :             return;
    3366             :         }
    3367             : 
    3368             :         /*
    3369             :          * By the time the leader in a parallel query reaches end of
    3370             :          * transaction, it has waited for all workers to exit.
    3371             :          */
    3372             :         Assert(!ParallelContextActive());
    3373             : 
    3374             :         /*
    3375             :          * If the leader in a parallel query earlier stashed a partially
    3376             :          * released SERIALIZABLEXACT for final clean-up at end of transaction
    3377             :          * (because workers might still have been accessing it), then it's
    3378             :          * time to restore it.
    3379             :          */
    3380      723848 :         if (SavedSerializableXact != InvalidSerializableXact)
    3381             :         {
    3382             :             Assert(MySerializableXact == InvalidSerializableXact);
    3383           2 :             MySerializableXact = SavedSerializableXact;
    3384           2 :             SavedSerializableXact = InvalidSerializableXact;
    3385             :             Assert(SxactIsPartiallyReleased(MySerializableXact));
    3386             :         }
    3387             :     }
    3388             : 
    3389      723912 :     if (MySerializableXact == InvalidSerializableXact)
    3390             :     {
    3391             :         Assert(LocalPredicateLockHash == NULL);
    3392      721012 :         return;
    3393             :     }
    3394             : 
    3395        2900 :     LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    3396             : 
    3397             :     /*
    3398             :      * If the transaction is committing, but it has been partially released
    3399             :      * already, then treat this as a roll back.  It was marked as rolled back.
    3400             :      */
    3401        2900 :     if (isCommit && SxactIsPartiallyReleased(MySerializableXact))
    3402           2 :         isCommit = false;
    3403             : 
    3404             :     /*
    3405             :      * If we're called in the middle of a transaction because we discovered
    3406             :      * that the SXACT_FLAG_RO_SAFE flag was set, then we'll partially release
    3407             :      * it (that is, release the predicate locks and conflicts, but not the
    3408             :      * SERIALIZABLEXACT itself) if we're the first backend to have noticed.
    3409             :      */
    3410        2900 :     if (isReadOnlySafe && IsInParallelMode())
    3411             :     {
    3412             :         /*
    3413             :          * The leader needs to stash a pointer to it, so that it can
    3414             :          * completely release it at end-of-transaction.
    3415             :          */
    3416           6 :         if (!IsParallelWorker())
    3417           2 :             SavedSerializableXact = MySerializableXact;
    3418             : 
    3419             :         /*
    3420             :          * The first backend to reach this condition will partially release
    3421             :          * the SERIALIZABLEXACT.  All others will just clear their
    3422             :          * backend-local state so that they stop doing SSI checks for the rest
    3423             :          * of the transaction.
    3424             :          */
    3425           6 :         if (SxactIsPartiallyReleased(MySerializableXact))
    3426             :         {
    3427           4 :             LWLockRelease(SerializableXactHashLock);
    3428           4 :             ReleasePredicateLocksLocal();
    3429           4 :             return;
    3430             :         }
    3431             :         else
    3432             :         {
    3433           2 :             MySerializableXact->flags |= SXACT_FLAG_PARTIALLY_RELEASED;
    3434             :             /* ... and proceed to perform the partial release below. */
    3435             :         }
    3436             :     }
    3437             :     Assert(!isCommit || SxactIsPrepared(MySerializableXact));
    3438             :     Assert(!isCommit || !SxactIsDoomed(MySerializableXact));
    3439             :     Assert(!SxactIsCommitted(MySerializableXact));
    3440             :     Assert(SxactIsPartiallyReleased(MySerializableXact)
    3441             :            || !SxactIsRolledBack(MySerializableXact));
    3442             : 
    3443             :     /* may not be serializable during COMMIT/ROLLBACK PREPARED */
    3444             :     Assert(MySerializableXact->pid == 0 || IsolationIsSerializable());
    3445             : 
    3446             :     /* We'd better not already be on the cleanup list. */
    3447             :     Assert(!SxactIsOnFinishedList(MySerializableXact));
    3448             : 
    3449        2896 :     topLevelIsDeclaredReadOnly = SxactIsReadOnly(MySerializableXact);
    3450             : 
    3451             :     /*
    3452             :      * We don't hold XidGenLock lock here, assuming that TransactionId is
    3453             :      * atomic!
    3454             :      *
    3455             :      * If this value is changing, we don't care that much whether we get the
    3456             :      * old or new value -- it is just used to determine how far
    3457             :      * SxactGlobalXmin must advance before this transaction can be fully
    3458             :      * cleaned up.  The worst that could happen is we wait for one more
    3459             :      * transaction to complete before freeing some RAM; correctness of visible
    3460             :      * behavior is not affected.
    3461             :      */
    3462        2896 :     MySerializableXact->finishedBefore = XidFromFullTransactionId(ShmemVariableCache->nextXid);
    3463             : 
    3464             :     /*
    3465             :      * If it's not a commit it's either a rollback or a read-only transaction
    3466             :      * flagged SXACT_FLAG_RO_SAFE, and we can clear our locks immediately.
    3467             :      */
    3468        2896 :     if (isCommit)
    3469             :     {
    3470        2296 :         MySerializableXact->flags |= SXACT_FLAG_COMMITTED;
    3471        2296 :         MySerializableXact->commitSeqNo = ++(PredXact->LastSxactCommitSeqNo);
    3472             :         /* Recognize implicit read-only transaction (commit without write). */
    3473        2296 :         if (!MyXactDidWrite)
    3474         370 :             MySerializableXact->flags |= SXACT_FLAG_READ_ONLY;
    3475             :     }
    3476             :     else
    3477             :     {
    3478             :         /*
    3479             :          * The DOOMED flag indicates that we intend to roll back this
    3480             :          * transaction and so it should not cause serialization failures for
    3481             :          * other transactions that conflict with it. Note that this flag might
    3482             :          * already be set, if another backend marked this transaction for
    3483             :          * abort.
    3484             :          *
    3485             :          * The ROLLED_BACK flag further indicates that ReleasePredicateLocks
    3486             :          * has been called, and so the SerializableXact is eligible for
    3487             :          * cleanup. This means it should not be considered when calculating
    3488             :          * SxactGlobalXmin.
    3489             :          */
    3490         600 :         MySerializableXact->flags |= SXACT_FLAG_DOOMED;
    3491         600 :         MySerializableXact->flags |= SXACT_FLAG_ROLLED_BACK;
    3492             : 
    3493             :         /*
    3494             :          * If the transaction was previously prepared, but is now failing due
    3495             :          * to a ROLLBACK PREPARED or (hopefully very rare) error after the
    3496             :          * prepare, clear the prepared flag.  This simplifies conflict
    3497             :          * checking.
    3498             :          */
    3499         600 :         MySerializableXact->flags &= ~SXACT_FLAG_PREPARED;
    3500             :     }
    3501             : 
    3502        2896 :     if (!topLevelIsDeclaredReadOnly)
    3503             :     {
    3504             :         Assert(PredXact->WritableSxactCount > 0);
    3505        2684 :         if (--(PredXact->WritableSxactCount) == 0)
    3506             :         {
    3507             :             /*
    3508             :              * Release predicate locks and rw-conflicts in for all committed
    3509             :              * transactions.  There are no longer any transactions which might
    3510             :              * conflict with the locks and no chance for new transactions to
    3511             :              * overlap.  Similarly, existing conflicts in can't cause pivots,
    3512             :              * and any conflicts in which could have completed a dangerous
    3513             :              * structure would already have caused a rollback, so any
    3514             :              * remaining ones must be benign.
    3515             :              */
    3516        1574 :             PredXact->CanPartialClearThrough = PredXact->LastSxactCommitSeqNo;
    3517             :         }
    3518             :     }
    3519             :     else
    3520             :     {
    3521             :         /*
    3522             :          * Read-only transactions: clear the list of transactions that might
    3523             :          * make us unsafe. Note that we use 'inLink' for the iteration as
    3524             :          * opposed to 'outLink' for the r/w xacts.
    3525             :          */
    3526             :         possibleUnsafeConflict = (RWConflict)
    3527         212 :             SHMQueueNext(&MySerializableXact->possibleUnsafeConflicts,
    3528         212 :                          &MySerializableXact->possibleUnsafeConflicts,
    3529             :                          offsetof(RWConflictData, inLink));
    3530         296 :         while (possibleUnsafeConflict)
    3531             :         {
    3532             :             nextConflict = (RWConflict)
    3533          84 :                 SHMQueueNext(&MySerializableXact->possibleUnsafeConflicts,
    3534          84 :                              &possibleUnsafeConflict->inLink,
    3535             :                              offsetof(RWConflictData, inLink));
    3536             : 
    3537             :             Assert(!SxactIsReadOnly(possibleUnsafeConflict->sxactOut));
    3538             :             Assert(MySerializableXact == possibleUnsafeConflict->sxactIn);
    3539             : 
    3540          84 :             ReleaseRWConflict(possibleUnsafeConflict);
    3541             : 
    3542          84 :             possibleUnsafeConflict = nextConflict;
    3543             :         }
    3544             :     }
    3545             : 
    3546             :     /* Check for conflict out to old committed transactions. */
    3547        2896 :     if (isCommit
    3548        2296 :         && !SxactIsReadOnly(MySerializableXact)
    3549        1926 :         && SxactHasSummaryConflictOut(MySerializableXact))
    3550             :     {
    3551             :         /*
    3552             :          * we don't know which old committed transaction we conflicted with,
    3553             :          * so be conservative and use FirstNormalSerCommitSeqNo here
    3554             :          */
    3555           0 :         MySerializableXact->SeqNo.earliestOutConflictCommit =
    3556             :             FirstNormalSerCommitSeqNo;
    3557           0 :         MySerializableXact->flags |= SXACT_FLAG_CONFLICT_OUT;
    3558             :     }
    3559             : 
    3560             :     /*
    3561             :      * Release all outConflicts to committed transactions.  If we're rolling
    3562             :      * back clear them all.  Set SXACT_FLAG_CONFLICT_OUT if any point to
    3563             :      * previously committed transactions.
    3564             :      */
    3565             :     conflict = (RWConflict)
    3566        2896 :         SHMQueueNext(&MySerializableXact->outConflicts,
    3567        2896 :                      &MySerializableXact->outConflicts,
    3568             :                      offsetof(RWConflictData, outLink));
    3569        4220 :     while (conflict)
    3570             :     {
    3571             :         nextConflict = (RWConflict)
    3572        1324 :             SHMQueueNext(&MySerializableXact->outConflicts,
    3573        1324 :                          &conflict->outLink,
    3574             :                          offsetof(RWConflictData, outLink));
    3575             : 
    3576        1324 :         if (isCommit
    3577         886 :             && !SxactIsReadOnly(MySerializableXact)
    3578         670 :             && SxactIsCommitted(conflict->sxactIn))
    3579             :         {
    3580         192 :             if ((MySerializableXact->flags & SXACT_FLAG_CONFLICT_OUT) == 0
    3581           0 :                 || conflict->sxactIn->prepareSeqNo < MySerializableXact->SeqNo.earliestOutConflictCommit)
    3582         192 :                 MySerializableXact->SeqNo.earliestOutConflictCommit = conflict->sxactIn->prepareSeqNo;
    3583         192 :             MySerializableXact->flags |= SXACT_FLAG_CONFLICT_OUT;
    3584             :         }
    3585             : 
    3586        1324 :         if (!isCommit
    3587         886 :             || SxactIsCommitted(conflict->sxactIn)
    3588         650 :             || (conflict->sxactIn->SeqNo.lastCommitBeforeSnapshot >= PredXact->LastSxactCommitSeqNo))
    3589         674 :             ReleaseRWConflict(conflict);
    3590             : 
    3591        1324 :         conflict = nextConflict;
    3592             :     }
    3593             : 
    3594             :     /*
    3595             :      * Release all inConflicts from committed and read-only transactions. If
    3596             :      * we're rolling back, clear them all.
    3597             :      */
    3598             :     conflict = (RWConflict)
    3599        2896 :         SHMQueueNext(&MySerializableXact->inConflicts,
    3600        2896 :                      &MySerializableXact->inConflicts,
    3601             :                      offsetof(RWConflictData, inLink));
    3602        4400 :     while (conflict)
    3603             :     {
    3604             :         nextConflict = (RWConflict)
    3605        1504 :             SHMQueueNext(&MySerializableXact->inConflicts,
    3606        1504 :                          &conflict->inLink,
    3607             :                          offsetof(RWConflictData, inLink));
    3608             : 
    3609        1504 :         if (!isCommit
    3610        1182 :             || SxactIsCommitted(conflict->sxactOut)
    3611         814 :             || SxactIsReadOnly(conflict->sxactOut))
    3612         850 :             ReleaseRWConflict(conflict);
    3613             : 
    3614        1504 :         conflict = nextConflict;
    3615             :     }
    3616             : 
    3617        2896 :     if (!topLevelIsDeclaredReadOnly)
    3618             :     {
    3619             :         /*
    3620             :          * Remove ourselves from the list of possible conflicts for concurrent
    3621             :          * READ ONLY transactions, flagging them as unsafe if we have a
    3622             :          * conflict out. If any are waiting DEFERRABLE transactions, wake them
    3623             :          * up if they are known safe or known unsafe.
    3624             :          */
    3625             :         possibleUnsafeConflict = (RWConflict)
    3626        2684 :             SHMQueueNext(&MySerializableXact->possibleUnsafeConflicts,
    3627        2684 :                          &MySerializableXact->possibleUnsafeConflicts,
    3628             :                          offsetof(RWConflictData, outLink));
    3629        2860 :         while (possibleUnsafeConflict)
    3630             :         {
    3631             :             nextConflict = (RWConflict)
    3632         176 :                 SHMQueueNext(&MySerializableXact->possibleUnsafeConflicts,
    3633         176 :                              &possibleUnsafeConflict->outLink,
    3634             :                              offsetof(RWConflictData, outLink));
    3635             : 
    3636         176 :             roXact = possibleUnsafeConflict->sxactIn;
    3637             :             Assert(MySerializableXact == possibleUnsafeConflict->sxactOut);
    3638             :             Assert(SxactIsReadOnly(roXact));
    3639             : 
    3640             :             /* Mark conflicted if necessary. */
    3641         176 :             if (isCommit
    3642         172 :                 && MyXactDidWrite
    3643         170 :                 && SxactHasConflictOut(MySerializableXact)
    3644          26 :                 && (MySerializableXact->SeqNo.earliestOutConflictCommit
    3645          26 :                     <= roXact->SeqNo.lastCommitBeforeSnapshot))
    3646             :             {
    3647             :                 /*
    3648             :                  * This releases possibleUnsafeConflict (as well as all other
    3649             :                  * possible conflicts for roXact)
    3650             :                  */
    3651           6 :                 FlagSxactUnsafe(roXact);
    3652             :             }
    3653             :             else
    3654             :             {
    3655         170 :                 ReleaseRWConflict(possibleUnsafeConflict);
    3656             : 
    3657             :                 /*
    3658             :                  * If we were the last possible conflict, flag it safe. The
    3659             :                  * transaction can now safely release its predicate locks (but
    3660             :                  * that transaction's backend has to do that itself).
    3661             :                  */
    3662         170 :                 if (SHMQueueEmpty(&roXact->possibleUnsafeConflicts))
    3663         128 :                     roXact->flags |= SXACT_FLAG_RO_SAFE;
    3664             :             }
    3665             : 
    3666             :             /*
    3667             :              * Wake up the process for a waiting DEFERRABLE transaction if we
    3668             :              * now know it's either safe or conflicted.
    3669             :              */
    3670         176 :             if (SxactIsDeferrableWaiting(roXact) &&
    3671           6 :                 (SxactIsROUnsafe(roXact) || SxactIsROSafe(roXact)))
    3672           4 :                 ProcSendSignal(roXact->pid);
    3673             : 
    3674         176 :             possibleUnsafeConflict = nextConflict;
    3675             :         }
    3676             :     }
    3677             : 
    3678             :     /*
    3679             :      * Check whether it's time to clean up old transactions. This can only be
    3680             :      * done when the last serializable transaction with the oldest xmin among
    3681             :      * serializable transactions completes.  We then find the "new oldest"
    3682             :      * xmin and purge any transactions which finished before this transaction
    3683             :      * was launched.
    3684             :      */
    3685        2896 :     needToClear = false;
    3686        2896 :     if (TransactionIdEquals(MySerializableXact->xmin, PredXact->SxactGlobalXmin))
    3687             :     {
    3688             :         Assert(PredXact->SxactGlobalXminCount > 0);
    3689        2860 :         if (--(PredXact->SxactGlobalXminCount) == 0)
    3690             :         {
    3691        1590 :             SetNewSxactGlobalXmin();
    3692        1590 :             needToClear = true;
    3693             :         }
    3694             :     }
    3695             : 
    3696        2896 :     LWLockRelease(SerializableXactHashLock);
    3697             : 
    3698        2896 :     LWLockAcquire(SerializableFinishedListLock, LW_EXCLUSIVE);
    3699             : 
    3700             :     /* Add this to the list of transactions to check for later cleanup. */
    3701        2896 :     if (isCommit)
    3702        2296 :         SHMQueueInsertBefore(FinishedSerializableTransactions,
    3703        2296 :                              &MySerializableXact->finishedLink);
    3704             : 
    3705             :     /*
    3706             :      * If we're releasing a RO_SAFE transaction in parallel mode, we'll only
    3707             :      * partially release it.  That's necessary because other backends may have
    3708             :      * a reference to it.  The leader will release the SERIALIZABLEXACT itself
    3709             :      * at the end of the transaction after workers have stopped running.
    3710             :      */
    3711        2896 :     if (!isCommit)
    3712         600 :         ReleaseOneSerializableXact(MySerializableXact,
    3713         600 :                                    isReadOnlySafe && IsInParallelMode(),
    3714             :                                    false);
    3715             : 
    3716        2896 :     LWLockRelease(SerializableFinishedListLock);
    3717             : 
    3718        2896 :     if (needToClear)
    3719        1590 :         ClearOldPredicateLocks();
    3720             : 
    3721        2896 :     ReleasePredicateLocksLocal();
    3722             : }
    3723             : 
    3724             : static void
    3725        7970 : ReleasePredicateLocksLocal(void)
    3726             : {
    3727        7970 :     MySerializableXact = InvalidSerializableXact;
    3728        7970 :     MyXactDidWrite = false;
    3729             : 
    3730             :     /* Delete per-transaction lock table */
    3731        7970 :     if (LocalPredicateLockHash != NULL)
    3732             :     {
    3733        2892 :         hash_destroy(LocalPredicateLockHash);
    3734        2892 :         LocalPredicateLockHash = NULL;
    3735             :     }
    3736        7970 : }
    3737             : 
    3738             : /*
    3739             :  * Clear old predicate locks, belonging to committed transactions that are no
    3740             :  * longer interesting to any in-progress transaction.
    3741             :  */
    3742             : static void
    3743        1590 : ClearOldPredicateLocks(void)
    3744             : {
    3745             :     SERIALIZABLEXACT *finishedSxact;
    3746             :     PREDICATELOCK *predlock;
    3747             : 
    3748             :     /*
    3749             :      * Loop through finished transactions. They are in commit order, so we can
    3750             :      * stop as soon as we find one that's still interesting.
    3751             :      */
    3752        1590 :     LWLockAcquire(SerializableFinishedListLock, LW_EXCLUSIVE);
    3753             :     finishedSxact = (SERIALIZABLEXACT *)
    3754        1590 :         SHMQueueNext(FinishedSerializableTransactions,
    3755             :                      FinishedSerializableTransactions,
    3756             :                      offsetof(SERIALIZABLEXACT, finishedLink));
    3757        1590 :     LWLockAcquire(SerializableXactHashLock, LW_SHARED);
    3758        3900 :     while (finishedSxact)
    3759             :     {
    3760             :         SERIALIZABLEXACT *nextSxact;
    3761             : 
    3762             :         nextSxact = (SERIALIZABLEXACT *)
    3763        2326 :             SHMQueueNext(FinishedSerializableTransactions,
    3764        2326 :                          &(finishedSxact->finishedLink),
    3765             :                          offsetof(SERIALIZABLEXACT, finishedLink));
    3766        2326 :         if (!TransactionIdIsValid(PredXact->SxactGlobalXmin)
    3767          52 :             || TransactionIdPrecedesOrEquals(finishedSxact->finishedBefore,
    3768          52 :                                              PredXact->SxactGlobalXmin))
    3769             :         {
    3770             :             /*
    3771             :              * This transaction committed before any in-progress transaction
    3772             :              * took its snapshot. It's no longer interesting.
    3773             :              */
    3774        2296 :             LWLockRelease(SerializableXactHashLock);
    3775        2296 :             SHMQueueDelete(&(finishedSxact->finishedLink));
    3776        2296 :             ReleaseOneSerializableXact(finishedSxact, false, false);
    3777        2296 :             LWLockAcquire(SerializableXactHashLock, LW_SHARED);
    3778             :         }
    3779          30 :         else if (finishedSxact->commitSeqNo > PredXact->HavePartialClearedThrough
    3780          30 :                  && finishedSxact->commitSeqNo <= PredXact->CanPartialClearThrough)
    3781             :         {
    3782             :             /*
    3783             :              * Any active transactions that took their snapshot before this
    3784             :              * transaction committed are read-only, so we can clear part of
    3785             :              * its state.
    3786             :              */
    3787          14 :             LWLockRelease(SerializableXactHashLock);
    3788             : 
    3789          14 :             if (SxactIsReadOnly(finishedSxact))
    3790             :             {
    3791             :                 /* A read-only transaction can be removed entirely */
    3792           0 :                 SHMQueueDelete(&(finishedSxact->finishedLink));
    3793           0 :                 ReleaseOneSerializableXact(finishedSxact, false, false);
    3794             :             }
    3795             :             else
    3796             :             {
    3797             :                 /*
    3798             :                  * A read-write transaction can only be partially cleared. We
    3799             :                  * need to keep the SERIALIZABLEXACT but can release the
    3800             :                  * SIREAD locks and conflicts in.
    3801             :                  */
    3802          14 :                 ReleaseOneSerializableXact(finishedSxact, true, false);
    3803             :             }
    3804             : 
    3805          14 :             PredXact->HavePartialClearedThrough = finishedSxact->commitSeqNo;
    3806          14 :             LWLockAcquire(SerializableXactHashLock, LW_SHARED);
    3807             :         }
    3808             :         else
    3809             :         {
    3810             :             /* Still interesting. */
    3811             :             break;
    3812             :         }
    3813        2310 :         finishedSxact = nextSxact;
    3814             :     }
    3815        1590 :     LWLockRelease(SerializableXactHashLock);
    3816             : 
    3817             :     /*
    3818             :      * Loop through predicate locks on dummy transaction for summarized data.
    3819             :      */
    3820        1590 :     LWLockAcquire(SerializablePredicateListLock, LW_SHARED);
    3821             :     predlock = (PREDICATELOCK *)
    3822        1590 :         SHMQueueNext(&OldCommittedSxact->predicateLocks,
    3823        1590 :                      &OldCommittedSxact->predicateLocks,
    3824             :                      offsetof(PREDICATELOCK, xactLink));
    3825        1590 :     while (predlock)
    3826             :     {
    3827             :         PREDICATELOCK *nextpredlock;
    3828             :         bool        canDoPartialCleanup;
    3829             : 
    3830             :         nextpredlock = (PREDICATELOCK *)
    3831           0 :             SHMQueueNext(&OldCommittedSxact->predicateLocks,
    3832           0 :                          &predlock->xactLink,
    3833             :                          offsetof(PREDICATELOCK, xactLink));
    3834             : 
    3835           0 :         LWLockAcquire(SerializableXactHashLock, LW_SHARED);
    3836             :         Assert(predlock->commitSeqNo != 0);
    3837             :         Assert(predlock->commitSeqNo != InvalidSerCommitSeqNo);
    3838           0 :         canDoPartialCleanup = (predlock->commitSeqNo <= PredXact->CanPartialClearThrough);
    3839           0 :         LWLockRelease(SerializableXactHashLock);
    3840             : 
    3841             :         /*
    3842             :          * If this lock originally belonged to an old enough transaction, we
    3843             :          * can release it.
    3844             :          */
    3845           0 :         if (canDoPartialCleanup)
    3846             :         {
    3847             :             PREDICATELOCKTAG tag;
    3848             :             PREDICATELOCKTARGET *target;
    3849             :             PREDICATELOCKTARGETTAG targettag;
    3850             :             uint32      targettaghash;
    3851             :             LWLock     *partitionLock;
    3852             : 
    3853           0 :             tag = predlock->tag;
    3854           0 :             target = tag.myTarget;
    3855           0 :             targettag = target->tag;
    3856           0 :             targettaghash = PredicateLockTargetTagHashCode(&targettag);
    3857           0 :             partitionLock = PredicateLockHashPartitionLock(targettaghash);
    3858             : 
    3859           0 :             LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    3860             : 
    3861           0 :             SHMQueueDelete(&(predlock->targetLink));
    3862           0 :             SHMQueueDelete(&(predlock->xactLink));
    3863             : 
    3864           0 :             hash_search_with_hash_value(PredicateLockHash, &tag,
    3865           0 :                                         PredicateLockHashCodeFromTargetHashCode(&tag,
    3866             :                                                                                 targettaghash),
    3867             :                                         HASH_REMOVE, NULL);
    3868           0 :             RemoveTargetIfNoLongerUsed(target, targettaghash);
    3869             : 
    3870           0 :             LWLockRelease(partitionLock);
    3871             :         }
    3872             : 
    3873           0 :         predlock = nextpredlock;
    3874             :     }
    3875             : 
    3876        1590 :     LWLockRelease(SerializablePredicateListLock);
    3877        1590 :     LWLockRelease(SerializableFinishedListLock);
    3878        1590 : }
    3879             : 
    3880             : /*
    3881             :  * This is the normal way to delete anything from any of the predicate
    3882             :  * locking hash tables.  Given a transaction which we know can be deleted:
    3883             :  * delete all predicate locks held by that transaction and any predicate
    3884             :  * lock targets which are now unreferenced by a lock; delete all conflicts
    3885             :  * for the transaction; delete all xid values for the transaction; then
    3886             :  * delete the transaction.
    3887             :  *
    3888             :  * When the partial flag is set, we can release all predicate locks and
    3889             :  * in-conflict information -- we've established that there are no longer
    3890             :  * any overlapping read write transactions for which this transaction could
    3891             :  * matter -- but keep the transaction entry itself and any outConflicts.
    3892             :  *
    3893             :  * When the summarize flag is set, we've run short of room for sxact data
    3894             :  * and must summarize to the SLRU.  Predicate locks are transferred to a
    3895             :  * dummy "old" transaction, with duplicate locks on a single target
    3896             :  * collapsing to a single lock with the "latest" commitSeqNo from among
    3897             :  * the conflicting locks..
    3898             :  */
    3899             : static void
    3900        2910 : ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial,
    3901             :                            bool summarize)
    3902             : {
    3903             :     PREDICATELOCK *predlock;
    3904             :     SERIALIZABLEXIDTAG sxidtag;
    3905             :     RWConflict  conflict,
    3906             :                 nextConflict;
    3907             : 
    3908             :     Assert(sxact != NULL);
    3909             :     Assert(SxactIsRolledBack(sxact) || SxactIsCommitted(sxact));
    3910             :     Assert(partial || !SxactIsOnFinishedList(sxact));
    3911             :     Assert(LWLockHeldByMe(SerializableFinishedListLock));
    3912             : 
    3913             :     /*
    3914             :      * First release all the predicate locks held by this xact (or transfer
    3915             :      * them to OldCommittedSxact if summarize is true)
    3916             :      */
    3917        2910 :     LWLockAcquire(SerializablePredicateListLock, LW_SHARED);
    3918        2910 :     if (IsInParallelMode())
    3919           4 :         LWLockAcquire(&sxact->perXactPredicateListLock, LW_EXCLUSIVE);
    3920             :     predlock = (PREDICATELOCK *)
    3921        2910 :         SHMQueueNext(&(sxact->predicateLocks),
    3922        2910 :                      &(sxact->predicateLocks),
    3923             :                      offsetof(PREDICATELOCK, xactLink));
    3924        8468 :     while (predlock)
    3925             :     {
    3926             :         PREDICATELOCK *nextpredlock;
    3927             :         PREDICATELOCKTAG tag;
    3928             :         SHM_QUEUE  *targetLink;
    3929             :         PREDICATELOCKTARGET *target;
    3930             :         PREDICATELOCKTARGETTAG targettag;
    3931             :         uint32      targettaghash;
    3932             :         LWLock     *partitionLock;
    3933             : 
    3934             :         nextpredlock = (PREDICATELOCK *)
    3935        5558 :             SHMQueueNext(&(sxact->predicateLocks),
    3936        5558 :                          &(predlock->xactLink),
    3937             :                          offsetof(PREDICATELOCK, xactLink));
    3938             : 
    3939        5558 :         tag = predlock->tag;
    3940        5558 :         targetLink = &(predlock->targetLink);
    3941        5558 :         target = tag.myTarget;
    3942        5558 :         targettag = target->tag;
    3943        5558 :         targettaghash = PredicateLockTargetTagHashCode(&targettag);
    3944        5558 :         partitionLock = PredicateLockHashPartitionLock(targettaghash);
    3945             : 
    3946        5558 :         LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    3947             : 
    3948        5558 :         SHMQueueDelete(targetLink);
    3949             : 
    3950        5558 :         hash_search_with_hash_value(PredicateLockHash, &tag,
    3951        5558 :                                     PredicateLockHashCodeFromTargetHashCode(&tag,
    3952             :                                                                             targettaghash),
    3953             :                                     HASH_REMOVE, NULL);
    3954        5558 :         if (summarize)
    3955             :         {
    3956             :             bool        found;
    3957             : 
    3958             :             /* Fold into dummy transaction list. */
    3959           0 :             tag.myXact = OldCommittedSxact;
    3960           0 :             predlock = hash_search_with_hash_value(PredicateLockHash, &tag,
    3961           0 :                                                    PredicateLockHashCodeFromTargetHashCode(&tag,
    3962             :                                                                                            targettaghash),
    3963             :                                                    HASH_ENTER_NULL, &found);
    3964           0 :             if (!predlock)
    3965           0 :                 ereport(ERROR,
    3966             :                         (errcode(ERRCODE_OUT_OF_MEMORY),
    3967             :                          errmsg("out of shared memory"),
    3968             :                          errhint("You might need to increase max_pred_locks_per_transaction.")));
    3969           0 :             if (found)
    3970             :             {
    3971             :                 Assert(predlock->commitSeqNo != 0);
    3972             :                 Assert(predlock->commitSeqNo != InvalidSerCommitSeqNo);
    3973           0 :                 if (predlock->commitSeqNo < sxact->commitSeqNo)
    3974           0 :                     predlock->commitSeqNo = sxact->commitSeqNo;
    3975             :             }
    3976             :             else
    3977             :             {
    3978           0 :                 SHMQueueInsertBefore(&(target->predicateLocks),
    3979             :                                      &(predlock->targetLink));
    3980           0 :                 SHMQueueInsertBefore(&(OldCommittedSxact->predicateLocks),
    3981             :                                      &(predlock->xactLink));
    3982           0 :                 predlock->commitSeqNo = sxact->commitSeqNo;
    3983             :             }
    3984             :         }
    3985             :         else
    3986        5558 :             RemoveTargetIfNoLongerUsed(target, targettaghash);
    3987             : 
    3988        5558 :         LWLockRelease(partitionLock);
    3989             : 
    3990        5558 :         predlock = nextpredlock;
    3991             :     }
    3992             : 
    3993             :     /*
    3994             :      * Rather than retail removal, just re-init the head after we've run
    3995             :      * through the list.
    3996             :      */
    3997        2910 :     SHMQueueInit(&sxact->predicateLocks);
    3998             : 
    3999        2910 :     if (IsInParallelMode())
    4000           4 :         LWLockRelease(&sxact->perXactPredicateListLock);
    4001        2910 :     LWLockRelease(SerializablePredicateListLock);
    4002             : 
    4003        2910 :     sxidtag.xid = sxact->topXid;
    4004        2910 :     LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    4005             : 
    4006             :     /* Release all outConflicts (unless 'partial' is true) */
    4007        2910 :     if (!partial)
    4008             :     {
    4009             :         conflict = (RWConflict)
    4010        2894 :             SHMQueueNext(&sxact->outConflicts,
    4011        2894 :                          &sxact->outConflicts,
    4012             :                          offsetof(RWConflictData, outLink));
    4013        2894 :         while (conflict)
    4014             :         {
    4015             :             nextConflict = (RWConflict)
    4016           0 :                 SHMQueueNext(&sxact->outConflicts,
    4017           0 :                              &conflict->outLink,
    4018             :                              offsetof(RWConflictData, outLink));
    4019           0 :             if (summarize)
    4020           0 :                 conflict->sxactIn->flags |= SXACT_FLAG_SUMMARY_CONFLICT_IN;
    4021           0 :             ReleaseRWConflict(conflict);
    4022           0 :             conflict = nextConflict;
    4023             :         }
    4024             :     }
    4025             : 
    4026             :     /* Release all inConflicts. */
    4027             :     conflict = (RWConflict)
    4028        2910 :         SHMQueueNext(&sxact->inConflicts,
    4029        2910 :                      &sxact->inConflicts,
    4030             :                      offsetof(RWConflictData, inLink));
    4031        2910 :     while (conflict)
    4032             :     {
    4033             :         nextConflict = (RWConflict)
    4034           0 :             SHMQueueNext(&sxact->inConflicts,
    4035           0 :                          &conflict->inLink,
    4036             :                          offsetof(RWConflictData, inLink));
    4037           0 :         if (summarize)
    4038           0 :             conflict->sxactOut->flags |= SXACT_FLAG_SUMMARY_CONFLICT_OUT;
    4039           0 :         ReleaseRWConflict(conflict);
    4040           0 :         conflict = nextConflict;
    4041             :     }
    4042             : 
    4043             :     /* Finally, get rid of the xid and the record of the transaction itself. */
    4044        2910 :     if (!partial)
    4045             :     {
    4046        2894 :         if (sxidtag.xid != InvalidTransactionId)
    4047        2464 :             hash_search(SerializableXidHash, &sxidtag, HASH_REMOVE, NULL);
    4048        2894 :         ReleasePredXact(sxact);
    4049             :     }
    4050             : 
    4051        2910 :     LWLockRelease(SerializableXactHashLock);
    4052        2910 : }
    4053             : 
    4054             : /*
    4055             :  * Tests whether the given top level transaction is concurrent with
    4056             :  * (overlaps) our current transaction.
    4057             :  *
    4058             :  * We need to identify the top level transaction for SSI, anyway, so pass
    4059             :  * that to this function to save the overhead of checking the snapshot's
    4060             :  * subxip array.
    4061             :  */
    4062             : static bool
    4063        1038 : XidIsConcurrent(TransactionId xid)
    4064             : {
    4065             :     Snapshot    snap;
    4066             :     uint32      i;
    4067             : 
    4068             :     Assert(TransactionIdIsValid(xid));
    4069             :     Assert(!TransactionIdEquals(xid, GetTopTransactionIdIfAny()));
    4070             : 
    4071        1038 :     snap = GetTransactionSnapshot();
    4072             : 
    4073        1038 :     if (TransactionIdPrecedes(xid, snap->xmin))
    4074           0 :         return false;
    4075             : 
    4076        1038 :     if (TransactionIdFollowsOrEquals(xid, snap->xmax))
    4077        1022 :         return true;
    4078             : 
    4079          16 :     for (i = 0; i < snap->xcnt; i++)
    4080             :     {
    4081          16 :         if (xid == snap->xip[i])
    4082          16 :             return true;
    4083             :     }
    4084             : 
    4085           0 :     return false;
    4086             : }
    4087             : 
    4088             : bool
    4089   386203284 : CheckForSerializableConflictOutNeeded(Relation relation, Snapshot snapshot)
    4090             : {
    4091   386203284 :     if (!SerializationNeededForRead(relation, snapshot))
    4092   386153462 :         return false;
    4093             : 
    4094             :     /* Check if someone else has already decided that we need to die */
    4095       49822 :     if (SxactIsDoomed(MySerializableXact))
    4096             :     {
    4097           0 :         ereport(ERROR,
    4098             :                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    4099             :                  errmsg("could not serialize access due to read/write dependencies among transactions"),
    4100             :                  errdetail_internal("Reason code: Canceled on identification as a pivot, during conflict out checking."),
    4101             :                  errhint("The transaction might succeed if retried.")));
    4102             :     }
    4103             : 
    4104       49822 :     return true;
    4105             : }
    4106             : 
    4107             : /*
    4108             :  * CheckForSerializableConflictOut
    4109             :  *      A table AM is reading a tuple that has been modified.  If it determines
    4110             :  *      that the tuple version it is reading is not visible to us, it should
    4111             :  *      pass in the top level xid of the transaction that created it.
    4112             :  *      Otherwise, if it determines that it is visible to us but it has been
    4113             :  *      deleted or there is a newer version available due to an update, it
    4114             :  *      should pass in the top level xid of the modifying transaction.
    4115             :  *
    4116             :  * This function will check for overlap with our own transaction.  If the given
    4117             :  * xid is also serializable and the transactions overlap (i.e., they cannot see
    4118             :  * each other's writes), then we have a conflict out.
    4119             :  */
    4120             : void
    4121        1100 : CheckForSerializableConflictOut(Relation relation, TransactionId xid, Snapshot snapshot)
    4122             : {
    4123             :     SERIALIZABLEXIDTAG sxidtag;
    4124             :     SERIALIZABLEXID *sxid;
    4125             :     SERIALIZABLEXACT *sxact;
    4126             : 
    4127        1100 :     if (!SerializationNeededForRead(relation, snapshot))
    4128         388 :         return;
    4129             : 
    4130             :     /* Check if someone else has already decided that we need to die */
    4131        1100 :     if (SxactIsDoomed(MySerializableXact))
    4132             :     {
    4133           0 :         ereport(ERROR,
    4134             :                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    4135             :                  errmsg("could not serialize access due to read/write dependencies among transactions"),
    4136             :                  errdetail_internal("Reason code: Canceled on identification as a pivot, during conflict out checking."),
    4137             :                  errhint("The transaction might succeed if retried.")));
    4138             :     }
    4139             :     Assert(TransactionIdIsValid(xid));
    4140             : 
    4141        1100 :     if (TransactionIdEquals(xid, GetTopTransactionIdIfAny()))
    4142           0 :         return;
    4143             : 
    4144             :     /*
    4145             :      * Find sxact or summarized info for the top level xid.
    4146             :      */
    4147        1100 :     sxidtag.xid = xid;
    4148        1100 :     LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    4149             :     sxid = (SERIALIZABLEXID *)
    4150        1100 :         hash_search(SerializableXidHash, &sxidtag, HASH_FIND, NULL);
    4151        1100 :     if (!sxid)
    4152             :     {
    4153             :         /*
    4154             :          * Transaction not found in "normal" SSI structures.  Check whether it
    4155             :          * got pushed out to SLRU storage for "old committed" transactions.
    4156             :          */
    4157             :         SerCommitSeqNo conflictCommitSeqNo;
    4158             : 
    4159          42 :         conflictCommitSeqNo = SerialGetMinConflictCommitSeqNo(xid);
    4160          42 :         if (conflictCommitSeqNo != 0)
    4161             :         {
    4162           0 :             if (conflictCommitSeqNo != InvalidSerCommitSeqNo
    4163           0 :                 && (!SxactIsReadOnly(MySerializableXact)
    4164           0 :                     || conflictCommitSeqNo
    4165           0 :                     <= MySerializableXact->SeqNo.lastCommitBeforeSnapshot))
    4166           0 :                 ereport(ERROR,
    4167             :                         (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    4168             :                          errmsg("could not serialize access due to read/write dependencies among transactions"),
    4169             :                          errdetail_internal("Reason code: Canceled on conflict out to old pivot %u.", xid),
    4170             :                          errhint("The transaction might succeed if retried.")));
    4171             : 
    4172           0 :             if (SxactHasSummaryConflictIn(MySerializableXact)
    4173           0 :                 || !SHMQueueEmpty(&MySerializableXact->inConflicts))
    4174           0 :                 ereport(ERROR,
    4175             :                         (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    4176             :                          errmsg("could not serialize access due to read/write dependencies among transactions"),
    4177             :                          errdetail_internal("Reason code: Canceled on identification as a pivot, with conflict out to old committed transaction %u.", xid),
    4178             :                          errhint("The transaction might succeed if retried.")));
    4179             : 
    4180           0 :             MySerializableXact->flags |= SXACT_FLAG_SUMMARY_CONFLICT_OUT;
    4181             :         }
    4182             : 
    4183             :         /* It's not serializable or otherwise not important. */
    4184          42 :         LWLockRelease(SerializableXactHashLock);
    4185          42 :         return;
    4186             :     }
    4187        1058 :     sxact = sxid->myXact;
    4188             :     Assert(TransactionIdEquals(sxact->topXid, xid));
    4189        1058 :     if (sxact == MySerializableXact || SxactIsDoomed(sxact))
    4190             :     {
    4191             :         /* Can't conflict with ourself or a transaction that will roll back. */
    4192           8 :         LWLockRelease(SerializableXactHashLock);
    4193           8 :         return;
    4194             :     }
    4195             : 
    4196             :     /*
    4197             :      * We have a conflict out to a transaction which has a conflict out to a
    4198             :      * summarized transaction.  That summarized transaction must have
    4199             :      * committed first, and we can't tell when it committed in relation to our
    4200             :      * snapshot acquisition, so something needs to be canceled.
    4201             :      */
    4202        1050 :     if (SxactHasSummaryConflictOut(sxact))
    4203             :     {
    4204           0 :         if (!SxactIsPrepared(sxact))
    4205             :         {
    4206           0 :             sxact->flags |= SXACT_FLAG_DOOMED;
    4207           0 :             LWLockRelease(SerializableXactHashLock);
    4208           0 :             return;
    4209             :         }
    4210             :         else
    4211             :         {
    4212           0 :             LWLockRelease(SerializableXactHashLock);
    4213           0 :             ereport(ERROR,
    4214             :                     (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    4215             :                      errmsg("could not serialize access due to read/write dependencies among transactions"),
    4216             :                      errdetail_internal("Reason code: Canceled on conflict out to old pivot."),
    4217             :                      errhint("The transaction might succeed if retried.")));
    4218             :         }
    4219             :     }
    4220             : 
    4221             :     /*
    4222             :      * If this is a read-only transaction and the writing transaction has
    4223             :      * committed, and it doesn't have a rw-conflict to a transaction which
    4224             :      * committed before it, no conflict.
    4225             :      */
    4226        1050 :     if (SxactIsReadOnly(MySerializableXact)
    4227         238 :         && SxactIsCommitted(sxact)
    4228          16 :         && !SxactHasSummaryConflictOut(sxact)
    4229          16 :         && (!SxactHasConflictOut(sxact)
    4230           4 :             || MySerializableXact->SeqNo.lastCommitBeforeSnapshot < sxact->SeqNo.earliestOutConflictCommit))
    4231             :     {
    4232             :         /* Read-only transaction will appear to run first.  No conflict. */
    4233          12 :         LWLockRelease(SerializableXactHashLock);
    4234          12 :         return;
    4235             :     }
    4236             : 
    4237        1038 :     if (!XidIsConcurrent(xid))
    4238             :     {
    4239             :         /* This write was already in our snapshot; no conflict. */
    4240           0 :         LWLockRelease(SerializableXactHashLock);
    4241           0 :         return;
    4242             :     }
    4243             : 
    4244        1038 :     if (RWConflictExists(MySerializableXact, sxact))
    4245             :     {
    4246             :         /* We don't want duplicate conflict records in the list. */
    4247         326 :         LWLockRelease(SerializableXactHashLock);
    4248         326 :         return;
    4249             :     }
    4250             : 
    4251             :     /*
    4252             :      * Flag the conflict.  But first, if this conflict creates a dangerous
    4253             :      * structure, ereport an error.
    4254             :      */
    4255         712 :     FlagRWConflict(MySerializableXact, sxact);
    4256         686 :     LWLockRelease(SerializableXactHashLock);
    4257             : }
    4258             : 
    4259             : /*
    4260             :  * Check a particular target for rw-dependency conflict in. A subroutine of
    4261             :  * CheckForSerializableConflictIn().
    4262             :  */
    4263             : static void
    4264       14766 : CheckTargetForConflictsIn(PREDICATELOCKTARGETTAG *targettag)
    4265             : {
    4266             :     uint32      targettaghash;
    4267             :     LWLock     *partitionLock;
    4268             :     PREDICATELOCKTARGET *target;
    4269             :     PREDICATELOCK *predlock;
    4270       14766 :     PREDICATELOCK *mypredlock = NULL;
    4271             :     PREDICATELOCKTAG mypredlocktag;
    4272             : 
    4273             :     Assert(MySerializableXact != InvalidSerializableXact);
    4274             : 
    4275             :     /*
    4276             :      * The same hash and LW lock apply to the lock target and the lock itself.
    4277             :      */
    4278       14766 :     targettaghash = PredicateLockTargetTagHashCode(targettag);
    4279       14766 :     partitionLock = PredicateLockHashPartitionLock(targettaghash);
    4280       14766 :     LWLockAcquire(partitionLock, LW_SHARED);
    4281             :     target = (PREDICATELOCKTARGET *)
    4282       14766 :         hash_search_with_hash_value(PredicateLockTargetHash,
    4283             :                                     targettag, targettaghash,
    4284             :                                     HASH_FIND, NULL);
    4285       14766 :     if (!target)
    4286             :     {
    4287             :         /* Nothing has this target locked; we're done here. */
    4288       11084 :         LWLockRelease(partitionLock);
    4289       11084 :         return;
    4290             :     }
    4291             : 
    4292             :     /*
    4293             :      * Each lock for an overlapping transaction represents a conflict: a
    4294             :      * rw-dependency in to this transaction.
    4295             :      */
    4296             :     predlock = (PREDICATELOCK *)
    4297        3682 :         SHMQueueNext(&(target->predicateLocks),
    4298        3682 :                      &(target->predicateLocks),
    4299             :                      offsetof(PREDICATELOCK, targetLink));
    4300        3682 :     LWLockAcquire(SerializableXactHashLock, LW_SHARED);
    4301        8288 :     while (predlock)
    4302             :     {
    4303             :         SHM_QUEUE  *predlocktargetlink;
    4304             :         PREDICATELOCK *nextpredlock;
    4305             :         SERIALIZABLEXACT *sxact;
    4306             : 
    4307        4738 :         predlocktargetlink = &(predlock->targetLink);
    4308             :         nextpredlock = (PREDICATELOCK *)
    4309        4738 :             SHMQueueNext(&(target->predicateLocks),
    4310             :                          predlocktargetlink,
    4311             :                          offsetof(PREDICATELOCK, targetLink));
    4312             : 
    4313        4738 :         sxact = predlock->tag.myXact;
    4314        4738 :         if (sxact == MySerializableXact)
    4315             :         {
    4316             :             /*
    4317             :              * If we're getting a write lock on a tuple, we don't need a
    4318             :              * predicate (SIREAD) lock on the same tuple. We can safely remove
    4319             :              * our SIREAD lock, but we'll defer doing so until after the loop
    4320             :              * because that requires upgrading to an exclusive partition lock.
    4321             :              *
    4322             :              * We can't use this optimization within a subtransaction because
    4323             :              * the subtransaction could roll back, and we would be left
    4324             :              * without any lock at the top level.
    4325             :              */
    4326        3078 :             if (!IsSubTransaction()
    4327        3078 :                 && GET_PREDICATELOCKTARGETTAG_OFFSET(*targettag))
    4328             :             {
    4329         776 :                 mypredlock = predlock;
    4330         776 :                 mypredlocktag = predlock->tag;
    4331             :             }
    4332             :         }
    4333        1660 :         else if (!SxactIsDoomed(sxact)
    4334        1660 :                  && (!SxactIsCommitted(sxact)
    4335         166 :                      || TransactionIdPrecedes(GetTransactionSnapshot()->xmin,
    4336             :                                               sxact->finishedBefore))
    4337        1642 :                  && !RWConflictExists(sxact, MySerializableXact))
    4338             :         {
    4339         970 :             LWLockRelease(SerializableXactHashLock);
    4340         970 :             LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    4341             : 
    4342             :             /*
    4343             :              * Re-check after getting exclusive lock because the other
    4344             :              * transaction may have flagged a conflict.
    4345             :              */
    4346         970 :             if (!SxactIsDoomed(sxact)
    4347         970 :                 && (!SxactIsCommitted(sxact)
    4348         148 :                     || TransactionIdPrecedes(GetTransactionSnapshot()->xmin,
    4349             :                                              sxact->finishedBefore))
    4350         970 :                 && !RWConflictExists(sxact, MySerializableXact))
    4351             :             {
    4352         970 :                 FlagRWConflict(sxact, MySerializableXact);
    4353             :             }
    4354             : 
    4355         838 :             LWLockRelease(SerializableXactHashLock);
    4356         838 :             LWLockAcquire(SerializableXactHashLock, LW_SHARED);
    4357             :         }
    4358             : 
    4359        4606 :         predlock = nextpredlock;
    4360             :     }
    4361        3550 :     LWLockRelease(SerializableXactHashLock);
    4362        3550 :     LWLockRelease(partitionLock);
    4363             : 
    4364             :     /*
    4365             :      * If we found one of our own SIREAD locks to remove, remove it now.
    4366             :      *
    4367             :      * At this point our transaction already has a RowExclusiveLock on the
    4368             :      * relation, so we are OK to drop the predicate lock on the tuple, if
    4369             :      * found, without fearing that another write against the tuple will occur
    4370             :      * before the MVCC information makes it to the buffer.
    4371             :      */
    4372        3550 :     if (mypredlock != NULL)
    4373             :     {
    4374             :         uint32      predlockhashcode;
    4375             :         PREDICATELOCK *rmpredlock;
    4376             : 
    4377         762 :         LWLockAcquire(SerializablePredicateListLock, LW_SHARED);
    4378         762 :         if (IsInParallelMode())
    4379           0 :             LWLockAcquire(&MySerializableXact->perXactPredicateListLock, LW_EXCLUSIVE);
    4380         762 :         LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    4381         762 :         LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    4382             : 
    4383             :         /*
    4384             :          * Remove the predicate lock from shared memory, if it wasn't removed
    4385             :          * while the locks were released.  One way that could happen is from
    4386             :          * autovacuum cleaning up an index.
    4387             :          */
    4388         762 :         predlockhashcode = PredicateLockHashCodeFromTargetHashCode
    4389             :             (&mypredlocktag, targettaghash);
    4390             :         rmpredlock = (PREDICATELOCK *)
    4391         762 :             hash_search_with_hash_value(PredicateLockHash,
    4392             :                                         &mypredlocktag,
    4393             :                                         predlockhashcode,
    4394             :                                         HASH_FIND, NULL);
    4395         762 :         if (rmpredlock != NULL)
    4396             :         {
    4397             :             Assert(rmpredlock == mypredlock);
    4398             : 
    4399         762 :             SHMQueueDelete(&(mypredlock->targetLink));
    4400         762 :             SHMQueueDelete(&(mypredlock->xactLink));
    4401             : 
    4402             :             rmpredlock = (PREDICATELOCK *)
    4403         762 :                 hash_search_with_hash_value(PredicateLockHash,
    4404             :                                             &mypredlocktag,
    4405             :                                             predlockhashcode,
    4406             :                                             HASH_REMOVE, NULL);
    4407             :             Assert(rmpredlock == mypredlock);
    4408             : 
    4409         762 :             RemoveTargetIfNoLongerUsed(target, targettaghash);
    4410             :         }
    4411             : 
    4412         762 :         LWLockRelease(SerializableXactHashLock);
    4413         762 :         LWLockRelease(partitionLock);
    4414         762 :         if (IsInParallelMode())
    4415           0 :             LWLockRelease(&MySerializableXact->perXactPredicateListLock);
    4416         762 :         LWLockRelease(SerializablePredicateListLock);
    4417             : 
    4418         762 :         if (rmpredlock != NULL)
    4419             :         {
    4420             :             /*
    4421             :              * Remove entry in local lock table if it exists. It's OK if it
    4422             :              * doesn't exist; that means the lock was transferred to a new
    4423             :              * target by a different backend.
    4424             :              */
    4425         762 :             hash_search_with_hash_value(LocalPredicateLockHash,
    4426             :                                         targettag, targettaghash,
    4427             :                                         HASH_REMOVE, NULL);
    4428             : 
    4429         762 :             DecrementParentLocks(targettag);
    4430             :         }
    4431             :     }
    4432             : }
    4433             : 
    4434             : /*
    4435             :  * CheckForSerializableConflictIn
    4436             :  *      We are writing the given tuple.  If that indicates a rw-conflict
    4437             :  *      in from another serializable transaction, take appropriate action.
    4438             :  *
    4439             :  * Skip checking for any granularity for which a parameter is missing.
    4440             :  *
    4441             :  * A tuple update or delete is in conflict if we have a predicate lock
    4442             :  * against the relation or page in which the tuple exists, or against the
    4443             :  * tuple itself.
    4444             :  */
    4445             : void
    4446    35450274 : CheckForSerializableConflictIn(Relation relation, ItemPointer tid, BlockNumber blkno)
    4447             : {
    4448             :     PREDICATELOCKTARGETTAG targettag;
    4449             : 
    4450    35450274 :     if (!SerializationNeededForWrite(relation))
    4451    35441526 :         return;
    4452             : 
    4453             :     /* Check if someone else has already decided that we need to die */
    4454        8748 :     if (SxactIsDoomed(MySerializableXact))
    4455           2 :         ereport(ERROR,
    4456             :                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    4457             :                  errmsg("could not serialize access due to read/write dependencies among transactions"),
    4458             :                  errdetail_internal("Reason code: Canceled on identification as a pivot, during conflict in checking."),
    4459             :                  errhint("The transaction might succeed if retried.")));
    4460             : 
    4461             :     /*
    4462             :      * We're doing a write which might cause rw-conflicts now or later.
    4463             :      * Memorize that fact.
    4464             :      */
    4465        8746 :     MyXactDidWrite = true;
    4466             : 
    4467             :     /*
    4468             :      * It is important that we check for locks from the finest granularity to
    4469             :      * the coarsest granularity, so that granularity promotion doesn't cause
    4470             :      * us to miss a lock.  The new (coarser) lock will be acquired before the
    4471             :      * old (finer) locks are released.
    4472             :      *
    4473             :      * It is not possible to take and hold a lock across the checks for all
    4474             :      * granularities because each target could be in a separate partition.
    4475             :      */
    4476        8746 :     if (tid != NULL)
    4477             :     {
    4478        1252 :         SET_PREDICATELOCKTARGETTAG_TUPLE(targettag,
    4479             :                                          relation->rd_node.dbNode,
    4480             :                                          relation->rd_id,
    4481             :                                          ItemPointerGetBlockNumber(tid),
    4482             :                                          ItemPointerGetOffsetNumber(tid));
    4483        1252 :         CheckTargetForConflictsIn(&targettag);
    4484             :     }
    4485             : 
    4486        8700 :     if (blkno != InvalidBlockNumber)
    4487             :     {
    4488        4874 :         SET_PREDICATELOCKTARGETTAG_PAGE(targettag,
    4489             :                                         relation->rd_node.dbNode,
    4490             :                                         relation->rd_id,
    4491             :                                         blkno);
    4492        4874 :         CheckTargetForConflictsIn(&targettag);
    4493             :     }
    4494             : 
    4495        8640 :     SET_PREDICATELOCKTARGETTAG_RELATION(targettag,
    4496             :                                         relation->rd_node.dbNode,
    4497             :                                         relation->rd_id);
    4498        8640 :     CheckTargetForConflictsIn(&targettag);
    4499             : }
    4500             : 
    4501             : /*
    4502             :  * CheckTableForSerializableConflictIn
    4503             :  *      The entire table is going through a DDL-style logical mass delete
    4504             :  *      like TRUNCATE or DROP TABLE.  If that causes a rw-conflict in from
    4505             :  *      another serializable transaction, take appropriate action.
    4506             :  *
    4507             :  * While these operations do not operate entirely within the bounds of
    4508             :  * snapshot isolation, they can occur inside a serializable transaction, and
    4509             :  * will logically occur after any reads which saw rows which were destroyed
    4510             :  * by these operations, so we do what we can to serialize properly under
    4511             :  * SSI.
    4512             :  *
    4513             :  * The relation passed in must be a heap relation. Any predicate lock of any
    4514             :  * granularity on the heap will cause a rw-conflict in to this transaction.
    4515             :  * Predicate locks on indexes do not matter because they only exist to guard
    4516             :  * against conflicting inserts into the index, and this is a mass *delete*.
    4517             :  * When a table is truncated or dropped, the index will also be truncated
    4518             :  * or dropped, and we'll deal with locks on the index when that happens.
    4519             :  *
    4520             :  * Dropping or truncating a table also needs to drop any existing predicate
    4521             :  * locks on heap tuples or pages, because they're about to go away. This
    4522             :  * should be done before altering the predicate locks because the transaction
    4523             :  * could be rolled back because of a conflict, in which case the lock changes
    4524             :  * are not needed. (At the moment, we don't actually bother to drop the
    4525             :  * existing locks on a dropped or truncated table at the moment. That might
    4526             :  * lead to some false positives, but it doesn't seem worth the trouble.)
    4527             :  */
    4528             : void
    4529       26832 : CheckTableForSerializableConflictIn(Relation relation)
    4530             : {
    4531             :     HASH_SEQ_STATUS seqstat;
    4532             :     PREDICATELOCKTARGET *target;
    4533             :     Oid         dbId;
    4534             :     Oid         heapId;
    4535             :     int         i;
    4536             : 
    4537             :     /*
    4538             :      * Bail out quickly if there are no serializable transactions running.
    4539             :      * It's safe to check this without taking locks because the caller is
    4540             :      * holding an ACCESS EXCLUSIVE lock on the relation.  No new locks which
    4541             :      * would matter here can be acquired while that is held.
    4542             :      */
    4543       26832 :     if (!TransactionIdIsValid(PredXact->SxactGlobalXmin))
    4544       26828 :         return;
    4545             : 
    4546         174 :     if (!SerializationNeededForWrite(relation))
    4547         170 :         return;
    4548             : 
    4549             :     /*
    4550             :      * We're doing a write which might cause rw-conflicts now or later.
    4551             :      * Memorize that fact.
    4552             :      */
    4553           4 :     MyXactDidWrite = true;
    4554             : 
    4555             :     Assert(relation->rd_index == NULL); /* not an index relation */
    4556             : 
    4557           4 :     dbId = relation->rd_node.dbNode;
    4558           4 :     heapId = relation->rd_id;
    4559             : 
    4560           4 :     LWLockAcquire(SerializablePredicateListLock, LW_EXCLUSIVE);
    4561          68 :     for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
    4562          64 :         LWLockAcquire(PredicateLockHashPartitionLockByIndex(i), LW_SHARED);
    4563           4 :     LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    4564             : 
    4565             :     /* Scan through target list */
    4566           4 :     hash_seq_init(&seqstat, PredicateLockTargetHash);
    4567             : 
    4568           8 :     while ((target = (PREDICATELOCKTARGET *) hash_seq_search(&seqstat)))
    4569             :     {
    4570             :         PREDICATELOCK *predlock;
    4571             : 
    4572             :         /*
    4573             :          * Check whether this is a target which needs attention.
    4574             :          */
    4575           4 :         if (GET_PREDICATELOCKTARGETTAG_RELATION(target->tag) != heapId)
    4576           4 :             continue;           /* wrong relation id */
    4577           0 :         if (GET_PREDICATELOCKTARGETTAG_DB(target->tag) != dbId)
    4578           0 :             continue;           /* wrong database id */
    4579             : 
    4580             :         /*
    4581             :          * Loop through locks for this target and flag conflicts.
    4582             :          */
    4583             :         predlock = (PREDICATELOCK *)
    4584           0 :             SHMQueueNext(&(target->predicateLocks),
    4585           0 :                          &(target->predicateLocks),
    4586             :                          offsetof(PREDICATELOCK, targetLink));
    4587           0 :         while (predlock)
    4588             :         {
    4589             :             PREDICATELOCK *nextpredlock;
    4590             : 
    4591             :             nextpredlock = (PREDICATELOCK *)
    4592           0 :                 SHMQueueNext(&(target->predicateLocks),
    4593           0 :                              &(predlock->targetLink),
    4594             :                              offsetof(PREDICATELOCK, targetLink));
    4595             : 
    4596           0 :             if (predlock->tag.myXact != MySerializableXact
    4597           0 :                 && !RWConflictExists(predlock->tag.myXact, MySerializableXact))
    4598             :             {
    4599           0 :                 FlagRWConflict(predlock->tag.myXact, MySerializableXact);
    4600             :             }
    4601             : 
    4602           0 :             predlock = nextpredlock;
    4603             :         }
    4604             :     }
    4605             : 
    4606             :     /* Release locks in reverse order */
    4607           4 :     LWLockRelease(SerializableXactHashLock);
    4608          68 :     for (i = NUM_PREDICATELOCK_PARTITIONS - 1; i >= 0; i--)
    4609          64 :         LWLockRelease(PredicateLockHashPartitionLockByIndex(i));
    4610           4 :     LWLockRelease(SerializablePredicateListLock);
    4611             : }
    4612             : 
    4613             : 
    4614             : /*
    4615             :  * Flag a rw-dependency between two serializable transactions.
    4616             :  *
    4617             :  * The caller is responsible for ensuring that we have a LW lock on
    4618             :  * the transaction hash table.
    4619             :  */
    4620             : static void
    4621        1682 : FlagRWConflict(SERIALIZABLEXACT *reader, SERIALIZABLEXACT *writer)
    4622             : {
    4623             :     Assert(reader != writer);
    4624             : 
    4625             :     /* First, see if this conflict causes failure. */
    4626        1682 :     OnConflict_CheckForSerializationFailure(reader, writer);
    4627             : 
    4628             :     /* Actually do the conflict flagging. */
    4629        1524 :     if (reader == OldCommittedSxact)
    4630           0 :         writer->flags |= SXACT_FLAG_SUMMARY_CONFLICT_IN;
    4631        1524 :     else if (writer == OldCommittedSxact)
    4632           0 :         reader->flags |= SXACT_FLAG_SUMMARY_CONFLICT_OUT;
    4633             :     else
    4634        1524 :         SetRWConflict(reader, writer);
    4635        1524 : }
    4636             : 
    4637             : /*----------------------------------------------------------------------------
    4638             :  * We are about to add a RW-edge to the dependency graph - check that we don't
    4639             :  * introduce a dangerous structure by doing so, and abort one of the
    4640             :  * transactions if so.
    4641             :  *
    4642             :  * A serialization failure can only occur if there is a dangerous structure
    4643             :  * in the dependency graph:
    4644             :  *
    4645             :  *      Tin ------> Tpivot ------> Tout
    4646             :  *            rw             rw
    4647             :  *
    4648             :  * Furthermore, Tout must commit first.
    4649             :  *
    4650             :  * One more optimization is that if Tin is declared READ ONLY (or commits
    4651             :  * without writing), we can only have a problem if Tout committed before Tin
    4652             :  * acquired its snapshot.
    4653             :  *----------------------------------------------------------------------------
    4654             :  */
    4655             : static void
    4656        1682 : OnConflict_CheckForSerializationFailure(const SERIALIZABLEXACT *reader,
    4657             :                                         SERIALIZABLEXACT *writer)
    4658             : {
    4659             :     bool        failure;
    4660             :     RWConflict  conflict;
    4661             : 
    4662             :     Assert(LWLockHeldByMe(SerializableXactHashLock));
    4663             : 
    4664        1682 :     failure = false;
    4665             : 
    4666             :     /*------------------------------------------------------------------------
    4667             :      * Check for already-committed writer with rw-conflict out flagged
    4668             :      * (conflict-flag on W means that T2 committed before W):
    4669             :      *
    4670             :      *      R ------> W ------> T2
    4671             :      *          rw        rw
    4672             :      *
    4673             :      * That is a dangerous structure, so we must abort. (Since the writer
    4674             :      * has already committed, we must be the reader)
    4675             :      *------------------------------------------------------------------------
    4676             :      */
    4677        1682 :     if (SxactIsCommitted(writer)
    4678          36 :         && (SxactHasConflictOut(writer) || SxactHasSummaryConflictOut(writer)))
    4679           4 :         failure = true;
    4680             : 
    4681             :     /*------------------------------------------------------------------------
    4682             :      * Check whether the writer has become a pivot with an out-conflict
    4683             :      * committed transaction (T2), and T2 committed first:
    4684             :      *
    4685             :      *      R ------> W ------> T2
    4686             :      *          rw        rw
    4687             :      *
    4688             :      * Because T2 must've committed first, there is no anomaly if:
    4689             :      * - the reader committed before T2
    4690             :      * - the writer committed before T2
    4691             :      * - the reader is a READ ONLY transaction and the reader was concurrent
    4692             :      *   with T2 (= reader acquired its snapshot before T2 committed)
    4693             :      *
    4694             :      * We also handle the case that T2 is prepared but not yet committed
    4695             :      * here. In that case T2 has already checked for conflicts, so if it
    4696             :      * commits first, making the above conflict real, it's too late for it
    4697             :      * to abort.
    4698             :      *------------------------------------------------------------------------
    4699             :      */
    4700        1682 :     if (!failure)
    4701             :     {
    4702        1678 :         if (SxactHasSummaryConflictOut(writer))
    4703             :         {
    4704           0 :             failure = true;
    4705           0 :             conflict = NULL;
    4706             :         }
    4707             :         else
    4708             :             conflict = (RWConflict)
    4709        1678 :                 SHMQueueNext(&writer->outConflicts,
    4710        1678 :                              &writer->outConflicts,
    4711             :                              offsetof(RWConflictData, outLink));
    4712        2088 :         while (conflict)
    4713             :         {
    4714         558 :             SERIALIZABLEXACT *t2 = conflict->sxactIn;
    4715             : 
    4716         558 :             if (SxactIsPrepared(t2)
    4717         160 :                 && (!SxactIsCommitted(reader)
    4718         130 :                     || t2->prepareSeqNo <= reader->commitSeqNo)
    4719         160 :                 && (!SxactIsCommitted(writer)
    4720           0 :                     || t2->prepareSeqNo <= writer->commitSeqNo)
    4721         160 :                 && (!SxactIsReadOnly(reader)
    4722          24 :                     || t2->prepareSeqNo <= reader->SeqNo.lastCommitBeforeSnapshot))
    4723             :             {
    4724         148 :                 failure = true;
    4725         148 :                 break;
    4726             :             }
    4727             :             conflict = (RWConflict)
    4728         410 :                 SHMQueueNext(&writer->outConflicts,
    4729         410 :                              &conflict->outLink,
    4730             :                              offsetof(RWConflictData, outLink));
    4731             :         }
    4732             :     }
    4733             : 
    4734             :     /*------------------------------------------------------------------------
    4735             :      * Check whether the reader has become a pivot with a writer
    4736             :      * that's committed (or prepared):
    4737             :      *
    4738             :      *      T0 ------> R ------> W
    4739             :      *           rw        rw
    4740             :      *
    4741             :      * Because W must've committed first for an anomaly to occur, there is no
    4742             :      * anomaly if:
    4743             :      * - T0 committed before the writer
    4744             :      * - T0 is READ ONLY, and overlaps the writer
    4745             :      *------------------------------------------------------------------------
    4746             :      */
    4747        1682 :     if (!failure && SxactIsPrepared(writer) && !SxactIsReadOnly(reader))
    4748             :     {
    4749          34 :         if (SxactHasSummaryConflictIn(reader))
    4750             :         {
    4751           0 :             failure = true;
    4752           0 :             conflict = NULL;
    4753             :         }
    4754             :         else
    4755             :             conflict = (RWConflict)
    4756          34 :                 SHMQueueNext(&reader->inConflicts,
    4757             :                              &reader->inConflicts,
    4758             :                              offsetof(RWConflictData, inLink));
    4759          34 :         while (conflict)
    4760             :         {
    4761          22 :             SERIALIZABLEXACT *t0 = conflict->sxactOut;
    4762             : 
    4763          22 :             if (!SxactIsDoomed(t0)
    4764          22 :                 && (!SxactIsCommitted(t0)
    4765          22 :                     || t0->commitSeqNo >= writer->prepareSeqNo)
    4766          22 :                 && (!SxactIsReadOnly(t0)
    4767           0 :                     || t0->SeqNo.lastCommitBeforeSnapshot >= writer->prepareSeqNo))
    4768             :             {
    4769          22 :                 failure = true;
    4770          22 :                 break;
    4771             :             }
    4772             :             conflict = (RWConflict)
    4773           0 :                 SHMQueueNext(&reader->inConflicts,
    4774           0 :                              &conflict->inLink,
    4775             :                              offsetof(RWConflictData, inLink));
    4776             :         }
    4777             :     }
    4778             : 
    4779        1682 :     if (failure)
    4780             :     {
    4781             :         /*
    4782             :          * We have to kill a transaction to avoid a possible anomaly from
    4783             :          * occurring. If the writer is us, we can just ereport() to cause a
    4784             :          * transaction abort. Otherwise we flag the writer for termination,
    4785             :          * causing it to abort when it tries to commit. However, if the writer
    4786             :          * is a prepared transaction, already prepared, we can't abort it
    4787             :          * anymore, so we have to kill the reader instead.
    4788             :          */
    4789         174 :         if (MySerializableXact == writer)
    4790             :         {
    4791         132 :             LWLockRelease(SerializableXactHashLock);
    4792         132 :             ereport(ERROR,
    4793             :                     (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    4794             :                      errmsg("could not serialize access due to read/write dependencies among transactions"),
    4795             :                      errdetail_internal("Reason code: Canceled on identification as a pivot, during write."),
    4796             :                      errhint("The transaction might succeed if retried.")));
    4797             :         }
    4798          42 :         else if (SxactIsPrepared(writer))
    4799             :         {
    4800          26 :             LWLockRelease(SerializableXactHashLock);
    4801             : 
    4802             :             /* if we're not the writer, we have to be the reader */
    4803             :             Assert(MySerializableXact == reader);
    4804          26 :             ereport(ERROR,
    4805             :                     (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    4806             :                      errmsg("could not serialize access due to read/write dependencies among transactions"),
    4807             :                      errdetail_internal("Reason code: Canceled on conflict out to pivot %u, during read.", writer->topXid),
    4808             :                      errhint("The transaction might succeed if retried.")));
    4809             :         }
    4810          16 :         writer->flags |= SXACT_FLAG_DOOMED;
    4811             :     }
    4812        1524 : }
    4813             : 
    4814             : /*
    4815             :  * PreCommit_CheckForSerializationFailure
    4816             :  *      Check for dangerous structures in a serializable transaction
    4817             :  *      at commit.
    4818             :  *
    4819             :  * We're checking for a dangerous structure as each conflict is recorded.
    4820             :  * The only way we could have a problem at commit is if this is the "out"
    4821             :  * side of a pivot, and neither the "in" side nor the pivot has yet
    4822             :  * committed.
    4823             :  *
    4824             :  * If a dangerous structure is found, the pivot (the near conflict) is
    4825             :  * marked for death, because rolling back another transaction might mean
    4826             :  * that we fail without ever making progress.  This transaction is
    4827             :  * committing writes, so letting it commit ensures progress.  If we
    4828             :  * canceled the far conflict, it might immediately fail again on retry.
    4829             :  */
    4830             : void
    4831      703486 : PreCommit_CheckForSerializationFailure(void)
    4832             : {
    4833             :     RWConflict  nearConflict;
    4834             : 
    4835      703486 :     if (MySerializableXact == InvalidSerializableXact)
    4836      700872 :         return;
    4837             : 
    4838             :     Assert(IsolationIsSerializable());
    4839             : 
    4840        2614 :     LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    4841             : 
    4842             :     /* Check if someone else has already decided that we need to die */
    4843        2614 :     if (SxactIsDoomed(MySerializableXact))
    4844             :     {
    4845             :         Assert(!SxactIsPartiallyReleased(MySerializableXact));
    4846         294 :         LWLockRelease(SerializableXactHashLock);
    4847         294 :         ereport(ERROR,
    4848             :                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    4849             :                  errmsg("could not serialize access due to read/write dependencies among transactions"),
    4850             :                  errdetail_internal("Reason code: Canceled on identification as a pivot, during commit attempt."),
    4851             :                  errhint("The transaction might succeed if retried.")));
    4852             :     }
    4853             : 
    4854             :     nearConflict = (RWConflict)
    4855        2320 :         SHMQueueNext(&MySerializableXact->inConflicts,
    4856        2320 :                      &MySerializableXact->inConflicts,
    4857             :                      offsetof(RWConflictData, inLink));
    4858        3504 :     while (nearConflict)
    4859             :     {
    4860        1184 :         if (!SxactIsCommitted(nearConflict->sxactOut)
    4861         816 :             && !SxactIsDoomed(nearConflict->sxactOut))
    4862             :         {
    4863             :             RWConflict  farConflict;
    4864             : 
    4865             :             farConflict = (RWConflict)
    4866         816 :                 SHMQueueNext(&nearConflict->sxactOut->inConflicts,
    4867         816 :                              &nearConflict->sxactOut->inConflicts,
    4868             :                              offsetof(RWConflictData, inLink));
    4869         876 :             while (farConflict)
    4870             :             {
    4871         340 :                 if (farConflict->sxactOut == MySerializableXact
    4872          84 :                     || (!SxactIsCommitted(farConflict->sxactOut)
    4873          48 :                         && !SxactIsReadOnly(farConflict->sxactOut)
    4874          24 :                         && !SxactIsDoomed(farConflict->sxactOut)))
    4875             :                 {
    4876             :                     /*
    4877             :                      * Normally, we kill the pivot transaction to make sure we
    4878             :                      * make progress if the failing transaction is retried.
    4879             :                      * However, we can't kill it if it's already prepared, so
    4880             :                      * in that case we commit suicide instead.
    4881             :                      */
    4882         280 :                     if (SxactIsPrepared(nearConflict->sxactOut))
    4883             :                     {
    4884           0 :                         LWLockRelease(SerializableXactHashLock);
    4885           0 :                         ereport(ERROR,
    4886             :                                 (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
    4887             :                                  errmsg("could not serialize access due to read/write dependencies among transactions"),
    4888             :                                  errdetail_internal("Reason code: Canceled on commit attempt with conflict in from prepared pivot."),
    4889             :                                  errhint("The transaction might succeed if retried.")));
    4890             :                     }
    4891         280 :                     nearConflict->sxactOut->flags |= SXACT_FLAG_DOOMED;
    4892         280 :                     break;
    4893             :                 }
    4894             :                 farConflict = (RWConflict)
    4895          60 :                     SHMQueueNext(&nearConflict->sxactOut->inConflicts,
    4896          60 :                                  &farConflict->inLink,
    4897             :                                  offsetof(RWConflictData, inLink));
    4898             :             }
    4899             :         }
    4900             : 
    4901             :         nearConflict = (RWConflict)
    4902        1184 :             SHMQueueNext(&MySerializableXact->inConflicts,
    4903        1184 :                          &nearConflict->inLink,
    4904             :                          offsetof(RWConflictData, inLink));
    4905             :     }
    4906             : 
    4907        2320 :     MySerializableXact->prepareSeqNo = ++(PredXact->LastSxactCommitSeqNo);
    4908        2320 :     MySerializableXact->flags |= SXACT_FLAG_PREPARED;
    4909             : 
    4910        2320 :     LWLockRelease(SerializableXactHashLock);
    4911             : }
    4912             : 
    4913             : /*------------------------------------------------------------------------*/
    4914             : 
    4915             : /*
    4916             :  * Two-phase commit support
    4917             :  */
    4918             : 
    4919             : /*
    4920             :  * AtPrepare_Locks
    4921             :  *      Do the preparatory work for a PREPARE: make 2PC state file
    4922             :  *      records for all predicate locks currently held.
    4923             :  */
    4924             : void
    4925         670 : AtPrepare_PredicateLocks(void)
    4926             : {
    4927             :     PREDICATELOCK *predlock;
    4928             :     SERIALIZABLEXACT *sxact;
    4929             :     TwoPhasePredicateRecord record;
    4930             :     TwoPhasePredicateXactRecord *xactRecord;
    4931             :     TwoPhasePredicateLockRecord *lockRecord;
    4932             : 
    4933         670 :     sxact = MySerializableXact;
    4934         670 :     xactRecord = &(record.data.xactRecord);
    4935         670 :     lockRecord = &(record.data.lockRecord);
    4936             : 
    4937         670 :     if (MySerializableXact == InvalidSerializableXact)
    4938         658 :         return;
    4939             : 
    4940             :     /* Generate an xact record for our SERIALIZABLEXACT */
    4941          12 :     record.type = TWOPHASEPREDICATERECORD_XACT;
    4942          12 :     xactRecord->xmin = MySerializableXact->xmin;
    4943          12 :     xactRecord->flags = MySerializableXact->flags;
    4944             : 
    4945             :     /*
    4946             :      * Note that we don't include the list of conflicts in our out in the
    4947             :      * statefile, because new conflicts can be added even after the
    4948             :      * transaction prepares. We'll just make a conservative assumption during
    4949             :      * recovery instead.
    4950             :      */
    4951             : 
    4952          12 :     RegisterTwoPhaseRecord(TWOPHASE_RM_PREDICATELOCK_ID, 0,
    4953             :                            &record, sizeof(record));
    4954             : 
    4955             :     /*
    4956             :      * Generate a lock record for each lock.
    4957             :      *
    4958             :      * To do this, we need to walk the predicate lock list in our sxact rather
    4959             :      * than using the local predicate lock table because the latter is not
    4960             :      * guaranteed to be accurate.
    4961             :      */
    4962          12 :     LWLockAcquire(SerializablePredicateListLock, LW_SHARED);
    4963             : 
    4964             :     /*
    4965             :      * No need to take sxact->perXactPredicateListLock in parallel mode
    4966             :      * because there cannot be any parallel workers running while we are
    4967             :      * preparing a transaction.
    4968             :      */
    4969             :     Assert(!IsParallelWorker() && !ParallelContextActive());
    4970             : 
    4971             :     predlock = (PREDICATELOCK *)
    4972          12 :         SHMQueueNext(&(sxact->predicateLocks),
    4973          12 :                      &(sxact->predicateLocks),
    4974             :                      offsetof(PREDICATELOCK, xactLink));
    4975             : 
    4976          22 :     while (predlock != NULL)
    4977             :     {
    4978          10 :         record.type = TWOPHASEPREDICATERECORD_LOCK;
    4979          10 :         lockRecord->target = predlock->tag.myTarget->tag;
    4980             : 
    4981          10 :         RegisterTwoPhaseRecord(TWOPHASE_RM_PREDICATELOCK_ID, 0,
    4982             :                                &record, sizeof(record));
    4983             : 
    4984             :         predlock = (PREDICATELOCK *)
    4985          10 :             SHMQueueNext(&(sxact->predicateLocks),
    4986          10 :                          &(predlock->xactLink),
    4987             :                          offsetof(PREDICATELOCK, xactLink));
    4988             :     }
    4989             : 
    4990          12 :     LWLockRelease(SerializablePredicateListLock);
    4991             : }
    4992             : 
    4993             : /*
    4994             :  * PostPrepare_Locks
    4995             :  *      Clean up after successful PREPARE. Unlike the non-predicate
    4996             :  *      lock manager, we do not need to transfer locks to a dummy
    4997             :  *      PGPROC because our SERIALIZABLEXACT will stay around
    4998             :  *      anyway. We only need to clean up our local state.
    4999             :  */
    5000             : void
    5001         670 : PostPrepare_PredicateLocks(TransactionId xid)
    5002             : {
    5003         670 :     if (MySerializableXact == InvalidSerializableXact)
    5004         658 :         return;
    5005             : 
    5006             :     Assert(SxactIsPrepared(MySerializableXact));
    5007             : 
    5008          12 :     MySerializableXact->pid = 0;
    5009             : 
    5010          12 :     hash_destroy(LocalPredicateLockHash);
    5011          12 :     LocalPredicateLockHash = NULL;
    5012             : 
    5013          12 :     MySerializableXact = InvalidSerializableXact;
    5014          12 :     MyXactDidWrite = false;
    5015             : }
    5016             : 
    5017             : /*
    5018             :  * PredicateLockTwoPhaseFinish
    5019             :  *      Release a prepared transaction's predicate locks once it
    5020             :  *      commits or aborts.
    5021             :  */
    5022             : void
    5023         672 : PredicateLockTwoPhaseFinish(TransactionId xid, bool isCommit)
    5024             : {
    5025             :     SERIALIZABLEXID *sxid;
    5026             :     SERIALIZABLEXIDTAG sxidtag;
    5027             : 
    5028         672 :     sxidtag.xid = xid;
    5029             : 
    5030         672 :     LWLockAcquire(SerializableXactHashLock, LW_SHARED);
    5031             :     sxid = (SERIALIZABLEXID *)
    5032         672 :         hash_search(SerializableXidHash, &sxidtag, HASH_FIND, NULL);
    5033         672 :     LWLockRelease(SerializableXactHashLock);
    5034             : 
    5035             :     /* xid will not be found if it wasn't a serializable transaction */
    5036         672 :     if (sxid == NULL)
    5037         660 :         return;
    5038             : 
    5039             :     /* Release its locks */
    5040          12 :     MySerializableXact = sxid->myXact;
    5041          12 :     MyXactDidWrite = true;      /* conservatively assume that we wrote
    5042             :                                  * something */
    5043          12 :     ReleasePredicateLocks(isCommit, false);
    5044             : }
    5045             : 
    5046             : /*
    5047             :  * Re-acquire a predicate lock belonging to a transaction that was prepared.
    5048             :  */
    5049             : void
    5050           0 : predicatelock_twophase_recover(TransactionId xid, uint16 info,
    5051             :                                void *recdata, uint32 len)
    5052             : {
    5053             :     TwoPhasePredicateRecord *record;
    5054             : 
    5055             :     Assert(len == sizeof(TwoPhasePredicateRecord));
    5056             : 
    5057           0 :     record = (TwoPhasePredicateRecord *) recdata;
    5058             : 
    5059             :     Assert((record->type == TWOPHASEPREDICATERECORD_XACT) ||
    5060             :            (record->type == TWOPHASEPREDICATERECORD_LOCK));
    5061             : 
    5062           0 :     if (record->type == TWOPHASEPREDICATERECORD_XACT)
    5063             :     {
    5064             :         /* Per-transaction record. Set up a SERIALIZABLEXACT. */
    5065             :         TwoPhasePredicateXactRecord *xactRecord;
    5066             :         SERIALIZABLEXACT *sxact;
    5067             :         SERIALIZABLEXID *sxid;
    5068             :         SERIALIZABLEXIDTAG sxidtag;
    5069             :         bool        found;
    5070             : 
    5071           0 :         xactRecord = (TwoPhasePredicateXactRecord *) &record->data.xactRecord;
    5072             : 
    5073           0 :         LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
    5074           0 :         sxact = CreatePredXact();
    5075           0 :         if (!sxact)
    5076           0 :             ereport(ERROR,
    5077             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    5078             :                      errmsg("out of shared memory")));
    5079             : 
    5080             :         /* vxid for a prepared xact is InvalidBackendId/xid; no pid */
    5081           0 :         sxact->vxid.backendId = InvalidBackendId;
    5082           0 :         sxact->vxid.localTransactionId = (LocalTransactionId) xid;
    5083           0 :         sxact->pid = 0;
    5084             : 
    5085             :         /* a prepared xact hasn't committed yet */
    5086           0 :         sxact->prepareSeqNo = RecoverySerCommitSeqNo;
    5087           0 :         sxact->commitSeqNo = InvalidSerCommitSeqNo;
    5088           0 :         sxact->finishedBefore = InvalidTransactionId;
    5089             : 
    5090           0 :         sxact->SeqNo.lastCommitBeforeSnapshot = RecoverySerCommitSeqNo;
    5091             : 
    5092             :         /*
    5093             :          * Don't need to track this; no transactions running at the time the
    5094             :          * recovered xact started are still active, except possibly other
    5095             :          * prepared xacts and we don't care whether those are RO_SAFE or not.
    5096             :          */
    5097           0 :         SHMQueueInit(&(sxact->possibleUnsafeConflicts));
    5098             : 
    5099           0 :         SHMQueueInit(&(sxact->predicateLocks));
    5100           0 :         SHMQueueElemInit(&(sxact->finishedLink));
    5101             : 
    5102           0 :         sxact->topXid = xid;
    5103           0 :         sxact->xmin = xactRecord->xmin;
    5104           0 :         sxact->flags = xactRecord->flags;
    5105             :         Assert(SxactIsPrepared(sxact));
    5106           0 :         if (!SxactIsReadOnly(sxact))
    5107             :         {
    5108           0 :             ++(PredXact->WritableSxactCount);
    5109             :             Assert(PredXact->WritableSxactCount <=
    5110             :                    (MaxBackends + max_prepared_xacts));
    5111             :         }
    5112             : 
    5113             :         /*
    5114             :          * We don't know whether the transaction had any conflicts or not, so
    5115             :          * we'll conservatively assume that it had both a conflict in and a
    5116             :          * conflict out, and represent that with the summary conflict flags.
    5117             :          */
    5118           0 :         SHMQueueInit(&(sxact->outConflicts));
    5119           0 :         SHMQueueInit(&(sxact->inConflicts));
    5120           0 :         sxact->flags |= SXACT_FLAG_SUMMARY_CONFLICT_IN;
    5121           0 :         sxact->flags |= SXACT_FLAG_SUMMARY_CONFLICT_OUT;
    5122             : 
    5123             :         /* Register the transaction's xid */
    5124           0 :         sxidtag.xid = xid;
    5125           0 :         sxid = (SERIALIZABLEXID *) hash_search(SerializableXidHash,
    5126             :                                                &sxidtag,
    5127             :                                                HASH_ENTER, &found);
    5128             :         Assert(sxid != NULL);
    5129             :         Assert(!found);
    5130           0 :         sxid->myXact = (SERIALIZABLEXACT *) sxact;
    5131             : 
    5132             :         /*
    5133             :          * Update global xmin. Note that this is a special case compared to
    5134             :          * registering a normal transaction, because the global xmin might go
    5135             :          * backwards. That's OK, because until recovery is over we're not
    5136             :          * going to complete any transactions or create any non-prepared
    5137             :          * transactions, so there's no danger of throwing away.
    5138             :          */
    5139           0 :         if ((!TransactionIdIsValid(PredXact->SxactGlobalXmin)) ||
    5140           0 :             (TransactionIdFollows(PredXact->SxactGlobalXmin, sxact->xmin)))
    5141             :         {
    5142           0 :             PredXact->SxactGlobalXmin = sxact->xmin;
    5143           0 :             PredXact->SxactGlobalXminCount = 1;
    5144           0 :             SerialSetActiveSerXmin(sxact->xmin);
    5145             :         }
    5146           0 :         else if (TransactionIdEquals(sxact->xmin, PredXact->SxactGlobalXmin))
    5147             :         {
    5148             :             Assert(PredXact->SxactGlobalXminCount > 0);
    5149           0 :             PredXact->SxactGlobalXminCount++;
    5150             :         }
    5151             : 
    5152           0 :         LWLockRelease(SerializableXactHashLock);
    5153             :     }
    5154           0 :     else if (record->type == TWOPHASEPREDICATERECORD_LOCK)
    5155             :     {
    5156             :         /* Lock record. Recreate the PREDICATELOCK */
    5157             :         TwoPhasePredicateLockRecord *lockRecord;
    5158             :         SERIALIZABLEXID *sxid;
    5159             :         SERIALIZABLEXACT *sxact;
    5160             :         SERIALIZABLEXIDTAG sxidtag;
    5161             :         uint32      targettaghash;
    5162             : 
    5163           0 :         lockRecord = (TwoPhasePredicateLockRecord *) &record->data.lockRecord;
    5164           0 :         targettaghash = PredicateLockTargetTagHashCode(&lockRecord->target);
    5165             : 
    5166           0 :         LWLockAcquire(SerializableXactHashLock, LW_SHARED);
    5167           0 :         sxidtag.xid = xid;
    5168             :         sxid = (SERIALIZABLEXID *)
    5169           0 :             hash_search(SerializableXidHash, &sxidtag, HASH_FIND, NULL);
    5170           0 :         LWLockRelease(SerializableXactHashLock);
    5171             : 
    5172             :         Assert(sxid != NULL);
    5173           0 :         sxact = sxid->myXact;
    5174             :         Assert(sxact != InvalidSerializableXact);
    5175             : 
    5176           0 :         CreatePredicateLock(&lockRecord->target, targettaghash, sxact);
    5177             :     }
    5178           0 : }
    5179             : 
    5180             : /*
    5181             :  * Prepare to share the current SERIALIZABLEXACT with parallel workers.
    5182             :  * Return a handle object that can be used by AttachSerializableXact() in a
    5183             :  * parallel worker.
    5184             :  */
    5185             : SerializableXactHandle
    5186         518 : ShareSerializableXact(void)
    5187             : {
    5188         518 :     return MySerializableXact;
    5189             : }
    5190             : 
    5191             : /*
    5192             :  * Allow parallel workers to import the leader's SERIALIZABLEXACT.
    5193             :  */
    5194             : void
    5195        1690 : AttachSerializableXact(SerializableXactHandle handle)
    5196             : {
    5197             : 
    5198             :     Assert(MySerializableXact == InvalidSerializableXact);
    5199             : 
    5200        1690 :     MySerializableXact = (SERIALIZABLEXACT *) handle;
    5201        1690 :     if (MySerializableXact != InvalidSerializableXact)
    5202          10 :         CreateLocalPredicateLockHash();
    5203        1690 : }

Generated by: LCOV version 1.14