]> granicus.if.org Git - postgresql/blobdiff - src/backend/storage/lmgr/predicate.c
Fix inconsistencies in the code
[postgresql] / src / backend / storage / lmgr / predicate.c
index e7f44cce841e2587e59b1c807bea6a7ae2b04e8d..565c3ac4397592de9c73be864c0bb866f0215e41 100644 (file)
  * examining the MVCC data.)
  *
  * (1) Besides tuples actually read, they must cover ranges of tuples
- *             which would have been read based on the predicate.      This will
+ *             which would have been read based on the predicate.  This will
  *             require modelling the predicates through locks against database
  *             objects such as pages, index ranges, or entire tables.
  *
- * (2) They must be kept in RAM for quick access.      Because of this, it
+ * (2) They must be kept in RAM for quick access.  Because of this, it
  *             isn't possible to always maintain tuple-level granularity -- when
  *             the space allocated to store these approaches exhaustion, a
  *             request for a lock may need to scan for situations where a single
@@ -49,7 +49,7 @@
  *
  * (4) While they are associated with a transaction, they must survive
  *             a successful COMMIT of that transaction, and remain until all
- *             overlapping transactions complete.      This even means that they
+ *             overlapping transactions complete.  This even means that they
  *             must survive termination of the transaction's process.  If a
  *             top level transaction is rolled back, however, it is immediately
  *             flagged so that it can be ignored, and its SIREAD locks can be
@@ -62,7 +62,7 @@
  *             an existing SIREAD lock for the same transaction, the SIREAD lock
  *             can be deleted.
  *
- * (7) A write from a serializable transaction must ensure that a xact
+ * (7) A write from a serializable transaction must ensure that an xact
  *             record exists for the transaction, with the same lifespan (until
  *             all concurrent transaction complete or the transaction is rolled
  *             back) so that rw-dependencies to that transaction can be
  *                     may yet matter because they overlap still-active transactions.
  *
  *     SerializablePredicateLockListLock
- *             - Protects the linked list of locks held by a transaction.      Note
+ *             - Protects the linked list of locks held by a transaction.  Note
  *                     that the locks themselves are also covered by the partition
  *                     locks of their respective lock targets; this lock only affects
  *                     the linked list connecting the locks related to a transaction.
  *             - All transactions share this single lock (with no partitioning).
  *             - There is never a need for a process other than the one running
  *                     an active transaction to walk the list of locks held by that
- *                     transaction.
+ *                     transaction, except parallel query workers sharing the leader's
+ *                     transaction.  In the parallel case, an extra per-sxact lock is
+ *                     taken; see below.
  *             - It is relatively infrequent that another process needs to
  *                     modify the list for a transaction, but it does happen for such
  *                     things as index page splits for pages with predicate locks and
- *                     freeing of predicate locked pages by a vacuum process.  When
+ *                     freeing of predicate locked pages by a vacuum process.  When
  *                     removing a lock in such cases, the lock itself contains the
  *                     pointers needed to remove it from the list.  When adding a
  *                     lock in such cases, the lock can be added using the anchor in
- *                     the transaction structure.      Neither requires walking the list.
+ *                     the transaction structure.  Neither requires walking the list.
  *             - Cleaning up the list for a terminated transaction is sometimes
  *                     not done on a retail basis, in which case no lock is required.
  *             - Due to the above, a process accessing its active transaction's
  *                     than its own active transaction must acquire an exclusive
  *                     lock.
  *
- *     FirstPredicateLockMgrLock based partition locks
+ *     SERIALIZABLEXACT's member 'predicateLockListLock'
+ *             - Protects the linked list of locks held by a transaction.  Only
+ *                     needed for parallel mode, where multiple backends share the
+ *                     same SERIALIZABLEXACT object.  Not needed if
+ *                     SerializablePredicateLockListLock is held exclusively.
+ *
+ *     PredicateLockHashPartitionLock(hashcode)
  *             - The same lock protects a target, all locks on that target, and
- *                     the linked list of locks on the target..
- *             - When more than one is needed, acquire in ascending order.
+ *                     the linked list of locks on the target.
+ *             - When more than one is needed, acquire in ascending address order.
+ *             - When all are needed (rare), acquire in ascending index order with
+ *                     PredicateLockHashPartitionLockByIndex(index).
  *
  *     SerializableXactHashLock
  *             - Protects both PredXact and SerializableXidHash.
  *
  *
- * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * predicate lock maintenance
  *             GetSerializableTransactionSnapshot(Snapshot snapshot)
  *             SetSerializableTransactionSnapshot(Snapshot snapshot,
- *                                                                                TransactionId sourcexid)
+ *                                                                                VirtualTransactionId *sourcevxid)
  *             RegisterPredicateLockingXid(void)
  *             PredicateLockRelation(Relation relation, Snapshot snapshot)
  *             PredicateLockPage(Relation relation, BlockNumber blkno,
  *             PredicateLockPageCombine(Relation relation, BlockNumber oldblkno,
  *                                                              BlockNumber newblkno)
  *             TransferPredicateLocksToHeapRelation(Relation relation)
- *             ReleasePredicateLocks(bool isCommit)
+ *             ReleasePredicateLocks(bool isCommit, bool isReadOnlySafe)
  *
  * conflict detection (may also trigger rollback)
  *             CheckForSerializableConflictOut(bool visible, Relation relation,
 
 #include "postgres.h"
 
+#include "access/heapam.h"
 #include "access/htup_details.h"
+#include "access/parallel.h"
 #include "access/slru.h"
 #include "access/subtrans.h"
 #include "access/transam.h"
 #include "access/twophase.h"
 #include "access/twophase_rmgr.h"
 #include "access/xact.h"
+#include "access/xlog.h"
 #include "miscadmin.h"
+#include "pgstat.h"
 #include "storage/bufmgr.h"
 #include "storage/predicate.h"
 #include "storage/predicate_internals.h"
 #include "storage/procarray.h"
 #include "utils/rel.h"
 #include "utils/snapmgr.h"
-#include "utils/tqual.h"
 
 /* Uncomment the next line to test the graceful degradation code. */
 /* #define TEST_OLDSERXID */
 #define PredicateLockHashPartition(hashcode) \
        ((hashcode) % NUM_PREDICATELOCK_PARTITIONS)
 #define PredicateLockHashPartitionLock(hashcode) \
-       ((LWLockId) (FirstPredicateLockMgrLock + PredicateLockHashPartition(hashcode)))
+       (&MainLWLockArray[PREDICATELOCK_MANAGER_LWLOCK_OFFSET + \
+               PredicateLockHashPartition(hashcode)].lock)
+#define PredicateLockHashPartitionLockByIndex(i) \
+       (&MainLWLockArray[PREDICATELOCK_MANAGER_LWLOCK_OFFSET + (i)].lock)
 
 #define NPREDICATELOCKTARGETENTS() \
        mul_size(max_predicate_locks_per_xact, add_size(MaxBackends, max_prepared_xacts))
 #define SxactIsDeferrableWaiting(sxact) (((sxact)->flags & SXACT_FLAG_DEFERRABLE_WAITING) != 0)
 #define SxactIsROSafe(sxact) (((sxact)->flags & SXACT_FLAG_RO_SAFE) != 0)
 #define SxactIsROUnsafe(sxact) (((sxact)->flags & SXACT_FLAG_RO_UNSAFE) != 0)
+#define SxactIsPartiallyReleased(sxact) (((sxact)->flags & SXACT_FLAG_PARTIALLY_RELEASED) != 0)
 
 /*
  * Compute the hash code associated with a PREDICATELOCKTARGETTAG.
  * the lock partition number from the hashcode.
  */
 #define PredicateLockTargetTagHashCode(predicatelocktargettag) \
-       (tag_hash((predicatelocktargettag), sizeof(PREDICATELOCKTARGETTAG)))
+       get_hash_value(PredicateLockTargetHash, predicatelocktargettag)
 
 /*
  * Given a predicate lock tag, and the hash for its target,
@@ -311,11 +328,9 @@ static SlruCtlData OldSerXidSlruCtlData;
 #define OLDSERXID_ENTRIESPERPAGE       (OLDSERXID_PAGESIZE / OLDSERXID_ENTRYSIZE)
 
 /*
- * Set maximum pages based on the lesser of the number needed to track all
- * transactions and the maximum that SLRU supports.
+ * Set maximum pages based on the number needed to track all transactions.
  */
-#define OLDSERXID_MAX_PAGE                     Min(SLRU_PAGES_PER_SEGMENT * 0x10000 - 1, \
-                                                                               (MaxTransactionId) / OLDSERXID_ENTRIESPERPAGE)
+#define OLDSERXID_MAX_PAGE                     (MaxTransactionId / OLDSERXID_ENTRIESPERPAGE)
 
 #define OldSerXidNextPage(page) (((page) >= OLDSERXID_MAX_PAGE) ? 0 : (page) + 1)
 
@@ -323,16 +338,14 @@ static SlruCtlData OldSerXidSlruCtlData;
        (OldSerXidSlruCtl->shared->page_buffer[slotno] + \
        ((((uint32) (xid)) % OLDSERXID_ENTRIESPERPAGE) * OLDSERXID_ENTRYSIZE))))
 
-#define OldSerXidPage(xid)     ((((uint32) (xid)) / OLDSERXID_ENTRIESPERPAGE) % (OLDSERXID_MAX_PAGE + 1))
-#define OldSerXidSegment(page) ((page) / SLRU_PAGES_PER_SEGMENT)
+#define OldSerXidPage(xid)     (((uint32) (xid)) / OLDSERXID_ENTRIESPERPAGE)
 
 typedef struct OldSerXidControlData
 {
        int                     headPage;               /* newest initialized page */
        TransactionId headXid;          /* newest valid Xid in the SLRU */
        TransactionId tailXid;          /* oldest xmin we might be interested in */
-       bool            warningIssued;  /* have we issued SLRU wrap-around warning? */
-}      OldSerXidControlData;
+}                      OldSerXidControlData;
 
 typedef struct OldSerXidControlData *OldSerXidControl;
 
