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