PostgreSQL Source Code  git master
proc.c File Reference
#include "postgres.h"
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include "access/transam.h"
#include "access/twophase.h"
#include "access/xact.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "replication/slot.h"
#include "replication/syncrep.h"
#include "replication/walsender.h"
#include "storage/condition_variable.h"
#include "storage/ipc.h"
#include "storage/lmgr.h"
#include "storage/pmsignal.h"
#include "storage/proc.h"
#include "storage/procarray.h"
#include "storage/procsignal.h"
#include "storage/spin.h"
#include "storage/standby.h"
#include "utils/timeout.h"
#include "utils/timestamp.h"
Include dependency graph for proc.c:

Go to the source code of this file.

Functions

static void RemoveProcFromArray (int code, Datum arg)
 
static void ProcKill (int code, Datum arg)
 
static void AuxiliaryProcKill (int code, Datum arg)
 
static void CheckDeadLock (void)
 
Size ProcGlobalShmemSize (void)
 
int ProcGlobalSemas (void)
 
void InitProcGlobal (void)
 
void InitProcess (void)
 
void InitProcessPhase2 (void)
 
void InitAuxiliaryProcess (void)
 
void PublishStartupProcessInformation (void)
 
void SetStartupBufferPinWaitBufId (int bufid)
 
int GetStartupBufferPinWaitBufId (void)
 
bool HaveNFreeProcs (int n)
 
bool IsWaitingForLock (void)
 
void LockErrorCleanup (void)
 
void ProcReleaseLocks (bool isCommit)
 
PGPROCAuxiliaryPidGetProc (int pid)
 
void ProcQueueInit (PROC_QUEUE *queue)
 
int ProcSleep (LOCALLOCK *locallock, LockMethod lockMethodTable)
 
PGPROCProcWakeup (PGPROC *proc, int waitStatus)
 
void ProcLockWakeup (LockMethod lockMethodTable, LOCK *lock)
 
void CheckDeadLockAlert (void)
 
void ProcWaitForSignal (uint32 wait_event_info)
 
void ProcSendSignal (int pid)
 
void BecomeLockGroupLeader (void)
 
bool BecomeLockGroupMember (PGPROC *leader, int pid)
 

Variables

int DeadlockTimeout = 1000
 
int StatementTimeout = 0
 
int LockTimeout = 0
 
int IdleInTransactionSessionTimeout = 0
 
bool log_lock_waits = false
 
PGPROCMyProc = NULL
 
PGXACTMyPgXact = NULL
 
NON_EXEC_STATIC slock_tProcStructLock = NULL
 
PROC_HDRProcGlobal = NULL
 
NON_EXEC_STATIC PGPROCAuxiliaryProcs = NULL
 
PGPROCPreparedXactProcs = NULL
 
static LOCALLOCKlockAwaited = NULL
 
static DeadLockState deadlock_state = DS_NOT_YET_CHECKED
 
static volatile sig_atomic_t got_deadlock_timeout
 

Function Documentation

◆ AuxiliaryPidGetProc()

PGPROC* AuxiliaryPidGetProc ( int  pid)

Definition at line 985 of file proc.c.

References AuxiliaryProcs, name, NUM_AUXILIARY_PROCS, PGPROC::pid, ProcQueueInit(), and ShmemInitStruct().

Referenced by pg_stat_get_activity().

986 {
987  PGPROC *result = NULL;
988  int index;
989 
990  if (pid == 0) /* never match dummy PGPROCs */
991  return NULL;
992 
993  for (index = 0; index < NUM_AUXILIARY_PROCS; index++)
994  {
995  PGPROC *proc = &AuxiliaryProcs[index];
996 
997  if (proc->pid == pid)
998  {
999  result = proc;
1000  break;
1001  }
1002  }
1003  return result;
1004 }
Definition: type.h:89
#define NUM_AUXILIARY_PROCS
Definition: proc.h:292
NON_EXEC_STATIC PGPROC * AuxiliaryProcs
Definition: proc.c:81
Definition: proc.h:95
int pid
Definition: proc.h:109

◆ AuxiliaryProcKill()

static void AuxiliaryProcKill ( int  code,
Datum  arg 
)
static

Definition at line 938 of file proc.c.

References Assert, AuxiliaryProcs, ConditionVariableCancelSleep(), DatumGetInt32, DisownLatch(), LWLockReleaseAll(), MyProc, NUM_AUXILIARY_PROCS, PG_USED_FOR_ASSERTS_ONLY, PGPROC::pid, PGPROC::procLatch, ProcStructLock, SpinLockAcquire, SpinLockRelease, PROC_HDR::spins_per_delay, SwitchBackToLocalLatch(), and update_spins_per_delay().

Referenced by InitAuxiliaryProcess().

939 {
940  int proctype = DatumGetInt32(arg);
942  PGPROC *proc;
943 
944  Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
945 
946  auxproc = &AuxiliaryProcs[proctype];
947 
948  Assert(MyProc == auxproc);
949 
950  /* Release any LW locks I am holding (see notes above) */
952 
953  /* Cancel any pending condition variable sleep, too */
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  */
963  proc = MyProc;
964  MyProc = NULL;
965  DisownLatch(&proc->procLatch);
966 
968 
969  /* Mark auxiliary proc no longer in use */
970  proc->pid = 0;
971 
972  /* Update shared estimate of spins_per_delay */
974 
976 }
#define DatumGetInt32(X)
Definition: postgres.h:472
PGPROC * MyProc
Definition: proc.c:67
PROC_HDR * ProcGlobal
Definition: proc.c:80
void DisownLatch(Latch *latch)
Definition: latch.c:316
Latch procLatch
Definition: proc.h:104
#define SpinLockAcquire(lock)
Definition: spin.h:62
void SwitchBackToLocalLatch(void)
Definition: miscinit.c:361
void ConditionVariableCancelSleep(void)
int spins_per_delay
Definition: proc.h:269
#define NUM_AUXILIARY_PROCS
Definition: proc.h:292
int update_spins_per_delay(int shared_spins_per_delay)
Definition: s_lock.c:206
#define SpinLockRelease(lock)
Definition: spin.h:64
NON_EXEC_STATIC slock_t * ProcStructLock
Definition: proc.c:77
#define Assert(condition)
Definition: c.h:739
NON_EXEC_STATIC PGPROC * AuxiliaryProcs
Definition: proc.c:81
void * arg
void LWLockReleaseAll(void)
Definition: lwlock.c:1825
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:123
Definition: proc.h:95
int pid
Definition: proc.h:109

◆ BecomeLockGroupLeader()

void BecomeLockGroupLeader ( void  )

Definition at line 1848 of file proc.c.

References Assert, dlist_push_head(), PGPROC::lockGroupLeader, PGPROC::lockGroupLink, PGPROC::lockGroupMembers, LockHashPartitionLockByProc, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), and MyProc.

Referenced by LaunchParallelWorkers().

1849 {
1850  LWLock *leader_lwlock;
1851 
1852  /* If we already did it, we don't need to do it again. */
1853  if (MyProc->lockGroupLeader == MyProc)
1854  return;
1855 
1856  /* We had better not be a follower. */
1857  Assert(MyProc->lockGroupLeader == NULL);
1858 
1859  /* Create single-member group, containing only ourselves. */
1860  leader_lwlock = LockHashPartitionLockByProc(MyProc);
1861  LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
1864  LWLockRelease(leader_lwlock);
1865 }
Definition: lwlock.h:32
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition: ilist.h:300
dlist_head lockGroupMembers
Definition: proc.h:204
PGPROC * MyProc
Definition: proc.c:67
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
dlist_node lockGroupLink
Definition: proc.h:205
#define LockHashPartitionLockByProc(leader_pgproc)
Definition: lock.h:517
#define Assert(condition)
Definition: c.h:739
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
PGPROC * lockGroupLeader
Definition: proc.h:203

◆ BecomeLockGroupMember()

bool BecomeLockGroupMember ( PGPROC leader,
int  pid 
)

Definition at line 1878 of file proc.c.

References Assert, dlist_push_tail(), PGPROC::lockGroupLeader, PGPROC::lockGroupLink, PGPROC::lockGroupMembers, LockHashPartitionLockByProc, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), and PGPROC::pid.

Referenced by ParallelWorkerMain().

1879 {
1880  LWLock *leader_lwlock;
1881  bool ok = false;
1882 
1883  /* Group leader can't become member of group */
1884  Assert(MyProc != leader);
1885 
1886  /* Can't already be a member of a group */
1887  Assert(MyProc->lockGroupLeader == NULL);
1888 
1889  /* PID must be valid. */
1890  Assert(pid != 0);
1891 
1892  /*
1893  * Get lock protecting the group fields. Note LockHashPartitionLockByProc
1894  * accesses leader->pgprocno in a PGPROC that might be free. This is safe
1895  * because all PGPROCs' pgprocno fields are set during shared memory
1896  * initialization and never change thereafter; so we will acquire the
1897  * correct lock even if the leader PGPROC is in process of being recycled.
1898  */
1899  leader_lwlock = LockHashPartitionLockByProc(leader);
1900  LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
1901 
1902  /* Is this the leader we're looking for? */
1903  if (leader->pid == pid && leader->lockGroupLeader == leader)
1904  {
1905  /* OK, join the group */
1906  ok = true;
1907  MyProc->lockGroupLeader = leader;
1909  }
1910  LWLockRelease(leader_lwlock);
1911 
1912  return ok;
1913 }
Definition: lwlock.h:32
dlist_head lockGroupMembers
Definition: proc.h:204
PGPROC * MyProc
Definition: proc.c:67
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition: ilist.h:317
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
dlist_node lockGroupLink
Definition: proc.h:205
#define LockHashPartitionLockByProc(leader_pgproc)
Definition: lock.h:517
#define Assert(condition)
Definition: c.h:739
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
int pid
Definition: proc.h:109
PGPROC * lockGroupLeader
Definition: proc.h:203

◆ CheckDeadLock()

static void CheckDeadLock ( void  )
static

Definition at line 1689 of file proc.c.

