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 typedef struct XidCacheStatus
39 {
40  /* number of cached subxids, never more than PGPROC_MAX_CACHED_SUBXIDS */
42  /* has PGPROC->subxids overflowed */
43  bool overflowed;
45 
46 struct XidCache
47 {
49 };
50 
51 /*
52  * Flags for ProcGlobal->vacuumFlags[]
53  */
54 #define PROC_IS_AUTOVACUUM 0x01 /* is it an autovac worker? */
55 #define PROC_IN_VACUUM 0x02 /* currently running lazy vacuum */
56 #define PROC_VACUUM_FOR_WRAPAROUND 0x08 /* set by autovac only */
57 #define PROC_IN_LOGICAL_DECODING 0x10 /* currently doing logical
58  * decoding outside xact */
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  * Mirrored fields:
102  *
103  * Some fields in PGPROC (see "mirrored in ..." comment) are mirrored into an
104  * element of more densely packed ProcGlobal arrays. These arrays are indexed
105  * by PGPROC->pgxactoff. Both copies need to be maintained coherently.
106  *
107  * NB: The pgxactoff indexed value can *never* be accessed without holding
108  * locks.
109  *
110  * See PROC_HDR for details.
111  */
112 struct PGPROC
113 {
114  /* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
115  SHM_QUEUE links; /* list link if process is in a list */
116  PGPROC **procgloballist; /* procglobal list that owns this PGPROC */
117 
118  PGSemaphore sem; /* ONE semaphore to sleep on */
120 
121  Latch procLatch; /* generic latch for process */
122 
123 
124  TransactionId xid; /* id of top-level transaction currently being
125  * executed by this proc, if running and XID
126  * is assigned; else InvalidTransactionId.
127  * mirrored in ProcGlobal->xids[pgxactoff] */
128 
129  TransactionId xmin; /* minimal running XID as it was when we were
130  * starting our xact, excluding LAZY VACUUM:
131  * vacuum must not remove tuples deleted by
132  * xid >= xmin ! */
133 
134  LocalTransactionId lxid; /* local id of top-level transaction currently
135  * being executed by this proc, if running;
136  * else InvalidLocalTransactionId */
137  int pid; /* Backend's process ID; 0 if prepared xact */
138 
139  int pgxactoff; /* offset into various ProcGlobal->arrays
140  * with data mirrored from this PGPROC */
141  int pgprocno;
142 
143  /* These fields are zero while a backend is still starting up: */
144  BackendId backendId; /* This backend's backend ID (if assigned) */
145  Oid databaseId; /* OID of database this backend is using */
146  Oid roleId; /* OID of role using this backend */
147 
148  Oid tempNamespaceId; /* OID of temp schema this backend is
149  * using */
150 
151  bool isBackgroundWorker; /* true if background worker. */
152 
153  /*
154  * While in hot standby mode, shows that a conflict signal has been sent
155  * for the current transaction. Set/cleared while holding ProcArrayLock,
156  * though not required. Accessed without lock, if needed.
157  */
159 
160  /* Info about LWLock the process is currently waiting for, if any. */
161  bool lwWaiting; /* true if waiting for an LW lock */
162  uint8 lwWaitMode; /* lwlock mode being waited for */
163  proclist_node lwWaitLink; /* position in LW lock wait list */
164 
165  /* Support for condition variables. */
166  proclist_node cvWaitLink; /* position in CV wait list */
167 
168  /* Info about lock the process is currently waiting for, if any. */
169  /* waitLock and waitProcLock are NULL if not currently waiting. */
170  LOCK *waitLock; /* Lock object we're sleeping on ... */
171  PROCLOCK *waitProcLock; /* Per-holder info for awaited lock */
172  LOCKMODE waitLockMode; /* type of lock we're waiting for */
173  LOCKMASK heldLocks; /* bitmask for lock types already held on this
174  * lock object by this backend */
175 
176  bool delayChkpt; /* true if this proc delays checkpoint start */
177 
178  uint8 vacuumFlags; /* this backend's vacuum flags, see PROC_*
179  * above. mirrored in
180  * ProcGlobal->vacuumFlags[pgxactoff] */
181  /*
182  * Info to allow us to wait for synchronous replication, if needed.
183  * waitLSN is InvalidXLogRecPtr if not waiting; set only by user backend.
184  * syncRepState must not be touched except by owning process or WALSender.
185  * syncRepLinks used only while holding SyncRepLock.
186  */
187  XLogRecPtr waitLSN; /* waiting for this LSN or higher */
188  int syncRepState; /* wait state for sync rep */
189  SHM_QUEUE syncRepLinks; /* list link if process is in syncrep queue */
190 
191  /*
192  * All PROCLOCK objects for locks held or awaited by this backend are
193  * linked into one of these lists, according to the partition number of
194  * their lock.
195  */
197 
198  XidCacheStatus subxidStatus; /* mirrored with
199  * ProcGlobal->subxidStates[i] */
200  struct XidCache subxids; /* cache for subtransaction XIDs */
201 
202  /* Support for group XID clearing. */
203  /* true, if member of ProcArray group waiting for XID clear */
205  /* next ProcArray group member waiting for XID clear */
207 
208  /*
209  * latest transaction id among the transaction's main XID and
210  * subtransactions
211  */
213 
214  uint32 wait_event_info; /* proc's wait information */
215 
216  /* Support for group transaction status update. */
217  bool clogGroupMember; /* true, if member of clog group */
218  pg_atomic_uint32 clogGroupNext; /* next clog group member */
219  TransactionId clogGroupMemberXid; /* transaction id of clog group member */
220  XidStatus clogGroupMemberXidStatus; /* transaction status of clog
221  * group member */
222  int clogGroupMemberPage; /* clog page corresponding to
223  * transaction id of clog group member */
224  XLogRecPtr clogGroupMemberLsn; /* WAL location of commit record for clog
225  * group member */
226 
227  /* Lock manager data, recording fast-path locks taken by this backend. */
228  LWLock fpInfoLock; /* protects per-backend fast-path state */
229  uint64 fpLockBits; /* lock modes held for each fast-path slot */
230  Oid fpRelId[FP_LOCK_SLOTS_PER_BACKEND]; /* slots for rel oids */
231  bool fpVXIDLock; /* are we holding a fast-path VXID lock? */
232  LocalTransactionId fpLocalTransactionId; /* lxid for fast-path VXID
233  * lock */
234 
235  /*
236  * Support for lock groups. Use LockHashPartitionLockByProc on the group
237  * leader to get the LWLock protecting these fields.
238  */
239  PGPROC *lockGroupLeader; /* lock group leader, if I'm a member */
240  dlist_head lockGroupMembers; /* list of members, if I'm a leader */
241  dlist_node lockGroupLink; /* my member link, if I'm a member */
242 };
243 
244 /* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
245 
246 
247 extern PGDLLIMPORT PGPROC *MyProc;
248 
249 /*
250  * There is one ProcGlobal struct for the whole database cluster.
251  *
252  * Adding/Removing an entry into the procarray requires holding *both*
253  * ProcArrayLock and XidGenLock in exclusive mode (in that order). Both are
254  * needed because the dense arrays (see below) are accessed from
255  * GetNewTransactionId() and GetSnapshotData(), and we don't want to add
256  * further contention by both using the same lock. Adding/Removing a procarray
257  * entry is much less frequent.
258  *
259  * Some fields in PGPROC are mirrored into more densely packed arrays (e.g.
260  * xids), with one entry for each backend. These arrays only contain entries
261  * for PGPROCs that have been added to the shared array with ProcArrayAdd()
262  * (in contrast to PGPROC array which has unused PGPROCs interspersed).
263  *
264  * The dense arrays are indexed by PGPROC->pgxactoff. Any concurrent
265  * ProcArrayAdd() / ProcArrayRemove() can lead to pgxactoff of a procarray
266  * member to change. Therefore it is only safe to use PGPROC->pgxactoff to
267  * access the dense array while holding either ProcArrayLock or XidGenLock.
268  *
269  * As long as a PGPROC is in the procarray, the mirrored values need to be
270  * maintained in both places in a coherent manner.
271  *
272  * The denser separate arrays are beneficial for three main reasons: First, to
273  * allow for as tight loops accessing the data as possible. Second, to prevent
274  * updates of frequently changing data (e.g. xmin) from invalidating
275  * cachelines also containing less frequently changing data (e.g. xid,
276  * vacuumFlags). Third to condense frequently accessed data into as few
277  * cachelines as possible.
278  *
279  * There are two main reasons to have the data mirrored between these dense
280  * arrays and PGPROC. First, as explained above, a PGPROC's array entries can
281  * only be accessed with either ProcArrayLock or XidGenLock held, whereas the
282  * PGPROC entries do not require that (obviously there may still be locking
283  * requirements around the individual field, separate from the concerns
284  * here). That is particularly important for a backend to efficiently checks
285  * it own values, which it often can safely do without locking. Second, the
286  * PGPROC fields allow to avoid unnecessary accesses and modification to the
287  * dense arrays. A backend's own PGPROC is more likely to be in a local cache,
288  * whereas the cachelines for the dense array will be modified by other
289  * backends (often removing it from the cache for other cores/sockets). At
290  * commit/abort time a check of the PGPROC value can avoid accessing/dirtying
291  * the corresponding array value.
292  *
293  * Basically it makes sense to access the PGPROC variable when checking a
294  * single backend's data, especially when already looking at the PGPROC for
295  * other reasons already. It makes sense to look at the "dense" arrays if we
296  * need to look at many / most entries, because we then benefit from the
297  * reduced indirection and better cross-process cache-ability.
298  *
299  * When entering a PGPROC for 2PC transactions with ProcArrayAdd(), the data
300  * in the dense arrays is initialized from the PGPROC while it already holds
301  * ProcArrayLock.
302  */
303 typedef struct PROC_HDR
304 {
305  /* Array of PGPROC structures (not including dummies for prepared txns) */
307 
308  /* Array mirroring PGPROC.xid for each PGPROC currently in the procarray */
310 
311  /*
312  * Array mirroring PGPROC.subxidStatus for each PGPROC currently in the
313  * procarray.
314  */
316 
317  /*
318  * Array mirroring PGPROC.vacuumFlags for each PGPROC currently in the
319  * procarray.
320  */
322 
323  /* Length of allProcs array */
325  /* Head of list of free PGPROC structures */
327  /* Head of list of autovacuum's free PGPROC structures */
329  /* Head of list of bgworker free PGPROC structures */
331  /* Head of list of walsender free PGPROC structures */
333  /* First pgproc waiting for group XID clear */
335  /* First pgproc waiting for group transaction status update */
337  /* WALWriter process's latch */
339  /* Checkpointer process's latch */
341  /* Current shared estimate of appropriate spins_per_delay value */
343  /* The proc of the Startup process, since not in ProcArray */
346  /* Buffer id of the buffer that Startup process waits for pin on, or -1 */
348 } PROC_HDR;
349 
351 
352 extern PGPROC *PreparedXactProcs;
353 
354 /* Accessor for PGPROC given a pgprocno. */
355 #define GetPGProcByNumber(n) (&ProcGlobal->allProcs[(n)])
356 
357 /*
358  * We set aside some extra PGPROC structures for auxiliary processes,
359  * ie things that aren't full-fledged backends but need shmem access.
360  *
361  * Background writer, checkpointer and WAL writer run during normal operation.
362  * Startup process and WAL receiver also consume 2 slots, but WAL writer is
363  * launched only after startup has exited, so we only need 4 slots.
364  */
365 #define NUM_AUXILIARY_PROCS 4
366 
367 /* configurable options */
368 extern PGDLLIMPORT int DeadlockTimeout;
369 extern PGDLLIMPORT int StatementTimeout;
370 extern PGDLLIMPORT int LockTimeout;
372 extern bool log_lock_waits;
373 
374 
375 /*
376  * Function Prototypes
377  */
378 extern int ProcGlobalSemas(void);
379 extern Size ProcGlobalShmemSize(void);
380 extern void InitProcGlobal(void);
381 extern void InitProcess(void);
382 extern void InitProcessPhase2(void);
383 extern void InitAuxiliaryProcess(void);
384 
385 extern void PublishStartupProcessInformation(void);
386 extern void SetStartupBufferPinWaitBufId(int bufid);
387 extern int GetStartupBufferPinWaitBufId(void);
388 
389 extern bool HaveNFreeProcs(int n);
390 extern void ProcReleaseLocks(bool isCommit);
391 
392 extern void ProcQueueInit(PROC_QUEUE *queue);
393 extern ProcWaitStatus ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable);
394 extern PGPROC *ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus);
395 extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
396 extern void CheckDeadLockAlert(void);
397 extern bool IsWaitingForLock(void);
398 extern void LockErrorCleanup(void);
399 
400 extern void ProcWaitForSignal(uint32 wait_event_info);
401 extern void ProcSendSignal(int pid);
402 
403 extern PGPROC *AuxiliaryPidGetProc(int pid);
404 
405 extern void BecomeLockGroupLeader(void);
406 extern bool BecomeLockGroupMember(PGPROC *leader, int pid);
407 
408 #endif /* _PROC_H_ */
PGDLLIMPORT PGPROC * MyProc
Definition: proc.c:67
bool procArrayGroupMember
Definition: proc.h:204
Definition: lwlock.h:31
XidStatus clogGroupMemberXidStatus
Definition: proc.h:220
bool log_lock_waits
Definition: proc.c:64
PGDLLIMPORT int StatementTimeout
Definition: proc.c:61
BackendId backendId
Definition: proc.h:144
uint32 TransactionId
Definition: c.h:521
uint32 wait_event_info
Definition: proc.h:214
int LOCKMODE
Definition: lockdefs.h:26
XLogRecPtr clogGroupMemberLsn
Definition: proc.h:224
dlist_head lockGroupMembers
Definition: proc.h:240
XidCacheStatus * subxidStates
Definition: proc.h:315
Oid tempNamespaceId
Definition: proc.h:148
SHM_QUEUE links
Definition: proc.h:115
LOCKMODE waitLockMode
Definition: proc.h:172
bool lwWaiting
Definition: proc.h:161
unsigned char uint8
Definition: c.h:373
PGDLLIMPORT int LockTimeout
Definition: proc.c:62
int XidStatus
Definition: clog.h:25
PGPROC * bgworkerFreeProcs
Definition: proc.h:330
int GetStartupBufferPinWaitBufId(void)
Definition: proc.c:652
Oid roleId
Definition: proc.h:146
void LockErrorCleanup(void)
Definition: proc.c:706
uint8 lwWaitMode
Definition: proc.h:162
pg_atomic_uint32 clogGroupNext
Definition: proc.h:218
ProcWaitStatus waitStatus
Definition: proc.h:119
bool fpVXIDLock
Definition: proc.h:231
unsigned int Oid
Definition: postgres_ext.h:31
PGPROC * autovacFreeProcs
Definition: proc.h:328
void ProcSendSignal(int pid)
Definition: proc.c:1808
XidCacheStatus subxidStatus
Definition: proc.h:198
PGPROC * walsenderFreeProcs
Definition: proc.h:332
void SetStartupBufferPinWaitBufId(int bufid)
Definition: proc.c:640
Latch procLatch
Definition: proc.h:121
void InitProcessPhase2(void)
Definition: proc.c:476
#define PGDLLIMPORT
Definition: c.h:1259
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:980
bool overflowed
Definition: proc.h:43
PGDLLIMPORT int DeadlockTimeout
Definition: proc.c:60
bool isBackgroundWorker
Definition: proc.h:151
PGDLLIMPORT int IdleInTransactionSessionTimeout
Definition: proc.c:63
dlist_node lockGroupLink
Definition: proc.h:241
Latch * walwriterLatch
Definition: proc.h:338
int spins_per_delay
Definition: proc.h:342
bool delayChkpt
Definition: proc.h:176
bool BecomeLockGroupMember(PGPROC *leader, int pid)
Definition: proc.c:1876
int ProcGlobalSemas(void)
Definition: proc.c:123
proclist_node lwWaitLink
Definition: proc.h:163
TransactionId xmin
Definition: proc.h:129
void ProcWaitForSignal(uint32 wait_event_info)
Definition: proc.c:1796
PROCLOCK * waitProcLock
Definition: proc.h:171
Definition: latch.h:110
int clogGroupMemberPage
Definition: proc.h:222
#define FP_LOCK_SLOTS_PER_BACKEND
Definition: proc.h:70
bool recoveryConflictPending
Definition: proc.h:158
bool HaveNFreeProcs(int n)
Definition: proc.c:666
TransactionId * xids
Definition: proc.h:309
TransactionId clogGroupMemberXid
Definition: proc.h:219
Oid databaseId
Definition: proc.h:145
unsigned int uint32
Definition: c.h:375
PGPROC ** procgloballist
Definition: proc.h:116
Definition: lock.h:297
uint32 LocalTransactionId
Definition: c.h:523
LOCK * waitLock
Definition: proc.h:170
void ProcReleaseLocks(bool isCommit)
Definition: proc.c:782
void BecomeLockGroupLeader(void)
Definition: proc.c:1846
void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
Definition: proc.c:1628
pg_atomic_uint32 procArrayGroupNext
Definition: proc.h:206
Definition: proc.h:303
PGPROC * PreparedXactProcs
Definition: proc.c:81
int startupProcPid
Definition: proc.h:345
uint64 fpLockBits
Definition: proc.h:229
struct XidCacheStatus XidCacheStatus
int BackendId
Definition: backendid.h:21
PGPROC * ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus)
Definition: proc.c:1592
ProcWaitStatus
Definition: proc.h:78
#define PGPROC_MAX_CACHED_SUBXIDS
Definition: proc.h:36
ProcWaitStatus ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
Definition: proc.c:1057
Latch * checkpointerLatch
Definition: proc.h:340
PGDLLIMPORT PROC_HDR * ProcGlobal
Definition: proc.c:79
void InitProcGlobal(void)
Definition: proc.c:158
void ProcQueueInit(PROC_QUEUE *queue)
Definition: proc.c:1033
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void CheckDeadLockAlert(void)
Definition: proc.c:1773
PGPROC * freeProcs
Definition: proc.h:326
void InitAuxiliaryProcess(void)
Definition: proc.c:511
size_t Size
Definition: c.h:474
SHM_QUEUE syncRepLinks
Definition: proc.h:189
uint32 allProcCount
Definition: proc.h:324
int LOCKMASK
Definition: lockdefs.h:25
proclist_node cvWaitLink
Definition: proc.h:166
uint8 count
Definition: proc.h:41
TransactionId xid
Definition: proc.h:124
pg_atomic_uint32 clogGroupFirst
Definition: proc.h:336
void PublishStartupProcessInformation(void)
Definition: proc.c:622
Definition: lock.h:358
int pgprocno
Definition: proc.h:141
pg_atomic_uint32 procArrayGroupFirst
Definition: proc.h:334
bool IsWaitingForLock(void)
Definition: proc.c:689
bool clogGroupMember
Definition: proc.h:217
int startupBufferPinWaitBufId
Definition: proc.h:347
int pgxactoff
Definition: proc.h:139
uint8 * vacuumFlags
Definition: proc.h:321
uint8 vacuumFlags
Definition: proc.h:178
void InitProcess(void)
Definition: proc.c:301
PGPROC * allProcs
Definition: proc.h:306
struct PROC_HDR PROC_HDR
PGPROC * startupProc
Definition: proc.h:344
LWLock fpInfoLock
Definition: proc.h:228
PGSemaphore sem
Definition: proc.h:118
int syncRepState
Definition: proc.h:188
Definition: proc.h:112
int pid
Definition: proc.h:137
Size ProcGlobalShmemSize(void)
Definition: proc.c:101
XLogRecPtr waitLSN
Definition: proc.h:187
PGPROC * lockGroupLeader
Definition: proc.h:239
LocalTransactionId fpLocalTransactionId
Definition: proc.h:232
TransactionId procArrayGroupMemberXid
Definition: proc.h:212
LOCKMASK heldLocks
Definition: proc.h:173
#define NUM_LOCK_PARTITIONS
Definition: lwlock.h:115
Definition: proc.h:46
LocalTransactionId lxid
Definition: proc.h:134