LCOV - differential code coverage report
Current view: top level - src/backend/storage/lmgr - predicate.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC ECB DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 74.8 % 1457 1090 21 13 333 7 18 1065 1 12 15
Current Date: 2024-04-14 14:21:10 Functions: 91.7 % 72 66 2 4 11 55 1
Baseline: 16@8cea358b128 Branches: 57.0 % 1102 628 4 10 460 7 621
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 36.8 % 19 7 12 7
(60,120] days: 69.2 % 13 9 4 9
(120,180] days: 28.6 % 7 2 5 2
(240..) days: 75.6 % 1418 1072 13 333 7 1065 1
Function coverage date bins:
[..60] days: 100.0 % 1 1 1
(120,180] days: 100.0 % 1 1 1
(240..) days: 91.4 % 70 64 2 4 9 55
Branch coverage date bins:
[..60] days: 0.0 % 2 0 2
(120,180] days: 0.0 % 2 0 2
(240..) days: 57.2 % 1098 628 10 460 7 621

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

Generated by: LCOV version 2.1-beta2-3-g6141622