PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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/xlogutils.h"
#include "access/xlogwait.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/autovacuum.h"
#include "replication/slotsync.h"
#include "replication/syncrep.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 "utils/wait_event.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 DeadLockState CheckDeadLock (void)
 
static Size PGProcShmemSize (void)
 
static Size FastPathLockShmemSize (void)
 
Size ProcGlobalShmemSize (void)
 
int ProcGlobalSemas (void)
 
void InitProcGlobal (void)
 
void InitProcess (void)
 
void InitProcessPhase2 (void)
 
void InitAuxiliaryProcess (void)
 
void SetStartupBufferPinWaitBufId (int bufid)
 
int GetStartupBufferPinWaitBufId (void)
 
bool HaveNFreeProcs (int n, int *nfree)
 
void LockErrorCleanup (void)
 
void ProcReleaseLocks (bool isCommit)
 
PGPROCAuxiliaryPidGetProc (int pid)
 
ProcWaitStatus JoinWaitQueue (LOCALLOCK *locallock, LockMethod lockMethodTable, bool dontWait)
 
ProcWaitStatus ProcSleep (LOCALLOCK *locallock)
 
void ProcWakeup (PGPROC *proc, ProcWaitStatus waitStatus)
 
void ProcLockWakeup (LockMethod lockMethodTable, LOCK *lock)
 
void CheckDeadLockAlert (void)
 
void GetLockHoldersAndWaiters (LOCALLOCK *locallock, StringInfo lock_holders_sbuf, StringInfo lock_waiters_sbuf, int *lockHoldersNum)
 
void ProcWaitForSignal (uint32 wait_event_info)
 
void ProcSendSignal (ProcNumber procNumber)
 
void BecomeLockGroupLeader (void)
 
bool BecomeLockGroupMember (PGPROC *leader, int pid)
 

Variables

int DeadlockTimeout = 1000
 
int StatementTimeout = 0
 
int LockTimeout = 0
 
int IdleInTransactionSessionTimeout = 0
 
int TransactionTimeout = 0
 
int IdleSessionTimeout = 0
 
bool log_lock_waits = true
 
PGPROCMyProc = NULL
 
PROC_HDRProcGlobal = NULL
 
NON_EXEC_STATIC PGPROCAuxiliaryProcs = NULL
 
PGPROCPreparedXactProcs = NULL
 
static volatile sig_atomic_t got_deadlock_timeout
 

Function Documentation

◆ AuxiliaryPidGetProc()

PGPROC * AuxiliaryPidGetProc ( int  pid)

Definition at line 1085 of file proc.c.

1086{
1087 PGPROC *result = NULL;
1088 int index;
1089
1090 if (pid == 0) /* never match dummy PGPROCs */
1091 return NULL;
1092
1093 for (index = 0; index < NUM_AUXILIARY_PROCS; index++)
1094 {
1095 PGPROC *proc = &AuxiliaryProcs[index];
1096
1097 if (proc->pid == pid)
1098 {
1099 result = proc;
1100 break;
1101 }
1102 }
1103 return result;
1104}
static int fb(int x)
#define NUM_AUXILIARY_PROCS
Definition proc.h:524
NON_EXEC_STATIC PGPROC * AuxiliaryProcs
Definition proc.c:72
Definition proc.h:176
int pid
Definition proc.h:194
Definition type.h:96

References AuxiliaryProcs, fb(), NUM_AUXILIARY_PROCS, and PGPROC::pid.

Referenced by pg_log_backend_memory_contexts(), pg_stat_get_activity(), pg_stat_get_backend_wait_event(), pg_stat_get_backend_wait_event_type(), pg_stat_reset_backend_stats(), and pgstat_fetch_stat_backend_by_pid().

◆ AuxiliaryProcKill()

static void AuxiliaryProcKill ( int  code,
Datum  arg 
)
static

Definition at line 1034 of file proc.c.

1035{
1036 int proctype = DatumGetInt32(arg);
1038 PGPROC *proc;
1039
1041
1042 /* not safe if forked by system(), etc. */
1043 if (MyProc->pid != (int) getpid())
1044 elog(PANIC, "AuxiliaryProcKill() called in child process");
1045
1047
1048 Assert(MyProc == auxproc);
1049
1050 /* Release any LW locks I am holding (see notes above) */
1052
1053 /* Cancel any pending condition variable sleep, too */
1055
1056 /* look at the equivalent ProcKill() code for comments */
1059
1060 proc = MyProc;
1061 MyProc = NULL;
1063 DisownLatch(&proc->procLatch);
1064
1066
1067 /* Mark auxiliary proc no longer in use */
1068 proc->pid = 0;
1071
1072 /* Update shared estimate of spins_per_delay */
1074
1076}
#define PG_USED_FOR_ASSERTS_ONLY
Definition c.h:243
#define Assert(condition)
Definition c.h:945
bool ConditionVariableCancelSleep(void)
Datum arg
Definition elog.c:1322
#define PANIC
Definition elog.h:42
#define elog(elevel,...)
Definition elog.h:226
ProcNumber MyProcNumber
Definition globals.c:90
void DisownLatch(Latch *latch)
Definition latch.c:144
void LWLockReleaseAll(void)
Definition lwlock.c:1893
void SwitchBackToLocalLatch(void)
Definition miscinit.c:243
static int32 DatumGetInt32(Datum X)
Definition postgres.h:202
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
int update_spins_per_delay(int shared_spins_per_delay)
Definition s_lock.c:218
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56
PGPROC * MyProc
Definition proc.c:68
PROC_HDR * ProcGlobal
Definition proc.c:71
LocalTransactionId lxid
Definition proc.h:228
struct PGPROC::@133 vxid
ProcNumber procNumber
Definition proc.h:223
Latch procLatch
Definition proc.h:253
slock_t freeProcsLock
Definition proc.h:465
int spins_per_delay
Definition proc.h:489
#define InvalidTransactionId
Definition transam.h:31
void pgstat_reset_wait_event_storage(void)
Definition wait_event.c:361

References arg, Assert, AuxiliaryProcs, ConditionVariableCancelSleep(), DatumGetInt32(), DisownLatch(), elog, fb(), PROC_HDR::freeProcsLock, INVALID_PROC_NUMBER, InvalidTransactionId, LWLockReleaseAll(), PGPROC::lxid, MyProc, MyProcNumber, NUM_AUXILIARY_PROCS, PANIC, PG_USED_FOR_ASSERTS_ONLY, pgstat_reset_wait_event_storage(), PGPROC::pid, ProcGlobal, PGPROC::procLatch, PGPROC::procNumber, SpinLockAcquire(), SpinLockRelease(), PROC_HDR::spins_per_delay, SwitchBackToLocalLatch(), update_spins_per_delay(), and PGPROC::vxid.

Referenced by InitAuxiliaryProcess().

◆ BecomeLockGroupLeader()

void BecomeLockGroupLeader ( void  )

Definition at line 2018 of file proc.c.

2019{
2021
2022 /* If we already did it, we don't need to do it again. */
2024 return;
2025
2026 /* We had better not be a follower. */
2028
2029 /* Create single-member group, containing only ourselves. */
2035}
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition ilist.h:347
#define LockHashPartitionLockByProc(leader_pgproc)
Definition lock.h:543
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1177
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1794
@ LW_EXCLUSIVE
Definition lwlock.h:112
dlist_head lockGroupMembers
Definition proc.h:296
dlist_node lockGroupLink
Definition proc.h:297
PGPROC * lockGroupLeader
Definition proc.h:295

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

Referenced by LaunchParallelWorkers().

◆ BecomeLockGroupMember()

bool BecomeLockGroupMember ( PGPROC leader,
int  pid 
)

Definition at line 2048 of file proc.c.

2049{
2051 bool ok = false;
2052
2053 /* Group leader can't become member of group */
2054 Assert(MyProc != leader);
2055
2056 /* Can't already be a member of a group */
2058
2059 /* PID must be valid. */
2060 Assert(pid != 0);
2061
2062 /*
2063 * Get lock protecting the group fields. Note LockHashPartitionLockByProc
2064 * calculates the proc number based on the PGPROC slot without looking at
2065 * its contents, so we will acquire the correct lock even if the leader
2066 * PGPROC is in process of being recycled.
2067 */
2070
2071 /* Is this the leader we're looking for? */
2072 if (leader->pid == pid && leader->lockGroupLeader == leader)
2073 {
2074 /* OK, join the group */
2075 ok = true;
2076 MyProc->lockGroupLeader = leader;
2078 }
2080
2081 return ok;
2082}
static void dlist_push_tail(dlist_head *head, dlist_node *node)
Definition ilist.h:364

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

Referenced by ParallelWorkerMain().

◆ CheckDeadLock()

static DeadLockState CheckDeadLock ( void  )
static

Definition at line 1799 of file proc.c.

1800{
1801 int i;
1802 DeadLockState result;
1803
1804 /*
1805 * Acquire exclusive lock on the entire shared lock data structures. Must
1806 * grab LWLocks in partition-number order to avoid LWLock deadlock.
1807 *
1808 * Note that the deadlock check interrupt had better not be enabled
1809 * anywhere that this process itself holds lock partition locks, else this
1810 * will wait forever. Also note that LWLockAcquire creates a critical
1811 * section, so that this routine cannot be interrupted by cancel/die
1812 * interrupts.
1813 */
1814 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
1816
1817 /*
1818 * Check to see if we've been awoken by anyone in the interim.
1819 *
1820 * If we have, we can return and resume our transaction -- happy day.
1821 * Before we are awoken the process releasing the lock grants it to us so
1822 * we know that we don't have to wait anymore.
1823 *
1824 * We check by looking to see if we've been unlinked from the wait queue.
1825 * This is safe because we hold the lock partition lock.
1826 */
1828 {
1829 result = DS_NO_DEADLOCK;
1830 goto check_done;
1831 }
1832
1833#ifdef LOCK_DEBUG
1834 if (Debug_deadlocks)
1835 DumpAllLocks();
1836#endif
1837
1838 /* Run the deadlock check */
1839 result = DeadLockCheck(MyProc);
1840
1841 if (result == DS_HARD_DEADLOCK)
1842 {
1843 /*
1844 * Oops. We have a deadlock.
1845 *
1846 * Get this process out of wait state. (Note: we could do this more
1847 * efficiently by relying on lockAwaited, but use this coding to
1848 * preserve the flexibility to kill some other transaction than the
1849 * one detecting the deadlock.)
1850 *
1851 * RemoveFromWaitQueue sets MyProc->waitStatus to
1852 * PROC_WAIT_STATUS_ERROR, so ProcSleep will report an error after we
1853 * return.
1854 */
1857
1858 /*
1859 * We're done here. Transaction abort caused by the error that
1860 * ProcSleep will raise will cause any other locks we hold to be
1861 * released, thus allowing other processes to wake up; we don't need
1862 * to do that here. NOTE: an exception is that releasing locks we
1863 * hold doesn't consider the possibility of waiters that were blocked
1864 * behind us on the lock we just failed to get, and might now be
1865 * wakable because we're not in front of them anymore. However,
1866 * RemoveFromWaitQueue took care of waking up any such processes.
1867 */
1868 }
1869
1870 /*
1871 * And release locks. We do this in reverse order for two reasons: (1)
1872 * Anyone else who needs more than one of the locks will be trying to lock
1873 * them in increasing order; we don't want to release the other process
1874 * until it can get all the locks it needs. (2) This avoids O(N^2)
1875 * behavior inside LWLockRelease.
1876 */
1878 for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
1880
1881 return result;
1882}
DeadLockState DeadLockCheck(PGPROC *proc)
Definition deadlock.c:220
static bool dlist_node_is_detached(const dlist_node *node)
Definition ilist.h:525
int i
Definition isn.c:77
void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode)
Definition lock.c:2046
uint32 LockTagHashCode(const LOCKTAG *locktag)
Definition lock.c:557
DeadLockState
Definition lock.h:511
@ DS_HARD_DEADLOCK
Definition lock.h:515
@ DS_NO_DEADLOCK
Definition lock.h:513
#define LockHashPartitionLockByIndex(i)
Definition lock.h:531
#define NUM_LOCK_PARTITIONS
Definition lwlock.h:95
LOCKTAG tag
Definition lock.h:313
LOCK * waitLock
Definition proc.h:301
dlist_node waitLink
Definition proc.h:302

