PostgreSQL Source Code git master
Loading...
Searching...
No Matches
proc.h File Reference
#include "access/xlogdefs.h"
#include "lib/ilist.h"
#include "miscadmin.h"
#include "storage/latch.h"
#include "storage/lock.h"
#include "storage/pg_sema.h"
#include "storage/proclist_types.h"
#include "storage/procnumber.h"
#include "storage/spin.h"
Include dependency graph for proc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  XidCacheStatus
 
struct  XidCache
 
struct  PGPROC
 
struct  PROC_HDR
 

Macros

#define PGPROC_MAX_CACHED_SUBXIDS   64 /* XXX guessed-at value */
 
#define PROC_IS_AUTOVACUUM   0x01 /* is it an autovac worker? */
 
#define PROC_IN_VACUUM   0x02 /* currently running lazy vacuum */
 
#define PROC_IN_SAFE_IC
 
#define PROC_VACUUM_FOR_WRAPAROUND   0x08 /* set by autovac only */
 
#define PROC_IN_LOGICAL_DECODING
 
#define PROC_AFFECTS_ALL_HORIZONS
 
#define PROC_VACUUM_STATE_MASK    (PROC_IN_VACUUM | PROC_IN_SAFE_IC | PROC_VACUUM_FOR_WRAPAROUND)
 
#define PROC_XMIN_FLAGS   (PROC_IN_VACUUM | PROC_IN_SAFE_IC)
 
#define FP_LOCK_GROUPS_PER_BACKEND_MAX   1024
 
#define FP_LOCK_SLOTS_PER_GROUP   16 /* don't change */
 
#define FastPathLockSlotsPerBackend()    (FP_LOCK_SLOTS_PER_GROUP * FastPathLockGroupsPerBackend)
 
#define DELAY_CHKPT_START   (1<<0)
 
#define DELAY_CHKPT_COMPLETE   (1<<1)
 
#define DELAY_CHKPT_IN_COMMIT   (DELAY_CHKPT_START | 1<<2)
 
#define GetPGProcByNumber(n)   (&ProcGlobal->allProcs[(n)])
 
#define GetNumberFromPGProc(proc)   ((proc) - &ProcGlobal->allProcs[0])
 
#define NUM_SPECIAL_WORKER_PROCS   2
 
#define MAX_IO_WORKERS   32
 
#define NUM_AUXILIARY_PROCS   (6 + MAX_IO_WORKERS)
 
#define FIRST_PREPARED_XACT_PROC_NUMBER   (MaxBackends + NUM_AUXILIARY_PROCS)
 

Typedefs

typedef int XidStatus
 
typedef struct XidCacheStatus XidCacheStatus
 
typedef struct PGPROC PGPROC
 
typedef struct PROC_HDR PROC_HDR
 

Enumerations

enum  ProcWaitStatus { PROC_WAIT_STATUS_OK , PROC_WAIT_STATUS_WAITING , PROC_WAIT_STATUS_ERROR }
 

Functions

int ProcGlobalSemas (void)
 
void InitProcess (void)
 
void InitProcessPhase2 (void)
 
void InitAuxiliaryProcess (void)
 
void SetStartupBufferPinWaitBufId (int bufid)
 
int GetStartupBufferPinWaitBufId (void)
 
bool HaveNFreeProcs (int n, int *nfree)
 
void ProcReleaseLocks (bool isCommit)
 
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 LockErrorCleanup (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)
 
PGPROCAuxiliaryPidGetProc (int pid)
 
void BecomeLockGroupLeader (void)
 
bool BecomeLockGroupMember (PGPROC *leader, int pid)
 

Variables

PGDLLIMPORT int FastPathLockGroupsPerBackend
 
PGDLLIMPORT PGPROCMyProc
 
PGDLLIMPORT PROC_HDRProcGlobal
 
PGDLLIMPORT PGPROCPreparedXactProcs
 
PGDLLIMPORT int DeadlockTimeout
 
PGDLLIMPORT int StatementTimeout
 
PGDLLIMPORT int LockTimeout
 
PGDLLIMPORT int IdleInTransactionSessionTimeout
 
PGDLLIMPORT int TransactionTimeout
 
PGDLLIMPORT int IdleSessionTimeout
 
PGDLLIMPORT bool log_lock_waits
 

Macro Definition Documentation

◆ DELAY_CHKPT_COMPLETE

#define DELAY_CHKPT_COMPLETE   (1<<1)

Definition at line 140 of file proc.h.

◆ DELAY_CHKPT_IN_COMMIT

#define DELAY_CHKPT_IN_COMMIT   (DELAY_CHKPT_START | 1<<2)

Definition at line 141 of file proc.h.

◆ DELAY_CHKPT_START

#define DELAY_CHKPT_START   (1<<0)

Definition at line 139 of file proc.h.

◆ FastPathLockSlotsPerBackend

#define FastPathLockSlotsPerBackend ( )     (FP_LOCK_SLOTS_PER_GROUP * FastPathLockGroupsPerBackend)

Definition at line 97 of file proc.h.

