LCOV - code coverage report
Current view: top level - src/backend/storage/lmgr - predicate.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 46.3 % 1856 860
Test Date: 2026-01-26 10:56:24 Functions: 70.8 % 72 51
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 26.8 % 1187 318

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

Generated by: LCOV version 2.3.2-1