proc.h 9.4 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * proc.h
4
 *	  per-process shared memory data structures
5 6
 *
 *
B
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * src/include/storage/proc.h
11 12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
#ifndef _PROC_H_
#define _PROC_H_

17
#include "access/xlog.h"
18
#include "datatype/timestamp.h"
19
#include "storage/latch.h"
20
#include "storage/lock.h"
21
#include "storage/pg_sema.h"
22

23 24
/*
 * Each backend advertises up to PGPROC_MAX_CACHED_SUBXIDS TransactionIds
B
Bruce Momjian 已提交
25
 * for non-aborted subtransactions of its current top transaction.	These
26 27 28 29 30 31 32 33
 * have to be treated as running XIDs by other backends.
 *
 * We also keep track of whether the cache overflowed (ie, the transaction has
 * generated at least one subtransaction that didn't fit in the cache).
 * If none of the caches have overflowed, we can assume that an XID that's not
 * listed anywhere in the PGPROC array is not a running transaction.  Else we
 * have to look at pg_subtrans.
 */
B
Bruce Momjian 已提交
34
#define PGPROC_MAX_CACHED_SUBXIDS 64	/* XXX guessed-at value */
35

B
Bruce Momjian 已提交
36 37 38
struct XidCache
{
	TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS];
39 40
};

41 42 43 44
/* Flags for PGPROC->vacuumFlags */
#define		PROC_IS_AUTOVACUUM	0x01	/* is it an autovac worker? */
#define		PROC_IN_VACUUM		0x02	/* currently running lazy vacuum */
#define		PROC_IN_ANALYZE		0x04	/* currently running analyze */
B
Bruce Momjian 已提交
45
#define		PROC_VACUUM_FOR_WRAPAROUND 0x08		/* set by autovac only */
46 47 48 49

/* flags reset at EOXact */
#define		PROC_VACUUM_STATE_MASK (0x0E)

50 51 52 53 54 55 56 57
/*
 * We allow a small number of "weak" relation locks (AccesShareLock,
 * RowShareLock, RowExclusiveLock) to be recorded in the PGPROC structure
 * rather than the main lock table.  This eases contention on the lock
 * manager LWLocks.  See storage/lmgr/README for additional details.
 */
#define		FP_LOCK_SLOTS_PER_BACKEND 16

58
/*
J
Jan Wieck 已提交
59 60
 * Each backend has a PGPROC struct in shared memory.  There is also a list of
 * currently-unused PGPROC structs that will be reallocated to new backends.
61
 *
B
Bruce Momjian 已提交
62
 * links: list link for any list the PGPROC is in.	When waiting for a lock,
J
Jan Wieck 已提交
63
 * the PGPROC is linked into that lock's waitProcs queue.  A recycled PGPROC
64
 * is linked into ProcGlobal's freeProcs list.
65 66 67 68 69 70
 *
 * Note: twophase.c also sets up a dummy PGPROC struct for each currently
 * prepared transaction.  These PGPROCs appear in the ProcArray data structure
 * so that the prepared transactions appear to be still running and are
 * correctly shown as holding locks.  A prepared transaction PGPROC can be
 * distinguished from a real one at need by the fact that it has pid == 0.
71 72
 * The semaphore and lock-activity fields in a prepared-xact PGPROC are unused,
 * but its myProcLocks[] lists are valid.
73
 */
