codekingpro/portable-devtools
116k
1/*-------------------------------------------------------------------------2 *3 * proc.h4 * per-process shared memory data structures5 *6 *7 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group8 * Portions Copyright (c) 1994, Regents of the University of California9 *10 * src/include/storage/proc.h11 *12 *-------------------------------------------------------------------------13 */14#ifndef _PROC_H_15#define _PROC_H_16 17#include "access/clog.h"18#include "access/xlogdefs.h"19#include "lib/ilist.h"20#include "storage/latch.h"21#include "storage/lock.h"22#include "storage/pg_sema.h"23#include "storage/proclist_types.h"24 25/*26 * Each backend advertises up to PGPROC_MAX_CACHED_SUBXIDS TransactionIds27 * for non-aborted subtransactions of its current top transaction. These28 * have to be treated as running XIDs by other backends.29 *30 * We also keep track of whether the cache overflowed (ie, the transaction has31 * generated at least one subtransaction that didn't fit in the cache).32 * If none of the caches have overflowed, we can assume that an XID that's not33 * listed anywhere in the PGPROC array is not a running transaction. Else we34 * have to look at pg_subtrans.35 *36 * See src/test/isolation/specs/subxid-overflow.spec if you change this.37 */38#define PGPROC_MAX_CACHED_SUBXIDS 64 /* XXX guessed-at value */39 40typedef struct XidCacheStatus41{42 /* number of cached subxids, never more than PGPROC_MAX_CACHED_SUBXIDS */43 uint8 count;44 /* has PGPROC->subxids overflowed */45 bool overflowed;46} XidCacheStatus;47 48struct XidCache49{50 TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS];51};52 53/*54 * Flags for PGPROC->statusFlags and PROC_HDR->statusFlags[]55 */56#define PROC_IS_AUTOVACUUM 0x01 /* is it an autovac worker? */57#define PROC_IN_VACUUM 0x02 /* currently running lazy vacuum */58#define PROC_IN_SAFE_IC 0x04 /* currently running CREATE INDEX59 * CONCURRENTLY or REINDEX60 * CONCURRENTLY on non-expressional,61 * non-partial index */62#define PROC_VACUUM_FOR_WRAPAROUND 0x08 /* set by autovac only */63#define PROC_IN_LOGICAL_DECODING 0x10 /* currently doing logical64 * decoding outside xact */65#define PROC_AFFECTS_ALL_HORIZONS 0x20 /* this proc's xmin must be66 * included in vacuum horizons67 * in all databases */68 69/* flags reset at EOXact */70#define PROC_VACUUM_STATE_MASK \71 (PROC_IN_VACUUM | PROC_IN_SAFE_IC | PROC_VACUUM_FOR_WRAPAROUND)72 73/*74 * Xmin-related flags. Make sure any flags that affect how the process' Xmin75 * value is interpreted by VACUUM are included here.76 */77#define PROC_XMIN_FLAGS (PROC_IN_VACUUM | PROC_IN_SAFE_IC)78 79/*80 * We allow a small number of "weak" relation locks (AccessShareLock,81 * RowShareLock, RowExclusiveLock) to be recorded in the PGPROC structure82 * rather than the main lock table. This eases contention on the lock83 * manager LWLocks. See storage/lmgr/README for additional details.84 */85#define FP_LOCK_SLOTS_PER_BACKEND 1686 87/*88 * An invalid pgprocno. Must be larger than the maximum number of PGPROC89 * structures we could possibly have. See comments for MAX_BACKENDS.90 */91#define INVALID_PGPROCNO PG_INT32_MAX92 93/*94 * Flags for PGPROC.delayChkptFlags95 *96 * These flags can be used to delay the start or completion of a checkpoint97 * for short periods. A flag is in effect if the corresponding bit is set in98 * the PGPROC of any backend.99 *100 * For our purposes here, a checkpoint has three phases: (1) determine the101 * location to which the redo pointer will be moved, (2) write all the102 * data durably to disk, and (3) WAL-log the checkpoint.103 *104 * Setting DELAY_CHKPT_START prevents the system from moving from phase 1105 * to phase 2. This is useful when we are performing a WAL-logged modification106 * of data that will be flushed to disk in phase 2. By setting this flag107 * before writing WAL and clearing it after we've both written WAL and108 * performed the corresponding modification, we ensure that if the WAL record109 * is inserted prior to the new redo point, the corresponding data changes will110 * also be flushed to disk before the checkpoint can complete. (In the111 * extremely common case where the data being modified is in shared buffers112 * and we acquire an exclusive content lock on the relevant buffers before113 * writing WAL, this mechanism is not needed, because phase 2 will block114 * until we release the content lock and then flush the modified data to115 * disk.)116 *117 * Setting DELAY_CHKPT_COMPLETE prevents the system from moving from phase 2118 * to phase 3. This is useful if we are performing a WAL-logged operation that119 * might invalidate buffers, such as relation truncation. In this case, we need120 * to ensure that any buffers which were invalidated and thus not flushed by121 * the checkpoint are actually destroyed on disk. Replay can cope with a file122 * or block that doesn't exist, but not with a block that has the wrong123 * contents.124 */125#define DELAY_CHKPT_START (1<<0)126#define DELAY_CHKPT_COMPLETE (1<<1)127 128typedef enum129{130 PROC_WAIT_STATUS_OK,131 PROC_WAIT_STATUS_WAITING,132 PROC_WAIT_STATUS_ERROR,133} ProcWaitStatus;134 135/*136 * Each backend has a PGPROC struct in shared memory. There is also a list of137 * currently-unused PGPROC structs that will be reallocated to new backends.138 *139 * links: list link for any list the PGPROC is in. When waiting for a lock,140 * the PGPROC is linked into that lock's waitProcs queue. A recycled PGPROC141 * is linked into ProcGlobal's freeProcs list.142 *143 * Note: twophase.c also sets up a dummy PGPROC struct for each currently144 * prepared transaction. These PGPROCs appear in the ProcArray data structure145 * so that the prepared transactions appear to be still running and are146 * correctly shown as holding locks. A prepared transaction PGPROC can be147 * distinguished from a real one at need by the fact that it has pid == 0.148 * The semaphore and lock-activity fields in a prepared-xact PGPROC are unused,149 * but its myProcLocks[] lists are valid.150 *151 * We allow many fields of this struct to be accessed without locks, such as152 * delayChkptFlags and isBackgroundWorker. However, keep in mind that writing153 * mirrored ones (see below) requires holding ProcArrayLock or XidGenLock in154 * at least shared mode, so that pgxactoff does not change concurrently.155 *156 * Mirrored fields:157 *158 * Some fields in PGPROC (see "mirrored in ..." comment) are mirrored into an159 * element of more densely packed ProcGlobal arrays. These arrays are indexed160 * by PGPROC->pgxactoff. Both copies need to be maintained coherently.161 *162 * NB: The pgxactoff indexed value can *never* be accessed without holding163 * locks.164 *165 * See PROC_HDR for details.166 */167struct PGPROC168{169 /* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */170 dlist_node links; /* list link if process is in a list */171 dlist_head *procgloballist; /* procglobal list that owns this PGPROC */172 173 PGSemaphore sem; /* ONE semaphore to sleep on */174 ProcWaitStatus waitStatus;175 176 Latch procLatch; /* generic latch for process */177 178 179 TransactionId xid; /* id of top-level transaction currently being180 * executed by this proc, if running and XID181 * is assigned; else InvalidTransactionId.182 * mirrored in ProcGlobal->xids[pgxactoff] */183 184 TransactionId xmin; /* minimal running XID as it was when we were185 * starting our xact, excluding LAZY VACUUM:186 * vacuum must not remove tuples deleted by187 * xid >= xmin ! */188 189 LocalTransactionId lxid; /* local id of top-level transaction currently190 * being executed by this proc, if running;191 * else InvalidLocalTransactionId */192 int pid; /* Backend's process ID; 0 if prepared xact */193 194 int pgxactoff; /* offset into various ProcGlobal->arrays with195 * data mirrored from this PGPROC */196 197 int pgprocno; /* Number of this PGPROC in198 * ProcGlobal->allProcs array. This is set199 * once by InitProcGlobal().200 * ProcGlobal->allProcs[n].pgprocno == n */201 202 /* These fields are zero while a backend is still starting up: */203 BackendId backendId; /* This backend's backend ID (if assigned) */204 Oid databaseId; /* OID of database this backend is using */205 Oid roleId; /* OID of role using this backend */206 207 Oid tempNamespaceId; /* OID of temp schema this backend is208 * using */209 210 bool isBackgroundWorker; /* true if background worker. */211 212 /*213 * While in hot standby mode, shows that a conflict signal has been sent214 * for the current transaction. Set/cleared while holding ProcArrayLock,215 * though not required. Accessed without lock, if needed.216 */217 bool recoveryConflictPending;218 219 /* Info about LWLock the process is currently waiting for, if any. */220 uint8 lwWaiting; /* see LWLockWaitState */221 uint8 lwWaitMode; /* lwlock mode being waited for */222 proclist_node lwWaitLink; /* position in LW lock wait list */223 224 /* Support for condition variables. */225 proclist_node cvWaitLink; /* position in CV wait list */226 227 /* Info about lock the process is currently waiting for, if any. */228 /* waitLock and waitProcLock are NULL if not currently waiting. */229 LOCK *waitLock; /* Lock object we're sleeping on ... */230 PROCLOCK *waitProcLock; /* Per-holder info for awaited lock */231 LOCKMODE waitLockMode; /* type of lock we're waiting for */232 LOCKMASK heldLocks; /* bitmask for lock types already held on this233 * lock object by this backend */234 pg_atomic_uint64 waitStart; /* time at which wait for lock acquisition235 * started */236 237 int delayChkptFlags; /* for DELAY_CHKPT_* flags */238 239 uint8 statusFlags; /* this backend's status flags, see PROC_*240 * above. mirrored in241 * ProcGlobal->statusFlags[pgxactoff] */242 243 /*244 * Info to allow us to wait for synchronous replication, if needed.245 * waitLSN is InvalidXLogRecPtr if not waiting; set only by user backend.246 * syncRepState must not be touched except by owning process or WALSender.247 * syncRepLinks used only while holding SyncRepLock.248 */249 XLogRecPtr waitLSN; /* waiting for this LSN or higher */250 int syncRepState; /* wait state for sync rep */251 dlist_node syncRepLinks; /* list link if process is in syncrep queue */252 253 /*254 * All PROCLOCK objects for locks held or awaited by this backend are255 * linked into one of these lists, according to the partition number of256 * their lock.257 */258 dlist_head myProcLocks[NUM_LOCK_PARTITIONS];259 260 XidCacheStatus subxidStatus; /* mirrored with261 * ProcGlobal->subxidStates[i] */262 struct XidCache subxids; /* cache for subtransaction XIDs */263 264 /* Support for group XID clearing. */265 /* true, if member of ProcArray group waiting for XID clear */266 bool procArrayGroupMember;267 /* next ProcArray group member waiting for XID clear */268 pg_atomic_uint32 procArrayGroupNext;269 270 /*271 * latest transaction id among the transaction's main XID and272 * subtransactions273 */274 TransactionId procArrayGroupMemberXid;275 276 uint32 wait_event_info; /* proc's wait information */277 278 /* Support for group transaction status update. */279 bool clogGroupMember; /* true, if member of clog group */280 pg_atomic_uint32 clogGroupNext; /* next clog group member */281 TransactionId clogGroupMemberXid; /* transaction id of clog group member */282 XidStatus clogGroupMemberXidStatus; /* transaction status of clog283 * group member */284 int clogGroupMemberPage; /* clog page corresponding to285 * transaction id of clog group member */286 XLogRecPtr clogGroupMemberLsn; /* WAL location of commit record for clog287 * group member */288 289 /* Lock manager data, recording fast-path locks taken by this backend. */290 LWLock fpInfoLock; /* protects per-backend fast-path state */291 uint64 fpLockBits; /* lock modes held for each fast-path slot */292 Oid fpRelId[FP_LOCK_SLOTS_PER_BACKEND]; /* slots for rel oids */293 bool fpVXIDLock; /* are we holding a fast-path VXID lock? */294 LocalTransactionId fpLocalTransactionId; /* lxid for fast-path VXID295 * lock */296 297 /*298 * Support for lock groups. Use LockHashPartitionLockByProc on the group299 * leader to get the LWLock protecting these fields.300 */301 PGPROC *lockGroupLeader; /* lock group leader, if I'm a member */302 dlist_head lockGroupMembers; /* list of members, if I'm a leader */303 dlist_node lockGroupLink; /* my member link, if I'm a member */304};305 306/* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */307 308 309extern PGDLLIMPORT PGPROC *MyProc;310 311/*312 * There is one ProcGlobal struct for the whole database cluster.313 *314 * Adding/Removing an entry into the procarray requires holding *both*315 * ProcArrayLock and XidGenLock in exclusive mode (in that order). Both are316 * needed because the dense arrays (see below) are accessed from317 * GetNewTransactionId() and GetSnapshotData(), and we don't want to add318 * further contention by both using the same lock. Adding/Removing a procarray319 * entry is much less frequent.320 *321 * Some fields in PGPROC are mirrored into more densely packed arrays (e.g.322 * xids), with one entry for each backend. These arrays only contain entries323 * for PGPROCs that have been added to the shared array with ProcArrayAdd()324 * (in contrast to PGPROC array which has unused PGPROCs interspersed).325 *326 * The dense arrays are indexed by PGPROC->pgxactoff. Any concurrent327 * ProcArrayAdd() / ProcArrayRemove() can lead to pgxactoff of a procarray328 * member to change. Therefore it is only safe to use PGPROC->pgxactoff to329 * access the dense array while holding either ProcArrayLock or XidGenLock.330 *331 * As long as a PGPROC is in the procarray, the mirrored values need to be332 * maintained in both places in a coherent manner.333 *334 * The denser separate arrays are beneficial for three main reasons: First, to335 * allow for as tight loops accessing the data as possible. Second, to prevent336 * updates of frequently changing data (e.g. xmin) from invalidating337 * cachelines also containing less frequently changing data (e.g. xid,338 * statusFlags). Third to condense frequently accessed data into as few339 * cachelines as possible.340 *341 * There are two main reasons to have the data mirrored between these dense342 * arrays and PGPROC. First, as explained above, a PGPROC's array entries can343 * only be accessed with either ProcArrayLock or XidGenLock held, whereas the344 * PGPROC entries do not require that (obviously there may still be locking345 * requirements around the individual field, separate from the concerns346 * here). That is particularly important for a backend to efficiently checks347 * it own values, which it often can safely do without locking. Second, the348 * PGPROC fields allow to avoid unnecessary accesses and modification to the349 * dense arrays. A backend's own PGPROC is more likely to be in a local cache,350 * whereas the cachelines for the dense array will be modified by other351 * backends (often removing it from the cache for other cores/sockets). At352 * commit/abort time a check of the PGPROC value can avoid accessing/dirtying353 * the corresponding array value.354 *355 * Basically it makes sense to access the PGPROC variable when checking a356 * single backend's data, especially when already looking at the PGPROC for357 * other reasons already. It makes sense to look at the "dense" arrays if we358 * need to look at many / most entries, because we then benefit from the359 * reduced indirection and better cross-process cache-ability.360 *361 * When entering a PGPROC for 2PC transactions with ProcArrayAdd(), the data362 * in the dense arrays is initialized from the PGPROC while it already holds363 * ProcArrayLock.364 */365typedef struct PROC_HDR366{367 /* Array of PGPROC structures (not including dummies for prepared txns) */368 PGPROC *allProcs;369 370 /* Array mirroring PGPROC.xid for each PGPROC currently in the procarray */371 TransactionId *xids;372 373 /*374 * Array mirroring PGPROC.subxidStatus for each PGPROC currently in the375 * procarray.376 */377 XidCacheStatus *subxidStates;378 379 /*380 * Array mirroring PGPROC.statusFlags for each PGPROC currently in the381 * procarray.382 */383 uint8 *statusFlags;384 385 /* Length of allProcs array */386 uint32 allProcCount;387 /* Head of list of free PGPROC structures */388 dlist_head freeProcs;389 /* Head of list of autovacuum's free PGPROC structures */390 dlist_head autovacFreeProcs;391 /* Head of list of bgworker free PGPROC structures */392 dlist_head bgworkerFreeProcs;393 /* Head of list of walsender free PGPROC structures */394 dlist_head walsenderFreeProcs;395 /* First pgproc waiting for group XID clear */396 pg_atomic_uint32 procArrayGroupFirst;397 /* First pgproc waiting for group transaction status update */398 pg_atomic_uint32 clogGroupFirst;399 /* WALWriter process's latch */400 Latch *walwriterLatch;401 /* Checkpointer process's latch */402 Latch *checkpointerLatch;403 /* Current shared estimate of appropriate spins_per_delay value */404 int spins_per_delay;405 /* Buffer id of the buffer that Startup process waits for pin on, or -1 */406 int startupBufferPinWaitBufId;407} PROC_HDR;408 409extern PGDLLIMPORT PROC_HDR *ProcGlobal;410 411extern PGDLLIMPORT PGPROC *PreparedXactProcs;412 413/* Accessor for PGPROC given a pgprocno. */414#define GetPGProcByNumber(n) (&ProcGlobal->allProcs[(n)])415 416/*417 * We set aside some extra PGPROC structures for auxiliary processes,418 * ie things that aren't full-fledged backends but need shmem access.419 *420 * Background writer, checkpointer, WAL writer and archiver run during normal421 * operation. Startup process and WAL receiver also consume 2 slots, but WAL422 * writer is launched only after startup has exited, so we only need 5 slots.423 */424#define NUM_AUXILIARY_PROCS 5425 426/* configurable options */427extern PGDLLIMPORT int DeadlockTimeout;428extern PGDLLIMPORT int StatementTimeout;429extern PGDLLIMPORT int LockTimeout;430extern PGDLLIMPORT int IdleInTransactionSessionTimeout;431extern PGDLLIMPORT int IdleSessionTimeout;432extern PGDLLIMPORT bool log_lock_waits;433 434 435/*436 * Function Prototypes437 */438extern int ProcGlobalSemas(void);439extern Size ProcGlobalShmemSize(void);440extern void InitProcGlobal(void);441extern void InitProcess(void);442extern void InitProcessPhase2(void);443extern void InitAuxiliaryProcess(void);444 445extern void SetStartupBufferPinWaitBufId(int bufid);446extern int GetStartupBufferPinWaitBufId(void);447 448extern bool HaveNFreeProcs(int n, int *nfree);449extern void ProcReleaseLocks(bool isCommit);450 451extern ProcWaitStatus ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable);452extern void ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus);453extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);454extern void CheckDeadLockAlert(void);455extern bool IsWaitingForLock(void);456extern void LockErrorCleanup(void);457 458extern void ProcWaitForSignal(uint32 wait_event_info);459extern void ProcSendSignal(int pgprocno);460 461extern PGPROC *AuxiliaryPidGetProc(int pid);462 463extern void BecomeLockGroupLeader(void);464extern bool BecomeLockGroupMember(PGPROC *leader, int pid);465 466#endif /* _PROC_H_ */467 