* per-process shared memory data structures
*
*
- * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: proc.h,v 1.49 2001/09/29 21:35:14 momjian Exp $
+ * src/include/storage/proc.h
*
*-------------------------------------------------------------------------
*/
#define _PROC_H_
#include "access/xlog.h"
-#include "storage/backendid.h"
-#include "storage/ipc.h"
+#include "datatype/timestamp.h"
+#include "storage/latch.h"
#include "storage/lock.h"
+#include "storage/pg_sema.h"
+/*
+ * Each backend advertises up to PGPROC_MAX_CACHED_SUBXIDS TransactionIds
+ * for non-aborted subtransactions of its current top transaction. These
+ * 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.
+ */
+#define PGPROC_MAX_CACHED_SUBXIDS 64 /* XXX guessed-at value */
-typedef struct
+struct XidCache
{
- IpcSemaphoreId semId; /* SysV semaphore set ID */
- int semNum; /* semaphore number within set */
-} SEMA;
+ TransactionId xids[PGPROC_MAX_CACHED_SUBXIDS];
+};
+
+/* Flags for PGXACT->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 */
+#define PROC_VACUUM_FOR_WRAPAROUND 0x08 /* set by autovac only */
+
+/* flags reset at EOXact */
+#define PROC_VACUUM_STATE_MASK (0x0E)
/*
- * Each backend has a PROC struct in shared memory. There is also a list of
- * currently-unused PROC structs that will be reallocated to new backends.
+ * 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
+
+/*
+ * 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.
*
- * links: list link for any list the PROC is in. When waiting for a lock,
- * the PROC is linked into that lock's waitProcs queue. A recycled PROC
+ * links: list link for any list the PGPROC is in. When waiting for a lock,
+ * the PGPROC is linked into that lock's waitProcs queue. A recycled PGPROC
* is linked into ProcGlobal's freeProcs list.
+ *
+ * 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.
+ * The semaphore and lock-activity fields in a prepared-xact PGPROC are unused,
+ * but its myProcLocks[] lists are valid.
*/
-struct PROC
+struct PGPROC
{
/* proc->links MUST BE FIRST IN STRUCT (see ProcSleep,ProcWakeup,etc) */
SHM_QUEUE links; /* list link if process is in a list */
- SEMA sem; /* ONE semaphore to sleep on */
- int errType; /* STATUS_OK or STATUS_ERROR after wakeup */
+ PGSemaphoreData sem; /* ONE semaphore to sleep on */
+ int waitStatus; /* STATUS_WAITING, STATUS_OK or STATUS_ERROR */
- TransactionId xid; /* transaction currently being executed by
- * this proc */
+ Latch procLatch; /* generic latch for process */
- TransactionId xmin; /* minimal running XID as it was when we
- * were starting our xact: vacuum must not
- * remove tuples deleted by xid >= xmin ! */
+ LocalTransactionId lxid; /* local id of top-level transaction currently
+ * being executed by this proc, if running;
+ * else InvalidLocalTransactionId */
+ int pid; /* Backend's process ID; 0 if prepared xact */
+ int pgprocno;
- int pid; /* This backend's process id */
+ /* These fields are zero while a backend is still starting up: */
+ BackendId backendId; /* This backend's backend ID (if assigned) */
Oid databaseId; /* OID of database this backend is using */
+ Oid roleId; /* OID of role using this backend */
/*
- * XLOG location of first XLOG record written by this backend's
- * current transaction. If backend is not in a transaction or hasn't
- * yet modified anything, logRec.xrecoff is zero.
+ * 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.
*/
- XLogRecPtr logRec;
+ bool recoveryConflictPending;
/* 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 */
- struct PROC *lwWaitLink; /* next waiter for same LW lock */
+ uint8 lwWaitMode; /* lwlock mode being waited for */
+ struct PGPROC *lwWaitLink; /* next waiter for same LW lock */
/* Info about lock the process is currently waiting for, if any. */
- /* waitLock and waitHolder are NULL if not currently waiting. */
+ /* waitLock and waitProcLock are NULL if not currently waiting. */
LOCK *waitLock; /* Lock object we're sleeping on ... */
- PROCLOCK *waitHolder; /* Per-holder info for awaited lock */
+ PROCLOCK *waitProcLock; /* Per-holder info for awaited lock */
LOCKMODE waitLockMode; /* type of lock we're waiting for */
- LOCKMASK heldLocks; /* bitmask for lock types already held on
- * this lock object by this backend */
+ LOCKMASK heldLocks; /* bitmask for lock types already held on this
+ * lock object by this backend */
- SHM_QUEUE procHolders; /* list of PROCLOCK objects for locks held
- * or awaited by this backend */
-};
+ /*
+ * 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.
+ * syncRepLinks used only while holding SyncRepLock.
+ */
+ 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 */
+
+ /*
+ * 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];
-/* NOTE: "typedef struct PROC PROC" appears in storage/lock.h. */
+ struct XidCache subxids; /* cache for subtransaction XIDs */
+ /* 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 */
+ bool fpVXIDLock; /* are we holding a fast-path VXID lock? */
+ LocalTransactionId fpLocalTransactionId; /* lxid for fast-path VXID
+ * lock */
+};
+
+/* NOTE: "typedef struct PGPROC PGPROC" appears in storage/lock.h. */
-extern PROC *MyProc;
+extern PGDLLIMPORT PGPROC *MyProc;
+extern PGDLLIMPORT struct PGXACT *MyPgXact;
/*
- * There is one ProcGlobal struct for the whole installation.
- *
- * PROC_NSEMS_PER_SET is the number of semaphores in each sys-V semaphore set
- * we allocate. It must be no more than 32 (or however many bits in an int
- * on your machine), or our free-semaphores bitmap won't work. It also must
- * be *less than* your kernel's SEMMSL (max semaphores per set) parameter,
- * which is often around 25. (Less than, because we allocate one extra sema
- * in each set for identification purposes.)
- *
- * PROC_SEM_MAP_ENTRIES is the number of semaphore sets we need to allocate
- * to keep track of up to maxBackends backends.
+ * Prior to PostgreSQL 9.2, the fields 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.
*/
-#define PROC_NSEMS_PER_SET 16
-#define PROC_SEM_MAP_ENTRIES(maxBackends) (((maxBackends)-1)/PROC_NSEMS_PER_SET+1)
-
-typedef struct
+typedef struct PGXACT
{
- /* info about a single set of per-process semaphores */
- IpcSemaphoreId procSemId;
- int32 freeSemMap;
- /*
- * In freeSemMap, bit i is set if the i'th semaphore of this sema
- * set is allocated to a process. (i counts from 0 at the LSB)
- */
-} SEM_MAP_ENTRY;
+ 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 */
+
+ uint8 nxids;
+} PGXACT;
+/*
+ * There is one ProcGlobal struct for the whole database cluster.
+ */
typedef struct PROC_HDR
{
- /* Head of list of free PROC structures */
- SHMEM_OFFSET freeProcs;
-
- /* Info about semaphore sets used for per-process semaphores */
- int semMapEntries;
- /*
- * VARIABLE LENGTH ARRAY: actual length is semMapEntries.
- * THIS MUST BE LAST IN THE STRUCT DECLARATION.
- */
- SEM_MAP_ENTRY procSemMap[1];
+ /* Array of PGPROC structures (not including dummies for prepared txns) */
+ PGPROC *allProcs;
+ /* Array of PGXACT structures (not including dummies for prepared txns) */
+ PGXACT *allPgXact;
+ /* Length of allProcs array */
+ uint32 allProcCount;
+ /* Head of list of free PGPROC structures */
+ PGPROC *freeProcs;
+ /* Head of list of autovacuum's free PGPROC structures */
+ PGPROC *autovacFreeProcs;
+ /* WALWriter process's latch */
+ Latch *walwriterLatch;
+ /* Checkpointer process's latch */
+ Latch *checkpointerLatch;
+ /* Current shared estimate of appropriate spins_per_delay value */
+ int spins_per_delay;
+ /* The proc of the Startup process, since not in ProcArray */
+ PGPROC *startupProc;
+ int startupProcPid;
+ /* Buffer id of the buffer that Startup process waits for pin on, or -1 */
+ int startupBufferPinWaitBufId;
} PROC_HDR;
+extern PROC_HDR *ProcGlobal;
-/* configurable option */
+extern PGPROC *PreparedXactProcs;
+
+/*
+ * We set aside some extra PGPROC structures for auxiliary processes,
+ * ie things that aren't full-fledged backends but need shmem access.
+ *
+ * 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.
+ */
+#define NUM_AUXILIARY_PROCS 4
+
+
+/* configurable options */
extern int DeadlockTimeout;
+extern int StatementTimeout;
+extern bool log_lock_waits;
+
+extern volatile bool cancel_from_timeout;
/*
* Function Prototypes
*/
-extern void InitProcGlobal(int maxBackends);
+extern int ProcGlobalSemas(void);
+extern Size ProcGlobalShmemSize(void);
+extern void InitProcGlobal(void);
extern void InitProcess(void);
-extern void InitDummyProcess(void);
+extern void InitProcessPhase2(void);
+extern void InitAuxiliaryProcess(void);
+
+extern void PublishStartupProcessInformation(void);
+extern void SetStartupBufferPinWaitBufId(int bufid);
+extern int GetStartupBufferPinWaitBufId(void);
+
+extern bool HaveNFreeProcs(int n);
extern void ProcReleaseLocks(bool isCommit);
extern void ProcQueueInit(PROC_QUEUE *queue);
-extern int ProcSleep(LOCKMETHODTABLE *lockMethodTable, LOCKMODE lockmode,
- LOCK *lock, PROCLOCK *holder);
-extern PROC *ProcWakeup(PROC *proc, int errType);
-extern void ProcLockWakeup(LOCKMETHODTABLE *lockMethodTable, LOCK *lock);
-extern bool LockWaitCancel(void);
-extern void HandleDeadLock(SIGNAL_ARGS);
+extern int ProcSleep(LOCALLOCK *locallock, LockMethod lockMethodTable);
+extern PGPROC *ProcWakeup(PGPROC *proc, int waitStatus);
+extern void ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock);
+extern bool IsWaitingForLock(void);
+extern void LockErrorCleanup(void);
extern void ProcWaitForSignal(void);
-extern void ProcCancelWaitForSignal(void);
-extern void ProcSendSignal(BackendId procId);
+extern void ProcSendSignal(int pid);
+
+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);
-extern bool enable_sigalrm_interrupt(int delayms);
-extern bool disable_sigalrm_interrupt(void);
+extern bool enable_standby_sig_alarm(TimestampTz now,
+ TimestampTz fin_time, bool deadlock_only);
+extern bool disable_standby_sig_alarm(void);
+extern void handle_standby_sig_alarm(SIGNAL_ARGS);
-#endif /* PROC_H */
+#endif /* PROC_H */