J
Jan Wieck 已提交
74
struct PGPROC
75
{
76
	/* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
77 78
	SHM_QUEUE	links;			/* list link if process is in a list */

79
	PGSemaphoreData sem;		/* ONE semaphore to sleep on */
80
	int			waitStatus;		/* STATUS_WAITING, STATUS_OK or STATUS_ERROR */
81

82 83
	Latch		procLatch;		/* generic latch for process */

84 85 86
	LocalTransactionId lxid;	/* local id of top-level transaction currently
								 * being executed by this proc, if running;
								 * else InvalidLocalTransactionId */
87
	int			pid;			/* Backend's process ID; 0 if prepared xact */
88
	int			pgprocno;
89 90

	/* These fields are zero while a backend is still starting up: */
91
	BackendId	backendId;		/* This backend's backend ID (if assigned) */
92
	Oid			databaseId;		/* OID of database this backend is using */
93
	Oid			roleId;			/* OID of role using this backend */
94

95
	/*
96 97 98
	 * While in hot standby mode, shows that a conflict signal has been sent
	 * for the current transaction. Set/cleared while holding ProcArrayLock,
	 * though not required. Accessed without lock, if needed.
99
	 */
100
	bool		recoveryConflictPending;
101

102 103 104
	/* Info about LWLock the process is currently waiting for, if any. */
	bool		lwWaiting;		/* true if waiting for an LW lock */
	bool		lwExclusive;	/* true if waiting for exclusive access */
J
Jan Wieck 已提交
105
	struct PGPROC *lwWaitLink;	/* next waiter for same LW lock */
106

107
	/* Info about lock the process is currently waiting for, if any. */
108
	/* waitLock and waitProcLock are NULL if not currently waiting. */
109
	LOCK	   *waitLock;		/* Lock object we're sleeping on ... */
110
	PROCLOCK   *waitProcLock;	/* Per-holder info for awaited lock */
111
	LOCKMODE	waitLockMode;	/* type of lock we're waiting for */
B
Bruce Momjian 已提交
112 113
	LOCKMASK	heldLocks;		/* bitmask for lock types already held on this
								 * lock object by this backend */
114

115 116 117 118
	/*
	 * Info to allow us to wait for synchronous replication, if needed.
	 * waitLSN is InvalidXLogRecPtr if not waiting; set only by user backend.
	 * syncRepState must not be touched except by owning process or WALSender.
119
	 * syncRepLinks used only while holding SyncRepLock.
120
	 */
121 122 123
	XLogRecPtr	waitLSN;		/* waiting for this LSN or higher */
	int			syncRepState;	/* wait state for sync rep */
	SHM_QUEUE	syncRepLinks;	/* list link if process is in syncrep queue */
124

125 126 127 128 129 130
	/*
	 * All PROCLOCK objects for locks held or awaited by this backend are
	 * linked into one of these lists, according to the partition number of
	 * their lock.
	 */
	SHM_QUEUE	myProcLocks[NUM_LOCK_PARTITIONS];
131

B
Bruce Momjian 已提交
132
	struct XidCache subxids;	/* cache for subtransaction XIDs */
133 134 135 136 137 138 139

	/* Per-backend LWLock.  Protects fields below. */
	LWLockId	backendLock;	/* protects the fields below */

	/* Lock manager data, recording fast-path locks taken by this backend. */
	uint64		fpLockBits;		/* lock modes held for each fast-path slot */
	Oid			fpRelId[FP_LOCK_SLOTS_PER_BACKEND]; /* slots for rel oids */
140 141
	bool		fpVXIDLock;		/* are we holding a fast-path VXID lock? */
	LocalTransactionId fpLocalTransactionId;	/* lxid for fast-path VXID lock */
142 143
};

J
Jan Wieck 已提交
144
/* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
145

146

147
extern PGDLLIMPORT PGPROC *MyProc;
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
extern PGDLLIMPORT struct PGXACT *MyPgXact;

/*
 * Prior to PostgreSQL 9.2, the fieds below were stored as part of the
 * PGPROC.  However, benchmarking revealed that packing these particular
 * members into a separate array as tightly as possible sped up GetSnapshotData
 * considerably on systems with many CPU cores, by reducing the number of
 * cache lines needing to be fetched.  Thus, think very carefully before adding
 * anything else here.
 */
