PostgreSQL Source Code  git master
proc.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * proc.h
4  * per-process shared memory data structures
5  *
6  *
7  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/proc.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef _PROC_H_
15 #define _PROC_H_
16 
17 #include "access/clog.h"
18 #include "access/xlogdefs.h"
19 #include "lib/ilist.h"
20 #include "storage/latch.h"
21 #include "storage/lock.h"
22 #include "storage/pg_sema.h"
23 #include "storage/proclist_types.h"
24 
25 /*
26  * Each backend advertises up to PGPROC_MAX_CACHED_SUBXIDS TransactionIds
27  * for non-aborted subtransactions of its current top transaction. These
28  * have to be treated as running XIDs by other backends.
29  *
30  * We also keep track of whether the cache overflowed (ie, the transaction has
31  * generated at least one subtransaction that didn't fit in the cache).
32  * If none of the caches have overflowed, we can assume that an XID that's not
33  * listed anywhere in the PGPROC array is not a running transaction. Else we
34  * have to look at pg_subtrans.
35  */
36 #define PGPROC_MAX_CACHED_SUBXIDS 64 /* XXX guessed-at value */
37 
38 struct XidCache
39 {
41 };
42 
43 /*
44  * Flags for PGXACT->vacuumFlags
45  *
46  * Note: If you modify these flags, you need to modify PROCARRAY_XXX flags
47  * in src/include/storage/procarray.h.
48  *
49  * PROC_RESERVED may later be assigned for use in vacuumFlags, but its value is
50  * used for PROCARRAY_SLOTS_XMIN in procarray.h, so GetOldestXmin won't be able
51  * to match and ignore processes with this flag set.
52  */
53 #define PROC_IS_AUTOVACUUM 0x01 /* is it an autovac worker? */
54 #define PROC_IN_VACUUM 0x02 /* currently running lazy vacuum */
55 #define PROC_VACUUM_FOR_WRAPAROUND 0x08 /* set by autovac only */
56 #define PROC_IN_LOGICAL_DECODING 0x10 /* currently doing logical
57  * decoding outside xact */
58 #define PROC_RESERVED 0x20 /* reserved for procarray */
59 
60 /* flags reset at EOXact */
61 #define PROC_VACUUM_STATE_MASK \
62  (PROC_IN_VACUUM | PROC_VACUUM_FOR_WRAPAROUND)
63 
64 /*
65  * We allow a small number of "weak" relation locks (AccessShareLock,
66  * RowShareLock, RowExclusiveLock) to be recorded in the PGPROC structure
67  * rather than the main lock table. This eases contention on the lock
68  * manager LWLocks. See storage/lmgr/README for additional details.
69  */
70 #define FP_LOCK_SLOTS_PER_BACKEND 16
71 
72 /*
73  * An invalid pgprocno. Must be larger than the maximum number of PGPROC
74  * structures we could possibly have. See comments for MAX_BACKENDS.
75  */
76 #define INVALID_PGPROCNO PG_INT32_MAX
77 
78 typedef enum
79 {
84 
85 /*
86  * Each backend has a PGPROC struct in shared memory. There is also a list of
87  * currently-unused PGPROC structs that will be reallocated to new backends.
88  *
89  * links: list link for any list the PGPROC is in. When waiting for a lock,
90  * the PGPROC is linked into that lock's waitProcs queue. A recycled PGPROC
91  * is linked into ProcGlobal's freeProcs list.
92  *
93  * Note: twophase.c also sets up a dummy PGPROC struct for each currently
94  * prepared transaction. These PGPROCs appear in the ProcArray data structure
95  * so that the prepared transactions appear to be still running and are
96  * correctly shown as holding locks. A prepared transaction PGPROC can be
97  * distinguished from a real one at need by the fact that it has pid == 0.
98  * The semaphore and lock-activity fields in a prepared-xact PGPROC are unused,
99  * but its myProcLocks[] lists are valid.
100  */
101 struct PGPROC
102 {
103  /* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
104  SHM_QUEUE links; /* list link if process is in a list */
105  PGPROC **procgloballist; /* procglobal list that owns this PGPROC */
106 
107  PGSemaphore sem; /* ONE semaphore to sleep on */
109 
110  Latch procLatch; /* generic latch for process */
111 
112  LocalTransactionId lxid; /* local id of top-level transaction currently
113  * being executed by this proc, if running;
114  * else InvalidLocalTransactionId */
115  int pid; /* Backend's process ID; 0 if prepared xact */
116  int pgprocno;
117 
118  /* These fields are zero while a backend is still starting up: */
119  BackendId backendId; /* This backend's backend ID (if assigned) */
120  Oid databaseId; /* OID of database this backend is using */
121  Oid roleId; /* OID of role using this backend */
122 
123  Oid tempNamespaceId; /* OID of temp schema this backend is
124  * using */
125 
126  bool isBackgroundWorker; /* true if background worker. */
127 
128  /*
129  * While in hot standby mode, shows that a conflict signal has been sent
130  * for the current transaction. Set/cleared while holding ProcArrayLock,
131  * though not required. Accessed without lock, if needed.
132  */
134 
135  /* Info about LWLock the process is currently waiting for, if any. */
136  bool lwWaiting; /* true if waiting for an LW lock */
137  uint8 lwWaitMode; /* lwlock mode being waited for */
138  proclist_node lwWaitLink; /* position in LW lock wait list */
139 
140  /* Support for condition variables. */
141  proclist_node cvWaitLink; /* position in CV wait list */
142 
143  /* Info about lock the process is currently waiting for, if any. */
144  /* waitLock and waitProcLock are NULL if not currently waiting. */
145  LOCK *waitLock; /* Lock object we're sleeping on ... */
146  PROCLOCK *waitProcLock; /* Per-holder info for awaited lock */
147  LOCKMODE waitLockMode; /* type of lock we're waiting for */
148  LOCKMASK heldLocks; /* bitmask for lock types already held on this
149  * lock object by this backend */
150 
151  bool delayChkpt; /* true if this proc delays checkpoint start */
152 
153  /*
154  * Info to allow us to wait for synchronous replication, if needed.
155  * waitLSN is InvalidXLogRecPtr if not waiting; set only by user backend.
156  * syncRepState must not be touched except by owning process or WALSender.
157  * syncRepLinks used only while holding SyncRepLock.
158  */
159  XLogRecPtr waitLSN; /* waiting for this LSN or higher */
160  int syncRepState; /* wait state for sync rep */
161  SHM_QUEUE syncRepLinks; /* list link if process is in syncrep queue */
162 
163  /*
164  * All PROCLOCK objects for locks held or awaited by this backend are
165  * linked into one of these lists, according to the partition number of
166  * their lock.
167  */
169 
170  struct XidCache subxids; /* cache for subtransaction XIDs */
171 
172  /* Support for group XID clearing. */
173  /* true, if member of ProcArray group waiting for XID clear */
175  /* next ProcArray group member waiting for XID clear */
177 
178  /*
179  * latest transaction id among the transaction's main XID and
180  * subtransactions
181  */
183 
184  uint32 wait_event_info; /* proc's wait information */
185 
186  /* Support for group transaction status update. */
187  bool clogGroupMember; /* true, if member of clog group */
188  pg_atomic_uint32 clogGroupNext; /* next clog group member */
189  TransactionId clogGroupMemberXid; /* transaction id of clog group member */
190  XidStatus clogGroupMemberXidStatus; /* transaction status of clog
191  * group member */
192  int clogGroupMemberPage; /* clog page corresponding to
193  * transaction id of clog group member */
194  XLogRecPtr clogGroupMemberLsn; /* WAL location of commit record for clog
195  * group member */
196 
197  /* Lock manager data, recording fast-path locks taken by this backend. */
198  LWLock fpInfoLock; /* protects per-backend fast-path state */
199  uint64 fpLockBits; /* lock modes held for each fast-path slot */
200  Oid fpRelId[FP_LOCK_SLOTS_PER_BACKEND]; /* slots for rel oids */
201  bool fpVXIDLock; /* are we holding a fast-path VXID lock? */
202  LocalTransactionId fpLocalTransactionId; /* lxid for fast-path VXID
203  * lock */
204 
205  /*
206  * Support for lock groups. Use LockHashPartitionLockByProc on the group
207  * leader to get the LWLock protecting these fields.
208  */
209  PGPROC *lockGroupLeader; /* lock group leader, if I'm a member */
210  dlist_head lockGroupMembers; /* list of members, if I'm a leader */
211  dlist_node lockGroupLink; /* my member link, if I'm a member */
212 };
213 
214 /* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
215 
216 
217 extern PGDLLIMPORT PGPROC *MyProc;
218 extern PGDLLIMPORT struct PGXACT *MyPgXact;
219 
220 /*
221  * Prior to PostgreSQL 9.2, the fields below were stored as part of the
222  * PGPROC. However, benchmarking revealed that packing these particular
223  * members into a separate array as tightly as possible sped up GetSnapshotData
224  * considerably on systems with many CPU cores, by reducing the number of
225  * cache lines needing to be fetched. Thus, think very carefully before adding
226  * anything else here.
227  */
228 typedef struct PGXACT
229 {
230  TransactionId xid; /* id of top-level transaction currently being
231  * executed by this proc, if running and XID
232  * is assigned; else InvalidTransactionId */
233 
234  TransactionId xmin; /* minimal running XID as it was when we were
235  * starting our xact, excluding LAZY VACUUM:
236  * vacuum must not remove tuples deleted by
237  * xid >= xmin ! */
238 
239  uint8 vacuumFlags; /* vacuum-related flags, see above */
241 
243 } PGXACT;
244 
245 /*
246  * There is one ProcGlobal struct for the whole database cluster.
247  */
248 typedef struct PROC_HDR
249 {
250  /* Array of PGPROC structures (not including dummies for prepared txns) */
252  /* Array of PGXACT structures (not including dummies for prepared txns) */
254  /* Length of allProcs array */
256  /* Head of list of free PGPROC structures */
258  /* Head of list of autovacuum's free PGPROC structures */
260  /* Head of list of bgworker free PGPROC structures */
262  /* Head of list of walsender free PGPROC structures */
264  /* First pgproc waiting for group XID clear */
266  /* First pgproc waiting for group transaction status update */
268  /* WALWriter process's latch */
270  /* Checkpointer process's latch */
272  /* Current shared estimate of appropriate spins_per_delay value */
274  /* The proc of the Startup process, since not in ProcArray */
277  /* Buffer id of the buffer that Startup process waits for pin on, or -1 */
279 } PROC_HDR;
280 
282 
283 extern PGPROC *PreparedXactProcs;
284 
285 /* Accessor for PGPROC given a pgprocno. */
286 #define GetPGProcByNumber(n) (&ProcGlobal->allProcs[(n)])
287 
288 /*
289  * We set aside some extra PGPROC structures for auxiliary processes,
290  * ie things that aren't full-fledged backends but need shmem access.
291  *
292  * Background writer, checkpointer and WAL writer run during normal operation.
293  * Startup process and WAL receiver also consume 2 slots, but WAL writer is
294  * launched only after startup has exited, so we only need 4 slots.
295  */
296 #define NUM_AUXILIARY_PROCS 4
297 
298 /* configurable options */
299 extern PGDLLIMPORT int DeadlockTimeout;
300 extern PGDLLIMPORT int StatementTimeout;
301 extern PGDLLIMPORT int LockTimeout;
303 extern bool log_lock_waits;
304 
305 
306 /*
307  * Function Prototypes
308  */
309 extern int ProcGlobalSemas(void);
310 extern Size ProcGlobalShmemSize(void);
311 extern void InitProcGlobal(void);
312 extern void InitProcess(void);
313 extern void InitProcessPhase2(void);
314 extern void InitAuxiliaryProcess(void);
315 
316 extern void PublishStartupProcessInformation(void);
317 extern void SetStartupBufferPinWaitBufId(int bufid);
318 extern int GetStartupBufferPinWaitBufId(void);
319 
320 extern bool HaveNFreeProcs(int n);
321 extern void ProcReleaseLocks(bool isCommit);
322 
323 extern void ProcQueueInit(PROC_QUEUE *queue);
324 extern ProcWaitStatus ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable);
325 extern PGPROC *ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus);
326 extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
327 extern void CheckDeadLockAlert(void);
328 extern bool IsWaitingForLock(void);
329 extern void LockErrorCleanup(void);
330 
331 extern void ProcWaitForSignal(uint32 wait_event_info);
332 extern void ProcSendSignal(int pid);
333 
334 extern PGPROC *AuxiliaryPidGetProc(int pid);
335 
336 extern void BecomeLockGroupLeader(void);
337 extern bool BecomeLockGroupMember(PGPROC *leader, int pid);
338 
339 #endif /* _PROC_H_ */
PGDLLIMPORT PGPROC * MyProc
Definition: proc.c:67
bool procArrayGroupMember
Definition: proc.h:174
Definition: lwlock.h:31
XidStatus clogGroupMemberXidStatus
Definition: proc.h:190
bool log_lock_waits
Definition: proc.c:64
PGDLLIMPORT int StatementTimeout
Definition: proc.c:61
BackendId backendId
Definition: proc.h:119
uint32 TransactionId
Definition: c.h:520
uint32 wait_event_info
Definition: proc.h:184
Definition: proc.h:228
int LOCKMODE
Definition: lockdefs.h:26
XLogRecPtr clogGroupMemberLsn
Definition: proc.h:194
dlist_head lockGroupMembers
Definition: proc.h:210
Oid tempNamespaceId
Definition: proc.h:123
TransactionId xmin
Definition: proc.h:234
PGXACT * allPgXact
Definition: proc.h:253
TransactionId xid
Definition: proc.h:230
SHM_QUEUE links
Definition: proc.h:104
LOCKMODE waitLockMode
Definition: proc.h:147
bool lwWaiting
Definition: proc.h:136
unsigned char uint8
Definition: c.h:372
PGDLLIMPORT int LockTimeout
Definition: proc.c:62
int XidStatus
Definition: clog.h:24
PGPROC * bgworkerFreeProcs
Definition: proc.h:261
int GetStartupBufferPinWaitBufId(void)
Definition: proc.c:657
Oid roleId
Definition: proc.h:121
void LockErrorCleanup(void)
Definition: proc.c:711
uint8 lwWaitMode
Definition: proc.h:137
pg_atomic_uint32 clogGroupNext
Definition: proc.h:188
ProcWaitStatus waitStatus
Definition: proc.h:108
bool fpVXIDLock
Definition: proc.h:201
unsigned int Oid
Definition: postgres_ext.h:31
PGPROC * autovacFreeProcs
Definition: proc.h:259
void ProcSendSignal(int pid)
Definition: proc.c:1812
PGPROC * walsenderFreeProcs
Definition: proc.h:263
void SetStartupBufferPinWaitBufId(int bufid)
Definition: proc.c:645
Latch procLatch
Definition: proc.h:110
void InitProcessPhase2(void)
Definition: proc.c:480
#define PGDLLIMPORT
Definition: c.h:1257
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:985
uint8 vacuumFlags
Definition: proc.h:239
PGDLLIMPORT int DeadlockTimeout
Definition: proc.c:60
bool isBackgroundWorker
Definition: proc.h:126
PGDLLIMPORT int IdleInTransactionSessionTimeout
Definition: proc.c:63
dlist_node lockGroupLink
Definition: proc.h:211
Latch * walwriterLatch
Definition: proc.h:269
int spins_per_delay
Definition: proc.h:273
bool delayChkpt
Definition: proc.h:151
bool BecomeLockGroupMember(PGPROC *leader, int pid)
Definition: proc.c:1880
int ProcGlobalSemas(void)
Definition: proc.c:128
proclist_node lwWaitLink
Definition: proc.h:138
void ProcWaitForSignal(uint32 wait_event_info)
Definition: proc.c:1800
PROCLOCK * waitProcLock
Definition: proc.h:146
Definition: latch.h:110
int clogGroupMemberPage
Definition: proc.h:192
#define FP_LOCK_SLOTS_PER_BACKEND
Definition: proc.h:70
bool recoveryConflictPending
Definition: proc.h:133
uint8 nxids
Definition: proc.h:242
bool HaveNFreeProcs(int n)
Definition: proc.c:671
TransactionId clogGroupMemberXid
Definition: proc.h:189
Oid databaseId
Definition: proc.h:120
unsigned int uint32
Definition: c.h:374
PGPROC ** procgloballist
Definition: proc.h:105
Definition: lock.h:287
uint32 LocalTransactionId
Definition: c.h:522
LOCK * waitLock
Definition: proc.h:145
void ProcReleaseLocks(bool isCommit)
Definition: proc.c:787
void BecomeLockGroupLeader(void)
Definition: proc.c:1850
void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
Definition: proc.c:1632
pg_atomic_uint32 procArrayGroupNext
Definition: proc.h:176
Definition: proc.h:248
PGDLLIMPORT struct PGXACT * MyPgXact
Definition: proc.c:68
PGPROC * PreparedXactProcs
Definition: proc.c:82
int startupProcPid
Definition: proc.h:276
uint64 fpLockBits
Definition: proc.h:199
int BackendId
Definition: backendid.h:21
PGPROC * ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus)
Definition: proc.c:1596
ProcWaitStatus
Definition: proc.h:78
bool overflowed
Definition: proc.h:240
#define PGPROC_MAX_CACHED_SUBXIDS
Definition: proc.h:36
ProcWaitStatus ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
Definition: proc.c:1062
Latch * checkpointerLatch
Definition: proc.h:271
PGDLLIMPORT PROC_HDR * ProcGlobal
Definition: proc.c:80
TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: proc.h:40
void InitProcGlobal(void)
Definition: proc.c:163
void ProcQueueInit(PROC_QUEUE *queue)
Definition: proc.c:1038
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void CheckDeadLockAlert(void)
Definition: proc.c:1777
PGPROC * freeProcs
Definition: proc.h:257
void InitAuxiliaryProcess(void)
Definition: proc.c:515
size_t Size
Definition: c.h:473
SHM_QUEUE syncRepLinks
Definition: proc.h:161
uint32 allProcCount
Definition: proc.h:255
int LOCKMASK
Definition: lockdefs.h:25
proclist_node cvWaitLink
Definition: proc.h:141
pg_atomic_uint32 clogGroupFirst
Definition: proc.h:267
void PublishStartupProcessInformation(void)
Definition: proc.c:627
Definition: lock.h:348
int pgprocno
Definition: proc.h:116
pg_atomic_uint32 procArrayGroupFirst
Definition: proc.h:265
bool IsWaitingForLock(void)
Definition: proc.c:694
bool clogGroupMember
Definition: proc.h:187
int startupBufferPinWaitBufId
Definition: proc.h:278
void InitProcess(void)
Definition: proc.c:304
PGPROC * allProcs
Definition: proc.h:251
struct PROC_HDR PROC_HDR
PGPROC * startupProc
Definition: proc.h:275
LWLock fpInfoLock
Definition: proc.h:198
PGSemaphore sem
Definition: proc.h:107
int syncRepState
Definition: proc.h:160
Definition: proc.h:101
int pid
Definition: proc.h:115
Size ProcGlobalShmemSize(void)
Definition: proc.c:102
XLogRecPtr waitLSN
Definition: proc.h:159
PGPROC * lockGroupLeader
Definition: proc.h:209
LocalTransactionId fpLocalTransactionId
Definition: proc.h:202
TransactionId procArrayGroupMemberXid
Definition: proc.h:182
LOCKMASK heldLocks
Definition: proc.h:148
#define NUM_LOCK_PARTITIONS
Definition: lwlock.h:115
Definition: proc.h:38
LocalTransactionId lxid
Definition: proc.h:112
struct PGXACT PGXACT