LCOV - code coverage report
Current view: top level - src/backend/storage/lmgr - lock.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 1110 1277 86.9 %
Date: 2025-04-01 16:15:31 Functions: 56 60 93.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * lock.c
       4             :  *    POSTGRES primary lock mechanism
       5             :  *
       6             :  * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  *
      10             :  * IDENTIFICATION
      11             :  *    src/backend/storage/lmgr/lock.c
      12             :  *
      13             :  * NOTES
      14             :  *    A lock table is a shared memory hash table.  When
      15             :  *    a process tries to acquire a lock of a type that conflicts
      16             :  *    with existing locks, it is put to sleep using the routines
      17             :  *    in storage/lmgr/proc.c.
      18             :  *
      19             :  *    For the most part, this code should be invoked via lmgr.c
      20             :  *    or another lock-management module, not directly.
      21             :  *
      22             :  *  Interface:
      23             :  *
      24             :  *  LockManagerShmemInit(), GetLocksMethodTable(), GetLockTagsMethodTable(),
      25             :  *  LockAcquire(), LockRelease(), LockReleaseAll(),
      26             :  *  LockCheckConflicts(), GrantLock()
      27             :  *
      28             :  *-------------------------------------------------------------------------
      29             :  */
      30             : #include "postgres.h"
      31             : 
      32             : #include <signal.h>
      33             : #include <unistd.h>
      34             : 
      35             : #include "access/transam.h"
      36             : #include "access/twophase.h"
      37             : #include "access/twophase_rmgr.h"
      38             : #include "access/xlog.h"
      39             : #include "access/xlogutils.h"
      40             : #include "miscadmin.h"
      41             : #include "pg_trace.h"
      42             : #include "storage/lmgr.h"
      43             : #include "storage/proc.h"
      44             : #include "storage/procarray.h"
      45             : #include "storage/spin.h"
      46             : #include "storage/standby.h"
      47             : #include "utils/memutils.h"
      48             : #include "utils/ps_status.h"
      49             : #include "utils/resowner.h"
      50             : 
      51             : 
      52             : /* GUC variables */
      53             : int         max_locks_per_xact; /* used to set the lock table size */
      54             : bool        log_lock_failure = false;
      55             : 
      56             : #define NLOCKENTS() \
      57             :     mul_size(max_locks_per_xact, add_size(MaxBackends, max_prepared_xacts))
      58             : 
      59             : 
      60             : /*
      61             :  * Data structures defining the semantics of the standard lock methods.
      62             :  *
      63             :  * The conflict table defines the semantics of the various lock modes.
      64             :  */
      65             : static const LOCKMASK LockConflicts[] = {
      66             :     0,
      67             : 
      68             :     /* AccessShareLock */
      69             :     LOCKBIT_ON(AccessExclusiveLock),
      70             : 
      71             :     /* RowShareLock */
      72             :     LOCKBIT_ON(ExclusiveLock) | LOCKBIT_ON(AccessExclusiveLock),
      73             : 
      74             :     /* RowExclusiveLock */
      75             :     LOCKBIT_ON(ShareLock) | LOCKBIT_ON(ShareRowExclusiveLock) |
      76             :     LOCKBIT_ON(ExclusiveLock) | LOCKBIT_ON(AccessExclusiveLock),
      77             : 
      78             :     /* ShareUpdateExclusiveLock */
      79             :     LOCKBIT_ON(ShareUpdateExclusiveLock) |
      80             :     LOCKBIT_ON(ShareLock) | LOCKBIT_ON(ShareRowExclusiveLock) |
      81             :     LOCKBIT_ON(ExclusiveLock) | LOCKBIT_ON(AccessExclusiveLock),
      82             : 
      83             :     /* ShareLock */
      84             :     LOCKBIT_ON(RowExclusiveLock) | LOCKBIT_ON(ShareUpdateExclusiveLock) |
      85             :     LOCKBIT_ON(ShareRowExclusiveLock) |
      86             :     LOCKBIT_ON(ExclusiveLock) | LOCKBIT_ON(AccessExclusiveLock),
      87             : 
      88             :     /* ShareRowExclusiveLock */
      89             :     LOCKBIT_ON(RowExclusiveLock) | LOCKBIT_ON(ShareUpdateExclusiveLock) |
      90             :     LOCKBIT_ON(ShareLock) | LOCKBIT_ON(ShareRowExclusiveLock) |
      91             :     LOCKBIT_ON(ExclusiveLock) | LOCKBIT_ON(AccessExclusiveLock),
      92             : 
      93             :     /* ExclusiveLock */
      94             :     LOCKBIT_ON(RowShareLock) |
      95             :     LOCKBIT_ON(RowExclusiveLock) | LOCKBIT_ON(ShareUpdateExclusiveLock) |
      96             :     LOCKBIT_ON(ShareLock) | LOCKBIT_ON(ShareRowExclusiveLock) |
      97             :     LOCKBIT_ON(ExclusiveLock) | LOCKBIT_ON(AccessExclusiveLock),
      98             : 
      99             :     /* AccessExclusiveLock */
     100             :     LOCKBIT_ON(AccessShareLock) | LOCKBIT_ON(RowShareLock) |
     101             :     LOCKBIT_ON(RowExclusiveLock) | LOCKBIT_ON(ShareUpdateExclusiveLock) |
     102             :     LOCKBIT_ON(ShareLock) | LOCKBIT_ON(ShareRowExclusiveLock) |
     103             :     LOCKBIT_ON(ExclusiveLock) | LOCKBIT_ON(AccessExclusiveLock)
     104             : 
     105             : };
     106             : 
     107             : /* Names of lock modes, for debug printouts */
     108             : static const char *const lock_mode_names[] =
     109             : {
     110             :     "INVALID",
     111             :     "AccessShareLock",
     112             :     "RowShareLock",
     113             :     "RowExclusiveLock",
     114             :     "ShareUpdateExclusiveLock",
     115             :     "ShareLock",
     116             :     "ShareRowExclusiveLock",
     117             :     "ExclusiveLock",
     118             :     "AccessExclusiveLock"
     119             : };
     120             : 
     121             : #ifndef LOCK_DEBUG
     122             : static bool Dummy_trace = false;
     123             : #endif
     124             : 
     125             : static const LockMethodData default_lockmethod = {
     126             :     MaxLockMode,
     127             :     LockConflicts,
     128             :     lock_mode_names,
     129             : #ifdef LOCK_DEBUG
     130             :     &Trace_locks
     131             : #else
     132             :     &Dummy_trace
     133             : #endif
     134             : };
     135             : 
     136             : static const LockMethodData user_lockmethod = {
     137             :     MaxLockMode,
     138             :     LockConflicts,
     139             :     lock_mode_names,
     140             : #ifdef LOCK_DEBUG
     141             :     &Trace_userlocks
     142             : #else
     143             :     &Dummy_trace
     144             : #endif
     145             : };
     146             : 
     147             : /*
     148             :  * map from lock method id to the lock table data structures
     149             :  */
     150             : static const LockMethod LockMethods[] = {
     151             :     NULL,
     152             :     &default_lockmethod,
     153             :     &user_lockmethod
     154             : };
     155             : 
     156             : 
     157             : /* Record that's written to 2PC state file when a lock is persisted */
     158             : typedef struct TwoPhaseLockRecord
     159             : {
     160             :     LOCKTAG     locktag;
     161             :     LOCKMODE    lockmode;
     162             : } TwoPhaseLockRecord;
     163             : 
     164             : 
     165             : /*
     166             :  * Count of the number of fast path lock slots we believe to be used.  This
     167             :  * might be higher than the real number if another backend has transferred
     168             :  * our locks to the primary lock table, but it can never be lower than the
     169             :  * real value, since only we can acquire locks on our own behalf.
     170             :  *
     171             :  * XXX Allocate a static array of the maximum size. We could use a pointer
     172             :  * and then allocate just the right size to save a couple kB, but then we
     173             :  * would have to initialize that, while for the static array that happens
     174             :  * automatically. Doesn't seem worth the extra complexity.
     175             :  */
     176             : static int  FastPathLocalUseCounts[FP_LOCK_GROUPS_PER_BACKEND_MAX];
     177             : 
     178             : /*
     179             :  * Flag to indicate if the relation extension lock is held by this backend.
     180             :  * This flag is used to ensure that while holding the relation extension lock
     181             :  * we don't try to acquire a heavyweight lock on any other object.  This
     182             :  * restriction implies that the relation extension lock won't ever participate
     183             :  * in the deadlock cycle because we can never wait for any other heavyweight
     184             :  * lock after acquiring this lock.
     185             :  *
     186             :  * Such a restriction is okay for relation extension locks as unlike other
     187             :  * heavyweight locks these are not held till the transaction end.  These are
     188             :  * taken for a short duration to extend a particular relation and then
     189             :  * released.
     190             :  */
     191             : static bool IsRelationExtensionLockHeld PG_USED_FOR_ASSERTS_ONLY = false;
     192             : 
     193             : /*
     194             :  * Number of fast-path locks per backend - size of the arrays in PGPROC.
     195             :  * This is set only once during start, before initializing shared memory,
     196             :  * and remains constant after that.
     197             :  *
     198             :  * We set the limit based on max_locks_per_transaction GUC, because that's
     199             :  * the best information about expected number of locks per backend we have.
     200             :  * See InitializeFastPathLocks() for details.
     201             :  */
     202             : int         FastPathLockGroupsPerBackend = 0;
     203             : 
     204             : /*
     205             :  * Macros to calculate the fast-path group and index for a relation.
     206             :  *
     207             :  * The formula is a simple hash function, designed to spread the OIDs a bit,
     208             :  * so that even contiguous values end up in different groups. In most cases
     209             :  * there will be gaps anyway, but the multiplication should help a bit.
     210             :  *
     211             :  * The selected constant (49157) is a prime not too close to 2^k, and it's
     212             :  * small enough to not cause overflows (in 64-bit).
     213             :  */
     214             : #define FAST_PATH_REL_GROUP(rel) \
     215             :     (((uint64) (rel) * 49157) % FastPathLockGroupsPerBackend)
     216             : 
     217             : /*
     218             :  * Given the group/slot indexes, calculate the slot index in the whole array
     219             :  * of fast-path lock slots.
     220             :  */
     221             : #define FAST_PATH_SLOT(group, index) \
     222             :     (AssertMacro((uint32) (group) < FastPathLockGroupsPerBackend), \
     223             :      AssertMacro((uint32) (index) < FP_LOCK_SLOTS_PER_GROUP), \
     224             :      ((group) * FP_LOCK_SLOTS_PER_GROUP + (index)))
     225             : 
     226             : /*
     227             :  * Given a slot index (into the whole per-backend array), calculated using
     228             :  * the FAST_PATH_SLOT macro, split it into group and index (in the group).
     229             :  */
     230             : #define FAST_PATH_GROUP(index)  \
     231             :     (AssertMacro((uint32) (index) < FastPathLockSlotsPerBackend()), \
     232             :      ((index) / FP_LOCK_SLOTS_PER_GROUP))
     233             : #define FAST_PATH_INDEX(index)  \
     234             :     (AssertMacro((uint32) (index) < FastPathLockSlotsPerBackend()), \
     235             :      ((index) % FP_LOCK_SLOTS_PER_GROUP))
     236             : 
     237             : /* Macros for manipulating proc->fpLockBits */
     238             : #define FAST_PATH_BITS_PER_SLOT         3
     239             : #define FAST_PATH_LOCKNUMBER_OFFSET     1
     240             : #define FAST_PATH_MASK                  ((1 << FAST_PATH_BITS_PER_SLOT) - 1)
     241             : #define FAST_PATH_BITS(proc, n)         (proc)->fpLockBits[FAST_PATH_GROUP(n)]
     242             : #define FAST_PATH_GET_BITS(proc, n) \
     243             :     ((FAST_PATH_BITS(proc, n) >> (FAST_PATH_BITS_PER_SLOT * FAST_PATH_INDEX(n))) & FAST_PATH_MASK)
     244             : #define FAST_PATH_BIT_POSITION(n, l) \
     245             :     (AssertMacro((l) >= FAST_PATH_LOCKNUMBER_OFFSET), \
     246             :      AssertMacro((l) < FAST_PATH_BITS_PER_SLOT+FAST_PATH_LOCKNUMBER_OFFSET), \
     247             :      AssertMacro((n) < FastPathLockSlotsPerBackend()), \
     248             :      ((l) - FAST_PATH_LOCKNUMBER_OFFSET + FAST_PATH_BITS_PER_SLOT * (FAST_PATH_INDEX(n))))
     249             : #define FAST_PATH_SET_LOCKMODE(proc, n, l) \
     250             :      FAST_PATH_BITS(proc, n) |= UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, l)
     251             : #define FAST_PATH_CLEAR_LOCKMODE(proc, n, l) \
     252             :      FAST_PATH_BITS(proc, n) &= ~(UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, l))
     253             : #define FAST_PATH_CHECK_LOCKMODE(proc, n, l) \
     254             :      (FAST_PATH_BITS(proc, n) & (UINT64CONST(1) << FAST_PATH_BIT_POSITION(n, l)))
     255             : 
     256             : /*
     257             :  * The fast-path lock mechanism is concerned only with relation locks on
     258             :  * unshared relations by backends bound to a database.  The fast-path
     259             :  * mechanism exists mostly to accelerate acquisition and release of locks
     260             :  * that rarely conflict.  Because ShareUpdateExclusiveLock is
     261             :  * self-conflicting, it can't use the fast-path mechanism; but it also does
     262             :  * not conflict with any of the locks that do, so we can ignore it completely.
     263             :  */
     264             : #define EligibleForRelationFastPath(locktag, mode) \
     265             :     ((locktag)->locktag_lockmethodid == DEFAULT_LOCKMETHOD && \
     266             :     (locktag)->locktag_type == LOCKTAG_RELATION && \
     267             :     (locktag)->locktag_field1 == MyDatabaseId && \
     268             :     MyDatabaseId != InvalidOid && \
     269             :     (mode) < ShareUpdateExclusiveLock)
     270             : #define ConflictsWithRelationFastPath(locktag, mode) \
     271             :     ((locktag)->locktag_lockmethodid == DEFAULT_LOCKMETHOD && \
     272             :     (locktag)->locktag_type == LOCKTAG_RELATION && \
     273             :     (locktag)->locktag_field1 != InvalidOid && \
     274             :     (mode) > ShareUpdateExclusiveLock)
     275             : 
     276             : static bool FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode);
     277             : static bool FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode);
     278             : static bool FastPathTransferRelationLocks(LockMethod lockMethodTable,
     279             :                                           const LOCKTAG *locktag, uint32 hashcode);
     280             : static PROCLOCK *FastPathGetRelationLockEntry(LOCALLOCK *locallock);
     281             : 
     282             : /*
     283             :  * To make the fast-path lock mechanism work, we must have some way of
     284             :  * preventing the use of the fast-path when a conflicting lock might be present.
     285             :  * We partition* the locktag space into FAST_PATH_STRONG_LOCK_HASH_PARTITIONS,
     286             :  * and maintain an integer count of the number of "strong" lockers
     287             :  * in each partition.  When any "strong" lockers are present (which is
     288             :  * hopefully not very often), the fast-path mechanism can't be used, and we
     289             :  * must fall back to the slower method of pushing matching locks directly
     290             :  * into the main lock tables.
     291             :  *
     292             :  * The deadlock detector does not know anything about the fast path mechanism,
     293             :  * so any locks that might be involved in a deadlock must be transferred from
     294             :  * the fast-path queues to the main lock table.
     295             :  */
     296             : 
     297             : #define FAST_PATH_STRONG_LOCK_HASH_BITS         10
     298             : #define FAST_PATH_STRONG_LOCK_HASH_PARTITIONS \
     299             :     (1 << FAST_PATH_STRONG_LOCK_HASH_BITS)
     300             : #define FastPathStrongLockHashPartition(hashcode) \
     301             :     ((hashcode) % FAST_PATH_STRONG_LOCK_HASH_PARTITIONS)
     302             : 
     303             : typedef struct
     304             : {
     305             :     slock_t     mutex;
     306             :     uint32      count[FAST_PATH_STRONG_LOCK_HASH_PARTITIONS];
     307             : } FastPathStrongRelationLockData;
     308             : 
     309             : static volatile FastPathStrongRelationLockData *FastPathStrongRelationLocks;
     310             : 
     311             : 
     312             : /*
     313             :  * Pointers to hash tables containing lock state
     314             :  *
     315             :  * The LockMethodLockHash and LockMethodProcLockHash hash tables are in
     316             :  * shared memory; LockMethodLocalHash is local to each backend.
     317             :  */
     318             : static HTAB *LockMethodLockHash;
     319             : static HTAB *LockMethodProcLockHash;
     320             : static HTAB *LockMethodLocalHash;
     321             : 
     322             : 
     323             : /* private state for error cleanup */
     324             : static LOCALLOCK *StrongLockInProgress;
     325             : static LOCALLOCK *awaitedLock;
     326             : static ResourceOwner awaitedOwner;
     327             : 
     328             : 
     329             : #ifdef LOCK_DEBUG
     330             : 
     331             : /*------
     332             :  * The following configuration options are available for lock debugging:
     333             :  *
     334             :  *     TRACE_LOCKS      -- give a bunch of output what's going on in this file
     335             :  *     TRACE_USERLOCKS  -- same but for user locks
     336             :  *     TRACE_LOCK_OIDMIN-- do not trace locks for tables below this oid
     337             :  *                         (use to avoid output on system tables)
     338             :  *     TRACE_LOCK_TABLE -- trace locks on this table (oid) unconditionally
     339             :  *     DEBUG_DEADLOCKS  -- currently dumps locks at untimely occasions ;)
     340             :  *
     341             :  * Furthermore, but in storage/lmgr/lwlock.c:
     342             :  *     TRACE_LWLOCKS    -- trace lightweight locks (pretty useless)
     343             :  *
     344             :  * Define LOCK_DEBUG at compile time to get all these enabled.
     345             :  * --------
     346             :  */
     347             : 
     348             : int         Trace_lock_oidmin = FirstNormalObjectId;
     349             : bool        Trace_locks = false;
     350             : bool        Trace_userlocks = false;
     351             : int         Trace_lock_table = 0;
     352             : bool        Debug_deadlocks = false;
     353             : 
     354             : 
     355             : inline static bool
     356             : LOCK_DEBUG_ENABLED(const LOCKTAG *tag)
     357             : {
     358             :     return
     359             :         (*(LockMethods[tag->locktag_lockmethodid]->trace_flag) &&
     360             :          ((Oid) tag->locktag_field2 >= (Oid) Trace_lock_oidmin))
     361             :         || (Trace_lock_table &&
     362             :             (tag->locktag_field2 == Trace_lock_table));
     363             : }
     364             : 
     365             : 
     366             : inline static void
     367             : LOCK_PRINT(const char *where, const LOCK *lock, LOCKMODE type)
     368             : {
     369             :     if (LOCK_DEBUG_ENABLED(&lock->tag))
     370             :         elog(LOG,
     371             :              "%s: lock(%p) id(%u,%u,%u,%u,%u,%u) grantMask(%x) "
     372             :              "req(%d,%d,%d,%d,%d,%d,%d)=%d "
     373             :              "grant(%d,%d,%d,%d,%d,%d,%d)=%d wait(%d) type(%s)",
     374             :              where, lock,
     375             :              lock->tag.locktag_field1, lock->tag.locktag_field2,
     376             :              lock->tag.locktag_field3, lock->tag.locktag_field4,
     377             :              lock->tag.locktag_type, lock->tag.locktag_lockmethodid,
     378             :              lock->grantMask,
     379             :              lock->requested[1], lock->requested[2], lock->requested[3],
     380             :              lock->requested[4], lock->requested[5], lock->requested[6],
     381             :              lock->requested[7], lock->nRequested,
     382             :              lock->granted[1], lock->granted[2], lock->granted[3],
     383             :              lock->granted[4], lock->granted[5], lock->granted[6],
     384             :              lock->granted[7], lock->nGranted,
     385             :              dclist_count(&lock->waitProcs),
     386             :              LockMethods[LOCK_LOCKMETHOD(*lock)]->lockModeNames[type]);
     387             : }
     388             : 
     389             : 
     390             : inline static void
     391             : PROCLOCK_PRINT(const char *where, const PROCLOCK *proclockP)
     392             : {
     393             :     if (LOCK_DEBUG_ENABLED(&proclockP->tag.myLock->tag))
     394             :         elog(LOG,
     395             :              "%s: proclock(%p) lock(%p) method(%u) proc(%p) hold(%x)",
     396             :              where, proclockP, proclockP->tag.myLock,
     397             :              PROCLOCK_LOCKMETHOD(*(proclockP)),
     398             :              proclockP->tag.myProc, (int) proclockP->holdMask);
     399             : }
     400             : #else                           /* not LOCK_DEBUG */
     401             : 
     402             : #define LOCK_PRINT(where, lock, type)  ((void) 0)
     403             : #define PROCLOCK_PRINT(where, proclockP)  ((void) 0)
     404             : #endif                          /* not LOCK_DEBUG */
     405             : 
     406             : 
     407             : static uint32 proclock_hash(const void *key, Size keysize);
     408             : static void RemoveLocalLock(LOCALLOCK *locallock);
     409             : static PROCLOCK *SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc,
     410             :                                   const LOCKTAG *locktag, uint32 hashcode, LOCKMODE lockmode);
     411             : static void GrantLockLocal(LOCALLOCK *locallock, ResourceOwner owner);
     412             : static void BeginStrongLockAcquire(LOCALLOCK *locallock, uint32 fasthashcode);
     413             : static void FinishStrongLockAcquire(void);
     414             : static ProcWaitStatus WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner);
     415             : static void ReleaseLockIfHeld(LOCALLOCK *locallock, bool sessionLock);
     416             : static void LockReassignOwner(LOCALLOCK *locallock, ResourceOwner parent);
     417             : static bool UnGrantLock(LOCK *lock, LOCKMODE lockmode,
     418             :                         PROCLOCK *proclock, LockMethod lockMethodTable);
     419             : static void CleanUpLock(LOCK *lock, PROCLOCK *proclock,
     420             :                         LockMethod lockMethodTable, uint32 hashcode,
     421             :                         bool wakeupNeeded);
     422             : static void LockRefindAndRelease(LockMethod lockMethodTable, PGPROC *proc,
     423             :                                  LOCKTAG *locktag, LOCKMODE lockmode,
     424             :                                  bool decrement_strong_lock_count);
     425             : static void GetSingleProcBlockerStatusData(PGPROC *blocked_proc,
     426             :                                            BlockedProcsData *data);
     427             : 
     428             : 
     429             : /*
     430             :  * Initialize the lock manager's shmem data structures.
     431             :  *
     432             :  * This is called from CreateSharedMemoryAndSemaphores(), which see for more
     433             :  * comments.  In the normal postmaster case, the shared hash tables are
     434             :  * created here, and backends inherit pointers to them via fork().  In the
     435             :  * EXEC_BACKEND case, each backend re-executes this code to obtain pointers to
     436             :  * the already existing shared hash tables.  In either case, each backend must
     437             :  * also call InitLockManagerAccess() to create the locallock hash table.
     438             :  */
     439             : void
     440        2032 : LockManagerShmemInit(void)
     441             : {
     442             :     HASHCTL     info;
     443             :     long        init_table_size,
     444             :                 max_table_size;
     445             :     bool        found;
     446             : 
     447             :     /*
     448             :      * Compute init/max size to request for lock hashtables.  Note these
     449             :      * calculations must agree with LockManagerShmemSize!
     450             :      */
     451        2032 :     max_table_size = NLOCKENTS();
     452        2032 :     init_table_size = max_table_size / 2;
     453             : 
     454             :     /*
     455             :      * Allocate hash table for LOCK structs.  This stores per-locked-object
     456             :      * information.
     457             :      */
     458        2032 :     info.keysize = sizeof(LOCKTAG);
     459        2032 :     info.entrysize = sizeof(LOCK);
     460        2032 :     info.num_partitions = NUM_LOCK_PARTITIONS;
     461             : 
     462        2032 :     LockMethodLockHash = ShmemInitHash("LOCK hash",
     463             :                                        init_table_size,
     464             :                                        max_table_size,
     465             :                                        &info,
     466             :                                        HASH_ELEM | HASH_BLOBS | HASH_PARTITION);
     467             : 
     468             :     /* Assume an average of 2 holders per lock */
     469        2032 :     max_table_size *= 2;
     470        2032 :     init_table_size *= 2;
     471             : 
     472             :     /*
     473             :      * Allocate hash table for PROCLOCK structs.  This stores
     474             :      * per-lock-per-holder information.
     475             :      */
     476        2032 :     info.keysize = sizeof(PROCLOCKTAG);
     477        2032 :     info.entrysize = sizeof(PROCLOCK);
     478        2032 :     info.hash = proclock_hash;
     479        2032 :     info.num_partitions = NUM_LOCK_PARTITIONS;
     480             : 
     481        2032 :     LockMethodProcLockHash = ShmemInitHash("PROCLOCK hash",
     482             :                                            init_table_size,
     483             :                                            max_table_size,
     484             :                                            &info,
     485             :                                            HASH_ELEM | HASH_FUNCTION | HASH_PARTITION);
     486             : 
     487             :     /*
     488             :      * Allocate fast-path structures.
     489             :      */
     490        2032 :     FastPathStrongRelationLocks =
     491        2032 :         ShmemInitStruct("Fast Path Strong Relation Lock Data",
     492             :                         sizeof(FastPathStrongRelationLockData), &found);
     493        2032 :     if (!found)
     494        2032 :         SpinLockInit(&FastPathStrongRelationLocks->mutex);
     495        2032 : }
     496             : 
     497             : /*
     498             :  * Initialize the lock manager's backend-private data structures.
     499             :  */
     500             : void
     501       42318 : InitLockManagerAccess(void)
     502             : {
     503             :     /*
     504             :      * Allocate non-shared hash table for LOCALLOCK structs.  This stores lock
     505             :      * counts and resource owner information.
     506             :      */
     507             :     HASHCTL     info;
     508             : 
     509       42318 :     info.keysize = sizeof(LOCALLOCKTAG);
     510       42318 :     info.entrysize = sizeof(LOCALLOCK);
     511             : 
     512       42318 :     LockMethodLocalHash = hash_create("LOCALLOCK hash",
     513             :                                       16,
     514             :                                       &info,
     515             :                                       HASH_ELEM | HASH_BLOBS);
     516       42318 : }
     517             : 
     518             : 
     519             : /*
     520             :  * Fetch the lock method table associated with a given lock
     521             :  */
     522             : LockMethod
     523         186 : GetLocksMethodTable(const LOCK *lock)
     524             : {
     525         186 :     LOCKMETHODID lockmethodid = LOCK_LOCKMETHOD(*lock);
     526             : 
     527             :     Assert(0 < lockmethodid && lockmethodid < lengthof(LockMethods));
     528         186 :     return LockMethods[lockmethodid];
     529             : }
     530             : 
     531             : /*
     532             :  * Fetch the lock method table associated with a given locktag
     533             :  */
     534             : LockMethod
     535        2254 : GetLockTagsMethodTable(const LOCKTAG *locktag)
     536             : {
     537        2254 :     LOCKMETHODID lockmethodid = (LOCKMETHODID) locktag->locktag_lockmethodid;
     538             : 
     539             :     Assert(0 < lockmethodid && lockmethodid < lengthof(LockMethods));
     540        2254 :     return LockMethods[lockmethodid];
     541             : }
     542             : 
     543             : 
     544             : /*
     545             :  * Compute the hash code associated with a LOCKTAG.
     546             :  *
     547             :  * To avoid unnecessary recomputations of the hash code, we try to do this
     548             :  * just once per function, and then pass it around as needed.  Aside from
     549             :  * passing the hashcode to hash_search_with_hash_value(), we can extract
     550             :  * the lock partition number from the hashcode.
     551             :  */
     552             : uint32
     553    34884914 : LockTagHashCode(const LOCKTAG *locktag)
     554             : {
     555    34884914 :     return get_hash_value(LockMethodLockHash, locktag);
     556             : }
     557             : 
     558             : /*
     559             :  * Compute the hash code associated with a PROCLOCKTAG.
     560             :  *
     561             :  * Because we want to use just one set of partition locks for both the
     562             :  * LOCK and PROCLOCK hash tables, we have to make sure that PROCLOCKs
     563             :  * fall into the same partition number as their associated LOCKs.
     564             :  * dynahash.c expects the partition number to be the low-order bits of
     565             :  * the hash code, and therefore a PROCLOCKTAG's hash code must have the
     566             :  * same low-order bits as the associated LOCKTAG's hash code.  We achieve
     567             :  * this with this specialized hash function.
     568             :  */
     569             : static uint32
     570        1356 : proclock_hash(const void *key, Size keysize)
     571             : {
     572        1356 :     const PROCLOCKTAG *proclocktag = (const PROCLOCKTAG *) key;
     573             :     uint32      lockhash;
     574             :     Datum       procptr;
     575             : 
     576             :     Assert(keysize == sizeof(PROCLOCKTAG));
     577             : 
     578             :     /* Look into the associated LOCK object, and compute its hash code */
     579        1356 :     lockhash = LockTagHashCode(&proclocktag->myLock->tag);
     580             : 
     581             :     /*
     582             :      * To make the hash code also depend on the PGPROC, we xor the proc
     583             :      * struct's address into the hash code, left-shifted so that the
     584             :      * partition-number bits don't change.  Since this is only a hash, we
     585             :      * don't care if we lose high-order bits of the address; use an
     586             :      * intermediate variable to suppress cast-pointer-to-int warnings.
     587             :      */
     588        1356 :     procptr = PointerGetDatum(proclocktag->myProc);
     589        1356 :     lockhash ^= ((uint32) procptr) << LOG2_NUM_LOCK_PARTITIONS;
     590             : 
     591        1356 :     return lockhash;
     592             : }
     593             : 
     594             : /*
     595             :  * Compute the hash code associated with a PROCLOCKTAG, given the hashcode
     596             :  * for its underlying LOCK.
     597             :  *
     598             :  * We use this just to avoid redundant calls of LockTagHashCode().
     599             :  */
     600             : static inline uint32
     601     8316796 : ProcLockHashCode(const PROCLOCKTAG *proclocktag, uint32 hashcode)
     602             : {
     603     8316796 :     uint32      lockhash = hashcode;
     604             :     Datum       procptr;
     605             : 
     606             :     /*
     607             :      * This must match proclock_hash()!
     608             :      */
     609     8316796 :     procptr = PointerGetDatum(proclocktag->myProc);
     610     8316796 :     lockhash ^= ((uint32) procptr) << LOG2_NUM_LOCK_PARTITIONS;
     611             : 
     612     8316796 :     return lockhash;
     613             : }
     614             : 
     615             : /*
     616             :  * Given two lock modes, return whether they would conflict.
     617             :  */
     618             : bool
     619         488 : DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2)
     620             : {
     621         488 :     LockMethod  lockMethodTable = LockMethods[DEFAULT_LOCKMETHOD];
     622             : 
     623         488 :     if (lockMethodTable->conflictTab[mode1] & LOCKBIT_ON(mode2))
     624         284 :         return true;
     625             : 
     626         204 :     return false;
     627             : }
     628             : 
     629             : /*
     630             :  * LockHeldByMe -- test whether lock 'locktag' is held by the current
     631             :  *      transaction
     632             :  *
     633             :  * Returns true if current transaction holds a lock on 'tag' of mode
     634             :  * 'lockmode'.  If 'orstronger' is true, a stronger lockmode is also OK.
     635             :  * ("Stronger" is defined as "numerically higher", which is a bit
     636             :  * semantically dubious but is OK for the purposes we use this for.)
     637             :  */
     638             : bool
     639           0 : LockHeldByMe(const LOCKTAG *locktag,
     640             :              LOCKMODE lockmode, bool orstronger)
     641             : {
     642             :     LOCALLOCKTAG localtag;
     643             :     LOCALLOCK  *locallock;
     644             : 
     645             :     /*
     646             :      * See if there is a LOCALLOCK entry for this lock and lockmode
     647             :      */
     648           0 :     MemSet(&localtag, 0, sizeof(localtag)); /* must clear padding */
     649           0 :     localtag.lock = *locktag;
     650           0 :     localtag.mode = lockmode;
     651             : 
     652           0 :     locallock = (LOCALLOCK *) hash_search(LockMethodLocalHash,
     653             :                                           &localtag,
     654             :                                           HASH_FIND, NULL);
     655             : 
     656           0 :     if (locallock && locallock->nLocks > 0)
     657           0 :         return true;
     658             : 
     659           0 :     if (orstronger)
     660             :     {
     661             :         LOCKMODE    slockmode;
     662             : 
     663           0 :         for (slockmode = lockmode + 1;
     664             :              slockmode <= MaxLockMode;
     665           0 :              slockmode++)
     666             :         {
     667           0 :             if (LockHeldByMe(locktag, slockmode, false))
     668           0 :                 return true;
     669             :         }
     670             :     }
     671             : 
     672           0 :     return false;
     673             : }
     674             : 
     675             : #ifdef USE_ASSERT_CHECKING
     676             : /*
     677             :  * GetLockMethodLocalHash -- return the hash of local locks, for modules that
     678             :  *      evaluate assertions based on all locks held.
     679             :  */
     680             : HTAB *
     681             : GetLockMethodLocalHash(void)
     682             : {
     683             :     return LockMethodLocalHash;
     684             : }
     685             : #endif
     686             : 
     687             : /*
     688             :  * LockHasWaiters -- look up 'locktag' and check if releasing this
     689             :  *      lock would wake up other processes waiting for it.
     690             :  */
     691             : bool
     692           0 : LockHasWaiters(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
     693             : {
     694           0 :     LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
     695             :     LockMethod  lockMethodTable;
     696             :     LOCALLOCKTAG localtag;
     697             :     LOCALLOCK  *locallock;
     698             :     LOCK       *lock;
     699             :     PROCLOCK   *proclock;
     700             :     LWLock     *partitionLock;
     701           0 :     bool        hasWaiters = false;
     702             : 
     703           0 :     if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
     704           0 :         elog(ERROR, "unrecognized lock method: %d", lockmethodid);
     705           0 :     lockMethodTable = LockMethods[lockmethodid];
     706           0 :     if (lockmode <= 0 || lockmode > lockMethodTable->numLockModes)
     707           0 :         elog(ERROR, "unrecognized lock mode: %d", lockmode);
     708             : 
     709             : #ifdef LOCK_DEBUG
     710             :     if (LOCK_DEBUG_ENABLED(locktag))
     711             :         elog(LOG, "LockHasWaiters: lock [%u,%u] %s",
     712             :              locktag->locktag_field1, locktag->locktag_field2,
     713             :              lockMethodTable->lockModeNames[lockmode]);
     714             : #endif
     715             : 
     716             :     /*
     717             :      * Find the LOCALLOCK entry for this lock and lockmode
     718             :      */
     719           0 :     MemSet(&localtag, 0, sizeof(localtag)); /* must clear padding */
     720           0 :     localtag.lock = *locktag;
     721           0 :     localtag.mode = lockmode;
     722             : 
     723           0 :     locallock = (LOCALLOCK *) hash_search(LockMethodLocalHash,
     724             :                                           &localtag,
     725             :                                           HASH_FIND, NULL);
     726             : 
     727             :     /*
     728             :      * let the caller print its own error message, too. Do not ereport(ERROR).
     729             :      */
     730           0 :     if (!locallock || locallock->nLocks <= 0)
     731             :     {
     732           0 :         elog(WARNING, "you don't own a lock of type %s",
     733             :              lockMethodTable->lockModeNames[lockmode]);
     734           0 :         return false;
     735             :     }
     736             : 
     737             :     /*
     738             :      * Check the shared lock table.
     739             :      */
     740           0 :     partitionLock = LockHashPartitionLock(locallock->hashcode);
     741             : 
     742           0 :     LWLockAcquire(partitionLock, LW_SHARED);
     743             : 
     744             :     /*
     745             :      * We don't need to re-find the lock or proclock, since we kept their
     746             :      * addresses in the locallock table, and they couldn't have been removed
     747             :      * while we were holding a lock on them.
     748             :      */
     749           0 :     lock = locallock->lock;
     750             :     LOCK_PRINT("LockHasWaiters: found", lock, lockmode);
     751           0 :     proclock = locallock->proclock;
     752             :     PROCLOCK_PRINT("LockHasWaiters: found", proclock);
     753             : 
     754             :     /*
     755             :      * Double-check that we are actually holding a lock of the type we want to
     756             :      * release.
     757             :      */
     758           0 :     if (!(proclock->holdMask & LOCKBIT_ON(lockmode)))
     759             :     {
     760             :         PROCLOCK_PRINT("LockHasWaiters: WRONGTYPE", proclock);
     761           0 :         LWLockRelease(partitionLock);
     762           0 :         elog(WARNING, "you don't own a lock of type %s",
     763             :              lockMethodTable->lockModeNames[lockmode]);
     764           0 :         RemoveLocalLock(locallock);
     765           0 :         return false;
     766             :     }
     767             : 
     768             :     /*
     769             :      * Do the checking.
     770             :      */
     771           0 :     if ((lockMethodTable->conflictTab[lockmode] & lock->waitMask) != 0)
     772           0 :         hasWaiters = true;
     773             : 
     774           0 :     LWLockRelease(partitionLock);
     775             : 
     776           0 :     return hasWaiters;
     777             : }
     778             : 
     779             : /*
     780             :  * LockAcquire -- Check for lock conflicts, sleep if conflict found,
     781             :  *      set lock if/when no conflicts.
     782             :  *
     783             :  * Inputs:
     784             :  *  locktag: unique identifier for the lockable object
     785             :  *  lockmode: lock mode to acquire
     786             :  *  sessionLock: if true, acquire lock for session not current transaction
     787             :  *  dontWait: if true, don't wait to acquire lock
     788             :  *
     789             :  * Returns one of:
     790             :  *      LOCKACQUIRE_NOT_AVAIL       lock not available, and dontWait=true
     791             :  *      LOCKACQUIRE_OK              lock successfully acquired
     792             :  *      LOCKACQUIRE_ALREADY_HELD    incremented count for lock already held
     793             :  *      LOCKACQUIRE_ALREADY_CLEAR   incremented count for lock already clear
     794             :  *
     795             :  * In the normal case where dontWait=false and the caller doesn't need to
     796             :  * distinguish a freshly acquired lock from one already taken earlier in
     797             :  * this same transaction, there is no need to examine the return value.
     798             :  *
     799             :  * Side Effects: The lock is acquired and recorded in lock tables.
     800             :  *
     801             :  * NOTE: if we wait for the lock, there is no way to abort the wait
     802             :  * short of aborting the transaction.
     803             :  */
     804             : LockAcquireResult
     805     1501062 : LockAcquire(const LOCKTAG *locktag,
     806             :             LOCKMODE lockmode,
     807             :             bool sessionLock,
     808             :             bool dontWait)
     809             : {
     810     1501062 :     return LockAcquireExtended(locktag, lockmode, sessionLock, dontWait,
     811             :                                true, NULL, false);
     812             : }
     813             : 
     814             : /*
     815             :  * LockAcquireExtended - allows us to specify additional options
     816             :  *
     817             :  * reportMemoryError specifies whether a lock request that fills the lock
     818             :  * table should generate an ERROR or not.  Passing "false" allows the caller
     819             :  * to attempt to recover from lock-table-full situations, perhaps by forcibly
     820             :  * canceling other lock holders and then retrying.  Note, however, that the
     821             :  * return code for that is LOCKACQUIRE_NOT_AVAIL, so that it's unsafe to use
     822             :  * in combination with dontWait = true, as the cause of failure couldn't be
     823             :  * distinguished.
     824             :  *
     825             :  * If locallockp isn't NULL, *locallockp receives a pointer to the LOCALLOCK
     826             :  * table entry if a lock is successfully acquired, or NULL if not.
     827             :  *
     828             :  * logLockFailure indicates whether to log details when a lock acquisition
     829             :  * fails with dontWait = true.
     830             :  */
     831             : LockAcquireResult
     832    37959594 : LockAcquireExtended(const LOCKTAG *locktag,
     833             :                     LOCKMODE lockmode,
     834             :                     bool sessionLock,
     835             :                     bool dontWait,
     836             :                     bool reportMemoryError,
     837             :                     LOCALLOCK **locallockp,
     838             :                     bool logLockFailure)
     839             : {
     840    37959594 :     LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
     841             :     LockMethod  lockMethodTable;
     842             :     LOCALLOCKTAG localtag;
     843             :     LOCALLOCK  *locallock;
     844             :     LOCK       *lock;
     845             :     PROCLOCK   *proclock;
     846             :     bool        found;
     847             :     ResourceOwner owner;
     848             :     uint32      hashcode;
     849             :     LWLock     *partitionLock;
     850             :     bool        found_conflict;
     851             :     ProcWaitStatus waitResult;
     852    37959594 :     bool        log_lock = false;
     853             : 
     854    37959594 :     if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
     855           0 :         elog(ERROR, "unrecognized lock method: %d", lockmethodid);
     856    37959594 :     lockMethodTable = LockMethods[lockmethodid];
     857    37959594 :     if (lockmode <= 0 || lockmode > lockMethodTable->numLockModes)
     858           0 :         elog(ERROR, "unrecognized lock mode: %d", lockmode);
     859             : 
     860    37959594 :     if (RecoveryInProgress() && !InRecovery &&
     861      562706 :         (locktag->locktag_type == LOCKTAG_OBJECT ||
     862      562706 :          locktag->locktag_type == LOCKTAG_RELATION) &&
     863             :         lockmode > RowExclusiveLock)
     864           0 :         ereport(ERROR,
     865             :                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
     866             :                  errmsg("cannot acquire lock mode %s on database objects while recovery is in progress",
     867             :                         lockMethodTable->lockModeNames[lockmode]),
     868             :                  errhint("Only RowExclusiveLock or less can be acquired on database objects during recovery.")));
     869             : 
     870             : #ifdef LOCK_DEBUG
     871             :     if (LOCK_DEBUG_ENABLED(locktag))
     872             :         elog(LOG, "LockAcquire: lock [%u,%u] %s",
     873             :              locktag->locktag_field1, locktag->locktag_field2,
     874             :              lockMethodTable->lockModeNames[lockmode]);
     875             : #endif
     876             : 
     877             :     /* Identify owner for lock */
     878    37959594 :     if (sessionLock)
     879      173718 :         owner = NULL;
     880             :     else
     881    37785876 :         owner = CurrentResourceOwner;
     882             : 
     883             :     /*
     884             :      * Find or create a LOCALLOCK entry for this lock and lockmode
     885             :      */
     886    37959594 :     MemSet(&localtag, 0, sizeof(localtag)); /* must clear padding */
     887    37959594 :     localtag.lock = *locktag;
     888    37959594 :     localtag.mode = lockmode;
     889             : 
     890    37959594 :     locallock = (LOCALLOCK *) hash_search(LockMethodLocalHash,
     891             :                                           &localtag,
     892             :                                           HASH_ENTER, &found);
     893             : 
     894             :     /*
     895             :      * if it's a new locallock object, initialize it
     896             :      */
     897    37959594 :     if (!found)
     898             :     {
     899    33799406 :         locallock->lock = NULL;
     900    33799406 :         locallock->proclock = NULL;
     901    33799406 :         locallock->hashcode = LockTagHashCode(&(localtag.lock));
     902    33799406 :         locallock->nLocks = 0;
     903    33799406 :         locallock->holdsStrongLockCount = false;
     904    33799406 :         locallock->lockCleared = false;
     905    33799406 :         locallock->numLockOwners = 0;
     906    33799406 :         locallock->maxLockOwners = 8;
     907    33799406 :         locallock->lockOwners = NULL;    /* in case next line fails */
     908    33799406 :         locallock->lockOwners = (LOCALLOCKOWNER *)
     909    33799406 :             MemoryContextAlloc(TopMemoryContext,
     910    33799406 :                                locallock->maxLockOwners * sizeof(LOCALLOCKOWNER));
     911             :     }
     912             :     else
     913             :     {
     914             :         /* Make sure there will be room to remember the lock */
     915     4160188 :         if (locallock->numLockOwners >= locallock->maxLockOwners)
     916             :         {
     917          38 :             int         newsize = locallock->maxLockOwners * 2;
     918             : 
     919          38 :             locallock->lockOwners = (LOCALLOCKOWNER *)
     920          38 :                 repalloc(locallock->lockOwners,
     921             :                          newsize * sizeof(LOCALLOCKOWNER));
     922          38 :             locallock->maxLockOwners = newsize;
     923             :         }
     924             :     }
     925    37959594 :     hashcode = locallock->hashcode;
     926             : 
     927    37959594 :     if (locallockp)
     928    36458360 :         *locallockp = locallock;
     929             : 
     930             :     /*
     931             :      * If we already hold the lock, we can just increase the count locally.
     932             :      *
     933             :      * If lockCleared is already set, caller need not worry about absorbing
     934             :      * sinval messages related to the lock's object.
     935             :      */
     936    37959594 :     if (locallock->nLocks > 0)
     937             :     {
     938     4160188 :         GrantLockLocal(locallock, owner);
     939     4160188 :         if (locallock->lockCleared)
     940     4011248 :             return LOCKACQUIRE_ALREADY_CLEAR;
     941             :         else
     942      148940 :             return LOCKACQUIRE_ALREADY_HELD;
     943             :     }
     944             : 
     945             :     /*
     946             :      * We don't acquire any other heavyweight lock while holding the relation
     947             :      * extension lock.  We do allow to acquire the same relation extension
     948             :      * lock more than once but that case won't reach here.
     949             :      */
     950             :     Assert(!IsRelationExtensionLockHeld);
     951             : 
     952             :     /*
     953             :      * Prepare to emit a WAL record if acquisition of this lock needs to be
     954             :      * replayed in a standby server.
     955             :      *
     956             :      * Here we prepare to log; after lock is acquired we'll issue log record.
     957             :      * This arrangement simplifies error recovery in case the preparation step
     958             :      * fails.
     959             :      *
     960             :      * Only AccessExclusiveLocks can conflict with lock types that read-only
     961             :      * transactions can acquire in a standby server. Make sure this definition
     962             :      * matches the one in GetRunningTransactionLocks().
     963             :      */
     964    33799406 :     if (lockmode >= AccessExclusiveLock &&
     965      451434 :         locktag->locktag_type == LOCKTAG_RELATION &&
     966      303378 :         !RecoveryInProgress() &&
     967      255688 :         XLogStandbyInfoActive())
     968             :     {
     969      195226 :         LogAccessExclusiveLockPrepare();
     970      195226 :         log_lock = true;
     971             :     }
     972             : 
     973             :     /*
     974             :      * Attempt to take lock via fast path, if eligible.  But if we remember
     975             :      * having filled up the fast path array, we don't attempt to make any
     976             :      * further use of it until we release some locks.  It's possible that some
     977             :      * other backend has transferred some of those locks to the shared hash
     978             :      * table, leaving space free, but it's not worth acquiring the LWLock just
     979             :      * to check.  It's also possible that we're acquiring a second or third
     980             :      * lock type on a relation we have already locked using the fast-path, but
     981             :      * for now we don't worry about that case either.
     982             :      */
     983    33799406 :     if (EligibleForRelationFastPath(locktag, lockmode) &&
     984    30288928 :         FastPathLocalUseCounts[FAST_PATH_REL_GROUP(locktag->locktag_field2)] < FP_LOCK_SLOTS_PER_GROUP)
     985             :     {
     986    29937602 :         uint32      fasthashcode = FastPathStrongLockHashPartition(hashcode);
     987             :         bool        acquired;
     988             : 
     989             :         /*
     990             :          * LWLockAcquire acts as a memory sequencing point, so it's safe to
     991             :          * assume that any strong locker whose increment to
     992             :          * FastPathStrongRelationLocks->counts becomes visible after we test
     993             :          * it has yet to begin to transfer fast-path locks.
     994             :          */
     995    29937602 :         LWLockAcquire(&MyProc->fpInfoLock, LW_EXCLUSIVE);
     996    29937602 :         if (FastPathStrongRelationLocks->count[fasthashcode] != 0)
     997      487954 :             acquired = false;
     998             :         else
     999    29449648 :             acquired = FastPathGrantRelationLock(locktag->locktag_field2,
    1000             :                                                  lockmode);
    1001    29937602 :         LWLockRelease(&MyProc->fpInfoLock);
    1002    29937602 :         if (acquired)
    1003             :         {
    1004             :             /*
    1005             :              * The locallock might contain stale pointers to some old shared
    1006             :              * objects; we MUST reset these to null before considering the
    1007             :              * lock to be acquired via fast-path.
    1008             :              */
    1009    29449648 :             locallock->lock = NULL;
    1010    29449648 :             locallock->proclock = NULL;
    1011    29449648 :             GrantLockLocal(locallock, owner);
    1012    29449648 :             return LOCKACQUIRE_OK;
    1013             :         }
    1014             :     }
    1015             : 
    1016             :     /*
    1017             :      * If this lock could potentially have been taken via the fast-path by
    1018             :      * some other backend, we must (temporarily) disable further use of the
    1019             :      * fast-path for this lock tag, and migrate any locks already taken via
    1020             :      * this method to the main lock table.
    1021             :      */
    1022     4349758 :     if (ConflictsWithRelationFastPath(locktag, lockmode))
    1023             :     {
    1024      359710 :         uint32      fasthashcode = FastPathStrongLockHashPartition(hashcode);
    1025             : 
    1026      359710 :         BeginStrongLockAcquire(locallock, fasthashcode);
    1027      359710 :         if (!FastPathTransferRelationLocks(lockMethodTable, locktag,
    1028             :                                            hashcode))
    1029             :         {
    1030           0 :             AbortStrongLockAcquire();
    1031           0 :             if (locallock->nLocks == 0)
    1032           0 :                 RemoveLocalLock(locallock);
    1033           0 :             if (locallockp)
    1034           0 :                 *locallockp = NULL;
    1035           0 :             if (reportMemoryError)
    1036           0 :                 ereport(ERROR,
    1037             :                         (errcode(ERRCODE_OUT_OF_MEMORY),
    1038             :                          errmsg("out of shared memory"),
    1039             :                          errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
    1040             :             else
    1041           0 :                 return LOCKACQUIRE_NOT_AVAIL;
    1042             :         }
    1043             :     }
    1044             : 
    1045             :     /*
    1046             :      * We didn't find the lock in our LOCALLOCK table, and we didn't manage to
    1047             :      * take it via the fast-path, either, so we've got to mess with the shared
    1048             :      * lock table.
    1049             :      */
    1050     4349758 :     partitionLock = LockHashPartitionLock(hashcode);
    1051             : 
    1052     4349758 :     LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    1053             : 
    1054             :     /*
    1055             :      * Find or create lock and proclock entries with this tag
    1056             :      *
    1057             :      * Note: if the locallock object already existed, it might have a pointer
    1058             :      * to the lock already ... but we should not assume that that pointer is
    1059             :      * valid, since a lock object with zero hold and request counts can go
    1060             :      * away anytime.  So we have to use SetupLockInTable() to recompute the
    1061             :      * lock and proclock pointers, even if they're already set.
    1062             :      */
    1063     4349758 :     proclock = SetupLockInTable(lockMethodTable, MyProc, locktag,
    1064             :                                 hashcode, lockmode);
    1065     4349758 :     if (!proclock)
    1066             :     {
    1067           0 :         AbortStrongLockAcquire();
    1068           0 :         LWLockRelease(partitionLock);
    1069           0 :         if (locallock->nLocks == 0)
    1070           0 :             RemoveLocalLock(locallock);
    1071           0 :         if (locallockp)
    1072           0 :             *locallockp = NULL;
    1073           0 :         if (reportMemoryError)
    1074           0 :             ereport(ERROR,
    1075             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    1076             :                      errmsg("out of shared memory"),
    1077             :                      errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
    1078             :         else
    1079           0 :             return LOCKACQUIRE_NOT_AVAIL;
    1080             :     }
    1081     4349758 :     locallock->proclock = proclock;
    1082     4349758 :     lock = proclock->tag.myLock;
    1083     4349758 :     locallock->lock = lock;
    1084             : 
    1085             :     /*
    1086             :      * If lock requested conflicts with locks requested by waiters, must join
    1087             :      * wait queue.  Otherwise, check for conflict with already-held locks.
    1088             :      * (That's last because most complex check.)
    1089             :      */
    1090     4349758 :     if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
    1091         370 :         found_conflict = true;
    1092             :     else
    1093     4349388 :         found_conflict = LockCheckConflicts(lockMethodTable, lockmode,
    1094             :                                             lock, proclock);
    1095             : 
    1096     4349758 :     if (!found_conflict)
    1097             :     {
    1098             :         /* No conflict with held or previously requested locks */
    1099     4345736 :         GrantLock(lock, proclock, lockmode);
    1100     4345736 :         waitResult = PROC_WAIT_STATUS_OK;
    1101             :     }
    1102             :     else
    1103             :     {
    1104             :         /*
    1105             :          * Join the lock's wait queue.  We call this even in the dontWait
    1106             :          * case, because JoinWaitQueue() may discover that we can acquire the
    1107             :          * lock immediately after all.
    1108             :          */
    1109        4022 :         waitResult = JoinWaitQueue(locallock, lockMethodTable, dontWait);
    1110             :     }
    1111             : 
    1112     4349758 :     if (waitResult == PROC_WAIT_STATUS_ERROR)
    1113             :     {
    1114             :         /*
    1115             :          * We're not getting the lock because a deadlock was detected already
    1116             :          * while trying to join the wait queue, or because we would have to
    1117             :          * wait but the caller requested no blocking.
    1118             :          *
    1119             :          * Undo the changes to shared entries before releasing the partition
    1120             :          * lock.
    1121             :          */
    1122        1462 :         AbortStrongLockAcquire();
    1123             : 
    1124        1462 :         if (proclock->holdMask == 0)
    1125             :         {
    1126             :             uint32      proclock_hashcode;
    1127             : 
    1128        1056 :             proclock_hashcode = ProcLockHashCode(&proclock->tag,
    1129             :                                                  hashcode);
    1130        1056 :             dlist_delete(&proclock->lockLink);
    1131        1056 :             dlist_delete(&proclock->procLink);
    1132        1056 :             if (!hash_search_with_hash_value(LockMethodProcLockHash,
    1133        1056 :                                              &(proclock->tag),
    1134             :                                              proclock_hashcode,
    1135             :                                              HASH_REMOVE,
    1136             :                                              NULL))
    1137           0 :                 elog(PANIC, "proclock table corrupted");
    1138             :         }
    1139             :         else
    1140             :             PROCLOCK_PRINT("LockAcquire: did not join wait queue", proclock);
    1141        1462 :         lock->nRequested--;
    1142        1462 :         lock->requested[lockmode]--;
    1143             :         LOCK_PRINT("LockAcquire: did not join wait queue",
    1144             :                    lock, lockmode);
    1145             :         Assert((lock->nRequested > 0) &&
    1146             :                (lock->requested[lockmode] >= 0));
    1147             :         Assert(lock->nGranted <= lock->nRequested);
    1148        1462 :         LWLockRelease(partitionLock);
    1149        1462 :         if (locallock->nLocks == 0)
    1150        1462 :             RemoveLocalLock(locallock);
    1151             : 
    1152        1462 :         if (dontWait)
    1153             :         {
    1154             :             /*
    1155             :              * Log lock holders and waiters as a detail log message if
    1156             :              * logLockFailure = true and lock acquisition fails with dontWait
    1157             :              * = true
    1158             :              */
    1159        1460 :             if (logLockFailure)
    1160             :             {
    1161             :                 StringInfoData buf,
    1162             :                             lock_waiters_sbuf,
    1163             :                             lock_holders_sbuf;
    1164             :                 const char *modename;
    1165           0 :                 int         lockHoldersNum = 0;
    1166             : 
    1167           0 :                 initStringInfo(&buf);
    1168           0 :                 initStringInfo(&lock_waiters_sbuf);
    1169           0 :                 initStringInfo(&lock_holders_sbuf);
    1170             : 
    1171           0 :                 DescribeLockTag(&buf, &locallock->tag.lock);
    1172           0 :                 modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
    1173             :                                            lockmode);
    1174             : 
    1175             :                 /* Gather a list of all lock holders and waiters */
    1176           0 :                 LWLockAcquire(partitionLock, LW_SHARED);
    1177           0 :                 GetLockHoldersAndWaiters(locallock, &lock_holders_sbuf,
    1178             :                                          &lock_waiters_sbuf, &lockHoldersNum);
    1179           0 :                 LWLockRelease(partitionLock);
    1180             : 
    1181           0 :                 ereport(LOG,
    1182             :                         (errmsg("process %d could not obtain %s on %s",
    1183             :                                 MyProcPid, modename, buf.data),
    1184             :                          errdetail_log_plural(
    1185             :                                               "Process holding the lock: %s, Wait queue: %s.",
    1186             :                                               "Processes holding the lock: %s, Wait queue: %s.",
    1187             :                                               lockHoldersNum,
    1188             :                                               lock_holders_sbuf.data,
    1189             :                                               lock_waiters_sbuf.data)));
    1190             : 
    1191           0 :                 pfree(buf.data);
    1192           0 :                 pfree(lock_holders_sbuf.data);
    1193           0 :                 pfree(lock_waiters_sbuf.data);
    1194             :             }
    1195        1460 :             if (locallockp)
    1196         440 :                 *locallockp = NULL;
    1197        1460 :             return LOCKACQUIRE_NOT_AVAIL;
    1198             :         }
    1199             :         else
    1200             :         {
    1201           2 :             DeadLockReport();
    1202             :             /* DeadLockReport() will not return */
    1203             :         }
    1204             :     }
    1205             : 
    1206             :     /*
    1207             :      * We are now in the lock queue, or the lock was already granted.  If
    1208             :      * queued, go to sleep.
    1209             :      */
    1210     4348296 :     if (waitResult == PROC_WAIT_STATUS_WAITING)
    1211             :     {
    1212             :         Assert(!dontWait);
    1213             :         PROCLOCK_PRINT("LockAcquire: sleeping on lock", proclock);
    1214             :         LOCK_PRINT("LockAcquire: sleeping on lock", lock, lockmode);
    1215        2556 :         LWLockRelease(partitionLock);
    1216             : 
    1217        2556 :         waitResult = WaitOnLock(locallock, owner);
    1218             : 
    1219             :         /*
    1220             :          * NOTE: do not do any material change of state between here and
    1221             :          * return.  All required changes in locktable state must have been
    1222             :          * done when the lock was granted to us --- see notes in WaitOnLock.
    1223             :          */
    1224             : 
    1225        2472 :         if (waitResult == PROC_WAIT_STATUS_ERROR)
    1226             :         {
    1227             :             /*
    1228             :              * We failed as a result of a deadlock, see CheckDeadLock(). Quit
    1229             :              * now.
    1230             :              */
    1231             :             Assert(!dontWait);
    1232          10 :             DeadLockReport();
    1233             :             /* DeadLockReport() will not return */
    1234             :         }
    1235             :     }
    1236             :     else
    1237     4345740 :         LWLockRelease(partitionLock);
    1238             :     Assert(waitResult == PROC_WAIT_STATUS_OK);
    1239             : 
    1240             :     /* The lock was granted to us.  Update the local lock entry accordingly */
    1241             :     Assert((proclock->holdMask & LOCKBIT_ON(lockmode)) != 0);
    1242     4348202 :     GrantLockLocal(locallock, owner);
    1243             : 
    1244             :     /*
    1245             :      * Lock state is fully up-to-date now; if we error out after this, no
    1246             :      * special error cleanup is required.
    1247             :      */
    1248     4348202 :     FinishStrongLockAcquire();
    1249             : 
    1250             :     /*
    1251             :      * Emit a WAL record if acquisition of this lock needs to be replayed in a
    1252             :      * standby server.
    1253             :      */
    1254     4348202 :     if (log_lock)
    1255             :     {
    1256             :         /*
    1257             :          * Decode the locktag back to the original values, to avoid sending
    1258             :          * lots of empty bytes with every message.  See lock.h to check how a
    1259             :          * locktag is defined for LOCKTAG_RELATION
    1260             :          */
    1261      194800 :         LogAccessExclusiveLock(locktag->locktag_field1,
    1262             :                                locktag->locktag_field2);
    1263             :     }
    1264             : 
    1265     4348202 :     return LOCKACQUIRE_OK;
    1266             : }
    1267             : 
    1268             : /*
    1269             :  * Find or create LOCK and PROCLOCK objects as needed for a new lock
    1270             :  * request.
    1271             :  *
    1272             :  * Returns the PROCLOCK object, or NULL if we failed to create the objects
    1273             :  * for lack of shared memory.
    1274             :  *
    1275             :  * The appropriate partition lock must be held at entry, and will be
    1276             :  * held at exit.
    1277             :  */
    1278             : static PROCLOCK *
    1279     4353034 : SetupLockInTable(LockMethod lockMethodTable, PGPROC *proc,
    1280             :                  const LOCKTAG *locktag, uint32 hashcode, LOCKMODE lockmode)
    1281             : {
    1282             :     LOCK       *lock;
    1283             :     PROCLOCK   *proclock;
    1284             :     PROCLOCKTAG proclocktag;
    1285             :     uint32      proclock_hashcode;
    1286             :     bool        found;
    1287             : 
    1288             :     /*
    1289             :      * Find or create a lock with this tag.
    1290             :      */
    1291     4353034 :     lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash,
    1292             :                                                 locktag,
    1293             :                                                 hashcode,
    1294             :                                                 HASH_ENTER_NULL,
    1295             :                                                 &found);
    1296     4353034 :     if (!lock)
    1297           0 :         return NULL;
    1298             : 
    1299             :     /*
    1300             :      * if it's a new lock object, initialize it
    1301             :      */
    1302     4353034 :     if (!found)
    1303             :     {
    1304     3929118 :         lock->grantMask = 0;
    1305     3929118 :         lock->waitMask = 0;
    1306     3929118 :         dlist_init(&lock->procLocks);
    1307     3929118 :         dclist_init(&lock->waitProcs);
    1308     3929118 :         lock->nRequested = 0;
    1309     3929118 :         lock->nGranted = 0;
    1310    23574708 :         MemSet(lock->requested, 0, sizeof(int) * MAX_LOCKMODES);
    1311     3929118 :         MemSet(lock->granted, 0, sizeof(int) * MAX_LOCKMODES);
    1312             :         LOCK_PRINT("LockAcquire: new", lock, lockmode);
    1313             :     }
    1314             :     else
    1315             :     {
    1316             :         LOCK_PRINT("LockAcquire: found", lock, lockmode);
    1317             :         Assert((lock->nRequested >= 0) && (lock->requested[lockmode] >= 0));
    1318             :         Assert((lock->nGranted >= 0) && (lock->granted[lockmode] >= 0));
    1319             :         Assert(lock->nGranted <= lock->nRequested);
    1320             :     }
    1321             : 
    1322             :     /*
    1323             :      * Create the hash key for the proclock table.
    1324             :      */
    1325     4353034 :     proclocktag.myLock = lock;
    1326     4353034 :     proclocktag.myProc = proc;
    1327             : 
    1328     4353034 :     proclock_hashcode = ProcLockHashCode(&proclocktag, hashcode);
    1329             : 
    1330             :     /*
    1331             :      * Find or create a proclock entry with this tag
    1332             :      */
    1333     4353034 :     proclock = (PROCLOCK *) hash_search_with_hash_value(LockMethodProcLockHash,
    1334             :                                                         &proclocktag,
    1335             :                                                         proclock_hashcode,
    1336             :                                                         HASH_ENTER_NULL,
    1337             :                                                         &found);
    1338     4353034 :     if (!proclock)
    1339             :     {
    1340             :         /* Oops, not enough shmem for the proclock */
    1341           0 :         if (lock->nRequested == 0)
    1342             :         {
    1343             :             /*
    1344             :              * There are no other requestors of this lock, so garbage-collect
    1345             :              * the lock object.  We *must* do this to avoid a permanent leak
    1346             :              * of shared memory, because there won't be anything to cause
    1347             :              * anyone to release the lock object later.
    1348             :              */
    1349             :             Assert(dlist_is_empty(&(lock->procLocks)));
    1350           0 :             if (!hash_search_with_hash_value(LockMethodLockHash,
    1351           0 :                                              &(lock->tag),
    1352             :                                              hashcode,
    1353             :                                              HASH_REMOVE,
    1354             :                                              NULL))
    1355           0 :                 elog(PANIC, "lock table corrupted");
    1356             :         }
    1357           0 :         return NULL;
    1358             :     }
    1359             : 
    1360             :     /*
    1361             :      * If new, initialize the new entry
    1362             :      */
    1363     4353034 :     if (!found)
    1364             :     {
    1365     3959370 :         uint32      partition = LockHashPartition(hashcode);
    1366             : 
    1367             :         /*
    1368             :          * It might seem unsafe to access proclock->groupLeader without a
    1369             :          * lock, but it's not really.  Either we are initializing a proclock
    1370             :          * on our own behalf, in which case our group leader isn't changing
    1371             :          * because the group leader for a process can only ever be changed by
    1372             :          * the process itself; or else we are transferring a fast-path lock to
    1373             :          * the main lock table, in which case that process can't change its
    1374             :          * lock group leader without first releasing all of its locks (and in
    1375             :          * particular the one we are currently transferring).
    1376             :          */
    1377     7918740 :         proclock->groupLeader = proc->lockGroupLeader != NULL ?
    1378     3959370 :             proc->lockGroupLeader : proc;
    1379     3959370 :         proclock->holdMask = 0;
    1380     3959370 :         proclock->releaseMask = 0;
    1381             :         /* Add proclock to appropriate lists */
    1382     3959370 :         dlist_push_tail(&lock->procLocks, &proclock->lockLink);
    1383     3959370 :         dlist_push_tail(&proc->myProcLocks[partition], &proclock->procLink);
    1384             :         PROCLOCK_PRINT("LockAcquire: new", proclock);
    1385             :     }
    1386             :     else
    1387             :     {
    1388             :         PROCLOCK_PRINT("LockAcquire: found", proclock);
    1389             :         Assert((proclock->holdMask & ~lock->grantMask) == 0);
    1390             : 
    1391             : #ifdef CHECK_DEADLOCK_RISK
    1392             : 
    1393             :         /*
    1394             :          * Issue warning if we already hold a lower-level lock on this object
    1395             :          * and do not hold a lock of the requested level or higher. This
    1396             :          * indicates a deadlock-prone coding practice (eg, we'd have a
    1397             :          * deadlock if another backend were following the same code path at
    1398             :          * about the same time).
    1399             :          *
    1400             :          * This is not enabled by default, because it may generate log entries
    1401             :          * about user-level coding practices that are in fact safe in context.
    1402             :          * It can be enabled to help find system-level problems.
    1403             :          *
    1404             :          * XXX Doing numeric comparison on the lockmodes is a hack; it'd be
    1405             :          * better to use a table.  For now, though, this works.
    1406             :          */
    1407             :         {
    1408             :             int         i;
    1409             : 
    1410             :             for (i = lockMethodTable->numLockModes; i > 0; i--)
    1411             :             {
    1412             :                 if (proclock->holdMask & LOCKBIT_ON(i))
    1413             :                 {
    1414             :                     if (i >= (int) lockmode)
    1415             :                         break;  /* safe: we have a lock >= req level */
    1416             :                     elog(LOG, "deadlock risk: raising lock level"
    1417             :                          " from %s to %s on object %u/%u/%u",
    1418             :                          lockMethodTable->lockModeNames[i],
    1419             :                          lockMethodTable->lockModeNames[lockmode],
    1420             :                          lock->tag.locktag_field1, lock->tag.locktag_field2,
    1421             :                          lock->tag.locktag_field3);
    1422             :                     break;
    1423             :                 }
    1424             :             }
    1425             :         }
    1426             : #endif                          /* CHECK_DEADLOCK_RISK */
    1427             :     }
    1428             : 
    1429             :     /*
    1430             :      * lock->nRequested and lock->requested[] count the total number of
    1431             :      * requests, whether granted or waiting, so increment those immediately.
    1432             :      * The other counts don't increment till we get the lock.
    1433             :      */
    1434     4353034 :     lock->nRequested++;
    1435     4353034 :     lock->requested[lockmode]++;
    1436             :     Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
    1437             : 
    1438             :     /*
    1439             :      * We shouldn't already hold the desired lock; else locallock table is
    1440             :      * broken.
    1441             :      */
    1442     4353034 :     if (proclock->holdMask & LOCKBIT_ON(lockmode))
    1443           0 :         elog(ERROR, "lock %s on object %u/%u/%u is already held",
    1444             :              lockMethodTable->lockModeNames[lockmode],
    1445             :              lock->tag.locktag_field1, lock->tag.locktag_field2,
    1446             :              lock->tag.locktag_field3);
    1447             : 
    1448     4353034 :     return proclock;
    1449             : }
    1450             : 
    1451             : /*
    1452             :  * Check and set/reset the flag that we hold the relation extension lock.
    1453             :  *
    1454             :  * It is callers responsibility that this function is called after
    1455             :  * acquiring/releasing the relation extension lock.
    1456             :  *
    1457             :  * Pass acquired as true if lock is acquired, false otherwise.
    1458             :  */
    1459             : static inline void
    1460    68788954 : CheckAndSetLockHeld(LOCALLOCK *locallock, bool acquired)
    1461             : {
    1462             : #ifdef USE_ASSERT_CHECKING
    1463             :     if (LOCALLOCK_LOCKTAG(*locallock) == LOCKTAG_RELATION_EXTEND)
    1464             :         IsRelationExtensionLockHeld = acquired;
    1465             : #endif
    1466    68788954 : }
    1467             : 
    1468             : /*
    1469             :  * Subroutine to free a locallock entry
    1470             :  */
    1471             : static void
    1472    33799406 : RemoveLocalLock(LOCALLOCK *locallock)
    1473             : {
    1474             :     int         i;
    1475             : 
    1476    33954622 :     for (i = locallock->numLockOwners - 1; i >= 0; i--)
    1477             :     {
    1478      155216 :         if (locallock->lockOwners[i].owner != NULL)
    1479      155142 :             ResourceOwnerForgetLock(locallock->lockOwners[i].owner, locallock);
    1480             :     }
    1481    33799406 :     locallock->numLockOwners = 0;
    1482    33799406 :     if (locallock->lockOwners != NULL)
    1483    33799406 :         pfree(locallock->lockOwners);
    1484    33799406 :     locallock->lockOwners = NULL;
    1485             : 
    1486    33799406 :     if (locallock->holdsStrongLockCount)
    1487             :     {
    1488             :         uint32      fasthashcode;
    1489             : 
    1490      359134 :         fasthashcode = FastPathStrongLockHashPartition(locallock->hashcode);
    1491             : 
    1492      359134 :         SpinLockAcquire(&FastPathStrongRelationLocks->mutex);
    1493             :         Assert(FastPathStrongRelationLocks->count[fasthashcode] > 0);
    1494      359134 :         FastPathStrongRelationLocks->count[fasthashcode]--;
    1495      359134 :         locallock->holdsStrongLockCount = false;
    1496      359134 :         SpinLockRelease(&FastPathStrongRelationLocks->mutex);
    1497             :     }
    1498             : 
    1499    33799406 :     if (!hash_search(LockMethodLocalHash,
    1500    33799406 :                      &(locallock->tag),
    1501             :                      HASH_REMOVE, NULL))
    1502           0 :         elog(WARNING, "locallock table corrupted");
    1503             : 
    1504             :     /*
    1505             :      * Indicate that the lock is released for certain types of locks
    1506             :      */
    1507    33799406 :     CheckAndSetLockHeld(locallock, false);
    1508    33799406 : }
    1509             : 
    1510             : /*
    1511             :  * LockCheckConflicts -- test whether requested lock conflicts
    1512             :  *      with those already granted
    1513             :  *
    1514             :  * Returns true if conflict, false if no conflict.
    1515             :  *
    1516             :  * NOTES:
    1517             :  *      Here's what makes this complicated: one process's locks don't
    1518             :  * conflict with one another, no matter what purpose they are held for
    1519             :  * (eg, session and transaction locks do not conflict).  Nor do the locks
    1520             :  * of one process in a lock group conflict with those of another process in
    1521             :  * the same group.  So, we must subtract off these locks when determining
    1522             :  * whether the requested new lock conflicts with those already held.
    1523             :  */
    1524             : bool
    1525     4352260 : LockCheckConflicts(LockMethod lockMethodTable,
    1526             :                    LOCKMODE lockmode,
    1527             :                    LOCK *lock,
    1528             :                    PROCLOCK *proclock)
    1529             : {
    1530     4352260 :     int         numLockModes = lockMethodTable->numLockModes;
    1531             :     LOCKMASK    myLocks;
    1532     4352260 :     int         conflictMask = lockMethodTable->conflictTab[lockmode];
    1533             :     int         conflictsRemaining[MAX_LOCKMODES];
    1534     4352260 :     int         totalConflictsRemaining = 0;
    1535             :     dlist_iter  proclock_iter;
    1536             :     int         i;
    1537             : 
    1538             :     /*
    1539             :      * first check for global conflicts: If no locks conflict with my request,
    1540             :      * then I get the lock.
    1541             :      *
    1542             :      * Checking for conflict: lock->grantMask represents the types of
    1543             :      * currently held locks.  conflictTable[lockmode] has a bit set for each
    1544             :      * type of lock that conflicts with request.   Bitwise compare tells if
    1545             :      * there is a conflict.
    1546             :      */
    1547     4352260 :     if (!(conflictMask & lock->grantMask))
    1548             :     {
    1549             :         PROCLOCK_PRINT("LockCheckConflicts: no conflict", proclock);
    1550     4170362 :         return false;
    1551             :     }
    1552             : 
    1553             :     /*
    1554             :      * Rats.  Something conflicts.  But it could still be my own lock, or a
    1555             :      * lock held by another member of my locking group.  First, figure out how
    1556             :      * many conflicts remain after subtracting out any locks I hold myself.
    1557             :      */
    1558      181898 :     myLocks = proclock->holdMask;
    1559     1637082 :     for (i = 1; i <= numLockModes; i++)
    1560             :     {
    1561     1455184 :         if ((conflictMask & LOCKBIT_ON(i)) == 0)
    1562             :         {
    1563      779350 :             conflictsRemaining[i] = 0;
    1564      779350 :             continue;
    1565             :         }
    1566      675834 :         conflictsRemaining[i] = lock->granted[i];
    1567      675834 :         if (myLocks & LOCKBIT_ON(i))
    1568      196928 :             --conflictsRemaining[i];
    1569      675834 :         totalConflictsRemaining += conflictsRemaining[i];
    1570             :     }
    1571             : 
    1572             :     /* If no conflicts remain, we get the lock. */
    1573      181898 :     if (totalConflictsRemaining == 0)
    1574             :     {
    1575             :         PROCLOCK_PRINT("LockCheckConflicts: resolved (simple)", proclock);
    1576      176900 :         return false;
    1577             :     }
    1578             : 
    1579             :     /* If no group locking, it's definitely a conflict. */
    1580        4998 :     if (proclock->groupLeader == MyProc && MyProc->lockGroupLeader == NULL)
    1581             :     {
    1582             :         Assert(proclock->tag.myProc == MyProc);
    1583             :         PROCLOCK_PRINT("LockCheckConflicts: conflicting (simple)",
    1584             :                        proclock);
    1585        3644 :         return true;
    1586             :     }
    1587             : 
    1588             :     /*
    1589             :      * The relation extension lock conflict even between the group members.
    1590             :      */
    1591        1354 :     if (LOCK_LOCKTAG(*lock) == LOCKTAG_RELATION_EXTEND)
    1592             :     {
    1593             :         PROCLOCK_PRINT("LockCheckConflicts: conflicting (group)",
    1594             :                        proclock);
    1595          76 :         return true;
    1596             :     }
    1597             : 
    1598             :     /*
    1599             :      * Locks held in conflicting modes by members of our own lock group are
    1600             :      * not real conflicts; we can subtract those out and see if we still have
    1601             :      * a conflict.  This is O(N) in the number of processes holding or
    1602             :      * awaiting locks on this object.  We could improve that by making the
    1603             :      * shared memory state more complex (and larger) but it doesn't seem worth
    1604             :      * it.
    1605             :      */
    1606        2204 :     dlist_foreach(proclock_iter, &lock->procLocks)
    1607             :     {
    1608        1894 :         PROCLOCK   *otherproclock =
    1609        1894 :             dlist_container(PROCLOCK, lockLink, proclock_iter.cur);
    1610             : 
    1611        1894 :         if (proclock != otherproclock &&
    1612        1584 :             proclock->groupLeader == otherproclock->groupLeader &&
    1613         972 :             (otherproclock->holdMask & conflictMask) != 0)
    1614             :         {
    1615         968 :             int         intersectMask = otherproclock->holdMask & conflictMask;
    1616             : 
    1617        8712 :             for (i = 1; i <= numLockModes; i++)
    1618             :             {
    1619        7744 :                 if ((intersectMask & LOCKBIT_ON(i)) != 0)
    1620             :                 {
    1621         988 :                     if (conflictsRemaining[i] <= 0)
    1622           0 :                         elog(PANIC, "proclocks held do not match lock");
    1623         988 :                     conflictsRemaining[i]--;
    1624         988 :                     totalConflictsRemaining--;
    1625             :                 }
    1626             :             }
    1627             : 
    1628         968 :             if (totalConflictsRemaining == 0)
    1629             :             {
    1630             :                 PROCLOCK_PRINT("LockCheckConflicts: resolved (group)",
    1631             :                                proclock);
    1632         968 :                 return false;
    1633             :             }
    1634             :         }
    1635             :     }
    1636             : 
    1637             :     /* Nope, it's a real conflict. */
    1638             :     PROCLOCK_PRINT("LockCheckConflicts: conflicting (group)", proclock);
    1639         310 :     return true;
    1640             : }
    1641             : 
    1642             : /*
    1643             :  * GrantLock -- update the lock and proclock data structures to show
    1644             :  *      the lock request has been granted.
    1645             :  *
    1646             :  * NOTE: if proc was blocked, it also needs to be removed from the wait list
    1647             :  * and have its waitLock/waitProcLock fields cleared.  That's not done here.
    1648             :  *
    1649             :  * NOTE: the lock grant also has to be recorded in the associated LOCALLOCK
    1650             :  * table entry; but since we may be awaking some other process, we can't do
    1651             :  * that here; it's done by GrantLockLocal, instead.
    1652             :  */
    1653             : void
    1654     4351682 : GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode)
    1655             : {
    1656     4351682 :     lock->nGranted++;
    1657     4351682 :     lock->granted[lockmode]++;
    1658     4351682 :     lock->grantMask |= LOCKBIT_ON(lockmode);
    1659     4351682 :     if (lock->granted[lockmode] == lock->requested[lockmode])
    1660     4351116 :         lock->waitMask &= LOCKBIT_OFF(lockmode);
    1661     4351682 :     proclock->holdMask |= LOCKBIT_ON(lockmode);
    1662             :     LOCK_PRINT("GrantLock", lock, lockmode);
    1663             :     Assert((lock->nGranted > 0) && (lock->granted[lockmode] > 0));
    1664             :     Assert(lock->nGranted <= lock->nRequested);
    1665     4351682 : }
    1666             : 
    1667             : /*
    1668             :  * UnGrantLock -- opposite of GrantLock.
    1669             :  *
    1670             :  * Updates the lock and proclock data structures to show that the lock
    1671             :  * is no longer held nor requested by the current holder.
    1672             :  *
    1673             :  * Returns true if there were any waiters waiting on the lock that
    1674             :  * should now be woken up with ProcLockWakeup.
    1675             :  */
    1676             : static bool
    1677     4351530 : UnGrantLock(LOCK *lock, LOCKMODE lockmode,
    1678             :             PROCLOCK *proclock, LockMethod lockMethodTable)
    1679             : {
    1680     4351530 :     bool        wakeupNeeded = false;
    1681             : 
    1682             :     Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
    1683             :     Assert((lock->nGranted > 0) && (lock->granted[lockmode] > 0));
    1684             :     Assert(lock->nGranted <= lock->nRequested);
    1685             : 
    1686             :     /*
    1687             :      * fix the general lock stats
    1688             :      */
    1689     4351530 :     lock->nRequested--;
    1690     4351530 :     lock->requested[lockmode]--;
    1691     4351530 :     lock->nGranted--;
    1692     4351530 :     lock->granted[lockmode]--;
    1693             : 
    1694     4351530 :     if (lock->granted[lockmode] == 0)
    1695             :     {
    1696             :         /* change the conflict mask.  No more of this lock type. */
    1697     4331032 :         lock->grantMask &= LOCKBIT_OFF(lockmode);
    1698             :     }
    1699             : 
    1700             :     LOCK_PRINT("UnGrantLock: updated", lock, lockmode);
    1701             : 
    1702             :     /*
    1703             :      * We need only run ProcLockWakeup if the released lock conflicts with at
    1704             :      * least one of the lock types requested by waiter(s).  Otherwise whatever
    1705             :      * conflict made them wait must still exist.  NOTE: before MVCC, we could
    1706             :      * skip wakeup if lock->granted[lockmode] was still positive. But that's
    1707             :      * not true anymore, because the remaining granted locks might belong to
    1708             :      * some waiter, who could now be awakened because he doesn't conflict with
    1709             :      * his own locks.
    1710             :      */
    1711     4351530 :     if (lockMethodTable->conflictTab[lockmode] & lock->waitMask)
    1712        2438 :         wakeupNeeded = true;
    1713             : 
    1714             :     /*
    1715             :      * Now fix the per-proclock state.
    1716             :      */
    1717     4351530 :     proclock->holdMask &= LOCKBIT_OFF(lockmode);
    1718             :     PROCLOCK_PRINT("UnGrantLock: updated", proclock);
    1719             : 
    1720     4351530 :     return wakeupNeeded;
    1721             : }
    1722             : 
    1723             : /*
    1724             :  * CleanUpLock -- clean up after releasing a lock.  We garbage-collect the
    1725             :  * proclock and lock objects if possible, and call ProcLockWakeup if there
    1726             :  * are remaining requests and the caller says it's OK.  (Normally, this
    1727             :  * should be called after UnGrantLock, and wakeupNeeded is the result from
    1728             :  * UnGrantLock.)
    1729             :  *
    1730             :  * The appropriate partition lock must be held at entry, and will be
    1731             :  * held at exit.
    1732             :  */
    1733             : static void
    1734     4282342 : CleanUpLock(LOCK *lock, PROCLOCK *proclock,
    1735             :             LockMethod lockMethodTable, uint32 hashcode,
    1736             :             bool wakeupNeeded)
    1737             : {
    1738             :     /*
    1739             :      * If this was my last hold on this lock, delete my entry in the proclock
    1740             :      * table.
    1741             :      */
    1742     4282342 :     if (proclock->holdMask == 0)
    1743             :     {
    1744             :         uint32      proclock_hashcode;
    1745             : 
    1746             :         PROCLOCK_PRINT("CleanUpLock: deleting", proclock);
    1747     3958356 :         dlist_delete(&proclock->lockLink);
    1748     3958356 :         dlist_delete(&proclock->procLink);
    1749     3958356 :         proclock_hashcode = ProcLockHashCode(&proclock->tag, hashcode);
    1750     3958356 :         if (!hash_search_with_hash_value(LockMethodProcLockHash,
    1751     3958356 :                                          &(proclock->tag),
    1752             :                                          proclock_hashcode,
    1753             :                                          HASH_REMOVE,
    1754             :                                          NULL))
    1755           0 :             elog(PANIC, "proclock table corrupted");
    1756             :     }
    1757             : 
    1758     4282342 :     if (lock->nRequested == 0)
    1759             :     {
    1760             :         /*
    1761             :          * The caller just released the last lock, so garbage-collect the lock
    1762             :          * object.
    1763             :          */
    1764             :         LOCK_PRINT("CleanUpLock: deleting", lock, 0);
    1765             :         Assert(dlist_is_empty(&lock->procLocks));
    1766     3929130 :         if (!hash_search_with_hash_value(LockMethodLockHash,
    1767     3929130 :                                          &(lock->tag),
    1768             :                                          hashcode,
    1769             :                                          HASH_REMOVE,
    1770             :                                          NULL))
    1771           0 :             elog(PANIC, "lock table corrupted");
    1772             :     }
    1773      353212 :     else if (wakeupNeeded)
    1774             :     {
    1775             :         /* There are waiters on this lock, so wake them up. */
    1776        2524 :         ProcLockWakeup(lockMethodTable, lock);
    1777             :     }
    1778     4282342 : }
    1779             : 
    1780             : /*
    1781             :  * GrantLockLocal -- update the locallock data structures to show
    1782             :  *      the lock request has been granted.
    1783             :  *
    1784             :  * We expect that LockAcquire made sure there is room to add a new
    1785             :  * ResourceOwner entry.
    1786             :  */
    1787             : static void
    1788    37958038 : GrantLockLocal(LOCALLOCK *locallock, ResourceOwner owner)
    1789             : {
    1790    37958038 :     LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
    1791             :     int         i;
    1792             : 
    1793             :     Assert(locallock->numLockOwners < locallock->maxLockOwners);
    1794             :     /* Count the total */
    1795    37958038 :     locallock->nLocks++;
    1796             :     /* Count the per-owner lock */
    1797    39720810 :     for (i = 0; i < locallock->numLockOwners; i++)
    1798             :     {
    1799     4731262 :         if (lockOwners[i].owner == owner)
    1800             :         {
    1801     2968490 :             lockOwners[i].nLocks++;
    1802     2968490 :             return;
    1803             :         }
    1804             :     }
    1805    34989548 :     lockOwners[i].owner = owner;
    1806    34989548 :     lockOwners[i].nLocks = 1;
    1807    34989548 :     locallock->numLockOwners++;
    1808    34989548 :     if (owner != NULL)
    1809    34816868 :         ResourceOwnerRememberLock(owner, locallock);
    1810             : 
    1811             :     /* Indicate that the lock is acquired for certain types of locks. */
    1812    34989548 :     CheckAndSetLockHeld(locallock, true);
    1813             : }
    1814             : 
    1815             : /*
    1816             :  * BeginStrongLockAcquire - inhibit use of fastpath for a given LOCALLOCK,
    1817             :  * and arrange for error cleanup if it fails
    1818             :  */
    1819             : static void
    1820      359710 : BeginStrongLockAcquire(LOCALLOCK *locallock, uint32 fasthashcode)
    1821             : {
    1822             :     Assert(StrongLockInProgress == NULL);
    1823             :     Assert(locallock->holdsStrongLockCount == false);
    1824             : 
    1825             :     /*
    1826             :      * Adding to a memory location is not atomic, so we take a spinlock to
    1827             :      * ensure we don't collide with someone else trying to bump the count at
    1828             :      * the same time.
    1829             :      *
    1830             :      * XXX: It might be worth considering using an atomic fetch-and-add
    1831             :      * instruction here, on architectures where that is supported.
    1832             :      */
    1833             : 
    1834      359710 :     SpinLockAcquire(&FastPathStrongRelationLocks->mutex);
    1835      359710 :     FastPathStrongRelationLocks->count[fasthashcode]++;
    1836      359710 :     locallock->holdsStrongLockCount = true;
    1837      359710 :     StrongLockInProgress = locallock;
    1838      359710 :     SpinLockRelease(&FastPathStrongRelationLocks->mutex);
    1839      359710 : }
    1840             : 
    1841             : /*
    1842             :  * FinishStrongLockAcquire - cancel pending cleanup for a strong lock
    1843             :  * acquisition once it's no longer needed
    1844             :  */
    1845             : static void
    1846     4348202 : FinishStrongLockAcquire(void)
    1847             : {
    1848     4348202 :     StrongLockInProgress = NULL;
    1849     4348202 : }
    1850             : 
    1851             : /*
    1852             :  * AbortStrongLockAcquire - undo strong lock state changes performed by
    1853             :  * BeginStrongLockAcquire.
    1854             :  */
    1855             : void
    1856      884252 : AbortStrongLockAcquire(void)
    1857             : {
    1858             :     uint32      fasthashcode;
    1859      884252 :     LOCALLOCK  *locallock = StrongLockInProgress;
    1860             : 
    1861      884252 :     if (locallock == NULL)
    1862      883826 :         return;
    1863             : 
    1864         426 :     fasthashcode = FastPathStrongLockHashPartition(locallock->hashcode);
    1865             :     Assert(locallock->holdsStrongLockCount == true);
    1866         426 :     SpinLockAcquire(&FastPathStrongRelationLocks->mutex);
    1867             :     Assert(FastPathStrongRelationLocks->count[fasthashcode] > 0);
    1868         426 :     FastPathStrongRelationLocks->count[fasthashcode]--;
    1869         426 :     locallock->holdsStrongLockCount = false;
    1870         426 :     StrongLockInProgress = NULL;
    1871         426 :     SpinLockRelease(&FastPathStrongRelationLocks->mutex);
    1872             : }
    1873             : 
    1874             : /*
    1875             :  * GrantAwaitedLock -- call GrantLockLocal for the lock we are doing
    1876             :  *      WaitOnLock on.
    1877             :  *
    1878             :  * proc.c needs this for the case where we are booted off the lock by
    1879             :  * timeout, but discover that someone granted us the lock anyway.
    1880             :  *
    1881             :  * We could just export GrantLockLocal, but that would require including
    1882             :  * resowner.h in lock.h, which creates circularity.
    1883             :  */
    1884             : void
    1885           0 : GrantAwaitedLock(void)
    1886             : {
    1887           0 :     GrantLockLocal(awaitedLock, awaitedOwner);
    1888           0 : }
    1889             : 
    1890             : /*
    1891             :  * GetAwaitedLock -- Return the lock we're currently doing WaitOnLock on.
    1892             :  */
    1893             : LOCALLOCK *
    1894      882806 : GetAwaitedLock(void)
    1895             : {
    1896      882806 :     return awaitedLock;
    1897             : }
    1898             : 
    1899             : /*
    1900             :  * ResetAwaitedLock -- Forget that we are waiting on a lock.
    1901             :  */
    1902             : void
    1903          82 : ResetAwaitedLock(void)
    1904             : {
    1905          82 :     awaitedLock = NULL;
    1906          82 : }
    1907             : 
    1908             : /*
    1909             :  * MarkLockClear -- mark an acquired lock as "clear"
    1910             :  *
    1911             :  * This means that we know we have absorbed all sinval messages that other
    1912             :  * sessions generated before we acquired this lock, and so we can confidently
    1913             :  * assume we know about any catalog changes protected by this lock.
    1914             :  */
    1915             : void
    1916    32570364 : MarkLockClear(LOCALLOCK *locallock)
    1917             : {
    1918             :     Assert(locallock->nLocks > 0);
    1919    32570364 :     locallock->lockCleared = true;
    1920    32570364 : }
    1921             : 
    1922             : /*
    1923             :  * WaitOnLock -- wait to acquire a lock
    1924             :  *
    1925             :  * This is a wrapper around ProcSleep, with extra tracing and bookkeeping.
    1926             :  */
    1927             : static ProcWaitStatus
    1928        2556 : WaitOnLock(LOCALLOCK *locallock, ResourceOwner owner)
    1929             : {
    1930             :     ProcWaitStatus result;
    1931             : 
    1932             :     TRACE_POSTGRESQL_LOCK_WAIT_START(locallock->tag.lock.locktag_field1,
    1933             :                                      locallock->tag.lock.locktag_field2,
    1934             :                                      locallock->tag.lock.locktag_field3,
    1935             :                                      locallock->tag.lock.locktag_field4,
    1936             :                                      locallock->tag.lock.locktag_type,
    1937             :                                      locallock->tag.mode);
    1938             : 
    1939             :     /* adjust the process title to indicate that it's waiting */
    1940        2556 :     set_ps_display_suffix("waiting");
    1941             : 
    1942             :     /*
    1943             :      * Record the fact that we are waiting for a lock, so that
    1944             :      * LockErrorCleanup will clean up if cancel/die happens.
    1945             :      */
    1946        2556 :     awaitedLock = locallock;
    1947        2556 :     awaitedOwner = owner;
    1948             : 
    1949             :     /*
    1950             :      * NOTE: Think not to put any shared-state cleanup after the call to
    1951             :      * ProcSleep, in either the normal or failure path.  The lock state must
    1952             :      * be fully set by the lock grantor, or by CheckDeadLock if we give up
    1953             :      * waiting for the lock.  This is necessary because of the possibility
    1954             :      * that a cancel/die interrupt will interrupt ProcSleep after someone else
    1955             :      * grants us the lock, but before we've noticed it. Hence, after granting,
    1956             :      * the locktable state must fully reflect the fact that we own the lock;
    1957             :      * we can't do additional work on return.
    1958             :      *
    1959             :      * We can and do use a PG_TRY block to try to clean up after failure, but
    1960             :      * this still has a major limitation: elog(FATAL) can occur while waiting
    1961             :      * (eg, a "die" interrupt), and then control won't come back here. So all
    1962             :      * cleanup of essential state should happen in LockErrorCleanup, not here.
    1963             :      * We can use PG_TRY to clear the "waiting" status flags, since doing that
    1964             :      * is unimportant if the process exits.
    1965             :      */
    1966        2556 :     PG_TRY();
    1967             :     {
    1968        2556 :         result = ProcSleep(locallock);
    1969             :     }
    1970          74 :     PG_CATCH();
    1971             :     {
    1972             :         /* In this path, awaitedLock remains set until LockErrorCleanup */
    1973             : 
    1974             :         /* reset ps display to remove the suffix */
    1975          74 :         set_ps_display_remove_suffix();
    1976             : 
    1977             :         /* and propagate the error */
    1978          74 :         PG_RE_THROW();
    1979             :     }
    1980        2472 :     PG_END_TRY();
    1981             : 
    1982             :     /*
    1983             :      * We no longer want LockErrorCleanup to do anything.
    1984             :      */
    1985        2472 :     awaitedLock = NULL;
    1986             : 
    1987             :     /* reset ps display to remove the suffix */
    1988        2472 :     set_ps_display_remove_suffix();
    1989             : 
    1990             :     TRACE_POSTGRESQL_LOCK_WAIT_DONE(locallock->tag.lock.locktag_field1,
    1991             :                                     locallock->tag.lock.locktag_field2,
    1992             :                                     locallock->tag.lock.locktag_field3,
    1993             :                                     locallock->tag.lock.locktag_field4,
    1994             :                                     locallock->tag.lock.locktag_type,
    1995             :                                     locallock->tag.mode);
    1996             : 
    1997        2472 :     return result;
    1998             : }
    1999             : 
    2000             : /*
    2001             :  * Remove a proc from the wait-queue it is on (caller must know it is on one).
    2002             :  * This is only used when the proc has failed to get the lock, so we set its
    2003             :  * waitStatus to PROC_WAIT_STATUS_ERROR.
    2004             :  *
    2005             :  * Appropriate partition lock must be held by caller.  Also, caller is
    2006             :  * responsible for signaling the proc if needed.
    2007             :  *
    2008             :  * NB: this does not clean up any locallock object that may exist for the lock.
    2009             :  */
    2010             : void
    2011          92 : RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode)
    2012             : {
    2013          92 :     LOCK       *waitLock = proc->waitLock;
    2014          92 :     PROCLOCK   *proclock = proc->waitProcLock;
    2015          92 :     LOCKMODE    lockmode = proc->waitLockMode;
    2016          92 :     LOCKMETHODID lockmethodid = LOCK_LOCKMETHOD(*waitLock);
    2017             : 
    2018             :     /* Make sure proc is waiting */
    2019             :     Assert(proc->waitStatus == PROC_WAIT_STATUS_WAITING);
    2020             :     Assert(proc->links.next != NULL);
    2021             :     Assert(waitLock);
    2022             :     Assert(!dclist_is_empty(&waitLock->waitProcs));
    2023             :     Assert(0 < lockmethodid && lockmethodid < lengthof(LockMethods));
    2024             : 
    2025             :     /* Remove proc from lock's wait queue */
    2026          92 :     dclist_delete_from_thoroughly(&waitLock->waitProcs, &proc->links);
    2027             : 
    2028             :     /* Undo increments of request counts by waiting process */
    2029             :     Assert(waitLock->nRequested > 0);
    2030             :     Assert(waitLock->nRequested > proc->waitLock->nGranted);
    2031          92 :     waitLock->nRequested--;
    2032             :     Assert(waitLock->requested[lockmode] > 0);
    2033          92 :     waitLock->requested[lockmode]--;
    2034             :     /* don't forget to clear waitMask bit if appropriate */
    2035          92 :     if (waitLock->granted[lockmode] == waitLock->requested[lockmode])
    2036          90 :         waitLock->waitMask &= LOCKBIT_OFF(lockmode);
    2037             : 
    2038             :     /* Clean up the proc's own state, and pass it the ok/fail signal */
    2039          92 :     proc->waitLock = NULL;
    2040          92 :     proc->waitProcLock = NULL;
    2041          92 :     proc->waitStatus = PROC_WAIT_STATUS_ERROR;
    2042             : 
    2043             :     /*
    2044             :      * Delete the proclock immediately if it represents no already-held locks.
    2045             :      * (This must happen now because if the owner of the lock decides to
    2046             :      * release it, and the requested/granted counts then go to zero,
    2047             :      * LockRelease expects there to be no remaining proclocks.) Then see if
    2048             :      * any other waiters for the lock can be woken up now.
    2049             :      */
    2050          92 :     CleanUpLock(waitLock, proclock,
    2051             :                 LockMethods[lockmethodid], hashcode,
    2052             :                 true);
    2053          92 : }
    2054             : 
    2055             : /*
    2056             :  * LockRelease -- look up 'locktag' and release one 'lockmode' lock on it.
    2057             :  *      Release a session lock if 'sessionLock' is true, else release a
    2058             :  *      regular transaction lock.
    2059             :  *
    2060             :  * Side Effects: find any waiting processes that are now wakable,
    2061             :  *      grant them their requested locks and awaken them.
    2062             :  *      (We have to grant the lock here to avoid a race between
    2063             :  *      the waking process and any new process to
    2064             :  *      come along and request the lock.)
    2065             :  */
    2066             : bool
    2067    33636036 : LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
    2068             : {
    2069    33636036 :     LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
    2070             :     LockMethod  lockMethodTable;
    2071             :     LOCALLOCKTAG localtag;
    2072             :     LOCALLOCK  *locallock;
    2073             :     LOCK       *lock;
    2074             :     PROCLOCK   *proclock;
    2075             :     LWLock     *partitionLock;
    2076             :     bool        wakeupNeeded;
    2077             : 
    2078    33636036 :     if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
    2079           0 :         elog(ERROR, "unrecognized lock method: %d", lockmethodid);
    2080    33636036 :     lockMethodTable = LockMethods[lockmethodid];
    2081    33636036 :     if (lockmode <= 0 || lockmode > lockMethodTable->numLockModes)
    2082           0 :         elog(ERROR, "unrecognized lock mode: %d", lockmode);
    2083             : 
    2084             : #ifdef LOCK_DEBUG
    2085             :     if (LOCK_DEBUG_ENABLED(locktag))
    2086             :         elog(LOG, "LockRelease: lock [%u,%u] %s",
    2087             :              locktag->locktag_field1, locktag->locktag_field2,
    2088             :              lockMethodTable->lockModeNames[lockmode]);
    2089             : #endif
    2090             : 
    2091             :     /*
    2092             :      * Find the LOCALLOCK entry for this lock and lockmode
    2093             :      */
    2094    33636036 :     MemSet(&localtag, 0, sizeof(localtag)); /* must clear padding */
    2095    33636036 :     localtag.lock = *locktag;
    2096    33636036 :     localtag.mode = lockmode;
    2097             : 
    2098    33636036 :     locallock = (LOCALLOCK *) hash_search(LockMethodLocalHash,
    2099             :                                           &localtag,
    2100             :                                           HASH_FIND, NULL);
    2101             : 
    2102             :     /*
    2103             :      * let the caller print its own error message, too. Do not ereport(ERROR).
    2104             :      */
    2105    33636036 :     if (!locallock || locallock->nLocks <= 0)
    2106             :     {
    2107          26 :         elog(WARNING, "you don't own a lock of type %s",
    2108             :              lockMethodTable->lockModeNames[lockmode]);
    2109          26 :         return false;
    2110             :     }
    2111             : 
    2112             :     /*
    2113             :      * Decrease the count for the resource owner.
    2114             :      */
    2115             :     {
    2116    33636010 :         LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
    2117             :         ResourceOwner owner;
    2118             :         int         i;
    2119             : 
    2120             :         /* Identify owner for lock */
    2121    33636010 :         if (sessionLock)
    2122      172658 :             owner = NULL;
    2123             :         else
    2124    33463352 :             owner = CurrentResourceOwner;
    2125             : 
    2126    33637728 :         for (i = locallock->numLockOwners - 1; i >= 0; i--)
    2127             :         {
    2128    33637704 :             if (lockOwners[i].owner == owner)
    2129             :             {
    2130             :                 Assert(lockOwners[i].nLocks > 0);
    2131    33635986 :                 if (--lockOwners[i].nLocks == 0)
    2132             :                 {
    2133    32459360 :                     if (owner != NULL)
    2134    32286754 :                         ResourceOwnerForgetLock(owner, locallock);
    2135             :                     /* compact out unused slot */
    2136    32459360 :                     locallock->numLockOwners--;
    2137    32459360 :                     if (i < locallock->numLockOwners)
    2138         120 :                         lockOwners[i] = lockOwners[locallock->numLockOwners];
    2139             :                 }
    2140    33635986 :                 break;
    2141             :             }
    2142             :         }
    2143    33636010 :         if (i < 0)
    2144             :         {
    2145             :             /* don't release a lock belonging to another owner */
    2146          24 :             elog(WARNING, "you don't own a lock of type %s",
    2147             :                  lockMethodTable->lockModeNames[lockmode]);
    2148          24 :             return false;
    2149             :         }
    2150             :     }
    2151             : 
    2152             :     /*
    2153             :      * Decrease the total local count.  If we're still holding the lock, we're
    2154             :      * done.
    2155             :      */
    2156    33635986 :     locallock->nLocks--;
    2157             : 
    2158    33635986 :     if (locallock->nLocks > 0)
    2159     1898916 :         return true;
    2160             : 
    2161             :     /*
    2162             :      * At this point we can no longer suppose we are clear of invalidation
    2163             :      * messages related to this lock.  Although we'll delete the LOCALLOCK
    2164             :      * object before any intentional return from this routine, it seems worth
    2165             :      * the trouble to explicitly reset lockCleared right now, just in case
    2166             :      * some error prevents us from deleting the LOCALLOCK.
    2167             :      */
    2168    31737070 :     locallock->lockCleared = false;
    2169             : 
    2170             :     /* Attempt fast release of any lock eligible for the fast path. */
    2171    31737070 :     if (EligibleForRelationFastPath(locktag, lockmode) &&
    2172    29181272 :         FastPathLocalUseCounts[FAST_PATH_REL_GROUP(locktag->locktag_field2)] > 0)
    2173             :     {
    2174             :         bool        released;
    2175             : 
    2176             :         /*
    2177             :          * We might not find the lock here, even if we originally entered it
    2178             :          * here.  Another backend may have moved it to the main table.
    2179             :          */
    2180    28780466 :         LWLockAcquire(&MyProc->fpInfoLock, LW_EXCLUSIVE);
    2181    28780466 :         released = FastPathUnGrantRelationLock(locktag->locktag_field2,
    2182             :                                                lockmode);
    2183    28780466 :         LWLockRelease(&MyProc->fpInfoLock);
    2184    28780466 :         if (released)
    2185             :         {
    2186    28405720 :             RemoveLocalLock(locallock);
    2187    28405720 :             return true;
    2188             :         }
    2189             :     }
    2190             : 
    2191             :     /*
    2192             :      * Otherwise we've got to mess with the shared lock table.
    2193             :      */
    2194     3331350 :     partitionLock = LockHashPartitionLock(locallock->hashcode);
    2195             : 
    2196     3331350 :     LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    2197             : 
    2198             :     /*
    2199             :      * Normally, we don't need to re-find the lock or proclock, since we kept
    2200             :      * their addresses in the locallock table, and they couldn't have been
    2201             :      * removed while we were holding a lock on them.  But it's possible that
    2202             :      * the lock was taken fast-path and has since been moved to the main hash
    2203             :      * table by another backend, in which case we will need to look up the
    2204             :      * objects here.  We assume the lock field is NULL if so.
    2205             :      */
    2206     3331350 :     lock = locallock->lock;
    2207     3331350 :     if (!lock)
    2208             :     {
    2209             :         PROCLOCKTAG proclocktag;
    2210             : 
    2211             :         Assert(EligibleForRelationFastPath(locktag, lockmode));
    2212          10 :         lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash,
    2213             :                                                     locktag,
    2214             :                                                     locallock->hashcode,
    2215             :                                                     HASH_FIND,
    2216             :                                                     NULL);
    2217          10 :         if (!lock)
    2218           0 :             elog(ERROR, "failed to re-find shared lock object");
    2219          10 :         locallock->lock = lock;
    2220             : 
    2221          10 :         proclocktag.myLock = lock;
    2222          10 :         proclocktag.myProc = MyProc;
    2223          10 :         locallock->proclock = (PROCLOCK *) hash_search(LockMethodProcLockHash,
    2224             :                                                        &proclocktag,
    2225             :                                                        HASH_FIND,
    2226             :                                                        NULL);
    2227          10 :         if (!locallock->proclock)
    2228           0 :             elog(ERROR, "failed to re-find shared proclock object");
    2229             :     }
    2230             :     LOCK_PRINT("LockRelease: found", lock, lockmode);
    2231     3331350 :     proclock = locallock->proclock;
    2232             :     PROCLOCK_PRINT("LockRelease: found", proclock);
    2233             : 
    2234             :     /*
    2235             :      * Double-check that we are actually holding a lock of the type we want to
    2236             :      * release.
    2237             :      */
    2238     3331350 :     if (!(proclock->holdMask & LOCKBIT_ON(lockmode)))
    2239             :     {
    2240             :         PROCLOCK_PRINT("LockRelease: WRONGTYPE", proclock);
    2241           0 :         LWLockRelease(partitionLock);
    2242           0 :         elog(WARNING, "you don't own a lock of type %s",
    2243             :              lockMethodTable->lockModeNames[lockmode]);
    2244           0 :         RemoveLocalLock(locallock);
    2245           0 :         return false;
    2246             :     }
    2247             : 
    2248             :     /*
    2249             :      * Do the releasing.  CleanUpLock will waken any now-wakable waiters.
    2250             :      */
    2251     3331350 :     wakeupNeeded = UnGrantLock(lock, lockmode, proclock, lockMethodTable);
    2252             : 
    2253     3331350 :     CleanUpLock(lock, proclock,
    2254             :                 lockMethodTable, locallock->hashcode,
    2255             :                 wakeupNeeded);
    2256             : 
    2257     3331350 :     LWLockRelease(partitionLock);
    2258             : 
    2259     3331350 :     RemoveLocalLock(locallock);
    2260     3331350 :     return true;
    2261             : }
    2262             : 
    2263             : /*
    2264             :  * LockReleaseAll -- Release all locks of the specified lock method that
    2265             :  *      are held by the current process.
    2266             :  *
    2267             :  * Well, not necessarily *all* locks.  The available behaviors are:
    2268             :  *      allLocks == true: release all locks including session locks.
    2269             :  *      allLocks == false: release all non-session locks.
    2270             :  */
    2271             : void
    2272     1683152 : LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
    2273             : {
    2274             :     HASH_SEQ_STATUS status;
    2275             :     LockMethod  lockMethodTable;
    2276             :     int         i,
    2277             :                 numLockModes;
    2278             :     LOCALLOCK  *locallock;
    2279             :     LOCK       *lock;
    2280             :     int         partition;
    2281     1683152 :     bool        have_fast_path_lwlock = false;
    2282             : 
    2283     1683152 :     if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
    2284           0 :         elog(ERROR, "unrecognized lock method: %d", lockmethodid);
    2285     1683152 :     lockMethodTable = LockMethods[lockmethodid];
    2286             : 
    2287             : #ifdef LOCK_DEBUG
    2288             :     if (*(lockMethodTable->trace_flag))
    2289             :         elog(LOG, "LockReleaseAll: lockmethod=%d", lockmethodid);
    2290             : #endif
    2291             : 
    2292             :     /*
    2293             :      * Get rid of our fast-path VXID lock, if appropriate.  Note that this is
    2294             :      * the only way that the lock we hold on our own VXID can ever get
    2295             :      * released: it is always and only released when a toplevel transaction
    2296             :      * ends.
    2297             :      */
    2298     1683152 :     if (lockmethodid == DEFAULT_LOCKMETHOD)
    2299      824760 :         VirtualXactLockTableCleanup();
    2300             : 
    2301     1683152 :     numLockModes = lockMethodTable->numLockModes;
    2302             : 
    2303             :     /*
    2304             :      * First we run through the locallock table and get rid of unwanted
    2305             :      * entries, then we scan the process's proclocks and get rid of those. We
    2306             :      * do this separately because we may have multiple locallock entries
    2307             :      * pointing to the same proclock, and we daren't end up with any dangling
    2308             :      * pointers.  Fast-path locks are cleaned up during the locallock table
    2309             :      * scan, though.
    2310             :      */
    2311     1683152 :     hash_seq_init(&status, LockMethodLocalHash);
    2312             : 
    2313     4042828 :     while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
    2314             :     {
    2315             :         /*
    2316             :          * If the LOCALLOCK entry is unused, something must've gone wrong
    2317             :          * while trying to acquire this lock.  Just forget the local entry.
    2318             :          */
    2319     2359676 :         if (locallock->nLocks == 0)
    2320             :         {
    2321          94 :             RemoveLocalLock(locallock);
    2322          94 :             continue;
    2323             :         }
    2324             : 
    2325             :         /* Ignore items that are not of the lockmethod to be removed */
    2326     2359582 :         if (LOCALLOCK_LOCKMETHOD(*locallock) != lockmethodid)
    2327      150436 :             continue;
    2328             : 
    2329             :         /*
    2330             :          * If we are asked to release all locks, we can just zap the entry.
    2331             :          * Otherwise, must scan to see if there are session locks. We assume
    2332             :          * there is at most one lockOwners entry for session locks.
    2333             :          */
    2334     2209146 :         if (!allLocks)
    2335             :         {
    2336     2056824 :             LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
    2337             : 
    2338             :             /* If session lock is above array position 0, move it down to 0 */
    2339     4243464 :             for (i = 0; i < locallock->numLockOwners; i++)
    2340             :             {
    2341     2186640 :                 if (lockOwners[i].owner == NULL)
    2342      149826 :                     lockOwners[0] = lockOwners[i];
    2343             :                 else
    2344     2036814 :                     ResourceOwnerForgetLock(lockOwners[i].owner, locallock);
    2345             :             }
    2346             : 
    2347     2056824 :             if (locallock->numLockOwners > 0 &&
    2348     2056824 :                 lockOwners[0].owner == NULL &&
    2349      149826 :                 lockOwners[0].nLocks > 0)
    2350             :             {
    2351             :                 /* Fix the locallock to show just the session locks */
    2352      149826 :                 locallock->nLocks = lockOwners[0].nLocks;
    2353      149826 :                 locallock->numLockOwners = 1;
    2354             :                 /* We aren't deleting this locallock, so done */
    2355      149826 :                 continue;
    2356             :             }
    2357             :             else
    2358     1906998 :                 locallock->numLockOwners = 0;
    2359             :         }
    2360             : 
    2361             : #ifdef USE_ASSERT_CHECKING
    2362             : 
    2363             :         /*
    2364             :          * Tuple locks are currently held only for short durations within a
    2365             :          * transaction. Check that we didn't forget to release one.
    2366             :          */
    2367             :         if (LOCALLOCK_LOCKTAG(*locallock) == LOCKTAG_TUPLE && !allLocks)
    2368             :             elog(WARNING, "tuple lock held at commit");
    2369             : #endif
    2370             : 
    2371             :         /*
    2372             :          * If the lock or proclock pointers are NULL, this lock was taken via
    2373             :          * the relation fast-path (and is not known to have been transferred).
    2374             :          */
    2375     2059320 :         if (locallock->proclock == NULL || locallock->lock == NULL)
    2376             :         {
    2377     1043300 :             LOCKMODE    lockmode = locallock->tag.mode;
    2378             :             Oid         relid;
    2379             : 
    2380             :             /* Verify that a fast-path lock is what we've got. */
    2381     1043300 :             if (!EligibleForRelationFastPath(&locallock->tag.lock, lockmode))
    2382           0 :                 elog(PANIC, "locallock table corrupted");
    2383             : 
    2384             :             /*
    2385             :              * If we don't currently hold the LWLock that protects our
    2386             :              * fast-path data structures, we must acquire it before attempting
    2387             :              * to release the lock via the fast-path.  We will continue to
    2388             :              * hold the LWLock until we're done scanning the locallock table,
    2389             :              * unless we hit a transferred fast-path lock.  (XXX is this
    2390             :              * really such a good idea?  There could be a lot of entries ...)
    2391             :              */
    2392     1043300 :             if (!have_fast_path_lwlock)
    2393             :             {
    2394      358252 :                 LWLockAcquire(&MyProc->fpInfoLock, LW_EXCLUSIVE);
    2395      358252 :                 have_fast_path_lwlock = true;
    2396             :             }
    2397             : 
    2398             :             /* Attempt fast-path release. */
    2399     1043300 :             relid = locallock->tag.lock.locktag_field2;
    2400     1043300 :             if (FastPathUnGrantRelationLock(relid, lockmode))
    2401             :             {
    2402     1041146 :                 RemoveLocalLock(locallock);
    2403     1041146 :                 continue;
    2404             :             }
    2405             : 
    2406             :             /*
    2407             :              * Our lock, originally taken via the fast path, has been
    2408             :              * transferred to the main lock table.  That's going to require
    2409             :              * some extra work, so release our fast-path lock before starting.
    2410             :              */
    2411        2154 :             LWLockRelease(&MyProc->fpInfoLock);
    2412        2154 :             have_fast_path_lwlock = false;
    2413             : 
    2414             :             /*
    2415             :              * Now dump the lock.  We haven't got a pointer to the LOCK or
    2416             :              * PROCLOCK in this case, so we have to handle this a bit
    2417             :              * differently than a normal lock release.  Unfortunately, this
    2418             :              * requires an extra LWLock acquire-and-release cycle on the
    2419             :              * partitionLock, but hopefully it shouldn't happen often.
    2420             :              */
    2421        2154 :             LockRefindAndRelease(lockMethodTable, MyProc,
    2422             :                                  &locallock->tag.lock, lockmode, false);
    2423        2154 :             RemoveLocalLock(locallock);
    2424        2154 :             continue;
    2425             :         }
    2426             : 
    2427             :         /* Mark the proclock to show we need to release this lockmode */
    2428     1016020 :         if (locallock->nLocks > 0)
    2429     1016020 :             locallock->proclock->releaseMask |= LOCKBIT_ON(locallock->tag.mode);
    2430             : 
    2431             :         /* And remove the locallock hashtable entry */
    2432     1016020 :         RemoveLocalLock(locallock);
    2433             :     }
    2434             : 
    2435             :     /* Done with the fast-path data structures */
    2436     1683152 :     if (have_fast_path_lwlock)
    2437      356098 :         LWLockRelease(&MyProc->fpInfoLock);
    2438             : 
    2439             :     /*
    2440             :      * Now, scan each lock partition separately.
    2441             :      */
    2442    28613584 :     for (partition = 0; partition < NUM_LOCK_PARTITIONS; partition++)
    2443             :     {
    2444             :         LWLock     *partitionLock;
    2445    26930432 :         dlist_head *procLocks = &MyProc->myProcLocks[partition];
    2446             :         dlist_mutable_iter proclock_iter;
    2447             : 
    2448    26930432 :         partitionLock = LockHashPartitionLockByIndex(partition);
    2449             : 
    2450             :         /*
    2451             :          * If the proclock list for this partition is empty, we can skip
    2452             :          * acquiring the partition lock.  This optimization is trickier than
    2453             :          * it looks, because another backend could be in process of adding
    2454             :          * something to our proclock list due to promoting one of our
    2455             :          * fast-path locks.  However, any such lock must be one that we
    2456             :          * decided not to delete above, so it's okay to skip it again now;
    2457             :          * we'd just decide not to delete it again.  We must, however, be
    2458             :          * careful to re-fetch the list header once we've acquired the
    2459             :          * partition lock, to be sure we have a valid, up-to-date pointer.
    2460             :          * (There is probably no significant risk if pointer fetch/store is
    2461             :          * atomic, but we don't wish to assume that.)
    2462             :          *
    2463             :          * XXX This argument assumes that the locallock table correctly
    2464             :          * represents all of our fast-path locks.  While allLocks mode
    2465             :          * guarantees to clean up all of our normal locks regardless of the
    2466             :          * locallock situation, we lose that guarantee for fast-path locks.
    2467             :          * This is not ideal.
    2468             :          */
    2469    26930432 :         if (dlist_is_empty(procLocks))
    2470    25901544 :             continue;           /* needn't examine this partition */
    2471             : 
    2472     1028888 :         LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    2473             : 
    2474     2274488 :         dlist_foreach_modify(proclock_iter, procLocks)
    2475             :         {
    2476     1245600 :             PROCLOCK   *proclock = dlist_container(PROCLOCK, procLink, proclock_iter.cur);
    2477     1245600 :             bool        wakeupNeeded = false;
    2478             : 
    2479             :             Assert(proclock->tag.myProc == MyProc);
    2480             : 
    2481     1245600 :             lock = proclock->tag.myLock;
    2482             : 
    2483             :             /* Ignore items that are not of the lockmethod to be removed */
    2484     1245600 :             if (LOCK_LOCKMETHOD(*lock) != lockmethodid)
    2485      150432 :                 continue;
    2486             : 
    2487             :             /*
    2488             :              * In allLocks mode, force release of all locks even if locallock
    2489             :              * table had problems
    2490             :              */
    2491     1095168 :             if (allLocks)
    2492       83174 :                 proclock->releaseMask = proclock->holdMask;
    2493             :             else
    2494             :                 Assert((proclock->releaseMask & ~proclock->holdMask) == 0);
    2495             : 
    2496             :             /*
    2497             :              * Ignore items that have nothing to be released, unless they have
    2498             :              * holdMask == 0 and are therefore recyclable
    2499             :              */
    2500     1095168 :             if (proclock->releaseMask == 0 && proclock->holdMask != 0)
    2501      148426 :                 continue;
    2502             : 
    2503             :             PROCLOCK_PRINT("LockReleaseAll", proclock);
    2504             :             LOCK_PRINT("LockReleaseAll", lock, 0);
    2505             :             Assert(lock->nRequested >= 0);
    2506             :             Assert(lock->nGranted >= 0);
    2507             :             Assert(lock->nGranted <= lock->nRequested);
    2508             :             Assert((proclock->holdMask & ~lock->grantMask) == 0);
    2509             : 
    2510             :             /*
    2511             :              * Release the previously-marked lock modes
    2512             :              */
    2513     8520678 :             for (i = 1; i <= numLockModes; i++)
    2514             :             {
    2515     7573936 :                 if (proclock->releaseMask & LOCKBIT_ON(i))
    2516     1016022 :                     wakeupNeeded |= UnGrantLock(lock, i, proclock,
    2517             :                                                 lockMethodTable);
    2518             :             }
    2519             :             Assert((lock->nRequested >= 0) && (lock->nGranted >= 0));
    2520             :             Assert(lock->nGranted <= lock->nRequested);
    2521             :             LOCK_PRINT("LockReleaseAll: updated", lock, 0);
    2522             : 
    2523      946742 :             proclock->releaseMask = 0;
    2524             : 
    2525             :             /* CleanUpLock will wake up waiters if needed. */
    2526      946742 :             CleanUpLock(lock, proclock,
    2527             :                         lockMethodTable,
    2528      946742 :                         LockTagHashCode(&lock->tag),
    2529             :                         wakeupNeeded);
    2530             :         }                       /* loop over PROCLOCKs within this partition */
    2531             : 
    2532     1028888 :         LWLockRelease(partitionLock);
    2533             :     }                           /* loop over partitions */
    2534             : 
    2535             : #ifdef LOCK_DEBUG
    2536             :     if (*(lockMethodTable->trace_flag))
    2537             :         elog(LOG, "LockReleaseAll done");
    2538             : #endif
    2539     1683152 : }
    2540             : 
    2541             : /*
    2542             :  * LockReleaseSession -- Release all session locks of the specified lock method
    2543             :  *      that are held by the current process.
    2544             :  */
    2545             : void
    2546         238 : LockReleaseSession(LOCKMETHODID lockmethodid)
    2547             : {
    2548             :     HASH_SEQ_STATUS status;
    2549             :     LOCALLOCK  *locallock;
    2550             : 
    2551         238 :     if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
    2552           0 :         elog(ERROR, "unrecognized lock method: %d", lockmethodid);
    2553             : 
    2554         238 :     hash_seq_init(&status, LockMethodLocalHash);
    2555             : 
    2556         452 :     while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
    2557             :     {
    2558             :         /* Ignore items that are not of the specified lock method */
    2559         214 :         if (LOCALLOCK_LOCKMETHOD(*locallock) != lockmethodid)
    2560          20 :             continue;
    2561             : 
    2562         194 :         ReleaseLockIfHeld(locallock, true);
    2563             :     }
    2564         238 : }
    2565             : 
    2566             : /*
    2567             :  * LockReleaseCurrentOwner
    2568             :  *      Release all locks belonging to CurrentResourceOwner
    2569             :  *
    2570             :  * If the caller knows what those locks are, it can pass them as an array.
    2571             :  * That speeds up the call significantly, when a lot of locks are held.
    2572             :  * Otherwise, pass NULL for locallocks, and we'll traverse through our hash
    2573             :  * table to find them.
    2574             :  */
    2575             : void
    2576       10622 : LockReleaseCurrentOwner(LOCALLOCK **locallocks, int nlocks)
    2577             : {
    2578       10622 :     if (locallocks == NULL)
    2579             :     {
    2580             :         HASH_SEQ_STATUS status;
    2581             :         LOCALLOCK  *locallock;
    2582             : 
    2583           8 :         hash_seq_init(&status, LockMethodLocalHash);
    2584             : 
    2585         544 :         while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
    2586         536 :             ReleaseLockIfHeld(locallock, false);
    2587             :     }
    2588             :     else
    2589             :     {
    2590             :         int         i;
    2591             : 
    2592       15704 :         for (i = nlocks - 1; i >= 0; i--)
    2593        5090 :             ReleaseLockIfHeld(locallocks[i], false);
    2594             :     }
    2595       10622 : }
    2596             : 
    2597             : /*
    2598             :  * ReleaseLockIfHeld
    2599             :  *      Release any session-level locks on this lockable object if sessionLock
    2600             :  *      is true; else, release any locks held by CurrentResourceOwner.
    2601             :  *
    2602             :  * It is tempting to pass this a ResourceOwner pointer (or NULL for session
    2603             :  * locks), but without refactoring LockRelease() we cannot support releasing
    2604             :  * locks belonging to resource owners other than CurrentResourceOwner.
    2605             :  * If we were to refactor, it'd be a good idea to fix it so we don't have to
    2606             :  * do a hashtable lookup of the locallock, too.  However, currently this
    2607             :  * function isn't used heavily enough to justify refactoring for its
    2608             :  * convenience.
    2609             :  */
    2610             : static void
    2611        5820 : ReleaseLockIfHeld(LOCALLOCK *locallock, bool sessionLock)
    2612             : {
    2613             :     ResourceOwner owner;
    2614             :     LOCALLOCKOWNER *lockOwners;
    2615             :     int         i;
    2616             : 
    2617             :     /* Identify owner for lock (must match LockRelease!) */
    2618        5820 :     if (sessionLock)
    2619         194 :         owner = NULL;
    2620             :     else
    2621        5626 :         owner = CurrentResourceOwner;
    2622             : 
    2623             :     /* Scan to see if there are any locks belonging to the target owner */
    2624        5820 :     lockOwners = locallock->lockOwners;
    2625        6206 :     for (i = locallock->numLockOwners - 1; i >= 0; i--)
    2626             :     {
    2627        5820 :         if (lockOwners[i].owner == owner)
    2628             :         {
    2629             :             Assert(lockOwners[i].nLocks > 0);
    2630        5434 :             if (lockOwners[i].nLocks < locallock->nLocks)
    2631             :             {
    2632             :                 /*
    2633             :                  * We will still hold this lock after forgetting this
    2634             :                  * ResourceOwner.
    2635             :                  */
    2636        1406 :                 locallock->nLocks -= lockOwners[i].nLocks;
    2637             :                 /* compact out unused slot */
    2638        1406 :                 locallock->numLockOwners--;
    2639        1406 :                 if (owner != NULL)
    2640        1406 :                     ResourceOwnerForgetLock(owner, locallock);
    2641        1406 :                 if (i < locallock->numLockOwners)
    2642           0 :                     lockOwners[i] = lockOwners[locallock->numLockOwners];
    2643             :             }
    2644             :             else
    2645             :             {
    2646             :                 Assert(lockOwners[i].nLocks == locallock->nLocks);
    2647             :                 /* We want to call LockRelease just once */
    2648        4028 :                 lockOwners[i].nLocks = 1;
    2649        4028 :                 locallock->nLocks = 1;
    2650        4028 :                 if (!LockRelease(&locallock->tag.lock,
    2651             :                                  locallock->tag.mode,
    2652             :                                  sessionLock))
    2653           0 :                     elog(WARNING, "ReleaseLockIfHeld: failed??");
    2654             :             }
    2655        5434 :             break;
    2656             :         }
    2657             :     }
    2658        5820 : }
    2659             : 
    2660             : /*
    2661             :  * LockReassignCurrentOwner
    2662             :  *      Reassign all locks belonging to CurrentResourceOwner to belong
    2663             :  *      to its parent resource owner.
    2664             :  *
    2665             :  * If the caller knows what those locks are, it can pass them as an array.
    2666             :  * That speeds up the call significantly, when a lot of locks are held
    2667             :  * (e.g pg_dump with a large schema).  Otherwise, pass NULL for locallocks,
    2668             :  * and we'll traverse through our hash table to find them.
    2669             :  */
    2670             : void
    2671      697384 : LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks)
    2672             : {
    2673      697384 :     ResourceOwner parent = ResourceOwnerGetParent(CurrentResourceOwner);
    2674             : 
    2675             :     Assert(parent != NULL);
    2676             : 
    2677      697384 :     if (locallocks == NULL)
    2678             :     {
    2679             :         HASH_SEQ_STATUS status;
    2680             :         LOCALLOCK  *locallock;
    2681             : 
    2682        6900 :         hash_seq_init(&status, LockMethodLocalHash);
    2683             : 
    2684      208206 :         while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
    2685      201306 :             LockReassignOwner(locallock, parent);
    2686             :     }
    2687             :     else
    2688             :     {
    2689             :         int         i;
    2690             : 
    2691     1551204 :         for (i = nlocks - 1; i >= 0; i--)
    2692      860720 :             LockReassignOwner(locallocks[i], parent);
    2693             :     }
    2694      697384 : }
    2695             : 
    2696             : /*
    2697             :  * Subroutine of LockReassignCurrentOwner. Reassigns a given lock belonging to
    2698             :  * CurrentResourceOwner to its parent.
    2699             :  */
    2700             : static void
    2701     1062026 : LockReassignOwner(LOCALLOCK *locallock, ResourceOwner parent)
    2702             : {
    2703             :     LOCALLOCKOWNER *lockOwners;
    2704             :     int         i;
    2705     1062026 :     int         ic = -1;
    2706     1062026 :     int         ip = -1;
    2707             : 
    2708             :     /*
    2709             :      * Scan to see if there are any locks belonging to current owner or its
    2710             :      * parent
    2711             :      */
    2712     1062026 :     lockOwners = locallock->lockOwners;
    2713     2547572 :     for (i = locallock->numLockOwners - 1; i >= 0; i--)
    2714             :     {
    2715     1485546 :         if (lockOwners[i].owner == CurrentResourceOwner)
    2716     1027072 :             ic = i;
    2717      458474 :         else if (lockOwners[i].owner == parent)
    2718      371646 :             ip = i;
    2719             :     }
    2720             : 
    2721     1062026 :     if (ic < 0)
    2722       34954 :         return;                 /* no current locks */
    2723             : 
    2724     1027072 :     if (ip < 0)
    2725             :     {
    2726             :         /* Parent has no slot, so just give it the child's slot */
    2727      690320 :         lockOwners[ic].owner = parent;
    2728      690320 :         ResourceOwnerRememberLock(parent, locallock);
    2729             :     }
    2730             :     else
    2731             :     {
    2732             :         /* Merge child's count with parent's */
    2733      336752 :         lockOwners[ip].nLocks += lockOwners[ic].nLocks;
    2734             :         /* compact out unused slot */
    2735      336752 :         locallock->numLockOwners--;
    2736      336752 :         if (ic < locallock->numLockOwners)
    2737        1644 :             lockOwners[ic] = lockOwners[locallock->numLockOwners];
    2738             :     }
    2739     1027072 :     ResourceOwnerForgetLock(CurrentResourceOwner, locallock);
    2740             : }
    2741             : 
    2742             : /*
    2743             :  * FastPathGrantRelationLock
    2744             :  *      Grant lock using per-backend fast-path array, if there is space.
    2745             :  */
    2746             : static bool
    2747    29449648 : FastPathGrantRelationLock(Oid relid, LOCKMODE lockmode)
    2748             : {
    2749             :     uint32      i;
    2750    29449648 :     uint32      unused_slot = FastPathLockSlotsPerBackend();
    2751             : 
    2752             :     /* fast-path group the lock belongs to */
    2753    29449648 :     uint32      group = FAST_PATH_REL_GROUP(relid);
    2754             : 
    2755             :     /* Scan for existing entry for this relid, remembering empty slot. */
    2756   499514108 :     for (i = 0; i < FP_LOCK_SLOTS_PER_GROUP; i++)
    2757             :     {
    2758             :         /* index into the whole per-backend array */
    2759   470855924 :         uint32      f = FAST_PATH_SLOT(group, i);
    2760             : 
    2761   470855924 :         if (FAST_PATH_GET_BITS(MyProc, f) == 0)
    2762   454831278 :             unused_slot = f;
    2763    16024646 :         else if (MyProc->fpRelId[f] == relid)
    2764             :         {
    2765             :             Assert(!FAST_PATH_CHECK_LOCKMODE(MyProc, f, lockmode));
    2766      791464 :             FAST_PATH_SET_LOCKMODE(MyProc, f, lockmode);
    2767      791464 :             return true;
    2768             :         }
    2769             :     }
    2770             : 
    2771             :     /* If no existing entry, use any empty slot. */
    2772    28658184 :     if (unused_slot < FastPathLockSlotsPerBackend())
    2773             :     {
    2774    28658184 :         MyProc->fpRelId[unused_slot] = relid;
    2775    28658184 :         FAST_PATH_SET_LOCKMODE(MyProc, unused_slot, lockmode);
    2776    28658184 :         ++FastPathLocalUseCounts[group];
    2777    28658184 :         return true;
    2778             :     }
    2779             : 
    2780             :     /* No existing entry, and no empty slot. */
    2781           0 :     return false;
    2782             : }
    2783             : 
    2784             : /*
    2785             :  * FastPathUnGrantRelationLock
    2786             :  *      Release fast-path lock, if present.  Update backend-private local
    2787             :  *      use count, while we're at it.
    2788             :  */
    2789             : static bool
    2790    29823766 : FastPathUnGrantRelationLock(Oid relid, LOCKMODE lockmode)
    2791             : {
    2792             :     uint32      i;
    2793    29823766 :     bool        result = false;
    2794             : 
    2795             :     /* fast-path group the lock belongs to */
    2796    29823766 :     uint32      group = FAST_PATH_REL_GROUP(relid);
    2797             : 
    2798    29823766 :     FastPathLocalUseCounts[group] = 0;
    2799   507004022 :     for (i = 0; i < FP_LOCK_SLOTS_PER_GROUP; i++)
    2800             :     {
    2801             :         /* index into the whole per-backend array */
    2802   477180256 :         uint32      f = FAST_PATH_SLOT(group, i);
    2803             : 
    2804   477180256 :         if (MyProc->fpRelId[f] == relid
    2805    40729870 :             && FAST_PATH_CHECK_LOCKMODE(MyProc, f, lockmode))
    2806             :         {
    2807             :             Assert(!result);
    2808    29446866 :             FAST_PATH_CLEAR_LOCKMODE(MyProc, f, lockmode);
    2809    29446866 :             result = true;
    2810             :             /* we continue iterating so as to update FastPathLocalUseCount */
    2811             :         }
    2812   477180256 :         if (FAST_PATH_GET_BITS(MyProc, f) != 0)
    2813    21503626 :             ++FastPathLocalUseCounts[group];
    2814             :     }
    2815    29823766 :     return result;
    2816             : }
    2817             : 
    2818             : /*
    2819             :  * FastPathTransferRelationLocks
    2820             :  *      Transfer locks matching the given lock tag from per-backend fast-path
    2821             :  *      arrays to the shared hash table.
    2822             :  *
    2823             :  * Returns true if successful, false if ran out of shared memory.
    2824             :  */
    2825             : static bool
    2826      359710 : FastPathTransferRelationLocks(LockMethod lockMethodTable, const LOCKTAG *locktag,
    2827             :                               uint32 hashcode)
    2828             : {
    2829      359710 :     LWLock     *partitionLock = LockHashPartitionLock(hashcode);
    2830      359710 :     Oid         relid = locktag->locktag_field2;
    2831             :     uint32      i;
    2832             : 
    2833             :     /* fast-path group the lock belongs to */
    2834      359710 :     uint32      group = FAST_PATH_REL_GROUP(relid);
    2835             : 
    2836             :     /*
    2837             :      * Every PGPROC that can potentially hold a fast-path lock is present in
    2838             :      * ProcGlobal->allProcs.  Prepared transactions are not, but any
    2839             :      * outstanding fast-path locks held by prepared transactions are
    2840             :      * transferred to the main lock table.
    2841             :      */
    2842    52344292 :     for (i = 0; i < ProcGlobal->allProcCount; i++)
    2843             :     {
    2844    51984582 :         PGPROC     *proc = &ProcGlobal->allProcs[i];
    2845             :         uint32      j;
    2846             : 
    2847    51984582 :         LWLockAcquire(&proc->fpInfoLock, LW_EXCLUSIVE);
    2848             : 
    2849             :         /*
    2850             :          * If the target backend isn't referencing the same database as the
    2851             :          * lock, then we needn't examine the individual relation IDs at all;
    2852             :          * none of them can be relevant.
    2853             :          *
    2854             :          * proc->databaseId is set at backend startup time and never changes
    2855             :          * thereafter, so it might be safe to perform this test before
    2856             :          * acquiring &proc->fpInfoLock.  In particular, it's certainly safe to
    2857             :          * assume that if the target backend holds any fast-path locks, it
    2858             :          * must have performed a memory-fencing operation (in particular, an
    2859             :          * LWLock acquisition) since setting proc->databaseId.  However, it's
    2860             :          * less clear that our backend is certain to have performed a memory
    2861             :          * fencing operation since the other backend set proc->databaseId.  So
    2862             :          * for now, we test it after acquiring the LWLock just to be safe.
    2863             :          *
    2864             :          * Also skip groups without any registered fast-path locks.
    2865             :          */
    2866    51984582 :         if (proc->databaseId != locktag->locktag_field1 ||
    2867    18333348 :             proc->fpLockBits[group] == 0)
    2868             :         {
    2869    51663744 :             LWLockRelease(&proc->fpInfoLock);
    2870    51663744 :             continue;
    2871             :         }
    2872             : 
    2873     5451914 :         for (j = 0; j < FP_LOCK_SLOTS_PER_GROUP; j++)
    2874             :         {
    2875             :             uint32      lockmode;
    2876             : 
    2877             :             /* index into the whole per-backend array */
    2878     5133146 :             uint32      f = FAST_PATH_SLOT(group, j);
    2879             : 
    2880             :             /* Look for an allocated slot matching the given relid. */
    2881     5133146 :             if (relid != proc->fpRelId[f] || FAST_PATH_GET_BITS(proc, f) == 0)
    2882     5131076 :                 continue;
    2883             : 
    2884             :             /* Find or create lock object. */
    2885        2070 :             LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    2886        8280 :             for (lockmode = FAST_PATH_LOCKNUMBER_OFFSET;
    2887             :                  lockmode < FAST_PATH_LOCKNUMBER_OFFSET + FAST_PATH_BITS_PER_SLOT;
    2888        6210 :                  ++lockmode)
    2889             :             {
    2890             :                 PROCLOCK   *proclock;
    2891             : 
    2892        6210 :                 if (!FAST_PATH_CHECK_LOCKMODE(proc, f, lockmode))
    2893        4030 :                     continue;
    2894        2180 :                 proclock = SetupLockInTable(lockMethodTable, proc, locktag,
    2895             :                                             hashcode, lockmode);
    2896        2180 :                 if (!proclock)
    2897             :                 {
    2898           0 :                     LWLockRelease(partitionLock);
    2899           0 :                     LWLockRelease(&proc->fpInfoLock);
    2900           0 :                     return false;
    2901             :                 }
    2902        2180 :                 GrantLock(proclock->tag.myLock, proclock, lockmode);
    2903        2180 :                 FAST_PATH_CLEAR_LOCKMODE(proc, f, lockmode);
    2904             :             }
    2905        2070 :             LWLockRelease(partitionLock);
    2906             : 
    2907             :             /* No need to examine remaining slots. */
    2908        2070 :             break;
    2909             :         }
    2910      320838 :         LWLockRelease(&proc->fpInfoLock);
    2911             :     }
    2912      359710 :     return true;
    2913             : }
    2914             : 
    2915             : /*
    2916             :  * FastPathGetRelationLockEntry
    2917             :  *      Return the PROCLOCK for a lock originally taken via the fast-path,
    2918             :  *      transferring it to the primary lock table if necessary.
    2919             :  *
    2920             :  * Note: caller takes care of updating the locallock object.
    2921             :  */
    2922             : static PROCLOCK *
    2923         618 : FastPathGetRelationLockEntry(LOCALLOCK *locallock)
    2924             : {
    2925         618 :     LockMethod  lockMethodTable = LockMethods[DEFAULT_LOCKMETHOD];
    2926         618 :     LOCKTAG    *locktag = &locallock->tag.lock;
    2927         618 :     PROCLOCK   *proclock = NULL;
    2928         618 :     LWLock     *partitionLock = LockHashPartitionLock(locallock->hashcode);
    2929         618 :     Oid         relid = locktag->locktag_field2;
    2930             :     uint32      i,
    2931             :                 group;
    2932             : 
    2933             :     /* fast-path group the lock belongs to */
    2934         618 :     group = FAST_PATH_REL_GROUP(relid);
    2935             : 
    2936         618 :     LWLockAcquire(&MyProc->fpInfoLock, LW_EXCLUSIVE);
    2937             : 
    2938        9892 :     for (i = 0; i < FP_LOCK_SLOTS_PER_GROUP; i++)
    2939             :     {
    2940             :         uint32      lockmode;
    2941             : 
    2942             :         /* index into the whole per-backend array */
    2943        9876 :         uint32      f = FAST_PATH_SLOT(group, i);
    2944             : 
    2945             :         /* Look for an allocated slot matching the given relid. */
    2946        9876 :         if (relid != MyProc->fpRelId[f] || FAST_PATH_GET_BITS(MyProc, f) == 0)
    2947        9274 :             continue;
    2948             : 
    2949             :         /* If we don't have a lock of the given mode, forget it! */
    2950         602 :         lockmode = locallock->tag.mode;
    2951         602 :         if (!FAST_PATH_CHECK_LOCKMODE(MyProc, f, lockmode))
    2952           0 :             break;
    2953             : 
    2954             :         /* Find or create lock object. */
    2955         602 :         LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    2956             : 
    2957         602 :         proclock = SetupLockInTable(lockMethodTable, MyProc, locktag,
    2958             :                                     locallock->hashcode, lockmode);
    2959         602 :         if (!proclock)
    2960             :         {
    2961           0 :             LWLockRelease(partitionLock);
    2962           0 :             LWLockRelease(&MyProc->fpInfoLock);
    2963           0 :             ereport(ERROR,
    2964             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    2965             :                      errmsg("out of shared memory"),
    2966             :                      errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
    2967             :         }
    2968         602 :         GrantLock(proclock->tag.myLock, proclock, lockmode);
    2969         602 :         FAST_PATH_CLEAR_LOCKMODE(MyProc, f, lockmode);
    2970             : 
    2971         602 :         LWLockRelease(partitionLock);
    2972             : 
    2973             :         /* No need to examine remaining slots. */
    2974         602 :         break;
    2975             :     }
    2976             : 
    2977         618 :     LWLockRelease(&MyProc->fpInfoLock);
    2978             : 
    2979             :     /* Lock may have already been transferred by some other backend. */
    2980         618 :     if (proclock == NULL)
    2981             :     {
    2982             :         LOCK       *lock;
    2983             :         PROCLOCKTAG proclocktag;
    2984             :         uint32      proclock_hashcode;
    2985             : 
    2986          16 :         LWLockAcquire(partitionLock, LW_SHARED);
    2987             : 
    2988          16 :         lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash,
    2989             :                                                     locktag,
    2990             :                                                     locallock->hashcode,
    2991             :                                                     HASH_FIND,
    2992             :                                                     NULL);
    2993          16 :         if (!lock)
    2994           0 :             elog(ERROR, "failed to re-find shared lock object");
    2995             : 
    2996          16 :         proclocktag.myLock = lock;
    2997          16 :         proclocktag.myProc = MyProc;
    2998             : 
    2999          16 :         proclock_hashcode = ProcLockHashCode(&proclocktag, locallock->hashcode);
    3000             :         proclock = (PROCLOCK *)
    3001          16 :             hash_search_with_hash_value(LockMethodProcLockHash,
    3002             :                                         &proclocktag,
    3003             :                                         proclock_hashcode,
    3004             :                                         HASH_FIND,
    3005             :                                         NULL);
    3006          16 :         if (!proclock)
    3007           0 :             elog(ERROR, "failed to re-find shared proclock object");
    3008          16 :         LWLockRelease(partitionLock);
    3009             :     }
    3010             : 
    3011         618 :     return proclock;
    3012             : }
    3013             : 
    3014             : /*
    3015             :  * GetLockConflicts
    3016             :  *      Get an array of VirtualTransactionIds of xacts currently holding locks
    3017             :  *      that would conflict with the specified lock/lockmode.
    3018             :  *      xacts merely awaiting such a lock are NOT reported.
    3019             :  *
    3020             :  * The result array is palloc'd and is terminated with an invalid VXID.
    3021             :  * *countp, if not null, is updated to the number of items set.
    3022             :  *
    3023             :  * Of course, the result could be out of date by the time it's returned, so
    3024             :  * use of this function has to be thought about carefully.  Similarly, a
    3025             :  * PGPROC with no "lxid" will be considered non-conflicting regardless of any
    3026             :  * lock it holds.  Existing callers don't care about a locker after that
    3027             :  * locker's pg_xact updates complete.  CommitTransaction() clears "lxid" after
    3028             :  * pg_xact updates and before releasing locks.
    3029             :  *
    3030             :  * Note we never include the current xact's vxid in the result array,
    3031             :  * since an xact never blocks itself.
    3032             :  */
    3033             : VirtualTransactionId *
    3034        2700 : GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
    3035             : {
    3036             :     static VirtualTransactionId *vxids;
    3037        2700 :     LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
    3038             :     LockMethod  lockMethodTable;
    3039             :     LOCK       *lock;
    3040             :     LOCKMASK    conflictMask;
    3041             :     dlist_iter  proclock_iter;
    3042             :     PROCLOCK   *proclock;
    3043             :     uint32      hashcode;
    3044             :     LWLock     *partitionLock;
    3045        2700 :     int         count = 0;
    3046        2700 :     int         fast_count = 0;
    3047             : 
    3048        2700 :     if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
    3049           0 :         elog(ERROR, "unrecognized lock method: %d", lockmethodid);
    3050        2700 :     lockMethodTable = LockMethods[lockmethodid];
    3051        2700 :     if (lockmode <= 0 || lockmode > lockMethodTable->numLockModes)
    3052           0 :         elog(ERROR, "unrecognized lock mode: %d", lockmode);
    3053             : 
    3054             :     /*
    3055             :      * Allocate memory to store results, and fill with InvalidVXID.  We only
    3056             :      * need enough space for MaxBackends + max_prepared_xacts + a terminator.
    3057             :      * InHotStandby allocate once in TopMemoryContext.
    3058             :      */
    3059        2700 :     if (InHotStandby)
    3060             :     {
    3061           8 :         if (vxids == NULL)
    3062           2 :             vxids = (VirtualTransactionId *)
    3063           2 :                 MemoryContextAlloc(TopMemoryContext,
    3064             :                                    sizeof(VirtualTransactionId) *
    3065           2 :                                    (MaxBackends + max_prepared_xacts + 1));
    3066             :     }
    3067             :     else
    3068        2692 :         vxids = (VirtualTransactionId *)
    3069        2692 :             palloc0(sizeof(VirtualTransactionId) *
    3070        2692 :                     (MaxBackends + max_prepared_xacts + 1));
    3071             : 
    3072             :     /* Compute hash code and partition lock, and look up conflicting modes. */
    3073        2700 :     hashcode = LockTagHashCode(locktag);
    3074        2700 :     partitionLock = LockHashPartitionLock(hashcode);
    3075        2700 :     conflictMask = lockMethodTable->conflictTab[lockmode];
    3076             : 
    3077             :     /*
    3078             :      * Fast path locks might not have been entered in the primary lock table.
    3079             :      * If the lock we're dealing with could conflict with such a lock, we must
    3080             :      * examine each backend's fast-path array for conflicts.
    3081             :      */
    3082        2700 :     if (ConflictsWithRelationFastPath(locktag, lockmode))
    3083             :     {
    3084             :         int         i;
    3085        2700 :         Oid         relid = locktag->locktag_field2;
    3086             :         VirtualTransactionId vxid;
    3087             : 
    3088             :         /* fast-path group the lock belongs to */
    3089        2700 :         uint32      group = FAST_PATH_REL_GROUP(relid);
    3090             : 
    3091             :         /*
    3092             :          * Iterate over relevant PGPROCs.  Anything held by a prepared
    3093             :          * transaction will have been transferred to the primary lock table,
    3094             :          * so we need not worry about those.  This is all a bit fuzzy, because
    3095             :          * new locks could be taken after we've visited a particular
    3096             :          * partition, but the callers had better be prepared to deal with that
    3097             :          * anyway, since the locks could equally well be taken between the
    3098             :          * time we return the value and the time the caller does something
    3099             :          * with it.
    3100             :          */
    3101      422772 :         for (i = 0; i < ProcGlobal->allProcCount; i++)
    3102             :         {
    3103      420072 :             PGPROC     *proc = &ProcGlobal->allProcs[i];
    3104             :             uint32      j;
    3105             : 
    3106             :             /* A backend never blocks itself */
    3107      420072 :             if (proc == MyProc)
    3108        2700 :                 continue;
    3109             : 
    3110      417372 :             LWLockAcquire(&proc->fpInfoLock, LW_SHARED);
    3111             : 
    3112             :             /*
    3113             :              * If the target backend isn't referencing the same database as
    3114             :              * the lock, then we needn't examine the individual relation IDs
    3115             :              * at all; none of them can be relevant.
    3116             :              *
    3117             :              * See FastPathTransferRelationLocks() for discussion of why we do
    3118             :              * this test after acquiring the lock.
    3119             :              *
    3120             :              * Also skip groups without any registered fast-path locks.
    3121             :              */
    3122      417372 :             if (proc->databaseId != locktag->locktag_field1 ||
    3123      173430 :                 proc->fpLockBits[group] == 0)
    3124             :             {
    3125      416616 :                 LWLockRelease(&proc->fpInfoLock);
    3126      416616 :                 continue;
    3127             :             }
    3128             : 
    3129       12434 :             for (j = 0; j < FP_LOCK_SLOTS_PER_GROUP; j++)
    3130             :             {
    3131             :                 uint32      lockmask;
    3132             : 
    3133             :                 /* index into the whole per-backend array */
    3134       12072 :                 uint32      f = FAST_PATH_SLOT(group, j);
    3135             : 
    3136             :                 /* Look for an allocated slot matching the given relid. */
    3137       12072 :                 if (relid != proc->fpRelId[f])
    3138       11678 :                     continue;
    3139         394 :                 lockmask = FAST_PATH_GET_BITS(proc, f);
    3140         394 :                 if (!lockmask)
    3141           0 :                     continue;
    3142         394 :                 lockmask <<= FAST_PATH_LOCKNUMBER_OFFSET;
    3143             : 
    3144             :                 /*
    3145             :                  * There can only be one entry per relation, so if we found it
    3146             :                  * and it doesn't conflict, we can skip the rest of the slots.
    3147             :                  */
    3148         394 :                 if ((lockmask & conflictMask) == 0)
    3149          10 :                     break;
    3150             : 
    3151             :                 /* Conflict! */
    3152         384 :                 GET_VXID_FROM_PGPROC(vxid, *proc);
    3153             : 
    3154         384 :                 if (VirtualTransactionIdIsValid(vxid))
    3155         384 :                     vxids[count++] = vxid;
    3156             :                 /* else, xact already committed or aborted */
    3157             : 
    3158             :                 /* No need to examine remaining slots. */
    3159         384 :                 break;
    3160             :             }
    3161             : 
    3162         756 :             LWLockRelease(&proc->fpInfoLock);
    3163             :         }
    3164             :     }
    3165             : 
    3166             :     /* Remember how many fast-path conflicts we found. */
    3167        2700 :     fast_count = count;
    3168             : 
    3169             :     /*
    3170             :      * Look up the lock object matching the tag.
    3171             :      */
    3172        2700 :     LWLockAcquire(partitionLock, LW_SHARED);
    3173             : 
    3174        2700 :     lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash,
    3175             :                                                 locktag,
    3176             :                                                 hashcode,
    3177             :                                                 HASH_FIND,
    3178             :                                                 NULL);
    3179        2700 :     if (!lock)
    3180             :     {
    3181             :         /*
    3182             :          * If the lock object doesn't exist, there is nothing holding a lock
    3183             :          * on this lockable object.
    3184             :          */
    3185         140 :         LWLockRelease(partitionLock);
    3186         140 :         vxids[count].procNumber = INVALID_PROC_NUMBER;
    3187         140 :         vxids[count].localTransactionId = InvalidLocalTransactionId;
    3188         140 :         if (countp)
    3189           0 :             *countp = count;
    3190         140 :         return vxids;
    3191             :     }
    3192             : 
    3193             :     /*
    3194             :      * Examine each existing holder (or awaiter) of the lock.
    3195             :      */
    3196        5162 :     dlist_foreach(proclock_iter, &lock->procLocks)
    3197             :     {
    3198        2602 :         proclock = dlist_container(PROCLOCK, lockLink, proclock_iter.cur);
    3199             : 
    3200        2602 :         if (conflictMask & proclock->holdMask)
    3201             :         {
    3202        2594 :             PGPROC     *proc = proclock->tag.myProc;
    3203             : 
    3204             :             /* A backend never blocks itself */
    3205        2594 :             if (proc != MyProc)
    3206             :             {
    3207             :                 VirtualTransactionId vxid;
    3208             : 
    3209          42 :                 GET_VXID_FROM_PGPROC(vxid, *proc);
    3210             : 
    3211          42 :                 if (VirtualTransactionIdIsValid(vxid))
    3212             :                 {
    3213             :                     int         i;
    3214             : 
    3215             :                     /* Avoid duplicate entries. */
    3216          72 :                     for (i = 0; i < fast_count; ++i)
    3217          32 :                         if (VirtualTransactionIdEquals(vxids[i], vxid))
    3218           2 :                             break;
    3219          42 :                     if (i >= fast_count)
    3220          40 :                         vxids[count++] = vxid;
    3221             :                 }
    3222             :                 /* else, xact already committed or aborted */
    3223             :             }
    3224             :         }
    3225             :     }
    3226             : 
    3227        2560 :     LWLockRelease(partitionLock);
    3228             : 
    3229        2560 :     if (count > MaxBackends + max_prepared_xacts)    /* should never happen */
    3230           0 :         elog(PANIC, "too many conflicting locks found");
    3231             : 
    3232        2560 :     vxids[count].procNumber = INVALID_PROC_NUMBER;
    3233        2560 :     vxids[count].localTransactionId = InvalidLocalTransactionId;
    3234        2560 :     if (countp)
    3235        2554 :         *countp = count;
    3236        2560 :     return vxids;
    3237             : }
    3238             : 
    3239             : /*
    3240             :  * Find a lock in the shared lock table and release it.  It is the caller's
    3241             :  * responsibility to verify that this is a sane thing to do.  (For example, it
    3242             :  * would be bad to release a lock here if there might still be a LOCALLOCK
    3243             :  * object with pointers to it.)
    3244             :  *
    3245             :  * We currently use this in two situations: first, to release locks held by
    3246             :  * prepared transactions on commit (see lock_twophase_postcommit); and second,
    3247             :  * to release locks taken via the fast-path, transferred to the main hash
    3248             :  * table, and then released (see LockReleaseAll).
    3249             :  */
    3250             : static void
    3251        4158 : LockRefindAndRelease(LockMethod lockMethodTable, PGPROC *proc,
    3252             :                      LOCKTAG *locktag, LOCKMODE lockmode,
    3253             :                      bool decrement_strong_lock_count)
    3254             : {
    3255             :     LOCK       *lock;
    3256             :     PROCLOCK   *proclock;
    3257             :     PROCLOCKTAG proclocktag;
    3258             :     uint32      hashcode;
    3259             :     uint32      proclock_hashcode;
    3260             :     LWLock     *partitionLock;
    3261             :     bool        wakeupNeeded;
    3262             : 
    3263        4158 :     hashcode = LockTagHashCode(locktag);
    3264        4158 :     partitionLock = LockHashPartitionLock(hashcode);
    3265             : 
    3266        4158 :     LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    3267             : 
    3268             :     /*
    3269             :      * Re-find the lock object (it had better be there).
    3270             :      */
    3271        4158 :     lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash,
    3272             :                                                 locktag,
    3273             :                                                 hashcode,
    3274             :                                                 HASH_FIND,
    3275             :                                                 NULL);
    3276        4158 :     if (!lock)
    3277           0 :         elog(PANIC, "failed to re-find shared lock object");
    3278             : 
    3279             :     /*
    3280             :      * Re-find the proclock object (ditto).
    3281             :      */
    3282        4158 :     proclocktag.myLock = lock;
    3283        4158 :     proclocktag.myProc = proc;
    3284             : 
    3285        4158 :     proclock_hashcode = ProcLockHashCode(&proclocktag, hashcode);
    3286             : 
    3287        4158 :     proclock = (PROCLOCK *) hash_search_with_hash_value(LockMethodProcLockHash,
    3288             :                                                         &proclocktag,
    3289             :                                                         proclock_hashcode,
    3290             :                                                         HASH_FIND,
    3291             :                                                         NULL);
    3292        4158 :     if (!proclock)
    3293           0 :         elog(PANIC, "failed to re-find shared proclock object");
    3294             : 
    3295             :     /*
    3296             :      * Double-check that we are actually holding a lock of the type we want to
    3297             :      * release.
    3298             :      */
    3299        4158 :     if (!(proclock->holdMask & LOCKBIT_ON(lockmode)))
    3300             :     {
    3301             :         PROCLOCK_PRINT("lock_twophase_postcommit: WRONGTYPE", proclock);
    3302           0 :         LWLockRelease(partitionLock);
    3303           0 :         elog(WARNING, "you don't own a lock of type %s",
    3304             :              lockMethodTable->lockModeNames[lockmode]);
    3305           0 :         return;
    3306             :     }
    3307             : 
    3308             :     /*
    3309             :      * Do the releasing.  CleanUpLock will waken any now-wakable waiters.
    3310             :      */
    3311        4158 :     wakeupNeeded = UnGrantLock(lock, lockmode, proclock, lockMethodTable);
    3312             : 
    3313        4158 :     CleanUpLock(lock, proclock,
    3314             :                 lockMethodTable, hashcode,
    3315             :                 wakeupNeeded);
    3316             : 
    3317        4158 :     LWLockRelease(partitionLock);
    3318             : 
    3319             :     /*
    3320             :      * Decrement strong lock count.  This logic is needed only for 2PC.
    3321             :      */
    3322        4158 :     if (decrement_strong_lock_count
    3323        1510 :         && ConflictsWithRelationFastPath(locktag, lockmode))
    3324             :     {
    3325         162 :         uint32      fasthashcode = FastPathStrongLockHashPartition(hashcode);
    3326             : 
    3327         162 :         SpinLockAcquire(&FastPathStrongRelationLocks->mutex);
    3328             :         Assert(FastPathStrongRelationLocks->count[fasthashcode] > 0);
    3329         162 :         FastPathStrongRelationLocks->count[fasthashcode]--;
    3330         162 :         SpinLockRelease(&FastPathStrongRelationLocks->mutex);
    3331             :     }
    3332             : }
    3333             : 
    3334             : /*
    3335             :  * CheckForSessionAndXactLocks
    3336             :  *      Check to see if transaction holds both session-level and xact-level
    3337             :  *      locks on the same object; if so, throw an error.
    3338             :  *
    3339             :  * If we have both session- and transaction-level locks on the same object,
    3340             :  * PREPARE TRANSACTION must fail.  This should never happen with regular
    3341             :  * locks, since we only take those at session level in some special operations
    3342             :  * like VACUUM.  It's possible to hit this with advisory locks, though.
    3343             :  *
    3344             :  * It would be nice if we could keep the session hold and give away the
    3345             :  * transactional hold to the prepared xact.  However, that would require two
    3346             :  * PROCLOCK objects, and we cannot be sure that another PROCLOCK will be
    3347             :  * available when it comes time for PostPrepare_Locks to do the deed.
    3348             :  * So for now, we error out while we can still do so safely.
    3349             :  *
    3350             :  * Since the LOCALLOCK table stores a separate entry for each lockmode,
    3351             :  * we can't implement this check by examining LOCALLOCK entries in isolation.
    3352             :  * We must build a transient hashtable that is indexed by locktag only.
    3353             :  */
    3354             : static void
    3355         604 : CheckForSessionAndXactLocks(void)
    3356             : {
    3357             :     typedef struct
    3358             :     {
    3359             :         LOCKTAG     lock;       /* identifies the lockable object */
    3360             :         bool        sessLock;   /* is any lockmode held at session level? */
    3361             :         bool        xactLock;   /* is any lockmode held at xact level? */
    3362             :     } PerLockTagEntry;
    3363             : 
    3364             :     HASHCTL     hash_ctl;
    3365             :     HTAB       *lockhtab;
    3366             :     HASH_SEQ_STATUS status;
    3367             :     LOCALLOCK  *locallock;
    3368             : 
    3369             :     /* Create a local hash table keyed by LOCKTAG only */
    3370         604 :     hash_ctl.keysize = sizeof(LOCKTAG);
    3371         604 :     hash_ctl.entrysize = sizeof(PerLockTagEntry);
    3372         604 :     hash_ctl.hcxt = CurrentMemoryContext;
    3373             : 
    3374         604 :     lockhtab = hash_create("CheckForSessionAndXactLocks table",
    3375             :                            256, /* arbitrary initial size */
    3376             :                            &hash_ctl,
    3377             :                            HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
    3378             : 
    3379             :     /* Scan local lock table to find entries for each LOCKTAG */
    3380         604 :     hash_seq_init(&status, LockMethodLocalHash);
    3381             : 
    3382        2084 :     while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
    3383             :     {
    3384        1484 :         LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
    3385             :         PerLockTagEntry *hentry;
    3386             :         bool        found;
    3387             :         int         i;
    3388             : 
    3389             :         /*
    3390             :          * Ignore VXID locks.  We don't want those to be held by prepared
    3391             :          * transactions, since they aren't meaningful after a restart.
    3392             :          */
    3393        1484 :         if (locallock->tag.lock.locktag_type == LOCKTAG_VIRTUALTRANSACTION)
    3394           0 :             continue;
    3395             : 
    3396             :         /* Ignore it if we don't actually hold the lock */
    3397        1484 :         if (locallock->nLocks <= 0)
    3398           0 :             continue;
    3399             : 
    3400             :         /* Otherwise, find or make an entry in lockhtab */
    3401        1484 :         hentry = (PerLockTagEntry *) hash_search(lockhtab,
    3402        1484 :                                                  &locallock->tag.lock,
    3403             :                                                  HASH_ENTER, &found);
    3404        1484 :         if (!found)             /* initialize, if newly created */
    3405        1366 :             hentry->sessLock = hentry->xactLock = false;
    3406             : 
    3407             :         /* Scan to see if we hold lock at session or xact level or both */
    3408        2968 :         for (i = locallock->numLockOwners - 1; i >= 0; i--)
    3409             :         {
    3410        1484 :             if (lockOwners[i].owner == NULL)
    3411          18 :                 hentry->sessLock = true;
    3412             :             else
    3413        1466 :                 hentry->xactLock = true;
    3414             :         }
    3415             : 
    3416             :         /*
    3417             :          * We can throw error immediately when we see both types of locks; no
    3418             :          * need to wait around to see if there are more violations.
    3419             :          */
    3420        1484 :         if (hentry->sessLock && hentry->xactLock)
    3421           4 :             ereport(ERROR,
    3422             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3423             :                      errmsg("cannot PREPARE while holding both session-level and transaction-level locks on the same object")));
    3424             :     }
    3425             : 
    3426             :     /* Success, so clean up */
    3427         600 :     hash_destroy(lockhtab);
    3428         600 : }
    3429             : 
    3430             : /*
    3431             :  * AtPrepare_Locks
    3432             :  *      Do the preparatory work for a PREPARE: make 2PC state file records
    3433             :  *      for all locks currently held.
    3434             :  *
    3435             :  * Session-level locks are ignored, as are VXID locks.
    3436             :  *
    3437             :  * For the most part, we don't need to touch shared memory for this ---
    3438             :  * all the necessary state information is in the locallock table.
    3439             :  * Fast-path locks are an exception, however: we move any such locks to
    3440             :  * the main table before allowing PREPARE TRANSACTION to succeed.
    3441             :  */
    3442             : void
    3443         604 : AtPrepare_Locks(void)
    3444             : {
    3445             :     HASH_SEQ_STATUS status;
    3446             :     LOCALLOCK  *locallock;
    3447             : 
    3448             :     /* First, verify there aren't locks of both xact and session level */
    3449         604 :     CheckForSessionAndXactLocks();
    3450             : 
    3451             :     /* Now do the per-locallock cleanup work */
    3452         600 :     hash_seq_init(&status, LockMethodLocalHash);
    3453             : 
    3454        2074 :     while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
    3455             :     {
    3456             :         TwoPhaseLockRecord record;
    3457        1474 :         LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
    3458             :         bool        haveSessionLock;
    3459             :         bool        haveXactLock;
    3460             :         int         i;
    3461             : 
    3462             :         /*
    3463             :          * Ignore VXID locks.  We don't want those to be held by prepared
    3464             :          * transactions, since they aren't meaningful after a restart.
    3465             :          */
    3466        1474 :         if (locallock->tag.lock.locktag_type == LOCKTAG_VIRTUALTRANSACTION)
    3467          14 :             continue;
    3468             : 
    3469             :         /* Ignore it if we don't actually hold the lock */
    3470        1474 :         if (locallock->nLocks <= 0)
    3471           0 :             continue;
    3472             : 
    3473             :         /* Scan to see whether we hold it at session or transaction level */
    3474        1474 :         haveSessionLock = haveXactLock = false;
    3475        2948 :         for (i = locallock->numLockOwners - 1; i >= 0; i--)
    3476             :         {
    3477        1474 :             if (lockOwners[i].owner == NULL)
    3478          14 :                 haveSessionLock = true;
    3479             :             else
    3480        1460 :                 haveXactLock = true;
    3481             :         }
    3482             : 
    3483             :         /* Ignore it if we have only session lock */
    3484        1474 :         if (!haveXactLock)
    3485          14 :             continue;
    3486             : 
    3487             :         /* This can't happen, because we already checked it */
    3488        1460 :         if (haveSessionLock)
    3489           0 :             ereport(ERROR,
    3490             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3491             :                      errmsg("cannot PREPARE while holding both session-level and transaction-level locks on the same object")));
    3492             : 
    3493             :         /*
    3494             :          * If the local lock was taken via the fast-path, we need to move it
    3495             :          * to the primary lock table, or just get a pointer to the existing
    3496             :          * primary lock table entry if by chance it's already been
    3497             :          * transferred.
    3498             :          */
    3499        1460 :         if (locallock->proclock == NULL)
    3500             :         {
    3501         618 :             locallock->proclock = FastPathGetRelationLockEntry(locallock);
    3502         618 :             locallock->lock = locallock->proclock->tag.myLock;
    3503             :         }
    3504             : 
    3505             :         /*
    3506             :          * Arrange to not release any strong lock count held by this lock
    3507             :          * entry.  We must retain the count until the prepared transaction is
    3508             :          * committed or rolled back.
    3509             :          */
    3510        1460 :         locallock->holdsStrongLockCount = false;
    3511             : 
    3512             :         /*
    3513             :          * Create a 2PC record.
    3514             :          */
    3515        1460 :         memcpy(&(record.locktag), &(locallock->tag.lock), sizeof(LOCKTAG));
    3516        1460 :         record.lockmode = locallock->tag.mode;
    3517             : 
    3518        1460 :         RegisterTwoPhaseRecord(TWOPHASE_RM_LOCK_ID, 0,
    3519             :                                &record, sizeof(TwoPhaseLockRecord));
    3520             :     }
    3521         600 : }
    3522             : 
    3523             : /*
    3524             :  * PostPrepare_Locks
    3525             :  *      Clean up after successful PREPARE
    3526             :  *
    3527             :  * Here, we want to transfer ownership of our locks to a dummy PGPROC
    3528             :  * that's now associated with the prepared transaction, and we want to
    3529             :  * clean out the corresponding entries in the LOCALLOCK table.
    3530             :  *
    3531             :  * Note: by removing the LOCALLOCK entries, we are leaving dangling
    3532             :  * pointers in the transaction's resource owner.  This is OK at the
    3533             :  * moment since resowner.c doesn't try to free locks retail at a toplevel
    3534             :  * transaction commit or abort.  We could alternatively zero out nLocks
    3535             :  * and leave the LOCALLOCK entries to be garbage-collected by LockReleaseAll,
    3536             :  * but that probably costs more cycles.
    3537             :  */
    3538             : void
    3539         600 : PostPrepare_Locks(TransactionId xid)
    3540             : {
    3541         600 :     PGPROC     *newproc = TwoPhaseGetDummyProc(xid, false);
    3542             :     HASH_SEQ_STATUS status;
    3543             :     LOCALLOCK  *locallock;
    3544             :     LOCK       *lock;
    3545             :     PROCLOCK   *proclock;
    3546             :     PROCLOCKTAG proclocktag;
    3547             :     int         partition;
    3548             : 
    3549             :     /* Can't prepare a lock group follower. */
    3550             :     Assert(MyProc->lockGroupLeader == NULL ||
    3551             :            MyProc->lockGroupLeader == MyProc);
    3552             : 
    3553             :     /* This is a critical section: any error means big trouble */
    3554         600 :     START_CRIT_SECTION();
    3555             : 
    3556             :     /*
    3557             :      * First we run through the locallock table and get rid of unwanted
    3558             :      * entries, then we scan the process's proclocks and transfer them to the
    3559             :      * target proc.
    3560             :      *
    3561             :      * We do this separately because we may have multiple locallock entries
    3562             :      * pointing to the same proclock, and we daren't end up with any dangling
    3563             :      * pointers.
    3564             :      */
    3565         600 :     hash_seq_init(&status, LockMethodLocalHash);
    3566             : 
    3567        2074 :     while ((locallock = (LOCALLOCK *) hash_seq_search(&status)) != NULL)
    3568             :     {
    3569        1474 :         LOCALLOCKOWNER *lockOwners = locallock->lockOwners;
    3570             :         bool        haveSessionLock;
    3571             :         bool        haveXactLock;
    3572             :         int         i;
    3573             : 
    3574        1474 :         if (locallock->proclock == NULL || locallock->lock == NULL)
    3575             :         {
    3576             :             /*
    3577             :              * We must've run out of shared memory while trying to set up this
    3578             :              * lock.  Just forget the local entry.
    3579             :              */
    3580             :             Assert(locallock->nLocks == 0);
    3581           0 :             RemoveLocalLock(locallock);
    3582           0 :             continue;
    3583             :         }
    3584             : 
    3585             :         /* Ignore VXID locks */
    3586        1474 :         if (locallock->tag.lock.locktag_type == LOCKTAG_VIRTUALTRANSACTION)
    3587           0 :             continue;
    3588             : 
    3589             :         /* Scan to see whether we hold it at session or transaction level */
    3590        1474 :         haveSessionLock = haveXactLock = false;
    3591        2948 :         for (i = locallock->numLockOwners - 1; i >= 0; i--)
    3592             :         {
    3593        1474 :             if (lockOwners[i].owner == NULL)
    3594          14 :                 haveSessionLock = true;
    3595             :             else
    3596        1460 :                 haveXactLock = true;
    3597             :         }
    3598             : 
    3599             :         /* Ignore it if we have only session lock */
    3600        1474 :         if (!haveXactLock)
    3601          14 :             continue;
    3602             : 
    3603             :         /* This can't happen, because we already checked it */
    3604        1460 :         if (haveSessionLock)
    3605           0 :             ereport(PANIC,
    3606             :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
    3607             :                      errmsg("cannot PREPARE while holding both session-level and transaction-level locks on the same object")));
    3608             : 
    3609             :         /* Mark the proclock to show we need to release this lockmode */
    3610        1460 :         if (locallock->nLocks > 0)
    3611        1460 :             locallock->proclock->releaseMask |= LOCKBIT_ON(locallock->tag.mode);
    3612             : 
    3613             :         /* And remove the locallock hashtable entry */
    3614        1460 :         RemoveLocalLock(locallock);
    3615             :     }
    3616             : 
    3617             :     /*
    3618             :      * Now, scan each lock partition separately.
    3619             :      */
    3620       10200 :     for (partition = 0; partition < NUM_LOCK_PARTITIONS; partition++)
    3621             :     {
    3622             :         LWLock     *partitionLock;
    3623        9600 :         dlist_head *procLocks = &(MyProc->myProcLocks[partition]);
    3624             :         dlist_mutable_iter proclock_iter;
    3625             : 
    3626        9600 :         partitionLock = LockHashPartitionLockByIndex(partition);
    3627             : 
    3628             :         /*
    3629             :          * If the proclock list for this partition is empty, we can skip
    3630             :          * acquiring the partition lock.  This optimization is safer than the
    3631             :          * situation in LockReleaseAll, because we got rid of any fast-path
    3632             :          * locks during AtPrepare_Locks, so there cannot be any case where
    3633             :          * another backend is adding something to our lists now.  For safety,
    3634             :          * though, we code this the same way as in LockReleaseAll.
    3635             :          */
    3636        9600 :         if (dlist_is_empty(procLocks))
    3637        8228 :             continue;           /* needn't examine this partition */
    3638             : 
    3639        1372 :         LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    3640             : 
    3641        2792 :         dlist_foreach_modify(proclock_iter, procLocks)
    3642             :         {
    3643        1420 :             proclock = dlist_container(PROCLOCK, procLink, proclock_iter.cur);
    3644             : 
    3645             :             Assert(proclock->tag.myProc == MyProc);
    3646             : 
    3647        1420 :             lock = proclock->tag.myLock;
    3648             : 
    3649             :             /* Ignore VXID locks */
    3650        1420 :             if (lock->tag.locktag_type == LOCKTAG_VIRTUALTRANSACTION)
    3651          60 :                 continue;
    3652             : 
    3653             :             PROCLOCK_PRINT("PostPrepare_Locks", proclock);
    3654             :             LOCK_PRINT("PostPrepare_Locks", lock, 0);
    3655             :             Assert(lock->nRequested >= 0);
    3656             :             Assert(lock->nGranted >= 0);
    3657             :             Assert(lock->nGranted <= lock->nRequested);
    3658             :             Assert((proclock->holdMask & ~lock->grantMask) == 0);
    3659             : 
    3660             :             /* Ignore it if nothing to release (must be a session lock) */
    3661        1360 :             if (proclock->releaseMask == 0)
    3662          14 :                 continue;
    3663             : 
    3664             :             /* Else we should be releasing all locks */
    3665        1346 :             if (proclock->releaseMask != proclock->holdMask)
    3666           0 :                 elog(PANIC, "we seem to have dropped a bit somewhere");
    3667             : 
    3668             :             /*
    3669             :              * We cannot simply modify proclock->tag.myProc to reassign
    3670             :              * ownership of the lock, because that's part of the hash key and
    3671             :              * the proclock would then be in the wrong hash chain.  Instead
    3672             :              * use hash_update_hash_key.  (We used to create a new hash entry,
    3673             :              * but that risks out-of-memory failure if other processes are
    3674             :              * busy making proclocks too.)  We must unlink the proclock from
    3675             :              * our procLink chain and put it into the new proc's chain, too.
    3676             :              *
    3677             :              * Note: the updated proclock hash key will still belong to the
    3678             :              * same hash partition, cf proclock_hash().  So the partition lock
    3679             :              * we already hold is sufficient for this.
    3680             :              */
    3681        1346 :             dlist_delete(&proclock->procLink);
    3682             : 
    3683             :             /*
    3684             :              * Create the new hash key for the proclock.
    3685             :              */
    3686        1346 :             proclocktag.myLock = lock;
    3687        1346 :             proclocktag.myProc = newproc;
    3688             : 
    3689             :             /*
    3690             :              * Update groupLeader pointer to point to the new proc.  (We'd
    3691             :              * better not be a member of somebody else's lock group!)
    3692             :              */
    3693             :             Assert(proclock->groupLeader == proclock->tag.myProc);
    3694        1346 :             proclock->groupLeader = newproc;
    3695             : 
    3696             :             /*
    3697             :              * Update the proclock.  We should not find any existing entry for
    3698             :              * the same hash key, since there can be only one entry for any
    3699             :              * given lock with my own proc.
    3700             :              */
    3701        1346 :             if (!hash_update_hash_key(LockMethodProcLockHash,
    3702             :                                       proclock,
    3703             :                                       &proclocktag))
    3704           0 :                 elog(PANIC, "duplicate entry found while reassigning a prepared transaction's locks");
    3705             : 
    3706             :             /* Re-link into the new proc's proclock list */
    3707        1346 :             dlist_push_tail(&newproc->myProcLocks[partition], &proclock->procLink);
    3708             : 
    3709             :             PROCLOCK_PRINT("PostPrepare_Locks: updated", proclock);
    3710             :         }                       /* loop over PROCLOCKs within this partition */
    3711             : 
    3712        1372 :         LWLockRelease(partitionLock);
    3713             :     }                           /* loop over partitions */
    3714             : 
    3715         600 :     END_CRIT_SECTION();
    3716         600 : }
    3717             : 
    3718             : 
    3719             : /*
    3720             :  * Estimate shared-memory space used for lock tables
    3721             :  */
    3722             : Size
    3723        3794 : LockManagerShmemSize(void)
    3724             : {
    3725        3794 :     Size        size = 0;
    3726             :     long        max_table_size;
    3727             : 
    3728             :     /* lock hash table */
    3729        3794 :     max_table_size = NLOCKENTS();
    3730        3794 :     size = add_size(size, hash_estimate_size(max_table_size, sizeof(LOCK)));
    3731             : 
    3732             :     /* proclock hash table */
    3733        3794 :     max_table_size *= 2;
    3734        3794 :     size = add_size(size, hash_estimate_size(max_table_size, sizeof(PROCLOCK)));
    3735             : 
    3736             :     /*
    3737             :      * Since NLOCKENTS is only an estimate, add 10% safety margin.
    3738             :      */
    3739        3794 :     size = add_size(size, size / 10);
    3740             : 
    3741        3794 :     return size;
    3742             : }
    3743             : 
    3744             : /*
    3745             :  * GetLockStatusData - Return a summary of the lock manager's internal
    3746             :  * status, for use in a user-level reporting function.
    3747             :  *
    3748             :  * The return data consists of an array of LockInstanceData objects,
    3749             :  * which are a lightly abstracted version of the PROCLOCK data structures,
    3750             :  * i.e. there is one entry for each unique lock and interested PGPROC.
    3751             :  * It is the caller's responsibility to match up related items (such as
    3752             :  * references to the same lockable object or PGPROC) if wanted.
    3753             :  *
    3754             :  * The design goal is to hold the LWLocks for as short a time as possible;
    3755             :  * thus, this function simply makes a copy of the necessary data and releases
    3756             :  * the locks, allowing the caller to contemplate and format the data for as
    3757             :  * long as it pleases.
    3758             :  */
    3759             : LockData *
    3760         736 : GetLockStatusData(void)
    3761             : {
    3762             :     LockData   *data;
    3763             :     PROCLOCK   *proclock;
    3764             :     HASH_SEQ_STATUS seqstat;
    3765             :     int         els;
    3766             :     int         el;
    3767             :     int         i;
    3768             : 
    3769         736 :     data = (LockData *) palloc(sizeof(LockData));
    3770             : 
    3771             :     /* Guess how much space we'll need. */
    3772         736 :     els = MaxBackends;
    3773         736 :     el = 0;
    3774         736 :     data->locks = (LockInstanceData *) palloc(sizeof(LockInstanceData) * els);
    3775             : 
    3776             :     /*
    3777             :      * First, we iterate through the per-backend fast-path arrays, locking
    3778             :      * them one at a time.  This might produce an inconsistent picture of the
    3779             :      * system state, but taking all of those LWLocks at the same time seems
    3780             :      * impractical (in particular, note MAX_SIMUL_LWLOCKS).  It shouldn't
    3781             :      * matter too much, because none of these locks can be involved in lock
    3782             :      * conflicts anyway - anything that might must be present in the main lock
    3783             :      * table.  (For the same reason, we don't sweat about making leaderPid
    3784             :      * completely valid.  We cannot safely dereference another backend's
    3785             :      * lockGroupLeader field without holding all lock partition locks, and
    3786             :      * it's not worth that.)
    3787             :      */
    3788      112206 :     for (i = 0; i < ProcGlobal->allProcCount; ++i)
    3789             :     {
    3790      111470 :         PGPROC     *proc = &ProcGlobal->allProcs[i];
    3791             : 
    3792             :         /* Skip backends with pid=0, as they don't hold fast-path locks */
    3793      111470 :         if (proc->pid == 0)
    3794      102398 :             continue;
    3795             : 
    3796        9072 :         LWLockAcquire(&proc->fpInfoLock, LW_SHARED);
    3797             : 
    3798       45360 :         for (uint32 g = 0; g < FastPathLockGroupsPerBackend; g++)
    3799             :         {
    3800             :             /* Skip groups without registered fast-path locks */
    3801       36288 :             if (proc->fpLockBits[g] == 0)
    3802       31466 :                 continue;
    3803             : 
    3804       81974 :             for (int j = 0; j < FP_LOCK_SLOTS_PER_GROUP; j++)
    3805             :             {
    3806             :                 LockInstanceData *instance;
    3807       77152 :                 uint32      f = FAST_PATH_SLOT(g, j);
    3808       77152 :                 uint32      lockbits = FAST_PATH_GET_BITS(proc, f);
    3809             : 
    3810             :                 /* Skip unallocated slots */
    3811       77152 :                 if (!lockbits)
    3812       69620 :                     continue;
    3813             : 
    3814        7532 :                 if (el >= els)
    3815             :                 {
    3816          20 :                     els += MaxBackends;
    3817          20 :                     data->locks = (LockInstanceData *)
    3818          20 :                         repalloc(data->locks, sizeof(LockInstanceData) * els);
    3819             :                 }
    3820             : 
    3821        7532 :                 instance = &data->locks[el];
    3822        7532 :                 SET_LOCKTAG_RELATION(instance->locktag, proc->databaseId,
    3823             :                                      proc->fpRelId[f]);
    3824        7532 :                 instance->holdMask = lockbits << FAST_PATH_LOCKNUMBER_OFFSET;
    3825        7532 :                 instance->waitLockMode = NoLock;
    3826        7532 :                 instance->vxid.procNumber = proc->vxid.procNumber;
    3827        7532 :                 instance->vxid.localTransactionId = proc->vxid.lxid;
    3828        7532 :                 instance->pid = proc->pid;
    3829        7532 :                 instance->leaderPid = proc->pid;
    3830        7532 :                 instance->fastpath = true;
    3831             : 
    3832             :                 /*
    3833             :                  * Successfully taking fast path lock means there were no
    3834             :                  * conflicting locks.
    3835             :                  */
    3836        7532 :                 instance->waitStart = 0;
    3837             : 
    3838        7532 :                 el++;
    3839             :             }
    3840             :         }
    3841             : 
    3842        9072 :         if (proc->fpVXIDLock)
    3843             :         {
    3844             :             VirtualTransactionId vxid;
    3845             :             LockInstanceData *instance;
    3846             : 
    3847        2532 :             if (el >= els)
    3848             :             {
    3849           4 :                 els += MaxBackends;
    3850           4 :                 data->locks = (LockInstanceData *)
    3851           4 :                     repalloc(data->locks, sizeof(LockInstanceData) * els);
    3852             :             }
    3853             : 
    3854        2532 :             vxid.procNumber = proc->vxid.procNumber;
    3855        2532 :             vxid.localTransactionId = proc->fpLocalTransactionId;
    3856             : 
    3857        2532 :             instance = &data->locks[el];
    3858        2532 :             SET_LOCKTAG_VIRTUALTRANSACTION(instance->locktag, vxid);
    3859        2532 :             instance->holdMask = LOCKBIT_ON(ExclusiveLock);
    3860        2532 :             instance->waitLockMode = NoLock;
    3861        2532 :             instance->vxid.procNumber = proc->vxid.procNumber;
    3862        2532 :             instance->vxid.localTransactionId = proc->vxid.lxid;
    3863        2532 :             instance->pid = proc->pid;
    3864        2532 :             instance->leaderPid = proc->pid;
    3865        2532 :             instance->fastpath = true;
    3866        2532 :             instance->waitStart = 0;
    3867             : 
    3868        2532 :             el++;
    3869             :         }
    3870             : 
    3871        9072 :         LWLockRelease(&proc->fpInfoLock);
    3872             :     }
    3873             : 
    3874             :     /*
    3875             :      * Next, acquire lock on the entire shared lock data structure.  We do
    3876             :      * this so that, at least for locks in the primary lock table, the state
    3877             :      * will be self-consistent.
    3878             :      *
    3879             :      * Since this is a read-only operation, we take shared instead of
    3880             :      * exclusive lock.  There's not a whole lot of point to this, because all
    3881             :      * the normal operations require exclusive lock, but it doesn't hurt
    3882             :      * anything either. It will at least allow two backends to do
    3883             :      * GetLockStatusData in parallel.
    3884             :      *
    3885             :      * Must grab LWLocks in partition-number order to avoid LWLock deadlock.
    3886             :      */
    3887       12512 :     for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
    3888       11776 :         LWLockAcquire(LockHashPartitionLockByIndex(i), LW_SHARED);
    3889             : 
    3890             :     /* Now we can safely count the number of proclocks */
    3891         736 :     data->nelements = el + hash_get_num_entries(LockMethodProcLockHash);
    3892         736 :     if (data->nelements > els)
    3893             :     {
    3894          48 :         els = data->nelements;
    3895          48 :         data->locks = (LockInstanceData *)
    3896          48 :             repalloc(data->locks, sizeof(LockInstanceData) * els);
    3897             :     }
    3898             : 
    3899             :     /* Now scan the tables to copy the data */
    3900         736 :     hash_seq_init(&seqstat, LockMethodProcLockHash);
    3901             : 
    3902        6130 :     while ((proclock = (PROCLOCK *) hash_seq_search(&seqstat)))
    3903             :     {
    3904        5394 :         PGPROC     *proc = proclock->tag.myProc;
    3905        5394 :         LOCK       *lock = proclock->tag.myLock;
    3906        5394 :         LockInstanceData *instance = &data->locks[el];
    3907             : 
    3908        5394 :         memcpy(&instance->locktag, &lock->tag, sizeof(LOCKTAG));
    3909        5394 :         instance->holdMask = proclock->holdMask;
    3910        5394 :         if (proc->waitLock == proclock->tag.myLock)
    3911          18 :             instance->waitLockMode = proc->waitLockMode;
    3912             :         else
    3913        5376 :             instance->waitLockMode = NoLock;
    3914        5394 :         instance->vxid.procNumber = proc->vxid.procNumber;
    3915        5394 :         instance->vxid.localTransactionId = proc->vxid.lxid;
    3916        5394 :         instance->pid = proc->pid;
    3917        5394 :         instance->leaderPid = proclock->groupLeader->pid;
    3918        5394 :         instance->fastpath = false;
    3919        5394 :         instance->waitStart = (TimestampTz) pg_atomic_read_u64(&proc->waitStart);
    3920             : 
    3921        5394 :         el++;
    3922             :     }
    3923             : 
    3924             :     /*
    3925             :      * And release locks.  We do this in reverse order for two reasons: (1)
    3926             :      * Anyone else who needs more than one of the locks will be trying to lock
    3927             :      * them in increasing order; we don't want to release the other process
    3928             :      * until it can get all the locks it needs. (2) This avoids O(N^2)
    3929             :      * behavior inside LWLockRelease.
    3930             :      */
    3931       12512 :     for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
    3932       11776 :         LWLockRelease(LockHashPartitionLockByIndex(i));
    3933             : 
    3934             :     Assert(el == data->nelements);
    3935             : 
    3936         736 :     return data;
    3937             : }
    3938             : 
    3939             : /*
    3940             :  * GetBlockerStatusData - Return a summary of the lock manager's state
    3941             :  * concerning locks that are blocking the specified PID or any member of
    3942             :  * the PID's lock group, for use in a user-level reporting function.
    3943             :  *
    3944             :  * For each PID within the lock group that is awaiting some heavyweight lock,
    3945             :  * the return data includes an array of LockInstanceData objects, which are
    3946             :  * the same data structure used by GetLockStatusData; but unlike that function,
    3947             :  * this one reports only the PROCLOCKs associated with the lock that that PID
    3948             :  * is blocked on.  (Hence, all the locktags should be the same for any one
    3949             :  * blocked PID.)  In addition, we return an array of the PIDs of those backends
    3950             :  * that are ahead of the blocked PID in the lock's wait queue.  These can be
    3951             :  * compared with the PIDs in the LockInstanceData objects to determine which
    3952             :  * waiters are ahead of or behind the blocked PID in the queue.
    3953             :  *
    3954             :  * If blocked_pid isn't a valid backend PID or nothing in its lock group is
    3955             :  * waiting on any heavyweight lock, return empty arrays.
    3956             :  *
    3957             :  * The design goal is to hold the LWLocks for as short a time as possible;
    3958             :  * thus, this function simply makes a copy of the necessary data and releases
    3959             :  * the locks, allowing the caller to contemplate and format the data for as
    3960             :  * long as it pleases.
    3961             :  */
    3962             : BlockedProcsData *
    3963        3126 : GetBlockerStatusData(int blocked_pid)
    3964             : {
    3965             :     BlockedProcsData *data;
    3966             :     PGPROC     *proc;
    3967             :     int         i;
    3968             : 
    3969        3126 :     data = (BlockedProcsData *) palloc(sizeof(BlockedProcsData));
    3970             : 
    3971             :     /*
    3972             :      * Guess how much space we'll need, and preallocate.  Most of the time
    3973             :      * this will avoid needing to do repalloc while holding the LWLocks.  (We
    3974             :      * assume, but check with an Assert, that MaxBackends is enough entries
    3975             :      * for the procs[] array; the other two could need enlargement, though.)
    3976             :      */
    3977        3126 :     data->nprocs = data->nlocks = data->npids = 0;
    3978        3126 :     data->maxprocs = data->maxlocks = data->maxpids = MaxBackends;
    3979        3126 :     data->procs = (BlockedProcData *) palloc(sizeof(BlockedProcData) * data->maxprocs);
    3980        3126 :     data->locks = (LockInstanceData *) palloc(sizeof(LockInstanceData) * data->maxlocks);
    3981        3126 :     data->waiter_pids = (int *) palloc(sizeof(int) * data->maxpids);
    3982             : 
    3983             :     /*
    3984             :      * In order to search the ProcArray for blocked_pid and assume that that
    3985             :      * entry won't immediately disappear under us, we must hold ProcArrayLock.
    3986             :      * In addition, to examine the lock grouping fields of any other backend,
    3987             :      * we must hold all the hash partition locks.  (Only one of those locks is
    3988             :      * actually relevant for any one lock group, but we can't know which one
    3989             :      * ahead of time.)  It's fairly annoying to hold all those locks
    3990             :      * throughout this, but it's no worse than GetLockStatusData(), and it
    3991             :      * does have the advantage that we're guaranteed to return a
    3992             :      * self-consistent instantaneous state.
    3993             :      */
    3994        3126 :     LWLockAcquire(ProcArrayLock, LW_SHARED);
    3995             : 
    3996        3126 :     proc = BackendPidGetProcWithLock(blocked_pid);
    3997             : 
    3998             :     /* Nothing to do if it's gone */
    3999        3126 :     if (proc != NULL)
    4000             :     {
    4001             :         /*
    4002             :          * Acquire lock on the entire shared lock data structure.  See notes
    4003             :          * in GetLockStatusData().
    4004             :          */
    4005       53142 :         for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
    4006       50016 :             LWLockAcquire(LockHashPartitionLockByIndex(i), LW_SHARED);
    4007             : 
    4008        3126 :         if (proc->lockGroupLeader == NULL)
    4009             :         {
    4010             :             /* Easy case, proc is not a lock group member */
    4011        2674 :             GetSingleProcBlockerStatusData(proc, data);
    4012             :         }
    4013             :         else
    4014             :         {
    4015             :             /* Examine all procs in proc's lock group */
    4016             :             dlist_iter  iter;
    4017             : 
    4018        1046 :             dlist_foreach(iter, &proc->lockGroupLeader->lockGroupMembers)
    4019             :             {
    4020             :                 PGPROC     *memberProc;
    4021             : 
    4022         594 :                 memberProc = dlist_container(PGPROC, lockGroupLink, iter.cur);
    4023         594 :                 GetSingleProcBlockerStatusData(memberProc, data);
    4024             :             }
    4025             :         }
    4026             : 
    4027             :         /*
    4028             :          * And release locks.  See notes in GetLockStatusData().
    4029             :          */
    4030       53142 :         for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
    4031       50016 :             LWLockRelease(LockHashPartitionLockByIndex(i));
    4032             : 
    4033             :         Assert(data->nprocs <= data->maxprocs);
    4034             :     }
    4035             : 
    4036        3126 :     LWLockRelease(ProcArrayLock);
    4037             : 
    4038        3126 :     return data;
    4039             : }
    4040             : 
    4041             : /* Accumulate data about one possibly-blocked proc for GetBlockerStatusData */
    4042             : static void
    4043        3268 : GetSingleProcBlockerStatusData(PGPROC *blocked_proc, BlockedProcsData *data)
    4044             : {
    4045        3268 :     LOCK       *theLock = blocked_proc->waitLock;
    4046             :     BlockedProcData *bproc;
    4047             :     dlist_iter  proclock_iter;
    4048             :     dlist_iter  proc_iter;
    4049             :     dclist_head *waitQueue;
    4050             :     int         queue_size;
    4051             : 
    4052             :     /* Nothing to do if this proc is not blocked */
    4053        3268 :     if (theLock == NULL)
    4054        1014 :         return;
    4055             : 
    4056             :     /* Set up a procs[] element */
    4057        2254 :     bproc = &data->procs[data->nprocs++];
    4058        2254 :     bproc->pid = blocked_proc->pid;
    4059        2254 :     bproc->first_lock = data->nlocks;
    4060        2254 :     bproc->first_waiter = data->npids;
    4061             : 
    4062             :     /*
    4063             :      * We may ignore the proc's fast-path arrays, since nothing in those could
    4064             :      * be related to a contended lock.
    4065             :      */
    4066             : 
    4067             :     /* Collect all PROCLOCKs associated with theLock */
    4068        6850 :     dlist_foreach(proclock_iter, &theLock->procLocks)
    4069             :     {
    4070        4596 :         PROCLOCK   *proclock =
    4071        4596 :             dlist_container(PROCLOCK, lockLink, proclock_iter.cur);
    4072        4596 :         PGPROC     *proc = proclock->tag.myProc;
    4073        4596 :         LOCK       *lock = proclock->tag.myLock;
    4074             :         LockInstanceData *instance;
    4075             : 
    4076        4596 :         if (data->nlocks >= data->maxlocks)
    4077             :         {
    4078           0 :             data->maxlocks += MaxBackends;
    4079           0 :             data->locks = (LockInstanceData *)
    4080           0 :                 repalloc(data->locks, sizeof(LockInstanceData) * data->maxlocks);
    4081             :         }
    4082             : 
    4083        4596 :         instance = &data->locks[data->nlocks];
    4084        4596 :         memcpy(&instance->locktag, &lock->tag, sizeof(LOCKTAG));
    4085        4596 :         instance->holdMask = proclock->holdMask;
    4086        4596 :         if (proc->waitLock == lock)
    4087        2332 :             instance->waitLockMode = proc->waitLockMode;
    4088             :         else
    4089        2264 :             instance->waitLockMode = NoLock;
    4090        4596 :         instance->vxid.procNumber = proc->vxid.procNumber;
    4091        4596 :         instance->vxid.localTransactionId = proc->vxid.lxid;
    4092        4596 :         instance->pid = proc->pid;
    4093        4596 :         instance->leaderPid = proclock->groupLeader->pid;
    4094        4596 :         instance->fastpath = false;
    4095        4596 :         data->nlocks++;
    4096             :     }
    4097             : 
    4098             :     /* Enlarge waiter_pids[] if it's too small to hold all wait queue PIDs */
    4099        2254 :     waitQueue = &(theLock->waitProcs);
    4100        2254 :     queue_size = dclist_count(waitQueue);
    4101             : 
    4102        2254 :     if (queue_size > data->maxpids - data->npids)
    4103             :     {
    4104           0 :         data->maxpids = Max(data->maxpids + MaxBackends,
    4105             :                             data->npids + queue_size);
    4106           0 :         data->waiter_pids = (int *) repalloc(data->waiter_pids,
    4107           0 :                                              sizeof(int) * data->maxpids);
    4108             :     }
    4109             : 
    4110             :     /* Collect PIDs from the lock's wait queue, stopping at blocked_proc */
    4111        2290 :     dclist_foreach(proc_iter, waitQueue)
    4112             :     {
    4113        2290 :         PGPROC     *queued_proc = dlist_container(PGPROC, links, proc_iter.cur);
    4114             : 
    4115        2290 :         if (queued_proc == blocked_proc)
    4116        2254 :             break;
    4117          36 :         data->waiter_pids[data->npids++] = queued_proc->pid;
    4118          36 :         queued_proc = (PGPROC *) queued_proc->links.next;
    4119             :     }
    4120             : 
    4121        2254 :     bproc->num_locks = data->nlocks - bproc->first_lock;
    4122        2254 :     bproc->num_waiters = data->npids - bproc->first_waiter;
    4123             : }
    4124             : 
    4125             : /*
    4126             :  * Returns a list of currently held AccessExclusiveLocks, for use by
    4127             :  * LogStandbySnapshot().  The result is a palloc'd array,
    4128             :  * with the number of elements returned into *nlocks.
    4129             :  *
    4130             :  * XXX This currently takes a lock on all partitions of the lock table,
    4131             :  * but it's possible to do better.  By reference counting locks and storing
    4132             :  * the value in the ProcArray entry for each backend we could tell if any
    4133             :  * locks need recording without having to acquire the partition locks and
    4134             :  * scan the lock table.  Whether that's worth the additional overhead
    4135             :  * is pretty dubious though.
    4136             :  */
    4137             : xl_standby_lock *
    4138        1976 : GetRunningTransactionLocks(int *nlocks)
    4139             : {
    4140             :     xl_standby_lock *accessExclusiveLocks;
    4141             :     PROCLOCK   *proclock;
    4142             :     HASH_SEQ_STATUS seqstat;
    4143             :     int         i;
    4144             :     int         index;
    4145             :     int         els;
    4146             : 
    4147             :     /*
    4148             :      * Acquire lock on the entire shared lock data structure.
    4149             :      *
    4150             :      * Must grab LWLocks in partition-number order to avoid LWLock deadlock.
    4151             :      */
    4152       33592 :     for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
    4153       31616 :         LWLockAcquire(LockHashPartitionLockByIndex(i), LW_SHARED);
    4154             : 
    4155             :     /* Now we can safely count the number of proclocks */
    4156        1976 :     els = hash_get_num_entries(LockMethodProcLockHash);
    4157             : 
    4158             :     /*
    4159             :      * Allocating enough space for all locks in the lock table is overkill,
    4160             :      * but it's more convenient and faster than having to enlarge the array.
    4161             :      */
    4162        1976 :     accessExclusiveLocks = palloc(els * sizeof(xl_standby_lock));
    4163             : 
    4164             :     /* Now scan the tables to copy the data */
    4165        1976 :     hash_seq_init(&seqstat, LockMethodProcLockHash);
    4166             : 
    4167             :     /*
    4168             :      * If lock is a currently granted AccessExclusiveLock then it will have
    4169             :      * just one proclock holder, so locks are never accessed twice in this
    4170             :      * particular case. Don't copy this code for use elsewhere because in the
    4171             :      * general case this will give you duplicate locks when looking at
    4172             :      * non-exclusive lock types.
    4173             :      */
    4174        1976 :     index = 0;
    4175       10496 :     while ((proclock = (PROCLOCK *) hash_seq_search(&seqstat)))
    4176             :     {
    4177             :         /* make sure this definition matches the one used in LockAcquire */
    4178        8520 :         if ((proclock->holdMask & LOCKBIT_ON(AccessExclusiveLock)) &&
    4179        4532 :             proclock->tag.myLock->tag.locktag_type == LOCKTAG_RELATION)
    4180             :         {
    4181        2830 :             PGPROC     *proc = proclock->tag.myProc;
    4182        2830 :             LOCK       *lock = proclock->tag.myLock;
    4183        2830 :             TransactionId xid = proc->xid;
    4184             : 
    4185             :             /*
    4186             :              * Don't record locks for transactions if we know they have
    4187             :              * already issued their WAL record for commit but not yet released
    4188             :              * lock. It is still possible that we see locks held by already
    4189             :              * complete transactions, if they haven't yet zeroed their xids.
    4190             :              */
    4191        2830 :             if (!TransactionIdIsValid(xid))
    4192          18 :                 continue;
    4193             : 
    4194        2812 :             accessExclusiveLocks[index].xid = xid;
    4195        2812 :             accessExclusiveLocks[index].dbOid = lock->tag.locktag_field1;
    4196        2812 :             accessExclusiveLocks[index].relOid = lock->tag.locktag_field2;
    4197             : 
    4198        2812 :             index++;
    4199             :         }
    4200             :     }
    4201             : 
    4202             :     Assert(index <= els);
    4203             : 
    4204             :     /*
    4205             :      * And release locks.  We do this in reverse order for two reasons: (1)
    4206             :      * Anyone else who needs more than one of the locks will be trying to lock
    4207             :      * them in increasing order; we don't want to release the other process
    4208             :      * until it can get all the locks it needs. (2) This avoids O(N^2)
    4209             :      * behavior inside LWLockRelease.
    4210             :      */
    4211       33592 :     for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
    4212       31616 :         LWLockRelease(LockHashPartitionLockByIndex(i));
    4213             : 
    4214        1976 :     *nlocks = index;
    4215        1976 :     return accessExclusiveLocks;
    4216             : }
    4217             : 
    4218             : /* Provide the textual name of any lock mode */
    4219             : const char *
    4220       16466 : GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode)
    4221             : {
    4222             :     Assert(lockmethodid > 0 && lockmethodid < lengthof(LockMethods));
    4223             :     Assert(mode > 0 && mode <= LockMethods[lockmethodid]->numLockModes);
    4224       16466 :     return LockMethods[lockmethodid]->lockModeNames[mode];
    4225             : }
    4226             : 
    4227             : #ifdef LOCK_DEBUG
    4228             : /*
    4229             :  * Dump all locks in the given proc's myProcLocks lists.
    4230             :  *
    4231             :  * Caller is responsible for having acquired appropriate LWLocks.
    4232             :  */
    4233             : void
    4234             : DumpLocks(PGPROC *proc)
    4235             : {
    4236             :     int         i;
    4237             : 
    4238             :     if (proc == NULL)
    4239             :         return;
    4240             : 
    4241             :     if (proc->waitLock)
    4242             :         LOCK_PRINT("DumpLocks: waiting on", proc->waitLock, 0);
    4243             : 
    4244             :     for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
    4245             :     {
    4246             :         dlist_head *procLocks = &proc->myProcLocks[i];
    4247             :         dlist_iter  iter;
    4248             : 
    4249             :         dlist_foreach(iter, procLocks)
    4250             :         {
    4251             :             PROCLOCK   *proclock = dlist_container(PROCLOCK, procLink, iter.cur);
    4252             :             LOCK       *lock = proclock->tag.myLock;
    4253             : 
    4254             :             Assert(proclock->tag.myProc == proc);
    4255             :             PROCLOCK_PRINT("DumpLocks", proclock);
    4256             :             LOCK_PRINT("DumpLocks", lock, 0);
    4257             :         }
    4258             :     }
    4259             : }
    4260             : 
    4261             : /*
    4262             :  * Dump all lmgr locks.
    4263             :  *
    4264             :  * Caller is responsible for having acquired appropriate LWLocks.
    4265             :  */
    4266             : void
    4267             : DumpAllLocks(void)
    4268             : {
    4269             :     PGPROC     *proc;
    4270             :     PROCLOCK   *proclock;
    4271             :     LOCK       *lock;
    4272             :     HASH_SEQ_STATUS status;
    4273             : 
    4274             :     proc = MyProc;
    4275             : 
    4276             :     if (proc && proc->waitLock)
    4277             :         LOCK_PRINT("DumpAllLocks: waiting on", proc->waitLock, 0);
    4278             : 
    4279             :     hash_seq_init(&status, LockMethodProcLockHash);
    4280             : 
    4281             :     while ((proclock = (PROCLOCK *) hash_seq_search(&status)) != NULL)
    4282             :     {
    4283             :         PROCLOCK_PRINT("DumpAllLocks", proclock);
    4284             : 
    4285             :         lock = proclock->tag.myLock;
    4286             :         if (lock)
    4287             :             LOCK_PRINT("DumpAllLocks", lock, 0);
    4288             :         else
    4289             :             elog(LOG, "DumpAllLocks: proclock->tag.myLock = NULL");
    4290             :     }
    4291             : }
    4292             : #endif                          /* LOCK_DEBUG */
    4293             : 
    4294             : /*
    4295             :  * LOCK 2PC resource manager's routines
    4296             :  */
    4297             : 
    4298             : /*
    4299             :  * Re-acquire a lock belonging to a transaction that was prepared.
    4300             :  *
    4301             :  * Because this function is run at db startup, re-acquiring the locks should
    4302             :  * never conflict with running transactions because there are none.  We
    4303             :  * assume that the lock state represented by the stored 2PC files is legal.
    4304             :  *
    4305             :  * When switching from Hot Standby mode to normal operation, the locks will
    4306             :  * be already held by the startup process. The locks are acquired for the new
    4307             :  * procs without checking for conflicts, so we don't get a conflict between the
    4308             :  * startup process and the dummy procs, even though we will momentarily have
    4309             :  * a situation where two procs are holding the same AccessExclusiveLock,
    4310             :  * which isn't normally possible because the conflict. If we're in standby
    4311             :  * mode, but a recovery snapshot hasn't been established yet, it's possible
    4312             :  * that some but not all of the locks are already held by the startup process.
    4313             :  *
    4314             :  * This approach is simple, but also a bit dangerous, because if there isn't
    4315             :  * enough shared memory to acquire the locks, an error will be thrown, which
    4316             :  * is promoted to FATAL and recovery will abort, bringing down postmaster.
    4317             :  * A safer approach would be to transfer the locks like we do in
    4318             :  * AtPrepare_Locks, but then again, in hot standby mode it's possible for
    4319             :  * read-only backends to use up all the shared lock memory anyway, so that
    4320             :  * replaying the WAL record that needs to acquire a lock will throw an error
    4321             :  * and PANIC anyway.
    4322             :  */
    4323             : void
    4324         176 : lock_twophase_recover(TransactionId xid, uint16 info,
    4325             :                       void *recdata, uint32 len)
    4326             : {
    4327         176 :     TwoPhaseLockRecord *rec = (TwoPhaseLockRecord *) recdata;
    4328         176 :     PGPROC     *proc = TwoPhaseGetDummyProc(xid, false);
    4329             :     LOCKTAG    *locktag;
    4330             :     LOCKMODE    lockmode;
    4331             :     LOCKMETHODID lockmethodid;
    4332             :     LOCK       *lock;
    4333             :     PROCLOCK   *proclock;
    4334             :     PROCLOCKTAG proclocktag;
    4335             :     bool        found;
    4336             :     uint32      hashcode;
    4337             :     uint32      proclock_hashcode;
    4338             :     int         partition;
    4339             :     LWLock     *partitionLock;
    4340             :     LockMethod  lockMethodTable;
    4341             : 
    4342             :     Assert(len == sizeof(TwoPhaseLockRecord));
    4343         176 :     locktag = &rec->locktag;
    4344         176 :     lockmode = rec->lockmode;
    4345         176 :     lockmethodid = locktag->locktag_lockmethodid;
    4346             : 
    4347         176 :     if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
    4348           0 :         elog(ERROR, "unrecognized lock method: %d", lockmethodid);
    4349         176 :     lockMethodTable = LockMethods[lockmethodid];
    4350             : 
    4351         176 :     hashcode = LockTagHashCode(locktag);
    4352         176 :     partition = LockHashPartition(hashcode);
    4353         176 :     partitionLock = LockHashPartitionLock(hashcode);
    4354             : 
    4355         176 :     LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    4356             : 
    4357             :     /*
    4358             :      * Find or create a lock with this tag.
    4359             :      */
    4360         176 :     lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash,
    4361             :                                                 locktag,
    4362             :                                                 hashcode,
    4363             :                                                 HASH_ENTER_NULL,
    4364             :                                                 &found);
    4365         176 :     if (!lock)
    4366             :     {
    4367           0 :         LWLockRelease(partitionLock);
    4368           0 :         ereport(ERROR,
    4369             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
    4370             :                  errmsg("out of shared memory"),
    4371             :                  errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
    4372             :     }
    4373             : 
    4374             :     /*
    4375             :      * if it's a new lock object, initialize it
    4376             :      */
    4377         176 :     if (!found)
    4378             :     {
    4379         152 :         lock->grantMask = 0;
    4380         152 :         lock->waitMask = 0;
    4381         152 :         dlist_init(&lock->procLocks);
    4382         152 :         dclist_init(&lock->waitProcs);
    4383         152 :         lock->nRequested = 0;
    4384         152 :         lock->nGranted = 0;
    4385         912 :         MemSet(lock->requested, 0, sizeof(int) * MAX_LOCKMODES);
    4386         152 :         MemSet(lock->granted, 0, sizeof(int) * MAX_LOCKMODES);
    4387             :         LOCK_PRINT("lock_twophase_recover: new", lock, lockmode);
    4388             :     }
    4389             :     else
    4390             :     {
    4391             :         LOCK_PRINT("lock_twophase_recover: found", lock, lockmode);
    4392             :         Assert((lock->nRequested >= 0) && (lock->requested[lockmode] >= 0));
    4393             :         Assert((lock->nGranted >= 0) && (lock->granted[lockmode] >= 0));
    4394             :         Assert(lock->nGranted <= lock->nRequested);
    4395             :     }
    4396             : 
    4397             :     /*
    4398             :      * Create the hash key for the proclock table.
    4399             :      */
    4400         176 :     proclocktag.myLock = lock;
    4401         176 :     proclocktag.myProc = proc;
    4402             : 
    4403         176 :     proclock_hashcode = ProcLockHashCode(&proclocktag, hashcode);
    4404             : 
    4405             :     /*
    4406             :      * Find or create a proclock entry with this tag
    4407             :      */
    4408         176 :     proclock = (PROCLOCK *) hash_search_with_hash_value(LockMethodProcLockHash,
    4409             :                                                         &proclocktag,
    4410             :                                                         proclock_hashcode,
    4411             :                                                         HASH_ENTER_NULL,
    4412             :                                                         &found);
    4413         176 :     if (!proclock)
    4414             :     {
    4415             :         /* Oops, not enough shmem for the proclock */
    4416           0 :         if (lock->nRequested == 0)
    4417             :         {
    4418             :             /*
    4419             :              * There are no other requestors of this lock, so garbage-collect
    4420             :              * the lock object.  We *must* do this to avoid a permanent leak
    4421             :              * of shared memory, because there won't be anything to cause
    4422             :              * anyone to release the lock object later.
    4423             :              */
    4424             :             Assert(dlist_is_empty(&lock->procLocks));
    4425           0 :             if (!hash_search_with_hash_value(LockMethodLockHash,
    4426           0 :                                              &(lock->tag),
    4427             :                                              hashcode,
    4428             :                                              HASH_REMOVE,
    4429             :                                              NULL))
    4430           0 :                 elog(PANIC, "lock table corrupted");
    4431             :         }
    4432           0 :         LWLockRelease(partitionLock);
    4433           0 :         ereport(ERROR,
    4434             :                 (errcode(ERRCODE_OUT_OF_MEMORY),
    4435             :                  errmsg("out of shared memory"),
    4436             :                  errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
    4437             :     }
    4438             : 
    4439             :     /*
    4440             :      * If new, initialize the new entry
    4441             :      */
    4442         176 :     if (!found)
    4443             :     {
    4444             :         Assert(proc->lockGroupLeader == NULL);
    4445         160 :         proclock->groupLeader = proc;
    4446         160 :         proclock->holdMask = 0;
    4447         160 :         proclock->releaseMask = 0;
    4448             :         /* Add proclock to appropriate lists */
    4449         160 :         dlist_push_tail(&lock->procLocks, &proclock->lockLink);
    4450         160 :         dlist_push_tail(&proc->myProcLocks[partition],
    4451             :                         &proclock->procLink);
    4452             :         PROCLOCK_PRINT("lock_twophase_recover: new", proclock);
    4453             :     }
    4454             :     else
    4455             :     {
    4456             :         PROCLOCK_PRINT("lock_twophase_recover: found", proclock);
    4457             :         Assert((proclock->holdMask & ~lock->grantMask) == 0);
    4458             :     }
    4459             : 
    4460             :     /*
    4461             :      * lock->nRequested and lock->requested[] count the total number of
    4462             :      * requests, whether granted or waiting, so increment those immediately.
    4463             :      */
    4464         176 :     lock->nRequested++;
    4465         176 :     lock->requested[lockmode]++;
    4466             :     Assert((lock->nRequested > 0) && (lock->requested[lockmode] > 0));
    4467             : 
    4468             :     /*
    4469             :      * We shouldn't already hold the desired lock.
    4470             :      */
    4471         176 :     if (proclock->holdMask & LOCKBIT_ON(lockmode))
    4472           0 :         elog(ERROR, "lock %s on object %u/%u/%u is already held",
    4473             :              lockMethodTable->lockModeNames[lockmode],
    4474             :              lock->tag.locktag_field1, lock->tag.locktag_field2,
    4475             :              lock->tag.locktag_field3);
    4476             : 
    4477             :     /*
    4478             :      * We ignore any possible conflicts and just grant ourselves the lock. Not
    4479             :      * only because we don't bother, but also to avoid deadlocks when
    4480             :      * switching from standby to normal mode. See function comment.
    4481             :      */
    4482         176 :     GrantLock(lock, proclock, lockmode);
    4483             : 
    4484             :     /*
    4485             :      * Bump strong lock count, to make sure any fast-path lock requests won't
    4486             :      * be granted without consulting the primary lock table.
    4487             :      */
    4488         176 :     if (ConflictsWithRelationFastPath(&lock->tag, lockmode))
    4489             :     {
    4490          36 :         uint32      fasthashcode = FastPathStrongLockHashPartition(hashcode);
    4491             : 
    4492          36 :         SpinLockAcquire(&FastPathStrongRelationLocks->mutex);
    4493          36 :         FastPathStrongRelationLocks->count[fasthashcode]++;
    4494          36 :         SpinLockRelease(&FastPathStrongRelationLocks->mutex);
    4495             :     }
    4496             : 
    4497         176 :     LWLockRelease(partitionLock);
    4498         176 : }
    4499             : 
    4500             : /*
    4501             :  * Re-acquire a lock belonging to a transaction that was prepared, when
    4502             :  * starting up into hot standby mode.
    4503             :  */
    4504             : void
    4505           0 : lock_twophase_standby_recover(TransactionId xid, uint16 info,
    4506             :                               void *recdata, uint32 len)
    4507             : {
    4508           0 :     TwoPhaseLockRecord *rec = (TwoPhaseLockRecord *) recdata;
    4509             :     LOCKTAG    *locktag;
    4510             :     LOCKMODE    lockmode;
    4511             :     LOCKMETHODID lockmethodid;
    4512             : 
    4513             :     Assert(len == sizeof(TwoPhaseLockRecord));
    4514           0 :     locktag = &rec->locktag;
    4515           0 :     lockmode = rec->lockmode;
    4516           0 :     lockmethodid = locktag->locktag_lockmethodid;
    4517             : 
    4518           0 :     if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
    4519           0 :         elog(ERROR, "unrecognized lock method: %d", lockmethodid);
    4520             : 
    4521           0 :     if (lockmode == AccessExclusiveLock &&
    4522           0 :         locktag->locktag_type == LOCKTAG_RELATION)
    4523             :     {
    4524           0 :         StandbyAcquireAccessExclusiveLock(xid,
    4525             :                                           locktag->locktag_field1 /* dboid */ ,
    4526             :                                           locktag->locktag_field2 /* reloid */ );
    4527             :     }
    4528           0 : }
    4529             : 
    4530             : 
    4531             : /*
    4532             :  * 2PC processing routine for COMMIT PREPARED case.
    4533             :  *
    4534             :  * Find and release the lock indicated by the 2PC record.
    4535             :  */
    4536             : void
    4537        1510 : lock_twophase_postcommit(TransactionId xid, uint16 info,
    4538             :                          void *recdata, uint32 len)
    4539             : {
    4540        1510 :     TwoPhaseLockRecord *rec = (TwoPhaseLockRecord *) recdata;
    4541        1510 :     PGPROC     *proc = TwoPhaseGetDummyProc(xid, true);
    4542             :     LOCKTAG    *locktag;
    4543             :     LOCKMETHODID lockmethodid;
    4544             :     LockMethod  lockMethodTable;
    4545             : 
    4546             :     Assert(len == sizeof(TwoPhaseLockRecord));
    4547        1510 :     locktag = &rec->locktag;
    4548        1510 :     lockmethodid = locktag->locktag_lockmethodid;
    4549             : 
    4550        1510 :     if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
    4551           0 :         elog(ERROR, "unrecognized lock method: %d", lockmethodid);
    4552        1510 :     lockMethodTable = LockMethods[lockmethodid];
    4553             : 
    4554        1510 :     LockRefindAndRelease(lockMethodTable, proc, locktag, rec->lockmode, true);
    4555        1510 : }
    4556             : 
    4557             : /*
    4558             :  * 2PC processing routine for ROLLBACK PREPARED case.
    4559             :  *
    4560             :  * This is actually just the same as the COMMIT case.
    4561             :  */
    4562             : void
    4563         252 : lock_twophase_postabort(TransactionId xid, uint16 info,
    4564             :                         void *recdata, uint32 len)
    4565             : {
    4566         252 :     lock_twophase_postcommit(xid, info, recdata, len);
    4567         252 : }
    4568             : 
    4569             : /*
    4570             :  *      VirtualXactLockTableInsert
    4571             :  *
    4572             :  *      Take vxid lock via the fast-path.  There can't be any pre-existing
    4573             :  *      lockers, as we haven't advertised this vxid via the ProcArray yet.
    4574             :  *
    4575             :  *      Since MyProc->fpLocalTransactionId will normally contain the same data
    4576             :  *      as MyProc->vxid.lxid, you might wonder if we really need both.  The
    4577             :  *      difference is that MyProc->vxid.lxid is set and cleared unlocked, and
    4578             :  *      examined by procarray.c, while fpLocalTransactionId is protected by
    4579             :  *      fpInfoLock and is used only by the locking subsystem.  Doing it this
    4580             :  *      way makes it easier to verify that there are no funny race conditions.
    4581             :  *
    4582             :  *      We don't bother recording this lock in the local lock table, since it's
    4583             :  *      only ever released at the end of a transaction.  Instead,
    4584             :  *      LockReleaseAll() calls VirtualXactLockTableCleanup().
    4585             :  */
    4586             : void
    4587      824148 : VirtualXactLockTableInsert(VirtualTransactionId vxid)
    4588             : {
    4589             :     Assert(VirtualTransactionIdIsValid(vxid));
    4590             : 
    4591      824148 :     LWLockAcquire(&MyProc->fpInfoLock, LW_EXCLUSIVE);
    4592             : 
    4593             :     Assert(MyProc->vxid.procNumber == vxid.procNumber);
    4594             :     Assert(MyProc->fpLocalTransactionId == InvalidLocalTransactionId);
    4595             :     Assert(MyProc->fpVXIDLock == false);
    4596             : 
    4597      824148 :     MyProc->fpVXIDLock = true;
    4598      824148 :     MyProc->fpLocalTransactionId = vxid.localTransactionId;
    4599             : 
    4600      824148 :     LWLockRelease(&MyProc->fpInfoLock);
    4601      824148 : }
    4602             : 
    4603             : /*
    4604             :  *      VirtualXactLockTableCleanup
    4605             :  *
    4606             :  *      Check whether a VXID lock has been materialized; if so, release it,
    4607             :  *      unblocking waiters.
    4608             :  */
    4609             : void
    4610      824966 : VirtualXactLockTableCleanup(void)
    4611             : {
    4612             :     bool        fastpath;
    4613             :     LocalTransactionId lxid;
    4614             : 
    4615             :     Assert(MyProc->vxid.procNumber != INVALID_PROC_NUMBER);
    4616             : 
    4617             :     /*
    4618             :      * Clean up shared memory state.
    4619             :      */
    4620      824966 :     LWLockAcquire(&MyProc->fpInfoLock, LW_EXCLUSIVE);
    4621             : 
    4622      824966 :     fastpath = MyProc->fpVXIDLock;
    4623      824966 :     lxid = MyProc->fpLocalTransactionId;
    4624      824966 :     MyProc->fpVXIDLock = false;
    4625      824966 :     MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
    4626             : 
    4627      824966 :     LWLockRelease(&MyProc->fpInfoLock);
    4628             : 
    4629             :     /*
    4630             :      * If fpVXIDLock has been cleared without touching fpLocalTransactionId,
    4631             :      * that means someone transferred the lock to the main lock table.
    4632             :      */
    4633      824966 :     if (!fastpath && LocalTransactionIdIsValid(lxid))
    4634             :     {
    4635             :         VirtualTransactionId vxid;
    4636             :         LOCKTAG     locktag;
    4637             : 
    4638         494 :         vxid.procNumber = MyProcNumber;
    4639         494 :         vxid.localTransactionId = lxid;
    4640         494 :         SET_LOCKTAG_VIRTUALTRANSACTION(locktag, vxid);
    4641             : 
    4642         494 :         LockRefindAndRelease(LockMethods[DEFAULT_LOCKMETHOD], MyProc,
    4643             :                              &locktag, ExclusiveLock, false);
    4644             :     }
    4645      824966 : }
    4646             : 
    4647             : /*
    4648             :  *      XactLockForVirtualXact
    4649             :  *
    4650             :  * If TransactionIdIsValid(xid), this is essentially XactLockTableWait(xid,
    4651             :  * NULL, NULL, XLTW_None) or ConditionalXactLockTableWait(xid).  Unlike those
    4652             :  * functions, it assumes "xid" is never a subtransaction and that "xid" is
    4653             :  * prepared, committed, or aborted.
    4654             :  *
    4655             :  * If !TransactionIdIsValid(xid), this locks every prepared XID having been
    4656             :  * known as "vxid" before its PREPARE TRANSACTION.
    4657             :  */
    4658             : static bool
    4659         552 : XactLockForVirtualXact(VirtualTransactionId vxid,
    4660             :                        TransactionId xid, bool wait)
    4661             : {
    4662         552 :     bool        more = false;
    4663             : 
    4664             :     /* There is no point to wait for 2PCs if you have no 2PCs. */
    4665         552 :     if (max_prepared_xacts == 0)
    4666         160 :         return true;
    4667             : 
    4668             :     do
    4669             :     {
    4670             :         LockAcquireResult lar;
    4671             :         LOCKTAG     tag;
    4672             : 
    4673             :         /* Clear state from previous iterations. */
    4674         392 :         if (more)
    4675             :         {
    4676           0 :             xid = InvalidTransactionId;
    4677           0 :             more = false;
    4678             :         }
    4679             : 
    4680             :         /* If we have no xid, try to find one. */
    4681         392 :         if (!TransactionIdIsValid(xid))
    4682         192 :             xid = TwoPhaseGetXidByVirtualXID(vxid, &more);
    4683         392 :         if (!TransactionIdIsValid(xid))
    4684             :         {
    4685             :             Assert(!more);
    4686         180 :             return true;
    4687             :         }
    4688             : 
    4689             :         /* Check or wait for XID completion. */
    4690         212 :         SET_LOCKTAG_TRANSACTION(tag, xid);
    4691         212 :         lar = LockAcquire(&tag, ShareLock, false, !wait);
    4692         212 :         if (lar == LOCKACQUIRE_NOT_AVAIL)
    4693           0 :             return false;
    4694         212 :         LockRelease(&tag, ShareLock, false);
    4695         212 :     } while (more);
    4696             : 
    4697         212 :     return true;
    4698             : }
    4699             : 
    4700             : /*
    4701             :  *      VirtualXactLock
    4702             :  *
    4703             :  * If wait = true, wait as long as the given VXID or any XID acquired by the
    4704             :  * same transaction is still running.  Then, return true.
    4705             :  *
    4706             :  * If wait = false, just check whether that VXID or one of those XIDs is still
    4707             :  * running, and return true or false.
    4708             :  */
    4709             : bool
    4710         632 : VirtualXactLock(VirtualTransactionId vxid, bool wait)
    4711             : {
    4712             :     LOCKTAG     tag;
    4713             :     PGPROC     *proc;
    4714         632 :     TransactionId xid = InvalidTransactionId;
    4715             : 
    4716             :     Assert(VirtualTransactionIdIsValid(vxid));
    4717             : 
    4718         632 :     if (VirtualTransactionIdIsRecoveredPreparedXact(vxid))
    4719             :         /* no vxid lock; localTransactionId is a normal, locked XID */
    4720           2 :         return XactLockForVirtualXact(vxid, vxid.localTransactionId, wait);
    4721             : 
    4722         630 :     SET_LOCKTAG_VIRTUALTRANSACTION(tag, vxid);
    4723             : 
    4724             :     /*
    4725             :      * If a lock table entry must be made, this is the PGPROC on whose behalf
    4726             :      * it must be done.  Note that the transaction might end or the PGPROC
    4727             :      * might be reassigned to a new backend before we get around to examining
    4728             :      * it, but it doesn't matter.  If we find upon examination that the
    4729             :      * relevant lxid is no longer running here, that's enough to prove that
    4730             :      * it's no longer running anywhere.
    4731             :      */
    4732         630 :     proc = ProcNumberGetProc(vxid.procNumber);
    4733         630 :     if (proc == NULL)
    4734           8 :         return XactLockForVirtualXact(vxid, InvalidTransactionId, wait);
    4735             : 
    4736             :     /*
    4737             :      * We must acquire this lock before checking the procNumber and lxid
    4738             :      * against the ones we're waiting for.  The target backend will only set
    4739             :      * or clear lxid while holding this lock.
    4740             :      */
    4741         622 :     LWLockAcquire(&proc->fpInfoLock, LW_EXCLUSIVE);
    4742             : 
    4743         622 :     if (proc->vxid.procNumber != vxid.procNumber
    4744         622 :         || proc->fpLocalTransactionId != vxid.localTransactionId)
    4745             :     {
    4746             :         /* VXID ended */
    4747          84 :         LWLockRelease(&proc->fpInfoLock);
    4748          84 :         return XactLockForVirtualXact(vxid, InvalidTransactionId, wait);
    4749             :     }
    4750             : 
    4751             :     /*
    4752             :      * If we aren't asked to wait, there's no need to set up a lock table
    4753             :      * entry.  The transaction is still in progress, so just return false.
    4754             :      */
    4755         538 :     if (!wait)
    4756             :     {
    4757          30 :         LWLockRelease(&proc->fpInfoLock);
    4758          30 :         return false;
    4759             :     }
    4760             : 
    4761             :     /*
    4762             :      * OK, we're going to need to sleep on the VXID.  But first, we must set
    4763             :      * up the primary lock table entry, if needed (ie, convert the proc's
    4764             :      * fast-path lock on its VXID to a regular lock).
    4765             :      */
    4766         508 :     if (proc->fpVXIDLock)
    4767             :     {
    4768             :         PROCLOCK   *proclock;
    4769             :         uint32      hashcode;
    4770             :         LWLock     *partitionLock;
    4771             : 
    4772         494 :         hashcode = LockTagHashCode(&tag);
    4773             : 
    4774         494 :         partitionLock = LockHashPartitionLock(hashcode);
    4775         494 :         LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    4776             : 
    4777         494 :         proclock = SetupLockInTable(LockMethods[DEFAULT_LOCKMETHOD], proc,
    4778             :                                     &tag, hashcode, ExclusiveLock);
    4779         494 :         if (!proclock)
    4780             :         {
    4781           0 :             LWLockRelease(partitionLock);
    4782           0 :             LWLockRelease(&proc->fpInfoLock);
    4783           0 :             ereport(ERROR,
    4784             :                     (errcode(ERRCODE_OUT_OF_MEMORY),
    4785             :                      errmsg("out of shared memory"),
    4786             :                      errhint("You might need to increase \"%s\".", "max_locks_per_transaction")));
    4787             :         }
    4788         494 :         GrantLock(proclock->tag.myLock, proclock, ExclusiveLock);
    4789             : 
    4790         494 :         LWLockRelease(partitionLock);
    4791             : 
    4792         494 :         proc->fpVXIDLock = false;
    4793             :     }
    4794             : 
    4795             :     /*
    4796             :      * If the proc has an XID now, we'll avoid a TwoPhaseGetXidByVirtualXID()
    4797             :      * search.  The proc might have assigned this XID but not yet locked it,
    4798             :      * in which case the proc will lock this XID before releasing the VXID.
    4799             :      * The fpInfoLock critical section excludes VirtualXactLockTableCleanup(),
    4800             :      * so we won't save an XID of a different VXID.  It doesn't matter whether
    4801             :      * we save this before or after setting up the primary lock table entry.
    4802             :      */
    4803         508 :     xid = proc->xid;
    4804             : 
    4805             :     /* Done with proc->fpLockBits */
    4806         508 :     LWLockRelease(&proc->fpInfoLock);
    4807             : 
    4808             :     /* Time to wait. */
    4809         508 :     (void) LockAcquire(&tag, ShareLock, false, false);
    4810             : 
    4811         458 :     LockRelease(&tag, ShareLock, false);
    4812         458 :     return XactLockForVirtualXact(vxid, xid, wait);
    4813             : }
    4814             : 
    4815             : /*
    4816             :  * LockWaiterCount
    4817             :  *
    4818             :  * Find the number of lock requester on this locktag
    4819             :  */
    4820             : int
    4821      129872 : LockWaiterCount(const LOCKTAG *locktag)
    4822             : {
    4823      129872 :     LOCKMETHODID lockmethodid = locktag->locktag_lockmethodid;
    4824             :     LOCK       *lock;
    4825             :     bool        found;
    4826             :     uint32      hashcode;
    4827             :     LWLock     *partitionLock;
    4828      129872 :     int         waiters = 0;
    4829             : 
    4830      129872 :     if (lockmethodid <= 0 || lockmethodid >= lengthof(LockMethods))
    4831           0 :         elog(ERROR, "unrecognized lock method: %d", lockmethodid);
    4832             : 
    4833      129872 :     hashcode = LockTagHashCode(locktag);
    4834      129872 :     partitionLock = LockHashPartitionLock(hashcode);
    4835      129872 :     LWLockAcquire(partitionLock, LW_EXCLUSIVE);
    4836             : 
    4837      129872 :     lock = (LOCK *) hash_search_with_hash_value(LockMethodLockHash,
    4838             :                                                 locktag,
    4839             :                                                 hashcode,
    4840             :                                                 HASH_FIND,
    4841             :                                                 &found);
    4842      129872 :     if (found)
    4843             :     {
    4844             :         Assert(lock != NULL);
    4845          48 :         waiters = lock->nRequested;
    4846             :     }
    4847      129872 :     LWLockRelease(partitionLock);
    4848             : 
    4849      129872 :     return waiters;
    4850             : }

Generated by: LCOV version 1.14