@@ -347,12 +360,19 @@ static OldSerXidControl oldSerXidControl;
 static SERIALIZABLEXACT *OldCommittedSxact;
 
 
-/* This configuration variable is used to set the predicate lock table size */
-int                    max_predicate_locks_per_xact;           /* set by guc.c */
+/*
+ * These configuration variables are used to set the predicate lock table size
+ * and to control promotion of predicate locks to coarser granularity in an
+ * attempt to degrade performance (mostly as false positive serialization
+ * failure) gracefully in the face of memory pressurel
+ */
+int                    max_predicate_locks_per_xact;   /* set by guc.c */
+int                    max_predicate_locks_per_relation;       /* set by guc.c */
+int                    max_predicate_locks_per_page;   /* set by guc.c */
 
 /*
  * This provides a list of objects in order to track transactions
- * participating in predicate locking. Entries in the list are fixed size,
+ * participating in predicate locking.  Entries in the list are fixed size,
  * and reside in shared memory.  The memory address of an entry must remain
  * fixed during its lifetime.  The list will be protected from concurrent
  * update externally; no provision is made in this code to manage that.  The
@@ -383,7 +403,7 @@ static SHM_QUEUE *FinishedSerializableTransactions;
  */
 static const PREDICATELOCKTARGETTAG ScratchTargetTag = {0, 0, 0, 0};
 static uint32 ScratchTargetTagHash;
-static int     ScratchPartitionLock;
+static LWLock *ScratchPartitionLock;
 
 /*
  * The local hash table used to determine when to combine multiple fine-
@@ -399,6 +419,15 @@ static HTAB *LocalPredicateLockHash = NULL;
 static SERIALIZABLEXACT *MySerializableXact = InvalidSerializableXact;
 static bool MyXactDidWrite = false;
 
+/*
+ * The SXACT_FLAG_RO_UNSAFE optimization might lead us to release
+ * MySerializableXact early.  If that happens in a parallel query, the leader
+ * needs to defer the destruction of the SERIALIZABLEXACT until end of
+ * transaction, because the workers still have a reference to it.  In that
+ * case, the leader stores it here.
+ */
+static SERIALIZABLEXACT *SavedSerializableXact = InvalidSerializableXact;
+
 /* local functions */
 
 static SERIALIZABLEXACT *CreatePredXact(void);
@@ -422,38 +451,41 @@ static uint32 predicatelock_hash(const void *key, Size keysize);
 static void SummarizeOldestCommittedSxact(void);
 static Snapshot GetSafeSnapshot(Snapshot snapshot);
 static Snapshot GetSerializableTransactionSnapshotInt(Snapshot snapshot,
-                                                                         TransactionId sourcexid);
+                                                                                                         VirtualTransactionId *sourcevxid,
+                                                                                                         int sourcepid);
 static bool PredicateLockExists(const PREDICATELOCKTARGETTAG *targettag);
 static bool GetParentPredicateLockTag(const PREDICATELOCKTARGETTAG *tag,
-                                                 PREDICATELOCKTARGETTAG *parent);
+                                                                         PREDICATELOCKTARGETTAG *parent);
 static bool CoarserLockCovers(const PREDICATELOCKTARGETTAG *newtargettag);
 static void RemoveScratchTarget(bool lockheld);
 static void RestoreScratchTarget(bool lockheld);
 static void RemoveTargetIfNoLongerUsed(PREDICATELOCKTARGET *target,
-                                                  uint32 targettaghash);
+                                                                          uint32 targettaghash);
 static void DeleteChildTargetLocks(const PREDICATELOCKTARGETTAG *newtargettag);
-static int     PredicateLockPromotionThreshold(const PREDICATELOCKTARGETTAG *tag);
+static int     MaxPredicateChildLocks(const PREDICATELOCKTARGETTAG *tag);
 static bool CheckAndPromotePredicateLockRequest(const PREDICATELOCKTARGETTAG *reqtag);
 static void DecrementParentLocks(const PREDICATELOCKTARGETTAG *targettag);
 static void CreatePredicateLock(const PREDICATELOCKTARGETTAG *targettag,
-                                       uint32 targettaghash,
-                                       SERIALIZABLEXACT *sxact);
+                                                               uint32 targettaghash,
+                                                               SERIALIZABLEXACT *sxact);
 static void DeleteLockTarget(PREDICATELOCKTARGET *target, uint32 targettaghash);
 static bool TransferPredicateLocksToNewTarget(PREDICATELOCKTARGETTAG oldtargettag,
-                                                                 PREDICATELOCKTARGETTAG newtargettag,
-                                                                 bool removeOld);
+                                                                                         PREDICATELOCKTARGETTAG newtargettag,
+                                                                                         bool removeOld);
 static void PredicateLockAcquire(const PREDICATELOCKTARGETTAG *targettag);
 static void DropAllPredicateLocksFromTable(Relation relation,
-                                                          bool transfer);
+                                                                                  bool transfer);
 static void SetNewSxactGlobalXmin(void);
 static void ClearOldPredicateLocks(void);
 static void ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial,
-                                                  bool summarize);
+                                                                          bool summarize);
 static bool XidIsConcurrent(TransactionId xid);
 static void CheckTargetForConflictsIn(PREDICATELOCKTARGETTAG *targettag);
 static void FlagRWConflict(SERIALIZABLEXACT *reader, SERIALIZABLEXACT *writer);
 static void OnConflict_CheckForSerializationFailure(const SERIALIZABLEXACT *reader,
-                                                                               SERIALIZABLEXACT *writer);
+                                                                                                       SERIALIZABLEXACT *writer);
+static void CreateLocalPredicateLockHash(void);
+static void ReleasePredicateLocksLocal(void);
 
 
 /*------------------------------------------------------------------------*/
@@ -478,8 +510,8 @@ PredicateLockingNeededForRelation(Relation relation)
  * as RO-safe since the last call, we release all predicate locks and reset
  * MySerializableXact. That makes subsequent calls to return quickly.
  *
- * This is marked as 'inline' to make to eliminate the function call overhead
- * in the common case that serialization is not needed.
+ * This is marked as 'inline' to eliminate the function call overhead in the
+ * common case that serialization is not needed.
  */
 static inline bool
 SerializationNeededForRead(Relation relation, Snapshot snapshot)
@@ -492,8 +524,8 @@ SerializationNeededForRead(Relation relation, Snapshot snapshot)
         * Don't acquire locks or conflict when scanning with a special snapshot.
         * This excludes things like CLUSTER and REINDEX. They use the wholesale
         * functions TransferPredicateLocksToHeapRelation() and
-        * CheckTableForSerializableConflictIn() to participate serialization, but
-        * the scans involved don't need serialization.
+        * CheckTableForSerializableConflictIn() to participate in serialization,
+        * but the scans involved don't need serialization.
         */
        if (!IsMVCCSnapshot(snapshot))
                return false;
@@ -510,7 +542,7 @@ SerializationNeededForRead(Relation relation, Snapshot snapshot)
         */
        if (SxactIsROSafe(MySerializableXact))
        {
-               ReleasePredicateLocks(false);
+               ReleasePredicateLocks(false, true);
                return false;
        }
 
@@ -544,7 +576,7 @@ SerializationNeededForWrite(Relation relation)
 
 /*
  * These functions are a simple implementation of a list for this specific
- * type of struct.     If there is ever a generalized shared memory list, we
+ * type of struct.  If there is ever a generalized shared memory list, we
  * should probably switch to that.
  */
 static SERIALIZABLEXACT *
@@ -764,7 +796,7 @@ OldSerXidPagePrecedesLogically(int p, int q)
        int                     diff;
 
        /*
-        * We have to compare modulo (OLDSERXID_MAX_PAGE+1)/2.  Both inputs should
+        * We have to compare modulo (OLDSERXID_MAX_PAGE+1)/2.  Both inputs should
         * be in the range 0..OLDSERXID_MAX_PAGE.
         */
        Assert(p >= 0 && p <= OLDSERXID_MAX_PAGE);
@@ -790,8 +822,9 @@ OldSerXidInit(void)
         * Set up SLRU management of the pg_serial data.
         */
        OldSerXidSlruCtl->PagePrecedes = OldSerXidPagePrecedesLogically;
-       SimpleLruInit(OldSerXidSlruCtl, "OldSerXid SLRU Ctl",
-                                 NUM_OLDSERXID_BUFFERS, 0, OldSerXidLock, "pg_serial");
+       SimpleLruInit(OldSerXidSlruCtl, "oldserxid",
+                                 NUM_OLDSERXID_BUFFERS, 0, OldSerXidLock, "pg_serial",
+                                 LWTRANCHE_OLDSERXID_BUFFERS);
        /* Override default assumption that writes should be fsync'd */
        OldSerXidSlruCtl->do_fsync = false;
 
@@ -801,6 +834,7 @@ OldSerXidInit(void)
        oldSerXidControl = (OldSerXidControl)
                ShmemInitStruct("OldSerXidControlData", sizeof(OldSerXidControlData), &found);
 