References Assert, deadlock_state, DeadLockCheck(), DS_HARD_DEADLOCK, i, PGPROC::links, LockHashPartitionLockByIndex, LockTagHashCode(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SHM_QUEUE::next, NUM_LOCK_PARTITIONS, SHM_QUEUE::prev, RemoveFromWaitQueue(), LOCK::tag, and PGPROC::waitLock.

Referenced by ProcSleep().

1690 {
1691  int i;
1692 
1693  /*
1694  * Acquire exclusive lock on the entire shared lock data structures. Must
1695  * grab LWLocks in partition-number order to avoid LWLock deadlock.
1696  *
1697  * Note that the deadlock check interrupt had better not be enabled
1698  * anywhere that this process itself holds lock partition locks, else this
1699  * will wait forever. Also note that LWLockAcquire creates a critical
1700  * section, so that this routine cannot be interrupted by cancel/die
1701  * interrupts.
1702  */
1703  for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
1705 
1706  /*
1707  * Check to see if we've been awoken by anyone in the interim.
1708  *
1709  * If we have, we can return and resume our transaction -- happy day.
1710  * Before we are awoken the process releasing the lock grants it to us so
1711  * we know that we don't have to wait anymore.
1712  *
1713  * We check by looking to see if we've been unlinked from the wait queue.
1714  * This is safe because we hold the lock partition lock.
1715  */
1716  if (MyProc->links.prev == NULL ||
1717  MyProc->links.next == NULL)
1718  goto check_done;
1719 
1720 #ifdef LOCK_DEBUG
1721  if (Debug_deadlocks)
1722  DumpAllLocks();
1723 #endif
1724 
1725  /* Run the deadlock check, and set deadlock_state for use by ProcSleep */
1727 
1729  {
1730  /*
1731  * Oops. We have a deadlock.
1732  *
1733  * Get this process out of wait state. (Note: we could do this more
1734  * efficiently by relying on lockAwaited, but use this coding to
1735  * preserve the flexibility to kill some other transaction than the
1736  * one detecting the deadlock.)
1737  *
1738  * RemoveFromWaitQueue sets MyProc->waitStatus to STATUS_ERROR, so
1739  * ProcSleep will report an error after we return from the signal
1740  * handler.
1741  */
1742  Assert(MyProc->waitLock != NULL);
1744 
1745  /*
1746  * We're done here. Transaction abort caused by the error that
1747  * ProcSleep will raise will cause any other locks we hold to be
1748  * released, thus allowing other processes to wake up; we don't need
1749  * to do that here. NOTE: an exception is that releasing locks we
1750  * hold doesn't consider the possibility of waiters that were blocked
1751  * behind us on the lock we just failed to get, and might now be
1752  * wakable because we're not in front of them anymore. However,
1753  * RemoveFromWaitQueue took care of waking up any such processes.
1754  */
1755  }
1756 
1757  /*
1758  * And release locks. We do this in reverse order for two reasons: (1)
1759  * Anyone else who needs more than one of the locks will be trying to lock
1760  * them in increasing order; we don't want to release the other process
1761  * until it can get all the locks it needs. (2) This avoids O(N^2)
1762  * behavior inside LWLockRelease.
1763  */
1764 check_done:
1765  for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
1767 }
PGPROC * MyProc
Definition: proc.c:67
SHM_QUEUE links
Definition: proc.h:98
struct SHM_QUEUE * next
Definition: shmem.h:31
LOCKTAG tag
Definition: lock.h:290
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define LockHashPartitionLockByIndex(i)
Definition: lock.h:505
static DeadLockState deadlock_state
Definition: proc.c:87
DeadLockState DeadLockCheck(PGPROC *proc)
Definition: deadlock.c:217
LOCK * waitLock
Definition: proc.h:139
uint32 LockTagHashCode(const LOCKTAG *locktag)
Definition: lock.c:490
void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode)
Definition: lock.c:1827
struct SHM_QUEUE * prev
Definition: shmem.h:30
#define Assert(condition)
Definition: c.h:739
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
int i
#define NUM_LOCK_PARTITIONS
Definition: lwlock.h:117

◆ CheckDeadLockAlert()

void CheckDeadLockAlert ( void  )

Definition at line 1775 of file proc.c.

References got_deadlock_timeout, MyLatch, and SetLatch().

Referenced by InitPostgres().

1776 {
1777  int save_errno = errno;
1778 
1779  got_deadlock_timeout = true;
1780 
1781  /*
1782  * Have to set the latch again, even if handle_sig_alarm already did. Back
1783  * then got_deadlock_timeout wasn't yet set... It's unlikely that this
1784  * ever would be a problem, but setting a set latch again is cheap.
1785  */
1786  SetLatch(MyLatch);
1787  errno = save_errno;
1788 }
void SetLatch(Latch *latch)
Definition: latch.c:436
static volatile sig_atomic_t got_deadlock_timeout
Definition: proc.c:90
struct Latch * MyLatch
Definition: globals.c:54

◆ GetStartupBufferPinWaitBufId()

int GetStartupBufferPinWaitBufId ( void  )

Definition at line 657 of file proc.c.

References ProcGlobal, and PROC_HDR::startupBufferPinWaitBufId.

Referenced by HoldingBufferPinThatDelaysRecovery().

658 {
659  /* use volatile pointer to prevent code rearrangement */
660  volatile PROC_HDR *procglobal = ProcGlobal;
661 
662  return procglobal->startupBufferPinWaitBufId;
663 }
PROC_HDR * ProcGlobal
Definition: proc.c:80
Definition: proc.h:244
int startupBufferPinWaitBufId
Definition: proc.h:274

◆ HaveNFreeProcs()

bool HaveNFreeProcs ( int  n)

Definition at line 671 of file proc.c.

References PROC_HDR::freeProcs, PGPROC::links, SHM_QUEUE::next, ProcStructLock, SpinLockAcquire, and SpinLockRelease.

Referenced by InitPostgres().

672 {
673  PGPROC *proc;
674 
676 
677  proc = ProcGlobal->freeProcs;
678 
679  while (n > 0 && proc != NULL)
680  {
681  proc = (PGPROC *) proc->links.next;
682  n--;
683  }
684 
686 
687  return (n <= 0);
688 }
SHM_QUEUE links
Definition: proc.h:98
struct SHM_QUEUE * next
Definition: shmem.h:31
PROC_HDR * ProcGlobal
Definition: proc.c:80
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
NON_EXEC_STATIC slock_t * ProcStructLock
Definition: proc.c:77
PGPROC * freeProcs
Definition: proc.h:253
Definition: proc.h:95

◆ InitAuxiliaryProcess()

void InitAuxiliaryProcess ( void  )

Definition at line 515 of file proc.c.

References PROC_HDR::allPgXact, Assert, AuxiliaryProcKill(), AuxiliaryProcs, PGPROC::backendId, PGPROC::databaseId, PGXACT::delayChkpt, dlist_is_empty(), elog, ERROR, FATAL, PGPROC::fpLocalTransactionId, PGPROC::fpVXIDLock, i, Int32GetDatum, InvalidBackendId, InvalidLocalTransactionId, InvalidOid, InvalidTransactionId, IsBackgroundWorker, PGPROC::isBackgroundWorker, PGPROC::links, PGPROC::lockGroupLeader, PGPROC::lockGroupMembers, PGPROC::lwWaiting, PGPROC::lwWaitMode, PGPROC::lxid, PGPROC::myProcLocks, MyProcPid, NUM_AUXILIARY_PROCS, NUM_LOCK_PARTITIONS, on_shmem_exit(), OwnLatch(), PANIC, PGPROC::pgprocno, PGSemaphoreReset(), PGPROC::pid, PGPROC::procLatch, ProcStructLock, PGPROC::roleId, PGPROC::sem, set_spins_per_delay(), SHMQueueElemInit(), SHMQueueEmpty(), SpinLockAcquire, SpinLockRelease, PROC_HDR::spins_per_delay, STATUS_OK, SwitchToSharedLatch(), PGPROC::tempNamespaceId, PGXACT::vacuumFlags, PGPROC::waitLock, PGPROC::waitProcLock, PGPROC::waitStatus, PGXACT::xid, and PGXACT::xmin.

Referenced by AuxiliaryProcessMain(), and BackendRun().

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  if (ProcGlobal == NULL || AuxiliaryProcs == NULL)
525  elog(PANIC, "proc header uninitialized");
526 
527  if (MyProc != NULL)
528  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  */
538 
540 
541  /*
542  * Find a free auxproc ... *big* trouble if there isn't one ...
543  */
544  for (proctype = 0; proctype < NUM_AUXILIARY_PROCS; proctype++)
545  {
546  auxproc = &AuxiliaryProcs[proctype];
547  if (auxproc->pid == 0)
548  break;
549  }
550  if (proctype >= NUM_AUXILIARY_PROCS)
551  {
553  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  ((volatile PGPROC *) auxproc)->pid = MyProcPid;
559 
560  MyProc = auxproc;
561  MyPgXact = &ProcGlobal->allPgXact[auxproc->pgprocno];
562 
564 
565  /*
566  * Initialize all fields of MyProc, except for those previously
567  * initialized by InitProcGlobal.
568  */
572  MyProc->fpVXIDLock = false;
581  MyPgXact->delayChkpt = false;
582  MyPgXact->vacuumFlags = 0;
583  MyProc->lwWaiting = false;
584  MyProc->lwWaitMode = 0;
585  MyProc->waitLock = NULL;
586  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++)
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  */
604 
605  /* Check that group locking fields are in a proper initial state. */
606  Assert(MyProc->lockGroupLeader == NULL);
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  */
615 
616  /*
617  * Arrange to clean up at process exit.
618  */
620 }
static void AuxiliaryProcKill(int code, Datum arg)
Definition: proc.c:938
void set_spins_per_delay(int shared_spins_per_delay)
Definition: s_lock.c:195
int MyProcPid
Definition: globals.c:40
BackendId backendId
Definition: proc.h:113
dlist_head lockGroupMembers
Definition: proc.h:204
Oid tempNamespaceId
Definition: proc.h:117
TransactionId xmin
Definition: proc.h:228
PGXACT * allPgXact
Definition: proc.h:249
PGPROC * MyProc
Definition: proc.c:67
TransactionId xid
Definition: proc.h:224
SHM_QUEUE links
Definition: proc.h:98
void PGSemaphoreReset(PGSemaphore sema)
Definition: posix_sema.c:295
bool lwWaiting
Definition: proc.h:130
Oid roleId
Definition: proc.h:115
PROC_HDR * ProcGlobal
Definition: proc.c:80
uint8 lwWaitMode
Definition: proc.h:131
bool fpVXIDLock
Definition: proc.h:195
#define PANIC
Definition: elog.h:53
Latch procLatch
Definition: proc.h:104
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:233
bool IsBackgroundWorker
Definition: globals.c:111
bool isBackgroundWorker
Definition: proc.h:120
#define SpinLockAcquire(lock)
Definition: spin.h:62
int spins_per_delay
Definition: proc.h:269
#define ERROR
Definition: elog.h:43
void OwnLatch(Latch *latch)
Definition: latch.c:296
#define FATAL
Definition: elog.h:52
PROCLOCK * waitProcLock
Definition: proc.h:140
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
#define InvalidTransactionId
Definition: transam.h:31
Oid databaseId
Definition: proc.h:114
LOCK * waitLock
Definition: proc.h:139
#define NUM_AUXILIARY_PROCS
Definition: proc.h:292
#define STATUS_OK
Definition: c.h:1120
bool delayChkpt
Definition: proc.h:235
bool SHMQueueEmpty(const SHM_QUEUE *queue)
Definition: shmqueue.c:180
#define SpinLockRelease(lock)
Definition: spin.h:64
#define InvalidBackendId
Definition: backendid.h:23
NON_EXEC_STATIC slock_t * ProcStructLock
Definition: proc.c:77
void SwitchToSharedLatch(void)
Definition: miscinit.c:342
int waitStatus
Definition: proc.h:102
#define InvalidOid
Definition: postgres_ext.h:36
#define Assert(condition)
Definition: c.h:739
NON_EXEC_STATIC PGPROC * AuxiliaryProcs
Definition: proc.c:81
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
void SHMQueueElemInit(SHM_QUEUE *queue)
Definition: shmqueue.c:57
#define Int32GetDatum(X)
Definition: postgres.h:479
int pgprocno
Definition: proc.h:110
#define elog(elevel,...)
Definition: elog.h:228
#define InvalidLocalTransactionId
Definition: lock.h:68
int i
SHM_QUEUE myProcLocks[NUM_LOCK_PARTITIONS]
Definition: proc.h:160
PGSemaphore sem
Definition: proc.h:101
Definition: proc.h:95
int pid
Definition: proc.h:109
PGPROC * lockGroupLeader
Definition: proc.h:203
LocalTransactionId fpLocalTransactionId
Definition: proc.h:196
#define NUM_LOCK_PARTITIONS
Definition: lwlock.h:117
LocalTransactionId lxid
Definition: proc.h:106

◆ InitProcess()

void InitProcess ( void  )

Definition at line 304 of file proc.c.

