gooderp18绿色标准版
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

336 行
12KB

  1. /*-------------------------------------------------------------------------
  2. *
  3. * proc.h
  4. * per-process shared memory data structures
  5. *
  6. *
  7. * Portions Copyright (c) 1996-2019, 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/clog.h"
  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. #include "storage/proclist_types.h"
  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. struct XidCache
  36. {
  37. TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS];
  38. };
  39. /*
  40. * Flags for PGXACT->vacuumFlags
  41. *
  42. * Note: If you modify these flags, you need to modify PROCARRAY_XXX flags
  43. * in src/include/storage/procarray.h.
  44. *
  45. * PROC_RESERVED may later be assigned for use in vacuumFlags, but its value is
  46. * used for PROCARRAY_SLOTS_XMIN in procarray.h, so GetOldestXmin won't be able
  47. * to match and ignore processes with this flag set.
  48. */
  49. #define PROC_IS_AUTOVACUUM 0x01 /* is it an autovac worker? */
  50. #define PROC_IN_VACUUM 0x02 /* currently running lazy vacuum */
  51. #define PROC_IN_ANALYZE 0x04 /* currently running analyze */
  52. #define PROC_VACUUM_FOR_WRAPAROUND 0x08 /* set by autovac only */
  53. #define PROC_IN_LOGICAL_DECODING 0x10 /* currently doing logical
  54. * decoding outside xact */
  55. #define PROC_RESERVED 0x20 /* reserved for procarray */
  56. /* flags reset at EOXact */
  57. #define PROC_VACUUM_STATE_MASK \
  58. (PROC_IN_VACUUM | PROC_IN_ANALYZE | PROC_VACUUM_FOR_WRAPAROUND)
  59. /*
  60. * We allow a small number of "weak" relation locks (AccessShareLock,
  61. * RowShareLock, RowExclusiveLock) to be recorded in the PGPROC structure
  62. * rather than the main lock table. This eases contention on the lock
  63. * manager LWLocks. See storage/lmgr/README for additional details.
  64. */
  65. #define FP_LOCK_SLOTS_PER_BACKEND 16
  66. /*
  67. * An invalid pgprocno. Must be larger than the maximum number of PGPROC
  68. * structures we could possibly have. See comments for MAX_BACKENDS.
  69. */
  70. #define INVALID_PGPROCNO PG_INT32_MAX
  71. /*
  72. * Each backend has a PGPROC struct in shared memory. There is also a list of
  73. * currently-unused PGPROC structs that will be reallocated to new backends.
  74. *
  75. * links: list link for any list the PGPROC is in. When waiting for a lock,
  76. * the PGPROC is linked into that lock's waitProcs queue. A recycled PGPROC
  77. * is linked into ProcGlobal's freeProcs list.
  78. *
  79. * Note: twophase.c also sets up a dummy PGPROC struct for each currently
  80. * prepared transaction. These PGPROCs appear in the ProcArray data structure
  81. * so that the prepared transactions appear to be still running and are
  82. * correctly shown as holding locks. A prepared transaction PGPROC can be
  83. * distinguished from a real one at need by the fact that it has pid == 0.
  84. * The semaphore and lock-activity fields in a prepared-xact PGPROC are unused,
  85. * but its myProcLocks[] lists are valid.
  86. */
  87. struct PGPROC
  88. {
  89. /* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
  90. SHM_QUEUE links; /* list link if process is in a list */
  91. PGPROC **procgloballist; /* procglobal list that owns this PGPROC */
  92. PGSemaphore sem; /* ONE semaphore to sleep on */
  93. int waitStatus; /* STATUS_WAITING, STATUS_OK or STATUS_ERROR */
  94. Latch procLatch; /* generic latch for process */
  95. LocalTransactionId lxid; /* local id of top-level transaction currently
  96. * being executed by this proc, if running;
  97. * else InvalidLocalTransactionId */
  98. int pid; /* Backend's process ID; 0 if prepared xact */
  99. int pgprocno;
  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. Oid tempNamespaceId; /* OID of temp schema this backend is
  105. * using */
  106. bool isBackgroundWorker; /* true if background worker. */
  107. /*
  108. * While in hot standby mode, shows that a conflict signal has been sent
  109. * for the current transaction. Set/cleared while holding ProcArrayLock,
  110. * though not required. Accessed without lock, if needed.
  111. */
  112. bool recoveryConflictPending;
  113. /* Info about LWLock the process is currently waiting for, if any. */
  114. bool lwWaiting; /* true if waiting for an LW lock */
  115. uint8 lwWaitMode; /* lwlock mode being waited for */
  116. proclist_node lwWaitLink; /* position in LW lock wait list */
  117. /* Support for condition variables. */
  118. proclist_node cvWaitLink; /* position in CV wait list */
  119. /* Info about lock the process is currently waiting for, if any. */
  120. /* waitLock and waitProcLock are NULL if not currently waiting. */
  121. LOCK *waitLock; /* Lock object we're sleeping on ... */
  122. PROCLOCK *waitProcLock; /* Per-holder info for awaited lock */
  123. LOCKMODE waitLockMode; /* type of lock we're waiting for */
  124. LOCKMASK heldLocks; /* bitmask for lock types already held on this
  125. * lock object by this backend */
  126. /*
  127. * Info to allow us to wait for synchronous replication, if needed.
  128. * waitLSN is InvalidXLogRecPtr if not waiting; set only by user backend.
  129. * syncRepState must not be touched except by owning process or WALSender.
  130. * syncRepLinks used only while holding SyncRepLock.
  131. */
  132. XLogRecPtr waitLSN; /* waiting for this LSN or higher */
  133. int syncRepState; /* wait state for sync rep */
  134. SHM_QUEUE syncRepLinks; /* list link if process is in syncrep queue */
  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. struct XidCache subxids; /* cache for subtransaction XIDs */
  142. /* Support for group XID clearing. */
  143. /* true, if member of ProcArray group waiting for XID clear */
  144. bool procArrayGroupMember;
  145. /* next ProcArray group member waiting for XID clear */
  146. pg_atomic_uint32 procArrayGroupNext;
  147. /*
  148. * latest transaction id among the transaction's main XID and
  149. * subtransactions
  150. */
  151. TransactionId procArrayGroupMemberXid;
  152. uint32 wait_event_info; /* proc's wait information */
  153. /* Support for group transaction status update. */
  154. bool clogGroupMember; /* true, if member of clog group */
  155. pg_atomic_uint32 clogGroupNext; /* next clog group member */
  156. TransactionId clogGroupMemberXid; /* transaction id of clog group member */
  157. XidStatus clogGroupMemberXidStatus; /* transaction status of clog
  158. * group member */
  159. int clogGroupMemberPage; /* clog page corresponding to
  160. * transaction id of clog group member */
  161. XLogRecPtr clogGroupMemberLsn; /* WAL location of commit record for clog
  162. * group member */
  163. /* Per-backend LWLock. Protects fields below (but not group fields). */
  164. LWLock backendLock;
  165. /* Lock manager data, recording fast-path locks taken by this backend. */
  166. uint64 fpLockBits; /* lock modes held for each fast-path slot */
  167. Oid fpRelId[FP_LOCK_SLOTS_PER_BACKEND]; /* slots for rel oids */
  168. bool fpVXIDLock; /* are we holding a fast-path VXID lock? */
  169. LocalTransactionId fpLocalTransactionId; /* lxid for fast-path VXID
  170. * lock */
  171. /*
  172. * Support for lock groups. Use LockHashPartitionLockByProc on the group
  173. * leader to get the LWLock protecting these fields.
  174. */
  175. PGPROC *lockGroupLeader; /* lock group leader, if I'm a member */
  176. dlist_head lockGroupMembers; /* list of members, if I'm a leader */
  177. dlist_node lockGroupLink; /* my member link, if I'm a member */
  178. };
  179. /* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
  180. extern PGDLLIMPORT PGPROC *MyProc;
  181. extern PGDLLIMPORT struct PGXACT *MyPgXact;
  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. TransactionId xmin; /* minimal running XID as it was when we were
  196. * starting our xact, excluding LAZY VACUUM:
  197. * vacuum must not remove tuples deleted by
  198. * xid >= xmin ! */
  199. uint8 vacuumFlags; /* vacuum-related flags, see above */
  200. bool overflowed;
  201. bool delayChkpt; /* true if this proc delays checkpoint start;
  202. * previously called InCommit */
  203. uint8 nxids;
  204. } PGXACT;
  205. /*
  206. * There is one ProcGlobal struct for the whole database cluster.
  207. */
  208. typedef struct PROC_HDR
  209. {
  210. /* Array of PGPROC structures (not including dummies for prepared txns) */
  211. PGPROC *allProcs;
  212. /* Array of PGXACT structures (not including dummies for prepared txns) */
  213. PGXACT *allPgXact;
  214. /* Length of allProcs array */
  215. uint32 allProcCount;
  216. /* Head of list of free PGPROC structures */
  217. PGPROC *freeProcs;
  218. /* Head of list of autovacuum's free PGPROC structures */
  219. PGPROC *autovacFreeProcs;
  220. /* Head of list of bgworker free PGPROC structures */
  221. PGPROC *bgworkerFreeProcs;
  222. /* Head of list of walsender free PGPROC structures */
  223. PGPROC *walsenderFreeProcs;
  224. /* First pgproc waiting for group XID clear */
  225. pg_atomic_uint32 procArrayGroupFirst;
  226. /* First pgproc waiting for group transaction status update */
  227. pg_atomic_uint32 clogGroupFirst;
  228. /* WALWriter process's latch */
  229. Latch *walwriterLatch;
  230. /* Checkpointer process's latch */
  231. Latch *checkpointerLatch;
  232. /* Current shared estimate of appropriate spins_per_delay value */
  233. int spins_per_delay;
  234. /* The proc of the Startup process, since not in ProcArray */
  235. PGPROC *startupProc;
  236. int startupProcPid;
  237. /* Buffer id of the buffer that Startup process waits for pin on, or -1 */
  238. int startupBufferPinWaitBufId;
  239. } PROC_HDR;
  240. extern PGDLLIMPORT PROC_HDR *ProcGlobal;
  241. extern PGPROC *PreparedXactProcs;
  242. /* Accessor for PGPROC given a pgprocno. */
  243. #define GetPGProcByNumber(n) (&ProcGlobal->allProcs[(n)])
  244. /*
  245. * We set aside some extra PGPROC structures for auxiliary processes,
  246. * ie things that aren't full-fledged backends but need shmem access.
  247. *
  248. * Background writer, checkpointer and WAL writer run during normal operation.
  249. * Startup process and WAL receiver also consume 2 slots, but WAL writer is
  250. * launched only after startup has exited, so we only need 4 slots.
  251. */
  252. #define NUM_AUXILIARY_PROCS 4
  253. /* configurable options */
  254. extern PGDLLIMPORT int DeadlockTimeout;
  255. extern PGDLLIMPORT int StatementTimeout;
  256. extern PGDLLIMPORT int LockTimeout;
  257. extern PGDLLIMPORT int IdleInTransactionSessionTimeout;
  258. extern bool log_lock_waits;
  259. /*
  260. * Function Prototypes
  261. */
  262. extern int ProcGlobalSemas(void);
  263. extern Size ProcGlobalShmemSize(void);
  264. extern void InitProcGlobal(void);
  265. extern void InitProcess(void);
  266. extern void InitProcessPhase2(void);
  267. extern void InitAuxiliaryProcess(void);
  268. extern void PublishStartupProcessInformation(void);
  269. extern void SetStartupBufferPinWaitBufId(int bufid);
  270. extern int GetStartupBufferPinWaitBufId(void);
  271. extern bool HaveNFreeProcs(int n);
  272. extern void ProcReleaseLocks(bool isCommit);
  273. extern void ProcQueueInit(PROC_QUEUE *queue);
  274. extern int ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable);
  275. extern PGPROC *ProcWakeup(PGPROC *proc, int waitStatus);
  276. extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
  277. extern void CheckDeadLockAlert(void);
  278. extern bool IsWaitingForLock(void);
  279. extern void LockErrorCleanup(void);
  280. extern void ProcWaitForSignal(uint32 wait_event_info);
  281. extern void ProcSendSignal(int pid);
  282. extern PGPROC *AuxiliaryPidGetProc(int pid);
  283. extern void BecomeLockGroupLeader(void);
  284. extern bool BecomeLockGroupMember(PGPROC *leader, int pid);
  285. #endif /* PROC_H */
上海开阖软件有限公司 沪ICP备12045867号-1