References Assert, DeadLockCheck(), dlist_node_is_detached(), DS_HARD_DEADLOCK, DS_NO_DEADLOCK, fb(), i, LockHashPartitionLockByIndex, LockTagHashCode(), LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyProc, NUM_LOCK_PARTITIONS, RemoveFromWaitQueue(), LOCK::tag, PGPROC::waitLink, and PGPROC::waitLock.

Referenced by ProcSleep().

◆ CheckDeadLockAlert()

void CheckDeadLockAlert ( void  )

Definition at line 1890 of file proc.c.

1891{
1892 int save_errno = errno;
1893
1894 got_deadlock_timeout = true;
1895
1896 /*
1897 * Have to set the latch again, even if handle_sig_alarm already did. Back
1898 * then got_deadlock_timeout wasn't yet set... It's unlikely that this
1899 * ever would be a problem, but setting a set latch again is cheap.
1900 *
1901 * Note that, when this function runs inside procsignal_sigusr1_handler(),
1902 * the handler function sets the latch again after the latch is set here.
1903 */
1905 errno = save_errno;
1906}
struct Latch * MyLatch
Definition globals.c:63
void SetLatch(Latch *latch)
Definition latch.c:290
static volatile sig_atomic_t got_deadlock_timeout
Definition proc.c:76

References fb(), got_deadlock_timeout, MyLatch, and SetLatch().

Referenced by InitPostgres(), and ProcessRecoveryConflictInterrupt().

◆ FastPathLockShmemSize()

static Size FastPathLockShmemSize ( void  )
static

Definition at line 106 of file proc.c.

107{
108 Size size = 0;
113
114 /*
115 * Memory needed for PGPROC fast-path lock arrays. Make sure the sizes are
116 * nicely aligned in each backend.
117 */
120
122
123 return size;
124}
#define MAXALIGN(LEN)
Definition c.h:898
uint64_t uint64
Definition c.h:619
size_t Size
Definition c.h:691
int MaxBackends
Definition globals.c:146
int FastPathLockGroupsPerBackend
Definition lock.c:202
unsigned int Oid
#define FastPathLockSlotsPerBackend()
Definition proc.h:94
Size add_size(Size s1, Size s2)
Definition shmem.c:485
Size mul_size(Size s1, Size s2)
Definition shmem.c:500
int max_prepared_xacts
Definition twophase.c:117

References add_size(), FastPathLockGroupsPerBackend, FastPathLockSlotsPerBackend, fb(), max_prepared_xacts, MAXALIGN, MaxBackends, mul_size(), and NUM_AUXILIARY_PROCS.

Referenced by InitProcGlobal(), and ProcGlobalShmemSize().

◆ GetLockHoldersAndWaiters()

void GetLockHoldersAndWaiters ( LOCALLOCK locallock,
StringInfo  lock_holders_sbuf,
StringInfo  lock_waiters_sbuf,
int lockHoldersNum 
)

Definition at line 1917 of file proc.c.

1919{
1922 LOCK *lock = locallock->lock;
1923 bool first_holder = true,
1924 first_waiter = true;
1925
1926#ifdef USE_ASSERT_CHECKING
1927 {
1928 uint32 hashcode = locallock->hashcode;
1930
1932 }
1933#endif
1934
1935 *lockHoldersNum = 0;
1936
1937 /*
1938 * Loop over the lock's procLocks to gather a list of all holders and
1939 * waiters. Thus we will be able to provide more detailed information for
1940 * lock debugging purposes.
1941 *
1942 * lock->procLocks contains all processes which hold or wait for this
1943 * lock.
1944 */
1946 {
1947 curproclock =
1948 dlist_container(PROCLOCK, lockLink, proc_iter.cur);
1949
1950 /*
1951 * We are a waiter if myProc->waitProcLock == curproclock; we are a
1952 * holder if it is NULL or something different.
1953 */
1954 if (curproclock->tag.myProc->waitProcLock == curproclock)
1955 {
1956 if (first_waiter)
1957 {
1959 curproclock->tag.myProc->pid);
1960 first_waiter = false;
1961 }
1962 else
1964 curproclock->tag.myProc->pid);
1965 }
1966 else
1967 {
1968 if (first_holder)
1969 {
1971 curproclock->tag.myProc->pid);
1972 first_holder = false;
1973 }
1974 else
1976 curproclock->tag.myProc->pid);
1977
1978 (*lockHoldersNum)++;
1979 }
1980 }
1981}
uint32_t uint32
Definition c.h:618
#define dlist_foreach(iter, lhead)
Definition ilist.h:623
#define dlist_container(type, membername, ptr)
Definition ilist.h:593
#define LockHashPartitionLock(hashcode)
Definition lock.h:528
bool LWLockHeldByMe(LWLock *lock)
Definition lwlock.c:1912
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
Definition lock.h:311
dlist_head procLocks
Definition lock.h:318

References appendStringInfo(), Assert, dlist_container, dlist_foreach, fb(), LockHashPartitionLock, LWLockHeldByMe(), and LOCK::procLocks.

Referenced by LockAcquireExtended(), and ProcSleep().

◆ GetStartupBufferPinWaitBufId()

int GetStartupBufferPinWaitBufId ( void  )

Definition at line 759 of file proc.c.

760{
761 /* use volatile pointer to prevent code rearrangement */
762 volatile PROC_HDR *procglobal = ProcGlobal;
763
765}
int startupBufferPinWaitBufId
Definition proc.h:491

References fb(), ProcGlobal, and PROC_HDR::startupBufferPinWaitBufId.

Referenced by HoldingBufferPinThatDelaysRecovery().

◆ HaveNFreeProcs()

bool HaveNFreeProcs ( int  n,
int nfree 
)

Definition at line 775 of file proc.c.

776{
777 dlist_iter iter;
778
779 Assert(n > 0);
780 Assert(nfree);
781
783
784 *nfree = 0;
786 {
787 (*nfree)++;
788 if (*nfree == n)
789 break;
790 }
791
793
794 return (*nfree == n);
795}
dlist_head freeProcs
Definition proc.h:468

References Assert, dlist_foreach, PROC_HDR::freeProcs, PROC_HDR::freeProcsLock, ProcGlobal, SpinLockAcquire(), and SpinLockRelease().

Referenced by InitPostgres().

◆ InitAuxiliaryProcess()

void InitAuxiliaryProcess ( void  )

Definition at line 606 of file proc.c.

607{
609 int proctype;
610
611 /*
612 * ProcGlobal should be set up already (if we are a backend, we inherit
613 * this by fork() or EXEC_BACKEND mechanism from the postmaster).
614 */
615 if (ProcGlobal == NULL || AuxiliaryProcs == NULL)
616 elog(PANIC, "proc header uninitialized");
617
618 if (MyProc != NULL)
619 elog(ERROR, "you already exist");
620
623
624 /*
625 * We use the freeProcsLock to protect assignment and releasing of
626 * AuxiliaryProcs entries.
627 *
628 * While we are holding the spinlock, also copy the current shared
629 * estimate of spins_per_delay to local storage.
630 */
632
634
635 /*
636 * Find a free auxproc ... *big* trouble if there isn't one ...
637 */
639 {
641 if (auxproc->pid == 0)
642 break;
643 }
645 {
647 elog(FATAL, "all AuxiliaryProcs are in use");
648 }
649
650 /* Mark auxiliary proc as in use by me */
651 /* use volatile pointer to prevent code rearrangement */
652 ((volatile PGPROC *) auxproc)->pid = MyProcPid;
653
655
656 MyProc = auxproc;
658
659 /*
660 * Initialize all fields of MyProc, except for those previously
661 * initialized by InitProcGlobal.
662 */
665 MyProc->fpVXIDLock = false;
676 MyProc->statusFlags = 0;
678 MyProc->lwWaitMode = 0;
683#ifdef USE_ASSERT_CHECKING
684 {
685 int i;
686
687 /* Last process should have released all locks. */
688 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
690 }
691#endif
693
694 /*
695 * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
696 * on it. That allows us to repoint the process latch, which so far
697 * points to process local one, to the shared one.
698 */
701
702 /* now that we have a proc, report wait events to shared memory */
704
705 /* Check that group locking fields are in a proper initial state. */
708
709 /*
710 * We might be reusing a semaphore that belonged to a failed process. So
711 * be careful and reinitialize its value here. (This is not strictly
712 * necessary anymore, but seems like a good idea for cleanliness.)
713 */
715
716 /*
717 * Arrange to clean up at process exit.
718 */
720
721 /*
722 * Now that we have a PGPROC, we could try to acquire lightweight locks.
723 * Initialize local state needed for them. (Heavyweight locks cannot be
724 * acquired in aux processes.)
725 */
727
728#ifdef EXEC_BACKEND
729
730 /*
731 * Initialize backend-local pointers to all the shared data structures.
732 * (We couldn't do this until now because it needs LWLocks.)
733 */
736#endif
737}
static void pg_atomic_write_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition atomics.h:485
static void pg_atomic_write_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:274
#define FATAL
Definition elog.h:41
#define ERROR
Definition elog.h:39
int MyProcPid
Definition globals.c:47
bool IsUnderPostmaster
Definition globals.c:120
static bool dlist_is_empty(const dlist_head *head)
Definition ilist.h:336
static void dlist_node_init(dlist_node *node)
Definition ilist.h:325
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:372
void OwnLatch(Latch *latch)
Definition latch.c:126
#define InvalidLocalTransactionId
Definition lock.h:67
void InitLWLockAccess(void)
Definition lwlock.c:551
@ LW_WS_NOT_WAITING
Definition lwlock.h:30
void SwitchToSharedLatch(void)
Definition miscinit.c:216
BackendType MyBackendType
Definition miscinit.c:65
void RegisterPostmasterChildActive(void)
Definition pmsignal.c:290
void PGSemaphoreReset(PGSemaphore sema)
Definition posix_sema.c:290
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
#define InvalidOid
#define GetNumberFromPGProc(proc)
Definition proc.h:502
@ PROC_WAIT_STATUS_OK
Definition proc.h:142
void set_spins_per_delay(int shared_spins_per_delay)
Definition s_lock.c:207
static void AuxiliaryProcKill(int code, Datum arg)
Definition proc.c:1034
TransactionId xmin
Definition proc.h:239
PROCLOCK * waitProcLock
Definition proc.h:303
dlist_node freeProcsLink
Definition proc.h:183
uint8 lwWaitMode
Definition proc.h:281
uint32 wait_event_info
Definition proc.h:375
BackendType backendType
Definition proc.h:195
uint8 statusFlags
Definition proc.h:207
Oid databaseId
Definition proc.h:198
pg_atomic_uint64 waitStart
Definition proc.h:308
bool fpVXIDLock
Definition proc.h:324
TransactionId xid
Definition proc.h:234
int delayChkptFlags
Definition proc.h:257
pg_atomic_uint32 pendingRecoveryConflicts
Definition proc.h:267
LocalTransactionId fpLocalTransactionId
Definition proc.h:325
PGSemaphore sem
Definition proc.h:255
dlist_head myProcLocks[NUM_LOCK_PARTITIONS]
Definition proc.h:318
Oid roleId
Definition proc.h:199
ProcWaitStatus waitStatus
Definition proc.h:311
Oid tempNamespaceId
Definition proc.h:201
uint8 lwWaiting
Definition proc.h:280
void pgstat_set_wait_event_storage(uint32 *wait_event_info)
Definition wait_event.c:349

