LCOV - code coverage report
Current view: top level - src/backend/storage/lmgr - proc.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 507 551 92.0 %
Date: 2025-01-18 04:15:08 Functions: 25 25 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  *
       3             :  * proc.c
       4             :  *    routines to manage per-process shared memory data structure
       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/proc.c
      12             :  *
      13             :  *-------------------------------------------------------------------------
      14             :  */
      15             : /*
      16             :  * Interface (a):
      17             :  *      JoinWaitQueue(), ProcSleep(), ProcWakeup()
      18             :  *
      19             :  * Waiting for a lock causes the backend to be put to sleep.  Whoever releases
      20             :  * the lock wakes the process up again (and gives it an error code so it knows
      21             :  * whether it was awoken on an error condition).
      22             :  *
      23             :  * Interface (b):
      24             :  *
      25             :  * ProcReleaseLocks -- frees the locks associated with current transaction
      26             :  *
      27             :  * ProcKill -- destroys the shared memory state (and locks)
      28             :  * associated with the process.
      29             :  */
      30             : #include "postgres.h"
      31             : 
      32             : #include <signal.h>
      33             : #include <unistd.h>
      34             : #include <sys/time.h>
      35             : 
      36             : #include "access/transam.h"
      37             : #include "access/twophase.h"
      38             : #include "access/xlogutils.h"
      39             : #include "miscadmin.h"
      40             : #include "pgstat.h"
      41             : #include "postmaster/autovacuum.h"
      42             : #include "replication/slotsync.h"
      43             : #include "replication/syncrep.h"
      44             : #include "storage/condition_variable.h"
      45             : #include "storage/ipc.h"
      46             : #include "storage/lmgr.h"
      47             : #include "storage/pmsignal.h"
      48             : #include "storage/proc.h"
      49             : #include "storage/procarray.h"
      50             : #include "storage/procsignal.h"
      51             : #include "storage/spin.h"
      52             : #include "storage/standby.h"
      53             : #include "utils/timeout.h"
      54             : #include "utils/timestamp.h"
      55             : 
      56             : /* GUC variables */
      57             : int         DeadlockTimeout = 1000;
      58             : int         StatementTimeout = 0;
      59             : int         LockTimeout = 0;
      60             : int         IdleInTransactionSessionTimeout = 0;
      61             : int         TransactionTimeout = 0;
      62             : int         IdleSessionTimeout = 0;
      63             : bool        log_lock_waits = false;
      64             : 
      65             : /* Pointer to this process's PGPROC struct, if any */
      66             : PGPROC     *MyProc = NULL;
      67             : 
      68             : /*
      69             :  * This spinlock protects the freelist of recycled PGPROC structures.
      70             :  * We cannot use an LWLock because the LWLock manager depends on already
      71             :  * having a PGPROC and a wait semaphore!  But these structures are touched
      72             :  * relatively infrequently (only at backend startup or shutdown) and not for
      73             :  * very long, so a spinlock is okay.
      74             :  */
      75             : NON_EXEC_STATIC slock_t *ProcStructLock = NULL;
      76             : 
      77             : /* Pointers to shared-memory structures */
      78             : PROC_HDR   *ProcGlobal = NULL;
      79             : NON_EXEC_STATIC PGPROC *AuxiliaryProcs = NULL;
      80             : PGPROC     *PreparedXactProcs = NULL;
      81             : 
      82             : static DeadLockState deadlock_state = DS_NOT_YET_CHECKED;
      83             : 
      84             : /* Is a deadlock check pending? */
      85             : static volatile sig_atomic_t got_deadlock_timeout;
      86             : 
      87             : static void RemoveProcFromArray(int code, Datum arg);
      88             : static void ProcKill(int code, Datum arg);
      89             : static void AuxiliaryProcKill(int code, Datum arg);
      90             : static void CheckDeadLock(void);
      91             : 
      92             : 
      93             : /*
      94             :  * Report shared-memory space needed by InitProcGlobal.
      95             :  */
      96             : Size
      97        3566 : ProcGlobalShmemSize(void)
      98             : {
      99        3566 :     Size        size = 0;
     100             :     Size        TotalProcs =
     101        3566 :         add_size(MaxBackends, add_size(NUM_AUXILIARY_PROCS, max_prepared_xacts));
     102             :     Size        fpLockBitsSize,
     103             :                 fpRelIdSize;
     104             : 
     105             :     /* ProcGlobal */
     106        3566 :     size = add_size(size, sizeof(PROC_HDR));
     107        3566 :     size = add_size(size, mul_size(TotalProcs, sizeof(PGPROC)));
     108        3566 :     size = add_size(size, sizeof(slock_t));
     109             : 
     110        3566 :     size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->xids)));
     111        3566 :     size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->subxidStates)));
     112        3566 :     size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->statusFlags)));
     113             : 
     114             :     /*
     115             :      * Memory needed for PGPROC fast-path lock arrays. Make sure the sizes are
     116             :      * nicely aligned in each backend.
     117             :      */
     118        3566 :     fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64));
     119        3566 :     fpRelIdSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(Oid) * FP_LOCK_SLOTS_PER_GROUP);
     120             : 
     121        3566 :     size = add_size(size, mul_size(TotalProcs, (fpLockBitsSize + fpRelIdSize)));
     122             : 
     123        3566 :     return size;
     124             : }
     125             : 
     126             : /*
     127             :  * Report number of semaphores needed by InitProcGlobal.
     128             :  */
     129             : int
     130        3566 : ProcGlobalSemas(void)
     131             : {
     132             :     /*
     133             :      * We need a sema per backend (including autovacuum), plus one for each
     134             :      * auxiliary process.
     135             :      */
     136        3566 :     return MaxBackends + NUM_AUXILIARY_PROCS;
     137             : }
     138             : 
     139             : /*
     140             :  * InitProcGlobal -
     141             :  *    Initialize the global process table during postmaster or standalone
     142             :  *    backend startup.
     143             :  *
     144             :  *    We also create all the per-process semaphores we will need to support
     145             :  *    the requested number of backends.  We used to allocate semaphores
     146             :  *    only when backends were actually started up, but that is bad because
     147             :  *    it lets Postgres fail under load --- a lot of Unix systems are
     148             :  *    (mis)configured with small limits on the number of semaphores, and
     149             :  *    running out when trying to start another backend is a common failure.
     150             :  *    So, now we grab enough semaphores to support the desired max number
     151             :  *    of backends immediately at initialization --- if the sysadmin has set
     152             :  *    MaxConnections, max_worker_processes, max_wal_senders, or
     153             :  *    autovacuum_worker_slots higher than his kernel will support, he'll
     154             :  *    find out sooner rather than later.
     155             :  *
     156             :  *    Another reason for creating semaphores here is that the semaphore
     157             :  *    implementation typically requires us to create semaphores in the
     158             :  *    postmaster, not in backends.
     159             :  *
     160             :  * Note: this is NOT called by individual backends under a postmaster,
     161             :  * not even in the EXEC_BACKEND case.  The ProcGlobal and AuxiliaryProcs
     162             :  * pointers must be propagated specially for EXEC_BACKEND operation.
     163             :  */
     164             : void
     165        1918 : InitProcGlobal(void)
     166             : {
     167             :     PGPROC     *procs;
     168             :     int         i,
     169             :                 j;
     170             :     bool        found;
     171        1918 :     uint32      TotalProcs = MaxBackends + NUM_AUXILIARY_PROCS + max_prepared_xacts;
     172             : 
     173             :     /* Used for setup of per-backend fast-path slots. */
     174             :     char       *fpPtr,
     175             :                *fpEndPtr PG_USED_FOR_ASSERTS_ONLY;
     176             :     Size        fpLockBitsSize,
     177             :                 fpRelIdSize;
     178             : 
     179             :     /* Create the ProcGlobal shared structure */
     180        1918 :     ProcGlobal = (PROC_HDR *)
     181        1918 :         ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
     182             :     Assert(!found);
     183             : 
     184             :     /*
     185             :      * Initialize the data structures.
     186             :      */
     187        1918 :     ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY;
     188        1918 :     dlist_init(&ProcGlobal->freeProcs);
     189        1918 :     dlist_init(&ProcGlobal->autovacFreeProcs);
     190        1918 :     dlist_init(&ProcGlobal->bgworkerFreeProcs);
     191        1918 :     dlist_init(&ProcGlobal->walsenderFreeProcs);
     192        1918 :     ProcGlobal->startupBufferPinWaitBufId = -1;
     193        1918 :     ProcGlobal->walwriterProc = INVALID_PROC_NUMBER;
     194        1918 :     ProcGlobal->checkpointerProc = INVALID_PROC_NUMBER;
     195        1918 :     pg_atomic_init_u32(&ProcGlobal->procArrayGroupFirst, INVALID_PROC_NUMBER);
     196        1918 :     pg_atomic_init_u32(&ProcGlobal->clogGroupFirst, INVALID_PROC_NUMBER);
     197             : 
     198             :     /*
     199             :      * Create and initialize all the PGPROC structures we'll need.  There are
     200             :      * six separate consumers: (1) normal backends, (2) autovacuum workers and
     201             :      * special workers, (3) background workers, (4) walsenders, (5) auxiliary
     202             :      * processes, and (6) prepared transactions.  (For largely-historical
     203             :      * reasons, we combine autovacuum and special workers into one category
     204             :      * with a single freelist.)  Each PGPROC structure is dedicated to exactly
     205             :      * one of these purposes, and they do not move between groups.
     206             :      */
     207        1918 :     procs = (PGPROC *) ShmemAlloc(TotalProcs * sizeof(PGPROC));
     208        1918 :     MemSet(procs, 0, TotalProcs * sizeof(PGPROC));
     209        1918 :     ProcGlobal->allProcs = procs;
     210             :     /* XXX allProcCount isn't really all of them; it excludes prepared xacts */
     211        1918 :     ProcGlobal->allProcCount = MaxBackends + NUM_AUXILIARY_PROCS;
     212             : 
     213             :     /*
     214             :      * Allocate arrays mirroring PGPROC fields in a dense manner. See
     215             :      * PROC_HDR.
     216             :      *
     217             :      * XXX: It might make sense to increase padding for these arrays, given
     218             :      * how hotly they are accessed.
     219             :      */
     220        3836 :     ProcGlobal->xids =
     221        1918 :         (TransactionId *) ShmemAlloc(TotalProcs * sizeof(*ProcGlobal->xids));
     222       77470 :     MemSet(ProcGlobal->xids, 0, TotalProcs * sizeof(*ProcGlobal->xids));
     223        1918 :     ProcGlobal->subxidStates = (XidCacheStatus *) ShmemAlloc(TotalProcs * sizeof(*ProcGlobal->subxidStates));
     224       25008 :     MemSet(ProcGlobal->subxidStates, 0, TotalProcs * sizeof(*ProcGlobal->subxidStates));
     225        1918 :     ProcGlobal->statusFlags = (uint8 *) ShmemAlloc(TotalProcs * sizeof(*ProcGlobal->statusFlags));
     226        5240 :     MemSet(ProcGlobal->statusFlags, 0, TotalProcs * sizeof(*ProcGlobal->statusFlags));
     227             : 
     228             :     /*
     229             :      * Allocate arrays for fast-path locks. Those are variable-length, so
     230             :      * can't be included in PGPROC directly. We allocate a separate piece of
     231             :      * shared memory and then divide that between backends.
     232             :      */
     233        1918 :     fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64));
     234        1918 :     fpRelIdSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(Oid) * FP_LOCK_SLOTS_PER_GROUP);
     235             : 
     236        1918 :     fpPtr = ShmemAlloc(TotalProcs * (fpLockBitsSize + fpRelIdSize));
     237        1918 :     MemSet(fpPtr, 0, TotalProcs * (fpLockBitsSize + fpRelIdSize));
     238             : 
     239             :     /* For asserts checking we did not overflow. */
     240        1918 :     fpEndPtr = fpPtr + (TotalProcs * (fpLockBitsSize + fpRelIdSize));
     241             : 
     242      191792 :     for (i = 0; i < TotalProcs; i++)
     243             :     {
     244      189874 :         PGPROC     *proc = &procs[i];
     245             : 
     246             :         /* Common initialization for all PGPROCs, regardless of type. */
     247             : 
     248             :         /*
     249             :          * Set the fast-path lock arrays, and move the pointer. We interleave
     250             :          * the two arrays, to (hopefully) get some locality for each backend.
     251             :          */
     252      189874 :         proc->fpLockBits = (uint64 *) fpPtr;
     253      189874 :         fpPtr += fpLockBitsSize;
     254             : 
     255      189874 :         proc->fpRelId = (Oid *) fpPtr;
     256      189874 :         fpPtr += fpRelIdSize;
     257             : 
     258             :         Assert(fpPtr <= fpEndPtr);
     259             : 
     260             :         /*
     261             :          * Set up per-PGPROC semaphore, latch, and fpInfoLock.  Prepared xact
     262             :          * dummy PGPROCs don't need these though - they're never associated
     263             :          * with a real process
     264             :          */
     265      189874 :         if (i < MaxBackends + NUM_AUXILIARY_PROCS)
     266             :         {
     267      188224 :             proc->sem = PGSemaphoreCreate();
     268      188224 :             InitSharedLatch(&(proc->procLatch));
     269      188224 :             LWLockInitialize(&(proc->fpInfoLock), LWTRANCHE_LOCK_FASTPATH);
     270             :         }
     271             : 
     272             :         /*
     273             :          * Newly created PGPROCs for normal backends, autovacuum workers,
     274             :          * special workers, bgworkers, and walsenders must be queued up on the
     275             :          * appropriate free list.  Because there can only ever be a small,
     276             :          * fixed number of auxiliary processes, no free list is used in that
     277             :          * case; InitAuxiliaryProcess() instead uses a linear search.  PGPROCs
     278             :          * for prepared transactions are added to a free list by
     279             :          * TwoPhaseShmemInit().
     280             :          */
     281      189874 :         if (i < MaxConnections)
     282             :         {
     283             :             /* PGPROC for normal backend, add to freeProcs list */
     284      123822 :             dlist_push_tail(&ProcGlobal->freeProcs, &proc->links);
     285      123822 :             proc->procgloballist = &ProcGlobal->freeProcs;
     286             :         }
     287       66052 :         else if (i < MaxConnections + autovacuum_worker_slots + NUM_SPECIAL_WORKER_PROCS)
     288             :         {
     289             :             /* PGPROC for AV or special worker, add to autovacFreeProcs list */
     290       24774 :             dlist_push_tail(&ProcGlobal->autovacFreeProcs, &proc->links);
     291       24774 :             proc->procgloballist = &ProcGlobal->autovacFreeProcs;
     292             :         }
     293       41278 :         else if (i < MaxConnections + autovacuum_worker_slots + NUM_SPECIAL_WORKER_PROCS + max_worker_processes)
     294             :         {
     295             :             /* PGPROC for bgworker, add to bgworkerFreeProcs list */
     296       15340 :             dlist_push_tail(&ProcGlobal->bgworkerFreeProcs, &proc->links);
     297       15340 :             proc->procgloballist = &ProcGlobal->bgworkerFreeProcs;
     298             :         }
     299       25938 :         else if (i < MaxBackends)
     300             :         {
     301             :             /* PGPROC for walsender, add to walsenderFreeProcs list */
     302       12780 :             dlist_push_tail(&ProcGlobal->walsenderFreeProcs, &proc->links);
     303       12780 :             proc->procgloballist = &ProcGlobal->walsenderFreeProcs;
     304             :         }
     305             : 
     306             :         /* Initialize myProcLocks[] shared memory queues. */
     307     3227858 :         for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
     308     3037984 :             dlist_init(&(proc->myProcLocks[j]));
     309             : 
     310             :         /* Initialize lockGroupMembers list. */
     311      189874 :         dlist_init(&proc->lockGroupMembers);
     312             : 
     313             :         /*
     314             :          * Initialize the atomic variables, otherwise, it won't be safe to
     315             :          * access them for backends that aren't currently in use.
     316             :          */
     317      189874 :         pg_atomic_init_u32(&(proc->procArrayGroupNext), INVALID_PROC_NUMBER);
     318      189874 :         pg_atomic_init_u32(&(proc->clogGroupNext), INVALID_PROC_NUMBER);
     319      189874 :         pg_atomic_init_u64(&(proc->waitStart), 0);
     320             :     }
     321             : 
     322             :     /* Should have consumed exactly the expected amount of fast-path memory. */
     323             :     Assert(fpPtr == fpEndPtr);
     324             : 
     325             :     /*
     326             :      * Save pointers to the blocks of PGPROC structures reserved for auxiliary
     327             :      * processes and prepared transactions.
     328             :      */
     329        1918 :     AuxiliaryProcs = &procs[MaxBackends];
     330        1918 :     PreparedXactProcs = &procs[MaxBackends + NUM_AUXILIARY_PROCS];
     331             : 
     332             :     /* Create ProcStructLock spinlock, too */
     333        1918 :     ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t));
     334        1918 :     SpinLockInit(ProcStructLock);
     335        1918 : }
     336             : 
     337             : /*
     338             :  * InitProcess -- initialize a per-process PGPROC entry for this backend
     339             :  */
     340             : void
     341       30210 : InitProcess(void)
     342             : {
     343             :     dlist_head *procgloballist;
     344             : 
     345             :     /*
     346             :      * ProcGlobal should be set up already (if we are a backend, we inherit
     347             :      * this by fork() or EXEC_BACKEND mechanism from the postmaster).
     348             :      */
     349       30210 :     if (ProcGlobal == NULL)
     350           0 :         elog(PANIC, "proc header uninitialized");
     351             : 
     352       30210 :     if (MyProc != NULL)
     353           0 :         elog(ERROR, "you already exist");
     354             : 
     355             :     /*
     356             :      * Before we start accessing the shared memory in a serious way, mark
     357             :      * ourselves as an active postmaster child; this is so that the postmaster
     358             :      * can detect it if we exit without cleaning up.
     359             :      */
     360       30210 :     if (IsUnderPostmaster)
     361       30012 :         RegisterPostmasterChildActive();
     362             : 
     363             :     /*
     364             :      * Decide which list should supply our PGPROC.  This logic must match the
     365             :      * way the freelists were constructed in InitProcGlobal().
     366             :      */
     367       30210 :     if (AmAutoVacuumWorkerProcess() || AmSpecialWorkerProcess())
     368        2976 :         procgloballist = &ProcGlobal->autovacFreeProcs;
     369       27234 :     else if (AmBackgroundWorkerProcess())
     370        4290 :         procgloballist = &ProcGlobal->bgworkerFreeProcs;
     371       22944 :     else if (AmWalSenderProcess())
     372        2128 :         procgloballist = &ProcGlobal->walsenderFreeProcs;
     373             :     else
     374       20816 :         procgloballist = &ProcGlobal->freeProcs;
     375             : 
     376             :     /*
     377             :      * Try to get a proc struct from the appropriate free list.  If this
     378             :      * fails, we must be out of PGPROC structures (not to mention semaphores).
     379             :      *
     380             :      * While we are holding the ProcStructLock, also copy the current shared
     381             :      * estimate of spins_per_delay to local storage.
     382             :      */
     383       30210 :     SpinLockAcquire(ProcStructLock);
     384             : 
     385       30210 :     set_spins_per_delay(ProcGlobal->spins_per_delay);
     386             : 
     387       30210 :     if (!dlist_is_empty(procgloballist))
     388             :     {
     389       30204 :         MyProc = dlist_container(PGPROC, links, dlist_pop_head_node(procgloballist));
     390       30204 :         SpinLockRelease(ProcStructLock);
     391             :     }
     392             :     else
     393             :     {
     394             :         /*
     395             :          * If we reach here, all the PGPROCs are in use.  This is one of the
     396             :          * possible places to detect "too many backends", so give the standard
     397             :          * error message.  XXX do we need to give a different failure message
     398             :          * in the autovacuum case?
     399             :          */
     400           6 :         SpinLockRelease(ProcStructLock);
     401           6 :         if (AmWalSenderProcess())
     402           4 :             ereport(FATAL,
     403             :                     (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
     404             :                      errmsg("number of requested standby connections exceeds \"max_wal_senders\" (currently %d)",
     405             :                             max_wal_senders)));
     406           2 :         ereport(FATAL,
     407             :                 (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
     408             :                  errmsg("sorry, too many clients already")));
     409             :     }
     410       30204 :     MyProcNumber = GetNumberFromPGProc(MyProc);
     411             : 
     412             :     /*
     413             :      * Cross-check that the PGPROC is of the type we expect; if this were not
     414             :      * the case, it would get returned to the wrong list.
     415             :      */
     416             :     Assert(MyProc->procgloballist == procgloballist);
     417             : 
     418             :     /*
     419             :      * Initialize all fields of MyProc, except for those previously
     420             :      * initialized by InitProcGlobal.
     421             :      */
     422       30204 :     dlist_node_init(&MyProc->links);
     423       30204 :     MyProc->waitStatus = PROC_WAIT_STATUS_OK;
     424       30204 :     MyProc->fpVXIDLock = false;
     425       30204 :     MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
     426       30204 :     MyProc->xid = InvalidTransactionId;
     427       30204 :     MyProc->xmin = InvalidTransactionId;
     428       30204 :     MyProc->pid = MyProcPid;
     429       30204 :     MyProc->vxid.procNumber = MyProcNumber;
     430       30204 :     MyProc->vxid.lxid = InvalidLocalTransactionId;
     431             :     /* databaseId and roleId will be filled in later */
     432       30204 :     MyProc->databaseId = InvalidOid;
     433       30204 :     MyProc->roleId = InvalidOid;
     434       30204 :     MyProc->tempNamespaceId = InvalidOid;
     435       30204 :     MyProc->isRegularBackend = AmRegularBackendProcess();
     436       30204 :     MyProc->delayChkptFlags = 0;
     437       30204 :     MyProc->statusFlags = 0;
     438             :     /* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */
     439       30204 :     if (AmAutoVacuumWorkerProcess())
     440        2268 :         MyProc->statusFlags |= PROC_IS_AUTOVACUUM;
     441       30204 :     MyProc->lwWaiting = LW_WS_NOT_WAITING;
     442       30204 :     MyProc->lwWaitMode = 0;
     443       30204 :     MyProc->waitLock = NULL;
     444       30204 :     MyProc->waitProcLock = NULL;
     445       30204 :     pg_atomic_write_u64(&MyProc->waitStart, 0);
     446             : #ifdef USE_ASSERT_CHECKING
     447             :     {
     448             :         int         i;
     449             : 
     450             :         /* Last process should have released all locks. */
     451             :         for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
     452             :             Assert(dlist_is_empty(&(MyProc->myProcLocks[i])));
     453             :     }
     454             : #endif
     455       30204 :     MyProc->recoveryConflictPending = false;
     456             : 
     457             :     /* Initialize fields for sync rep */
     458       30204 :     MyProc->waitLSN = 0;
     459       30204 :     MyProc->syncRepState = SYNC_REP_NOT_WAITING;
     460       30204 :     dlist_node_init(&MyProc->syncRepLinks);
     461             : 
     462             :     /* Initialize fields for group XID clearing. */
     463       30204 :     MyProc->procArrayGroupMember = false;
     464       30204 :     MyProc->procArrayGroupMemberXid = InvalidTransactionId;
     465             :     Assert(pg_atomic_read_u32(&MyProc->procArrayGroupNext) == INVALID_PROC_NUMBER);
     466             : 
     467             :     /* Check that group locking fields are in a proper initial state. */
     468             :     Assert(MyProc->lockGroupLeader == NULL);
     469             :     Assert(dlist_is_empty(&MyProc->lockGroupMembers));
     470             : 
     471             :     /* Initialize wait event information. */
     472       30204 :     MyProc->wait_event_info = 0;
     473             : 
     474             :     /* Initialize fields for group transaction status update. */
     475       30204 :     MyProc->clogGroupMember = false;
     476       30204 :     MyProc->clogGroupMemberXid = InvalidTransactionId;
     477       30204 :     MyProc->clogGroupMemberXidStatus = TRANSACTION_STATUS_IN_PROGRESS;
     478       30204 :     MyProc->clogGroupMemberPage = -1;
     479       30204 :     MyProc->clogGroupMemberLsn = InvalidXLogRecPtr;
     480             :     Assert(pg_atomic_read_u32(&MyProc->clogGroupNext) == INVALID_PROC_NUMBER);
     481             : 
     482             :     /*
     483             :      * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
     484             :      * on it.  That allows us to repoint the process latch, which so far
     485             :      * points to process local one, to the shared one.
     486             :      */
     487       30204 :     OwnLatch(&MyProc->procLatch);
     488       30204 :     SwitchToSharedLatch();
     489             : 
     490             :     /* now that we have a proc, report wait events to shared memory */
     491       30204 :     pgstat_set_wait_event_storage(&MyProc->wait_event_info);
     492             : 
     493             :     /*
     494             :      * We might be reusing a semaphore that belonged to a failed process. So
     495             :      * be careful and reinitialize its value here.  (This is not strictly
     496             :      * necessary anymore, but seems like a good idea for cleanliness.)
     497             :      */
     498       30204 :     PGSemaphoreReset(MyProc->sem);
     499             : 
     500             :     /*
     501             :      * Arrange to clean up at backend exit.
     502             :      */
     503       30204 :     on_shmem_exit(ProcKill, 0);
     504             : 
     505             :     /*
     506             :      * Now that we have a PGPROC, we could try to acquire locks, so initialize
     507             :      * local state needed for LWLocks, and the deadlock checker.
     508             :      */
     509       30204 :     InitLWLockAccess();
     510       30204 :     InitDeadLockChecking();
     511             : 
     512             : #ifdef EXEC_BACKEND
     513             : 
     514             :     /*
     515             :      * Initialize backend-local pointers to all the shared data structures.
     516             :      * (We couldn't do this until now because it needs LWLocks.)
     517             :      */
     518             :     if (IsUnderPostmaster)
     519             :         AttachSharedMemoryStructs();
     520             : #endif
     521       30204 : }
     522             : 
     523             : /*
     524             :  * InitProcessPhase2 -- make MyProc visible in the shared ProcArray.
     525             :  *
     526             :  * This is separate from InitProcess because we can't acquire LWLocks until
     527             :  * we've created a PGPROC, but in the EXEC_BACKEND case ProcArrayAdd won't
     528             :  * work until after we've done AttachSharedMemoryStructs.
     529             :  */
     530             : void
     531       30186 : InitProcessPhase2(void)
     532             : {
     533             :     Assert(MyProc != NULL);
     534             : 
     535             :     /*
     536             :      * Add our PGPROC to the PGPROC array in shared memory.
     537             :      */
     538       30186 :     ProcArrayAdd(MyProc);
     539             : 
     540             :     /*
     541             :      * Arrange to clean that up at backend exit.
     542             :      */
     543       30186 :     on_shmem_exit(RemoveProcFromArray, 0);
     544       30186 : }
     545             : 
     546             : /*
     547             :  * InitAuxiliaryProcess -- create a PGPROC entry for an auxiliary process
     548             :  *
     549             :  * This is called by bgwriter and similar processes so that they will have a
     550             :  * MyProc value that's real enough to let them wait for LWLocks.  The PGPROC
     551             :  * and sema that are assigned are one of the extra ones created during
     552             :  * InitProcGlobal.
     553             :  *
     554             :  * Auxiliary processes are presently not expected to wait for real (lockmgr)
     555             :  * locks, so we need not set up the deadlock checker.  They are never added
     556             :  * to the ProcArray or the sinval messaging mechanism, either.  They also
     557             :  * don't get a VXID assigned, since this is only useful when we actually
     558             :  * hold lockmgr locks.
     559             :  *
     560             :  * Startup process however uses locks but never waits for them in the
     561             :  * normal backend sense. Startup process also takes part in sinval messaging
     562             :  * as a sendOnly process, so never reads messages from sinval queue. So
     563             :  * Startup process does have a VXID and does show up in pg_locks.
     564             :  */
     565             : void
     566        4496 : InitAuxiliaryProcess(void)
     567             : {
     568             :     PGPROC     *auxproc;
     569             :     int         proctype;
     570             : 
     571             :     /*
     572             :      * ProcGlobal should be set up already (if we are a backend, we inherit
     573             :      * this by fork() or EXEC_BACKEND mechanism from the postmaster).
     574             :      */
     575        4496 :     if (ProcGlobal == NULL || AuxiliaryProcs == NULL)
     576           0 :         elog(PANIC, "proc header uninitialized");
     577             : 
     578        4496 :     if (MyProc != NULL)
     579           0 :         elog(ERROR, "you already exist");
     580             : 
     581        4496 :     if (IsUnderPostmaster)
     582        4496 :         RegisterPostmasterChildActive();
     583             : 
     584             :     /*
     585             :      * We use the ProcStructLock to protect assignment and releasing of
     586             :      * AuxiliaryProcs entries.
     587             :      *
     588             :      * While we are holding the ProcStructLock, also copy the current shared
     589             :      * estimate of spins_per_delay to local storage.
     590             :      */
     591        4496 :     SpinLockAcquire(ProcStructLock);
     592             : 
     593        4496 :     set_spins_per_delay(ProcGlobal->spins_per_delay);
     594             : 
     595             :     /*
     596             :      * Find a free auxproc ... *big* trouble if there isn't one ...
     597             :      */
     598       11176 :     for (proctype = 0; proctype < NUM_AUXILIARY_PROCS; proctype++)
     599             :     {
     600       11176 :         auxproc = &AuxiliaryProcs[proctype];
     601       11176 :         if (auxproc->pid == 0)
     602        4496 :             break;
     603             :     }
     604        4496 :     if (proctype >= NUM_AUXILIARY_PROCS)
     605             :     {
     606           0 :         SpinLockRelease(ProcStructLock);
     607           0 :         elog(FATAL, "all AuxiliaryProcs are in use");
     608             :     }
     609             : 
     610             :     /* Mark auxiliary proc as in use by me */
     611             :     /* use volatile pointer to prevent code rearrangement */
     612        4496 :     ((volatile PGPROC *) auxproc)->pid = MyProcPid;
     613             : 
     614        4496 :     SpinLockRelease(ProcStructLock);
     615             : 
     616        4496 :     MyProc = auxproc;
     617        4496 :     MyProcNumber = GetNumberFromPGProc(MyProc);
     618             : 
     619             :     /*
     620             :      * Initialize all fields of MyProc, except for those previously
     621             :      * initialized by InitProcGlobal.
     622             :      */
     623        4496 :     dlist_node_init(&MyProc->links);
     624        4496 :     MyProc->waitStatus = PROC_WAIT_STATUS_OK;
     625        4496 :     MyProc->fpVXIDLock = false;
     626        4496 :     MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
     627        4496 :     MyProc->xid = InvalidTransactionId;
     628        4496 :     MyProc->xmin = InvalidTransactionId;
     629        4496 :     MyProc->vxid.procNumber = INVALID_PROC_NUMBER;
     630        4496 :     MyProc->vxid.lxid = InvalidLocalTransactionId;
     631        4496 :     MyProc->databaseId = InvalidOid;
     632        4496 :     MyProc->roleId = InvalidOid;
     633        4496 :     MyProc->tempNamespaceId = InvalidOid;
     634        4496 :     MyProc->isRegularBackend = false;
     635        4496 :     MyProc->delayChkptFlags = 0;
     636        4496 :     MyProc->statusFlags = 0;
     637        4496 :     MyProc->lwWaiting = LW_WS_NOT_WAITING;
     638        4496 :     MyProc->lwWaitMode = 0;
     639        4496 :     MyProc->waitLock = NULL;
     640        4496 :     MyProc->waitProcLock = NULL;
     641        4496 :     pg_atomic_write_u64(&MyProc->waitStart, 0);
     642             : #ifdef USE_ASSERT_CHECKING
     643             :     {
     644             :         int         i;
     645             : 
     646             :         /* Last process should have released all locks. */
     647             :         for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
     648             :             Assert(dlist_is_empty(&(MyProc->myProcLocks[i])));
     649             :     }
     650             : #endif
     651             : 
     652             :     /*
     653             :      * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
     654             :      * on it.  That allows us to repoint the process latch, which so far
     655             :      * points to process local one, to the shared one.
     656             :      */
     657        4496 :     OwnLatch(&MyProc->procLatch);
     658        4496 :     SwitchToSharedLatch();
     659             : 
     660             :     /* now that we have a proc, report wait events to shared memory */
     661        4496 :     pgstat_set_wait_event_storage(&MyProc->wait_event_info);
     662             : 
     663             :     /* Check that group locking fields are in a proper initial state. */
     664             :     Assert(MyProc->lockGroupLeader == NULL);
     665             :     Assert(dlist_is_empty(&MyProc->lockGroupMembers));
     666             : 
     667             :     /*
     668             :      * We might be reusing a semaphore that belonged to a failed process. So
     669             :      * be careful and reinitialize its value here.  (This is not strictly
     670             :      * necessary anymore, but seems like a good idea for cleanliness.)
     671             :      */
     672        4496 :     PGSemaphoreReset(MyProc->sem);
     673             : 
     674             :     /*
     675             :      * Arrange to clean up at process exit.
     676             :      */
     677        4496 :     on_shmem_exit(AuxiliaryProcKill, Int32GetDatum(proctype));
     678             : 
     679             :     /*
     680             :      * Now that we have a PGPROC, we could try to acquire lightweight locks.
     681             :      * Initialize local state needed for them.  (Heavyweight locks cannot be
     682             :      * acquired in aux processes.)
     683             :      */
     684        4496 :     InitLWLockAccess();
     685             : 
     686             : #ifdef EXEC_BACKEND
     687             : 
     688             :     /*
     689             :      * Initialize backend-local pointers to all the shared data structures.
     690             :      * (We couldn't do this until now because it needs LWLocks.)
     691             :      */
     692             :     if (IsUnderPostmaster)
     693             :         AttachSharedMemoryStructs();
     694             : #endif
     695        4496 : }
     696             : 
     697             : /*
     698             :  * Used from bufmgr to share the value of the buffer that Startup waits on,
     699             :  * or to reset the value to "not waiting" (-1). This allows processing
     700             :  * of recovery conflicts for buffer pins. Set is made before backends look
     701             :  * at this value, so locking not required, especially since the set is
     702             :  * an atomic integer set operation.
     703             :  */
     704             : void
     705          40 : SetStartupBufferPinWaitBufId(int bufid)
     706             : {
     707             :     /* use volatile pointer to prevent code rearrangement */
     708          40 :     volatile PROC_HDR *procglobal = ProcGlobal;
     709             : 
     710          40 :     procglobal->startupBufferPinWaitBufId = bufid;
     711          40 : }
     712             : 
     713             : /*
     714             :  * Used by backends when they receive a request to check for buffer pin waits.
     715             :  */
     716             : int
     717          10 : GetStartupBufferPinWaitBufId(void)
     718             : {
     719             :     /* use volatile pointer to prevent code rearrangement */
     720          10 :     volatile PROC_HDR *procglobal = ProcGlobal;
     721             : 
     722          10 :     return procglobal->startupBufferPinWaitBufId;
     723             : }
     724             : 
     725             : /*
     726             :  * Check whether there are at least N free PGPROC objects.  If false is
     727             :  * returned, *nfree will be set to the number of free PGPROC objects.
     728             :  * Otherwise, *nfree will be set to n.
     729             :  *
     730             :  * Note: this is designed on the assumption that N will generally be small.
     731             :  */
     732             : bool
     733         446 : HaveNFreeProcs(int n, int *nfree)
     734             : {
     735             :     dlist_iter  iter;
     736             : 
     737             :     Assert(n > 0);
     738             :     Assert(nfree);
     739             : 
     740         446 :     SpinLockAcquire(ProcStructLock);
     741             : 
     742         446 :     *nfree = 0;
     743        1332 :     dlist_foreach(iter, &ProcGlobal->freeProcs)
     744             :     {
     745        1324 :         (*nfree)++;
     746        1324 :         if (*nfree == n)
     747         438 :             break;
     748             :     }
     749             : 
     750         446 :     SpinLockRelease(ProcStructLock);
     751             : 
     752         446 :     return (*nfree == n);
     753             : }
     754             : 
     755             : /*
     756             :  * Cancel any pending wait for lock, when aborting a transaction, and revert
     757             :  * any strong lock count acquisition for a lock being acquired.
     758             :  *
     759             :  * (Normally, this would only happen if we accept a cancel/die
     760             :  * interrupt while waiting; but an ereport(ERROR) before or during the lock
     761             :  * wait is within the realm of possibility, too.)
     762             :  */
     763             : void
     764      843614 : LockErrorCleanup(void)
     765             : {
     766             :     LOCALLOCK  *lockAwaited;
     767             :     LWLock     *partitionLock;
     768             :     DisableTimeoutParams timeouts[2];
     769             : 
     770      843614 :     HOLD_INTERRUPTS();
     771             : 
     772      843614 :     AbortStrongLockAcquire();
     773             : 
     774             :     /* Nothing to do if we weren't waiting for a lock */
     775      843614 :     lockAwaited = GetAwaitedLock();
     776      843614 :     if (lockAwaited == NULL)
     777             :     {
     778      843266 :         RESUME_INTERRUPTS();
     779      843266 :         return;
     780             :     }
     781             : 
     782             :     /*
     783             :      * Turn off the deadlock and lock timeout timers, if they are still
     784             :      * running (see ProcSleep).  Note we must preserve the LOCK_TIMEOUT
     785             :      * indicator flag, since this function is executed before
     786             :      * ProcessInterrupts when responding to SIGINT; else we'd lose the
     787             :      * knowledge that the SIGINT came from a lock timeout and not an external
     788             :      * source.
     789             :      */
     790         348 :     timeouts[0].id = DEADLOCK_TIMEOUT;
     791         348 :     timeouts[0].keep_indicator = false;
     792         348 :     timeouts[1].id = LOCK_TIMEOUT;
     793         348 :     timeouts[1].keep_indicator = true;
     794         348 :     disable_timeouts(timeouts, 2);
     795             : 
     796             :     /* Unlink myself from the wait queue, if on it (might not be anymore!) */
     797         348 :     partitionLock = LockHashPartitionLock(lockAwaited->hashcode);
     798         348 :     LWLockAcquire(partitionLock, LW_EXCLUSIVE);
     799             : 
     800         348 :     if (!dlist_node_is_detached(&MyProc->links))
     801             :     {
     802             :         /* We could not have been granted the lock yet */
     803          80 :         RemoveFromWaitQueue(MyProc, lockAwaited->hashcode);
     804             :     }
     805             :     else
     806             :     {
     807             :         /*
     808             :          * Somebody kicked us off the lock queue already.  Perhaps they
     809             :          * granted us the lock, or perhaps they detected a deadlock. If they
     810             :          * did grant us the lock, we'd better remember it in our local lock
     811             :          * table.
     812             :          */
     813         268 :         if (MyProc->waitStatus == PROC_WAIT_STATUS_OK)
     814           4 :             GrantAwaitedLock();
     815             :     }
     816             : 
     817         348 :     LWLockRelease(partitionLock);
     818             : 
     819         348 :     RESUME_INTERRUPTS();
     820             : }
     821             : 
     822             : 
     823             : /*
     824             :  * ProcReleaseLocks() -- release locks associated with current transaction
     825             :  *          at main transaction commit or abort
     826             :  *
     827             :  * At main transaction commit, we release standard locks except session locks.
     828             :  * At main transaction abort, we release all locks including session locks.
     829             :  *
     830             :  * Advisory locks are released only if they are transaction-level;
     831             :  * session-level holds remain, whether this is a commit or not.
     832             :  *
     833             :  * At subtransaction commit, we don't release any locks (so this func is not
     834             :  * needed at all); we will defer the releasing to the parent transaction.
     835             :  * At subtransaction abort, we release all locks held by the subtransaction;
     836             :  * this is implemented by retail releasing of the locks under control of
     837             :  * the ResourceOwner mechanism.
     838             :  */
     839             : void
     840      786098 : ProcReleaseLocks(bool isCommit)
     841             : {
     842      786098 :     if (!MyProc)
     843           0 :         return;
     844             :     /* If waiting, get off wait queue (should only be needed after error) */
     845      786098 :     LockErrorCleanup();
     846             :     /* Release standard locks, including session-level if aborting */
     847      786098 :     LockReleaseAll(DEFAULT_LOCKMETHOD, !isCommit);
     848             :     /* Release transaction-level advisory locks */
     849      786098 :     LockReleaseAll(USER_LOCKMETHOD, false);
     850             : }
     851             : 
     852             : 
     853             : /*
     854             :  * RemoveProcFromArray() -- Remove this process from the shared ProcArray.
     855             :  */
     856             : static void
     857       30186 : RemoveProcFromArray(int code, Datum arg)
     858             : {
     859             :     Assert(MyProc != NULL);
     860       30186 :     ProcArrayRemove(MyProc, InvalidTransactionId);
     861       30186 : }
     862             : 
     863             : /*
     864             :  * ProcKill() -- Destroy the per-proc data structure for
     865             :  *      this process. Release any of its held LW locks.
     866             :  */
     867             : static void
     868       30204 : ProcKill(int code, Datum arg)
     869             : {
     870             :     PGPROC     *proc;
     871             :     dlist_head *procgloballist;
     872             : 
     873             :     Assert(MyProc != NULL);
     874             : 
     875             :     /* not safe if forked by system(), etc. */
     876       30204 :     if (MyProc->pid != (int) getpid())
     877           0 :         elog(PANIC, "ProcKill() called in child process");
     878             : 
     879             :     /* Make sure we're out of the sync rep lists */
     880       30204 :     SyncRepCleanupAtProcExit();
     881             : 
     882             : #ifdef USE_ASSERT_CHECKING
     883             :     {
     884             :         int         i;
     885             : 
     886             :         /* Last process should have released all locks. */
     887             :         for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
     888             :             Assert(dlist_is_empty(&(MyProc->myProcLocks[i])));
     889             :     }
     890             : #endif
     891             : 
     892             :     /*
     893             :      * Release any LW locks I am holding.  There really shouldn't be any, but
     894             :      * it's cheap to check again before we cut the knees off the LWLock
     895             :      * facility by releasing our PGPROC ...
     896             :      */
     897       30204 :     LWLockReleaseAll();
     898             : 
     899             :     /* Cancel any pending condition variable sleep, too */
     900       30204 :     ConditionVariableCancelSleep();
     901             : 
     902             :     /*
     903             :      * Detach from any lock group of which we are a member.  If the leader
     904             :      * exits before all other group members, its PGPROC will remain allocated
     905             :      * until the last group process exits; that process must return the
     906             :      * leader's PGPROC to the appropriate list.
     907             :      */
     908       30204 :     if (MyProc->lockGroupLeader != NULL)
     909             :     {
     910        2850 :         PGPROC     *leader = MyProc->lockGroupLeader;
     911        2850 :         LWLock     *leader_lwlock = LockHashPartitionLockByProc(leader);
     912             : 
     913        2850 :         LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
     914             :         Assert(!dlist_is_empty(&leader->lockGroupMembers));
     915        2850 :         dlist_delete(&MyProc->lockGroupLink);
     916        2850 :         if (dlist_is_empty(&leader->lockGroupMembers))
     917             :         {
     918         138 :             leader->lockGroupLeader = NULL;
     919         138 :             if (leader != MyProc)
     920             :             {
     921           0 :                 procgloballist = leader->procgloballist;
     922             : 
     923             :                 /* Leader exited first; return its PGPROC. */
     924           0 :                 SpinLockAcquire(ProcStructLock);
     925           0 :                 dlist_push_head(procgloballist, &leader->links);
     926           0 :                 SpinLockRelease(ProcStructLock);
     927             :             }
     928             :         }
     929        2712 :         else if (leader != MyProc)
     930        2712 :             MyProc->lockGroupLeader = NULL;
     931        2850 :         LWLockRelease(leader_lwlock);
     932             :     }
     933             : 
     934             :     /*
     935             :      * Reset MyLatch to the process local one.  This is so that signal
     936             :      * handlers et al can continue using the latch after the shared latch
     937             :      * isn't ours anymore.
     938             :      *
     939             :      * Similarly, stop reporting wait events to MyProc->wait_event_info.
     940             :      *
     941             :      * After that clear MyProc and disown the shared latch.
     942             :      */
     943       30204 :     SwitchBackToLocalLatch();
     944       30204 :     pgstat_reset_wait_event_storage();
     945             : 
     946       30204 :     proc = MyProc;
     947       30204 :     MyProc = NULL;
     948       30204 :     MyProcNumber = INVALID_PROC_NUMBER;
     949       30204 :     DisownLatch(&proc->procLatch);
     950             : 
     951             :     /* Mark the proc no longer in use */
     952       30204 :     proc->pid = 0;
     953       30204 :     proc->vxid.procNumber = INVALID_PROC_NUMBER;
     954       30204 :     proc->vxid.lxid = InvalidTransactionId;
     955             : 
     956       30204 :     procgloballist = proc->procgloballist;
     957       30204 :     SpinLockAcquire(ProcStructLock);
     958             : 
     959             :     /*
     960             :      * If we're still a member of a locking group, that means we're a leader
     961             :      * which has somehow exited before its children.  The last remaining child
     962             :      * will release our PGPROC.  Otherwise, release it now.
     963             :      */
     964       30204 :     if (proc->lockGroupLeader == NULL)
     965             :     {
     966             :         /* Since lockGroupLeader is NULL, lockGroupMembers should be empty. */
     967             :         Assert(dlist_is_empty(&proc->lockGroupMembers));
     968             : 
     969             :         /* Return PGPROC structure (and semaphore) to appropriate freelist */
     970       30204 :         dlist_push_tail(procgloballist, &proc->links);
     971             :     }
     972             : 
     973             :     /* Update shared estimate of spins_per_delay */
     974       30204 :     ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);
     975             : 
     976       30204 :     SpinLockRelease(ProcStructLock);
     977             : 
     978             :     /* wake autovac launcher if needed -- see comments in FreeWorkerInfo */
     979       30204 :     if (AutovacuumLauncherPid != 0)
     980        2264 :         kill(AutovacuumLauncherPid, SIGUSR2);
     981       30204 : }
     982             : 
     983             : /*
     984             :  * AuxiliaryProcKill() -- Cut-down version of ProcKill for auxiliary
     985             :  *      processes (bgwriter, etc).  The PGPROC and sema are not released, only
     986             :  *      marked as not-in-use.
     987             :  */
     988             : static void
     989        4496 : AuxiliaryProcKill(int code, Datum arg)
     990             : {
     991        4496 :     int         proctype = DatumGetInt32(arg);
     992             :     PGPROC     *auxproc PG_USED_FOR_ASSERTS_ONLY;
     993             :     PGPROC     *proc;
     994             : 
     995             :     Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
     996             : 
     997             :     /* not safe if forked by system(), etc. */
     998        4496 :     if (MyProc->pid != (int) getpid())
     999           0 :         elog(PANIC, "AuxiliaryProcKill() called in child process");
    1000             : 
    1001        4496 :     auxproc = &AuxiliaryProcs[proctype];
    1002             : 
    1003             :     Assert(MyProc == auxproc);
    1004             : 
    1005             :     /* Release any LW locks I am holding (see notes above) */
    1006        4496 :     LWLockReleaseAll();
    1007             : 
    1008             :     /* Cancel any pending condition variable sleep, too */
    1009        4496 :     ConditionVariableCancelSleep();
    1010             : 
    1011             :     /* look at the equivalent ProcKill() code for comments */
    1012        4496 :     SwitchBackToLocalLatch();
    1013        4496 :     pgstat_reset_wait_event_storage();
    1014             : 
    1015        4496 :     proc = MyProc;
    1016        4496 :     MyProc = NULL;
    1017        4496 :     MyProcNumber = INVALID_PROC_NUMBER;
    1018        4496 :     DisownLatch(&proc->procLatch);
    1019             : 
    1020        4496 :     SpinLockAcquire(ProcStructLock);
    1021             : 
    1022             :     /* Mark auxiliary proc no longer in use */
    1023        4496 :     proc->pid = 0;
    1024        4496 :     proc->vxid.procNumber = INVALID_PROC_NUMBER;
    1025        4496 :     proc->vxid.lxid = InvalidTransactionId;
    1026             : 
    1027             :     /* Update shared estimate of spins_per_delay */
    1028        4496 :     ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);
    1029             : 
    1030        4496 :     SpinLockRelease(ProcStructLock);
    1031        4496 : }
    1032             : 
    1033             : /*
    1034             :  * AuxiliaryPidGetProc -- get PGPROC for an auxiliary process
    1035             :  * given its PID
    1036             :  *
    1037             :  * Returns NULL if not found.
    1038             :  */
    1039             : PGPROC *
    1040        4434 : AuxiliaryPidGetProc(int pid)
    1041             : {
    1042        4434 :     PGPROC     *result = NULL;
    1043             :     int         index;
    1044             : 
    1045        4434 :     if (pid == 0)               /* never match dummy PGPROCs */
    1046           0 :         return NULL;
    1047             : 
    1048        9054 :     for (index = 0; index < NUM_AUXILIARY_PROCS; index++)
    1049             :     {
    1050        9054 :         PGPROC     *proc = &AuxiliaryProcs[index];
    1051             : 
    1052        9054 :         if (proc->pid == pid)
    1053             :         {
    1054        4434 :             result = proc;
    1055        4434 :             break;
    1056             :         }
    1057             :     }
    1058        4434 :     return result;
    1059             : }
    1060             : 
    1061             : 
    1062             : /*
    1063             :  * JoinWaitQueue -- join the wait queue on the specified lock
    1064             :  *
    1065             :  * It's not actually guaranteed that we need to wait when this function is
    1066             :  * called, because it could be that when we try to find a position at which
    1067             :  * to insert ourself into the wait queue, we discover that we must be inserted
    1068             :  * ahead of everyone who wants a lock that conflict with ours. In that case,
    1069             :  * we get the lock immediately. Because of this, it's sensible for this function
    1070             :  * to have a dontWait argument, despite the name.
    1071             :  *
    1072             :  * On entry, the caller has already set up LOCK and PROCLOCK entries to
    1073             :  * reflect that we have "requested" the lock.  The caller is responsible for
    1074             :  * cleaning that up, if we end up not joining the queue after all.
    1075             :  *
    1076             :  * The lock table's partition lock must be held at entry, and is still held
    1077             :  * at exit.  The caller must release it before calling ProcSleep().
    1078             :  *
    1079             :  * Result is one of the following:
    1080             :  *
    1081             :  *  PROC_WAIT_STATUS_OK       - lock was immediately granted
    1082             :  *  PROC_WAIT_STATUS_WAITING  - joined the wait queue; call ProcSleep()
    1083             :  *  PROC_WAIT_STATUS_ERROR    - immediate deadlock was detected, or would
    1084             :  *                              need to wait and dontWait == true
    1085             :  *
    1086             :  * NOTES: The process queue is now a priority queue for locking.
    1087             :  */
    1088             : ProcWaitStatus
    1089        3830 : JoinWaitQueue(LOCALLOCK *locallock, LockMethod lockMethodTable, bool dontWait)
    1090             : {
    1091        3830 :     LOCKMODE    lockmode = locallock->tag.mode;
    1092        3830 :     LOCK       *lock = locallock->lock;
    1093        3830 :     PROCLOCK   *proclock = locallock->proclock;
    1094        3830 :     uint32      hashcode = locallock->hashcode;
    1095        3830 :     LWLock     *partitionLock PG_USED_FOR_ASSERTS_ONLY = LockHashPartitionLock(hashcode);
    1096        3830 :     dclist_head *waitQueue = &lock->waitProcs;
    1097        3830 :     PGPROC     *insert_before = NULL;
    1098             :     LOCKMASK    myProcHeldLocks;
    1099             :     LOCKMASK    myHeldLocks;
    1100        3830 :     bool        early_deadlock = false;
    1101        3830 :     PGPROC     *leader = MyProc->lockGroupLeader;
    1102             : 
    1103             :     Assert(LWLockHeldByMeInMode(partitionLock, LW_EXCLUSIVE));
    1104             : 
    1105             :     /*
    1106             :      * Set bitmask of locks this process already holds on this object.
    1107             :      */
    1108        3830 :     myHeldLocks = MyProc->heldLocks = proclock->holdMask;
    1109             : 
    1110             :     /*
    1111             :      * Determine which locks we're already holding.
    1112             :      *
    1113             :      * If group locking is in use, locks held by members of my locking group
    1114             :      * need to be included in myHeldLocks.  This is not required for relation
    1115             :      * extension lock which conflict among group members. However, including
    1116             :      * them in myHeldLocks will give group members the priority to get those
    1117             :      * locks as compared to other backends which are also trying to acquire
    1118             :      * those locks.  OTOH, we can avoid giving priority to group members for
    1119             :      * that kind of locks, but there doesn't appear to be a clear advantage of
    1120             :      * the same.
    1121             :      */
    1122        3830 :     myProcHeldLocks = proclock->holdMask;
    1123        3830 :     myHeldLocks = myProcHeldLocks;
    1124        3830 :     if (leader != NULL)
    1125             :     {
    1126             :         dlist_iter  iter;
    1127             : 
    1128          66 :         dlist_foreach(iter, &lock->procLocks)
    1129             :         {
    1130             :             PROCLOCK   *otherproclock;
    1131             : 
    1132          50 :             otherproclock = dlist_container(PROCLOCK, lockLink, iter.cur);
    1133             : 
    1134          50 :             if (otherproclock->groupLeader == leader)
    1135          24 :                 myHeldLocks |= otherproclock->holdMask;
    1136             :         }
    1137             :     }
    1138             : 
    1139             :     /*
    1140             :      * Determine where to add myself in the wait queue.
    1141             :      *
    1142             :      * Normally I should go at the end of the queue.  However, if I already
    1143             :      * hold locks that conflict with the request of any previous waiter, put
    1144             :      * myself in the queue just in front of the first such waiter. This is not
    1145             :      * a necessary step, since deadlock detection would move me to before that
    1146             :      * waiter anyway; but it's relatively cheap to detect such a conflict
    1147             :      * immediately, and avoid delaying till deadlock timeout.
    1148             :      *
    1149             :      * Special case: if I find I should go in front of some waiter, check to
    1150             :      * see if I conflict with already-held locks or the requests before that
    1151             :      * waiter.  If not, then just grant myself the requested lock immediately.
    1152             :      * This is the same as the test for immediate grant in LockAcquire, except
    1153             :      * we are only considering the part of the wait queue before my insertion
    1154             :      * point.
    1155             :      */
    1156        3830 :     if (myHeldLocks != 0 && !dclist_is_empty(waitQueue))
    1157             :     {
    1158          12 :         LOCKMASK    aheadRequests = 0;
    1159             :         dlist_iter  iter;
    1160             : 
    1161          12 :         dclist_foreach(iter, waitQueue)
    1162             :         {
    1163          12 :             PGPROC     *proc = dlist_container(PGPROC, links, iter.cur);
    1164             : 
    1165             :             /*
    1166             :              * If we're part of the same locking group as this waiter, its
    1167             :              * locks neither conflict with ours nor contribute to
    1168             :              * aheadRequests.
    1169             :              */
    1170          12 :             if (leader != NULL && leader == proc->lockGroupLeader)
    1171           0 :                 continue;
    1172             : 
    1173             :             /* Must he wait for me? */
    1174          12 :             if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
    1175             :             {
    1176             :                 /* Must I wait for him ? */
    1177          12 :                 if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
    1178             :                 {
    1179             :                     /*
    1180             :                      * Yes, so we have a deadlock.  Easiest way to clean up
    1181             :                      * correctly is to call RemoveFromWaitQueue(), but we
    1182             :                      * can't do that until we are *on* the wait queue. So, set
    1183             :                      * a flag to check below, and break out of loop.  Also,
    1184             :                      * record deadlock info for later message.
    1185             :                      */
    1186           2 :                     RememberSimpleDeadLock(MyProc, lockmode, lock, proc);
    1187           2 :                     early_deadlock = true;
    1188           2 :                     break;
    1189             :                 }
    1190             :                 /* I must go before this waiter.  Check special case. */
    1191          10 :                 if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
    1192          10 :                     !LockCheckConflicts(lockMethodTable, lockmode, lock,
    1193             :                                         proclock))
    1194             :                 {
    1195             :                     /* Skip the wait and just grant myself the lock. */
    1196          10 :                     GrantLock(lock, proclock, lockmode);
    1197          10 :                     return PROC_WAIT_STATUS_OK;
    1198             :                 }
    1199             : 
    1200             :                 /* Put myself into wait queue before conflicting process */
    1201           0 :                 insert_before = proc;
    1202           0 :                 break;
    1203             :             }
    1204             :             /* Nope, so advance to next waiter */
    1205           0 :             aheadRequests |= LOCKBIT_ON(proc->waitLockMode);
    1206             :         }
    1207             :     }
    1208             : 
    1209             :     /*
    1210             :      * If we detected deadlock, give up without waiting.  This must agree with
    1211             :      * CheckDeadLock's recovery code.
    1212             :      */
    1213        3820 :     if (early_deadlock)
    1214           2 :         return PROC_WAIT_STATUS_ERROR;
    1215             : 
    1216             :     /*
    1217             :      * At this point we know that we'd really need to sleep. If we've been
    1218             :      * commanded not to do that, bail out.
    1219             :      */
    1220        3818 :     if (dontWait)
    1221        1288 :         return PROC_WAIT_STATUS_ERROR;
    1222             : 
    1223             :     /*
    1224             :      * Insert self into queue, at the position determined above.
    1225             :      */
    1226        2530 :     if (insert_before)
    1227           0 :         dclist_insert_before(waitQueue, &insert_before->links, &MyProc->links);
    1228             :     else
    1229        2530 :         dclist_push_tail(waitQueue, &MyProc->links);
    1230             : 
    1231        2530 :     lock->waitMask |= LOCKBIT_ON(lockmode);
    1232             : 
    1233             :     /* Set up wait information in PGPROC object, too */
    1234        2530 :     MyProc->heldLocks = myProcHeldLocks;
    1235        2530 :     MyProc->waitLock = lock;
    1236        2530 :     MyProc->waitProcLock = proclock;
    1237        2530 :     MyProc->waitLockMode = lockmode;
    1238             : 
    1239        2530 :     MyProc->waitStatus = PROC_WAIT_STATUS_WAITING;
    1240             : 
    1241        2530 :     return PROC_WAIT_STATUS_WAITING;
    1242             : }
    1243             : 
    1244             : /*
    1245             :  * ProcSleep -- put process to sleep waiting on lock
    1246             :  *
    1247             :  * This must be called when JoinWaitQueue() returns PROC_WAIT_STATUS_WAITING.
    1248             :  * Returns after the lock has been granted, or if a deadlock is detected.  Can
    1249             :  * also bail out with ereport(ERROR), if some other error condition, or a
    1250             :  * timeout or cancellation is triggered.
    1251             :  *
    1252             :  * Result is one of the following:
    1253             :  *
    1254             :  *  PROC_WAIT_STATUS_OK      - lock was granted
    1255             :  *  PROC_WAIT_STATUS_ERROR   - a deadlock was detected
    1256             :  */
    1257             : ProcWaitStatus
    1258        2530 : ProcSleep(LOCALLOCK *locallock)
    1259             : {
    1260        2530 :     LOCKMODE    lockmode = locallock->tag.mode;
    1261        2530 :     LOCK       *lock = locallock->lock;
    1262        2530 :     uint32      hashcode = locallock->hashcode;
    1263        2530 :     LWLock     *partitionLock = LockHashPartitionLock(hashcode);
    1264        2530 :     TimestampTz standbyWaitStart = 0;
    1265        2530 :     bool        allow_autovacuum_cancel = true;
    1266        2530 :     bool        logged_recovery_conflict = false;
    1267             :     ProcWaitStatus myWaitStatus;
    1268             : 
    1269             :     /* The caller must've armed the on-error cleanup mechanism */
    1270             :     Assert(GetAwaitedLock() == locallock);
    1271             :     Assert(!LWLockHeldByMe(partitionLock));
    1272             : 
    1273             :     /*
    1274             :      * Now that we will successfully clean up after an ereport, it's safe to
    1275             :      * check to see if there's a buffer pin deadlock against the Startup
    1276             :      * process.  Of course, that's only necessary if we're doing Hot Standby
    1277             :      * and are not the Startup process ourselves.
    1278             :      */
    1279        2530 :     if (RecoveryInProgress() && !InRecovery)
    1280           2 :         CheckRecoveryConflictDeadlock();
    1281             : 
    1282             :     /* Reset deadlock_state before enabling the timeout handler */
    1283        2530 :     deadlock_state = DS_NOT_YET_CHECKED;
    1284        2530 :     got_deadlock_timeout = false;
    1285             : 
    1286             :     /*
    1287             :      * Set timer so we can wake up after awhile and check for a deadlock. If a
    1288             :      * deadlock is detected, the handler sets MyProc->waitStatus =
    1289             :      * PROC_WAIT_STATUS_ERROR, allowing us to know that we must report failure
    1290             :      * rather than success.
    1291             :      *
    1292             :      * By delaying the check until we've waited for a bit, we can avoid
    1293             :      * running the rather expensive deadlock-check code in most cases.
    1294             :      *
    1295             :      * If LockTimeout is set, also enable the timeout for that.  We can save a
    1296             :      * few cycles by enabling both timeout sources in one call.
    1297             :      *
    1298             :      * If InHotStandby we set lock waits slightly later for clarity with other
    1299             :      * code.
    1300             :      */
    1301        2530 :     if (!InHotStandby)
    1302             :     {
    1303        2526 :         if (LockTimeout > 0)
    1304             :         {
    1305             :             EnableTimeoutParams timeouts[2];
    1306             : 
    1307         222 :             timeouts[0].id = DEADLOCK_TIMEOUT;
    1308         222 :             timeouts[0].type = TMPARAM_AFTER;
    1309         222 :             timeouts[0].delay_ms = DeadlockTimeout;
    1310         222 :             timeouts[1].id = LOCK_TIMEOUT;
    1311         222 :             timeouts[1].type = TMPARAM_AFTER;
    1312         222 :             timeouts[1].delay_ms = LockTimeout;
    1313         222 :             enable_timeouts(timeouts, 2);
    1314             :         }
    1315             :         else
    1316        2304 :             enable_timeout_after(DEADLOCK_TIMEOUT, DeadlockTimeout);
    1317             : 
    1318             :         /*
    1319             :          * Use the current time obtained for the deadlock timeout timer as
    1320             :          * waitStart (i.e., the time when this process started waiting for the
    1321             :          * lock). Since getting the current time newly can cause overhead, we
    1322             :          * reuse the already-obtained time to avoid that overhead.
    1323             :          *
    1324             :          * Note that waitStart is updated without holding the lock table's
    1325             :          * partition lock, to avoid the overhead by additional lock
    1326             :          * acquisition. This can cause "waitstart" in pg_locks to become NULL
    1327             :          * for a very short period of time after the wait started even though
    1328             :          * "granted" is false. This is OK in practice because we can assume
    1329             :          * that users are likely to look at "waitstart" when waiting for the
    1330             :          * lock for a long time.
    1331             :          */
    1332        2526 :         pg_atomic_write_u64(&MyProc->waitStart,
    1333        2526 :                             get_timeout_start_time(DEADLOCK_TIMEOUT));
    1334             :     }
    1335           4 :     else if (log_recovery_conflict_waits)
    1336             :     {
    1337             :         /*
    1338             :          * Set the wait start timestamp if logging is enabled and in hot
    1339             :          * standby.
    1340             :          */
    1341           2 :         standbyWaitStart = GetCurrentTimestamp();
    1342             :     }
    1343             : 
    1344             :     /*
    1345             :      * If somebody wakes us between LWLockRelease and WaitLatch, the latch
    1346             :      * will not wait. But a set latch does not necessarily mean that the lock
    1347             :      * is free now, as there are many other sources for latch sets than
    1348             :      * somebody releasing the lock.
    1349             :      *
    1350             :      * We process interrupts whenever the latch has been set, so cancel/die
    1351             :      * interrupts are processed quickly. This means we must not mind losing
    1352             :      * control to a cancel/die interrupt here.  We don't, because we have no
    1353             :      * shared-state-change work to do after being granted the lock (the
    1354             :      * grantor did it all).  We do have to worry about canceling the deadlock
    1355             :      * timeout and updating the locallock table, but if we lose control to an
    1356             :      * error, LockErrorCleanup will fix that up.
    1357             :      */
    1358             :     do
    1359             :     {
    1360        4796 :         if (InHotStandby)
    1361             :         {
    1362          12 :             bool        maybe_log_conflict =
    1363          12 :                 (standbyWaitStart != 0 && !logged_recovery_conflict);
    1364             : 
    1365             :             /* Set a timer and wait for that or for the lock to be granted */
    1366          12 :             ResolveRecoveryConflictWithLock(locallock->tag.lock,
    1367             :                                             maybe_log_conflict);
    1368             : 
    1369             :             /*
    1370             :              * Emit the log message if the startup process is waiting longer
    1371             :              * than deadlock_timeout for recovery conflict on lock.
    1372             :              */
    1373          12 :             if (maybe_log_conflict)
    1374             :             {
    1375           4 :                 TimestampTz now = GetCurrentTimestamp();
    1376             : 
    1377           4 :                 if (TimestampDifferenceExceeds(standbyWaitStart, now,
    1378             :                                                DeadlockTimeout))
    1379             :                 {
    1380             :                     VirtualTransactionId *vxids;
    1381             :                     int         cnt;
    1382             : 
    1383           2 :                     vxids = GetLockConflicts(&locallock->tag.lock,
    1384             :                                              AccessExclusiveLock, &cnt);
    1385             : 
    1386             :                     /*
    1387             :                      * Log the recovery conflict and the list of PIDs of
    1388             :                      * backends holding the conflicting lock. Note that we do
    1389             :                      * logging even if there are no such backends right now
    1390             :                      * because the startup process here has already waited
    1391             :                      * longer than deadlock_timeout.
    1392             :                      */
    1393           2 :                     LogRecoveryConflict(PROCSIG_RECOVERY_CONFLICT_LOCK,
    1394             :                                         standbyWaitStart, now,
    1395           2 :                                         cnt > 0 ? vxids : NULL, true);
    1396           2 :                     logged_recovery_conflict = true;
    1397             :                 }
    1398             :             }
    1399             :         }
    1400             :         else
    1401             :         {
    1402        4784 :             (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
    1403        4784 :                              PG_WAIT_LOCK | locallock->tag.lock.locktag_type);
    1404        4784 :             ResetLatch(MyLatch);
    1405             :             /* check for deadlocks first, as that's probably log-worthy */
    1406        4784 :             if (got_deadlock_timeout)
    1407             :             {
    1408          58 :                 CheckDeadLock();
    1409          58 :                 got_deadlock_timeout = false;
    1410             :             }
    1411        4784 :             CHECK_FOR_INTERRUPTS();
    1412             :         }
    1413             : 
    1414             :         /*
    1415             :          * waitStatus could change from PROC_WAIT_STATUS_WAITING to something
    1416             :          * else asynchronously.  Read it just once per loop to prevent
    1417             :          * surprising behavior (such as missing log messages).
    1418             :          */
    1419        4714 :         myWaitStatus = *((volatile ProcWaitStatus *) &MyProc->waitStatus);
    1420             : 
    1421             :         /*
    1422             :          * If we are not deadlocked, but are waiting on an autovacuum-induced
    1423             :          * task, send a signal to interrupt it.
    1424             :          */
    1425        4714 :         if (deadlock_state == DS_BLOCKED_BY_AUTOVACUUM && allow_autovacuum_cancel)
    1426             :         {
    1427           0 :             PGPROC     *autovac = GetBlockingAutoVacuumPgproc();
    1428             :             uint8       statusFlags;
    1429             :             uint8       lockmethod_copy;
    1430             :             LOCKTAG     locktag_copy;
    1431             : 
    1432             :             /*
    1433             :              * Grab info we need, then release lock immediately.  Note this
    1434             :              * coding means that there is a tiny chance that the process
    1435             :              * terminates its current transaction and starts a different one
    1436             :              * before we have a change to send the signal; the worst possible
    1437             :              * consequence is that a for-wraparound vacuum is canceled.  But
    1438             :              * that could happen in any case unless we were to do kill() with
    1439             :              * the lock held, which is much more undesirable.
    1440             :              */
    1441           0 :             LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
    1442           0 :             statusFlags = ProcGlobal->statusFlags[autovac->pgxactoff];
    1443           0 :             lockmethod_copy = lock->tag.locktag_lockmethodid;
    1444           0 :             locktag_copy = lock->tag;
    1445           0 :             LWLockRelease(ProcArrayLock);
    1446             : 
    1447             :             /*
    1448             :              * Only do it if the worker is not working to protect against Xid
    1449             :              * wraparound.
    1450             :              */
    1451           0 :             if ((statusFlags & PROC_IS_AUTOVACUUM) &&
    1452           0 :                 !(statusFlags & PROC_VACUUM_FOR_WRAPAROUND))
    1453             :             {
    1454           0 :                 int         pid = autovac->pid;
    1455             : 
    1456             :                 /* report the case, if configured to do so */
    1457           0 :                 if (message_level_is_interesting(DEBUG1))
    1458             :                 {
    1459             :                     StringInfoData locktagbuf;
    1460             :                     StringInfoData logbuf;  /* errdetail for server log */
    1461             : 
    1462           0 :                     initStringInfo(&locktagbuf);
    1463           0 :                     initStringInfo(&logbuf);
    1464           0 :                     DescribeLockTag(&locktagbuf, &locktag_copy);
    1465           0 :                     appendStringInfo(&logbuf,
    1466             :                                      "Process %d waits for %s on %s.",
    1467             :                                      MyProcPid,
    1468             :                                      GetLockmodeName(lockmethod_copy, lockmode),
    1469             :                                      locktagbuf.data);
    1470             : 
    1471           0 :                     ereport(DEBUG1,
    1472             :                             (errmsg_internal("sending cancel to blocking autovacuum PID %d",
    1473             :                                              pid),
    1474             :                              errdetail_log("%s", logbuf.data)));
    1475             : 
    1476           0 :                     pfree(locktagbuf.data);
    1477           0 :                     pfree(logbuf.data);
    1478             :                 }
    1479             : 
    1480             :                 /* send the autovacuum worker Back to Old Kent Road */
    1481           0 :                 if (kill(pid, SIGINT) < 0)
    1482             :                 {
    1483             :                     /*
    1484             :                      * There's a race condition here: once we release the
    1485             :                      * ProcArrayLock, it's possible for the autovac worker to
    1486             :                      * close up shop and exit before we can do the kill().
    1487             :                      * Therefore, we do not whinge about no-such-process.
    1488             :                      * Other errors such as EPERM could conceivably happen if
    1489             :                      * the kernel recycles the PID fast enough, but such cases
    1490             :                      * seem improbable enough that it's probably best to issue
    1491             :                      * a warning if we see some other errno.
    1492             :                      */
    1493           0 :                     if (errno != ESRCH)
    1494           0 :                         ereport(WARNING,
    1495             :                                 (errmsg("could not send signal to process %d: %m",
    1496             :                                         pid)));
    1497             :                 }
    1498             :             }
    1499             : 
    1500             :             /* prevent signal from being sent again more than once */
    1501           0 :             allow_autovacuum_cancel = false;
    1502             :         }
    1503             : 
    1504             :         /*
    1505             :          * If awoken after the deadlock check interrupt has run, and
    1506             :          * log_lock_waits is on, then report about the wait.
    1507             :          */
    1508        4714 :         if (log_lock_waits && deadlock_state != DS_NOT_YET_CHECKED)
    1509             :         {
    1510             :             StringInfoData buf,
    1511             :                         lock_waiters_sbuf,
    1512             :                         lock_holders_sbuf;
    1513             :             const char *modename;
    1514             :             long        secs;
    1515             :             int         usecs;
    1516             :             long        msecs;
    1517             :             dlist_iter  proc_iter;
    1518             :             PROCLOCK   *curproclock;
    1519          44 :             bool        first_holder = true,
    1520          44 :                         first_waiter = true;
    1521          44 :             int         lockHoldersNum = 0;
    1522             : 
    1523          44 :             initStringInfo(&buf);
    1524          44 :             initStringInfo(&lock_waiters_sbuf);
    1525          44 :             initStringInfo(&lock_holders_sbuf);
    1526             : 
    1527          44 :             DescribeLockTag(&buf, &locallock->tag.lock);
    1528          44 :             modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
    1529             :                                        lockmode);
    1530          44 :             TimestampDifference(get_timeout_start_time(DEADLOCK_TIMEOUT),
    1531             :                                 GetCurrentTimestamp(),
    1532             :                                 &secs, &usecs);
    1533          44 :             msecs = secs * 1000 + usecs / 1000;
    1534          44 :             usecs = usecs % 1000;
    1535             : 
    1536             :             /*
    1537             :              * we loop over the lock's procLocks to gather a list of all
    1538             :              * holders and waiters. Thus we will be able to provide more
    1539             :              * detailed information for lock debugging purposes.
    1540             :              *
    1541             :              * lock->procLocks contains all processes which hold or wait for
    1542             :              * this lock.
    1543             :              */
    1544             : 
    1545          44 :             LWLockAcquire(partitionLock, LW_SHARED);
    1546             : 
    1547         140 :             dlist_foreach(proc_iter, &lock->procLocks)
    1548             :             {
    1549          96 :                 curproclock =
    1550          96 :                     dlist_container(PROCLOCK, lockLink, proc_iter.cur);
    1551             : 
    1552             :                 /*
    1553             :                  * we are a waiter if myProc->waitProcLock == curproclock; we
    1554             :                  * are a holder if it is NULL or something different
    1555             :                  */
    1556          96 :                 if (curproclock->tag.myProc->waitProcLock == curproclock)
    1557             :                 {
    1558          42 :                     if (first_waiter)
    1559             :                     {
    1560          24 :                         appendStringInfo(&lock_waiters_sbuf, "%d",
    1561          24 :                                          curproclock->tag.myProc->pid);
    1562          24 :                         first_waiter = false;
    1563             :                     }
    1564             :                     else
    1565          18 :                         appendStringInfo(&lock_waiters_sbuf, ", %d",
    1566          18 :                                          curproclock->tag.myProc->pid);
    1567             :                 }
    1568             :                 else
    1569             :                 {
    1570          54 :                     if (first_holder)
    1571             :                     {
    1572          44 :                         appendStringInfo(&lock_holders_sbuf, "%d",
    1573          44 :                                          curproclock->tag.myProc->pid);
    1574          44 :                         first_holder = false;
    1575             :                     }
    1576             :                     else
    1577          10 :                         appendStringInfo(&lock_holders_sbuf, ", %d",
    1578          10 :                                          curproclock->tag.myProc->pid);
    1579             : 
    1580          54 :                     lockHoldersNum++;
    1581             :                 }
    1582             :             }
    1583             : 
    1584          44 :             LWLockRelease(partitionLock);
    1585             : 
    1586          44 :             if (deadlock_state == DS_SOFT_DEADLOCK)
    1587           6 :                 ereport(LOG,
    1588             :                         (errmsg("process %d avoided deadlock for %s on %s by rearranging queue order after %ld.%03d ms",
    1589             :                                 MyProcPid, modename, buf.data, msecs, usecs),
    1590             :                          (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
    1591             :                                                "Processes holding the lock: %s. Wait queue: %s.",
    1592             :                                                lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
    1593          38 :             else if (deadlock_state == DS_HARD_DEADLOCK)
    1594             :             {
    1595             :                 /*
    1596             :                  * This message is a bit redundant with the error that will be
    1597             :                  * reported subsequently, but in some cases the error report
    1598             :                  * might not make it to the log (eg, if it's caught by an
    1599             :                  * exception handler), and we want to ensure all long-wait
    1600             :                  * events get logged.
    1601             :                  */
    1602           4 :                 ereport(LOG,
    1603             :                         (errmsg("process %d detected deadlock while waiting for %s on %s after %ld.%03d ms",
    1604             :                                 MyProcPid, modename, buf.data, msecs, usecs),
    1605             :                          (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
    1606             :                                                "Processes holding the lock: %s. Wait queue: %s.",
    1607             :                                                lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
    1608             :             }
    1609             : 
    1610          44 :             if (myWaitStatus == PROC_WAIT_STATUS_WAITING)
    1611          22 :                 ereport(LOG,
    1612             :                         (errmsg("process %d still waiting for %s on %s after %ld.%03d ms",
    1613             :                                 MyProcPid, modename, buf.data, msecs, usecs),
    1614             :                          (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
    1615             :                                                "Processes holding the lock: %s. Wait queue: %s.",
    1616             :                                                lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
    1617          22 :             else if (myWaitStatus == PROC_WAIT_STATUS_OK)
    1618          18 :                 ereport(LOG,
    1619             :                         (errmsg("process %d acquired %s on %s after %ld.%03d ms",
    1620             :                                 MyProcPid, modename, buf.data, msecs, usecs)));
    1621             :             else
    1622             :             {
    1623             :                 Assert(myWaitStatus == PROC_WAIT_STATUS_ERROR);
    1624             : 
    1625             :                 /*
    1626             :                  * Currently, the deadlock checker always kicks its own
    1627             :                  * process, which means that we'll only see
    1628             :                  * PROC_WAIT_STATUS_ERROR when deadlock_state ==
    1629             :                  * DS_HARD_DEADLOCK, and there's no need to print redundant
    1630             :                  * messages.  But for completeness and future-proofing, print
    1631             :                  * a message if it looks like someone else kicked us off the
    1632             :                  * lock.
    1633             :                  */
    1634           4 :                 if (deadlock_state != DS_HARD_DEADLOCK)
    1635           0 :                     ereport(LOG,
    1636             :                             (errmsg("process %d failed to acquire %s on %s after %ld.%03d ms",
    1637             :                                     MyProcPid, modename, buf.data, msecs, usecs),
    1638             :                              (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
    1639             :                                                    "Processes holding the lock: %s. Wait queue: %s.",
    1640             :                                                    lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
    1641             :             }
    1642             : 
    1643             :             /*
    1644             :              * At this point we might still need to wait for the lock. Reset
    1645             :              * state so we don't print the above messages again.
    1646             :              */
    1647          44 :             deadlock_state = DS_NO_DEADLOCK;
    1648             : 
    1649          44 :             pfree(buf.data);
    1650          44 :             pfree(lock_holders_sbuf.data);
    1651          44 :             pfree(lock_waiters_sbuf.data);
    1652             :         }
    1653        4714 :     } while (myWaitStatus == PROC_WAIT_STATUS_WAITING);
    1654             : 
    1655             :     /*
    1656             :      * Disable the timers, if they are still running.  As in LockErrorCleanup,
    1657             :      * we must preserve the LOCK_TIMEOUT indicator flag: if a lock timeout has
    1658             :      * already caused QueryCancelPending to become set, we want the cancel to
    1659             :      * be reported as a lock timeout, not a user cancel.
    1660             :      */
    1661        2448 :     if (!InHotStandby)
    1662             :     {
    1663        2444 :         if (LockTimeout > 0)
    1664             :         {
    1665             :             DisableTimeoutParams timeouts[2];
    1666             : 
    1667         210 :             timeouts[0].id = DEADLOCK_TIMEOUT;
    1668         210 :             timeouts[0].keep_indicator = false;
    1669         210 :             timeouts[1].id = LOCK_TIMEOUT;
    1670         210 :             timeouts[1].keep_indicator = true;
    1671         210 :             disable_timeouts(timeouts, 2);
    1672             :         }
    1673             :         else
    1674        2234 :             disable_timeout(DEADLOCK_TIMEOUT, false);
    1675             :     }
    1676             : 
    1677             :     /*
    1678             :      * Emit the log message if recovery conflict on lock was resolved but the
    1679             :      * startup process waited longer than deadlock_timeout for it.
    1680             :      */
    1681        2448 :     if (InHotStandby && logged_recovery_conflict)
    1682           2 :         LogRecoveryConflict(PROCSIG_RECOVERY_CONFLICT_LOCK,
    1683             :                             standbyWaitStart, GetCurrentTimestamp(),
    1684             :                             NULL, false);
    1685             : 
    1686             :     /*
    1687             :      * We don't have to do anything else, because the awaker did all the
    1688             :      * necessary updates of the lock table and MyProc. (The caller is
    1689             :      * responsible for updating the local lock table.)
    1690             :      */
    1691        2448 :     return myWaitStatus;
    1692             : }
    1693             : 
    1694             : 
    1695             : /*
    1696             :  * ProcWakeup -- wake up a process by setting its latch.
    1697             :  *
    1698             :  *   Also remove the process from the wait queue and set its links invalid.
    1699             :  *
    1700             :  * The appropriate lock partition lock must be held by caller.
    1701             :  *
    1702             :  * XXX: presently, this code is only used for the "success" case, and only
    1703             :  * works correctly for that case.  To clean up in failure case, would need
    1704             :  * to twiddle the lock's request counts too --- see RemoveFromWaitQueue.
    1705             :  * Hence, in practice the waitStatus parameter must be PROC_WAIT_STATUS_OK.
    1706             :  */
    1707             : void
    1708        2466 : ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus)
    1709             : {
    1710        2466 :     if (dlist_node_is_detached(&proc->links))
    1711           0 :         return;
    1712             : 
    1713             :     Assert(proc->waitStatus == PROC_WAIT_STATUS_WAITING);
    1714             : 
    1715             :     /* Remove process from wait queue */
    1716        2466 :     dclist_delete_from_thoroughly(&proc->waitLock->waitProcs, &proc->links);
    1717             : 
    1718             :     /* Clean up process' state and pass it the ok/fail signal */
    1719        2466 :     proc->waitLock = NULL;
    1720        2466 :     proc->waitProcLock = NULL;
    1721        2466 :     proc->waitStatus = waitStatus;
    1722        2466 :     pg_atomic_write_u64(&MyProc->waitStart, 0);
    1723             : 
    1724             :     /* And awaken it */
    1725        2466 :     SetLatch(&proc->procLatch);
    1726             : }
    1727             : 
    1728             : /*
    1729             :  * ProcLockWakeup -- routine for waking up processes when a lock is
    1730             :  *      released (or a prior waiter is aborted).  Scan all waiters
    1731             :  *      for lock, waken any that are no longer blocked.
    1732             :  *
    1733             :  * The appropriate lock partition lock must be held by caller.
    1734             :  */
    1735             : void
    1736        2514 : ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
    1737             : {
    1738        2514 :     dclist_head *waitQueue = &lock->waitProcs;
    1739        2514 :     LOCKMASK    aheadRequests = 0;
    1740             :     dlist_mutable_iter miter;
    1741             : 
    1742        2514 :     if (dclist_is_empty(waitQueue))
    1743          90 :         return;
    1744             : 
    1745        5730 :     dclist_foreach_modify(miter, waitQueue)
    1746             :     {
    1747        3306 :         PGPROC     *proc = dlist_container(PGPROC, links, miter.cur);
    1748        3306 :         LOCKMODE    lockmode = proc->waitLockMode;
    1749             : 
    1750             :         /*
    1751             :          * Waken if (a) doesn't conflict with requests of earlier waiters, and
    1752             :          * (b) doesn't conflict with already-held locks.
    1753             :          */
    1754        3306 :         if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
    1755        2900 :             !LockCheckConflicts(lockMethodTable, lockmode, lock,
    1756             :                                 proc->waitProcLock))
    1757             :         {
    1758             :             /* OK to waken */
    1759        2466 :             GrantLock(lock, proc->waitProcLock, lockmode);
    1760             :             /* removes proc from the lock's waiting process queue */
    1761        2466 :             ProcWakeup(proc, PROC_WAIT_STATUS_OK);
    1762             :         }
    1763             :         else
    1764             :         {
    1765             :             /*
    1766             :              * Lock conflicts: Don't wake, but remember requested mode for
    1767             :              * later checks.
    1768             :              */
    1769         840 :             aheadRequests |= LOCKBIT_ON(lockmode);
    1770             :         }
    1771             :     }
    1772             : }
    1773             : 
    1774             : /*
    1775             :  * CheckDeadLock
    1776             :  *
    1777             :  * We only get to this routine, if DEADLOCK_TIMEOUT fired while waiting for a
    1778             :  * lock to be released by some other process.  Check if there's a deadlock; if
    1779             :  * not, just return.  (But signal ProcSleep to log a message, if
    1780             :  * log_lock_waits is true.)  If we have a real deadlock, remove ourselves from
    1781             :  * the lock's wait queue and signal an error to ProcSleep.
    1782             :  */
    1783             : static void
    1784          58 : CheckDeadLock(void)
    1785             : {
    1786             :     int         i;
    1787             : 
    1788             :     /*
    1789             :      * Acquire exclusive lock on the entire shared lock data structures. Must
    1790             :      * grab LWLocks in partition-number order to avoid LWLock deadlock.
    1791             :      *
    1792             :      * Note that the deadlock check interrupt had better not be enabled
    1793             :      * anywhere that this process itself holds lock partition locks, else this
    1794             :      * will wait forever.  Also note that LWLockAcquire creates a critical
    1795             :      * section, so that this routine cannot be interrupted by cancel/die
    1796             :      * interrupts.
    1797             :      */
    1798         986 :     for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
    1799         928 :         LWLockAcquire(LockHashPartitionLockByIndex(i), LW_EXCLUSIVE);
    1800             : 
    1801             :     /*
    1802             :      * Check to see if we've been awoken by anyone in the interim.
    1803             :      *
    1804             :      * If we have, we can return and resume our transaction -- happy day.
    1805             :      * Before we are awoken the process releasing the lock grants it to us so
    1806             :      * we know that we don't have to wait anymore.
    1807             :      *
    1808             :      * We check by looking to see if we've been unlinked from the wait queue.
    1809             :      * This is safe because we hold the lock partition lock.
    1810             :      */
    1811          58 :     if (MyProc->links.prev == NULL ||
    1812          58 :         MyProc->links.next == NULL)
    1813           0 :         goto check_done;
    1814             : 
    1815             : #ifdef LOCK_DEBUG
    1816             :     if (Debug_deadlocks)
    1817             :         DumpAllLocks();
    1818             : #endif
    1819             : 
    1820             :     /* Run the deadlock check, and set deadlock_state for use by ProcSleep */
    1821          58 :     deadlock_state = DeadLockCheck(MyProc);
    1822             : 
    1823          58 :     if (deadlock_state == DS_HARD_DEADLOCK)
    1824             :     {
    1825             :         /*
    1826             :          * Oops.  We have a deadlock.
    1827             :          *
    1828             :          * Get this process out of wait state. (Note: we could do this more
    1829             :          * efficiently by relying on lockAwaited, but use this coding to
    1830             :          * preserve the flexibility to kill some other transaction than the
    1831             :          * one detecting the deadlock.)
    1832             :          *
    1833             :          * RemoveFromWaitQueue sets MyProc->waitStatus to
    1834             :          * PROC_WAIT_STATUS_ERROR, so ProcSleep will report an error after we
    1835             :          * return from the signal handler.
    1836             :          */
    1837             :         Assert(MyProc->waitLock != NULL);
    1838          10 :         RemoveFromWaitQueue(MyProc, LockTagHashCode(&(MyProc->waitLock->tag)));
    1839             : 
    1840             :         /*
    1841             :          * We're done here.  Transaction abort caused by the error that
    1842             :          * ProcSleep will raise will cause any other locks we hold to be
    1843             :          * released, thus allowing other processes to wake up; we don't need
    1844             :          * to do that here.  NOTE: an exception is that releasing locks we
    1845             :          * hold doesn't consider the possibility of waiters that were blocked
    1846             :          * behind us on the lock we just failed to get, and might now be
    1847             :          * wakable because we're not in front of them anymore.  However,
    1848             :          * RemoveFromWaitQueue took care of waking up any such processes.
    1849             :          */
    1850             :     }
    1851             : 
    1852             :     /*
    1853             :      * And release locks.  We do this in reverse order for two reasons: (1)
    1854             :      * Anyone else who needs more than one of the locks will be trying to lock
    1855             :      * them in increasing order; we don't want to release the other process
    1856             :      * until it can get all the locks it needs. (2) This avoids O(N^2)
    1857             :      * behavior inside LWLockRelease.
    1858             :      */
    1859          48 : check_done:
    1860         986 :     for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
    1861         928 :         LWLockRelease(LockHashPartitionLockByIndex(i));
    1862          58 : }
    1863             : 
    1864             : /*
    1865             :  * CheckDeadLockAlert - Handle the expiry of deadlock_timeout.
    1866             :  *
    1867             :  * NB: Runs inside a signal handler, be careful.
    1868             :  */
    1869             : void
    1870          58 : CheckDeadLockAlert(void)
    1871             : {
    1872          58 :     int         save_errno = errno;
    1873             : 
    1874          58 :     got_deadlock_timeout = true;
    1875             : 
    1876             :     /*
    1877             :      * Have to set the latch again, even if handle_sig_alarm already did. Back
    1878             :      * then got_deadlock_timeout wasn't yet set... It's unlikely that this
    1879             :      * ever would be a problem, but setting a set latch again is cheap.
    1880             :      *
    1881             :      * Note that, when this function runs inside procsignal_sigusr1_handler(),
    1882             :      * the handler function sets the latch again after the latch is set here.
    1883             :      */
    1884          58 :     SetLatch(MyLatch);
    1885          58 :     errno = save_errno;
    1886          58 : }
    1887             : 
    1888             : /*
    1889             :  * ProcWaitForSignal - wait for a signal from another backend.
    1890             :  *
    1891             :  * As this uses the generic process latch the caller has to be robust against
    1892             :  * unrelated wakeups: Always check that the desired state has occurred, and
    1893             :  * wait again if not.
    1894             :  */
    1895             : void
    1896          38 : ProcWaitForSignal(uint32 wait_event_info)
    1897             : {
    1898          38 :     (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
    1899             :                      wait_event_info);
    1900          38 :     ResetLatch(MyLatch);
    1901          38 :     CHECK_FOR_INTERRUPTS();
    1902          38 : }
    1903             : 
    1904             : /*
    1905             :  * ProcSendSignal - set the latch of a backend identified by ProcNumber
    1906             :  */
    1907             : void
    1908           8 : ProcSendSignal(ProcNumber procNumber)
    1909             : {
    1910           8 :     if (procNumber < 0 || procNumber >= ProcGlobal->allProcCount)
    1911           0 :         elog(ERROR, "procNumber out of range");
    1912             : 
    1913           8 :     SetLatch(&ProcGlobal->allProcs[procNumber].procLatch);
    1914           8 : }
    1915             : 
    1916             : /*
    1917             :  * BecomeLockGroupLeader - designate process as lock group leader
    1918             :  *
    1919             :  * Once this function has returned, other processes can join the lock group
    1920             :  * by calling BecomeLockGroupMember.
    1921             :  */
    1922             : void
    1923        1150 : BecomeLockGroupLeader(void)
    1924             : {
    1925             :     LWLock     *leader_lwlock;
    1926             : 
    1927             :     /* If we already did it, we don't need to do it again. */
    1928        1150 :     if (MyProc->lockGroupLeader == MyProc)
    1929        1012 :         return;
    1930             : 
    1931             :     /* We had better not be a follower. */
    1932             :     Assert(MyProc->lockGroupLeader == NULL);
    1933             : 
    1934             :     /* Create single-member group, containing only ourselves. */
    1935         138 :     leader_lwlock = LockHashPartitionLockByProc(MyProc);
    1936         138 :     LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
    1937         138 :     MyProc->lockGroupLeader = MyProc;
    1938         138 :     dlist_push_head(&MyProc->lockGroupMembers, &MyProc->lockGroupLink);
    1939         138 :     LWLockRelease(leader_lwlock);
    1940             : }
    1941             : 
    1942             : /*
    1943             :  * BecomeLockGroupMember - designate process as lock group member
    1944             :  *
    1945             :  * This is pretty straightforward except for the possibility that the leader
    1946             :  * whose group we're trying to join might exit before we manage to do so;
    1947             :  * and the PGPROC might get recycled for an unrelated process.  To avoid
    1948             :  * that, we require the caller to pass the PID of the intended PGPROC as
    1949             :  * an interlock.  Returns true if we successfully join the intended lock
    1950             :  * group, and false if not.
    1951             :  */
    1952             : bool
    1953        2712 : BecomeLockGroupMember(PGPROC *leader, int pid)
    1954             : {
    1955             :     LWLock     *leader_lwlock;
    1956        2712 :     bool        ok = false;
    1957             : 
    1958             :     /* Group leader can't become member of group */
    1959             :     Assert(MyProc != leader);
    1960             : 
    1961             :     /* Can't already be a member of a group */
    1962             :     Assert(MyProc->lockGroupLeader == NULL);
    1963             : 
    1964             :     /* PID must be valid. */
    1965             :     Assert(pid != 0);
    1966             : 
    1967             :     /*
    1968             :      * Get lock protecting the group fields.  Note LockHashPartitionLockByProc
    1969             :      * calculates the proc number based on the PGPROC slot without looking at
    1970             :      * its contents, so we will acquire the correct lock even if the leader
    1971             :      * PGPROC is in process of being recycled.
    1972             :      */
    1973        2712 :     leader_lwlock = LockHashPartitionLockByProc(leader);
    1974        2712 :     LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
    1975             : 
    1976             :     /* Is this the leader we're looking for? */
    1977        2712 :     if (leader->pid == pid && leader->lockGroupLeader == leader)
    1978             :     {
    1979             :         /* OK, join the group */
    1980        2712 :         ok = true;
    1981        2712 :         MyProc->lockGroupLeader = leader;
    1982        2712 :         dlist_push_tail(&leader->lockGroupMembers, &MyProc->lockGroupLink);
    1983             :     }
    1984        2712 :     LWLockRelease(leader_lwlock);
    1985             : 
    1986        2712 :     return ok;
    1987             : }

Generated by: LCOV version 1.14