References PROC_HDR::allPgXact, am_walsender, Assert, PROC_HDR::autovacFreeProcs, PGPROC::backendId, PROC_HDR::bgworkerFreeProcs, PGPROC::clogGroupMember, PGPROC::clogGroupMemberLsn, PGPROC::clogGroupMemberPage, PGPROC::clogGroupMemberXid, PGPROC::clogGroupMemberXidStatus, PGPROC::clogGroupNext, PGPROC::databaseId, PGXACT::delayChkpt, dlist_is_empty(), elog, ereport, errcode(), errmsg(), ERROR, FATAL, PGPROC::fpLocalTransactionId, PGPROC::fpVXIDLock, PROC_HDR::freeProcs, i, InitDeadLockChecking(), InitLWLockAccess(), INVALID_PGPROCNO, InvalidBackendId, InvalidLocalTransactionId, InvalidOid, InvalidTransactionId, InvalidXLogRecPtr, IsAnyAutoVacuumProcess, IsAutoVacuumLauncherProcess(), IsAutoVacuumWorkerProcess(), IsBackgroundWorker, PGPROC::isBackgroundWorker, IsUnderPostmaster, PGPROC::links, PGPROC::lockGroupLeader, PGPROC::lockGroupMembers, PGPROC::lwWaiting, PGPROC::lwWaitMode, PGPROC::lxid, MarkPostmasterChildActive(), max_wal_senders, PGPROC::myProcLocks, MyProcPid, SHM_QUEUE::next, NUM_LOCK_PARTITIONS, on_shmem_exit(), OwnLatch(), PANIC, pg_atomic_read_u32(), PGPROC::pgprocno, PGSemaphoreReset(), PGPROC::pid, PROC_IS_AUTOVACUUM, PGPROC::procArrayGroupMember, PGPROC::procArrayGroupMemberXid, PGPROC::procArrayGroupNext, PGPROC::procgloballist, ProcKill(), PGPROC::procLatch, ProcStructLock, PGPROC::recoveryConflictPending, PGPROC::roleId, PGPROC::sem, set_spins_per_delay(), SHMQueueElemInit(), SHMQueueEmpty(), SpinLockAcquire, SpinLockRelease, PROC_HDR::spins_per_delay, STATUS_OK, SwitchToSharedLatch(), SYNC_REP_NOT_WAITING, PGPROC::syncRepLinks, PGPROC::syncRepState, PGPROC::tempNamespaceId, TRANSACTION_STATUS_IN_PROGRESS, PGXACT::vacuumFlags, PGPROC::wait_event_info, PGPROC::waitLock, PGPROC::waitLSN, PGPROC::waitProcLock, PGPROC::waitStatus, PROC_HDR::walsenderFreeProcs, PGXACT::xid, and PGXACT::xmin.

Referenced by AutoVacLauncherMain(), AutoVacWorkerMain(), BackendRun(), BootstrapModeMain(), PostgresMain(), and StartBackgroundWorker().

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  if (ProcGlobal == NULL)
313  elog(PANIC, "proc header uninitialized");
314 
315  if (MyProc != NULL)
316  elog(ERROR, "you already exist");
317 
318  /* Decide which list should supply our PGPROC. */
320  procgloballist = &ProcGlobal->autovacFreeProcs;
321  else if (IsBackgroundWorker)
322  procgloballist = &ProcGlobal->bgworkerFreeProcs;
323  else if (am_walsender)
324  procgloballist = &ProcGlobal->walsenderFreeProcs;
325  else
326  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  */
336 
338 
339  MyProc = *procgloballist;
340 
341  if (MyProc != NULL)
342  {
343  *procgloballist = (PGPROC *) MyProc->links.next;
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  */
355  if (am_walsender)
356  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  ereport(FATAL,
361  (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
362  errmsg("sorry, too many clients already")));
363  }
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  */
380 
381  /*
382  * Initialize all fields of MyProc, except for those previously
383  * initialized by InitProcGlobal.
384  */
388  MyProc->fpVXIDLock = false;
392  MyProc->pid = MyProcPid;
393  /* backendId, databaseId and roleId will be filled in later */
399  MyPgXact->delayChkpt = false;
400  MyPgXact->vacuumFlags = 0;
401  /* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */
404  MyProc->lwWaiting = false;
405  MyProc->lwWaitMode = 0;
406  MyProc->waitLock = NULL;
407  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++)
415  }
416 #endif
418 
419  /* Initialize fields for sync rep */
420  MyProc->waitLSN = 0;
423 
424  /* Initialize fields for group XID clearing. */
425  MyProc->procArrayGroupMember = false;
428 
429  /* Check that group locking fields are in a proper initial state. */
430  Assert(MyProc->lockGroupLeader == NULL);
432 
433  /* Initialize wait event information. */
434  MyProc->wait_event_info = 0;
435 
436  /* Initialize fields for group transaction status update. */
437  MyProc->clogGroupMember = false;
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  */
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  */
458 
459  /*
460  * Arrange to clean up at backend exit.
461  */
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  */
470 }
bool procArrayGroupMember
Definition: proc.h:166
static void ProcKill(int code, Datum arg)
Definition: proc.c:815
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
XidStatus clogGroupMemberXidStatus
Definition: proc.h:182
void set_spins_per_delay(int shared_spins_per_delay)
Definition: s_lock.c:195
int MyProcPid
Definition: globals.c:40
BackendId backendId
Definition: proc.h:113
uint32 wait_event_info
Definition: proc.h:176
XLogRecPtr clogGroupMemberLsn
Definition: proc.h:186
dlist_head lockGroupMembers
Definition: proc.h:204
Oid tempNamespaceId
Definition: proc.h:117
TransactionId xmin
Definition: proc.h:228
PGXACT * allPgXact
Definition: proc.h:249
PGPROC * MyProc
Definition: proc.c:67
TransactionId xid
Definition: proc.h:224
SHM_QUEUE links
Definition: proc.h:98
struct SHM_QUEUE * next
Definition: shmem.h:31
void PGSemaphoreReset(PGSemaphore sema)
Definition: posix_sema.c:295
bool lwWaiting
Definition: proc.h:130
#define SYNC_REP_NOT_WAITING
Definition: syncrep.h:31
PGPROC * bgworkerFreeProcs
Definition: proc.h:257
Oid roleId
Definition: proc.h:115
int errcode(int sqlerrcode)
Definition: elog.c:608
PROC_HDR * ProcGlobal
Definition: proc.c:80
uint8 lwWaitMode
Definition: proc.h:131
pg_atomic_uint32 clogGroupNext
Definition: proc.h:180
bool fpVXIDLock
Definition: proc.h:195
#define PANIC
Definition: elog.h:53
PGPROC * autovacFreeProcs
Definition: proc.h:255
PGPROC * walsenderFreeProcs
Definition: proc.h:259
Latch procLatch
Definition: proc.h:104
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:233
bool IsBackgroundWorker
Definition: globals.c:111
bool isBackgroundWorker
Definition: proc.h:120
bool am_walsender
Definition: walsender.c:114
#define SpinLockAcquire(lock)
Definition: spin.h:62
int spins_per_delay
Definition: proc.h:269
#define ERROR
Definition: elog.h:43
void OwnLatch(Latch *latch)
Definition: latch.c:296
#define FATAL
Definition: elog.h:52
PROCLOCK * waitProcLock
Definition: proc.h:140
void InitDeadLockChecking(void)
Definition: deadlock.c:143
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
int clogGroupMemberPage
Definition: proc.h:184
bool recoveryConflictPending
Definition: proc.h:127
bool IsUnderPostmaster
Definition: globals.c:109
#define InvalidTransactionId
Definition: transam.h:31
TransactionId clogGroupMemberXid
Definition: proc.h:181
Oid databaseId
Definition: proc.h:114
PGPROC ** procgloballist
Definition: proc.h:99
LOCK * waitLock
Definition: proc.h:139
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:3281
#define ereport(elevel, rest)
Definition: elog.h:141
#define STATUS_OK
Definition: c.h:1120
bool delayChkpt
Definition: proc.h:235
#define INVALID_PGPROCNO
Definition: proc.h:77
pg_atomic_uint32 procArrayGroupNext
Definition: proc.h:168
int max_wal_senders
Definition: walsender.c:120
bool SHMQueueEmpty(const SHM_QUEUE *queue)
Definition: shmqueue.c:180
#define SpinLockRelease(lock)
Definition: spin.h:64
#define InvalidBackendId
Definition: backendid.h:23
NON_EXEC_STATIC slock_t * ProcStructLock
Definition: proc.c:77
void SwitchToSharedLatch(void)
Definition: miscinit.c:342
int waitStatus
Definition: proc.h:102
#define InvalidOid
Definition: postgres_ext.h:36
#define IsAnyAutoVacuumProcess()
Definition: autovacuum.h:53
bool IsAutoVacuumLauncherProcess(void)
Definition: autovacuum.c:3275
#define Assert(condition)
Definition: c.h:739
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
PGPROC * freeProcs
Definition: proc.h:253
SHM_QUEUE syncRepLinks
Definition: proc.h:153
void SHMQueueElemInit(SHM_QUEUE *queue)
Definition: shmqueue.c:57
int pgprocno
Definition: proc.h:110
int errmsg(const char *fmt,...)
Definition: elog.c:822
bool clogGroupMember
Definition: proc.h:179
#define elog(elevel,...)
Definition: elog.h:228
#define InvalidLocalTransactionId
Definition: lock.h:68
int i
SHM_QUEUE myProcLocks[NUM_LOCK_PARTITIONS]
Definition: proc.h:160
PGSemaphore sem
Definition: proc.h:101
int syncRepState
Definition: proc.h:152
Definition: proc.h:95
int pid
Definition: proc.h:109
XLogRecPtr waitLSN
Definition: proc.h:151
PGPROC * lockGroupLeader
Definition: proc.h:203
LocalTransactionId fpLocalTransactionId
Definition: proc.h:196
#define PROC_IS_AUTOVACUUM
Definition: proc.h:53
TransactionId procArrayGroupMemberXid
Definition: proc.h:174
void InitLWLockAccess(void)
Definition: lwlock.c:536
#define NUM_LOCK_PARTITIONS
Definition: lwlock.h:117
#define TRANSACTION_STATUS_IN_PROGRESS
Definition: clog.h:26
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:241
void MarkPostmasterChildActive(void)
Definition: pmsignal.c:256
LocalTransactionId lxid
Definition: proc.h:106

◆ InitProcessPhase2()

void InitProcessPhase2 ( void  )

Definition at line 480 of file proc.c.

References Assert, on_shmem_exit(), ProcArrayAdd(), and RemoveProcFromArray().

Referenced by InitPostgres().

481 {
482  Assert(MyProc != NULL);
483 
484  /*
485  * Add our PGPROC to the PGPROC array in shared memory.
486  */
488 
489  /*
490  * Arrange to clean that up at backend exit.
491  */
493 }
static void RemoveProcFromArray(int code, Datum arg)
Definition: proc.c:804
PGPROC * MyProc
Definition: proc.c:67
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition: ipc.c:361
#define Assert(condition)
Definition: c.h:739
void ProcArrayAdd(PGPROC *proc)
Definition: procarray.c:277

◆ InitProcGlobal()

void InitProcGlobal ( void  )

Definition at line 163 of file proc.c.

References PROC_HDR::allPgXact, PROC_HDR::allProcCount, PROC_HDR::allProcs, Assert, PROC_HDR::autovacFreeProcs, autovacuum_max_workers, AuxiliaryProcs, PROC_HDR::bgworkerFreeProcs, PROC_HDR::checkpointerLatch, PROC_HDR::clogGroupFirst, DEFAULT_SPINS_PER_DELAY, dlist_init(), PROC_HDR::freeProcs, i, InitSharedLatch(), INVALID_PGPROCNO, PGPROC::links, LWLockInitialize(), LWTRANCHE_PROC, max_prepared_xacts, max_worker_processes, MaxBackends, MaxConnections, MemSet, SHM_QUEUE::next, NUM_AUXILIARY_PROCS, NUM_LOCK_PARTITIONS, pg_atomic_init_u32(), PGPROC::pgprocno, PGSemaphoreCreate(), PROC_HDR::procArrayGroupFirst, PGPROC::procgloballist, ProcStructLock, PGPROC::sem, ShmemAlloc(), ShmemInitStruct(), SHMQueueInit(), SpinLockInit, PROC_HDR::spins_per_delay, PROC_HDR::startupBufferPinWaitBufId, PROC_HDR::startupProc, PROC_HDR::startupProcPid, PROC_HDR::walsenderFreeProcs, and PROC_HDR::walwriterLatch.

Referenced by CreateSharedMemoryAndSemaphores().