References Assert, AuxiliaryProcKill(), AuxiliaryProcs, PGPROC::backendType, PGPROC::databaseId, PGPROC::delayChkptFlags, dlist_is_empty(), dlist_node_init(), elog, ERROR, FATAL, fb(), PGPROC::fpLocalTransactionId, PGPROC::fpVXIDLock, PGPROC::freeProcsLink, PROC_HDR::freeProcsLock, GetNumberFromPGProc, i, InitLWLockAccess(), Int32GetDatum(), INVALID_PROC_NUMBER, InvalidLocalTransactionId, InvalidOid, InvalidTransactionId, IsUnderPostmaster, PGPROC::lockGroupLeader, PGPROC::lockGroupMembers, LW_WS_NOT_WAITING, PGPROC::lwWaiting, PGPROC::lwWaitMode, PGPROC::lxid, MyBackendType, MyProc, PGPROC::myProcLocks, MyProcNumber, MyProcPid, NUM_AUXILIARY_PROCS, NUM_LOCK_PARTITIONS, on_shmem_exit(), OwnLatch(), PANIC, PGPROC::pendingRecoveryConflicts, pg_atomic_write_u32(), pg_atomic_write_u64(), PGSemaphoreReset(), pgstat_set_wait_event_storage(), PROC_WAIT_STATUS_OK, ProcGlobal, PGPROC::procLatch, PGPROC::procNumber, RegisterPostmasterChildActive(), PGPROC::roleId, PGPROC::sem, set_spins_per_delay(), SpinLockAcquire(), SpinLockRelease(), PROC_HDR::spins_per_delay, PGPROC::statusFlags, SwitchToSharedLatch(), PGPROC::tempNamespaceId, PGPROC::vxid, PGPROC::wait_event_info, PGPROC::waitLink, PGPROC::waitLock, PGPROC::waitProcLock, PGPROC::waitStart, PGPROC::waitStatus, PGPROC::xid, and PGPROC::xmin.

Referenced by AuxiliaryProcessMainCommon().

◆ InitProcess()

void InitProcess ( void  )

Definition at line 380 of file proc.c.

381{
382 dlist_head *procgloballist;
383
384 /*
385 * ProcGlobal should be set up already (if we are a backend, we inherit
386 * this by fork() or EXEC_BACKEND mechanism from the postmaster).
387 */
388 if (ProcGlobal == NULL)
389 elog(PANIC, "proc header uninitialized");
390
391 if (MyProc != NULL)
392 elog(ERROR, "you already exist");
393
394 /*
395 * Before we start accessing the shared memory in a serious way, mark
396 * ourselves as an active postmaster child; this is so that the postmaster
397 * can detect it if we exit without cleaning up.
398 */
401
402 /*
403 * Decide which list should supply our PGPROC. This logic must match the
404 * way the freelists were constructed in InitProcGlobal().
405 */
407 procgloballist = &ProcGlobal->autovacFreeProcs;
408 else if (AmBackgroundWorkerProcess())
409 procgloballist = &ProcGlobal->bgworkerFreeProcs;
410 else if (AmWalSenderProcess())
411 procgloballist = &ProcGlobal->walsenderFreeProcs;
412 else
413 procgloballist = &ProcGlobal->freeProcs;
414
415 /*
416 * Try to get a proc struct from the appropriate free list. If this
417 * fails, we must be out of PGPROC structures (not to mention semaphores).
418 *
419 * While we are holding the spinlock, also copy the current shared
420 * estimate of spins_per_delay to local storage.
421 */
423
425
426 if (!dlist_is_empty(procgloballist))
427 {
428 MyProc = dlist_container(PGPROC, freeProcsLink, dlist_pop_head_node(procgloballist));
430 }
431 else
432 {
433 /*
434 * If we reach here, all the PGPROCs are in use. This is one of the
435 * possible places to detect "too many backends", so give the standard
436 * error message. XXX do we need to give a different failure message
437 * in the autovacuum case?
438 */
440 if (AmWalSenderProcess())
443 errmsg("number of requested standby connections exceeds \"max_wal_senders\" (currently %d)",
447 errmsg("sorry, too many clients already")));
448 }
450
451 /*
452 * Cross-check that the PGPROC is of the type we expect; if this were not
453 * the case, it would get returned to the wrong list.
454 */
455 Assert(MyProc->procgloballist == procgloballist);
456
457 /*
458 * Initialize all fields of MyProc, except for those previously
459 * initialized by InitProcGlobal.
460 */
463 MyProc->fpVXIDLock = false;
470 /* databaseId and roleId will be filled in later */
476 MyProc->statusFlags = 0;
477 /* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */
481 MyProc->lwWaitMode = 0;
486#ifdef USE_ASSERT_CHECKING
487 {
488 int i;
489
490 /* Last process should have released all locks. */
491 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
493 }
494#endif
496
497 /* Initialize fields for sync rep */
501
502 /* Initialize fields for group XID clearing. */
506
507 /* Check that group locking fields are in a proper initial state. */
510
511 /* Initialize wait event information. */
513
514 /* Initialize fields for group transaction status update. */
515 MyProc->clogGroupMember = false;
521
522 /*
523 * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
524 * on it. That allows us to repoint the process latch, which so far
525 * points to process local one, to the shared one.
526 */
529
530 /* now that we have a proc, report wait events to shared memory */
532
533 /*
534 * We might be reusing a semaphore that belonged to a failed process. So
535 * be careful and reinitialize its value here. (This is not strictly
536 * necessary anymore, but seems like a good idea for cleanliness.)
537 */
539
540 /*
541 * Arrange to clean up at backend exit.
542 */
544
545 /*
546 * Now that we have a PGPROC, we could try to acquire locks, so initialize
547 * local state needed for LWLocks, and the deadlock checker.
548 */
551
552#ifdef EXEC_BACKEND
553
554 /*
555 * Initialize backend-local pointers to all the shared data structures.
556 * (We couldn't do this until now because it needs LWLocks.)
557 */
560#endif
561}
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition atomics.h:237
#define TRANSACTION_STATUS_IN_PROGRESS
Definition clog.h:27
void InitDeadLockChecking(void)
Definition deadlock.c:143
int errcode(int sqlerrcode)
Definition elog.c:874
#define ereport(elevel,...)
Definition elog.h:150
static dlist_node * dlist_pop_head_node(dlist_head *head)
Definition ilist.h:450
#define AmAutoVacuumWorkerProcess()
Definition miscadmin.h:383
#define AmBackgroundWorkerProcess()
Definition miscadmin.h:384
#define AmWalSenderProcess()
Definition miscadmin.h:385
#define AmSpecialWorkerProcess()
Definition miscadmin.h:396
static char * errmsg
#define PROC_IS_AUTOVACUUM
Definition proc.h:58
static void ProcKill(int code, Datum arg)
Definition proc.c:912
bool procArrayGroupMember
Definition proc.h:347
XLogRecPtr clogGroupMemberLsn
Definition proc.h:368
pg_atomic_uint32 procArrayGroupNext
Definition proc.h:349
dlist_head * procgloballist
Definition proc.h:182
TransactionId clogGroupMemberXid
Definition proc.h:363
int64 clogGroupMemberPage
Definition proc.h:366
bool clogGroupMember
Definition proc.h:361
XLogRecPtr waitLSN
Definition proc.h:338
dlist_node syncRepLinks
Definition proc.h:340
int syncRepState
Definition proc.h:339
pg_atomic_uint32 clogGroupNext
Definition proc.h:362
XidStatus clogGroupMemberXidStatus
Definition proc.h:364
TransactionId procArrayGroupMemberXid
Definition proc.h:355
dlist_head autovacFreeProcs
Definition proc.h:470
dlist_head walsenderFreeProcs
Definition proc.h:474
dlist_head bgworkerFreeProcs
Definition proc.h:472
#define SYNC_REP_NOT_WAITING
Definition syncrep.h:30
int max_wal_senders
Definition walsender.c:130
#define InvalidXLogRecPtr
Definition xlogdefs.h:28