150{
155
156/*
157 * Each backend has a PGPROC struct in shared memory. There is also a list of
158 * currently-unused PGPROC structs that will be reallocated to new backends.
159 *
160 * Note: twophase.c also sets up a dummy PGPROC struct for each currently
161 * prepared transaction. These PGPROCs appear in the ProcArray data structure
162 * so that the prepared transactions appear to be still running and are
163 * correctly shown as holding locks. A prepared transaction PGPROC can be
164 * distinguished from a real one at need by the fact that it has pid == 0.
165 * The semaphore and lock-activity fields in a prepared-xact PGPROC are unused,
166 * but its myProcLocks[] lists are valid.
167 *
168 * We allow many fields of this struct to be accessed without locks, such as
169 * delayChkptFlags and backendType. However, keep in mind that writing
170 * mirrored ones (see below) requires holding ProcArrayLock or XidGenLock in
171 * at least shared mode, so that pgxactoff does not change concurrently.
172 *
173 * Mirrored fields:
174 *
175 * Some fields in PGPROC (see "mirrored in ..." comment) are mirrored into an
176 * element of more densely packed ProcGlobal arrays. These arrays are indexed
177 * by PGPROC->pgxactoff. Both copies need to be maintained coherently.
178 *
179 * NB: The pgxactoff indexed value can *never* be accessed without holding
180 * locks.
181 *
182 * See PROC_HDR for details.
183 */
184typedef struct PGPROC
185{
186 /*
187 * Align the struct at cache line boundaries. This is just for
188 * performance, to avoid false sharing.
189 */
190 alignas(PG_CACHE_LINE_SIZE)
191 dlist_head *procgloballist; /* procglobal list that owns this PGPROC */
192 dlist_node freeProcsLink; /* link in procgloballist, when in recycled
193 * state */
194
195 /************************************************************************
196 * Backend identity
197 ************************************************************************/
198
199 /*
200 * These fields that don't change after backend startup, or only very
201 * rarely
202 */
203 int pid; /* Backend's process ID; 0 if prepared xact */
204 BackendType backendType; /* what kind of process is this? */
205
206 /* These fields are zero while a backend is still starting up: */
207 Oid databaseId; /* OID of database this backend is using */
208 Oid roleId; /* OID of role using this backend */
209
210 Oid tempNamespaceId; /* OID of temp schema this backend is
211 * using */
212
213 int pgxactoff; /* offset into various ProcGlobal->arrays with
214 * data mirrored from this PGPROC */
215
216 uint8 statusFlags; /* this backend's status flags, see PROC_*
217 * above. mirrored in
218 * ProcGlobal->statusFlags[pgxactoff] */
219
220 /************************************************************************
221 * Transactions and snapshots
222 ************************************************************************/
223
224 /*
225 * Currently running top-level transaction's virtual xid. Together these
226 * form a VirtualTransactionId, but we don't use that struct because this
227 * is not atomically assignable as whole, and we want to enforce code to
228 * consider both parts separately. See comments at VirtualTransactionId.
229 */
230 struct
231 {
232 ProcNumber procNumber; /* For regular backends, equal to
233 * GetNumberFromPGProc(proc). For prepared
234 * xacts, ID of the original backend that
235 * processed the transaction. For unused
236 * PGPROC entries, INVALID_PROC_NUMBER. */
237 LocalTransactionId lxid; /* local id of top-level transaction
238 * currently * being executed by this
239 * proc, if running; else
240 * InvalidLocalTransactionId */
241 } vxid;
242
243 TransactionId xid; /* id of top-level transaction currently being
244 * executed by this proc, if running and XID
245 * is assigned; else InvalidTransactionId.
246 * mirrored in ProcGlobal->xids[pgxactoff] */
247
248 TransactionId xmin; /* minimal running XID as it was when we were
249 * starting our xact, excluding LAZY VACUUM:
250 * vacuum must not remove tuples deleted by
251 * xid >= xmin ! */
252
253 XidCacheStatus subxidStatus; /* mirrored with
254 * ProcGlobal->subxidStates[i] */
255 struct XidCache subxids; /* cache for subtransaction XIDs */
256
257
258 /************************************************************************
259 * Inter-process signaling
260 ************************************************************************/
261
262 Latch procLatch; /* generic latch for process */
263
264 PGSemaphore sem; /* ONE semaphore to sleep on */
265
266 int delayChkptFlags; /* for DELAY_CHKPT_* flags */
267
268 /*
269 * While in hot standby mode, shows that a conflict signal has been sent
270 * for the current transaction. Set/cleared while holding ProcArrayLock,
271 * though not required. Accessed without lock, if needed.
272 *
273 * This is a bitmask; each bit corresponds to a RecoveryConflictReason
274 * enum value.
275 */
277
278 /************************************************************************
279 * LWLock waiting
280 ************************************************************************/
281
282 /*
283 * Info about LWLock the process is currently waiting for, if any.
284 *
285 * This is currently used both for lwlocks and buffer content locks, which
286 * is acceptable, although not pretty, because a backend can't wait for
287 * both types of locks at the same time.
288 */
289 uint8 lwWaiting; /* see LWLockWaitState */
290 uint8 lwWaitMode; /* lwlock mode being waited for */
291 proclist_node lwWaitLink; /* position in LW lock wait list */
292
293 /* Support for condition variables. */
294 proclist_node cvWaitLink; /* position in CV wait list */
295
296 /************************************************************************
297 * Lock manager data
298 ************************************************************************/
299
300 /*
301 * Support for lock groups. Use LockHashPartitionLockByProc on the group
302 * leader to get the LWLock protecting these fields.
303 */
304 PGPROC *lockGroupLeader; /* lock group leader, if I'm a member */
305 dlist_head lockGroupMembers; /* list of members, if I'm a leader */
306 dlist_node lockGroupLink; /* my member link, if I'm a member */
307
308 /* Info about lock the process is currently waiting for, if any. */
309 /* waitLock and waitProcLock are NULL if not currently waiting. */
310 LOCK *waitLock; /* Lock object we're sleeping on ... */
311 dlist_node waitLink; /* position in waitLock->waitProcs queue */
312 PROCLOCK *waitProcLock; /* Per-holder info for awaited lock */
313 LOCKMODE waitLockMode; /* type of lock we're waiting for */
314 LOCKMASK heldLocks; /* bitmask for lock types already held on this
315 * lock object by this backend */
316
317 pg_atomic_uint64 waitStart; /* time at which wait for lock acquisition
318 * started */
319
321
322 /*
323 * All PROCLOCK objects for locks held or awaited by this backend are
324 * linked into one of these lists, according to the partition number of
325 * their lock.
326 */
328
329 /*-- recording fast-path locks taken by this backend. --*/
330 LWLock fpInfoLock; /* protects per-backend fast-path state */
331 uint64 *fpLockBits; /* lock modes held for each fast-path slot */
332 Oid *fpRelId; /* slots for rel oids */
333 bool fpVXIDLock; /* are we holding a fast-path VXID lock? */
334 LocalTransactionId fpLocalTransactionId; /* lxid for fast-path VXID
335 * lock */
336
337 /************************************************************************
338 * Synchronous replication waiting
339 ************************************************************************/
340
341 /*
342 * Info to allow us to wait for synchronous replication, if needed.
343 * waitLSN is InvalidXLogRecPtr if not waiting; set only by user backend.
344 * syncRepState must not be touched except by owning process or WALSender.
345 * syncRepLinks used only while holding SyncRepLock.
346 */
347 XLogRecPtr waitLSN; /* waiting for this LSN or higher */
348 int syncRepState; /* wait state for sync rep */
349 dlist_node syncRepLinks; /* list link if process is in syncrep queue */
350
351 /************************************************************************
352 * Support for group XID clearing
353 ************************************************************************/
354
355 /* true, if member of ProcArray group waiting for XID clear */
357 /* next ProcArray group member waiting for XID clear */
359
360 /*
361 * latest transaction id among the transaction's main XID and
362 * subtransactions
363 */
365
366 /************************************************************************
367 * Support for group transaction status update
368 ************************************************************************/
369
370 bool clogGroupMember; /* true, if member of clog group */
371 pg_atomic_uint32 clogGroupNext; /* next clog group member */
372 TransactionId clogGroupMemberXid; /* transaction id of clog group member */
373 XidStatus clogGroupMemberXidStatus; /* transaction status of clog
374 * group member */
375 int64 clogGroupMemberPage; /* clog page corresponding to
376 * transaction id of clog group member */
377 XLogRecPtr clogGroupMemberLsn; /* WAL location of commit record for clog
378 * group member */
379
380 /************************************************************************
381 * Status reporting
382 ************************************************************************/
383
384 uint32 wait_event_info; /* proc's wait information */
385}
386PGPROC;
387
389
390/*
391 * There is one ProcGlobal struct for the whole database cluster.
392 *
393 * Adding/Removing an entry into the procarray requires holding *both*
394 * ProcArrayLock and XidGenLock in exclusive mode (in that order). Both are
395 * needed because the dense arrays (see below) are accessed from
396 * GetNewTransactionId() and GetSnapshotData(), and we don't want to add
397 * further contention by both using the same lock. Adding/Removing a procarray
398 * entry is much less frequent.
399 *
400 * Some fields in PGPROC are mirrored into more densely packed arrays (e.g.
401 * xids), with one entry for each backend. These arrays only contain entries
402 * for PGPROCs that have been added to the shared array with ProcArrayAdd()
403 * (in contrast to PGPROC array which has unused PGPROCs interspersed).
404 *
405 * The dense arrays are indexed by PGPROC->pgxactoff. Any concurrent
406 * ProcArrayAdd() / ProcArrayRemove() can lead to pgxactoff of a procarray
407 * member to change. Therefore it is only safe to use PGPROC->pgxactoff to
408 * access the dense array while holding either ProcArrayLock or XidGenLock.
409 *
410 * As long as a PGPROC is in the procarray, the mirrored values need to be
411 * maintained in both places in a coherent manner.
412 *
413 * The denser separate arrays are beneficial for three main reasons: First, to
414 * allow for as tight loops accessing the data as possible. Second, to prevent
415 * updates of frequently changing data (e.g. xmin) from invalidating
416 * cachelines also containing less frequently changing data (e.g. xid,
417 * statusFlags). Third to condense frequently accessed data into as few
418 * cachelines as possible.
419 *
420 * There are two main reasons to have the data mirrored between these dense
421 * arrays and PGPROC. First, as explained above, a PGPROC's array entries can
422 * only be accessed with either ProcArrayLock or XidGenLock held, whereas the
423 * PGPROC entries do not require that (obviously there may still be locking
424 * requirements around the individual field, separate from the concerns
425 * here). That is particularly important for a backend to efficiently checks
426 * it own values, which it often can safely do without locking. Second, the
427 * PGPROC fields allow to avoid unnecessary accesses and modification to the
428 * dense arrays. A backend's own PGPROC is more likely to be in a local cache,
429 * whereas the cachelines for the dense array will be modified by other
430 * backends (often removing it from the cache for other cores/sockets). At
431 * commit/abort time a check of the PGPROC value can avoid accessing/dirtying
432 * the corresponding array value.
433 *
434 * Basically it makes sense to access the PGPROC variable when checking a
435 * single backend's data, especially when already looking at the PGPROC for
436 * other reasons already. It makes sense to look at the "dense" arrays if we
437 * need to look at many / most entries, because we then benefit from the
438 * reduced indirection and better cross-process cache-ability.
439 *
440 * When entering a PGPROC for 2PC transactions with ProcArrayAdd(), the data
441 * in the dense arrays is initialized from the PGPROC while it already holds
442 * ProcArrayLock.
443 */
444typedef struct PROC_HDR
445{
446 /* Array of PGPROC structures (not including dummies for prepared txns) */
448
449 /* Array mirroring PGPROC.xid for each PGPROC currently in the procarray */
451
452 /*
453 * Array mirroring PGPROC.subxidStatus for each PGPROC currently in the
454 * procarray.
455 */
457
458 /*
459 * Array mirroring PGPROC.statusFlags for each PGPROC currently in the
460 * procarray.
461 */
463
464 /* Length of allProcs array */
466
467 /*
468 * This spinlock protects the below freelists of PGPROC structures. We
469 * cannot use an LWLock because the LWLock manager depends on already
470 * having a PGPROC and a wait semaphore! But these structures are touched
471 * relatively infrequently (only at backend startup or shutdown) and not
472 * for very long, so a spinlock is okay.
473 */
475
476 /* Head of list of free PGPROC structures */
478 /* Head of list of autovacuum & special worker free PGPROC structures */
480 /* Head of list of bgworker free PGPROC structures */
482 /* Head of list of walsender free PGPROC structures */
484
485 /* First pgproc waiting for group XID clear */
487 /* First pgproc waiting for group transaction status update */
489
490 /*
491 * Current slot numbers of some auxiliary processes. There can be only one
492 * of each of these running at a time.
493 */
496
497 /* Current shared estimate of appropriate spins_per_delay value */
498 int spins_per_delay;
499 /* Buffer id of the buffer that Startup process waits for pin on, or -1 */
501} PROC_HDR;
502
504
506
507/*
508 * Accessors for getting PGPROC given a ProcNumber and vice versa.
509 */
510#define GetPGProcByNumber(n) (&ProcGlobal->allProcs[(n)])
511#define GetNumberFromPGProc(proc) ((proc) - &ProcGlobal->allProcs[0])
512
513/*
514 * We set aside some extra PGPROC structures for "special worker" processes,
515 * which are full-fledged backends (they can run transactions)
516 * but are unique animals that there's never more than one of.
517 * Currently there are two such processes: the autovacuum launcher
518 * and the slotsync worker.
519 */
520#define NUM_SPECIAL_WORKER_PROCS 2
521
522/*
523 * We set aside some extra PGPROC structures for auxiliary processes,
524 * ie things that aren't full-fledged backends (they cannot run transactions
525 * or take heavyweight locks) but need shmem access.
526 *
527 * Background writer, checkpointer, WAL writer, WAL summarizer, and archiver
528 * run during normal operation. Startup process and WAL receiver also consume
529 * 2 slots, but WAL writer is launched only after startup has exited, so we
530 * only need 6 slots.
531 */
532#define MAX_IO_WORKERS 32
533#define NUM_AUXILIARY_PROCS (6 + MAX_IO_WORKERS)
534
535#define FIRST_PREPARED_XACT_PROC_NUMBER (MaxBackends + NUM_AUXILIARY_PROCS)
536
537/* configurable options */
540extern PGDLLIMPORT int LockTimeout;
544extern PGDLLIMPORT bool log_lock_waits;
545
546#ifdef EXEC_BACKEND
548#endif
549
550
551/*
552 * Function Prototypes
553 */
554extern int ProcGlobalSemas(void);
555extern void InitProcess(void);
556extern void InitProcessPhase2(void);
557extern void InitAuxiliaryProcess(void);
558
559extern void SetStartupBufferPinWaitBufId(int bufid);
560extern int GetStartupBufferPinWaitBufId(void);
561
562extern bool HaveNFreeProcs(int n, int *nfree);
563extern void ProcReleaseLocks(bool isCommit);
564
568extern void ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus);
570extern void CheckDeadLockAlert(void);
571extern void LockErrorCleanup(void);
575 int *lockHoldersNum);
576
577extern void ProcWaitForSignal(uint32 wait_event_info);
578extern void ProcSendSignal(ProcNumber procNumber);
579
580extern PGPROC *AuxiliaryPidGetProc(int pid);
581
582extern void BecomeLockGroupLeader(void);
583extern bool BecomeLockGroupMember(PGPROC *leader, int pid);
584
585#endif /* _PROC_H_ */
#define PGDLLIMPORT
Definition c.h:1421
uint8_t uint8
Definition c.h:622
int64_t int64
Definition c.h:621
uint64_t uint64
Definition c.h:625
uint32_t uint32
Definition c.h:624
uint32 LocalTransactionId
Definition c.h:738
uint32 TransactionId
Definition c.h:736
int XidStatus
Definition clog.h:25
int LOCKMODE
Definition lockdefs.h:26
int LOCKMASK
Definition lockdefs.h:25
#define NUM_LOCK_PARTITIONS
Definition lwlock.h:87
BackendType
Definition miscadmin.h:350
#define PG_CACHE_LINE_SIZE
unsigned int Oid
static int fb(int x)
ProcWaitStatus JoinWaitQueue(LOCALLOCK *locallock, LockMethod lockMethodTable, bool dontWait)
Definition proc.c:1146
void ProcSendSignal(ProcNumber procNumber)
Definition proc.c:2027
PGDLLIMPORT int IdleInTransactionSessionTimeout
Definition proc.c:65
void ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus)
Definition proc.c:1748
PGDLLIMPORT int IdleSessionTimeout
Definition proc.c:67
bool HaveNFreeProcs(int n, int *nfree)
Definition proc.c:787
void InitAuxiliaryProcess(void)
Definition proc.c:618
void GetLockHoldersAndWaiters(LOCALLOCK *locallock, StringInfo lock_holders_sbuf, StringInfo lock_waiters_sbuf, int *lockHoldersNum)
Definition proc.c:1941
PGDLLIMPORT PROC_HDR * ProcGlobal
Definition proc.c:74
int GetStartupBufferPinWaitBufId(void)
Definition proc.c:771
ProcWaitStatus ProcSleep(LOCALLOCK *locallock)
Definition proc.c:1315
PGDLLIMPORT PGPROC * MyProc
Definition proc.c:71
void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
Definition proc.c:1776
int ProcGlobalSemas(void)
Definition proc.c:130
void ProcReleaseLocks(bool isCommit)
Definition proc.c:896
void LockErrorCleanup(void)
Definition proc.c:818
bool BecomeLockGroupMember(PGPROC *leader, int pid)
Definition proc.c:2072
PGDLLIMPORT int StatementTimeout
Definition proc.c:63
void BecomeLockGroupLeader(void)
Definition proc.c:2042
PGDLLIMPORT int DeadlockTimeout
Definition proc.c:62
PGDLLIMPORT int LockTimeout
Definition proc.c:64
void InitProcess(void)
Definition proc.c:392
void CheckDeadLockAlert(void)
Definition proc.c:1914
void InitProcessPhase2(void)
Definition proc.c:583
PGDLLIMPORT bool log_lock_waits
Definition proc.c:68
ProcWaitStatus
Definition proc.h:144
@ PROC_WAIT_STATUS_OK
Definition proc.h:145
@ PROC_WAIT_STATUS_WAITING
Definition proc.h:146
@ PROC_WAIT_STATUS_ERROR
Definition proc.h:147
PGDLLIMPORT PGPROC * PreparedXactProcs
Definition proc.c:78
PGDLLIMPORT int TransactionTimeout
Definition proc.c:66
PGPROC * AuxiliaryPidGetProc(int pid)
Definition proc.c:1097
void SetStartupBufferPinWaitBufId(int bufid)
Definition proc.c:759
void ProcWaitForSignal(uint32 wait_event_info)
Definition proc.c:2015
int ProcNumber
Definition procnumber.h:24
NON_EXEC_STATIC PGPROC * AuxiliaryProcs
Definition proc.c:77
Definition lock.h:140
Definition latch.h:116
Definition proc.h:179
LWLock fpInfoLock
Definition proc.h:324
TransactionId xmin
Definition proc.h:242
bool procArrayGroupMember
Definition proc.h:350
LocalTransactionId lxid
Definition proc.h:231
PROCLOCK * waitProcLock
Definition proc.h:306
dlist_node freeProcsLink
Definition proc.h:186
XLogRecPtr clogGroupMemberLsn
Definition proc.h:371
pg_atomic_uint32 procArrayGroupNext
Definition proc.h:352
uint8 lwWaitMode
Definition proc.h:284
dlist_head lockGroupMembers
Definition proc.h:299
uint32 wait_event_info
Definition proc.h:378
dlist_head * procgloballist
Definition proc.h:185
Oid * fpRelId
Definition proc.h:326
BackendType backendType
Definition proc.h:198
uint8 statusFlags
Definition proc.h:210
TransactionId clogGroupMemberXid
Definition proc.h:366
Oid databaseId
Definition proc.h:201
int64 clogGroupMemberPage
Definition proc.h:369
bool clogGroupMember
Definition proc.h:364
uint64 * fpLockBits
Definition proc.h:325
pg_atomic_uint64 waitStart
Definition proc.h:311
bool fpVXIDLock
Definition proc.h:327
ProcNumber procNumber
Definition proc.h:226
int pid
Definition proc.h:197
XLogRecPtr waitLSN
Definition proc.h:341
dlist_node syncRepLinks
Definition proc.h:343
struct PGPROC::@136 vxid
int syncRepState
Definition proc.h:342
pg_atomic_uint32 clogGroupNext
Definition proc.h:365
dlist_node lockGroupLink
Definition proc.h:300
XidStatus clogGroupMemberXidStatus
Definition proc.h:367
int pgxactoff
Definition proc.h:207
XidCacheStatus subxidStatus
Definition proc.h:247
LOCK * waitLock
Definition proc.h:304
proclist_node lwWaitLink
Definition proc.h:285
TransactionId xid
Definition proc.h:237
LOCKMODE waitLockMode
Definition proc.h:307
struct XidCache subxids
Definition proc.h:249
int delayChkptFlags
Definition proc.h:260
dlist_node waitLink
Definition proc.h:305
PGPROC * lockGroupLeader
Definition proc.h:298
pg_atomic_uint32 pendingRecoveryConflicts
Definition proc.h:270
LocalTransactionId fpLocalTransactionId
Definition proc.h:328
TransactionId procArrayGroupMemberXid
Definition proc.h:358
LOCKMASK heldLocks
Definition proc.h:308
PGSemaphore sem
Definition proc.h:258
dlist_head myProcLocks[NUM_LOCK_PARTITIONS]
Definition proc.h:321
Oid roleId
Definition proc.h:202
ProcWaitStatus waitStatus
Definition proc.h:314
proclist_node cvWaitLink
Definition proc.h:288
Oid tempNamespaceId
Definition proc.h:204
uint8 lwWaiting
Definition proc.h:283
Latch procLatch
Definition proc.h:256
uint8 * statusFlags
Definition proc.h:456
XidCacheStatus * subxidStates
Definition proc.h:450
dlist_head autovacFreeProcs
Definition proc.h:473
dlist_head freeProcs
Definition proc.h:471
ProcNumber checkpointerProc
Definition proc.h:489
slock_t freeProcsLock
Definition proc.h:468
int startupBufferPinWaitBufId
Definition proc.h:494
PGPROC * allProcs
Definition proc.h:441
pg_atomic_uint32 clogGroupFirst
Definition proc.h:482
int spins_per_delay
Definition proc.h:492
TransactionId * xids
Definition proc.h:444
dlist_head walsenderFreeProcs
Definition proc.h:477
dlist_head bgworkerFreeProcs
Definition proc.h:475
ProcNumber walwriterProc
Definition proc.h:488
pg_atomic_uint32 procArrayGroupFirst
Definition proc.h:480
uint32 allProcCount
Definition proc.h:459
uint64 XLogRecPtr
Definition xlogdefs.h:21

