1 /*-------------------------------------------------------------------------
4 * per-process shared memory data structures
7 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * src/include/storage/proc.h
12 *-------------------------------------------------------------------------
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"
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.
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.
34 #define PGPROC_MAX_CACHED_SUBXIDS 64 /* XXX guessed-at value */
38 TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS];
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 */
49 /* flags reset at EOXact */
50 #define PROC_VACUUM_STATE_MASK \
51 (PROC_IN_VACUUM | PROC_IN_ANALYZE | PROC_VACUUM_FOR_WRAPAROUND)
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.
59 #define FP_LOCK_SLOTS_PER_BACKEND 16
62 * Each backend has a PGPROC struct in shared memory. There is also a list of
63 * currently-unused PGPROC structs that will be reallocated to new backends.
65 * links: list link for any list the PGPROC is in. When waiting for a lock,
66 * the PGPROC is linked into that lock's waitProcs queue. A recycled PGPROC
67 * is linked into ProcGlobal's freeProcs list.
69 * Note: twophase.c also sets up a dummy PGPROC struct for each currently
70 * prepared transaction. These PGPROCs appear in the ProcArray data structure
71 * so that the prepared transactions appear to be still running and are
72 * correctly shown as holding locks. A prepared transaction PGPROC can be
73 * distinguished from a real one at need by the fact that it has pid == 0.
74 * The semaphore and lock-activity fields in a prepared-xact PGPROC are unused,
75 * but its myProcLocks[] lists are valid.
79 /* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
80 SHM_QUEUE links; /* list link if process is in a list */
82 PGSemaphoreData sem; /* ONE semaphore to sleep on */
83 int waitStatus; /* STATUS_WAITING, STATUS_OK or STATUS_ERROR */
85 Latch procLatch; /* generic latch for process */
87 LocalTransactionId lxid; /* local id of top-level transaction currently
88 * being executed by this proc, if running;
89 * else InvalidLocalTransactionId */
90 int pid; /* Backend's process ID; 0 if prepared xact */
93 /* These fields are zero while a backend is still starting up: */
94 BackendId backendId; /* This backend's backend ID (if assigned) */
95 Oid databaseId; /* OID of database this backend is using */
96 Oid roleId; /* OID of role using this backend */
99 * While in hot standby mode, shows that a conflict signal has been sent
100 * for the current transaction. Set/cleared while holding ProcArrayLock,
101 * though not required. Accessed without lock, if needed.
103 bool recoveryConflictPending;
105 /* Info about LWLock the process is currently waiting for, if any. */
106 bool lwWaiting; /* true if waiting for an LW lock */
107 uint8 lwWaitMode; /* lwlock mode being waited for */
108 dlist_node lwWaitLink; /* position in LW lock wait list */
110 /* Info about lock the process is currently waiting for, if any. */
111 /* waitLock and waitProcLock are NULL if not currently waiting. */
112 LOCK *waitLock; /* Lock object we're sleeping on ... */
113 PROCLOCK *waitProcLock; /* Per-holder info for awaited lock */
114 LOCKMODE waitLockMode; /* type of lock we're waiting for */
115 LOCKMASK heldLocks; /* bitmask for lock types already held on this
116 * lock object by this backend */
119 * Info to allow us to wait for synchronous replication, if needed.
120 * waitLSN is InvalidXLogRecPtr if not waiting; set only by user backend.
121 * syncRepState must not be touched except by owning process or WALSender.
122 * syncRepLinks used only while holding SyncRepLock.
124 XLogRecPtr waitLSN; /* waiting for this LSN or higher */
125 int syncRepState; /* wait state for sync rep */
126 SHM_QUEUE syncRepLinks; /* list link if process is in syncrep queue */
129 * All PROCLOCK objects for locks held or awaited by this backend are
130 * linked into one of these lists, according to the partition number of
133 SHM_QUEUE myProcLocks[NUM_LOCK_PARTITIONS];
135 struct XidCache subxids; /* cache for subtransaction XIDs */
137 /* Per-backend LWLock. Protects fields below. */
138 LWLock *backendLock; /* protects the fields below */
140 /* Lock manager data, recording fast-path locks taken by this backend. */
141 uint64 fpLockBits; /* lock modes held for each fast-path slot */
142 Oid fpRelId[FP_LOCK_SLOTS_PER_BACKEND]; /* slots for rel oids */
143 bool fpVXIDLock; /* are we holding a fast-path VXID lock? */
144 LocalTransactionId fpLocalTransactionId; /* lxid for fast-path VXID
148 /* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
151 extern PGDLLIMPORT PGPROC *MyProc;
152 extern PGDLLIMPORT struct PGXACT *MyPgXact;
155 * Prior to PostgreSQL 9.2, the fields below were stored as part of the
156 * PGPROC. However, benchmarking revealed that packing these particular
157 * members into a separate array as tightly as possible sped up GetSnapshotData
158 * considerably on systems with many CPU cores, by reducing the number of
159 * cache lines needing to be fetched. Thus, think very carefully before adding
160 * anything else here.
162 typedef struct PGXACT
164 TransactionId xid; /* id of top-level transaction currently being
165 * executed by this proc, if running and XID
166 * is assigned; else InvalidTransactionId */
168 TransactionId xmin; /* minimal running XID as it was when we were
169 * starting our xact, excluding LAZY VACUUM:
170 * vacuum must not remove tuples deleted by
173 uint8 vacuumFlags; /* vacuum-related flags, see above */
175 bool delayChkpt; /* true if this proc delays checkpoint start;
176 * previously called InCommit */
182 * There is one ProcGlobal struct for the whole database cluster.
184 typedef struct PROC_HDR
186 /* Array of PGPROC structures (not including dummies for prepared txns) */
188 /* Array of PGXACT structures (not including dummies for prepared txns) */
190 /* Length of allProcs array */
192 /* Head of list of free PGPROC structures */
194 /* Head of list of autovacuum's free PGPROC structures */
195 PGPROC *autovacFreeProcs;
196 /* Head of list of bgworker free PGPROC structures */
197 PGPROC *bgworkerFreeProcs;
198 /* WALWriter process's latch */
199 Latch *walwriterLatch;
200 /* Checkpointer process's latch */
201 Latch *checkpointerLatch;
202 /* Current shared estimate of appropriate spins_per_delay value */
204 /* The proc of the Startup process, since not in ProcArray */
207 /* Buffer id of the buffer that Startup process waits for pin on, or -1 */
208 int startupBufferPinWaitBufId;
211 extern PROC_HDR *ProcGlobal;
213 extern PGPROC *PreparedXactProcs;
216 * We set aside some extra PGPROC structures for auxiliary processes,
217 * ie things that aren't full-fledged backends but need shmem access.
219 * Background writer, checkpointer and WAL writer run during normal operation.
220 * Startup process and WAL receiver also consume 2 slots, but WAL writer is
221 * launched only after startup has exited, so we only need 4 slots.
223 #define NUM_AUXILIARY_PROCS 4
226 /* configurable options */
227 extern int DeadlockTimeout;
228 extern int StatementTimeout;
229 extern int LockTimeout;
230 extern bool log_lock_waits;
234 * Function Prototypes
236 extern int ProcGlobalSemas(void);
237 extern Size ProcGlobalShmemSize(void);
238 extern void InitProcGlobal(void);
239 extern void InitProcess(void);
240 extern void InitProcessPhase2(void);
241 extern void InitAuxiliaryProcess(void);
243 extern void PublishStartupProcessInformation(void);
244 extern void SetStartupBufferPinWaitBufId(int bufid);
245 extern int GetStartupBufferPinWaitBufId(void);
247 extern bool HaveNFreeProcs(int n);
248 extern void ProcReleaseLocks(bool isCommit);
250 extern void ProcQueueInit(PROC_QUEUE *queue);
251 extern int ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable);
252 extern PGPROC *ProcWakeup(PGPROC *proc, int waitStatus);
253 extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
254 extern void CheckDeadLockAlert(void);
255 extern bool IsWaitingForLock(void);
256 extern void LockErrorCleanup(void);
258 extern void ProcWaitForSignal(void);
259 extern void ProcSendSignal(int pid);