164 {
165  PGPROC *procs;
166  PGXACT *pgxacts;
167  int i,
168  j;
169  bool found;
171 
172  /* Create the ProcGlobal shared structure */
173  ProcGlobal = (PROC_HDR *)
174  ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
175  Assert(!found);
176 
177  /*
178  * Initialize the data structures.
179  */
181  ProcGlobal->freeProcs = NULL;
185  ProcGlobal->startupProc = NULL;
188  ProcGlobal->walwriterLatch = NULL;
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  procs = (PGPROC *) ShmemAlloc(TotalProcs * sizeof(PGPROC));
202  MemSet(procs, 0, TotalProcs * sizeof(PGPROC));
203  ProcGlobal->allProcs = procs;
204  /* XXX allProcCount isn't really all of them; it excludes prepared xacts */
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  pgxacts = (PGXACT *) ShmemAlloc(TotalProcs * sizeof(PGXACT));
216  MemSet(pgxacts, 0, TotalProcs * sizeof(PGXACT));
217  ProcGlobal->allPgXact = pgxacts;
218 
219  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 backendLock. Prepared xact
225  * dummy PGPROCs don't need these though - they're never associated
226  * with a real process
227  */
228  if (i < MaxBackends + NUM_AUXILIARY_PROCS)
229  {
230  procs[i].sem = PGSemaphoreCreate();
231  InitSharedLatch(&(procs[i].procLatch));
232  LWLockInitialize(&(procs[i].backendLock), LWTRANCHE_PROC);
233  }
234  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  if (i < MaxConnections)
245  {
246  /* PGPROC for normal backend, add to freeProcs list */
247  procs[i].links.next = (SHM_QUEUE *) ProcGlobal->freeProcs;
248  ProcGlobal->freeProcs = &procs[i];
249  procs[i].procgloballist = &ProcGlobal->freeProcs;
250  }
251  else if (i < MaxConnections + autovacuum_max_workers + 1)
252  {
253  /* PGPROC for AV launcher/worker, add to autovacFreeProcs list */
255  ProcGlobal->autovacFreeProcs = &procs[i];
257  }
259  {
260  /* PGPROC for bgworker, add to bgworkerFreeProcs list */
262  ProcGlobal->bgworkerFreeProcs = &procs[i];
264  }
265  else if (i < MaxBackends)
266  {
267  /* PGPROC for walsender, add to walsenderFreeProcs list */
269  ProcGlobal->walsenderFreeProcs = &procs[i];
271  }
272 
273  /* Initialize myProcLocks[] shared memory queues. */
274  for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
275  SHMQueueInit(&(procs[i].myProcLocks[j]));
276 
277  /* Initialize lockGroupMembers list. */
278  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  pg_atomic_init_u32(&(procs[i].procArrayGroupNext), INVALID_PGPROCNO);
285  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  AuxiliaryProcs = &procs[MaxBackends];
294 
295  /* Create ProcStructLock spinlock, too */
296  ProcStructLock = (slock_t *) ShmemAlloc(sizeof(slock_t));
298 }
int slock_t
Definition: s_lock.h:934
void InitSharedLatch(Latch *latch)
Definition: latch.c:260
Definition: proc.h:222
PGXACT * allPgXact
Definition: proc.h:249
SHM_QUEUE links
Definition: proc.h:98
struct SHM_QUEUE * next
Definition: shmem.h:31
#define SpinLockInit(lock)
Definition: spin.h:60
PGPROC * PreparedXactProcs
Definition: proc.c:82
PGPROC * bgworkerFreeProcs
Definition: proc.h:257
PROC_HDR * ProcGlobal
Definition: proc.c:80
#define MemSet(start, val, len)
Definition: c.h:962
void * ShmemAlloc(Size size)
Definition: shmem.c:157
PGSemaphore PGSemaphoreCreate(void)
Definition: posix_sema.c:262
PGPROC * autovacFreeProcs
Definition: proc.h:255
PGPROC * walsenderFreeProcs
Definition: proc.h:259
Latch * walwriterLatch
Definition: proc.h:265
int spins_per_delay
Definition: proc.h:269
int max_prepared_xacts
Definition: twophase.c:117
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
int MaxBackends
Definition: globals.c:135
unsigned int uint32
Definition: c.h:359
PGPROC ** procgloballist
Definition: proc.h:99
#define NUM_AUXILIARY_PROCS
Definition: proc.h:292
#define INVALID_PGPROCNO
Definition: proc.h:77
Definition: proc.h:244
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition: lwlock.c:678
int startupProcPid
Definition: proc.h:272
NON_EXEC_STATIC slock_t * ProcStructLock
Definition: proc.c:77
int MaxConnections
Definition: globals.c:132
int autovacuum_max_workers
Definition: autovacuum.c:114
static void dlist_init(dlist_head *head)
Definition: ilist.h:278
Latch * checkpointerLatch
Definition: proc.h:267
#define Assert(condition)
Definition: c.h:739
NON_EXEC_STATIC PGPROC * AuxiliaryProcs
Definition: proc.c:81
PGPROC * freeProcs
Definition: proc.h:253
uint32 allProcCount
Definition: proc.h:251
pg_atomic_uint32 clogGroupFirst
Definition: proc.h:263
int pgprocno
Definition: proc.h:110
pg_atomic_uint32 procArrayGroupFirst
Definition: proc.h:261
void SHMQueueInit(SHM_QUEUE *queue)
Definition: shmqueue.c:36
int startupBufferPinWaitBufId
Definition: proc.h:274
int i
int max_worker_processes
Definition: globals.c:133
PGPROC * allProcs
Definition: proc.h:247
PGPROC * startupProc
Definition: proc.h:271
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:223
PGSemaphore sem
Definition: proc.h:101
Definition: proc.h:95
#define NUM_LOCK_PARTITIONS
Definition: lwlock.h:117
#define DEFAULT_SPINS_PER_DELAY
Definition: s_lock.h:1012

◆ IsWaitingForLock()

bool IsWaitingForLock ( void  )

Definition at line 694 of file proc.c.

Referenced by RecoveryConflictInterrupt().

695 {
696  if (lockAwaited == NULL)
697  return false;
698 
699  return true;
700 }
static LOCALLOCK * lockAwaited
Definition: proc.c:85

◆ LockErrorCleanup()

void LockErrorCleanup ( void  )

Definition at line 711 of file proc.c.

References AbortStrongLockAcquire(), DEADLOCK_TIMEOUT, disable_timeouts(), GrantAwaitedLock(), LOCALLOCK::hashcode, HOLD_INTERRUPTS, DisableTimeoutParams::id, DisableTimeoutParams::keep_indicator, PGPROC::links, LOCK_TIMEOUT, LockHashPartitionLock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), SHM_QUEUE::next, RemoveFromWaitQueue(), RESUME_INTERRUPTS, STATUS_OK, and PGPROC::waitStatus.

Referenced by AbortSubTransaction(), AbortTransaction(), ProcessInterrupts(), and ProcReleaseLocks().

712 {
713  LWLock *partitionLock;
714  DisableTimeoutParams timeouts[2];
715 
716  HOLD_INTERRUPTS();
717 
719 
720  /* Nothing to do if we weren't waiting for a lock */
721  if (lockAwaited == NULL)
722  {
724  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  timeouts[0].id = DEADLOCK_TIMEOUT;
736  timeouts[0].keep_indicator = false;
737  timeouts[1].id = LOCK_TIMEOUT;
738  timeouts[1].keep_indicator = true;
739  disable_timeouts(timeouts, 2);
740 
741  /* Unlink myself from the wait queue, if on it (might not be anymore!) */
742  partitionLock = LockHashPartitionLock(lockAwaited->hashcode);
743  LWLockAcquire(partitionLock, LW_EXCLUSIVE);
744 
745  if (MyProc->links.next != NULL)
746  {
747  /* We could not have been granted the lock yet */
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  if (MyProc->waitStatus == STATUS_OK)
760  }
761 
762  lockAwaited = NULL;
763 
764  LWLockRelease(partitionLock);
765 
767 }
uint32 hashcode
Definition: lock.h:410
Definition: lwlock.h:32
void GrantAwaitedLock(void)
Definition: lock.c:1695
PGPROC * MyProc
Definition: proc.c:67
SHM_QUEUE links
Definition: proc.h:98
struct SHM_QUEUE * next
Definition: shmem.h:31
#define LockHashPartitionLock(hashcode)
Definition: lock.h:502
void disable_timeouts(const DisableTimeoutParams *timeouts, int count)
Definition: timeout.c:565
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
TimeoutId id
Definition: timeout.h:65
void AbortStrongLockAcquire(void)
Definition: lock.c:1666
#define STATUS_OK
Definition: c.h:1120
void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode)
Definition: lock.c:1827
int waitStatus
Definition: proc.h:102
static LOCALLOCK * lockAwaited
Definition: proc.c:85
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116

◆ ProcGlobalSemas()

int ProcGlobalSemas ( void  )

Definition at line 128 of file proc.c.

References MaxBackends, and NUM_AUXILIARY_PROCS.

Referenced by CreateSharedMemoryAndSemaphores().

129 {
130  /*
131  * We need a sema per backend (including autovacuum), plus one for each
132  * auxiliary process.
133  */
135 }
int MaxBackends
Definition: globals.c:135
#define NUM_AUXILIARY_PROCS
Definition: proc.h:292

◆ ProcGlobalShmemSize()

Size ProcGlobalShmemSize ( void  )

Definition at line 102 of file proc.c.

References add_size(), max_prepared_xacts, MaxBackends, mul_size(), and NUM_AUXILIARY_PROCS.

Referenced by CreateSharedMemoryAndSemaphores().

103 {
104  Size size = 0;
105 
106  /* ProcGlobal */
107  size = add_size(size, sizeof(PROC_HDR));
108  /* MyProcs, including autovacuum workers and launcher */
109  size = add_size(size, mul_size(MaxBackends, sizeof(PGPROC)));
110  /* AuxiliaryProcs */
111  size = add_size(size, mul_size(NUM_AUXILIARY_PROCS, sizeof(PGPROC)));
112  /* Prepared xacts */
113  size = add_size(size, mul_size(max_prepared_xacts, sizeof(PGPROC)));
114  /* ProcStructLock */
115  size = add_size(size, sizeof(slock_t));
116 
117  size = add_size(size, mul_size(MaxBackends, sizeof(PGXACT)));
118  size = add_size(size, mul_size(NUM_AUXILIARY_PROCS, sizeof(PGXACT)));
119  size = add_size(size, mul_size(max_prepared_xacts, sizeof(PGXACT)));
120 
121  return size;
122 }
int slock_t
Definition: s_lock.h:934
Definition: proc.h:222
int max_prepared_xacts
Definition: twophase.c:117
int MaxBackends
Definition: globals.c:135
#define NUM_AUXILIARY_PROCS
Definition: proc.h:292
Definition: proc.h:244
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:467
Definition: proc.h:95

◆ ProcKill()

static void ProcKill ( int  code,
Datum  arg 
)
static

Definition at line 815 of file proc.c.

References Assert, AutovacuumLauncherPid, ConditionVariableCancelSleep(), DisownLatch(), dlist_delete(), dlist_is_empty(), i, IsAutoVacuumLauncherProcess(), IsUnderPostmaster, kill, PGPROC::links, PGPROC::lockGroupLeader, PGPROC::lockGroupLink, PGPROC::lockGroupMembers, LockHashPartitionLockByProc, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), LWLockReleaseAll(), MarkPostmasterChildInactive(), MyProc, PGPROC::myProcLocks, MyReplicationSlot, SHM_QUEUE::next, NUM_LOCK_PARTITIONS, PGPROC::procgloballist, PGPROC::procLatch, ProcStructLock, ReplicationSlotCleanup(), ReplicationSlotRelease(), SHMQueueEmpty(), SIGUSR2, SpinLockAcquire, SpinLockRelease, PROC_HDR::spins_per_delay, SwitchBackToLocalLatch(), SyncRepCleanupAtProcExit(), and update_spins_per_delay().