◆ FIRST_PREPARED_XACT_PROC_NUMBER

#define FIRST_PREPARED_XACT_PROC_NUMBER   (MaxBackends + NUM_AUXILIARY_PROCS)

Definition at line 529 of file proc.h.

◆ FP_LOCK_GROUPS_PER_BACKEND_MAX

#define FP_LOCK_GROUPS_PER_BACKEND_MAX   1024

Definition at line 95 of file proc.h.

◆ FP_LOCK_SLOTS_PER_GROUP

#define FP_LOCK_SLOTS_PER_GROUP   16 /* don't change */

Definition at line 96 of file proc.h.

◆ GetNumberFromPGProc

#define GetNumberFromPGProc (   proc)    ((proc) - &ProcGlobal->allProcs[0])

Definition at line 505 of file proc.h.

◆ GetPGProcByNumber

#define GetPGProcByNumber (   n)    (&ProcGlobal->allProcs[(n)])

Definition at line 504 of file proc.h.

◆ MAX_IO_WORKERS

#define MAX_IO_WORKERS   32

Definition at line 526 of file proc.h.

◆ NUM_AUXILIARY_PROCS

#define NUM_AUXILIARY_PROCS   (6 + MAX_IO_WORKERS)

Definition at line 527 of file proc.h.

◆ NUM_SPECIAL_WORKER_PROCS

