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