proc.h 6.41 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * proc.h
4
 *	  per-process shared memory data structures
5 6
 *
 *
Bruce Momjian's avatar
Bruce Momjian committed
7
 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
8
 * Portions Copyright (c) 1994, Regents of the University of California
9
 *
10
 * $PostgreSQL: pgsql/src/include/storage/proc.h,v 1.111 2009/02/19 08:02:32 heikki Exp $
11 12 13 14 15 16
 *
 *-------------------------------------------------------------------------
 */
#ifndef _PROC_H_
#define _PROC_H_

17
#include "storage/lock.h"
18
#include "storage/pg_sema.h"
19

20

21 22
/*
 * Each backend advertises up to PGPROC_MAX_CACHED_SUBXIDS TransactionIds
Bruce Momjian's avatar
Bruce Momjian committed
23
 * for non-aborted subtransactions of its current top transaction.	These
24 25 26 27 28 29 30 31
 * 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.
 */
Bruce Momjian's avatar
Bruce Momjian committed
32
#define PGPROC_MAX_CACHED_SUBXIDS 64	/* XXX guessed-at value */
33

Bruce Momjian's avatar
Bruce Momjian committed
34 35 36 37 38
struct XidCache
{
	bool		overflowed;
	int			nxids;
	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 */
Bruce Momjian's avatar
Bruce Momjian committed
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
/*
Jan Wieck's avatar
Jan Wieck committed
51 52
 * 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.
53
 *
Bruce Momjian's avatar
Bruce Momjian committed
54
 * links: list link for any list the PGPROC is in.	When waiting for a lock,
Jan Wieck's avatar
Jan Wieck committed
55
 * the PGPROC is linked into that lock's waitProcs queue.  A recycled PGPROC
56
 * is linked into ProcGlobal's freeProcs list.
57 58 59 60 61 62
 *
 * 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.
63 64
 * The semaphore and lock-activity fields in a prepared-xact PGPROC are unused,
 * but its myProcLocks[] lists are valid.
65
 */
Jan Wieck's avatar
Jan Wieck committed
66
struct PGPROC
67
{
68
	/* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
69 70
	SHM_QUEUE	links;			/* list link if process is in a list */

71
	PGSemaphoreData sem;		/* ONE semaphore to sleep on */
72
	int			waitStatus;		/* STATUS_WAITING, STATUS_OK or STATUS_ERROR */
73

74 75 76 77 78 79 80
	LocalTransactionId lxid;	/* local id of top-level transaction currently
								 * being executed by this proc, if running;
								 * else InvalidLocalTransactionId */

	TransactionId xid;			/* id of top-level transaction currently being
								 * executed by this proc, if running and XID
								 * is assigned; else InvalidTransactionId */
81

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

87
	int			pid;			/* This backend's process id, or 0 */
88
	BackendId	backendId;		/* This backend's backend ID (if assigned) */
89
	Oid			databaseId;		/* OID of database this backend is using */
90
	Oid			roleId;			/* OID of role using this backend */
91

92 93
	bool		inCommit;		/* true if within commit critical section */

94
	uint8		vacuumFlags;	/* vacuum-related flags, see above */
Bruce Momjian's avatar
Bruce Momjian committed
95

96 97 98
	/* 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 */
Jan Wieck's avatar
Jan Wieck committed
99
	struct PGPROC *lwWaitLink;	/* next waiter for same LW lock */
100

101
	/* Info about lock the process is currently waiting for, if any. */
102
	/* waitLock and waitProcLock are NULL if not currently waiting. */
103
	LOCK	   *waitLock;		/* Lock object we're sleeping on ... */
104
	PROCLOCK   *waitProcLock;	/* Per-holder info for awaited lock */
105
	LOCKMODE	waitLockMode;	/* type of lock we're waiting for */
106 107
	LOCKMASK	heldLocks;		/* bitmask for lock types already held on this
								 * lock object by this backend */
108

109 110 111 112 113 114
	/*
	 * 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];
115

Bruce Momjian's avatar
Bruce Momjian committed
116
	struct XidCache subxids;	/* cache for subtransaction XIDs */
117 118
};

Jan Wieck's avatar
Jan Wieck committed
119
/* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
120

121

122
extern PGDLLIMPORT PGPROC *MyProc;
123

124 125

/*
126
 * There is one ProcGlobal struct for the whole database cluster.
127
 */
128
typedef struct PROC_HDR
129
{
Jan Wieck's avatar
Jan Wieck committed
130
	/* Head of list of free PGPROC structures */
131
	PGPROC	   *freeProcs;
132
	/* Head of list of autovacuum's free PGPROC structures */
133
	PGPROC	   *autovacFreeProcs;
134 135
	/* Current shared estimate of appropriate spins_per_delay value */
	int			spins_per_delay;
136 137
} PROC_HDR;

138
/*
139
 * We set aside some extra PGPROC structures for auxiliary processes,
140
 * ie things that aren't full-fledged backends but need shmem access.
141 142 143 144
 * 
 * Background writer, WAL writer, and autovacuum launcher run during
 * normal operation. When recovery has just finished, the startup
 * process can co-exist with them for a brief period before it exits.
145
 */
146
#define NUM_AUXILIARY_PROCS		4
Jan Wieck's avatar
Jan Wieck committed
147 148


149
/* configurable options */
150
extern int	DeadlockTimeout;
151
extern int	StatementTimeout;
Bruce Momjian's avatar
Bruce Momjian committed
152
extern bool log_lock_waits;
153

154 155
extern volatile bool cancel_from_timeout;

156

157 158 159
/*
 * Function Prototypes
 */
160
extern int	ProcGlobalSemas(void);
161
extern Size ProcGlobalShmemSize(void);
162
extern void InitProcGlobal(void);
163
extern void InitProcess(void);
164
extern void InitProcessPhase2(void);
165
extern void InitAuxiliaryProcess(void);
166
extern bool HaveNFreeProcs(int n);
167
extern void ProcReleaseLocks(bool isCommit);
168

169
extern void ProcQueueInit(PROC_QUEUE *queue);
170
extern int	ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable);
171
extern PGPROC *ProcWakeup(PGPROC *proc, int waitStatus);
172
extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
173
extern void LockWaitCancel(void);
174

175
extern void ProcWaitForSignal(void);
176
extern void ProcSendSignal(int pid);
177

178 179 180
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);
181

182
#endif   /* PROC_H */