#define NUM_SPECIAL_WORKER_PROCS   2

Definition at line 514 of file proc.h.

◆ PGPROC_MAX_CACHED_SUBXIDS

#define PGPROC_MAX_CACHED_SUBXIDS   64 /* XXX guessed-at value */

Definition at line 43 of file proc.h.

◆ PROC_AFFECTS_ALL_HORIZONS

#define PROC_AFFECTS_ALL_HORIZONS
Value:
0x20 /* this proc's xmin must be
* included in vacuum horizons
* in all databases */

Definition at line 66 of file proc.h.

◆ PROC_IN_LOGICAL_DECODING

#define PROC_IN_LOGICAL_DECODING
Value:
0x10 /* currently doing logical
* decoding outside xact */

Definition at line 65 of file proc.h.

◆ PROC_IN_SAFE_IC

#define PROC_IN_SAFE_IC
Value:
0x04 /* currently running CREATE INDEX
* CONCURRENTLY or REINDEX
* CONCURRENTLY on non-expressional,
* non-partial index */

Definition at line 63 of file proc.h.

◆ PROC_IN_VACUUM

#define PROC_IN_VACUUM   0x02 /* currently running lazy vacuum */

Definition at line 62 of file proc.h.

◆ PROC_IS_AUTOVACUUM

#define PROC_IS_AUTOVACUUM   0x01 /* is it an autovac worker? */

Definition at line 61 of file proc.h.

◆ PROC_VACUUM_FOR_WRAPAROUND

#define PROC_VACUUM_FOR_WRAPAROUND   0x08 /* set by autovac only */

Definition at line 64 of file proc.h.

◆ PROC_VACUUM_STATE_MASK

#define PROC_VACUUM_STATE_MASK    (PROC_IN_VACUUM | PROC_IN_SAFE_IC | PROC_VACUUM_FOR_WRAPAROUND)

Definition at line 69 of file proc.h.

◆ PROC_XMIN_FLAGS

#define PROC_XMIN_FLAGS   (PROC_IN_VACUUM | PROC_IN_SAFE_IC)

Definition at line 76 of file proc.h.

Typedef Documentation

◆ PGPROC

◆ PROC_HDR

◆ XidCacheStatus

◆ XidStatus

Definition at line 28 of file proc.h.

Enumeration Type Documentation

◆ ProcWaitStatus

Enumerator
PROC_WAIT_STATUS_OK 
PROC_WAIT_STATUS_WAITING 
PROC_WAIT_STATUS_ERROR 

Definition at line 143 of file proc.h.

Function Documentation

◆ AuxiliaryPidGetProc()

PGPROC * AuxiliaryPidGetProc ( int  pid)
extern

Definition at line 1097 of file proc.c.

1098{
1099 PGPROC *result = NULL;
1100 int index;
1101
1102 if (pid == 0) /* never match dummy PGPROCs */
1103 return NULL;
1104
1105 for (index = 0; index < NUM_AUXILIARY_PROCS; index++)
1106 {
1107 PGPROC *proc = &AuxiliaryProcs[index];
1108
1109 if (proc->pid == pid)
1110 {
1111 result = proc;
1112 break;
1113 }
1114 }
1115 return result;
1116}
uint32 result
#define NUM_AUXILIARY_PROCS
Definition proc.h:527
Definition type.h:96

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

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().

◆ BecomeLockGroupLeader()

void BecomeLockGroupLeader ( void  )
extern

Definition at line 2042 of file proc.c.

2043{
2045
2046 /* If we already did it, we don't need to do it again. */
2048 return;
2049
2050 /* We had better not be a follower. */
2052
2053 /* Create single-member group, containing only ourselves. */
2059}
#define Assert(condition)
Definition c.h:943
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition ilist.h:347
#define LockHashPartitionLockByProc(leader_pgproc)
Definition lock.h:372
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_EXCLUSIVE
Definition lwlock.h:104
PGPROC * MyProc
Definition proc.c:71

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

Referenced by LaunchParallelWorkers(), and rebuild_relation().

◆ BecomeLockGroupMember()

bool BecomeLockGroupMember ( PGPROC leader,
int  pid 
)
extern

Definition at line 2072 of file proc.c.

2073{
2075 bool ok = false;
2076
2077 /* Group leader can't become member of group */
2078 Assert(MyProc != leader);
2079
2080 /* Can't already be a member of a group */
2082
2083 /* PID must be valid. */
2084 Assert(pid != 0);
2085
2086 /*
2087 * Get lock protecting the group fields. Note LockHashPartitionLockByProc
2088 * calculates the proc number based on the PGPROC slot without looking at
2089 * its contents, so we will acquire the correct lock even if the leader
2090 * PGPROC is in process of being recycled.
2091 */
2094
2095 /* Is this the leader we're looking for? */
2096 if (leader->pid == pid && leader->lockGroupLeader == leader)
2097 {
2098 /* OK, join the group */
2099 ok = true;
2100 MyProc->lockGroupLeader = leader;
2102 }
2104
2105 return ok;
2106}
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(), and RepackWorkerMain().

