]> granicus.if.org Git - postgresql/blob - src/include/storage/proc.h
Add per-user and per-database connection limit options.
[postgresql] / src / include / storage / proc.h
1 /*-------------------------------------------------------------------------
2  *
3  * proc.h
4  *        per-process shared memory data structures
5  *
6  *
7  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $PostgreSQL: pgsql/src/include/storage/proc.h,v 1.80 2005/07/31 17:19:22 tgl Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef _PROC_H_
15 #define _PROC_H_
16
17 #include "access/xlog.h"
18 #include "storage/lock.h"
19 #include "storage/pg_sema.h"
20
21
22 /*
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.
26  *
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.
32  */
33 #define PGPROC_MAX_CACHED_SUBXIDS 64    /* XXX guessed-at value */
34
35 struct XidCache
36 {
37         bool            overflowed;
38         int                     nxids;
39         TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS];
40 };
41
42 /*
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.
45  *
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.
49  *
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.
56  */
57 struct PGPROC
58 {
59         /* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
60         SHM_QUEUE       links;                  /* list link if process is in a list */
61
62         PGSemaphoreData sem;            /* ONE semaphore to sleep on */
63         int                     waitStatus;             /* STATUS_OK or STATUS_ERROR after wakeup */
64
65         TransactionId xid;                      /* transaction currently being executed by
66                                                                  * this proc */
67
68         TransactionId xmin;                     /* minimal running XID as it was when we
69                                                                  * were starting our xact: vacuum must not
70                                                                  * remove tuples deleted by xid >= xmin ! */
71
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 */
75
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 */
80
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
87                                                                  * this lock object by this backend */
88
89         SHM_QUEUE       procLocks;              /* list of PROCLOCK objects for locks held
90                                                                  * or awaited by this backend */
91
92         struct XidCache subxids;        /* cache for subtransaction XIDs */
93 };
94
95 /* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
96
97
98 extern DLLIMPORT PGPROC *MyProc;
99
100
101 /*
102  * There is one ProcGlobal struct for the whole installation.
103  */
104 typedef struct PROC_HDR
105 {
106         /* Head of list of free PGPROC structures */
107         SHMEM_OFFSET freeProcs;
108 } PROC_HDR;
109
110
111 #define DUMMY_PROC_DEFAULT      0
112 #define DUMMY_PROC_BGWRITER 1
113 #define NUM_DUMMY_PROCS         2
114
115
116 /* configurable options */
117 extern int      DeadlockTimeout;
118 extern int      StatementTimeout;
119
120
121 /*
122  * Function Prototypes
123  */
124 extern int      ProcGlobalSemas(void);
125 extern int      ProcGlobalShmemSize(void);
126 extern void InitProcGlobal(void);
127 extern void InitProcess(void);
128 extern void InitDummyProcess(int proctype);
129 extern bool HaveNFreeProcs(int n);
130 extern void ProcReleaseLocks(bool isCommit);
131
132 extern void ProcQueueInit(PROC_QUEUE *queue);
133 extern int ProcSleep(LockMethod lockMethodTable, LOCKMODE lockmode,
134                   LOCK *lock, PROCLOCK *proclock);
135 extern PGPROC *ProcWakeup(PGPROC *proc, int waitStatus);
136 extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
137 extern bool LockWaitCancel(void);
138
139 extern void ProcWaitForSignal(void);
140 extern void ProcCancelWaitForSignal(void);
141 extern void ProcSendSignal(int pid);
142
143 extern bool enable_sig_alarm(int delayms, bool is_statement_timeout);
144 extern bool disable_sig_alarm(bool is_statement_timeout);
145 extern void handle_sig_alarm(SIGNAL_ARGS);
146
147 #endif   /* PROC_H */