Referenced by InitProcess().

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 */
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++)
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  */
841 
842  /* Cancel any pending condition variable sleep, too */
844 
845  /* Make sure active replication slots are released */
846  if (MyReplicationSlot != NULL)
848 
849  /* Also cleanup all the temporary slots. */
851 
852  /*
853  * Detach from any lock group of which we are a member. If the leader
854  * exist before all other group members, it's 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  if (MyProc->lockGroupLeader != NULL)
859  {
860  PGPROC *leader = MyProc->lockGroupLeader;
861  LWLock *leader_lwlock = LockHashPartitionLockByProc(leader);
862 
863  LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
866  if (dlist_is_empty(&leader->lockGroupMembers))
867  {
868  leader->lockGroupLeader = NULL;
869  if (leader != MyProc)
870  {
871  procgloballist = leader->procgloballist;
872 
873  /* Leader exited first; return its PGPROC. */
875  leader->links.next = (SHM_QUEUE *) *procgloballist;
876  *procgloballist = leader;
878  }
879  }
880  else if (leader != MyProc)
881  MyProc->lockGroupLeader = NULL;
882  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  */
892  proc = MyProc;
893  MyProc = NULL;
894  DisownLatch(&proc->procLatch);
895 
896  procgloballist = proc->procgloballist;
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  if (proc->lockGroupLeader == NULL)
905  {
906  /* Since lockGroupLeader is NULL, lockGroupMembers should be empty. */
908 
909  /* Return PGPROC structure (and semaphore) to appropriate freelist */
910  proc->links.next = (SHM_QUEUE *) *procgloballist;
911  *procgloballist = proc;
912  }
913 
914  /* Update shared estimate of spins_per_delay */
916 
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  */
926 
927  /* wake autovac launcher if needed -- see comments in FreeWorkerInfo */
928  if (AutovacuumLauncherPid != 0)
930 }
Definition: lwlock.h:32
dlist_head lockGroupMembers
Definition: proc.h:204
PGPROC * MyProc
Definition: proc.c:67
SHM_QUEUE links
Definition: proc.h:98
struct SHM_QUEUE * next
Definition: shmem.h:31
PROC_HDR * ProcGlobal
Definition: proc.c:80
void MarkPostmasterChildInactive(void)
Definition: pmsignal.c:289
#define kill(pid, sig)
Definition: win32_port.h:426
void DisownLatch(Latch *latch)
Definition: latch.c:316
#define SIGUSR2
Definition: win32_port.h:167
Latch procLatch
Definition: proc.h:104
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
#define SpinLockAcquire(lock)
Definition: spin.h:62
void SwitchBackToLocalLatch(void)
Definition: miscinit.c:361
dlist_node lockGroupLink
Definition: proc.h:205
void ConditionVariableCancelSleep(void)
int spins_per_delay
Definition: proc.h:269
int AutovacuumLauncherPid
Definition: autovacuum.c:304
bool IsUnderPostmaster
Definition: globals.c:109
void ReplicationSlotRelease(void)
Definition: slot.c:424
PGPROC ** procgloballist
Definition: proc.h:99
static void dlist_delete(dlist_node *node)
Definition: ilist.h:358
int update_spins_per_delay(int shared_spins_per_delay)
Definition: s_lock.c:206
bool SHMQueueEmpty(const SHM_QUEUE *queue)
Definition: shmqueue.c:180
#define LockHashPartitionLockByProc(leader_pgproc)
Definition: lock.h:517
#define SpinLockRelease(lock)
Definition: spin.h:64
NON_EXEC_STATIC slock_t * ProcStructLock
Definition: proc.c:77
bool IsAutoVacuumLauncherProcess(void)
Definition: autovacuum.c:3275
ReplicationSlot * MyReplicationSlot
Definition: slot.c:96
#define Assert(condition)
Definition: c.h:739
static bool dlist_is_empty(dlist_head *head)
Definition: ilist.h:289
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
void SyncRepCleanupAtProcExit(void)
Definition: syncrep.c:369
int i
void ReplicationSlotCleanup(void)
Definition: slot.c:479
SHM_QUEUE myProcLocks[NUM_LOCK_PARTITIONS]
Definition: proc.h:160
void LWLockReleaseAll(void)
Definition: lwlock.c:1825
Definition: proc.h:95
PGPROC * lockGroupLeader
Definition: proc.h:203
#define NUM_LOCK_PARTITIONS
Definition: lwlock.h:117

◆ ProcLockWakeup()

void ProcLockWakeup ( LockMethod  lockMethodTable,
LOCK lock 
)

Definition at line 1628 of file proc.c.

References Assert, LockMethodData::conflictTab, GrantLock(), PROC_QUEUE::links, PGPROC::links, LOCKBIT_ON, LockCheckConflicts(), SHM_QUEUE::next, ProcWakeup(), PROC_QUEUE::size, STATUS_OK, PGPROC::waitLockMode, PGPROC::waitProcLock, and LOCK::waitProcs.

Referenced by CleanUpLock(), and DeadLockCheck().

1629 {
1630  PROC_QUEUE *waitQueue = &(lock->waitProcs);
1631  int queue_size = waitQueue->size;
1632  PGPROC *proc;
1633  LOCKMASK aheadRequests = 0;
1634 
1635  Assert(queue_size >= 0);
1636 
1637  if (queue_size == 0)
1638  return;
1639 
1640  proc = (PGPROC *) waitQueue->links.next;
1641 
1642  while (queue_size-- > 0)
1643  {
1644  LOCKMODE lockmode = proc->waitLockMode;
1645 
1646  /*
1647  * Waken if (a) doesn't conflict with requests of earlier waiters, and
1648  * (b) doesn't conflict with already-held locks.
1649  */
1650  if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1651  LockCheckConflicts(lockMethodTable,
1652  lockmode,
1653  lock,
1654  proc->waitProcLock) == STATUS_OK)
1655  {
1656  /* OK to waken */
1657  GrantLock(lock, proc->waitProcLock, lockmode);
1658  proc = ProcWakeup(proc, STATUS_OK);
1659 
1660  /*
1661  * ProcWakeup removes proc from the lock's waiting process queue
1662  * and returns the next proc in chain; don't use proc's next-link,
1663  * because it's been cleared.
1664  */
1665  }
1666  else
1667  {
1668  /*
1669  * Cannot wake this guy. Remember his request for later checks.
1670  */
1671  aheadRequests |= LOCKBIT_ON(lockmode);
1672  proc = (PGPROC *) proc->links.next;
1673  }
1674  }
1675 
1676  Assert(waitQueue->size >= 0);
1677 }
int LockCheckConflicts(LockMethod lockMethodTable, LOCKMODE lockmode, LOCK *lock, PROCLOCK *proclock)
Definition: lock.c:1344
int LOCKMODE
Definition: lockdefs.h:26
SHM_QUEUE links
Definition: lock.h:31
void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode)
Definition: lock.c:1467
SHM_QUEUE links
Definition: proc.h:98
struct SHM_QUEUE * next
Definition: shmem.h:31
LOCKMODE waitLockMode
Definition: proc.h:141
const LOCKMASK * conflictTab
Definition: lock.h:113
PROC_QUEUE waitProcs
Definition: lock.h:296
PROCLOCK * waitProcLock
Definition: proc.h:140
#define STATUS_OK
Definition: c.h:1120
#define Assert(condition)
Definition: c.h:739
int LOCKMASK
Definition: lockdefs.h:25
#define LOCKBIT_ON(lockmode)
Definition: lock.h:86
int size
Definition: lock.h:32
PGPROC * ProcWakeup(PGPROC *proc, int waitStatus)
Definition: proc.c:1592
Definition: proc.h:95

◆ ProcQueueInit()

void ProcQueueInit ( PROC_QUEUE queue)

Definition at line 1038 of file proc.c.

References PROC_QUEUE::links, SHMQueueInit(), and PROC_QUEUE::size.

Referenced by AuxiliaryPidGetProc(), DeadLockCheck(), lock_twophase_recover(), and SetupLockInTable().

1039 {
1040  SHMQueueInit(&(queue->links));
1041  queue->size = 0;
1042 }
SHM_QUEUE links
Definition: lock.h:31
void SHMQueueInit(SHM_QUEUE *queue)
Definition: shmqueue.c:36
int size
Definition: lock.h:32

◆ ProcReleaseLocks()

void ProcReleaseLocks ( bool  isCommit)

Definition at line 787 of file proc.c.

References DEFAULT_LOCKMETHOD, LockErrorCleanup(), LockReleaseAll(), and USER_LOCKMETHOD.

Referenced by ResourceOwnerReleaseInternal().

788 {
789  if (!MyProc)
790  return;
791  /* If waiting, get off wait queue (should only be needed after error) */
793  /* Release standard locks, including session-level if aborting */
795  /* Release transaction-level advisory locks */
797 }
PGPROC * MyProc
Definition: proc.c:67
#define DEFAULT_LOCKMETHOD
Definition: lock.h:127
#define USER_LOCKMETHOD
Definition: lock.h:128
void LockErrorCleanup(void)
Definition: proc.c:711
void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
Definition: lock.c:2089

◆ ProcSendSignal()

void ProcSendSignal ( int  pid)

Definition at line 1810 of file proc.c.

References BackendPidGetProc(), PGPROC::procLatch, ProcStructLock, RecoveryInProgress(), SetLatch(), SpinLockAcquire, SpinLockRelease, PROC_HDR::startupProc, and PROC_HDR::startupProcPid.

Referenced by ReleasePredicateLocks(), and UnpinBuffer().

1811 {
1812  PGPROC *proc = NULL;
1813 
1814  if (RecoveryInProgress())
1815  {
1817 
1818  /*
1819  * Check to see whether it is the Startup process we wish to signal.
1820  * This call is made by the buffer manager when it wishes to wake up a
1821  * process that has been waiting for a pin in so it can obtain a
1822  * cleanup lock using LockBufferForCleanup(). Startup is not a normal
1823  * backend, so BackendPidGetProc() will not return any pid at all. So
1824  * we remember the information for this special case.
1825  */
1826  if (pid == ProcGlobal->startupProcPid)
1827  proc = ProcGlobal->startupProc;
1828 
1830  }
1831 
1832  if (proc == NULL)
1833  proc = BackendPidGetProc(pid);
1834 
1835  if (proc != NULL)
1836  {
1837  SetLatch(&proc->procLatch);
1838  }
1839 }
PGPROC * BackendPidGetProc(int pid)
Definition: procarray.c:2365
PROC_HDR * ProcGlobal
Definition: proc.c:80
bool RecoveryInProgress(void)
Definition: xlog.c:7935
void SetLatch(Latch *latch)
Definition: latch.c:436
Latch procLatch
Definition: proc.h:104
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
int startupProcPid
Definition: proc.h:272
NON_EXEC_STATIC slock_t * ProcStructLock
Definition: proc.c:77
PGPROC * startupProc
Definition: proc.h:271
Definition: proc.h:95

◆ ProcSleep()

int ProcSleep ( LOCALLOCK locallock,
LockMethod  lockMethodTable 
)

Definition at line 1062 of file proc.c.