◆ CheckDeadLockAlert()

void CheckDeadLockAlert ( void  )
extern

Definition at line 1914 of file proc.c.

1915{
1916 int save_errno = errno;
1917
1918 got_deadlock_timeout = true;
1919
1920 /*
1921 * Have to set the latch again, even if handle_sig_alarm already did. Back
1922 * then got_deadlock_timeout wasn't yet set... It's unlikely that this
1923 * ever would be a problem, but setting a set latch again is cheap.
1924 *
1925 * Note that, when this function runs inside procsignal_sigusr1_handler(),
1926 * the handler function sets the latch again after the latch is set here.
1927 */
1929 errno = save_errno;
1930}
struct Latch * MyLatch
Definition globals.c:65
void SetLatch(Latch *latch)
Definition latch.c:290
static volatile sig_atomic_t got_deadlock_timeout
Definition proc.c:93

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

Referenced by InitPostgres(), and ProcessRecoveryConflictInterrupt().

◆ GetLockHoldersAndWaiters()

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

Definition at line 1941 of file proc.c.

1943{
1946 LOCK *lock = locallock->lock;
1947 bool first_holder = true,
1948 first_waiter = true;
1949
1950#ifdef USE_ASSERT_CHECKING
1951 {
1952 uint32 hashcode = locallock->hashcode;
1954
1956 }
1957#endif
1958
1959 *lockHoldersNum = 0;
1960
1961 /*
1962 * Loop over the lock's procLocks to gather a list of all holders and
1963 * waiters. Thus we will be able to provide more detailed information for
1964 * lock debugging purposes.
1965 *
1966 * lock->procLocks contains all processes which hold or wait for this
1967 * lock.
1968 */
1970 {
1971 curproclock =
1972 dlist_container(PROCLOCK, lockLink, proc_iter.cur);
1973
1974 /*
1975 * We are a waiter if myProc->waitProcLock == curproclock; we are a
1976 * holder if it is NULL or something different.
1977 */
1978 if (curproclock->tag.myProc->waitProcLock == curproclock)
1979 {
1980 if (first_waiter)
1981 {
1983 curproclock->tag.myProc->pid);
1984 first_waiter = false;
1985 }
1986 else
1988 curproclock->tag.myProc->pid);
1989 }
1990 else
1991 {
1992 if (first_holder)
1993 {
1995 curproclock->tag.myProc->pid);
1996 first_holder = false;
1997 }
1998 else
2000 curproclock->tag.myProc->pid);
2001
2002 (*lockHoldersNum)++;
2003 }
2004 }
2005}
#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:357
bool LWLockHeldByMe(LWLock *lock)
Definition lwlock.c:1885
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
dlist_head procLocks
Definition lock.h:147

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

Referenced by LockAcquireExtended(), and ProcSleep().

◆ GetStartupBufferPinWaitBufId()

int GetStartupBufferPinWaitBufId ( void  )
extern

Definition at line 771 of file proc.c.

772{
773 /* use volatile pointer to prevent code rearrangement */
774 volatile PROC_HDR *procglobal = ProcGlobal;
775
777}
PROC_HDR * ProcGlobal
Definition proc.c:74

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

Referenced by HoldingBufferPinThatDelaysRecovery().

◆ HaveNFreeProcs()

bool HaveNFreeProcs ( int  n,
int nfree 
)
extern

Definition at line 787 of file proc.c.

788{
789 dlist_iter iter;
790
791 Assert(n > 0);
792 Assert(nfree);
793
795
796 *nfree = 0;
798 {
799 (*nfree)++;
800 if (*nfree == n)
801 break;
802 }
803
805
806 return (*nfree == n);
807}
static void SpinLockRelease(volatile slock_t *lock)
Definition spin.h:62
static void SpinLockAcquire(volatile slock_t *lock)
Definition spin.h:56

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

Referenced by InitPostgres().

◆ InitAuxiliaryProcess()

void InitAuxiliaryProcess ( void  )
extern

Definition at line 618 of file proc.c.

619{
621 int proctype;
622
623 /*
624 * ProcGlobal should be set up already (if we are a backend, we inherit
625 * this by fork() or EXEC_BACKEND mechanism from the postmaster).
626 */
627 if (ProcGlobal == NULL || AuxiliaryProcs == NULL)
628 elog(PANIC, "proc header uninitialized");
629
630 if (MyProc != NULL)
631 elog(ERROR, "you already exist");
632
635
636 /*
637 * We use the freeProcsLock to protect assignment and releasing of
638 * AuxiliaryProcs entries.
639 *
640 * While we are holding the spinlock, also copy the current shared
641 * estimate of spins_per_delay to local storage.
642 */
644
646
647 /*
648 * Find a free auxproc ... *big* trouble if there isn't one ...
649 */
651 {
653 if (auxproc->pid == 0)
654 break;
655 }
657 {
659 elog(FATAL, "all AuxiliaryProcs are in use");
660 }
661
662 /* Mark auxiliary proc as in use by me */
663 /* use volatile pointer to prevent code rearrangement */
664 ((volatile PGPROC *) auxproc)->pid = MyProcPid;
665
667
668 MyProc = auxproc;
670
671 /*
672 * Initialize all fields of MyProc, except for those previously
673 * initialized by ProcGlobalShmemInit.
674 */
677 MyProc->fpVXIDLock = false;
688 MyProc->statusFlags = 0;
690 MyProc->lwWaitMode = 0;
695#ifdef USE_ASSERT_CHECKING
696 {
697 int i;
698
699 /* Last process should have released all locks. */
700 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
702 }
703#endif
705
706 /*
707 * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
708 * on it. That allows us to repoint the process latch, which so far
709 * points to process local one, to the shared one.
710 */
713
714 /* now that we have a proc, report wait events to shared memory */
716
717 /* Check that group locking fields are in a proper initial state. */
720
721 /*
722 * We might be reusing a semaphore that belonged to a failed process. So
723 * be careful and reinitialize its value here. (This is not strictly
724 * necessary anymore, but seems like a good idea for cleanliness.)
725 */
727
728 /*
729 * Arrange to clean up at process exit.
730 */
732
733 /*
734 * Now that we have a PGPROC, we could try to acquire lightweight locks.
735 * Initialize local state needed for them. (Heavyweight locks cannot be
736 * acquired in aux processes.)
737 */
739
740#ifdef EXEC_BACKEND
741
742 /*
743 * Initialize backend-local pointers to all the shared data structures.
744 * (We couldn't do this until now because it needs LWLocks.)
745 */
748#endif
749}
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:42
#define PANIC
Definition elog.h:44
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
int MyProcPid
Definition globals.c:49
ProcNumber MyProcNumber
Definition globals.c:92
bool IsUnderPostmaster
Definition globals.c:122
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
int i
Definition isn.c:77
void OwnLatch(Latch *latch)
Definition latch.c:126
#define InvalidLocalTransactionId
Definition lock.h:68
void InitLWLockAccess(void)
Definition lwlock.c:506
@ 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:289
void PGSemaphoreReset(PGSemaphore sema)
Definition posix_sema.c:288
static Datum Int32GetDatum(int32 X)
Definition postgres.h:212
#define InvalidOid
#define GetNumberFromPGProc(proc)
Definition proc.h:505
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
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:1046
#define InvalidTransactionId
Definition transam.h:31
void pgstat_set_wait_event_storage(uint32 *wait_event_info)
Definition wait_event.c:335

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  )
extern

Definition at line 392 of file proc.c.

