proc.h 11 KB

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