References _, PROC_HDR::allPgXact, appendStringInfo(), Assert, buf, CHECK_FOR_INTERRUPTS, CheckDeadLock(), CheckRecoveryConflictDeadlock(), LockMethodData::conflictTab, StringInfoData::data, deadlock_state, DEADLOCK_TIMEOUT, DeadlockTimeout, DEBUG1, EnableTimeoutParams::delay_ms, DescribeLockTag(), disable_timeout(), disable_timeouts(), DS_BLOCKED_BY_AUTOVACUUM, DS_HARD_DEADLOCK, DS_NO_DEADLOCK, DS_NOT_YET_CHECKED, DS_SOFT_DEADLOCK, enable_timeout_after(), enable_timeouts(), ereport, errdetail_log(), errdetail_log_plural(), errmsg(), get_timeout_start_time(), GetBlockingAutoVacuumPgproc(), GetCurrentTimestamp(), GetLockmodeName(), got_deadlock_timeout, GrantAwaitedLock(), GrantLock(), PROCLOCK::groupLeader, LOCALLOCK::hashcode, PGPROC::heldLocks, PROCLOCK::holdMask, i, EnableTimeoutParams::id, DisableTimeoutParams::id, InHotStandby, initStringInfo(), InRecovery, DisableTimeoutParams::keep_indicator, kill, PROC_QUEUE::links, PGPROC::links, LOCALLOCKTAG::lock, LOCALLOCK::lock, LOCK_TIMEOUT, LOCKBIT_ON, LockCheckConflicts(), PGPROC::lockGroupLeader, LockHashPartitionLock, PROCLOCK::lockLink, LOCKTAG::locktag_lockmethodid, LOCKTAG::locktag_type, LockTimeout, LOG, log_lock_waits, LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), LOCALLOCKTAG::mode, MyLatch, PROCLOCKTAG::myProc, MyProcPid, SHM_QUEUE::next, offsetof, pfree(), PG_WAIT_LOCK, PGPROC::pgprocno, PGPROC::pid, PROC_IS_AUTOVACUUM, PROC_VACUUM_FOR_WRAPAROUND, LOCALLOCK::proclock, LOCK::procLocks, RecoveryInProgress(), RememberSimpleDeadLock(), RemoveFromWaitQueue(), ResetLatch(), ResolveRecoveryConflictWithLock(), SHMQueueInsertBefore(), SHMQueueNext(), PROC_QUEUE::size, STATUS_ERROR, STATUS_OK, STATUS_WAITING, LOCK::tag, PROCLOCK::tag, LOCALLOCK::tag, TimestampDifference(), TMPARAM_AFTER, EnableTimeoutParams::type, PGXACT::vacuumFlags, WaitLatch(), PGPROC::waitLock, PGPROC::waitLockMode, LOCK::waitMask, PGPROC::waitProcLock, LOCK::waitProcs, PGPROC::waitStatus, WARNING, WL_EXIT_ON_PM_DEATH, and WL_LATCH_SET.

Referenced by WaitOnLock().