393{
394 dlist_head *procgloballist;
395
396 /*
397 * ProcGlobal should be set up already (if we are a backend, we inherit
398 * this by fork() or EXEC_BACKEND mechanism from the postmaster).
399 */
400 if (ProcGlobal == NULL)
401 elog(PANIC, "proc header uninitialized");
402
403 if (MyProc != NULL)
404 elog(ERROR, "you already exist");
405
406 /*
407 * Before we start accessing the shared memory in a serious way, mark
408 * ourselves as an active postmaster child; this is so that the postmaster
409 * can detect it if we exit without cleaning up.
410 */
413
414 /*
415 * Decide which list should supply our PGPROC. This logic must match the
416 * way the freelists were constructed in ProcGlobalShmemInit().
417 */
419 procgloballist = &ProcGlobal->autovacFreeProcs;
420 else if (AmBackgroundWorkerProcess())
421 procgloballist = &ProcGlobal->bgworkerFreeProcs;
422 else if (AmWalSenderProcess())
423 procgloballist = &ProcGlobal->walsenderFreeProcs;
424 else
425 procgloballist = &ProcGlobal->freeProcs;
426
427 /*
428 * Try to get a proc struct from the appropriate free list. If this
429 * fails, we must be out of PGPROC structures (not to mention semaphores).
430 *
431 * While we are holding the spinlock, also copy the current shared
432 * estimate of spins_per_delay to local storage.
433 */
435
437
438 if (!dlist_is_empty(procgloballist))
439 {
440 MyProc = dlist_container(PGPROC, freeProcsLink, dlist_pop_head_node(procgloballist));
442 }
443 else
444 {
445 /*
446 * If we reach here, all the PGPROCs are in use. This is one of the
447 * possible places to detect "too many backends", so give the standard
448 * error message. XXX do we need to give a different failure message
449 * in the autovacuum case?
450 */
452 if (AmWalSenderProcess())
455 errmsg("number of requested standby connections exceeds \"max_wal_senders\" (currently %d)",
459 errmsg("sorry, too many clients already")));
460 }
462
463 /*
464 * Cross-check that the PGPROC is of the type we expect; if this were not
465 * the case, it would get returned to the wrong list.
466 */
467 Assert(MyProc->procgloballist == procgloballist);
468
469 /*
470 * Initialize all fields of MyProc, except for those previously
471 * initialized by ProcGlobalShmemInit.
472 */
475 MyProc->fpVXIDLock = false;
482 /* databaseId and roleId will be filled in later */
488 MyProc->statusFlags = 0;
489 /* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */
493 MyProc->lwWaitMode = 0;
498#ifdef USE_ASSERT_CHECKING
499 {
500 int i;
501
502 /* Last process should have released all locks. */
503 for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
505 }
506#endif
508
509 /* Initialize fields for sync rep */
513
514 /* Initialize fields for group XID clearing. */
518
519 /* Check that group locking fields are in a proper initial state. */
522
523 /* Initialize wait event information. */
525
526 /* Initialize fields for group transaction status update. */
527 MyProc->clogGroupMember = false;
533
534 /*
535 * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
536 * on it. That allows us to repoint the process latch, which so far
537 * points to process local one, to the shared one.
538 */
541
542 /* now that we have a proc, report wait events to shared memory */
544
545 /*
546 * We might be reusing a semaphore that belonged to a failed process. So
547 * be careful and reinitialize its value here. (This is not strictly
548 * necessary anymore, but seems like a good idea for cleanliness.)
549 */
551
552 /*
553 * Arrange to clean up at backend exit.
554 */
556
557 /*
558 * Now that we have a PGPROC, we could try to acquire locks, so initialize
559 * local state needed for LWLocks, and the deadlock checker.
560 */
563
564#ifdef EXEC_BACKEND
565
566 /*
567 * Initialize backend-local pointers to all the shared data structures.
568 * (We couldn't do this until now because it needs LWLocks.)
569 */
572#endif
573}
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:152
static dlist_node * dlist_pop_head_node(dlist_head *head)
Definition ilist.h:450
#define AmAutoVacuumWorkerProcess()
Definition miscadmin.h:398
#define AmBackgroundWorkerProcess()
Definition miscadmin.h:399
#define AmWalSenderProcess()
Definition miscadmin.h:400
#define AmSpecialWorkerProcess()
Definition miscadmin.h:414
static char * errmsg
#define PROC_IS_AUTOVACUUM
Definition proc.h:61
static void ProcKill(int code, Datum arg)
Definition proc.c:924
#define SYNC_REP_NOT_WAITING
Definition syncrep.h:30
int max_wal_senders
Definition walsender.c:141
#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  )
extern

Definition at line 583 of file proc.c.

584{
585 Assert(MyProc != NULL);
586
587 /*
588 * Add our PGPROC to the PGPROC array in shared memory.
589 */
591
592 /*
593 * Arrange to clean that up at backend exit.
594 */
596}
void ProcArrayAdd(PGPROC *proc)
Definition procarray.c:464
static void RemoveProcFromArray(int code, Datum arg)
Definition proc.c:913

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

Referenced by InitPostgres().

◆ JoinWaitQueue()

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

Definition at line 1146 of file proc.c.

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

Definition at line 818 of file proc.c.

819{
823
825
827
828 /* Nothing to do if we weren't waiting for a lock */
830 if (lockAwaited == NULL)
831 {
833 return;
834 }
835
836 /*
837 * Turn off the deadlock and lock timeout timers, if they are still
838 * running (see ProcSleep). Note we must preserve the LOCK_TIMEOUT
839 * indicator flag, since this function is executed before
840 * ProcessInterrupts when responding to SIGINT; else we'd lose the
841 * knowledge that the SIGINT came from a lock timeout and not an external
842 * source.
843 */
845 timeouts[0].keep_indicator = false;
846 timeouts[1].id = LOCK_TIMEOUT;
847 timeouts[1].keep_indicator = true;
849
850 /* Unlink myself from the wait queue, if on it (might not be anymore!) */
853
855 {
856 /* We could not have been granted the lock yet */
858 }
859 else
860 {
861 /*
862 * Somebody kicked us off the lock queue already. Perhaps they
863 * granted us the lock, or perhaps they detected a deadlock. If they
864 * did grant us the lock, we'd better remember it in our local lock
865 * table.
866 */
869 }
870
872
874
876}
static bool dlist_node_is_detached(const dlist_node *node)
Definition ilist.h:525
void GrantAwaitedLock(void)
Definition lock.c:1898
void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode)
Definition lock.c:2055
void ResetAwaitedLock(void)
Definition lock.c:1916
void AbortStrongLockAcquire(void)
Definition lock.c:1869
LOCALLOCK * GetAwaitedLock(void)
Definition lock.c:1907
#define RESUME_INTERRUPTS()
Definition miscadmin.h:138
#define HOLD_INTERRUPTS()
Definition miscadmin.h:136
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().

◆ ProcGlobalSemas()

int ProcGlobalSemas ( void  )
extern

Definition at line 130 of file proc.c.

131{
132 /*
133 * We need a sema per backend (including autovacuum), plus one for each
134 * auxiliary process.
135 */
137}
int MaxBackends
Definition globals.c:149

References MaxBackends, and NUM_AUXILIARY_PROCS.

Referenced by InitializeShmemGUCs(), ProcGlobalShmemInit(), and ProcGlobalShmemRequest().

◆ ProcLockWakeup()

void ProcLockWakeup ( LockMethod  lockMethodTable,
LOCK lock 
)
extern

Definition at line 1776 of file proc.c.

1777{
1781
1783 return;
1784
1786 {
1787 PGPROC *proc = dlist_container(PGPROC, waitLink, miter.cur);
1788 LOCKMODE lockmode = proc->waitLockMode;
1789
1790 /*
1791 * Waken if (a) doesn't conflict with requests of earlier waiters, and
1792 * (b) doesn't conflict with already-held locks.
1793 */
1794 if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
1795 !LockCheckConflicts(lockMethodTable, lockmode, lock,
1796 proc->waitProcLock))
1797 {
1798 /* OK to waken */
1799 GrantLock(lock, proc->waitProcLock, lockmode);
1800 /* removes proc from the lock's waiting process queue */
1802 }
1803 else
1804 {
1805 /*
1806 * Lock conflicts: Don't wake, but remember requested mode for
1807 * later checks.
1808 */
1809 aheadRequests |= LOCKBIT_ON(lockmode);
1810 }
1811 }
1812}
#define dclist_foreach_modify(iter, lhead)
Definition ilist.h:973
void ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus)
Definition proc.c:1748

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)
extern

Definition at line 896 of file proc.c.

897{
898 if (!MyProc)
899 return;
900 /* If waiting, get off wait queue (should only be needed after error) */
902 /* Release standard locks, including session-level if aborting */
904 /* Release transaction-level advisory locks */
906}
void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
Definition lock.c:2316
#define DEFAULT_LOCKMETHOD
Definition locktag.h:25
#define USER_LOCKMETHOD
Definition locktag.h:26
void LockErrorCleanup(void)
Definition proc.c:818

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

Referenced by ResourceOwnerReleaseInternal().

◆ ProcSendSignal()

void ProcSendSignal ( ProcNumber  procNumber)
extern

Definition at line 2027 of file proc.c.

2028{
2030 elog(ERROR, "procNumber out of range");
2031
2032 SetLatch(&GetPGProcByNumber(procNumber)->procLatch);
2033}
#define GetPGProcByNumber(n)
Definition proc.h:504

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