References AmAutoVacuumWorkerProcess, AmBackgroundWorkerProcess, AmSpecialWorkerProcess, AmWalSenderProcess, Assert, PROC_HDR::autovacFreeProcs, PGPROC::backendType, PROC_HDR::bgworkerFreeProcs, PGPROC::clogGroupMember, PGPROC::clogGroupMemberLsn, PGPROC::clogGroupMemberPage, PGPROC::clogGroupMemberXid, PGPROC::clogGroupMemberXidStatus, PGPROC::clogGroupNext, PGPROC::databaseId, PGPROC::delayChkptFlags, dlist_container, dlist_is_empty(), dlist_node_init(), dlist_pop_head_node(), elog, ereport, errcode(), errmsg, ERROR, FATAL, fb(), PGPROC::fpLocalTransactionId, PGPROC::fpVXIDLock, PROC_HDR::freeProcs, PGPROC::freeProcsLink, PROC_HDR::freeProcsLock, GetNumberFromPGProc, i, InitDeadLockChecking(), InitLWLockAccess(), INVALID_PROC_NUMBER, InvalidLocalTransactionId, InvalidOid, InvalidTransactionId, InvalidXLogRecPtr, IsUnderPostmaster, PGPROC::lockGroupLeader, PGPROC::lockGroupMembers, LW_WS_NOT_WAITING, PGPROC::lwWaiting, PGPROC::lwWaitMode, PGPROC::lxid, max_wal_senders, MyBackendType, MyProc, PGPROC::myProcLocks, MyProcNumber, MyProcPid, NUM_LOCK_PARTITIONS, on_shmem_exit(), OwnLatch(), PANIC, PGPROC::pendingRecoveryConflicts, pg_atomic_read_u32(), pg_atomic_write_u32(), pg_atomic_write_u64(), PGSemaphoreReset(), pgstat_set_wait_event_storage(), PGPROC::pid, PROC_IS_AUTOVACUUM, PROC_WAIT_STATUS_OK, PGPROC::procArrayGroupMember, PGPROC::procArrayGroupMemberXid, PGPROC::procArrayGroupNext, ProcGlobal, PGPROC::procgloballist, ProcKill(), PGPROC::procLatch, PGPROC::procNumber, RegisterPostmasterChildActive(), PGPROC::roleId, PGPROC::sem, set_spins_per_delay(), SpinLockAcquire(), SpinLockRelease(), PROC_HDR::spins_per_delay, PGPROC::statusFlags, SwitchToSharedLatch(), SYNC_REP_NOT_WAITING, PGPROC::syncRepLinks, PGPROC::syncRepState, PGPROC::tempNamespaceId, TRANSACTION_STATUS_IN_PROGRESS, PGPROC::vxid, PGPROC::wait_event_info, PGPROC::waitLink, PGPROC::waitLock, PGPROC::waitLSN, PGPROC::waitProcLock, PGPROC::waitStart, PGPROC::waitStatus, PROC_HDR::walsenderFreeProcs, PGPROC::xid, and PGPROC::xmin.

Referenced by AutoVacLauncherMain(), AutoVacWorkerMain(), BackendMain(), BackgroundWorkerMain(), BootstrapModeMain(), PostgresSingleUserMain(), and ReplSlotSyncWorkerMain().

◆ InitProcessPhase2()

void InitProcessPhase2 ( void  )

Definition at line 571 of file proc.c.

572{
573 Assert(MyProc != NULL);
574
575 /*
576 * Add our PGPROC to the PGPROC array in shared memory.
577 */
579
580 /*
581 * Arrange to clean that up at backend exit.
582 */
584}
void ProcArrayAdd(PGPROC *proc)
Definition procarray.c:472
static void RemoveProcFromArray(int code, Datum arg)
Definition proc.c:901

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

Referenced by InitPostgres().

◆ InitProcGlobal()

void InitProcGlobal ( void  )

Definition at line 184 of file proc.c.

185{
186 PGPROC *procs;
187 int i,
188 j;
189 bool found;
191
192 /* Used for setup of per-backend fast-path slots. */
193 char *fpPtr,
198 char *ptr;
199
200 /* Create the ProcGlobal shared structure */
201 ProcGlobal = (PROC_HDR *)
202 ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
203 Assert(!found);
204
205 /*
206 * Initialize the data structures.
207 */
219
220 /*
221 * Create and initialize all the PGPROC structures we'll need. There are
222 * six separate consumers: (1) normal backends, (2) autovacuum workers and
223 * special workers, (3) background workers, (4) walsenders, (5) auxiliary
224 * processes, and (6) prepared transactions. (For largely-historical
225 * reasons, we combine autovacuum and special workers into one category
226 * with a single freelist.) Each PGPROC structure is dedicated to exactly
227 * one of these purposes, and they do not move between groups.
228 */
230
231 ptr = ShmemInitStruct("PGPROC structures",
233 &found);
234
235 MemSet(ptr, 0, requestSize);
236
237 procs = (PGPROC *) ptr;
238 ptr = ptr + TotalProcs * sizeof(PGPROC);
239
240 ProcGlobal->allProcs = procs;
241 /* XXX allProcCount isn't really all of them; it excludes prepared xacts */
243
244 /*
245 * Allocate arrays mirroring PGPROC fields in a dense manner. See
246 * PROC_HDR.
247 *
248 * XXX: It might make sense to increase padding for these arrays, given
249 * how hotly they are accessed.
250 */
251 ProcGlobal->xids = (TransactionId *) ptr;
252 ptr = ptr + (TotalProcs * sizeof(*ProcGlobal->xids));
253
255 ptr = ptr + (TotalProcs * sizeof(*ProcGlobal->subxidStates));
256
257 ProcGlobal->statusFlags = (uint8 *) ptr;
258 ptr = ptr + (TotalProcs * sizeof(*ProcGlobal->statusFlags));
259
260 /* make sure wer didn't overflow */
261 Assert((ptr > (char *) procs) && (ptr <= (char *) procs + requestSize));
262
263 /*
264 * Allocate arrays for fast-path locks. Those are variable-length, so
265 * can't be included in PGPROC directly. We allocate a separate piece of
266 * shared memory and then divide that between backends.
267 */
270
272
273 fpPtr = ShmemInitStruct("Fast-Path Lock Array",
275 &found);
276
278
279 /* For asserts checking we did not overflow. */
281
282 /* Reserve space for semaphores. */
284
285 for (i = 0; i < TotalProcs; i++)
286 {
287 PGPROC *proc = &procs[i];
288
289 /* Common initialization for all PGPROCs, regardless of type. */
290
291 /*
292 * Set the fast-path lock arrays, and move the pointer. We interleave
293 * the two arrays, to (hopefully) get some locality for each backend.
294 */
295 proc->fpLockBits = (uint64 *) fpPtr;
297
298 proc->fpRelId = (Oid *) fpPtr;
300
302
303 /*
304 * Set up per-PGPROC semaphore, latch, and fpInfoLock. Prepared xact
305 * dummy PGPROCs don't need these though - they're never associated
306 * with a real process
307 */
309 {
310 proc->sem = PGSemaphoreCreate();
311 InitSharedLatch(&(proc->procLatch));
313 }
314
315 /*
316 * Newly created PGPROCs for normal backends, autovacuum workers,
317 * special workers, bgworkers, and walsenders must be queued up on the
318 * appropriate free list. Because there can only ever be a small,
319 * fixed number of auxiliary processes, no free list is used in that
320 * case; InitAuxiliaryProcess() instead uses a linear search. PGPROCs
321 * for prepared transactions are added to a free list by
322 * TwoPhaseShmemInit().
323 */
324 if (i < MaxConnections)
325 {
326 /* PGPROC for normal backend, add to freeProcs list */
329 }
331 {
332 /* PGPROC for AV or special worker, add to autovacFreeProcs list */
335 }
337 {
338 /* PGPROC for bgworker, add to bgworkerFreeProcs list */
341 }
342 else if (i < MaxBackends)
343 {
344 /* PGPROC for walsender, add to walsenderFreeProcs list */
347 }
348
349 /* Initialize myProcLocks[] shared memory queues. */
350 for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
351 dlist_init(&(proc->myProcLocks[j]));
352
353 /* Initialize lockGroupMembers list. */
355
356 /*
357 * Initialize the atomic variables, otherwise, it won't be safe to
358 * access them for backends that aren't currently in use.
359 */
362 pg_atomic_init_u64(&(proc->waitStart), 0);
363 }
364
365 /* Should have consumed exactly the expected amount of fast-path memory. */
367
368 /*
369 * Save pointers to the blocks of PGPROC structures reserved for auxiliary
370 * processes and prepared transactions.
371 */
372 AuxiliaryProcs = &procs[MaxBackends];
374}
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:219
static void pg_atomic_init_u64(volatile pg_atomic_uint64 *ptr, uint64 val)
Definition atomics.h:453
int autovacuum_worker_slots
Definition autovacuum.c:120
uint8_t uint8
Definition c.h:616
#define MemSet(start, val, len)
Definition c.h:1109
uint32 TransactionId
Definition c.h:738
int MaxConnections
Definition globals.c:143
int max_worker_processes
Definition globals.c:144
static void dlist_init(dlist_head *head)
Definition ilist.h:314
int j
Definition isn.c:78
void InitSharedLatch(Latch *latch)
Definition latch.c:93
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition lwlock.c:699
void PGReserveSemaphores(int maxSemas)
Definition posix_sema.c:196
PGSemaphore PGSemaphoreCreate(void)
Definition posix_sema.c:257
#define FIRST_PREPARED_XACT_PROC_NUMBER
Definition proc.h:526
#define NUM_SPECIAL_WORKER_PROCS
Definition proc.h:511
#define DEFAULT_SPINS_PER_DELAY
Definition s_lock.h:718
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition shmem.c:381
static void SpinLockInit(volatile slock_t *lock)
Definition spin.h:50
PGPROC * PreparedXactProcs
Definition proc.c:73
static Size PGProcShmemSize(void)
Definition proc.c:88
static Size FastPathLockShmemSize(void)
Definition proc.c:106
int ProcGlobalSemas(void)
Definition proc.c:149
LWLock fpInfoLock
Definition proc.h:321
Oid * fpRelId
Definition proc.h:323
uint64 * fpLockBits
Definition proc.h:322
uint8 * statusFlags
Definition proc.h:453
XidCacheStatus * subxidStates
Definition proc.h:447
ProcNumber checkpointerProc
Definition proc.h:486
PGPROC * allProcs
Definition proc.h:438
pg_atomic_uint32 clogGroupFirst
Definition proc.h:479
TransactionId * xids
Definition proc.h:441
ProcNumber walwriterProc
Definition proc.h:485
pg_atomic_uint32 procArrayGroupFirst
Definition proc.h:477
uint32 allProcCount
Definition proc.h:456

References PROC_HDR::allProcCount, PROC_HDR::allProcs, Assert, PROC_HDR::autovacFreeProcs, autovacuum_worker_slots, AuxiliaryProcs, PROC_HDR::bgworkerFreeProcs, PROC_HDR::checkpointerProc, PROC_HDR::clogGroupFirst, PGPROC::clogGroupNext, DEFAULT_SPINS_PER_DELAY, dlist_init(), dlist_push_tail(), FastPathLockGroupsPerBackend, FastPathLockShmemSize(), FastPathLockSlotsPerBackend, fb(), FIRST_PREPARED_XACT_PROC_NUMBER, PGPROC::fpInfoLock, PGPROC::fpLockBits, PGPROC::fpRelId, PROC_HDR::freeProcs, PGPROC::freeProcsLink, PROC_HDR::freeProcsLock, i, InitSharedLatch(), INVALID_PROC_NUMBER, j, PGPROC::lockGroupMembers, LWLockInitialize(), max_prepared_xacts, max_worker_processes, MAXALIGN, MaxBackends, MaxConnections, MemSet, PGPROC::myProcLocks, NUM_AUXILIARY_PROCS, NUM_LOCK_PARTITIONS, NUM_SPECIAL_WORKER_PROCS, pg_atomic_init_u32(), pg_atomic_init_u64(), PG_USED_FOR_ASSERTS_ONLY, PGProcShmemSize(), PGReserveSemaphores(), PGSemaphoreCreate(), PreparedXactProcs, PROC_HDR::procArrayGroupFirst, PGPROC::procArrayGroupNext, ProcGlobal, PGPROC::procgloballist, ProcGlobalSemas(), PGPROC::procLatch, PGPROC::sem, ShmemInitStruct(), SpinLockInit(), PROC_HDR::spins_per_delay, PROC_HDR::startupBufferPinWaitBufId, PROC_HDR::statusFlags, PROC_HDR::subxidStates, PGPROC::waitStart, PROC_HDR::walsenderFreeProcs, PROC_HDR::walwriterProc, and PROC_HDR::xids.