+       Assert(found == IsUnderPostmaster);
        if (!found)
        {
                /*
@@ -809,7 +843,6 @@ OldSerXidInit(void)
                oldSerXidControl->headPage = -1;
                oldSerXidControl->headXid = InvalidTransactionId;
                oldSerXidControl->tailXid = InvalidTransactionId;
-               oldSerXidControl->warningIssued = false;
        }
 }
 
@@ -865,47 +898,6 @@ OldSerXidAdd(TransactionId xid, SerCommitSeqNo minConflictCommitSeqNo)
        if (isNewPage)
                oldSerXidControl->headPage = targetPage;
 
-       /*
-        * Give a warning if we're about to run out of SLRU pages.
-        *
-        * slru.c has a maximum of 64k segments, with 32 (SLRU_PAGES_PER_SEGMENT)
-        * pages each. We need to store a 64-bit integer for each Xid, and with
-        * default 8k block size, 65536*32 pages is only enough to cover 2^30
-        * XIDs. If we're about to hit that limit and wrap around, warn the user.
-        *
-        * To avoid spamming the user, we only give one warning when we've used 1
-        * billion XIDs, and stay silent until the situation is fixed and the
-        * number of XIDs used falls below 800 million again.
-        *
-        * XXX: We have no safeguard to actually *prevent* the wrap-around,
-        * though. All you get is a warning.
-        */
-       if (oldSerXidControl->warningIssued)
-       {
-               TransactionId lowWatermark;
-
-               lowWatermark = tailXid + 800000000;
-               if (lowWatermark < FirstNormalTransactionId)
-                       lowWatermark = FirstNormalTransactionId;
-               if (TransactionIdPrecedes(xid, lowWatermark))
-                       oldSerXidControl->warningIssued = false;
-       }
-       else
-       {
-               TransactionId highWatermark;
-
-               highWatermark = tailXid + 1000000000;
-               if (highWatermark < FirstNormalTransactionId)
-                       highWatermark = FirstNormalTransactionId;
-               if (TransactionIdFollows(xid, highWatermark))
-               {
-                       oldSerXidControl->warningIssued = true;
-                       ereport(WARNING,
-                                       (errmsg("memory for serializable conflict tracking is nearly exhausted"),
-                                        errhint("There might be an idle transaction or a forgotten prepared transaction causing this.")));
-               }
-       }
-
        if (isNewPage)
        {
                /* Initialize intervening pages. */
@@ -926,7 +918,7 @@ OldSerXidAdd(TransactionId xid, SerCommitSeqNo minConflictCommitSeqNo)
 }
 
 /*
- * Get the minimum commitSeqNo for any conflict out for the given xid. For
+ * Get the minimum commitSeqNo for any conflict out for the given xid.  For
  * a transaction which exists but has no conflict out, InvalidSerCommitSeqNo
  * will be returned.
  */
@@ -979,7 +971,7 @@ OldSerXidSetActiveSerXmin(TransactionId xid)
        /*
         * When no sxacts are active, nothing overlaps, set the xid values to
         * invalid to show that there are no valid entries.  Don't clear headPage,
-        * though.      A new xmin might still land on that page, and we don't want to
+        * though.  A new xmin might still land on that page, and we don't want to
         * repeatedly zero out the same page.
         */
        if (!TransactionIdIsValid(xid))
@@ -1091,11 +1083,14 @@ void
 InitPredicateLocks(void)
 {
        HASHCTL         info;
-       int                     hash_flags;
        long            max_table_size;
        Size            requestSize;
        bool            found;
 
+#ifndef EXEC_BACKEND
+       Assert(!IsUnderPostmaster);
+#endif
+
        /*
         * Compute size of predicate lock target hashtable. Note these
         * calculations must agree with PredicateLockShmemSize!
@@ -1109,18 +1104,14 @@ InitPredicateLocks(void)
        MemSet(&info, 0, sizeof(info));
        info.keysize = sizeof(PREDICATELOCKTARGETTAG);
        info.entrysize = sizeof(PREDICATELOCKTARGET);
-       info.hash = tag_hash;
        info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
-       hash_flags = (HASH_ELEM | HASH_FUNCTION | HASH_PARTITION | HASH_FIXED_SIZE);
 
        PredicateLockTargetHash = ShmemInitHash("PREDICATELOCKTARGET hash",
                                                                                        max_table_size,
                                                                                        max_table_size,
                                                                                        &info,
-                                                                                       hash_flags);
-
-       /* Assume an average of 2 xacts per target */
-       max_table_size *= 2;
+                                                                                       HASH_ELEM | HASH_BLOBS |
+                                                                                       HASH_PARTITION | HASH_FIXED_SIZE);
 
        /*
         * Reserve a dummy entry in the hash table; we use it to make sure there's
@@ -1128,7 +1119,16 @@ InitPredicateLocks(void)
         * because running out of space there could mean aborting a
         * non-serializable transaction.
         */
-       hash_search(PredicateLockTargetHash, &ScratchTargetTag, HASH_ENTER, NULL);
+       if (!IsUnderPostmaster)
+       {
+               (void) hash_search(PredicateLockTargetHash, &ScratchTargetTag,
+                                                  HASH_ENTER, &found);
+               Assert(!found);
+       }
+
+       /* Pre-calculate the hash and partition lock of the scratch entry */
+       ScratchTargetTagHash = PredicateLockTargetTagHashCode(&ScratchTargetTag);
+       ScratchPartitionLock = PredicateLockHashPartitionLock(ScratchTargetTagHash);
 
        /*
         * Allocate hash table for PREDICATELOCK structs.  This stores per
@@ -1139,13 +1139,16 @@ InitPredicateLocks(void)
        info.entrysize = sizeof(PREDICATELOCK);
        info.hash = predicatelock_hash;
        info.num_partitions = NUM_PREDICATELOCK_PARTITIONS;
-       hash_flags = (HASH_ELEM | HASH_FUNCTION | HASH_PARTITION | HASH_FIXED_SIZE);
+
+       /* Assume an average of 2 xacts per target */
+       max_table_size *= 2;
 
        PredicateLockHash = ShmemInitHash("PREDICATELOCK hash",
                                                                          max_table_size,
                                                                          max_table_size,
                                                                          &info,
-                                                                         hash_flags);
+                                                                         HASH_ELEM | HASH_FUNCTION |
+                                                                         HASH_PARTITION | HASH_FIXED_SIZE);
 
        /*
         * Compute size for serializable transaction hashtable. Note these
@@ -1166,6 +1169,7 @@ InitPredicateLocks(void)
        PredXact = ShmemInitStruct("PredXactList",
                                                           PredXactListDataSize,
                                                           &found);
+       Assert(found == IsUnderPostmaster);
        if (!found)
        {
                int                     i;
@@ -1181,16 +1185,12 @@ InitPredicateLocks(void)
                requestSize = mul_size((Size) max_table_size,
                                                           PredXactListElementDataSize);
                PredXact->element = ShmemAlloc(requestSize);
-               if (PredXact->element == NULL)
-                       ereport(ERROR,
-                                       (errcode(ERRCODE_OUT_OF_MEMORY),
-                        errmsg("not enough shared memory for elements of data structure"
-                                       " \"%s\" (%zu bytes requested)",
-                                       "PredXactList", requestSize)));
                /* Add all elements to available list, clean. */
                memset(PredXact->element, 0, requestSize);
                for (i = 0; i < max_table_size; i++)
                {
+                       LWLockInitialize(&PredXact->element[i].sxact.predicateLockListLock,
+                                                        LWTRANCHE_SXACT);
                        SHMQueueInsertBefore(&(PredXact->availableList),
                                                                 &(PredXact->element[i].link));
                }
@@ -1220,14 +1220,13 @@ InitPredicateLocks(void)
        MemSet(&info, 0, sizeof(info));
        info.keysize = sizeof(SERIALIZABLEXIDTAG);
        info.entrysize = sizeof(SERIALIZABLEXID);
-       info.hash = tag_hash;
-       hash_flags = (HASH_ELEM | HASH_FUNCTION | HASH_FIXED_SIZE);
 
        SerializableXidHash = ShmemInitHash("SERIALIZABLEXID hash",
                                                                                max_table_size,
                                                                                max_table_size,
                                                                                &info,
-                                                                               hash_flags);
+                                                                               HASH_ELEM | HASH_BLOBS |
+                                                                               HASH_FIXED_SIZE);
 
        /*
         * Allocate space for tracking rw-conflicts in lists attached to the
@@ -1245,6 +1244,7 @@ InitPredicateLocks(void)
        RWConflictPool = ShmemInitStruct("RWConflictPool",
                                                                         RWConflictPoolHeaderDataSize,
                                                                         &found);
+       Assert(found == IsUnderPostmaster);
        if (!found)
        {
                int                     i;
@@ -1253,12 +1253,6 @@ InitPredicateLocks(void)
                requestSize = mul_size((Size) max_table_size,
                                                           RWConflictDataSize);
                RWConflictPool->element = ShmemAlloc(requestSize);
-               if (RWConflictPool->element == NULL)
-                       ereport(ERROR,
-                                       (errcode(ERRCODE_OUT_OF_MEMORY),
-                        errmsg("not enough shared memory for elements of data structure"
-                                       " \"%s\" (%zu bytes requested)",
-                                       "RWConflictPool", requestSize)));
                /* Add all elements to available list, clean. */
                memset(RWConflictPool->element, 0, requestSize);
                for (i = 0; i < max_table_size; i++)
@@ -1276,6 +1270,7 @@ InitPredicateLocks(void)
                ShmemInitStruct("FinishedSerializableTransactions",
                                                sizeof(SHM_QUEUE),
                                                &found);
+       Assert(found == IsUnderPostmaster);
        if (!found)
                SHMQueueInit(FinishedSerializableTransactions);
 
@@ -1284,10 +1279,6 @@ InitPredicateLocks(void)
         * transactions.
         */
        OldSerXidInit();
-
-       /* Pre-calculate the hash and partition lock of the scratch entry */
-       ScratchTargetTagHash = PredicateLockTargetTagHashCode(&ScratchTargetTag);
-       ScratchPartitionLock = PredicateLockHashPartitionLock(ScratchTargetTagHash);
 }
 
 /*
@@ -1398,7 +1389,7 @@ GetPredicateLockStatusData(void)
         * in ascending order, then SerializableXactHashLock.
         */
        for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
-               LWLockAcquire(FirstPredicateLockMgrLock + i, LW_SHARED);
+               LWLockAcquire(PredicateLockHashPartitionLockByIndex(i), LW_SHARED);
        LWLockAcquire(SerializableXactHashLock, LW_SHARED);
 
        /* Get number of locks and allocate appropriately-sized arrays. */
@@ -1427,7 +1418,7 @@ GetPredicateLockStatusData(void)
        /* Release locks in reverse order */
        LWLockRelease(SerializableXactHashLock);
        for (i = NUM_PREDICATELOCK_PARTITIONS - 1; i >= 0; i--)