Referenced by ReleasePredicateLocks(), and WakePinCountWaiter().

◆ ProcSleep()

ProcWaitStatus ProcSleep ( LOCALLOCK locallock)
extern

Definition at line 1315 of file proc.c.

1316{
1317 LOCKMODE lockmode = locallock->tag.mode;
1318 LOCK *lock = locallock->lock;
1319 uint32 hashcode = locallock->hashcode;
1322 bool allow_autovacuum_cancel = true;
1323 bool logged_recovery_conflict = false;
1324 bool logged_lock_wait = false;
1327
1328 /* The caller must've armed the on-error cleanup mechanism */
1331
1332 /*
1333 * Now that we will successfully clean up after an ereport, it's safe to
1334 * check to see if there's a buffer pin deadlock against the Startup
1335 * process. Of course, that's only necessary if we're doing Hot Standby
1336 * and are not the Startup process ourselves.
1337 */
1340
1341 /* Reset deadlock_state before enabling the timeout handler */
1343 got_deadlock_timeout = false;
1344
1345 /*
1346 * Set timer so we can wake up after awhile and check for a deadlock. If a
1347 * deadlock is detected, the handler sets MyProc->waitStatus =
1348 * PROC_WAIT_STATUS_ERROR, allowing us to know that we must report failure
1349 * rather than success.
1350 *
1351 * By delaying the check until we've waited for a bit, we can avoid
1352 * running the rather expensive deadlock-check code in most cases.
1353 *
1354 * If LockTimeout is set, also enable the timeout for that. We can save a
1355 * few cycles by enabling both timeout sources in one call.
1356 *
1357 * If InHotStandby we set lock waits slightly later for clarity with other
1358 * code.
1359 */
1360 if (!InHotStandby)
1361 {
1362 if (LockTimeout > 0)
1363 {
1365
1367 timeouts[0].type = TMPARAM_AFTER;
1368 timeouts[0].delay_ms = DeadlockTimeout;
1369 timeouts[1].id = LOCK_TIMEOUT;
1370 timeouts[1].type = TMPARAM_AFTER;
1371 timeouts[1].delay_ms = LockTimeout;
1373 }
1374 else
1376
1377 /*
1378 * Use the current time obtained for the deadlock timeout timer as
1379 * waitStart (i.e., the time when this process started waiting for the
1380 * lock). Since getting the current time newly can cause overhead, we
1381 * reuse the already-obtained time to avoid that overhead.
1382 *
1383 * Note that waitStart is updated without holding the lock table's
1384 * partition lock, to avoid the overhead by additional lock
1385 * acquisition. This can cause "waitstart" in pg_locks to become NULL
1386 * for a very short period of time after the wait started even though
1387 * "granted" is false. This is OK in practice because we can assume
1388 * that users are likely to look at "waitstart" when waiting for the
1389 * lock for a long time.
1390 */
1393 }
1395 {
1396 /*
1397 * Set the wait start timestamp if logging is enabled and in hot
1398 * standby.
1399 */
1401 }
1402
1403 /*
1404 * If somebody wakes us between LWLockRelease and WaitLatch, the latch
1405 * will not wait. But a set latch does not necessarily mean that the lock
1406 * is free now, as there are many other sources for latch sets than
1407 * somebody releasing the lock.
1408 *
1409 * We process interrupts whenever the latch has been set, so cancel/die
1410 * interrupts are processed quickly. This means we must not mind losing
1411 * control to a cancel/die interrupt here. We don't, because we have no
1412 * shared-state-change work to do after being granted the lock (the
1413 * grantor did it all). We do have to worry about canceling the deadlock
1414 * timeout and updating the locallock table, but if we lose control to an
1415 * error, LockErrorCleanup will fix that up.
1416 */
1417 do
1418 {
1419 if (InHotStandby)
1420 {
1421 bool maybe_log_conflict =
1423
1424 /* Set a timer and wait for that or for the lock to be granted */
1427
1428 /*
1429 * Emit the log message if the startup process is waiting longer
1430 * than deadlock_timeout for recovery conflict on lock.
1431 */
1433 {
1435
1438 {
1440 int cnt;
1441
1442 vxids = GetLockConflicts(&locallock->tag.lock,
1443 AccessExclusiveLock, &cnt);
1444
1445 /*
1446 * Log the recovery conflict and the list of PIDs of
1447 * backends holding the conflicting lock. Note that we do
1448 * logging even if there are no such backends right now
1449 * because the startup process here has already waited
1450 * longer than deadlock_timeout.
1451 */
1454 cnt > 0 ? vxids : NULL, true);
1456 }
1457 }
1458 }
1459 else
1460 {
1462 PG_WAIT_LOCK | locallock->tag.lock.locktag_type);
1464 /* check for deadlocks first, as that's probably log-worthy */
1466 {
1468 got_deadlock_timeout = false;
1469 }
1471 }
1472
1473 /*
1474 * waitStatus could change from PROC_WAIT_STATUS_WAITING to something
1475 * else asynchronously. Read it just once per loop to prevent
1476 * surprising behavior (such as missing log messages).
1477 */
1478 myWaitStatus = *((volatile ProcWaitStatus *) &MyProc->waitStatus);
1479
1480 /*
1481 * If we are not deadlocked, but are waiting on an autovacuum-induced
1482 * task, send a signal to interrupt it.
1483 */
1485 {
1487 uint8 statusFlags;
1490
1491 /*
1492 * Grab info we need, then release lock immediately. Note this
1493 * coding means that there is a tiny chance that the process
1494 * terminates its current transaction and starts a different one
1495 * before we have a change to send the signal; the worst possible
1496 * consequence is that a for-wraparound vacuum is canceled. But
1497 * that could happen in any case unless we were to do kill() with
1498 * the lock held, which is much more undesirable.
1499 */
1501 statusFlags = ProcGlobal->statusFlags[autovac->pgxactoff];
1503 locktag_copy = lock->tag;
1505
1506 /*
1507 * Only do it if the worker is not working to protect against Xid
1508 * wraparound.
1509 */
1510 if ((statusFlags & PROC_IS_AUTOVACUUM) &&
1511 !(statusFlags & PROC_VACUUM_FOR_WRAPAROUND))
1512 {
1513 int pid = autovac->pid;
1514
1515 /* report the case, if configured to do so */
1517 {
1519 StringInfoData logbuf; /* errdetail for server log */
1520
1525 "Process %d waits for %s on %s.",
1526 MyProcPid,
1528 locktagbuf.data);
1529
1531 (errmsg_internal("sending cancel to blocking autovacuum PID %d",
1532 pid),
1533 errdetail_log("%s", logbuf.data)));
1534
1535 pfree(locktagbuf.data);
1536 pfree(logbuf.data);
1537 }
1538
1539 /* send the autovacuum worker Back to Old Kent Road */
1540 if (kill(pid, SIGINT) < 0)
1541 {
1542 /*
1543 * There's a race condition here: once we release the
1544 * ProcArrayLock, it's possible for the autovac worker to
1545 * close up shop and exit before we can do the kill().
1546 * Therefore, we do not whinge about no-such-process.
1547 * Other errors such as EPERM could conceivably happen if
1548 * the kernel recycles the PID fast enough, but such cases
1549 * seem improbable enough that it's probably best to issue
1550 * a warning if we see some other errno.
1551 */
1552 if (errno != ESRCH)
1554 (errmsg("could not send signal to process %d: %m",
1555 pid)));
1556 }
1557 }
1558
1559 /* prevent signal from being sent again more than once */
1561 }
1562
1563 /*
1564 * If awoken after the deadlock check interrupt has run, increment the
1565 * lock statistics counters and if log_lock_waits is on, then report
1566 * about the wait.
1567 */
1569 {
1570 long secs;
1571 int usecs;
1572 long msecs;
1573
1574 INJECTION_POINT("deadlock-timeout-fired", NULL);
1577 &secs, &usecs);
1578 msecs = secs * 1000 + usecs / 1000;
1579 usecs = usecs % 1000;
1580
1581 /* Increment the lock statistics counters if done waiting. */
1583 pgstat_count_lock_waits(locallock->tag.lock.locktag_type, msecs);
1584
1585 if (log_lock_waits)
1586 {
1590 const char *modename;
1591 int lockHoldersNum = 0;
1592
1596
1597 DescribeLockTag(&buf, &locallock->tag.lock);
1598 modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
1599 lockmode);
1600
1601 /* Gather a list of all lock holders and waiters */
1606
1608 ereport(LOG,
1609 (errmsg("process %d avoided deadlock for %s on %s by rearranging queue order after %ld.%03d ms",
1610 MyProcPid, modename, buf.data, msecs, usecs),
1611 (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1612 "Processes holding the lock: %s. Wait queue: %s.",
1614 else if (deadlock_state == DS_HARD_DEADLOCK)
1615 {
1616 /*
1617 * This message is a bit redundant with the error that
1618 * will be reported subsequently, but in some cases the
1619 * error report might not make it to the log (eg, if it's
1620 * caught by an exception handler), and we want to ensure
1621 * all long-wait events get logged.
1622 */
1623 ereport(LOG,
1624 (errmsg("process %d detected deadlock while waiting for %s on %s after %ld.%03d ms",
1625 MyProcPid, modename, buf.data, msecs, usecs),
1626 (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1627 "Processes holding the lock: %s. Wait queue: %s.",
1629 }
1630
1632 {
1633 /*
1634 * Guard the "still waiting on lock" log message so it is
1635 * reported at most once while waiting for the lock.
1636 *
1637 * Without this guard, the message can be emitted whenever
1638 * the lock-wait sleep is interrupted (for example by
1639 * SIGHUP for config reload or by
1640 * client_connection_check_interval). For example, if
1641 * client_connection_check_interval is set very low (e.g.,
1642 * 100 ms), the message could be logged repeatedly,
1643 * flooding the log and making it difficult to use.
1644 */
1645 if (!logged_lock_wait)
1646 {
1647 ereport(LOG,
1648 (errmsg("process %d still waiting for %s on %s after %ld.%03d ms",
1649 MyProcPid, modename, buf.data, msecs, usecs),
1650 (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1651 "Processes holding the lock: %s. Wait queue: %s.",
1653 logged_lock_wait = true;
1654 }
1655 }
1657 ereport(LOG,
1658 (errmsg("process %d acquired %s on %s after %ld.%03d ms",
1659 MyProcPid, modename, buf.data, msecs, usecs)));
1660 else
1661 {
1663
1664 /*
1665 * Currently, the deadlock checker always kicks its own
1666 * process, which means that we'll only see
1667 * PROC_WAIT_STATUS_ERROR when deadlock_state ==
1668 * DS_HARD_DEADLOCK, and there's no need to print
1669 * redundant messages. But for completeness and
1670 * future-proofing, print a message if it looks like
1671 * someone else kicked us off the lock.
1672 */
1674 ereport(LOG,
1675 (errmsg("process %d failed to acquire %s on %s after %ld.%03d ms",
1676 MyProcPid, modename, buf.data, msecs, usecs),
1677 (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1678 "Processes holding the lock: %s. Wait queue: %s.",
1680 }
1681 pfree(buf.data);
1684 }
1685
1686 /*
1687 * At this point we might still need to wait for the lock. Reset
1688 * state so we don't print the above messages again if
1689 * log_lock_waits is on.
1690 */
1692 }
1694
1695 /*
1696 * Disable the timers, if they are still running. As in LockErrorCleanup,
1697 * we must preserve the LOCK_TIMEOUT indicator flag: if a lock timeout has
1698 * already caused QueryCancelPending to become set, we want the cancel to
1699 * be reported as a lock timeout, not a user cancel.
1700 */
1701 if (!InHotStandby)
1702 {
1703 if (LockTimeout > 0)
1704 {
1706
1708 timeouts[0].keep_indicator = false;
1709 timeouts[1].id = LOCK_TIMEOUT;
1710 timeouts[1].keep_indicator = true;
1712 }
1713 else
1715 }
1716
1717 /*
1718 * Emit the log message if recovery conflict on lock was resolved but the
1719 * startup process waited longer than deadlock_timeout for it.
1720 */
1724 NULL, false);
1725
1726 /*
1727 * We don't have to do anything else, because the awaker did all the
1728 * necessary updates of the lock table and MyProc. (The caller is
1729 * responsible for updating the local lock table.)
1730 */
1731 return myWaitStatus;
1732}
void TimestampDifference(TimestampTz start_time, TimestampTz stop_time, long *secs, int *microsecs)
Definition timestamp.c:1715
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition timestamp.c:1775
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1639
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1603
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:32
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define WARNING
Definition elog.h:37
#define DEBUG1
Definition elog.h:31
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
#define INJECTION_POINT(name, arg)
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:3078
const char * GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode)
Definition lock.c:4236
DeadLockState
Definition lock.h:340
@ DS_HARD_DEADLOCK
Definition lock.h:344
@ DS_BLOCKED_BY_AUTOVACUUM
Definition lock.h:345
@ DS_NO_DEADLOCK
Definition lock.h:342
@ DS_NOT_YET_CHECKED
Definition lock.h:341
@ DS_SOFT_DEADLOCK
Definition lock.h:343
#define AccessExclusiveLock
Definition lockdefs.h:43
@ LW_SHARED
Definition lwlock.h:105
void pfree(void *pointer)
Definition mcxt.c:1616
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:125
static char buf[DEFAULT_XLOG_SEG_SIZE]
void pgstat_count_lock_waits(uint8 locktag_type, long msecs)
#define PROC_VACUUM_FOR_WRAPAROUND
Definition proc.h:64
bool log_lock_waits
Definition proc.c:68
void GetLockHoldersAndWaiters(LOCALLOCK *locallock, StringInfo lock_holders_sbuf, StringInfo lock_waiters_sbuf, int *lockHoldersNum)
Definition proc.c:1941
int DeadlockTimeout
Definition proc.c:62
int LockTimeout
Definition proc.c:64
static DeadLockState CheckDeadLock(void)
Definition proc.c:1823
void CheckRecoveryConflictDeadlock(void)
Definition standby.c:907
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:626
@ RECOVERY_CONFLICT_LOCK
Definition standby.h:40
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
uint8 locktag_lockmethodid
Definition locktag.h:71
LOCKTAG tag
Definition lock.h:142
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:6830
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(), INJECTION_POINT, 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, pgstat_count_lock_waits(), 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()

void ProcWaitForSignal ( uint32  wait_event_info)
extern

◆ ProcWakeup()

void ProcWakeup ( PGPROC proc,
ProcWaitStatus  waitStatus 
)
extern

Definition at line 1748 of file proc.c.

1749{
1750 if (dlist_node_is_detached(&proc->waitLink))
1751 return;
1752
1754
1755 /* Remove process from wait queue */
1757
1758 /* Clean up process' state and pass it the ok/fail signal */
1759 proc->waitLock = NULL;
1760 proc->waitProcLock = NULL;
1761 proc->waitStatus = waitStatus;
1762 pg_atomic_write_u64(&proc->waitStart, 0);
1763
1764 /* And awaken it */
1765 SetLatch(&proc->procLatch);
1766}
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().

◆ SetStartupBufferPinWaitBufId()

void SetStartupBufferPinWaitBufId ( int  bufid)
extern

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}

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

Referenced by LockBufferForCleanup().

Variable Documentation

◆ DeadlockTimeout

◆ FastPathLockGroupsPerBackend

PGDLLIMPORT int FastPathLockGroupsPerBackend
extern

◆ IdleInTransactionSessionTimeout

PGDLLIMPORT int IdleInTransactionSessionTimeout
extern

Definition at line 65 of file proc.c.

Referenced by PostgresMain(), and ProcessInterrupts().

◆ IdleSessionTimeout

PGDLLIMPORT int IdleSessionTimeout
extern

Definition at line 67 of file proc.c.

Referenced by PostgresMain(), and ProcessInterrupts().

◆ LockTimeout

PGDLLIMPORT int LockTimeout
extern

Definition at line 64 of file proc.c.

Referenced by ProcSleep().

◆ log_lock_waits

PGDLLIMPORT bool log_lock_waits
extern

Definition at line 68 of file proc.c.

Referenced by ProcSleep().

◆ MyProc

PGDLLIMPORT PGPROC* MyProc
extern

Definition at line 71 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(), RepackWorkerMain(), ReplicationSlotRelease(), report_recovery_conflict(), ResolveRecoveryConflictWithLock(), set_indexsafe_procflags(), SetAuthenticatedUserId(), SetDataChecksumsOff(), SetDataChecksumsOn(), SetDataChecksumsOnInProgress(), 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(), start_repack_decoding_worker(), 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

PGDLLIMPORT PGPROC* PreparedXactProcs
extern

Definition at line 78 of file proc.c.

Referenced by ProcGlobalShmemInit(), and TwoPhaseShmemInit().

◆ ProcGlobal

◆ StatementTimeout

PGDLLIMPORT int StatementTimeout
extern

Definition at line 63 of file proc.c.

Referenced by enable_statement_timeout().

◆ TransactionTimeout