1 /*-------------------------------------------------------------------------
4 * per-process shared memory data structures
7 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/include/storage/proc.h,v 1.84 2005/10/15 02:49:46 momjian Exp $
12 *-------------------------------------------------------------------------
17 #include "access/xlog.h"
18 #include "storage/lock.h"
19 #include "storage/pg_sema.h"
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.
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.
33 #define PGPROC_MAX_CACHED_SUBXIDS 64 /* XXX guessed-at value */
39 TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS];
43 * Each backend has a PGPROC struct in shared memory. There is also a list of
44 * currently-unused PGPROC structs that will be reallocated to new backends.
46 * links: list link for any list the PGPROC is in. When waiting for a lock,
47 * the PGPROC is linked into that lock's waitProcs queue. A recycled PGPROC
48 * is linked into ProcGlobal's freeProcs list.
50 * Note: twophase.c also sets up a dummy PGPROC struct for each currently
51 * prepared transaction. These PGPROCs appear in the ProcArray data structure
52 * so that the prepared transactions appear to be still running and are
53 * correctly shown as holding locks. A prepared transaction PGPROC can be
54 * distinguished from a real one at need by the fact that it has pid == 0.
55 * The semaphore and lock-related fields in a prepared-xact PGPROC are unused.
59 /* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
60 SHM_QUEUE links; /* list link if process is in a list */
62 PGSemaphoreData sem; /* ONE semaphore to sleep on */
63 int waitStatus; /* STATUS_OK or STATUS_ERROR after wakeup */
65 TransactionId xid; /* transaction currently being executed by
68 TransactionId xmin; /* minimal running XID as it was when we were
69 * starting our xact: vacuum must not remove
70 * tuples deleted by xid >= xmin ! */
72 int pid; /* This backend's process id, or 0 */
73 Oid databaseId; /* OID of database this backend is using */
74 Oid roleId; /* OID of role using this backend */
76 /* Info about LWLock the process is currently waiting for, if any. */
77 bool lwWaiting; /* true if waiting for an LW lock */
78 bool lwExclusive; /* true if waiting for exclusive access */
79 struct PGPROC *lwWaitLink; /* next waiter for same LW lock */
81 /* Info about lock the process is currently waiting for, if any. */
82 /* waitLock and waitProcLock are NULL if not currently waiting. */
83 LOCK *waitLock; /* Lock object we're sleeping on ... */
84 PROCLOCK *waitProcLock; /* Per-holder info for awaited lock */
85 LOCKMODE waitLockMode; /* type of lock we're waiting for */
86 LOCKMASK heldLocks; /* bitmask for lock types already held on this
87 * lock object by this backend */
89 SHM_QUEUE procLocks; /* list of PROCLOCK objects for locks held or
90 * awaited by this backend */
92 struct XidCache subxids; /* cache for subtransaction XIDs */
95 /* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
98 extern DLLIMPORT PGPROC *MyProc;
102 * There is one ProcGlobal struct for the whole installation.
104 typedef struct PROC_HDR
106 /* Head of list of free PGPROC structures */
107 SHMEM_OFFSET freeProcs;
108 /* Current shared estimate of appropriate spins_per_delay value */
113 #define DUMMY_PROC_DEFAULT 0
114 #define DUMMY_PROC_BGWRITER 1
115 #define NUM_DUMMY_PROCS 2
118 /* configurable options */
119 extern int DeadlockTimeout;
120 extern int StatementTimeout;
122 extern volatile bool cancel_from_timeout;
126 * Function Prototypes
128 extern int ProcGlobalSemas(void);
129 extern Size ProcGlobalShmemSize(void);
130 extern void InitProcGlobal(void);
131 extern void InitProcess(void);
132 extern void InitDummyProcess(int proctype);
133 extern bool HaveNFreeProcs(int n);
134 extern void ProcReleaseLocks(bool isCommit);
136 extern void ProcQueueInit(PROC_QUEUE *queue);
137 extern int ProcSleep(LockMethod lockMethodTable, LOCKMODE lockmode,
138 LOCK *lock, PROCLOCK *proclock);
139 extern PGPROC *ProcWakeup(PGPROC *proc, int waitStatus);
140 extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
141 extern bool LockWaitCancel(void);
143 extern void ProcWaitForSignal(void);
144 extern void ProcCancelWaitForSignal(void);
145 extern void ProcSendSignal(int pid);
147 extern bool enable_sig_alarm(int delayms, bool is_statement_timeout);
148 extern bool disable_sig_alarm(bool is_statement_timeout);
149 extern void handle_sig_alarm(SIGNAL_ARGS);