-               LWLockRelease(FirstPredicateLockMgrLock + i);
+               LWLockRelease(PredicateLockHashPartitionLockByIndex(i));
 
        return data;
 }
@@ -1464,7 +1455,7 @@ SummarizeOldestCommittedSxact(void)
 
        /*
         * Grab the first sxact off the finished list -- this will be the earliest
-        * commit.      Remove it from the list.
+        * commit.  Remove it from the list.
         */
        sxact = (SERIALIZABLEXACT *)
                SHMQueueNext(FinishedSerializableTransactions,
@@ -1475,7 +1466,7 @@ SummarizeOldestCommittedSxact(void)
        /* Add to SLRU summary information. */
        if (TransactionIdIsValid(sxact->topXid) && !SxactIsReadOnly(sxact))
                OldSerXidAdd(sxact->topXid, SxactHasConflictOut(sxact)
-                  ? sxact->SeqNo.earliestOutConflictCommit : InvalidSerCommitSeqNo);
+                                        ? sxact->SeqNo.earliestOutConflictCommit : InvalidSerCommitSeqNo);
 
        /* Summarize and release the detail. */
        ReleaseOneSerializableXact(sxact, false, true);
@@ -1512,7 +1503,7 @@ GetSafeSnapshot(Snapshot origSnapshot)
                 * one passed to it, but we avoid assuming that here.
                 */
                snapshot = GetSerializableTransactionSnapshotInt(origSnapshot,
-                                                                                                          InvalidTransactionId);
+                                                                                                                NULL, InvalidPid);
 
                if (MySerializableXact == InvalidSerializableXact)
                        return snapshot;        /* no concurrent r/w xacts; it's safe */
@@ -1528,7 +1519,7 @@ GetSafeSnapshot(Snapshot origSnapshot)
                                 SxactIsROUnsafe(MySerializableXact)))
                {
                        LWLockRelease(SerializableXactHashLock);
-                       ProcWaitForSignal();
+                       ProcWaitForSignal(WAIT_EVENT_SAFE_SNAPSHOT);
                        LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
                }
                MySerializableXact->flags &= ~SXACT_FLAG_DEFERRABLE_WAITING;
@@ -1545,18 +1536,68 @@ GetSafeSnapshot(Snapshot origSnapshot)
                ereport(DEBUG2,
                                (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
                                 errmsg("deferrable snapshot was unsafe; trying a new one")));
-               ReleasePredicateLocks(false);
+               ReleasePredicateLocks(false, false);
        }
 
        /*
         * Now we have a safe snapshot, so we don't need to do any further checks.
         */
        Assert(SxactIsROSafe(MySerializableXact));
-       ReleasePredicateLocks(false);
+       ReleasePredicateLocks(false, true);
 
        return snapshot;
 }
 
+/*
+ * GetSafeSnapshotBlockingPids
+ *             If the specified process is currently blocked in GetSafeSnapshot,
+ *             write the process IDs of all processes that it is blocked by
+ *             into the caller-supplied buffer output[].  The list is truncated at
+ *             output_size, and the number of PIDs written into the buffer is
+ *             returned.  Returns zero if the given PID is not currently blocked
+ *             in GetSafeSnapshot.
+ */
+int
+GetSafeSnapshotBlockingPids(int blocked_pid, int *output, int output_size)
+{
+       int                     num_written = 0;
+       SERIALIZABLEXACT *sxact;
+
+       LWLockAcquire(SerializableXactHashLock, LW_SHARED);
+
+       /* Find blocked_pid's SERIALIZABLEXACT by linear search. */
+       for (sxact = FirstPredXact(); sxact != NULL; sxact = NextPredXact(sxact))
+       {
+               if (sxact->pid == blocked_pid)
+                       break;
+       }
+
+       /* Did we find it, and is it currently waiting in GetSafeSnapshot? */
+       if (sxact != NULL && SxactIsDeferrableWaiting(sxact))
+       {
+               RWConflict      possibleUnsafeConflict;
+
+               /* Traverse the list of possible unsafe conflicts collecting PIDs. */
+               possibleUnsafeConflict = (RWConflict)
+                       SHMQueueNext(&sxact->possibleUnsafeConflicts,
+                                                &sxact->possibleUnsafeConflicts,
+                                                offsetof(RWConflictData, inLink));
+
+               while (possibleUnsafeConflict != NULL && num_written < output_size)
+               {
+                       output[num_written++] = possibleUnsafeConflict->sxactOut->pid;
+                       possibleUnsafeConflict = (RWConflict)
+                               SHMQueueNext(&sxact->possibleUnsafeConflicts,
+                                                        &possibleUnsafeConflict->inLink,
+                                                        offsetof(RWConflictData, inLink));
+               }
+       }
+
+       LWLockRelease(SerializableXactHashLock);
+
+       return num_written;
+}
+
 /*
  * Acquire a snapshot that can be used for the current transaction.
  *
@@ -1595,7 +1636,7 @@ GetSerializableTransactionSnapshot(Snapshot snapshot)
                return GetSafeSnapshot(snapshot);
 
        return GetSerializableTransactionSnapshotInt(snapshot,
-                                                                                                InvalidTransactionId);
+                                                                                                NULL, InvalidPid);
 }
 
 /*
@@ -1610,14 +1651,26 @@ GetSerializableTransactionSnapshot(Snapshot snapshot)
  */
 void
 SetSerializableTransactionSnapshot(Snapshot snapshot,
-                                                                  TransactionId sourcexid)
+                                                                  VirtualTransactionId *sourcevxid,
+                                                                  int sourcepid)
 {
        Assert(IsolationIsSerializable());
 
+       /*
+        * If this is called by parallel.c in a parallel worker, we don't want to
+        * create a SERIALIZABLEXACT just yet because the leader's
+        * SERIALIZABLEXACT will be installed with AttachSerializableXact().  We
+        * also don't want to reject SERIALIZABLE READ ONLY DEFERRABLE in this
+        * case, because the leader has already determined that the snapshot it
+        * has passed us is safe.  So there is nothing for us to do.
+        */
+       if (IsParallelWorker())
+               return;
+
        /*
         * We do not allow SERIALIZABLE READ ONLY DEFERRABLE transactions to
         * import snapshots, since there's no way to wait for a safe snapshot when
-        * we're using the snap we're told to.  (XXX instead of throwing an error,
+        * we're using the snap we're told to.  (XXX instead of throwing an error,
         * we could just ignore the XactDeferrable flag?)
         */
        if (XactReadOnly && XactDeferrable)
@@ -1625,7 +1678,8 @@ SetSerializableTransactionSnapshot(Snapshot snapshot,
                                (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                 errmsg("a snapshot-importing transaction must not be READ ONLY DEFERRABLE")));
 
-       (void) GetSerializableTransactionSnapshotInt(snapshot, sourcexid);
+       (void) GetSerializableTransactionSnapshotInt(snapshot, sourcevxid,
+                                                                                                sourcepid);
 }
 
 /*
@@ -1639,19 +1693,27 @@ SetSerializableTransactionSnapshot(Snapshot snapshot,
  */
 static Snapshot
 GetSerializableTransactionSnapshotInt(Snapshot snapshot,
-                                                                         TransactionId sourcexid)
+                                                                         VirtualTransactionId *sourcevxid,
+                                                                         int sourcepid)
 {
        PGPROC     *proc;
        VirtualTransactionId vxid;
        SERIALIZABLEXACT *sxact,
                           *othersxact;
-       HASHCTL         hash_ctl;
 
        /* We only do this for serializable transactions.  Once. */
        Assert(MySerializableXact == InvalidSerializableXact);
 
        Assert(!RecoveryInProgress());
 
+       /*
+        * Since all parts of a serializable transaction must use the same
+        * snapshot, it is too late to establish one after a parallel operation
+        * has begun.
+        */
+       if (IsInParallelMode())
+               elog(ERROR, "cannot establish serializable snapshot during a parallel operation");
+
        proc = MyProc;
        Assert(proc != NULL);
        GET_VXID_FROM_PGPROC(vxid, *proc);
@@ -1666,7 +1728,7 @@ GetSerializableTransactionSnapshotInt(Snapshot snapshot,
         * release SerializableXactHashLock to call SummarizeOldestCommittedSxact,
         * this means we have to create the sxact first, which is a bit annoying
         * (in particular, an elog(ERROR) in procarray.c would cause us to leak
-        * the sxact).  Consider refactoring to avoid this.
+        * the sxact).  Consider refactoring to avoid this.
         */
 #ifdef TEST_OLDSERXID
        SummarizeOldestCommittedSxact();
@@ -1685,17 +1747,17 @@ GetSerializableTransactionSnapshotInt(Snapshot snapshot,
        } while (!sxact);
 
        /* Get the snapshot, or check that it's safe to use */
-       if (!TransactionIdIsValid(sourcexid))
+       if (!sourcevxid)
                snapshot = GetSnapshotData(snapshot);
-       else if (!ProcArrayInstallImportedXmin(snapshot->xmin, sourcexid))
+       else if (!ProcArrayInstallImportedXmin(snapshot->xmin, sourcevxid))
        {
                ReleasePredXact(sxact);
                LWLockRelease(SerializableXactHashLock);
                ereport(ERROR,
                                (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
                                 errmsg("could not import the requested snapshot"),
-                          errdetail("The source transaction %u is not running anymore.",
-                                                sourcexid)));
+                                errdetail("The source process with PID %d is not running anymore.",
+                                                  sourcepid)));
        }
 
        /*
@@ -1784,18 +1846,25 @@ GetSerializableTransactionSnapshotInt(Snapshot snapshot,
 
        LWLockRelease(SerializableXactHashLock);
 
+       CreateLocalPredicateLockHash();
+
+       return snapshot;
+}
+
+static void
+CreateLocalPredicateLockHash(void)
+{
+       HASHCTL         hash_ctl;
+
        /* Initialize the backend-local hash table of parent locks */
        Assert(LocalPredicateLockHash == NULL);
        MemSet(&hash_ctl, 0, sizeof(hash_ctl));
        hash_ctl.keysize = sizeof(PREDICATELOCKTARGETTAG);
        hash_ctl.entrysize = sizeof(LOCALPREDICATELOCK);
