PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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_IN_ANALYZE 0x04 /* currently running analyze */
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 #define PROC_RESERVED 0x20 /* reserved for procarray */
60 
61 /* flags reset at EOXact */
62 #define PROC_VACUUM_STATE_MASK \
63  (PROC_IN_VACUUM | PROC_IN_ANALYZE | PROC_VACUUM_FOR_WRAPAROUND)
64 
65 /*
66  * We allow a small number of "weak" relation locks (AccesShareLock,
67  * RowShareLock, RowExclusiveLock) to be recorded in the PGPROC structure
68  * rather than the main lock table. This eases contention on the lock
69  * manager LWLocks. See storage/lmgr/README for additional details.
70  */
71 #define FP_LOCK_SLOTS_PER_BACKEND 16
72 
73 /*
74  * An invalid pgprocno. Must be larger than the maximum number of PGPROC
75  * structures we could possibly have. See comments for MAX_BACKENDS.
76  */
77 #define INVALID_PGPROCNO PG_INT32_MAX
78 
79 /*
80  * Each backend has a PGPROC struct in shared memory. There is also a list of
81  * currently-unused PGPROC structs that will be reallocated to new backends.
82  *
83  * links: list link for any list the PGPROC is in. When waiting for a lock,
84  * the PGPROC is linked into that lock's waitProcs queue. A recycled PGPROC
85  * is linked into ProcGlobal's freeProcs list.
86  *
87  * Note: twophase.c also sets up a dummy PGPROC struct for each currently
88  * prepared transaction. These PGPROCs appear in the ProcArray data structure
89  * so that the prepared transactions appear to be still running and are
90  * correctly shown as holding locks. A prepared transaction PGPROC can be
91  * distinguished from a real one at need by the fact that it has pid == 0.
92  * The semaphore and lock-activity fields in a prepared-xact PGPROC are unused,
93  * but its myProcLocks[] lists are valid.
94  */
95 struct PGPROC
96 {
97  /* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
98  SHM_QUEUE links; /* list link if process is in a list */
99  PGPROC **procgloballist; /* procglobal list that owns this PGPROC */
100 
101  PGSemaphore sem; /* ONE semaphore to sleep on */
102  int waitStatus; /* STATUS_WAITING, STATUS_OK or STATUS_ERROR */
103 
104  Latch procLatch; /* generic latch for process */
105 
106  LocalTransactionId lxid; /* local id of top-level transaction currently
107  * being executed by this proc, if running;
108  * else InvalidLocalTransactionId */
109  int pid; /* Backend's process ID; 0 if prepared xact */
110  int pgprocno;
111 
112  /* These fields are zero while a backend is still starting up: */
113  BackendId backendId; /* This backend's backend ID (if assigned) */
114  Oid databaseId; /* OID of database this backend is using */
115  Oid roleId; /* OID of role using this backend */
116 
117  bool isBackgroundWorker; /* true if background worker. */
118 
119  /*
120  * While in hot standby mode, shows that a conflict signal has been sent
121  * for the current transaction. Set/cleared while holding ProcArrayLock,
122  * though not required. Accessed without lock, if needed.
123  */
125 
126  /* Info about LWLock the process is currently waiting for, if any. */
127  bool lwWaiting; /* true if waiting for an LW lock */
128  uint8 lwWaitMode; /* lwlock mode being waited for */
129  proclist_node lwWaitLink; /* position in LW lock wait list */
130 
131  /* Support for condition variables. */
132  proclist_node cvWaitLink; /* position in CV wait list */
133 
134  /* Info about lock the process is currently waiting for, if any. */
135  /* waitLock and waitProcLock are NULL if not currently waiting. */
136  LOCK *waitLock; /* Lock object we're sleeping on ... */
137  PROCLOCK *waitProcLock; /* Per-holder info for awaited lock */
138  LOCKMODE waitLockMode; /* type of lock we're waiting for */
139  LOCKMASK heldLocks; /* bitmask for lock types already held on this
140  * lock object by this backend */
141 
142  /*
143  * Info to allow us to wait for synchronous replication, if needed.
144  * waitLSN is InvalidXLogRecPtr if not waiting; set only by user backend.
145  * syncRepState must not be touched except by owning process or WALSender.
146  * syncRepLinks used only while holding SyncRepLock.
147  */
148  XLogRecPtr waitLSN; /* waiting for this LSN or higher */
149  int syncRepState; /* wait state for sync rep */
150  SHM_QUEUE syncRepLinks; /* list link if process is in syncrep queue */
151 
152  /*
153  * All PROCLOCK objects for locks held or awaited by this backend are
154  * linked into one of these lists, according to the partition number of
155  * their lock.
156  */
158 
159  struct XidCache subxids; /* cache for subtransaction XIDs */
160 
161  /* Support for group XID clearing. */
162  /* true, if member of ProcArray group waiting for XID clear */
164  /* next ProcArray group member waiting for XID clear */
166 
167  /*
168  * latest transaction id among the transaction's main XID and
169  * subtransactions
170  */
172 
173  uint32 wait_event_info; /* proc's wait information */
174 
175  /* Support for group transaction status update. */
176  bool clogGroupMember; /* true, if member of clog group */
177  pg_atomic_uint32 clogGroupNext; /* next clog group member */
178  TransactionId clogGroupMemberXid; /* transaction id of clog group member */
179  XidStatus clogGroupMemberXidStatus; /* transaction status of clog
180  * group member */
181  int clogGroupMemberPage; /* clog page corresponding to
182  * transaction id of clog group member */
183  XLogRecPtr clogGroupMemberLsn; /* WAL location of commit record for clog
184  * group member */
185 
186  /* Per-backend LWLock. Protects fields below (but not group fields). */
188 
189  /* Lock manager data, recording fast-path locks taken by this backend. */
190  uint64 fpLockBits; /* lock modes held for each fast-path slot */
191  Oid fpRelId[FP_LOCK_SLOTS_PER_BACKEND]; /* slots for rel oids */
192  bool fpVXIDLock; /* are we holding a fast-path VXID lock? */
193  LocalTransactionId fpLocalTransactionId; /* lxid for fast-path VXID
194  * lock */
195 
196  /*
197  * Support for lock groups. Use LockHashPartitionLockByProc on the group
198  * leader to get the LWLock protecting these fields.
199  */
200  PGPROC *lockGroupLeader; /* lock group leader, if I'm a member */
201  dlist_head lockGroupMembers; /* list of members, if I'm a leader */
202  dlist_node lockGroupLink; /* my member link, if I'm a member */
203 };
204 
205 /* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
206 
207 
208 extern PGDLLIMPORT PGPROC *MyProc;
209 extern PGDLLIMPORT struct PGXACT *MyPgXact;
210 
211 /*
212  * Prior to PostgreSQL 9.2, the fields below were stored as part of the
213  * PGPROC. However, benchmarking revealed that packing these particular
214  * members into a separate array as tightly as possible sped up GetSnapshotData
215  * considerably on systems with many CPU cores, by reducing the number of
216  * cache lines needing to be fetched. Thus, think very carefully before adding
217  * anything else here.
218  */
219 typedef struct PGXACT
220 {
221  TransactionId xid; /* id of top-level transaction currently being
222  * executed by this proc, if running and XID
223  * is assigned; else InvalidTransactionId */
224 
225  TransactionId xmin; /* minimal running XID as it was when we were
226  * starting our xact, excluding LAZY VACUUM:
227  * vacuum must not remove tuples deleted by
228  * xid >= xmin ! */
229 
230  uint8 vacuumFlags; /* vacuum-related flags, see above */
232  bool delayChkpt; /* true if this proc delays checkpoint start;
233  * previously called InCommit */
234 
236 } PGXACT;
237 
238 /*
239  * There is one ProcGlobal struct for the whole database cluster.
240  */
241 typedef struct PROC_HDR
242 {
243  /* Array of PGPROC structures (not including dummies for prepared txns) */
245  /* Array of PGXACT structures (not including dummies for prepared txns) */
247  /* Length of allProcs array */
249  /* Head of list of free PGPROC structures */
251  /* Head of list of autovacuum's free PGPROC structures */
253  /* Head of list of bgworker free PGPROC structures */
255  /* First pgproc waiting for group XID clear */
257  /* First pgproc waiting for group transaction status update */
259  /* WALWriter process's latch */
261  /* Checkpointer process's latch */
263  /* Current shared estimate of appropriate spins_per_delay value */
265  /* The proc of the Startup process, since not in ProcArray */
268  /* Buffer id of the buffer that Startup process waits for pin on, or -1 */
270 } PROC_HDR;
271 
272 extern PROC_HDR *ProcGlobal;
273 
274 extern PGPROC *PreparedXactProcs;
275 
276 /* Accessor for PGPROC given a pgprocno. */
277 #define GetPGProcByNumber(n) (&ProcGlobal->allProcs[(n)])
278 
279 /*
280  * We set aside some extra PGPROC structures for auxiliary processes,
281  * ie things that aren't full-fledged backends but need shmem access.
282  *
283  * Background writer, checkpointer and WAL writer run during normal operation.
284  * Startup process and WAL receiver also consume 2 slots, but WAL writer is
285  * launched only after startup has exited, so we only need 4 slots.
286  */
287 #define NUM_AUXILIARY_PROCS 4
288 
289 /* configurable options */
290 extern int DeadlockTimeout;
291 extern int StatementTimeout;
292 extern int LockTimeout;
294 extern bool log_lock_waits;
295 
296 
297 /*
298  * Function Prototypes
299  */
300 extern int ProcGlobalSemas(void);
301 extern Size ProcGlobalShmemSize(void);
302 extern void InitProcGlobal(void);
303 extern void InitProcess(void);
304 extern void InitProcessPhase2(void);
305 extern void InitAuxiliaryProcess(void);
306 
307 extern void PublishStartupProcessInformation(void);
308 extern void SetStartupBufferPinWaitBufId(int bufid);
309 extern int GetStartupBufferPinWaitBufId(void);
310 
311 extern bool HaveNFreeProcs(int n);
312 extern void ProcReleaseLocks(bool isCommit);
313 
314 extern void ProcQueueInit(PROC_QUEUE *queue);
315 extern int ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable);
316 extern PGPROC *ProcWakeup(PGPROC *proc, int waitStatus);
317 extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
318 extern void CheckDeadLockAlert(void);
319 extern bool IsWaitingForLock(void);
320 extern void LockErrorCleanup(void);
321 
322 extern void ProcWaitForSignal(uint32 wait_event_info);
323 extern void ProcSendSignal(int pid);
324 
325 extern PGPROC *AuxiliaryPidGetProc(int pid);
326 
327 extern void BecomeLockGroupLeader(void);
328 extern bool BecomeLockGroupMember(PGPROC *leader, int pid);
329 
330 #endif /* PROC_H */
PGDLLIMPORT PGPROC * MyProc
Definition: proc.c:67
PROC_HDR * ProcGlobal
Definition: proc.c:80
bool procArrayGroupMember
Definition: proc.h:163
Definition: lwlock.h:32
XidStatus clogGroupMemberXidStatus
Definition: proc.h:179
bool log_lock_waits
Definition: proc.c:64
BackendId backendId
Definition: proc.h:113
uint32 TransactionId
Definition: c.h:391
uint32 wait_event_info
Definition: proc.h:173
Definition: proc.h:219
int LOCKMODE
Definition: lockdefs.h:26
XLogRecPtr clogGroupMemberLsn
Definition: proc.h:183
dlist_head lockGroupMembers
Definition: proc.h:201
TransactionId xmin
Definition: proc.h:225
PGXACT * allPgXact
Definition: proc.h:246
TransactionId xid
Definition: proc.h:221
SHM_QUEUE links
Definition: proc.h:98
LOCKMODE waitLockMode
Definition: proc.h:138
int DeadlockTimeout
Definition: proc.c:60
bool lwWaiting
Definition: proc.h:127
unsigned char uint8
Definition: c.h:256
int XidStatus
Definition: clog.h:24
PGPROC * bgworkerFreeProcs
Definition: proc.h:254
int GetStartupBufferPinWaitBufId(void)
Definition: proc.c:632
int ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable)
Definition: proc.c:1037
Oid roleId
Definition: proc.h:115
void LockErrorCleanup(void)
Definition: proc.c:686
uint8 lwWaitMode
Definition: proc.h:128
pg_atomic_uint32 clogGroupNext
Definition: proc.h:177
bool fpVXIDLock
Definition: proc.h:192
unsigned int Oid
Definition: postgres_ext.h:31
PGPROC * autovacFreeProcs
Definition: proc.h:252
void ProcSendSignal(int pid)
Definition: proc.c:1786
PGPROC * ProcWakeup(PGPROC *proc, int waitStatus)
Definition: proc.c:1568
void SetStartupBufferPinWaitBufId(int bufid)
Definition: proc.c:620
Latch procLatch
Definition: proc.h:104
void InitProcessPhase2(void)
Definition: proc.c:456
#define PGDLLIMPORT
Definition: c.h:1069
PGPROC * AuxiliaryPidGetProc(int pid)
Definition: proc.c:960
uint8 vacuumFlags
Definition: proc.h:230
bool isBackgroundWorker
Definition: proc.h:117
dlist_node lockGroupLink
Definition: proc.h:202
Latch * walwriterLatch
Definition: proc.h:260
int spins_per_delay
Definition: proc.h:264
bool BecomeLockGroupMember(PGPROC *leader, int pid)
Definition: proc.c:1854
int ProcGlobalSemas(void)
Definition: proc.c:128
int IdleInTransactionSessionTimeout
Definition: proc.c:63
int LockTimeout
Definition: proc.c:62
proclist_node lwWaitLink
Definition: proc.h:129
void ProcWaitForSignal(uint32 wait_event_info)
Definition: proc.c:1775
PROCLOCK * waitProcLock
Definition: proc.h:137
Definition: latch.h:110
int clogGroupMemberPage
Definition: proc.h:181
#define FP_LOCK_SLOTS_PER_BACKEND
Definition: proc.h:71
bool recoveryConflictPending
Definition: proc.h:124
uint8 nxids
Definition: proc.h:235
int StatementTimeout
Definition: proc.c:61
bool HaveNFreeProcs(int n)
Definition: proc.c:646
TransactionId clogGroupMemberXid
Definition: proc.h:178
Oid databaseId
Definition: proc.h:114
unsigned int uint32
Definition: c.h:258
PGPROC ** procgloballist
Definition: proc.h:99
Definition: lock.h:284
uint32 LocalTransactionId
Definition: c.h:393
LOCK * waitLock
Definition: proc.h:136
void ProcReleaseLocks(bool isCommit)
Definition: proc.c:762
struct XidCache subxids
Definition: proc.h:159
bool delayChkpt
Definition: proc.h:232
void BecomeLockGroupLeader(void)
Definition: proc.c:1824
void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
Definition: proc.c:1604
pg_atomic_uint32 procArrayGroupNext
Definition: proc.h:165
Definition: proc.h:241
PGDLLIMPORT struct PGXACT * MyPgXact
Definition: proc.c:68
PGPROC * PreparedXactProcs
Definition: proc.c:82
int startupProcPid
Definition: proc.h:267
uint64 fpLockBits
Definition: proc.h:190
int BackendId
Definition: backendid.h:21
bool overflowed
Definition: proc.h:231
int waitStatus
Definition: proc.h:102
#define PGPROC_MAX_CACHED_SUBXIDS
Definition: proc.h:36
Latch * checkpointerLatch
Definition: proc.h:262
TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS]
Definition: proc.h:40
Oid fpRelId[FP_LOCK_SLOTS_PER_BACKEND]
Definition: proc.h:191
void InitProcGlobal(void)
Definition: proc.c:162
void ProcQueueInit(PROC_QUEUE *queue)
Definition: proc.c:1013
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void CheckDeadLockAlert(void)
Definition: proc.c:1752
PGPROC * freeProcs
Definition: proc.h:250
void InitAuxiliaryProcess(void)
Definition: proc.c:491
LWLock backendLock
Definition: proc.h:187
size_t Size
Definition: c.h:350
SHM_QUEUE syncRepLinks
Definition: proc.h:150
uint32 allProcCount
Definition: proc.h:248
int LOCKMASK
Definition: lockdefs.h:25
proclist_node cvWaitLink
Definition: proc.h:132
pg_atomic_uint32 clogGroupFirst
Definition: proc.h:258
void PublishStartupProcessInformation(void)
Definition: proc.c:602
Definition: lock.h:344
int pgprocno
Definition: proc.h:110
pg_atomic_uint32 procArrayGroupFirst
Definition: proc.h:256
bool IsWaitingForLock(void)
Definition: proc.c:669
bool clogGroupMember
Definition: proc.h:176
int startupBufferPinWaitBufId
Definition: proc.h:269
void InitProcess(void)
Definition: proc.c:288
PGPROC * allProcs
Definition: proc.h:244
SHM_QUEUE myProcLocks[NUM_LOCK_PARTITIONS]
Definition: proc.h:157
struct PROC_HDR PROC_HDR
PGPROC * startupProc
Definition: proc.h:266
PGSemaphore sem
Definition: proc.h:101
int syncRepState
Definition: proc.h:149
Definition: proc.h:95
int pid
Definition: proc.h:109
Size ProcGlobalShmemSize(void)
Definition: proc.c:102
XLogRecPtr waitLSN
Definition: proc.h:148
PGPROC * lockGroupLeader
Definition: proc.h:200
LocalTransactionId fpLocalTransactionId
Definition: proc.h:193
TransactionId procArrayGroupMemberXid
Definition: proc.h:171
LOCKMASK heldLocks
Definition: proc.h:139
#define NUM_LOCK_PARTITIONS
Definition: lwlock.h:117
Definition: proc.h:38
LocalTransactionId lxid
Definition: proc.h:106
struct PGXACT PGXACT