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

Generated by: LCOV version 2.0-1