typedef struct PGXACT
{
	TransactionId xid;			/* id of top-level transaction currently being
								 * executed by this proc, if running and XID
								 * is assigned; else InvalidTransactionId */

	TransactionId xmin;			/* minimal running XID as it was when we were
								 * starting our xact, excluding LAZY VACUUM:
								 * vacuum must not remove tuples deleted by
								 * xid >= xmin ! */

	uint8		vacuumFlags;	/* vacuum-related flags, see above */
	bool		overflowed;
	bool		inCommit;		/* true if within commit critical section */
172

173 174
	uint8		nxids;
} PGXACT;
175 176

/*
177
 * There is one ProcGlobal struct for the whole database cluster.
178
 */
179
typedef struct PROC_HDR
180
{
181 182
	/* Array of PGPROC structures (not including dummies for prepared txns) */
	PGPROC	   *allProcs;
183 184
	/* Array of PGXACT structures (not including dummies for prepared txns */
	PGXACT	   *allPgXact;
185 186
	/* Length of allProcs array */
	uint32		allProcCount;
J
Jan Wieck 已提交
187
	/* Head of list of free PGPROC structures */
188
	PGPROC	   *freeProcs;
189
	/* Head of list of autovacuum's free PGPROC structures */
190
	PGPROC	   *autovacFreeProcs;
191 192
	/* Current shared estimate of appropriate spins_per_delay value */
	int			spins_per_delay;
193 194 195
	/* The proc of the Startup process, since not in ProcArray */
	PGPROC	   *startupProc;
	int			startupProcPid;
196
	/* Buffer id of the buffer that Startup process waits for pin on, or -1 */
197
	int			startupBufferPinWaitBufId;
198 199
} PROC_HDR;

200 201
extern PROC_HDR *ProcGlobal;

202 203
extern PGPROC *PreparedXactProcs;

204
/*
205
 * We set aside some extra PGPROC structures for auxiliary processes,
206
 * ie things that aren't full-fledged backends but need shmem access.
207
 *
208 209 210
 * Background writer, checkpointer and WAL writer run during normal operation.
 * Startup process and WAL receiver also consume 2 slots, but WAL writer is
 * launched only after startup has exited, so we only need 4 slots.
211
 */
212
#define NUM_AUXILIARY_PROCS		4
J
Jan Wieck 已提交
213 214


215
/* configurable options */
216
extern int	DeadlockTimeout;
217
extern int	StatementTimeout;
B
Bruce Momjian 已提交
218
extern bool log_lock_waits;
219

220 221
extern volatile bool cancel_from_timeout;

222

223 224 225
/*
 * Function Prototypes
 */
226
extern int	ProcGlobalSemas(void);
227
extern Size ProcGlobalShmemSize(void);
228
extern void InitProcGlobal(void);
229
extern void InitProcess(void);
230
extern void InitProcessPhase2(void);
231
extern void InitAuxiliaryProcess(void);
232 233

extern void PublishStartupProcessInformation(void);
234
extern void SetStartupBufferPinWaitBufId(int bufid);
B
Bruce Momjian 已提交
235
extern int	GetStartupBufferPinWaitBufId(void);
236

237
extern bool HaveNFreeProcs(int n);
238
extern void ProcReleaseLocks(bool isCommit);
239

240
extern void ProcQueueInit(PROC_QUEUE *queue);
241
extern int	ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable);
242
extern PGPROC *ProcWakeup(PGPROC *proc, int waitStatus);
243
extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
244
extern bool IsWaitingForLock(void);
245
extern void LockWaitCancel(void);
246

247
extern void ProcWaitForSignal(void);
248
extern void ProcSendSignal(int pid);
249

250 251 252
extern bool enable_sig_alarm(int delayms, bool is_statement_timeout);
extern bool disable_sig_alarm(bool is_statement_timeout);
extern void handle_sig_alarm(SIGNAL_ARGS);
253

254
extern bool enable_standby_sig_alarm(TimestampTz now,
B
Bruce Momjian 已提交
255
						 TimestampTz fin_time, bool deadlock_only);
256 257 258
extern bool disable_standby_sig_alarm(void);
extern void handle_standby_sig_alarm(SIGNAL_ARGS);

259
#endif   /* PROC_H */