-       hash_ctl.hash = tag_hash;
        LocalPredicateLockHash = hash_create("Local predicate lock",
                                                                                 max_predicate_locks_per_xact,
                                                                                 &hash_ctl,
-                                                                                HASH_ELEM | HASH_FUNCTION);
-
-       return snapshot;
+                                                                                HASH_ELEM | HASH_BLOBS);
 }
 
 /*
@@ -1856,7 +1925,7 @@ PageIsPredicateLocked(Relation relation, BlockNumber blkno)
 {
        PREDICATELOCKTARGETTAG targettag;
        uint32          targettaghash;
-       LWLockId        partitionLock;
+       LWLock     *partitionLock;
        PREDICATELOCKTARGET *target;
 
        SET_PREDICATELOCKTARGETTAG_PAGE(targettag,
@@ -1928,17 +1997,17 @@ GetParentPredicateLockTag(const PREDICATELOCKTARGETTAG *tag,
                case PREDLOCKTAG_PAGE:
                        /* parent lock is relation lock */
                        SET_PREDICATELOCKTARGETTAG_RELATION(*parent,
-                                                                                GET_PREDICATELOCKTARGETTAG_DB(*tag),
-                                                                 GET_PREDICATELOCKTARGETTAG_RELATION(*tag));
+                                                                                               GET_PREDICATELOCKTARGETTAG_DB(*tag),
+                                                                                               GET_PREDICATELOCKTARGETTAG_RELATION(*tag));
 
                        return true;
 
                case PREDLOCKTAG_TUPLE:
                        /* parent lock is page lock */
                        SET_PREDICATELOCKTARGETTAG_PAGE(*parent,
-                                                                                GET_PREDICATELOCKTARGETTAG_DB(*tag),
-                                                                  GET_PREDICATELOCKTARGETTAG_RELATION(*tag),
-                                                                         GET_PREDICATELOCKTARGETTAG_PAGE(*tag));
+                                                                                       GET_PREDICATELOCKTARGETTAG_DB(*tag),
+                                                                                       GET_PREDICATELOCKTARGETTAG_RELATION(*tag),
+                                                                                       GET_PREDICATELOCKTARGETTAG_PAGE(*tag));
                        return true;
        }
 
@@ -2048,9 +2117,11 @@ RemoveTargetIfNoLongerUsed(PREDICATELOCKTARGET *target, uint32 targettaghash)
 /*
  * Delete child target locks owned by this process.
  * This implementation is assuming that the usage of each target tag field
- * is uniform. No need to make this hard if we don't have to.
+ * is uniform.  No need to make this hard if we don't have to.
  *
- * We aren't acquiring lightweight locks for the predicate lock or lock
+ * We acquire an LWLock in the case of parallel mode, because worker
+ * backends have access to the leader's SERIALIZABLEXACT.  Otherwise,
+ * we aren't acquiring LWLocks for the predicate lock or lock
  * target structures associated with this transaction unless we're going
  * to modify them, because no other process is permitted to modify our
  * locks.
@@ -2063,6 +2134,8 @@ DeleteChildTargetLocks(const PREDICATELOCKTARGETTAG *newtargettag)
 
        LWLockAcquire(SerializablePredicateLockListLock, LW_SHARED);
        sxact = MySerializableXact;
+       if (IsInParallelMode())
+               LWLockAcquire(&sxact->predicateLockListLock, LW_EXCLUSIVE);
        predlock = (PREDICATELOCK *)
                SHMQueueNext(&(sxact->predicateLocks),
                                         &(sxact->predicateLocks),
@@ -2089,7 +2162,7 @@ DeleteChildTargetLocks(const PREDICATELOCKTARGETTAG *newtargettag)
                if (TargetTagIsCoveredBy(oldtargettag, *newtargettag))
                {
                        uint32          oldtargettaghash;
-                       LWLockId        partitionLock;
+                       LWLock     *partitionLock;
                        PREDICATELOCK *rmpredlock PG_USED_FOR_ASSERTS_ONLY;
 
                        oldtargettaghash = PredicateLockTargetTagHashCode(&oldtargettag);
@@ -2116,32 +2189,41 @@ DeleteChildTargetLocks(const PREDICATELOCKTARGETTAG *newtargettag)
 
                predlock = nextpredlock;
        }
+       if (IsInParallelMode())
+               LWLockRelease(&sxact->predicateLockListLock);
        LWLockRelease(SerializablePredicateLockListLock);
 }
 
 /*
- * Returns the promotion threshold for a given predicate lock
- * target. This is the number of descendant locks required to promote
- * to the specified tag. Note that the threshold includes non-direct
- * descendants, e.g. both tuples and pages for a relation lock.
+ * Returns the promotion limit for a given predicate lock target.  This is the
+ * max number of descendant locks allowed before promoting to the specified
+ * tag. Note that the limit includes non-direct descendants (e.g., both tuples
+ * and pages for a relation lock).
+ *
+ * Currently the default limit is 2 for a page lock, and half of the value of
+ * max_pred_locks_per_transaction - 1 for a relation lock, to match behavior
+ * of earlier releases when upgrading.
  *
- * TODO SSI: We should do something more intelligent about what the
- * thresholds are, either making it proportional to the number of
- * tuples in a page & pages in a relation, or at least making it a
- * GUC. Currently the threshold is 3 for a page lock, and
- * max_pred_locks_per_transaction/2 for a relation lock, chosen
- * entirely arbitrarily (and without benchmarking).
+ * TODO SSI: We should probably add additional GUCs to allow a maximum ratio
+ * of page and tuple locks based on the pages in a relation, and the maximum
+ * ratio of tuple locks to tuples in a page.  This would provide more
+ * generally "balanced" allocation of locks to where they are most useful,
+ * while still allowing the absolute numbers to prevent one relation from
+ * tying up all predicate lock resources.
  */
 static int
