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