Referenced by CreateOrAttachShmemStructs().

◆ JoinWaitQueue()

ProcWaitStatus JoinWaitQueue ( LOCALLOCK locallock,
LockMethod  lockMethodTable,
bool  dontWait 
)

Definition at line 1134 of file proc.c.

1135{
1136 LOCKMODE lockmode = locallock->tag.mode;
1137 LOCK *lock = locallock->lock;
1138 PROCLOCK *proclock = locallock->proclock;
1139 uint32 hashcode = locallock->hashcode;
1145 bool early_deadlock = false;
1146 PGPROC *leader = MyProc->lockGroupLeader;
1147
1149
1150 /*
1151 * Set bitmask of locks this process already holds on this object.
1152 */
1153 myHeldLocks = MyProc->heldLocks = proclock->holdMask;
1154
1155 /*
1156 * Determine which locks we're already holding.
1157 *
1158 * If group locking is in use, locks held by members of my locking group
1159 * need to be included in myHeldLocks. This is not required for relation
1160 * extension lock which conflict among group members. However, including
1161 * them in myHeldLocks will give group members the priority to get those
1162 * locks as compared to other backends which are also trying to acquire
1163 * those locks. OTOH, we can avoid giving priority to group members for
1164 * that kind of locks, but there doesn't appear to be a clear advantage of
1165 * the same.
1166 */
1167 myProcHeldLocks = proclock->holdMask;
1169 if (leader != NULL)
1170 {
1171 dlist_iter iter;
1172
1173 dlist_foreach(iter, &lock->procLocks)
1174 {
1176
1177 otherproclock = dlist_container(PROCLOCK, lockLink, iter.cur);
1178
1179 if (otherproclock->groupLeader == leader)
1180 myHeldLocks |= otherproclock->holdMask;
1181 }
1182 }
1183
1184 /*
1185 * Determine where to add myself in the wait queue.
1186 *
1187 * Normally I should go at the end of the queue. However, if I already
1188 * hold locks that conflict with the request of any previous waiter, put
1189 * myself in the queue just in front of the first such waiter. This is not
1190 * a necessary step, since deadlock detection would move me to before that
1191 * waiter anyway; but it's relatively cheap to detect such a conflict
1192 * immediately, and avoid delaying till deadlock timeout.
1193 *
1194 * Special case: if I find I should go in front of some waiter, check to
1195 * see if I conflict with already-held locks or the requests before that
1196 * waiter. If not, then just grant myself the requested lock immediately.
1197 * This is the same as the test for immediate grant in LockAcquire, except
1198 * we are only considering the part of the wait queue before my insertion
1199 * point.
1200 */
1202 {
1204 dlist_iter iter;
1205
1207 {
1208 PGPROC *proc = dlist_container(PGPROC, waitLink, iter.cur);
1209
1210 /*
1211 * If we're part of the same locking group as this waiter, its
1212 * locks neither conflict with ours nor contribute to
1213 * aheadRequests.
1214 */
1215 if (leader != NULL && leader == proc->lockGroupLeader)
1216 continue;
1217
1218 /* Must he wait for me? */
1219 if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
1220 {
1221 /* Must I wait for him ? */
1222 if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
1223 {
1224 /*
1225 * Yes, so we have a deadlock. Easiest way to clean up
1226 * correctly is to call RemoveFromWaitQueue(), but we
1227 * can't do that until we are *on* the wait queue. So, set
1228 * a flag to check below, and break out of loop. Also,
1229 * record deadlock info for later message.
1230 */
1231 RememberSimpleDeadLock(MyProc, lockmode, lock, proc);
1232 early_deadlock = true;
1233 break;
1234 }
1235 /* I must go before this waiter. Check special case. */
1236 if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1237 !LockCheckConflicts(lockMethodTable, lockmode, lock,
1238 proclock))
1239 {
1240 /* Skip the wait and just grant myself the lock. */
1241 GrantLock(lock, proclock, lockmode);
1242 return PROC_WAIT_STATUS_OK;
1243 }
1244
1245 /* Put myself into wait queue before conflicting process */
1246 insert_before = proc;
1247 break;
1248 }
1249 /* Nope, so advance to next waiter */
1251 }
1252 }
1253
1254 /*
1255 * If we detected deadlock, give up without waiting. This must agree with
1256 * CheckDeadLock's recovery code.
1257 */
1258 if (early_deadlock)
1260
1261 /*
1262 * At this point we know that we'd really need to sleep. If we've been
1263 * commanded not to do that, bail out.
1264 */
1265 if (dontWait)
1267
1268 /*
1269 * Insert self into queue, at the position determined above.
1270 */
1271 if (insert_before)
1273 else
1275
1276 lock->waitMask |= LOCKBIT_ON(lockmode);
1277
1278 /* Set up wait information in PGPROC object, too */
1280 MyProc->waitLock = lock;
1281 MyProc->waitProcLock = proclock;
1282 MyProc->waitLockMode = lockmode;
1283
1285
1287}
void RememberSimpleDeadLock(PGPROC *proc1, LOCKMODE lockmode, LOCK *lock, PGPROC *proc2)
Definition deadlock.c:1147
static void dclist_push_tail(dclist_head *head, dlist_node *node)
Definition ilist.h:709
static bool dclist_is_empty(const dclist_head *head)
Definition ilist.h:682
static void dclist_insert_before(dclist_head *head, dlist_node *before, dlist_node *node)
Definition ilist.h:745
#define dclist_foreach(iter, lhead)
Definition ilist.h:970
void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode)
Definition lock.c:1658
bool LockCheckConflicts(LockMethod lockMethodTable, LOCKMODE lockmode, LOCK *lock, PROCLOCK *proclock)
Definition lock.c:1529
#define LOCKBIT_ON(lockmode)
Definition lock.h:86
int LOCKMODE
Definition lockdefs.h:26
int LOCKMASK
Definition lockdefs.h:25
bool LWLockHeldByMeInMode(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1956
@ PROC_WAIT_STATUS_WAITING
Definition proc.h:143
@ PROC_WAIT_STATUS_ERROR
Definition proc.h:144
dclist_head waitProcs
Definition lock.h:319
LOCKMASK waitMask
Definition lock.h:317
LOCKMODE waitLockMode
Definition proc.h:304
LOCKMASK heldLocks
Definition proc.h:305
LOCKMASK holdMask
Definition lock.h:378
dlist_node * cur
Definition ilist.h:179

References Assert, dlist_iter::cur, dclist_foreach, dclist_insert_before(), dclist_is_empty(), dclist_push_tail(), dlist_container, dlist_foreach, fb(), GrantLock(), PGPROC::heldLocks, PROCLOCK::holdMask, LOCKBIT_ON, LockCheckConflicts(), PGPROC::lockGroupLeader, LockHashPartitionLock, LW_EXCLUSIVE, LWLockHeldByMeInMode(), MyProc, PG_USED_FOR_ASSERTS_ONLY, PROC_WAIT_STATUS_ERROR, PROC_WAIT_STATUS_OK, PROC_WAIT_STATUS_WAITING, LOCK::procLocks, RememberSimpleDeadLock(), PGPROC::waitLink, PGPROC::waitLock, PGPROC::waitLockMode, LOCK::waitMask, PGPROC::waitProcLock, LOCK::waitProcs, and PGPROC::waitStatus.

Referenced by LockAcquireExtended().

◆ LockErrorCleanup()

void LockErrorCleanup ( void  )

Definition at line 806 of file proc.c.

807{
811
813
815
816 /* Nothing to do if we weren't waiting for a lock */
818 if (lockAwaited == NULL)
819 {
821 return;
822 }
823
824 /*
825 * Turn off the deadlock and lock timeout timers, if they are still
826 * running (see ProcSleep). Note we must preserve the LOCK_TIMEOUT
827 * indicator flag, since this function is executed before
828 * ProcessInterrupts when responding to SIGINT; else we'd lose the
829 * knowledge that the SIGINT came from a lock timeout and not an external
830 * source.
831 */
833 timeouts[0].keep_indicator = false;
834 timeouts[1].id = LOCK_TIMEOUT;
835 timeouts[1].keep_indicator = true;
837
838 /* Unlink myself from the wait queue, if on it (might not be anymore!) */
841
843 {
844 /* We could not have been granted the lock yet */
846 }
847 else
848 {
849 /*
850 * Somebody kicked us off the lock queue already. Perhaps they
851 * granted us the lock, or perhaps they detected a deadlock. If they
852 * did grant us the lock, we'd better remember it in our local lock
853 * table.
854 */
857 }
858
860
862
864}
void GrantAwaitedLock(void)
Definition lock.c:1889
void ResetAwaitedLock(void)
Definition lock.c:1907
void AbortStrongLockAcquire(void)
Definition lock.c:1860
LOCALLOCK * GetAwaitedLock(void)
Definition lock.c:1898
#define RESUME_INTERRUPTS()
Definition miscadmin.h:136
#define HOLD_INTERRUPTS()
Definition miscadmin.h:134
void disable_timeouts(const DisableTimeoutParams *timeouts, int count)
Definition timeout.c:718
@ LOCK_TIMEOUT
Definition timeout.h:28
@ DEADLOCK_TIMEOUT
Definition timeout.h:27

References AbortStrongLockAcquire(), DEADLOCK_TIMEOUT, disable_timeouts(), dlist_node_is_detached(), fb(), GetAwaitedLock(), GrantAwaitedLock(), HOLD_INTERRUPTS, LOCK_TIMEOUT, LockHashPartitionLock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyProc, PROC_WAIT_STATUS_OK, RemoveFromWaitQueue(), ResetAwaitedLock(), RESUME_INTERRUPTS, PGPROC::waitLink, and PGPROC::waitStatus.

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

◆ PGProcShmemSize()

static Size PGProcShmemSize ( void  )
static

Definition at line 88 of file proc.c.

89{
90 Size size = 0;
93
94 size = add_size(size, mul_size(TotalProcs, sizeof(PGPROC)));
95 size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->xids)));
96 size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->subxidStates)));
97 size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->statusFlags)));
98
99 return size;
100}

