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