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

Generated by: LCOV version 1.14