References add_size(), fb(), max_prepared_xacts, MaxBackends, mul_size(), NUM_AUXILIARY_PROCS, ProcGlobal, PROC_HDR::statusFlags, PROC_HDR::subxidStates, and PROC_HDR::xids.

Referenced by InitProcGlobal(), and ProcGlobalShmemSize().

◆ ProcGlobalSemas()

int ProcGlobalSemas ( void  )

Definition at line 149 of file proc.c.

150{
151 /*
152 * We need a sema per backend (including autovacuum), plus one for each
153 * auxiliary process.
154 */
156}

References MaxBackends, and NUM_AUXILIARY_PROCS.

Referenced by InitializeShmemGUCs(), InitProcGlobal(), and ProcGlobalShmemSize().

◆ ProcGlobalShmemSize()

Size ProcGlobalShmemSize ( void  )

Definition at line 130 of file proc.c.

131{
132 Size size = 0;
133
134 /* ProcGlobal */
135 size = add_size(size, sizeof(PROC_HDR));
136 size = add_size(size, sizeof(slock_t));
137
139 size = add_size(size, PGProcShmemSize());
140 size = add_size(size, FastPathLockShmemSize());
141
142 return size;
143}
Size PGSemaphoreShmemSize(int maxSemas)
Definition posix_sema.c:165

References add_size(), FastPathLockShmemSize(), fb(), PGProcShmemSize(), PGSemaphoreShmemSize(), and ProcGlobalSemas().

Referenced by CalculateShmemSize().

◆ ProcKill()

static void ProcKill ( int  code,
Datum  arg 
)
static

Definition at line 912 of file proc.c.

913{
914 PGPROC *proc;
915 dlist_head *procgloballist;
916
917 Assert(MyProc != NULL);
918
919 /* not safe if forked by system(), etc. */
920 if (MyProc->pid != (int) getpid())
921 elog(PANIC, "ProcKill() called in child process");
922
923 /* Make sure we're out of the sync rep lists */
925
926#ifdef USE_ASSERT_CHECKING
927 {
928 int i;
929
930 /* Last process should have released all locks. */
931 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
933 }
934#endif
935
936 /*
937 * Release any LW locks I am holding. There really shouldn't be any, but
938 * it's cheap to check again before we cut the knees off the LWLock
939 * facility by releasing our PGPROC ...
940 */
942
943 /*
944 * Cleanup waiting for LSN if any.
945 */
947
948 /* Cancel any pending condition variable sleep, too */
950
951 /*
952 * Detach from any lock group of which we are a member. If the leader
953 * exits before all other group members, its PGPROC will remain allocated
954 * until the last group process exits; that process must return the
955 * leader's PGPROC to the appropriate list.
956 */
958 {
959 PGPROC *leader = MyProc->lockGroupLeader;
961
965 if (dlist_is_empty(&leader->lockGroupMembers))
966 {
967 leader->lockGroupLeader = NULL;
968 if (leader != MyProc)
969 {
970 procgloballist = leader->procgloballist;
971
972 /* Leader exited first; return its PGPROC. */
974 dlist_push_head(procgloballist, &leader->freeProcsLink);
976 }
977 }
978 else if (leader != MyProc)
981 }
982
983 /*
984 * Reset MyLatch to the process local one. This is so that signal
985 * handlers et al can continue using the latch after the shared latch
986 * isn't ours anymore.
987 *
988 * Similarly, stop reporting wait events to MyProc->wait_event_info.
989 *
990 * After that clear MyProc and disown the shared latch.
991 */
994
995 proc = MyProc;
996 MyProc = NULL;
998 DisownLatch(&proc->procLatch);
999
1000 /* Mark the proc no longer in use */
1001 proc->pid = 0;
1004
1005 procgloballist = proc->procgloballist;
1007
1008 /*
1009 * If we're still a member of a locking group, that means we're a leader
1010 * which has somehow exited before its children. The last remaining child
1011 * will release our PGPROC. Otherwise, release it now.
1012 */
1013 if (proc->lockGroupLeader == NULL)
1014 {
1015 /* Since lockGroupLeader is NULL, lockGroupMembers should be empty. */
1017
1018 /* Return PGPROC structure (and semaphore) to appropriate freelist */
1019 dlist_push_tail(procgloballist, &proc->freeProcsLink);
1020 }
1021
1022 /* Update shared estimate of spins_per_delay */
1024
1026}
static void dlist_delete(dlist_node *node)
Definition ilist.h:405
void SyncRepCleanupAtProcExit(void)
Definition syncrep.c:417
void WaitLSNCleanup(void)
Definition xlogwait.c:339

References Assert, ConditionVariableCancelSleep(), DisownLatch(), dlist_delete(), dlist_is_empty(), dlist_push_head(), dlist_push_tail(), elog, fb(), PGPROC::freeProcsLink, PROC_HDR::freeProcsLock, i, INVALID_PROC_NUMBER, InvalidTransactionId, PGPROC::lockGroupLeader, PGPROC::lockGroupLink, PGPROC::lockGroupMembers, LockHashPartitionLockByProc, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), LWLockReleaseAll(), PGPROC::lxid, MyProc, PGPROC::myProcLocks, MyProcNumber, NUM_LOCK_PARTITIONS, PANIC, pgstat_reset_wait_event_storage(), PGPROC::pid, ProcGlobal, PGPROC::procgloballist, PGPROC::procLatch, PGPROC::procNumber, SpinLockAcquire(), SpinLockRelease(), PROC_HDR::spins_per_delay, SwitchBackToLocalLatch(), SyncRepCleanupAtProcExit(), update_spins_per_delay(), PGPROC::vxid, and WaitLSNCleanup().

Referenced by InitProcess().

◆ ProcLockWakeup()

void ProcLockWakeup ( LockMethod  lockMethodTable,
LOCK lock 
)

Definition at line 1752 of file proc.c.

1753{
1757
1759 return;
1760
1762 {
1763 PGPROC *proc = dlist_container(PGPROC, waitLink, miter.cur);
1764 LOCKMODE lockmode = proc->waitLockMode;
1765
1766 /*
1767 * Waken if (a) doesn't conflict with requests of earlier waiters, and
1768 * (b) doesn't conflict with already-held locks.
1769 */
1770 if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1771 !LockCheckConflicts(lockMethodTable, lockmode, lock,
1772 proc->waitProcLock))
1773 {
1774 /* OK to waken */
1775 GrantLock(lock, proc->waitProcLock, lockmode);
1776 /* removes proc from the lock's waiting process queue */
1778 }
1779 else
1780 {
1781 /*
1782 * Lock conflicts: Don't wake, but remember requested mode for
1783 * later checks.
1784 */
1785 aheadRequests |= LOCKBIT_ON(lockmode);
1786 }
1787 }
1788}
#define dclist_foreach_modify(iter, lhead)
Definition ilist.h:973
void ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus)
Definition proc.c:1724

References dclist_foreach_modify, dclist_is_empty(), dlist_container, fb(), GrantLock(), LOCKBIT_ON, LockCheckConflicts(), PROC_WAIT_STATUS_OK, ProcWakeup(), PGPROC::waitLockMode, PGPROC::waitProcLock, and LOCK::waitProcs.

Referenced by CleanUpLock(), and DeadLockCheck().

◆ ProcReleaseLocks()

void ProcReleaseLocks ( bool  isCommit)

Definition at line 884 of file proc.c.

885{
886 if (!MyProc)
887 return;
888 /* If waiting, get off wait queue (should only be needed after error) */
890 /* Release standard locks, including session-level if aborting */
892 /* Release transaction-level advisory locks */
894}
void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
Definition lock.c:2307
#define DEFAULT_LOCKMETHOD
Definition lock.h:127
#define USER_LOCKMETHOD
Definition lock.h:128
void LockErrorCleanup(void)
Definition proc.c:806

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

Referenced by ResourceOwnerReleaseInternal().

◆ ProcSendSignal()

void ProcSendSignal ( ProcNumber  procNumber)

Definition at line 2003 of file proc.c.

2004{
2006 elog(ERROR, "procNumber out of range");
2007
2008 SetLatch(&GetPGProcByNumber(procNumber)->procLatch);
2009}
#define GetPGProcByNumber(n)
Definition proc.h:501

References PROC_HDR::allProcCount, elog, ERROR, fb(), GetPGProcByNumber, ProcGlobal, and SetLatch().

Referenced by ReleasePredicateLocks(), and WakePinCountWaiter().

◆ ProcSleep()

ProcWaitStatus ProcSleep ( LOCALLOCK locallock)

Definition at line 1303 of file proc.c.