-PredicateLockPromotionThreshold(const PREDICATELOCKTARGETTAG *tag)
+MaxPredicateChildLocks(const PREDICATELOCKTARGETTAG *tag)
 {
        switch (GET_PREDICATELOCKTARGETTAG_TYPE(*tag))
        {
                case PREDLOCKTAG_RELATION:
-                       return max_predicate_locks_per_xact / 2;
+                       return max_predicate_locks_per_relation < 0
+                               ? (max_predicate_locks_per_xact
+                                  / (-max_predicate_locks_per_relation)) - 1
+                               : max_predicate_locks_per_relation;
 
                case PREDLOCKTAG_PAGE:
-                       return 3;
+                       return max_predicate_locks_per_page;
 
                case PREDLOCKTAG_TUPLE:
 
@@ -2196,8 +2278,8 @@ CheckAndPromotePredicateLockRequest(const PREDICATELOCKTARGETTAG *reqtag)
                else
                        parentlock->childLocks++;
 
-               if (parentlock->childLocks >=
-                       PredicateLockPromotionThreshold(&targettag))
+               if (parentlock->childLocks >
+                       MaxPredicateChildLocks(&targettag))
                {
                        /*
                         * We should promote to this parent lock. Continue to check its
@@ -2301,12 +2383,14 @@ CreatePredicateLock(const PREDICATELOCKTARGETTAG *targettag,
        PREDICATELOCKTARGET *target;
        PREDICATELOCKTAG locktag;
        PREDICATELOCK *lock;
-       LWLockId        partitionLock;
+       LWLock     *partitionLock;
        bool            found;
 
        partitionLock = PredicateLockHashPartitionLock(targettaghash);
 
        LWLockAcquire(SerializablePredicateLockListLock, LW_SHARED);
+       if (IsInParallelMode())
+               LWLockAcquire(&sxact->predicateLockListLock, LW_EXCLUSIVE);
        LWLockAcquire(partitionLock, LW_EXCLUSIVE);
 
        /* Make sure that the target is represented. */
@@ -2327,7 +2411,7 @@ CreatePredicateLock(const PREDICATELOCKTARGETTAG *targettag,
        locktag.myXact = sxact;
        lock = (PREDICATELOCK *)
                hash_search_with_hash_value(PredicateLockHash, &locktag,
-                       PredicateLockHashCodeFromTargetHashCode(&locktag, targettaghash),
+                                                                       PredicateLockHashCodeFromTargetHashCode(&locktag, targettaghash),
                                                                        HASH_ENTER_NULL, &found);
        if (!lock)
                ereport(ERROR,
@@ -2344,6 +2428,8 @@ CreatePredicateLock(const PREDICATELOCKTARGETTAG *targettag,
        }
 
        LWLockRelease(partitionLock);
+       if (IsInParallelMode())
+               LWLockRelease(&sxact->predicateLockListLock);
        LWLockRelease(SerializablePredicateLockListLock);
 }
 
@@ -2494,7 +2580,7 @@ PredicateLockTuple(Relation relation, HeapTuple tuple, Snapshot snapshot)
        }
 
        /*
-        * Do quick-but-not-definitive test for a relation lock first.  This will
+        * Do quick-but-not-definitive test for a relation lock first.  This will
         * never cause a return when the relation is *not* locked, but will
         * occasionally let the check continue when there really *is* a relation
         * level lock.
@@ -2531,7 +2617,8 @@ DeleteLockTarget(PREDICATELOCKTARGET *target, uint32 targettaghash)
        PREDICATELOCK *nextpredlock;
        bool            found;
 
-       Assert(LWLockHeldByMe(SerializablePredicateLockListLock));
+       Assert(LWLockHeldByMeInMode(SerializablePredicateLockListLock,
+                                                               LW_EXCLUSIVE));
        Assert(LWLockHeldByMe(PredicateLockHashPartitionLock(targettaghash)));
 
        predlock = (PREDICATELOCK *)
@@ -2591,7 +2678,7 @@ DeleteLockTarget(PREDICATELOCKTARGET *target, uint32 targettaghash)
  * covers it, or if we are absolutely certain that no one will need to
  * refer to that lock in the future.
  *
- * Caller must hold SerializablePredicateLockListLock.
+ * Caller must hold SerializablePredicateLockListLock exclusively.
  */
 static bool
 TransferPredicateLocksToNewTarget(PREDICATELOCKTARGETTAG oldtargettag,
@@ -2599,14 +2686,15 @@ TransferPredicateLocksToNewTarget(PREDICATELOCKTARGETTAG oldtargettag,
                                                                  bool removeOld)
 {
        uint32          oldtargettaghash;
-       LWLockId        oldpartitionLock;
+       LWLock     *oldpartitionLock;
        PREDICATELOCKTARGET *oldtarget;
        uint32          newtargettaghash;
-       LWLockId        newpartitionLock;
+       LWLock     *newpartitionLock;
        bool            found;
        bool            outOfShmem = false;
 
-       Assert(LWLockHeldByMe(SerializablePredicateLockListLock));
+       Assert(LWLockHeldByMeInMode(SerializablePredicateLockListLock,
+                                                               LW_EXCLUSIVE));
 
        oldtargettaghash = PredicateLockTargetTagHashCode(&oldtargettag);
        newtargettaghash = PredicateLockTargetTagHashCode(&newtargettag);
@@ -2708,8 +2796,8 @@ TransferPredicateLocksToNewTarget(PREDICATELOCKTARGETTAG oldtargettag,
                                hash_search_with_hash_value
                                        (PredicateLockHash,
                                         &oldpredlock->tag,
-                                  PredicateLockHashCodeFromTargetHashCode(&oldpredlock->tag,
-                                                                                                                  oldtargettaghash),
+                                        PredicateLockHashCodeFromTargetHashCode(&oldpredlock->tag,
+                                                                                                                        oldtargettaghash),
                                         HASH_REMOVE, &found);
                                Assert(found);
                        }
@@ -2717,8 +2805,8 @@ TransferPredicateLocksToNewTarget(PREDICATELOCKTARGETTAG oldtargettag,
                        newpredlock = (PREDICATELOCK *)
                                hash_search_with_hash_value(PredicateLockHash,
                                                                                        &newpredlocktag,
-                                        PredicateLockHashCodeFromTargetHashCode(&newpredlocktag,
-                                                                                                                  newtargettaghash),
+                                                                                       PredicateLockHashCodeFromTargetHashCode(&newpredlocktag,
+                                                                                                                                                                       newtargettaghash),
                                                                                        HASH_ENTER_NULL,
                                                                                        &found);
                        if (!newpredlock)
@@ -2779,7 +2867,7 @@ exit:
                /* We shouldn't run out of memory if we're moving locks */
                Assert(!outOfShmem);
 
-               /* Put the scrach entry back */
+               /* Put the scratch entry back */
                RestoreScratchTarget(false);
        }
 
@@ -2806,7 +2894,7 @@ exit:
  * transaction which is not serializable.
  *
  * NOTE: This is currently only called with transfer set to true, but that may
- * change.     If we decide to clean up the locks from a table on commit of a
+ * change.  If we decide to clean up the locks from a table on commit of a
  * transaction which executed DROP TABLE, the false condition will be useful.
  */
 static void
@@ -2848,8 +2936,8 @@ DropAllPredicateLocksFromTable(Relation relation, bool transfer)
                heapId = relation->rd_index->indrelid;
        }
        Assert(heapId != InvalidOid);
-       Assert(transfer || !isIndex);           /* index OID only makes sense with
-                                                                                * transfer */
+       Assert(transfer || !isIndex);   /* index OID only makes sense with
+                                                                        * transfer */
 
        /* Retrieve first time needed, then keep. */
        heaptargettaghash = 0;
@@ -2858,7 +2946,7 @@ DropAllPredicateLocksFromTable(Relation relation, bool transfer)
        /* Acquire locks on all lock partitions */
        LWLockAcquire(SerializablePredicateLockListLock, LW_EXCLUSIVE);
        for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
-               LWLockAcquire(FirstPredicateLockMgrLock + i, LW_EXCLUSIVE);
+               LWLockAcquire(PredicateLockHashPartitionLockByIndex(i), LW_EXCLUSIVE);
        LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
 
        /*
@@ -2887,7 +2975,7 @@ DropAllPredicateLocksFromTable(Relation relation, bool transfer)
                        continue;                       /* already the right lock */
 
                /*
-                * If we made it here, we have work to do.      We make sure the heap
+                * If we made it here, we have work to do.  We make sure the heap
                 * relation lock exists, then we walk the list of predicate locks for
                 * the old target we found, moving all locks to the heap relation lock
                 * -- unless they already hold that.
@@ -2958,8 +3046,8 @@ DropAllPredicateLocksFromTable(Relation relation, bool transfer)
                                newpredlock = (PREDICATELOCK *)
                                        hash_search_with_hash_value(PredicateLockHash,
                                                                                                &newpredlocktag,
-                                        PredicateLockHashCodeFromTargetHashCode(&newpredlocktag,
-                                                                                                                 heaptargettaghash),
+                                                                                               PredicateLockHashCodeFromTargetHashCode(&newpredlocktag,
+                                                                                                                                                                               heaptargettaghash),
                                                                                                HASH_ENTER,
                                                                                                &found);
                                if (!found)
@@ -2996,7 +3084,7 @@ DropAllPredicateLocksFromTable(Relation relation, bool transfer)
        /* Release locks in reverse order */
        LWLockRelease(SerializableXactHashLock);
        for (i = NUM_PREDICATELOCK_PARTITIONS - 1; i >= 0; i--)
-               LWLockRelease(FirstPredicateLockMgrLock + i);
+               LWLockRelease(PredicateLockHashPartitionLockByIndex(i));
        LWLockRelease(SerializablePredicateLockListLock);
 }
 
@@ -3182,9 +3270,17 @@ SetNewSxactGlobalXmin(void)
  * If this transaction is committing and is holding any predicate locks,
  * it must be added to a list of completed serializable transactions still
  * holding locks.
+ *
+ * If isReadOnlySafe is true, then predicate locks are being released before
+ * the end of the transaction because MySerializableXact has been determined
+ * to be RO_SAFE.  In non-parallel mode we can release it completely, but it
+ * in parallel mode we partially release the SERIALIZABLEXACT and keep it
+ * around until the end of the transaction, allowing each backend to clear its
+ * MySerializableXact variable and benefit from the optimization in its own
+ * time.
  */
 void
-ReleasePredicateLocks(bool isCommit)
+ReleasePredicateLocks(bool isCommit, bool isReadOnlySafe)
 {
        bool            needToClear;
        RWConflict      conflict,
@@ -3195,7 +3291,7 @@ ReleasePredicateLocks(bool isCommit)
        /*
         * We can't trust XactReadOnly here, because a transaction which started
         * as READ WRITE can show as READ ONLY later, e.g., within
-        * substransactions.  We want to flag a transaction as READ ONLY if it
+        * subtransactions.  We want to flag a transaction as READ ONLY if it
         * commits without writing so that de facto READ ONLY transactions get the
         * benefit of some RO optimizations, so we will use this local variable to
         * get some cleanup logic right which is based on whether the transaction
@@ -3203,44 +3299,122 @@ ReleasePredicateLocks(bool isCommit)
         */
        bool            topLevelIsDeclaredReadOnly;
 
+       /* We can't be both committing and releasing early due to RO_SAFE. */
+       Assert(!(isCommit && isReadOnlySafe));
+
+       /* Are we at the end of a transaction, that is, a commit or abort? */
+       if (!isReadOnlySafe)
+       {
+               /*
+                * Parallel workers mustn't release predicate locks at the end of
+                * their transaction.  The leader will do that at the end of its
+                * transaction.
+                */
+               if (IsParallelWorker())
+               {
+                       ReleasePredicateLocksLocal();
+                       return;
+               }
+
+               /*
+                * By the time the leader in a parallel query reaches end of
+                * transaction, it has waited for all workers to exit.
+                */
+               Assert(!ParallelContextActive());
+
+               /*
+                * If the leader in a parallel query earlier stashed a partially
+                * released SERIALIZABLEXACT for final clean-up at end of transaction
+                * (because workers might still have been accessing it), then it's
+                * time to restore it.
+                */
+               if (SavedSerializableXact != InvalidSerializableXact)
+               {
+                       Assert(MySerializableXact == InvalidSerializableXact);
+                       MySerializableXact = SavedSerializableXact;
+                       SavedSerializableXact = InvalidSerializableXact;
+                       Assert(SxactIsPartiallyReleased(MySerializableXact));
+               }
+       }
+
        if (MySerializableXact == InvalidSerializableXact)
        {
                Assert(LocalPredicateLockHash == NULL);
                return;
        }
 
+       LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
+
+       /*
+        * If the transaction is committing, but it has been partially released
+        * already, then treat this as a roll back.  It was marked as rolled back.
+        */
+       if (isCommit && SxactIsPartiallyReleased(MySerializableXact))
+               isCommit = false;
+
+       /*
+        * If we're called in the middle of a transaction because we discovered
+        * that the SXACT_FLAG_RO_SAFE flag was set, then we'll partially release
+        * it (that is, release the predicate locks and conflicts, but not the
+        * SERIALIZABLEXACT itself) if we're the first backend to have noticed.
+        */
+       if (isReadOnlySafe && IsInParallelMode())
+       {
+               /*
+                * The leader needs to stash a pointer to it, so that it can
+                * completely release it at end-of-transaction.
+                */
+               if (!IsParallelWorker())
+                       SavedSerializableXact = MySerializableXact;
+
+               /*
+                * The first backend to reach this condition will partially release
+                * the SERIALIZABLEXACT.  All others will just clear their
+                * backend-local state so that they stop doing SSI checks for the rest
+                * of the transaction.
+                */
+               if (SxactIsPartiallyReleased(MySerializableXact))
+               {
+                       LWLockRelease(SerializableXactHashLock);
+                       ReleasePredicateLocksLocal();
+                       return;
+               }
+               else
+               {
+                       MySerializableXact->flags |= SXACT_FLAG_PARTIALLY_RELEASED;
+                       /* ... and proceed to perform the partial release below. */
+               }
+       }
        Assert(!isCommit || SxactIsPrepared(MySerializableXact));
        Assert(!isCommit || !SxactIsDoomed(MySerializableXact));
        Assert(!SxactIsCommitted(MySerializableXact));
-       Assert(!SxactIsRolledBack(MySerializableXact));
+       Assert(SxactIsPartiallyReleased(MySerializableXact)
+                  || !SxactIsRolledBack(MySerializableXact));
 
        /* may not be serializable during COMMIT/ROLLBACK PREPARED */
-       if (MySerializableXact->pid != 0)
-               Assert(IsolationIsSerializable());
+       Assert(MySerializableXact->pid == 0 || IsolationIsSerializable());
 
        /* We'd better not already be on the cleanup list. */
        Assert(!SxactIsOnFinishedList(MySerializableXact));
 
        topLevelIsDeclaredReadOnly = SxactIsReadOnly(MySerializableXact);
 
-       LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
-
        /*
         * We don't hold XidGenLock lock here, assuming that TransactionId is
         * atomic!
         *
         * If this value is changing, we don't care that much whether we get the
         * old or new value -- it is just used to determine how far
-        * GlobalSerizableXmin must advance before this transaction can be fully
-        * cleaned up.  The worst that could happen is we wait for one more
+        * GlobalSerializableXmin must advance before this transaction can be
+        * fully cleaned up.  The worst that could happen is we wait for one more
         * transaction to complete before freeing some RAM; correctness of visible
         * behavior is not affected.
         */
-       MySerializableXact->finishedBefore = ShmemVariableCache->nextXid;
+       MySerializableXact->finishedBefore = XidFromFullTransactionId(ShmemVariableCache->nextFullXid);
 
        /*
-        * If it's not a commit it's a rollback, and we can clear our locks
-        * immediately.
+        * If it's not a commit it's either a rollback or a read-only transaction
+        * flagged SXACT_FLAG_RO_SAFE, and we can clear our locks immediately.
         */
        if (isCommit)
        {
@@ -3335,7 +3509,7 @@ ReleasePredicateLocks(bool isCommit)
        }
 
        /*
-        * Release all outConflicts to committed transactions.  If we're rolling
+        * Release all outConflicts to committed transactions.  If we're rolling
         * back clear them all.  Set SXACT_FLAG_CONFLICT_OUT if any point to
         * previously committed transactions.
         */
@@ -3479,14 +3653,28 @@ ReleasePredicateLocks(bool isCommit)
                SHMQueueInsertBefore(FinishedSerializableTransactions,
                                                         &MySerializableXact->finishedLink);
 
+       /*
+        * If we're releasing a RO_SAFE transaction in parallel mode, we'll only
+        * partially release it.  That's necessary because other backends may have
+        * a reference to it.  The leader will release the SERIALIZABLEXACT itself
+        * at the end of the transaction after workers have stopped running.
+        */
        if (!isCommit)
-               ReleaseOneSerializableXact(MySerializableXact, false, false);
+               ReleaseOneSerializableXact(MySerializableXact,
+                                                                  isReadOnlySafe && IsInParallelMode(),
+                                                                  false);
 
        LWLockRelease(SerializableFinishedListLock);
 
        if (needToClear)
                ClearOldPredicateLocks();
 
+       ReleasePredicateLocksLocal();
+}
+
+static void
+ReleasePredicateLocksLocal(void)
+{
        MySerializableXact = InvalidSerializableXact;
        MyXactDidWrite = false;
 
@@ -3540,7 +3728,7 @@ ClearOldPredicateLocks(void)
                        LWLockAcquire(SerializableXactHashLock, LW_SHARED);
                }
                else if (finishedSxact->commitSeqNo > PredXact->HavePartialClearedThrough
-                  && finishedSxact->commitSeqNo <= PredXact->CanPartialClearThrough)
+                                && finishedSxact->commitSeqNo <= PredXact->CanPartialClearThrough)
                {
                        /*
                         * Any active transactions that took their snapshot before this
@@ -3611,7 +3799,7 @@ ClearOldPredicateLocks(void)
                        PREDICATELOCKTARGET *target;
                        PREDICATELOCKTARGETTAG targettag;
                        uint32          targettaghash;
-                       LWLockId        partitionLock;
+                       LWLock     *partitionLock;
 
                        tag = predlock->tag;
                        target = tag.myTarget;
@@ -3625,8 +3813,8 @@ ClearOldPredicateLocks(void)
                        SHMQueueDelete(&(predlock->xactLink));
 
                        hash_search_with_hash_value(PredicateLockHash, &tag,
-                                                               PredicateLockHashCodeFromTargetHashCode(&tag,
-                                                                                                                         targettaghash),
+                                                                               PredicateLockHashCodeFromTargetHashCode(&tag,
+                                                                                                                                                               targettaghash),
                                                                                HASH_REMOVE, NULL);
                        RemoveTargetIfNoLongerUsed(target, targettaghash);
 
@@ -3654,7 +3842,7 @@ ClearOldPredicateLocks(void)
  * matter -- but keep the transaction entry itself and any outConflicts.
  *
  * When the summarize flag is set, we've run short of room for sxact data
- * and must summarize to the SLRU.     Predicate locks are transferred to a
+ * and must summarize to the SLRU.  Predicate locks are transferred to a
  * dummy "old" transaction, with duplicate locks on a single target
  * collapsing to a single lock with the "latest" commitSeqNo from among
  * the conflicting locks..
@@ -3678,6 +3866,8 @@ ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial,
         * them to OldCommittedSxact if summarize is true)
         */
        LWLockAcquire(SerializablePredicateLockListLock, LW_SHARED);
+       if (IsInParallelMode())
+               LWLockAcquire(&sxact->predicateLockListLock, LW_EXCLUSIVE);
        predlock = (PREDICATELOCK *)
                SHMQueueNext(&(sxact->predicateLocks),
                                         &(sxact->predicateLocks),
@@ -3690,7 +3880,7 @@ ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial,
                PREDICATELOCKTARGET *target;
                PREDICATELOCKTARGETTAG targettag;
                uint32          targettaghash;
-               LWLockId        partitionLock;
+               LWLock     *partitionLock;
 
                nextpredlock = (PREDICATELOCK *)
                        SHMQueueNext(&(sxact->predicateLocks),
@@ -3709,8 +3899,8 @@ ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial,
                SHMQueueDelete(targetLink);
 
                hash_search_with_hash_value(PredicateLockHash, &tag,
-                                                               PredicateLockHashCodeFromTargetHashCode(&tag,
-                                                                                                                         targettaghash),
+                                                                       PredicateLockHashCodeFromTargetHashCode(&tag,
+                                                                                                                                                       targettaghash),
                                                                        HASH_REMOVE, NULL);
                if (summarize)
                {
@@ -3719,8 +3909,8 @@ ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial,
                        /* Fold into dummy transaction list. */
                        tag.myXact = OldCommittedSxact;
                        predlock = hash_search_with_hash_value(PredicateLockHash, &tag,
-                                                               PredicateLockHashCodeFromTargetHashCode(&tag,
-                                                                                                                         targettaghash),
+                                                                                                  PredicateLockHashCodeFromTargetHashCode(&tag,
+                                                                                                                                                                                  targettaghash),
                                                                                                   HASH_ENTER_NULL, &found);
                        if (!predlock)
                                ereport(ERROR,
@@ -3757,6 +3947,8 @@ ReleaseOneSerializableXact(SERIALIZABLEXACT *sxact, bool partial,
         */
        SHMQueueInit(&sxact->predicateLocks);
 
+       if (IsInParallelMode())
+               LWLockRelease(&sxact->predicateLockListLock);
        LWLockRelease(SerializablePredicateLockListLock);
 
        sxidtag.xid = sxact->topXid;
@@ -3847,7 +4039,7 @@ XidIsConcurrent(TransactionId xid)
 /*
  * CheckForSerializableConflictOut
  *             We are reading a tuple which has been modified.  If it is visible to
- *             us but has been deleted, that indicates a rw-conflict out.      If it's
+ *             us but has been deleted, that indicates a rw-conflict out.  If it's
  *             not visible and was created by a concurrent (overlapping)
  *             serializable transaction, that is also a rw-conflict out,
  *
@@ -3934,7 +4126,7 @@ CheckForSerializableConflictOut(bool visible, Relation relation,
        Assert(TransactionIdFollowsOrEquals(xid, TransactionXmin));
 
        /*
-        * Find top level xid.  Bail out if xid is too early to be a conflict, or
+        * Find top level xid.  Bail out if xid is too early to be a conflict, or
         * if it's our own xid.
         */
        if (TransactionIdEquals(xid, GetTopTransactionIdIfAny()))
@@ -3971,7 +4163,7 @@ CheckForSerializableConflictOut(bool visible, Relation relation,
                                                (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
                                                 errmsg("could not serialize access due to read/write dependencies among transactions"),
                                                 errdetail_internal("Reason code: Canceled on conflict out to old pivot %u.", xid),
-                                         errhint("The transaction might succeed if retried.")));
+                                                errhint("The transaction might succeed if retried.")));
 
                        if (SxactHasSummaryConflictIn(MySerializableXact)
                                || !SHMQueueEmpty(&MySerializableXact->inConflicts))
@@ -3979,7 +4171,7 @@ CheckForSerializableConflictOut(bool visible, Relation relation,
                                                (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
                                                 errmsg("could not serialize access due to read/write dependencies among transactions"),
                                                 errdetail_internal("Reason code: Canceled on identification as a pivot, with conflict out to old committed transaction %u.", xid),
-                                         errhint("The transaction might succeed if retried.")));
+                                                errhint("The transaction might succeed if retried.")));
 
                        MySerializableXact->flags |= SXACT_FLAG_SUMMARY_CONFLICT_OUT;
                }
@@ -3999,7 +4191,7 @@ CheckForSerializableConflictOut(bool visible, Relation relation,
 
        /*
         * We have a conflict out to a transaction which has a conflict out to a
-        * summarized transaction.      That summarized transaction must have
+        * summarized transaction.  That summarized transaction must have
         * committed first, and we can't tell when it committed in relation to our
         * snapshot acquisition, so something needs to be canceled.
         */
@@ -4033,7 +4225,7 @@ CheckForSerializableConflictOut(bool visible, Relation relation,
                && (!SxactHasConflictOut(sxact)
                        || MySerializableXact->SeqNo.lastCommitBeforeSnapshot < sxact->SeqNo.earliestOutConflictCommit))
        {
-               /* Read-only transaction will appear to run first.      No conflict. */
+               /* Read-only transaction will appear to run first.  No conflict. */
                LWLockRelease(SerializableXactHashLock);
                return;
        }
@@ -4068,7 +4260,7 @@ static void
 CheckTargetForConflictsIn(PREDICATELOCKTARGETTAG *targettag)
 {
        uint32          targettaghash;
-       LWLockId        partitionLock;
+       LWLock     *partitionLock;
        PREDICATELOCKTARGET *target;
        PREDICATELOCK *predlock;
        PREDICATELOCK *mypredlock = NULL;
@@ -4168,7 +4360,7 @@ CheckTargetForConflictsIn(PREDICATELOCKTARGETTAG *targettag)
        /*
         * If we found one of our own SIREAD locks to remove, remove it now.
         *
-        * At this point our transaction already has an ExclusiveRowLock on the
+        * At this point our transaction already has a RowExclusiveLock on the
         * relation, so we are OK to drop the predicate lock on the tuple, if
         * found, without fearing that another write against the tuple will occur
         * before the MVCC information makes it to the buffer.
@@ -4179,6 +4371,8 @@ CheckTargetForConflictsIn(PREDICATELOCKTARGETTAG *targettag)
                PREDICATELOCK *rmpredlock;
 
                LWLockAcquire(SerializablePredicateLockListLock, LW_SHARED);
+               if (IsInParallelMode())
+                       LWLockAcquire(&MySerializableXact->predicateLockListLock, LW_EXCLUSIVE);
                LWLockAcquire(partitionLock, LW_EXCLUSIVE);
                LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
 
@@ -4213,6 +4407,8 @@ CheckTargetForConflictsIn(PREDICATELOCKTARGETTAG *targettag)
 
                LWLockRelease(SerializableXactHashLock);
                LWLockRelease(partitionLock);
+               if (IsInParallelMode())
+                       LWLockRelease(&MySerializableXact->predicateLockListLock);
                LWLockRelease(SerializablePredicateLockListLock);
 
                if (rmpredlock != NULL)
@@ -4279,8 +4475,8 @@ CheckForSerializableConflictIn(Relation relation, HeapTuple tuple,
                SET_PREDICATELOCKTARGETTAG_TUPLE(targettag,
                                                                                 relation->rd_node.dbNode,
                                                                                 relation->rd_id,
-                                                                 ItemPointerGetBlockNumber(&(tuple->t_self)),
-                                                               ItemPointerGetOffsetNumber(&(tuple->t_self)));
+                                                                                ItemPointerGetBlockNumber(&(tuple->t_self)),
+                                                                                ItemPointerGetOffsetNumber(&(tuple->t_self)));
                CheckTargetForConflictsIn(&targettag);
        }
 
@@ -4360,8 +4556,8 @@ CheckTableForSerializableConflictIn(Relation relation)
 
        LWLockAcquire(SerializablePredicateLockListLock, LW_EXCLUSIVE);
        for (i = 0; i < NUM_PREDICATELOCK_PARTITIONS; i++)
-               LWLockAcquire(FirstPredicateLockMgrLock + i, LW_SHARED);
-       LWLockAcquire(SerializableXactHashLock, LW_SHARED);
+               LWLockAcquire(PredicateLockHashPartitionLockByIndex(i), LW_SHARED);
+       LWLockAcquire(SerializableXactHashLock, LW_EXCLUSIVE);
 
        /* Scan through target list */
        hash_seq_init(&seqstat, PredicateLockTargetHash);
@@ -4395,7 +4591,7 @@ CheckTableForSerializableConflictIn(Relation relation)
                                                         offsetof(PREDICATELOCK, targetLink));
 
                        if (predlock->tag.myXact != MySerializableXact
-                         && !RWConflictExists(predlock->tag.myXact, MySerializableXact))
+                               && !RWConflictExists(predlock->tag.myXact, MySerializableXact))
                        {
                                FlagRWConflict(predlock->tag.myXact, MySerializableXact);
                        }
@@ -4407,7 +4603,7 @@ CheckTableForSerializableConflictIn(Relation relation)
        /* Release locks in reverse order */
        LWLockRelease(SerializableXactHashLock);
        for (i = NUM_PREDICATELOCK_PARTITIONS - 1; i >= 0; i--)
-               LWLockRelease(FirstPredicateLockMgrLock + i);
+               LWLockRelease(PredicateLockHashPartitionLockByIndex(i));
        LWLockRelease(SerializablePredicateLockListLock);
 }
 
@@ -4476,7 +4672,7 @@ OnConflict_CheckForSerializationFailure(const SERIALIZABLEXACT *reader,
         *------------------------------------------------------------------------
         */
        if (SxactIsCommitted(writer)
-         && (SxactHasConflictOut(writer) || SxactHasSummaryConflictOut(writer)))
+               && (SxactHasConflictOut(writer) || SxactHasSummaryConflictOut(writer)))
                failure = true;
 
        /*------------------------------------------------------------------------
@@ -4520,7 +4716,7 @@ OnConflict_CheckForSerializationFailure(const SERIALIZABLEXACT *reader,
                                && (!SxactIsCommitted(writer)
                                        || t2->prepareSeqNo <= writer->commitSeqNo)
                                && (!SxactIsReadOnly(reader)
-                         || t2->prepareSeqNo <= reader->SeqNo.lastCommitBeforeSnapshot))
+                                       || t2->prepareSeqNo <= reader->SeqNo.lastCommitBeforeSnapshot))
                        {
                                failure = true;
                                break;
@@ -4565,7 +4761,7 @@ OnConflict_CheckForSerializationFailure(const SERIALIZABLEXACT *reader,
                                && (!SxactIsCommitted(t0)
                                        || t0->commitSeqNo >= writer->prepareSeqNo)
                                && (!SxactIsReadOnly(t0)
-                         || t0->SeqNo.lastCommitBeforeSnapshot >= writer->prepareSeqNo))
+                                       || t0->SeqNo.lastCommitBeforeSnapshot >= writer->prepareSeqNo))
                        {
                                failure = true;
                                break;
@@ -4624,7 +4820,7 @@ OnConflict_CheckForSerializationFailure(const SERIALIZABLEXACT *reader,
  *
  * If a dangerous structure is found, the pivot (the near conflict) is
  * marked for death, because rolling back another transaction might mean
- * that we flail without ever making progress. This transaction is
+ * that we flail without ever making progress.  This transaction is
  * committing writes, so letting it commit ensures progress.  If we
  * canceled the far conflict, it might immediately fail again on retry.
  */
@@ -4643,6 +4839,7 @@ PreCommit_CheckForSerializationFailure(void)
        /* Check if someone else has already decided that we need to die */
        if (SxactIsDoomed(MySerializableXact))
        {
+               Assert(!SxactIsPartiallyReleased(MySerializableXact));
                LWLockRelease(SerializableXactHashLock);
                ereport(ERROR,
                                (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE),
@@ -4737,7 +4934,7 @@ AtPrepare_PredicateLocks(void)
        if (MySerializableXact == InvalidSerializableXact)
                return;
 
-       /* Generate a xact record for our SERIALIZABLEXACT */
+       /* Generate an xact record for our SERIALIZABLEXACT */
        record.type = TWOPHASEPREDICATERECORD_XACT;
        xactRecord->xmin = MySerializableXact->xmin;
        xactRecord->flags = MySerializableXact->flags;
@@ -4761,6 +4958,13 @@ AtPrepare_PredicateLocks(void)
         */
        LWLockAcquire(SerializablePredicateLockListLock, LW_SHARED);
 
+       /*
+        * No need to take sxact->predicateLockListLock in parallel mode because
+        * there cannot be any parallel workers running while we are preparing a
+        * transaction.
+        */
+       Assert(!IsParallelWorker() && !ParallelContextActive());
+
        predlock = (PREDICATELOCK *)
                SHMQueueNext(&(sxact->predicateLocks),
                                         &(sxact->predicateLocks),
@@ -4833,7 +5037,7 @@ PredicateLockTwoPhaseFinish(TransactionId xid, bool isCommit)
        MySerializableXact = sxid->myXact;
        MyXactDidWrite = true;          /* conservatively assume that we wrote
                                                                 * something */
-       ReleasePredicateLocks(isCommit);
+       ReleasePredicateLocks(isCommit, false);
 }
 
 /*
@@ -4969,3 +5173,28 @@ predicatelock_twophase_recover(TransactionId xid, uint16 info,
                CreatePredicateLock(&lockRecord->target, targettaghash, sxact);
        }
 }
+
+/*
+ * Prepare to share the current SERIALIZABLEXACT with parallel workers.
+ * Return a handle object that can be used by AttachSerializableXact() in a
+ * parallel worker.
+ */
+SerializableXactHandle
+ShareSerializableXact(void)
+{
+       return MySerializableXact;
+}
+
+/*
+ * Allow parallel workers to import the leader's SERIALIZABLEXACT.
+ */
+void
+AttachSerializableXact(SerializableXactHandle handle)
+{
+
+       Assert(MySerializableXact == InvalidSerializableXact);
+
+       MySerializableXact = (SERIALIZABLEXACT *) handle;
+       if (MySerializableXact != InvalidSerializableXact)
+               CreateLocalPredicateLockHash();
+}