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

Generated by: LCOV version 1.13