1304{
1305 LOCKMODE lockmode = locallock->tag.mode;
1306 LOCK *lock = locallock->lock;
1307 uint32 hashcode = locallock->hashcode;
1310 bool allow_autovacuum_cancel = true;
1311 bool logged_recovery_conflict = false;
1312 bool logged_lock_wait = false;
1315
1316 /* The caller must've armed the on-error cleanup mechanism */
1319
1320 /*
1321 * Now that we will successfully clean up after an ereport, it's safe to
1322 * check to see if there's a buffer pin deadlock against the Startup
1323 * process. Of course, that's only necessary if we're doing Hot Standby
1324 * and are not the Startup process ourselves.
1325 */
1328
1329 /* Reset deadlock_state before enabling the timeout handler */
1331 got_deadlock_timeout = false;
1332
1333 /*
1334 * Set timer so we can wake up after awhile and check for a deadlock. If a
1335 * deadlock is detected, the handler sets MyProc->waitStatus =
1336 * PROC_WAIT_STATUS_ERROR, allowing us to know that we must report failure
1337 * rather than success.
1338 *
1339 * By delaying the check until we've waited for a bit, we can avoid
1340 * running the rather expensive deadlock-check code in most cases.
1341 *
1342 * If LockTimeout is set, also enable the timeout for that. We can save a
1343 * few cycles by enabling both timeout sources in one call.
1344 *
1345 * If InHotStandby we set lock waits slightly later for clarity with other
1346 * code.
1347 */
1348 if (!InHotStandby)
1349 {
1350 if (LockTimeout > 0)
1351 {
1353
1355 timeouts[0].type = TMPARAM_AFTER;
1356 timeouts[0].delay_ms = DeadlockTimeout;
1357 timeouts[1].id = LOCK_TIMEOUT;
1358 timeouts[1].type = TMPARAM_AFTER;
1359 timeouts[1].delay_ms = LockTimeout;
1361 }
1362 else
1364
1365 /*
1366 * Use the current time obtained for the deadlock timeout timer as
1367 * waitStart (i.e., the time when this process started waiting for the
1368 * lock). Since getting the current time newly can cause overhead, we
1369 * reuse the already-obtained time to avoid that overhead.
1370 *
1371 * Note that waitStart is updated without holding the lock table's
1372 * partition lock, to avoid the overhead by additional lock
1373 * acquisition. This can cause "waitstart" in pg_locks to become NULL
1374 * for a very short period of time after the wait started even though
1375 * "granted" is false. This is OK in practice because we can assume
1376 * that users are likely to look at "waitstart" when waiting for the
1377 * lock for a long time.
1378 */
1381 }
1383 {
1384 /*
1385 * Set the wait start timestamp if logging is enabled and in hot
1386 * standby.
1387 */
1389 }
1390
1391 /*
1392 * If somebody wakes us between LWLockRelease and WaitLatch, the latch
1393 * will not wait. But a set latch does not necessarily mean that the lock
1394 * is free now, as there are many other sources for latch sets than
1395 * somebody releasing the lock.
1396 *
1397 * We process interrupts whenever the latch has been set, so cancel/die
1398 * interrupts are processed quickly. This means we must not mind losing
1399 * control to a cancel/die interrupt here. We don't, because we have no
1400 * shared-state-change work to do after being granted the lock (the
1401 * grantor did it all). We do have to worry about canceling the deadlock
1402 * timeout and updating the locallock table, but if we lose control to an
1403 * error, LockErrorCleanup will fix that up.
1404 */
1405 do
1406 {
1407 if (InHotStandby)
1408 {
1409 bool maybe_log_conflict =
1411
1412 /* Set a timer and wait for that or for the lock to be granted */
1415
1416 /*
1417 * Emit the log message if the startup process is waiting longer
1418 * than deadlock_timeout for recovery conflict on lock.
1419 */
1421 {
1423
1426 {
1428 int cnt;
1429
1430 vxids = GetLockConflicts(&locallock->tag.lock,
1431 AccessExclusiveLock, &cnt);
1432
1433 /*
1434 * Log the recovery conflict and the list of PIDs of
1435 * backends holding the conflicting lock. Note that we do
1436 * logging even if there are no such backends right now
1437 * because the startup process here has already waited
1438 * longer than deadlock_timeout.
1439 */
1442 cnt > 0 ? vxids : NULL, true);
1444 }
1445 }
1446 }
1447 else
1448 {
1450 PG_WAIT_LOCK | locallock->tag.lock.locktag_type);
1452 /* check for deadlocks first, as that's probably log-worthy */
1454 {
1456 got_deadlock_timeout = false;
1457 }
1459 }
1460
1461 /*
1462 * waitStatus could change from PROC_WAIT_STATUS_WAITING to something
1463 * else asynchronously. Read it just once per loop to prevent
1464 * surprising behavior (such as missing log messages).
1465 */
1466 myWaitStatus = *((volatile ProcWaitStatus *) &MyProc->waitStatus);
1467
1468 /*
1469 * If we are not deadlocked, but are waiting on an autovacuum-induced
1470 * task, send a signal to interrupt it.
1471 */
1473 {
1475 uint8 statusFlags;
1478
1479 /*
1480 * Grab info we need, then release lock immediately. Note this
1481 * coding means that there is a tiny chance that the process
1482 * terminates its current transaction and starts a different one
1483 * before we have a change to send the signal; the worst possible
1484 * consequence is that a for-wraparound vacuum is canceled. But
1485 * that could happen in any case unless we were to do kill() with
1486 * the lock held, which is much more undesirable.
1487 */
1489 statusFlags = ProcGlobal->statusFlags[autovac->pgxactoff];
1491 locktag_copy = lock->tag;
1493
1494 /*
1495 * Only do it if the worker is not working to protect against Xid
1496 * wraparound.
1497 */
1498 if ((statusFlags & PROC_IS_AUTOVACUUM) &&
1499 !(statusFlags & PROC_VACUUM_FOR_WRAPAROUND))
1500 {
1501 int pid = autovac->pid;
1502
1503 /* report the case, if configured to do so */
1505 {
1507 StringInfoData logbuf; /* errdetail for server log */
1508
1513 "Process %d waits for %s on %s.",
1514 MyProcPid,
1516 locktagbuf.data);
1517
1519 (errmsg_internal("sending cancel to blocking autovacuum PID %d",
1520 pid),
1521 errdetail_log("%s", logbuf.data)));
1522
1523 pfree(locktagbuf.data);
1524 pfree(logbuf.data);
1525 }
1526
1527 /* send the autovacuum worker Back to Old Kent Road */
1528 if (kill(pid, SIGINT) < 0)
1529 {
1530 /*
1531 * There's a race condition here: once we release the
1532 * ProcArrayLock, it's possible for the autovac worker to
1533 * close up shop and exit before we can do the kill().
1534 * Therefore, we do not whinge about no-such-process.
1535 * Other errors such as EPERM could conceivably happen if
1536 * the kernel recycles the PID fast enough, but such cases
1537 * seem improbable enough that it's probably best to issue
1538 * a warning if we see some other errno.
1539 */
1540 if (errno != ESRCH)
1542 (errmsg("could not send signal to process %d: %m",
1543 pid)));
1544 }
1545 }
1546
1547 /* prevent signal from being sent again more than once */
1549 }
1550
1551 /*
1552 * If awoken after the deadlock check interrupt has run, and
1553 * log_lock_waits is on, then report about the wait.
1554 */
1556 {
1560 const char *modename;
1561 long secs;
1562 int usecs;
1563 long msecs;
1564 int lockHoldersNum = 0;
1565
1569
1570 DescribeLockTag(&buf, &locallock->tag.lock);
1571 modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
1572 lockmode);
1575 &secs, &usecs);
1576 msecs = secs * 1000 + usecs / 1000;
1577 usecs = usecs % 1000;
1578
1579 /* Gather a list of all lock holders and waiters */
1584
1586 ereport(LOG,
1587 (errmsg("process %d avoided deadlock for %s on %s by rearranging queue order after %ld.%03d ms",
1588 MyProcPid, modename, buf.data, msecs, usecs),
1589 (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1590 "Processes holding the lock: %s. Wait queue: %s.",
1592 else if (deadlock_state == DS_HARD_DEADLOCK)
1593 {
1594 /*
1595 * This message is a bit redundant with the error that will be
1596 * reported subsequently, but in some cases the error report
1597 * might not make it to the log (eg, if it's caught by an
1598 * exception handler), and we want to ensure all long-wait
1599 * events get logged.
1600 */
1601 ereport(LOG,
1602 (errmsg("process %d detected deadlock while waiting for %s on %s after %ld.%03d ms",
1603 MyProcPid, modename, buf.data, msecs, usecs),
1604 (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1605 "Processes holding the lock: %s. Wait queue: %s.",
1607 }
1608
1610 {
1611 /*
1612 * Guard the "still waiting on lock" log message so it is
1613 * reported at most once while waiting for the lock.
1614 *
1615 * Without this guard, the message can be emitted whenever the
1616 * lock-wait sleep is interrupted (for example by SIGHUP for
1617 * config reload or by client_connection_check_interval). For
1618 * example, if client_connection_check_interval is set very
1619 * low (e.g., 100 ms), the message could be logged repeatedly,
1620 * flooding the log and making it difficult to use.
1621 */
1622 if (!logged_lock_wait)
1623 {
1624 ereport(LOG,
1625 (errmsg("process %d still waiting for %s on %s after %ld.%03d ms",
1626 MyProcPid, modename, buf.data, msecs, usecs),
1627 (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1628 "Processes holding the lock: %s. Wait queue: %s.",
1630 logged_lock_wait = true;
1631 }
1632 }
1634 ereport(LOG,
1635 (errmsg("process %d acquired %s on %s after %ld.%03d ms",
1636 MyProcPid, modename, buf.data, msecs, usecs)));
1637 else
1638 {
1640
1641 /*
1642 * Currently, the deadlock checker always kicks its own
1643 * process, which means that we'll only see
1644 * PROC_WAIT_STATUS_ERROR when deadlock_state ==
1645 * DS_HARD_DEADLOCK, and there's no need to print redundant
1646 * messages. But for completeness and future-proofing, print
1647 * a message if it looks like someone else kicked us off the
1648 * lock.
1649 */
1651 ereport(LOG,
1652 (errmsg("process %d failed to acquire %s on %s after %ld.%03d ms",
1653 MyProcPid, modename, buf.data, msecs, usecs),
1654 (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1655 "Processes holding the lock: %s. Wait queue: %s.",
1657 }
1658
1659 /*
1660 * At this point we might still need to wait for the lock. Reset
1661 * state so we don't print the above messages again.
1662 */
1664
1665 pfree(buf.data);
1668 }
1670
1671 /*
1672 * Disable the timers, if they are still running. As in LockErrorCleanup,
1673 * we must preserve the LOCK_TIMEOUT indicator flag: if a lock timeout has
1674 * already caused QueryCancelPending to become set, we want the cancel to
1675 * be reported as a lock timeout, not a user cancel.
1676 */
1677 if (!InHotStandby)
1678 {
1679 if (LockTimeout > 0)
1680 {
1682
1684 timeouts[0].keep_indicator = false;
1685 timeouts[1].id = LOCK_TIMEOUT;
1686 timeouts[1].keep_indicator = true;
1688 }
1689 else
1691 }
1692
1693 /*
1694 * Emit the log message if recovery conflict on lock was resolved but the
1695 * startup process waited longer than deadlock_timeout for it.
1696 */
1700 NULL, false);
1701
1702 /*
1703 * We don't have to do anything else, because the awaker did all the
1704 * necessary updates of the lock table and MyProc. (The caller is
1705 * responsible for updating the local lock table.)
1706 */
1707 return myWaitStatus;
1708}
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition timestamp.c:1712
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition timestamp.c:1772
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1636
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1600
int64 TimestampTz
Definition timestamp.h:39
PGPROC * GetBlockingAutoVacuumPgproc(void)
Definition deadlock.c:290
bool message_level_is_interesting(int elevel)
Definition elog.c:284
#define LOG
Definition elog.h:31
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:36
#define DEBUG1
Definition elog.h:30
int int int errdetail_log(const char *fmt,...) pg_attribute_printf(1
int int int int errdetail_log_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...) pg_attribute_printf(1
return true
Definition isn.c:130
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
void DescribeLockTag(StringInfo buf, const LOCKTAG *tag)
Definition lmgr.c:1249
VirtualTransactionId * GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
Definition lock.c:3069
const char * GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode)
Definition lock.c:4252
@ DS_BLOCKED_BY_AUTOVACUUM
Definition lock.h:516
@ DS_NOT_YET_CHECKED
Definition lock.h:512
@ DS_SOFT_DEADLOCK
Definition lock.h:514
#define AccessExclusiveLock
Definition lockdefs.h:43
@ LW_SHARED
Definition lwlock.h:113
void pfree(void *pointer)
Definition mcxt.c:1616
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define PROC_VACUUM_FOR_WRAPAROUND
Definition proc.h:61
ProcWaitStatus
Definition proc.h:141
bool log_lock_waits
Definition proc.c:65
void GetLockHoldersAndWaiters(LOCALLOCK *locallock, StringInfo lock_holders_sbuf, StringInfo lock_waiters_sbuf, int *lockHoldersNum)
Definition proc.c:1917
int DeadlockTimeout
Definition proc.c:59
int LockTimeout
Definition proc.c:61
static DeadLockState CheckDeadLock(void)
Definition proc.c:1799
void CheckRecoveryConflictDeadlock(void)
Definition standby.c:906
bool log_recovery_conflict_waits
Definition standby.c:43
void LogRecoveryConflict(RecoveryConflictReason reason, TimestampTz wait_start, TimestampTz now, VirtualTransactionId *wait_list, bool still_waiting)
Definition standby.c:275
void ResolveRecoveryConflictWithLock(LOCKTAG locktag, bool logging_conflict)
Definition standby.c:625
@ RECOVERY_CONFLICT_LOCK
Definition standby.h:37
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
uint8 locktag_lockmethodid
Definition lock.h:173
void enable_timeout_after(TimeoutId id, int delay_ms)
Definition timeout.c:560
TimestampTz get_timeout_start_time(TimeoutId id)
Definition timeout.c:813
void disable_timeout(TimeoutId id, bool keep_indicator)
Definition timeout.c:685
void enable_timeouts(const EnableTimeoutParams *timeouts, int count)
Definition timeout.c:630
@ TMPARAM_AFTER
Definition timeout.h:53
#define PG_WAIT_LOCK
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET
#define kill(pid, sig)
Definition win32_port.h:490
bool RecoveryInProgress(void)
Definition xlog.c:6444
bool InRecovery
Definition xlogutils.c:50
#define InHotStandby
Definition xlogutils.h:60

References AccessExclusiveLock, appendStringInfo(), Assert, buf, CHECK_FOR_INTERRUPTS, CheckDeadLock(), CheckRecoveryConflictDeadlock(), DEADLOCK_TIMEOUT, DeadlockTimeout, DEBUG1, 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, errmsg_internal(), fb(), get_timeout_start_time(), GetAwaitedLock(), GetBlockingAutoVacuumPgproc(), GetCurrentTimestamp(), GetLockConflicts(), GetLockHoldersAndWaiters(), GetLockmodeName(), got_deadlock_timeout, EnableTimeoutParams::id, DisableTimeoutParams::id, InHotStandby, initStringInfo(), InRecovery, kill, LOCK_TIMEOUT, LockHashPartitionLock, LOCKTAG::locktag_lockmethodid, LockTimeout, LOG, log_lock_waits, log_recovery_conflict_waits, LogRecoveryConflict(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockHeldByMe(), LWLockRelease(), message_level_is_interesting(), MyLatch, MyProc, MyProcPid, now(), pfree(), pg_atomic_write_u64(), PG_WAIT_LOCK, PROC_IS_AUTOVACUUM, PROC_VACUUM_FOR_WRAPAROUND, PROC_WAIT_STATUS_ERROR, PROC_WAIT_STATUS_OK, PROC_WAIT_STATUS_WAITING, ProcGlobal, RECOVERY_CONFLICT_LOCK, RecoveryInProgress(), ResetLatch(), ResolveRecoveryConflictWithLock(), PROC_HDR::statusFlags, LOCK::tag, TimestampDifference(), TimestampDifferenceExceeds(), TMPARAM_AFTER, WaitLatch(), PGPROC::waitStart, PGPROC::waitStatus, WARNING, WL_EXIT_ON_PM_DEATH, and WL_LATCH_SET.

Referenced by WaitOnLock().

◆ ProcWaitForSignal()

◆ ProcWakeup()

void ProcWakeup ( PGPROC proc,
ProcWaitStatus  waitStatus 
)

Definition at line 1724 of file proc.c.

1725{
1726 if (dlist_node_is_detached(&proc->waitLink))
1727 return;
1728
1730
1731 /* Remove process from wait queue */
1733
1734 /* Clean up process' state and pass it the ok/fail signal */
1735 proc->waitLock = NULL;
1736 proc->waitProcLock = NULL;
1737 proc->waitStatus = waitStatus;
1738 pg_atomic_write_u64(&proc->waitStart, 0);
1739
1740 /* And awaken it */
1741 SetLatch(&proc->procLatch);
1742}
static void dclist_delete_from_thoroughly(dclist_head *head, dlist_node *node)
Definition ilist.h:776

References Assert, dclist_delete_from_thoroughly(), dlist_node_is_detached(), fb(), pg_atomic_write_u64(), PROC_WAIT_STATUS_WAITING, PGPROC::procLatch, SetLatch(), PGPROC::waitLink, PGPROC::waitLock, PGPROC::waitProcLock, LOCK::waitProcs, PGPROC::waitStart, and PGPROC::waitStatus.

Referenced by ProcLockWakeup().

◆ RemoveProcFromArray()

static void RemoveProcFromArray ( int  code,
Datum  arg 
)
static

Definition at line 901 of file proc.c.

902{
903 Assert(MyProc != NULL);
905}
void ProcArrayRemove(PGPROC *proc, TransactionId latestXid)
Definition procarray.c:569

References Assert, fb(), InvalidTransactionId, MyProc, and ProcArrayRemove().

Referenced by InitProcessPhase2().

◆ SetStartupBufferPinWaitBufId()

void SetStartupBufferPinWaitBufId ( int  bufid)

Definition at line 747 of file proc.c.

748{
749 /* use volatile pointer to prevent code rearrangement */
750 volatile PROC_HDR *procglobal = ProcGlobal;
751
753}

References fb(), ProcGlobal, and PROC_HDR::startupBufferPinWaitBufId.

Referenced by LockBufferForCleanup().

Variable Documentation

◆ AuxiliaryProcs

NON_EXEC_STATIC PGPROC* AuxiliaryProcs = NULL

◆ DeadlockTimeout

◆ got_deadlock_timeout

volatile sig_atomic_t got_deadlock_timeout
static

Definition at line 76 of file proc.c.

Referenced by CheckDeadLockAlert(), and ProcSleep().

◆ IdleInTransactionSessionTimeout

int IdleInTransactionSessionTimeout = 0

Definition at line 62 of file proc.c.

Referenced by PostgresMain(), and ProcessInterrupts().

◆ IdleSessionTimeout

int IdleSessionTimeout = 0

Definition at line 64 of file proc.c.

Referenced by PostgresMain(), and ProcessInterrupts().

◆ LockTimeout

int LockTimeout = 0

Definition at line 61 of file proc.c.

Referenced by ProcSleep().

◆ log_lock_waits

bool log_lock_waits = true

Definition at line 65 of file proc.c.

Referenced by ProcSleep().

◆ MyProc

PGPROC* MyProc = NULL

Definition at line 68 of file proc.c.

Referenced by _brin_parallel_build_main(), _bt_parallel_build_main(), _gin_parallel_build_main(), AbortTransaction(), AtEOSubXact_Namespace(), AtEOXact_Namespace(), AtEOXact_Snapshot(), attach_to_queues(), AutoVacWorkerMain(), AuxiliaryProcKill(), BaseInit(), BecomeLockGroupLeader(), BecomeLockGroupMember(), BufferLockAcquire(), BufferLockDequeueSelf(), BufferLockQueueSelf(), CheckDeadLock(), CommitTransaction(), ComputeXidHorizons(), ConditionVariableBroadcast(), consume_xids_common(), CountOtherDBBackends(), CreateReplicationSlot(), DefineIndex(), EndPrepare(), exec_eval_simple_expr(), exec_simple_check_plan(), exec_stmt_call(), ExecParallelGetReceiver(), ExecParallelSetupTupleQueues(), ExecWaitStmt(), ExportSnapshot(), FastPathGetRelationLockEntry(), FastPathGrantRelationLock(), FastPathUnGrantRelationLock(), FindLockCycleRecurseMember(), get_cast_hashentry(), GetCurrentVirtualXIDs(), GetLockConflicts(), GetNewTransactionId(), GetSerializableTransactionSnapshotInt(), GetSnapshotData(), GetSnapshotDataReuse(), GetStableLatestTransactionId(), HandleRecoveryConflictInterrupt(), InitAuxiliaryProcess(), InitBufferManagerAccess(), InitializeParallelDSM(), InitPostgres(), InitProcess(), InitProcessPhase2(), InitRecoveryTransactionEnvironment(), InitTempTableNamespace(), InitWalSender(), JoinWaitQueue(), lock_and_open_sequence(), LockAcquireExtended(), LockBufferInternal(), LockCheckConflicts(), LockErrorCleanup(), LockRelease(), LockReleaseAll(), log_status_format(), logicalrep_worker_attach(), LWLockAcquire(), LWLockAcquireOrWait(), LWLockAttemptLock(), LWLockDequeueSelf(), LWLockQueueSelf(), LWLockWaitForVar(), MarkAsPreparingGuts(), MinimumActiveBackends(), pa_setup_dsm(), parallel_vacuum_main(), ParallelApplyWorkerMain(), ParallelWorkerMain(), pg_truncate_visibility_map(), pgaio_init_backend(), pgstat_report_activity(), PhysicalReplicationSlotNewXmin(), PostPrepare_Locks(), PrepareTransaction(), ProcArrayGroupClearXid(), ProcArrayInstallImportedXmin(), ProcArrayInstallRestoredXmin(), ProcessInterrupts(), ProcessRecoveryConflictInterrupts(), ProcessStandbyHSFeedbackMessage(), ProcKill(), ProcReleaseLocks(), ProcSleep(), RecordTransactionCommit(), RecordTransactionCommitPrepared(), ReinitializeParallelDSM(), RelationTruncate(), RemoveProcFromArray(), ReplicationSlotRelease(), report_recovery_conflict(), ResolveRecoveryConflictWithLock(), set_indexsafe_procflags(), SetAuthenticatedUserId(), setup_dynamic_shared_memory(), shm_mq_attach(), shm_mq_detach_internal(), shm_mq_receive(), shm_mq_sendv(), shm_mq_wait_for_attach(), smgr_bulk_finish(), SnapBuildInitialSnapshot(), SnapshotResetXmin(), StartTransaction(), StartupDecodingContext(), SwitchBackToLocalLatch(), SwitchToSharedLatch(), SyncRepCancelWait(), SyncRepCleanupAtProcExit(), SyncRepQueueInsert(), SyncRepWaitForLSN(), TerminateOtherDBBackends(), TransactionGroupUpdateXidStatus(), TransactionIdIsInProgress(), TransactionIdSetPageStatus(), TruncateMultiXact(), vacuum_rel(), VirtualXactLockTableCleanup(), VirtualXactLockTableInsert(), WaitXLogInsertionsToFinish(), write_csvlog(), write_jsonlog(), and XidCacheRemoveRunningXids().

◆ PreparedXactProcs

PGPROC* PreparedXactProcs = NULL

Definition at line 73 of file proc.c.

Referenced by InitProcGlobal(), and TwoPhaseShmemInit().

◆ ProcGlobal

◆ StatementTimeout

int StatementTimeout = 0

Definition at line 60 of file proc.c.

Referenced by enable_statement_timeout().

◆ TransactionTimeout