]> granicus.if.org Git - postgresql/blob - src/include/storage/proc.h
Update copyright for 2016
[postgresql] / src / include / storage / proc.h
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 {
38         TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS];
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          */
110         bool            recoveryConflictPending;
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          */
140         SHM_QUEUE       myProcLocks[NUM_LOCK_PARTITIONS];
141
142         struct XidCache subxids;        /* cache for subtransaction XIDs */
143
144         /* Support for group XID clearing. */
145         bool                    clearXid;
146         pg_atomic_uint32        nextClearXidElem;
147         TransactionId   backendLatestXid;
148
149         /* Per-backend LWLock.  Protects fields below. */
150         LWLock     *backendLock;        /* protects the fields below */
151
152         /* Lock manager data, recording fast-path locks taken by this backend. */
153         uint64          fpLockBits;             /* lock modes held for each fast-path slot */
154         Oid                     fpRelId[FP_LOCK_SLOTS_PER_BACKEND];             /* slots for rel oids */
155         bool            fpVXIDLock;             /* are we holding a fast-path VXID lock? */
156         LocalTransactionId fpLocalTransactionId;        /* lxid for fast-path VXID
157                                                                                                  * lock */
158 };
159
160 /* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
161
162
163 extern PGDLLIMPORT PGPROC *MyProc;
164 extern PGDLLIMPORT struct PGXACT *MyPgXact;
165
166 /*
167  * Prior to PostgreSQL 9.2, the fields below were stored as part of the
168  * PGPROC.  However, benchmarking revealed that packing these particular
169  * members into a separate array as tightly as possible sped up GetSnapshotData
170  * considerably on systems with many CPU cores, by reducing the number of
171  * cache lines needing to be fetched.  Thus, think very carefully before adding
172  * anything else here.
173  */
174 typedef struct PGXACT
175 {
176         TransactionId xid;                      /* id of top-level transaction currently being
177                                                                  * executed by this proc, if running and XID
178                                                                  * is assigned; else InvalidTransactionId */
179
180         TransactionId xmin;                     /* minimal running XID as it was when we were
181                                                                  * starting our xact, excluding LAZY VACUUM:
182                                                                  * vacuum must not remove tuples deleted by
183                                                                  * xid >= xmin ! */
184
185         uint8           vacuumFlags;    /* vacuum-related flags, see above */
186         bool            overflowed;
187         bool            delayChkpt;             /* true if this proc delays checkpoint start;
188                                                                  * previously called InCommit */
189
190         uint8           nxids;
191 } PGXACT;
192
193 /*
194  * There is one ProcGlobal struct for the whole database cluster.
195  */
196 typedef struct PROC_HDR
197 {
198         /* Array of PGPROC structures (not including dummies for prepared txns) */
199         PGPROC     *allProcs;
200         /* Array of PGXACT structures (not including dummies for prepared txns) */
201         PGXACT     *allPgXact;
202         /* Length of allProcs array */
203         uint32          allProcCount;
204         /* Head of list of free PGPROC structures */
205         PGPROC     *freeProcs;
206         /* Head of list of autovacuum's free PGPROC structures */
207         PGPROC     *autovacFreeProcs;
208         /* Head of list of bgworker free PGPROC structures */
209         PGPROC     *bgworkerFreeProcs;
210         /* First pgproc waiting for group XID clear */
211         pg_atomic_uint32 firstClearXidElem;
212         /* WALWriter process's latch */
213         Latch      *walwriterLatch;
214         /* Checkpointer process's latch */
215         Latch      *checkpointerLatch;
216         /* Current shared estimate of appropriate spins_per_delay value */
217         int                     spins_per_delay;
218         /* The proc of the Startup process, since not in ProcArray */
219         PGPROC     *startupProc;
220         int                     startupProcPid;
221         /* Buffer id of the buffer that Startup process waits for pin on, or -1 */
222         int                     startupBufferPinWaitBufId;
223 } PROC_HDR;
224
225 extern PROC_HDR *ProcGlobal;
226
227 extern PGPROC *PreparedXactProcs;
228
229 /*
230  * We set aside some extra PGPROC structures for auxiliary processes,
231  * ie things that aren't full-fledged backends but need shmem access.
232  *
233  * Background writer, checkpointer and WAL writer run during normal operation.
234  * Startup process and WAL receiver also consume 2 slots, but WAL writer is
235  * launched only after startup has exited, so we only need 4 slots.
236  */
237 #define NUM_AUXILIARY_PROCS             4
238
239
240 /* configurable options */
241 extern int      DeadlockTimeout;
242 extern int      StatementTimeout;
243 extern int      LockTimeout;
244 extern bool log_lock_waits;
245
246
247 /*
248  * Function Prototypes
249  */
250 extern int      ProcGlobalSemas(void);
251 extern Size ProcGlobalShmemSize(void);
252 extern void InitProcGlobal(void);
253 extern void InitProcess(void);
254 extern void InitProcessPhase2(void);
255 extern void InitAuxiliaryProcess(void);
256
257 extern void PublishStartupProcessInformation(void);
258 extern void SetStartupBufferPinWaitBufId(int bufid);
259 extern int      GetStartupBufferPinWaitBufId(void);
260
261 extern bool HaveNFreeProcs(int n);
262 extern void ProcReleaseLocks(bool isCommit);
263
264 extern void ProcQueueInit(PROC_QUEUE *queue);
265 extern int      ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable);
266 extern PGPROC *ProcWakeup(PGPROC *proc, int waitStatus);
267 extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
268 extern void CheckDeadLockAlert(void);
269 extern bool IsWaitingForLock(void);
270 extern void LockErrorCleanup(void);
271
272 extern void ProcWaitForSignal(void);
273 extern void ProcSendSignal(int pid);
274
275 #endif   /* PROC_H */