1063 {
1064  LOCKMODE lockmode = locallock->tag.mode;
1065  LOCK *lock = locallock->lock;
1066  PROCLOCK *proclock = locallock->proclock;
1067  uint32 hashcode = locallock->hashcode;
1068  LWLock *partitionLock = LockHashPartitionLock(hashcode);
1069  PROC_QUEUE *waitQueue = &(lock->waitProcs);
1070  LOCKMASK myHeldLocks = MyProc->heldLocks;
1071  bool early_deadlock = false;
1072  bool allow_autovacuum_cancel = true;
1073  int myWaitStatus;
1074  PGPROC *proc;
1075  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.
1081  */
1082  if (leader != NULL)
1083  {
1084  SHM_QUEUE *procLocks = &(lock->procLocks);
1085  PROCLOCK *otherproclock;
1086 
1087  otherproclock = (PROCLOCK *)
1088  SHMQueueNext(procLocks, procLocks, offsetof(PROCLOCK, lockLink));
1089  while (otherproclock != NULL)
1090  {
1091  if (otherproclock->groupLeader == leader)
1092  myHeldLocks |= otherproclock->holdMask;
1093  otherproclock = (PROCLOCK *)
1094  SHMQueueNext(procLocks, &otherproclock->lockLink,
1095  offsetof(PROCLOCK, lockLink));
1096  }
1097  }
1098 
1099  /*
1100  * Determine where to add myself in the wait queue.
1101  *
1102  * Normally I should go at the end of the queue. However, if I already
1103  * hold locks that conflict with the request of any previous waiter, put
1104  * myself in the queue just in front of the first such waiter. This is not
1105  * a necessary step, since deadlock detection would move me to before that
1106  * waiter anyway; but it's relatively cheap to detect such a conflict
1107  * immediately, and avoid delaying till deadlock timeout.
1108  *
1109  * Special case: if I find I should go in front of some waiter, check to
1110  * see if I conflict with already-held locks or the requests before that
1111  * waiter. If not, then just grant myself the requested lock immediately.
1112  * This is the same as the test for immediate grant in LockAcquire, except
1113  * we are only considering the part of the wait queue before my insertion
1114  * point.
1115  */
1116  if (myHeldLocks != 0)
1117  {
1118  LOCKMASK aheadRequests = 0;
1119 
1120  proc = (PGPROC *) waitQueue->links.next;
1121  for (i = 0; i < waitQueue->size; i++)
1122  {
1123  /*
1124  * If we're part of the same locking group as this waiter, its
1125  * locks neither conflict with ours nor contribute to
1126  * aheadRequests.
1127  */
1128  if (leader != NULL && leader == proc->lockGroupLeader)
1129  {
1130  proc = (PGPROC *) proc->links.next;
1131  continue;
1132  }
1133  /* Must he wait for me? */
1134  if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
1135  {
1136  /* Must I wait for him ? */
1137  if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
1138  {
1139  /*
1140  * Yes, so we have a deadlock. Easiest way to clean up
1141  * correctly is to call RemoveFromWaitQueue(), but we
1142  * can't do that until we are *on* the wait queue. So, set
1143  * a flag to check below, and break out of loop. Also,
1144  * record deadlock info for later message.
1145  */
1146  RememberSimpleDeadLock(MyProc, lockmode, lock, proc);
1147  early_deadlock = true;
1148  break;
1149  }
1150  /* I must go before this waiter. Check special case. */
1151  if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1152  LockCheckConflicts(lockMethodTable,
1153  lockmode,
1154  lock,
1155  proclock) == STATUS_OK)
1156  {
1157  /* Skip the wait and just grant myself the lock. */
1158  GrantLock(lock, proclock, lockmode);
1159  GrantAwaitedLock();
1160  return STATUS_OK;
1161  }
1162  /* Break out of loop to put myself before him */
1163  break;
1164  }
1165  /* Nope, so advance to next waiter */
1166  aheadRequests |= LOCKBIT_ON(proc->waitLockMode);
1167  proc = (PGPROC *) proc->links.next;
1168  }
1169 
1170  /*
1171  * If we fall out of loop normally, proc points to waitQueue head, so
1172  * we will insert at tail of queue as desired.
1173  */
1174  }
1175  else
1176  {
1177  /* I hold no locks, so I can't push in front of anyone. */
1178  proc = (PGPROC *) &(waitQueue->links);
1179  }
1180 
1181  /*
1182  * Insert self into queue, ahead of the given proc (or at tail of queue).
1183  */
1184  SHMQueueInsertBefore(&(proc->links), &(MyProc->links));
1185  waitQueue->size++;
1186 
1187  lock->waitMask |= LOCKBIT_ON(lockmode);
1188 
1189  /* Set up wait information in PGPROC object, too */
1190  MyProc->waitLock = lock;
1191  MyProc->waitProcLock = proclock;
1192  MyProc->waitLockMode = lockmode;
1193 
1195 
1196  /*
1197  * If we detected deadlock, give up without waiting. This must agree with
1198  * CheckDeadLock's recovery code.
1199  */
1200  if (early_deadlock)
1201  {
1202  RemoveFromWaitQueue(MyProc, hashcode);
1203  return STATUS_ERROR;
1204  }
1205 
1206  /* mark that we are waiting for a lock */
1207  lockAwaited = locallock;
1208 
1209  /*
1210  * Release the lock table's partition lock.
1211  *
1212  * NOTE: this may also cause us to exit critical-section state, possibly
1213  * allowing a cancel/die interrupt to be accepted. This is OK because we
1214  * have recorded the fact that we are waiting for a lock, and so
1215  * LockErrorCleanup will clean up if cancel/die happens.
1216  */
1217  LWLockRelease(partitionLock);
1218 
1219  /*
1220  * Also, now that we will successfully clean up after an ereport, it's
1221  * safe to check to see if there's a buffer pin deadlock against the
1222  * Startup process. Of course, that's only necessary if we're doing Hot
1223  * Standby and are not the Startup process ourselves.
1224  */
1225  if (RecoveryInProgress() && !InRecovery)
1227 
1228  /* Reset deadlock_state before enabling the timeout handler */
1230  got_deadlock_timeout = false;
1231 
1232  /*
1233  * Set timer so we can wake up after awhile and check for a deadlock. If a
1234  * deadlock is detected, the handler sets MyProc->waitStatus =
1235  * STATUS_ERROR, allowing us to know that we must report failure rather
1236  * than success.
1237  *
1238  * By delaying the check until we've waited for a bit, we can avoid
1239  * running the rather expensive deadlock-check code in most cases.
1240  *
1241  * If LockTimeout is set, also enable the timeout for that. We can save a
1242  * few cycles by enabling both timeout sources in one call.
1243  *
1244  * If InHotStandby we set lock waits slightly later for clarity with other
1245  * code.
1246  */
1247  if (!InHotStandby)
1248  {
1249  if (LockTimeout > 0)
1250  {
1251  EnableTimeoutParams timeouts[2];
1252 
1253  timeouts[0].id = DEADLOCK_TIMEOUT;
1254  timeouts[0].type = TMPARAM_AFTER;
1255  timeouts[0].delay_ms = DeadlockTimeout;
1256  timeouts[1].id = LOCK_TIMEOUT;
1257  timeouts[1].type = TMPARAM_AFTER;
1258  timeouts[1].delay_ms = LockTimeout;
1259  enable_timeouts(timeouts, 2);
1260  }
1261  else
1263  }
1264 
1265  /*
1266  * If somebody wakes us between LWLockRelease and WaitLatch, the latch
1267  * will not wait. But a set latch does not necessarily mean that the lock
1268  * is free now, as there are many other sources for latch sets than
1269  * somebody releasing the lock.
1270  *
1271  * We process interrupts whenever the latch has been set, so cancel/die
1272  * interrupts are processed quickly. This means we must not mind losing
1273  * control to a cancel/die interrupt here. We don't, because we have no
1274  * shared-state-change work to do after being granted the lock (the
1275  * grantor did it all). We do have to worry about canceling the deadlock
1276  * timeout and updating the locallock table, but if we lose control to an
1277  * error, LockErrorCleanup will fix that up.
1278  */
1279  do
1280  {
1281  if (InHotStandby)
1282  {
1283  /* Set a timer and wait for that or for the Lock to be granted */
1285  }
1286  else
1287  {
1289  PG_WAIT_LOCK | locallock->tag.lock.locktag_type);
1291  /* check for deadlocks first, as that's probably log-worthy */
1293  {
1294  CheckDeadLock();
1295  got_deadlock_timeout = false;
1296  }
1298  }
1299 
1300  /*
1301  * waitStatus could change from STATUS_WAITING to something else
1302  * asynchronously. Read it just once per loop to prevent surprising
1303  * behavior (such as missing log messages).
1304  */
1305  myWaitStatus = *((volatile int *) &MyProc->waitStatus);
1306 
1307  /*
1308  * If we are not deadlocked, but are waiting on an autovacuum-induced
1309  * task, send a signal to interrupt it.
1310  */
1311  if (deadlock_state == DS_BLOCKED_BY_AUTOVACUUM && allow_autovacuum_cancel)
1312  {
1313  PGPROC *autovac = GetBlockingAutoVacuumPgproc();
1314  PGXACT *autovac_pgxact = &ProcGlobal->allPgXact[autovac->pgprocno];
1315 
1316  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1317 
1318  /*
1319  * Only do it if the worker is not working to protect against Xid
1320  * wraparound.
1321  */
1322  if ((autovac_pgxact->vacuumFlags & PROC_IS_AUTOVACUUM) &&
1323  !(autovac_pgxact->vacuumFlags & PROC_VACUUM_FOR_WRAPAROUND))
1324  {
1325  int pid = autovac->pid;
1326  StringInfoData locktagbuf;
1327  StringInfoData logbuf; /* errdetail for server log */
1328 
1329  initStringInfo(&locktagbuf);
1330  initStringInfo(&logbuf);
1331  DescribeLockTag(&locktagbuf, &lock->tag);
1332  appendStringInfo(&logbuf,
1333  _("Process %d waits for %s on %s."),
1334  MyProcPid,
1336  lockmode),
1337  locktagbuf.data);
1338 
1339  /* release lock as quickly as possible */
1340  LWLockRelease(ProcArrayLock);
1341 
1342  /* send the autovacuum worker Back to Old Kent Road */
1343  ereport(DEBUG1,
1344  (errmsg("sending cancel to blocking autovacuum PID %d",
1345  pid),
1346  errdetail_log("%s", logbuf.data)));
1347 
1348  if (kill(pid, SIGINT) < 0)
1349  {
1350  /*
1351  * There's a race condition here: once we release the
1352  * ProcArrayLock, it's possible for the autovac worker to
1353  * close up shop and exit before we can do the kill().
1354  * Therefore, we do not whinge about no-such-process.
1355  * Other errors such as EPERM could conceivably happen if
1356  * the kernel recycles the PID fast enough, but such cases
1357  * seem improbable enough that it's probably best to issue
1358  * a warning if we see some other errno.
1359  */
1360  if (errno != ESRCH)
1361  ereport(WARNING,
1362  (errmsg("could not send signal to process %d: %m",
1363  pid)));
1364  }
1365 
1366  pfree(logbuf.data);
1367  pfree(locktagbuf.data);
1368  }
1369  else
1370  LWLockRelease(ProcArrayLock);
1371 
1372  /* prevent signal from being resent more than once */
1373  allow_autovacuum_cancel = false;
1374  }
1375 
1376  /*
1377  * If awoken after the deadlock check interrupt has run, and
1378  * log_lock_waits is on, then report about the wait.
1379  */
1381  {
1383  lock_waiters_sbuf,
1384  lock_holders_sbuf;
1385  const char *modename;
1386  long secs;
1387  int usecs;
1388  long msecs;
1389  SHM_QUEUE *procLocks;
1390  PROCLOCK *proclock;
1391  bool first_holder = true,
1392  first_waiter = true;
1393  int lockHoldersNum = 0;
1394 
1395  initStringInfo(&buf);
1396  initStringInfo(&lock_waiters_sbuf);
1397  initStringInfo(&lock_holders_sbuf);
1398 
1399  DescribeLockTag(&buf, &locallock->tag.lock);
1400  modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
1401  lockmode);
1404  &secs, &usecs);
1405  msecs = secs * 1000 + usecs / 1000;
1406  usecs = usecs % 1000;
1407 
1408  /*
1409  * we loop over the lock's procLocks to gather a list of all
1410  * holders and waiters. Thus we will be able to provide more
1411  * detailed information for lock debugging purposes.
1412  *
1413  * lock->procLocks contains all processes which hold or wait for
1414  * this lock.
1415  */
1416 
1417  LWLockAcquire(partitionLock, LW_SHARED);
1418 
1419  procLocks = &(lock->procLocks);
1420  proclock = (PROCLOCK *) SHMQueueNext(procLocks, procLocks,
1421  offsetof(PROCLOCK, lockLink));
1422 
1423  while (proclock)
1424  {
1425  /*
1426  * we are a waiter if myProc->waitProcLock == proclock; we are
1427  * a holder if it is NULL or something different
1428  */
1429  if (proclock->tag.myProc->waitProcLock == proclock)
1430  {
1431  if (first_waiter)
1432  {
1433  appendStringInfo(&lock_waiters_sbuf, "%d",
1434  proclock->tag.myProc->pid);
1435  first_waiter = false;
1436  }
1437  else
1438  appendStringInfo(&lock_waiters_sbuf, ", %d",
1439  proclock->tag.myProc->pid);
1440  }
1441  else
1442  {
1443  if (first_holder)
1444  {
1445  appendStringInfo(&lock_holders_sbuf, "%d",
1446  proclock->tag.myProc->pid);
1447  first_holder = false;
1448  }
1449  else
1450  appendStringInfo(&lock_holders_sbuf, ", %d",
1451  proclock->tag.myProc->pid);
1452 
1453  lockHoldersNum++;
1454  }
1455 
1456  proclock = (PROCLOCK *) SHMQueueNext(procLocks, &proclock->lockLink,
1457  offsetof(PROCLOCK, lockLink));
1458  }
1459 
1460  LWLockRelease(partitionLock);
1461 
1463  ereport(LOG,
1464  (errmsg("process %d avoided deadlock for %s on %s by rearranging queue order after %ld.%03d ms",
1465  MyProcPid, modename, buf.data, msecs, usecs),
1466  (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1467  "Processes holding the lock: %s. Wait queue: %s.",
1468  lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1469  else if (deadlock_state == DS_HARD_DEADLOCK)
1470  {
1471  /*
1472  * This message is a bit redundant with the error that will be
1473  * reported subsequently, but in some cases the error report
1474  * might not make it to the log (eg, if it's caught by an
1475  * exception handler), and we want to ensure all long-wait
1476  * events get logged.
1477  */
1478  ereport(LOG,
1479  (errmsg("process %d detected deadlock while waiting for %s on %s after %ld.%03d ms",
1480  MyProcPid, modename, buf.data, msecs, usecs),
1481  (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1482  "Processes holding the lock: %s. Wait queue: %s.",
1483  lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1484  }
1485 
1486  if (myWaitStatus == STATUS_WAITING)
1487  ereport(LOG,
1488  (errmsg("process %d still waiting for %s on %s after %ld.%03d ms",
1489  MyProcPid, modename, buf.data, msecs, usecs),
1490  (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1491  "Processes holding the lock: %s. Wait queue: %s.",
1492  lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1493  else if (myWaitStatus == STATUS_OK)
1494  ereport(LOG,
1495  (errmsg("process %d acquired %s on %s after %ld.%03d ms",
1496  MyProcPid, modename, buf.data, msecs, usecs)));
1497  else
1498  {
1499  Assert(myWaitStatus == STATUS_ERROR);
1500 
1501  /*
1502  * Currently, the deadlock checker always kicks its own
1503  * process, which means that we'll only see STATUS_ERROR when
1504  * deadlock_state == DS_HARD_DEADLOCK, and there's no need to
1505  * print redundant messages. But for completeness and
1506  * future-proofing, print a message if it looks like someone
1507  * else kicked us off the lock.
1508  */
1510  ereport(LOG,
1511  (errmsg("process %d failed to acquire %s on %s after %ld.%03d ms",
1512  MyProcPid, modename, buf.data, msecs, usecs),
1513  (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1514  "Processes holding the lock: %s. Wait queue: %s.",
1515  lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1516  }
1517 
1518  /*
1519  * At this point we might still need to wait for the lock. Reset
1520  * state so we don't print the above messages again.
1521  */
1523 
1524  pfree(buf.data);
1525  pfree(lock_holders_sbuf.data);
1526  pfree(lock_waiters_sbuf.data);
1527  }
1528  } while (myWaitStatus == STATUS_WAITING);
1529 
1530  /*
1531  * Disable the timers, if they are still running. As in LockErrorCleanup,
1532  * we must preserve the LOCK_TIMEOUT indicator flag: if a lock timeout has
1533  * already caused QueryCancelPending to become set, we want the cancel to
1534  * be reported as a lock timeout, not a user cancel.
1535  */
1536  if (!InHotStandby)
1537  {
1538  if (LockTimeout > 0)
1539  {
1540  DisableTimeoutParams timeouts[2];
1541 
1542  timeouts[0].id = DEADLOCK_TIMEOUT;
1543  timeouts[0].keep_indicator = false;
1544  timeouts[1].id = LOCK_TIMEOUT;
1545  timeouts[1].keep_indicator = true;
1546  disable_timeouts(timeouts, 2);
1547  }
1548  else
1550  }
1551 
1552  /*
1553  * Re-acquire the lock table's partition lock. We have to do this to hold
1554  * off cancel/die interrupts before we can mess with lockAwaited (else we
1555  * might have a missed or duplicated locallock update).
1556  */
1557  LWLockAcquire(partitionLock, LW_EXCLUSIVE);
1558 
1559  /*
1560  * We no longer want LockErrorCleanup to do anything.
1561  */
1562  lockAwaited = NULL;
1563 
1564  /*
1565  * If we got the lock, be sure to remember it in the locallock table.
1566  */
1567  if (MyProc->waitStatus == STATUS_OK)
1568  GrantAwaitedLock();
1569 
1570  /*
1571  * We don't have to do anything else, because the awaker did all the
1572  * necessary update of the lock table and MyProc.
1573  */
1574  return MyProc->waitStatus;
1575 }
TimestampTz get_timeout_start_time(TimeoutId id)
Definition: timeout.c:668
PROCLOCKTAG tag
Definition: lock.h:350
void ResolveRecoveryConflictWithLock(LOCKTAG locktag)
Definition: standby.c:389
uint32 hashcode
Definition: lock.h:410
#define PG_WAIT_LOCK
Definition: pgstat.h:755
Definition: lwlock.h:32
TimeoutId id
Definition: timeout.h:54
int LockCheckConflicts(LockMethod lockMethodTable, LOCKMODE lockmode, LOCK *lock, PROCLOCK *proclock)
Definition: lock.c:1344
LOCALLOCKTAG tag
Definition: lock.h:407
#define DEBUG1
Definition: elog.h:25
int MyProcPid
Definition: globals.c:40
const char * GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode)
Definition: lock.c:3925
LOCKTAG lock
Definition: lock.h:388
void GrantAwaitedLock(void)
Definition: lock.c:1695
Definition: proc.h:222
int LOCKMODE
Definition: lockdefs.h:26
LOCKMODE mode
Definition: lock.h:389
PROCLOCK * proclock
Definition: lock.h:412
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1583
SHM_QUEUE links
Definition: lock.h:31
PGXACT * allPgXact
Definition: proc.h:249
PGPROC * MyProc
Definition: proc.c:67
LOCKMASK holdMask
Definition: lock.h:354
void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode)
Definition: lock.c:1467
SHM_QUEUE links
Definition: proc.h:98
TimeoutType type
Definition: timeout.h:55
struct SHM_QUEUE * next
Definition: shmem.h:31
LOCKMODE waitLockMode
Definition: proc.h:141
bool InRecovery
Definition: xlog.c:200
LOCKTAG tag
Definition: lock.h:290
#define PROC_VACUUM_FOR_WRAPAROUND
Definition: proc.h:56
const LOCKMASK * conflictTab
Definition: lock.h:113
#define LockHashPartitionLock(hashcode)
Definition: lock.h:502
SHM_QUEUE lockLink
Definition: lock.h:356
#define InHotStandby
Definition: xlog.h:74
PROC_HDR * ProcGlobal
Definition: proc.c:80
#define STATUS_ERROR
Definition: c.h:1121
#define kill(pid, sig)
Definition: win32_port.h:426
void SHMQueueInsertBefore(SHM_QUEUE *queue, SHM_QUEUE *elem)
Definition: shmqueue.c:89
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:7935
void disable_timeouts(const DisableTimeoutParams *timeouts, int count)
Definition: timeout.c:565
void ResetLatch(Latch *latch)
Definition: latch.c:519
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:344
PROC_QUEUE waitProcs
Definition: lock.h:296
uint8 vacuumFlags
Definition: proc.h:233
void RememberSimpleDeadLock(PGPROC *proc1, LOCKMODE lockmode, LOCK *lock, PGPROC *proc2)
Definition: deadlock.c:1155
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1726
void enable_timeouts(const EnableTimeoutParams *timeouts, int count)
Definition: timeout.c:483
void pfree(void *pointer)
Definition: mcxt.c:1056
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:91
static DeadLockState deadlock_state
Definition: proc.c:87
TimeoutId id
Definition: timeout.h:65
void DescribeLockTag(StringInfo buf, const LOCKTAG *tag)
Definition: lmgr.c:1085
PROCLOCK * waitProcLock
Definition: proc.h:140
static char * buf
Definition: pg_test_fsync.c:67
static volatile sig_atomic_t got_deadlock_timeout
Definition: proc.c:90
unsigned int uint32
Definition: c.h:359
int errdetail_log(const char *fmt,...)
Definition: elog.c:1003
void CheckRecoveryConflictDeadlock(void)
Definition: standby.c:542
Definition: lock.h:287
LOCK * waitLock
Definition: proc.h:139
#define ereport(elevel, rest)
Definition: elog.h:141
#define STATUS_OK
Definition: c.h:1120
LOCKMASK waitMask
Definition: lock.h:294
SHM_QUEUE procLocks
Definition: lock.h:295
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
#define WARNING
Definition: elog.h:40
Pointer SHMQueueNext(const SHM_QUEUE *queue, const SHM_QUEUE *curElem, Size linkOffset)
Definition: shmqueue.c:145
void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode)
Definition: lock.c:1827
int waitStatus
Definition: proc.h:102
int errdetail_log_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1024
void enable_timeout_after(TimeoutId id, int delay_ms)
Definition: timeout.c:435
uint8 locktag_type
Definition: lock.h:169
#define Assert(condition)
Definition: c.h:739
bool log_lock_waits
Definition: proc.c:64
static LOCALLOCK * lockAwaited
Definition: proc.c:85
int LockTimeout
Definition: proc.c:62
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1122
LOCK * lock
Definition: lock.h:411
int LOCKMASK
Definition: lockdefs.h:25
static void CheckDeadLock(void)
Definition: proc.c:1689
uint8 locktag_lockmethodid
Definition: lock.h:170
PGPROC * myProc
Definition: lock.h:344
#define LOCKBIT_ON(lockmode)
Definition: lock.h:86
Definition: lock.h:347
int pgprocno
Definition: proc.h:110
int errmsg(const char *fmt,...)
Definition: elog.c:822
int i
int size
Definition: lock.h:32
struct Latch * MyLatch
Definition: globals.c:54
int DeadlockTimeout
Definition: proc.c:60
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define STATUS_WAITING
Definition: c.h:1124
PGPROC * GetBlockingAutoVacuumPgproc(void)
Definition: deadlock.c:293
void disable_timeout(TimeoutId id, bool keep_indicator)
Definition: timeout.c:532
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition: timestamp.c:1657
Definition: proc.h:95
int pid
Definition: proc.h:109
#define WL_LATCH_SET
Definition: latch.h:124
#define _(x)
Definition: elog.c:87
PGPROC * lockGroupLeader
Definition: proc.h:203
#define PROC_IS_AUTOVACUUM
Definition: proc.h:53
#define offsetof(type, field)
Definition: c.h:662
LOCKMASK heldLocks
Definition: proc.h:142
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
PGPROC * groupLeader
Definition: lock.h:353

◆ ProcWaitForSignal()

void ProcWaitForSignal ( uint32  wait_event_info)

Definition at line 1798 of file proc.c.

References CHECK_FOR_INTERRUPTS, MyLatch, ResetLatch(), WaitLatch(), WL_EXIT_ON_PM_DEATH, and WL_LATCH_SET.

Referenced by GetSafeSnapshot(), LockBufferForCleanup(), ResolveRecoveryConflictWithBufferPin(), and ResolveRecoveryConflictWithLock().

1799 {
1801  wait_event_info);
1804 }
void ResetLatch(Latch *latch)
Definition: latch.c:519
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:344
struct Latch * MyLatch
Definition: globals.c:54
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define WL_LATCH_SET
Definition: latch.h:124
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129

◆ ProcWakeup()

PGPROC* ProcWakeup ( PGPROC proc,
int  waitStatus 
)

Definition at line 1592 of file proc.c.

References Assert, PGPROC::links, SHM_QUEUE::next, SHM_QUEUE::prev, PGPROC::procLatch, SetLatch(), SHMQueueDelete(), PROC_QUEUE::size, STATUS_WAITING, PGPROC::waitLock, PGPROC::waitProcLock, LOCK::waitProcs, and PGPROC::waitStatus.

Referenced by ProcLockWakeup().

1593 {
1594  PGPROC *retProc;
1595 
1596  /* Proc should be sleeping ... */
1597  if (proc->links.prev == NULL ||
1598  proc->links.next == NULL)
1599  return NULL;
1600  Assert(proc->waitStatus == STATUS_WAITING);
1601 
1602  /* Save next process before we zap the list link */
1603  retProc = (PGPROC *) proc->links.next;
1604 
1605  /* Remove process from wait queue */
1606  SHMQueueDelete(&(proc->links));
1607  (proc->waitLock->waitProcs.size)--;
1608 
1609  /* Clean up process' state and pass it the ok/fail signal */
1610  proc->waitLock = NULL;
1611  proc->waitProcLock = NULL;
1612  proc->waitStatus = waitStatus;
1613 
1614  /* And awaken it */
1615  SetLatch(&proc->procLatch);
1616 
1617  return retProc;
1618 }
SHM_QUEUE links
Definition: proc.h:98
struct SHM_QUEUE * next
Definition: shmem.h:31
void SetLatch(Latch *latch)
Definition: latch.c:436
Latch procLatch
Definition: proc.h:104
PROC_QUEUE waitProcs
Definition: lock.h:296
PROCLOCK * waitProcLock
Definition: proc.h:140
LOCK * waitLock
Definition: proc.h:139
int waitStatus
Definition: proc.h:102
struct SHM_QUEUE * prev
Definition: shmem.h:30
#define Assert(condition)
Definition: c.h:739
int size
Definition: lock.h:32
void SHMQueueDelete(SHM_QUEUE *queue)
Definition: shmqueue.c:68
#define STATUS_WAITING
Definition: c.h:1124
Definition: proc.h:95

◆ PublishStartupProcessInformation()

void PublishStartupProcessInformation ( void  )

Definition at line 627 of file proc.c.

References MyProc, MyProcPid, ProcStructLock, SpinLockAcquire, SpinLockRelease, PROC_HDR::startupProc, and PROC_HDR::startupProcPid.

Referenced by StartupXLOG().

628 {
630 
633 
635 }
int MyProcPid
Definition: globals.c:40
PGPROC * MyProc
Definition: proc.c:67
PROC_HDR * ProcGlobal
Definition: proc.c:80
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
int startupProcPid
Definition: proc.h:272
NON_EXEC_STATIC slock_t * ProcStructLock
Definition: proc.c:77
PGPROC * startupProc
Definition: proc.h:271

◆ RemoveProcFromArray()

static void RemoveProcFromArray ( int  code,
Datum  arg 
)
static

Definition at line 804 of file proc.c.

References Assert, InvalidTransactionId, and ProcArrayRemove().

Referenced by InitProcessPhase2().

805 {
806  Assert(MyProc != NULL);
808 }
PGPROC * MyProc
Definition: proc.c:67
void ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
Definition: procarray.c:335
#define InvalidTransactionId
Definition: transam.h:31
#define Assert(condition)
Definition: c.h:739

◆ SetStartupBufferPinWaitBufId()

void SetStartupBufferPinWaitBufId ( int  bufid)

Definition at line 645 of file proc.c.

References ProcGlobal, and PROC_HDR::startupBufferPinWaitBufId.

Referenced by LockBufferForCleanup().

646 {
647  /* use volatile pointer to prevent code rearrangement */
648  volatile PROC_HDR *procglobal = ProcGlobal;
649 
650  procglobal->startupBufferPinWaitBufId = bufid;
651 }
PROC_HDR * ProcGlobal
Definition: proc.c:80
Definition: proc.h:244
int startupBufferPinWaitBufId
Definition: proc.h:274

Variable Documentation

◆ AuxiliaryProcs

◆ deadlock_state

DeadLockState deadlock_state = DS_NOT_YET_CHECKED
static

Definition at line 87 of file proc.c.

Referenced by CheckDeadLock(), and ProcSleep().

◆ DeadlockTimeout

int DeadlockTimeout = 1000

Definition at line 60 of file proc.c.

Referenced by ProcSleep(), and ResolveRecoveryConflictWithBufferPin().

◆ got_deadlock_timeout

volatile sig_atomic_t got_deadlock_timeout
static

Definition at line 90 of file proc.c.

Referenced by CheckDeadLockAlert(), and ProcSleep().

◆ IdleInTransactionSessionTimeout

int IdleInTransactionSessionTimeout = 0

Definition at line 63 of file proc.c.

Referenced by PostgresMain(), and ProcessInterrupts().

◆ lockAwaited

LOCALLOCK* lockAwaited = NULL
static

Definition at line 85 of file proc.c.

◆ LockTimeout

int LockTimeout = 0

Definition at line 62 of file proc.c.

Referenced by ProcSleep().

◆ log_lock_waits

bool log_lock_waits = false

Definition at line 64 of file proc.c.

Referenced by ProcSleep().

◆ MyPgXact

◆ MyProc

PGPROC* MyProc = NULL

Definition at line 67 of file proc.c.

Referenced by AbortTransaction(), apw_sighup_handler(), apw_sigterm_handler(), AtEOSubXact_Namespace(), AtEOXact_Namespace(), attach_to_queues(), autoprewarm_main(), AutoVacWorkerMain(), AuxiliaryProcKill(), BackgroundWriterMain(), BecomeLockGroupLeader(), CheckpointerMain(), CommitTransaction(), ConditionVariableBroadcast(), ConditionVariableCancelSleep(), ConditionVariablePrepareToSleep(), ConditionVariableTimedSleep(), CountOtherDBBackends(), CreateReplicationSlot(), errdetail_abort(), exec_eval_simple_expr(), exec_stmt_call(), ExecParallelGetReceiver(), ExecParallelSetupTupleQueues(), ExportSnapshot(), FastPathGetRelationLockEntry(), FastPathGrantRelationLock(), FastPathUnGrantRelationLock(), FindLockCycleRecurseMember(), fmgr_sql(), get_cast_hashentry(), GetCurrentVirtualXIDs(), GetLockConflicts(), GetLockmodeName(), GetNewTransactionId(), GetSerializableTransactionSnapshotInt(), GetStableLatestTransactionId(), init_sql_fcache(), InitializeParallelDSM(), InitializeSessionUserId(), InitPostgres(), InitTempTableNamespace(), InitWalSenderSlot(), lock_and_open_sequence(), LockAcquireExtended(), LockCheckConflicts(), LockRelease(), LockReleaseAll(), log_line_prefix(), logicalrep_worker_attach(), LWLockAcquire(), LWLockAcquireOrWait(), LWLockAttemptLock(), LWLockDequeueSelf(), LWLockQueueSelf(), LWLockWaitForVar(), MinimumActiveBackends(), ParallelWorkerMain(), pgstat_report_activity(), pgstat_report_wait_end(), pgstat_report_wait_start(), PostPrepare_Locks(), PrepareTransaction(), ProcArrayGroupClearXid(), ProcKill(), PublishStartupProcessInformation(), RecoveryConflictInterrupt(), ReinitializeParallelDSM(), setup_dynamic_shared_memory(), SharedInvalBackendInit(), shm_mq_attach(), shm_mq_detach_internal(), shm_mq_receive(), shm_mq_sendv(), shm_mq_wait_for_attach(), StartTransaction(), SwitchBackToLocalLatch(), SwitchToSharedLatch(), SyncRepCancelWait(), SyncRepCleanupAtProcExit(), SyncRepQueueInsert(), SyncRepWaitForLSN(), TerminateOtherDBBackends(), TransactionGroupUpdateXidStatus(), TransactionIdIsInProgress(), TransactionIdSetPageStatus(), VirtualXactLockTableCleanup(), VirtualXactLockTableInsert(), WaitXLogInsertionsToFinish(), WALInsertLockAcquire(), WalReceiverMain(), WalWriterMain(), write_csvlog(), and XidCacheRemoveRunningXids().

◆ PreparedXactProcs

PGPROC* PreparedXactProcs = NULL

Definition at line 82 of file proc.c.

Referenced by PostmasterMarkPIDForWorkerNotify(), and TwoPhaseShmemInit().

◆ ProcGlobal

◆ ProcStructLock

◆ StatementTimeout

int StatementTimeout = 0

Definition at line 61 of file proc.c.

Referenced by enable_statement_timeout().