1996-08-28 03:59:28 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* lock.h
|
2000-12-22 01:51:54 +01:00
|
|
|
* POSTGRES low-level lock mechanism
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*
|
2002-06-20 22:29:54 +02:00
|
|
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2003-02-19 05:02:54 +01:00
|
|
|
* $Id: lock.h,v 1.70 2003/02/19 04:02:54 momjian Exp $
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef LOCK_H_
|
|
|
|
#define LOCK_H_
|
|
|
|
|
1999-07-16 01:04:24 +02:00
|
|
|
#include "storage/itemptr.h"
|
2001-09-29 06:02:27 +02:00
|
|
|
#include "storage/lwlock.h"
|
1999-07-16 19:07:40 +02:00
|
|
|
#include "storage/shmem.h"
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
/* originally in procq.h */
|
|
|
|
typedef struct PROC_QUEUE
|
|
|
|
{
|
2002-06-11 15:40:53 +02:00
|
|
|
SHM_QUEUE links; /* head of list of PGPROC objects */
|
2001-01-22 23:30:06 +01:00
|
|
|
int size; /* number of entries in list */
|
2000-12-22 01:51:54 +01:00
|
|
|
} PROC_QUEUE;
|
|
|
|
|
2002-06-11 15:40:53 +02:00
|
|
|
/* struct PGPROC is declared in storage/proc.h, but must forward-reference it */
|
|
|
|
typedef struct PGPROC PGPROC;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
|
2001-06-28 01:31:40 +02:00
|
|
|
extern int max_locks_per_xact;
|
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
#ifdef LOCK_DEBUG
|
2001-03-22 05:01:46 +01:00
|
|
|
extern int Trace_lock_oidmin;
|
2000-05-31 02:28:42 +02:00
|
|
|
extern bool Trace_locks;
|
|
|
|
extern bool Trace_userlocks;
|
2001-03-22 05:01:46 +01:00
|
|
|
extern int Trace_lock_table;
|
2000-05-31 02:28:42 +02:00
|
|
|
extern bool Debug_deadlocks;
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* LOCK_DEBUG */
|
2000-05-31 02:28:42 +02:00
|
|
|
|
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
typedef int LOCKMASK;
|
|
|
|
|
1998-06-30 04:33:34 +02:00
|
|
|
typedef int LOCKMODE;
|
|
|
|
typedef int LOCKMETHOD;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2001-01-22 23:30:06 +01:00
|
|
|
/* MAX_LOCKMODES cannot be larger than the # of bits in LOCKMASK */
|
2001-07-10 00:18:34 +02:00
|
|
|
#define MAX_LOCKMODES 10
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/* MAX_LOCK_METHODS is the number of distinct lock control tables allowed */
|
1998-09-01 06:40:42 +02:00
|
|
|
#define MAX_LOCK_METHODS 3
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1998-08-25 23:20:32 +02:00
|
|
|
#define INVALID_TABLEID 0
|
|
|
|
|
|
|
|
#define INVALID_LOCKMETHOD INVALID_TABLEID
|
|
|
|
#define DEFAULT_LOCKMETHOD 1
|
|
|
|
#define USER_LOCKMETHOD 2
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2001-06-22 02:04:59 +02:00
|
|
|
/*
|
|
|
|
* There is normally only one lock method, the default one.
|
|
|
|
* If user locks are enabled, an additional lock method is present.
|
2001-02-23 20:24:06 +01:00
|
|
|
*
|
2000-12-22 01:51:54 +01:00
|
|
|
* This is the control structure for a lock table. It
|
2001-03-22 05:01:46 +01:00
|
|
|
* lives in shared memory. This information is the same
|
2001-02-23 19:28:46 +01:00
|
|
|
* for all backends.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2002-07-19 01:06:20 +02:00
|
|
|
* lockHash -- hash table holding per-locked-object lock information
|
|
|
|
*
|
2003-02-18 03:13:24 +01:00
|
|
|
* proclockHash -- hash table holding per-lock-waiter/holder lock information
|
2002-07-19 01:06:20 +02:00
|
|
|
*
|
1998-06-30 04:33:34 +02:00
|
|
|
* lockmethod -- the handle used by the lock table's clients to
|
|
|
|
* refer to the type of lock table being used.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
1998-06-30 04:33:34 +02:00
|
|
|
* numLockModes -- number of lock types (READ,WRITE,etc) that
|
1997-09-07 07:04:48 +02:00
|
|
|
* are defined on this lock table
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* conflictTab -- this is an array of bitmasks showing lock
|
1997-09-07 07:04:48 +02:00
|
|
|
* type conflicts. conflictTab[i] is a mask with the j-th bit
|
|
|
|
* turned on if lock types i and j conflict.
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
2001-09-29 06:02:27 +02:00
|
|
|
* masterLock -- synchronizes access to the table
|
2002-07-19 01:06:20 +02:00
|
|
|
*
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
2002-07-19 01:06:20 +02:00
|
|
|
typedef struct LOCKMETHODTABLE
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2002-07-19 01:06:20 +02:00
|
|
|
HTAB *lockHash;
|
2003-02-18 03:13:24 +01:00
|
|
|
HTAB *proclockHash;
|
1998-09-01 06:40:42 +02:00
|
|
|
LOCKMETHOD lockmethod;
|
1998-06-30 04:33:34 +02:00
|
|
|
int numLockModes;
|
|
|
|
int conflictTab[MAX_LOCKMODES];
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockId masterLock;
|
1999-05-26 00:43:53 +02:00
|
|
|
} LOCKMETHODTABLE;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
1998-08-25 23:20:32 +02:00
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
/*
|
|
|
|
* LOCKTAG is the key information needed to look up a LOCK item in the
|
2001-03-22 05:01:46 +01:00
|
|
|
* lock hashtable. A LOCKTAG value uniquely identifies a lockable object.
|
2000-12-22 01:51:54 +01:00
|
|
|
*/
|
|
|
|
typedef struct LOCKTAG
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2003-02-19 05:02:54 +01:00
|
|
|
Oid objId;
|
|
|
|
Oid classId;
|
2000-12-22 01:51:54 +01:00
|
|
|
Oid dbId;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
BlockNumber blkno;
|
|
|
|
TransactionId xid;
|
2003-02-19 05:02:54 +01:00
|
|
|
} objsubId;
|
1999-03-06 22:17:56 +01:00
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
/*
|
|
|
|
* offnum should be part of objId.tupleId above, but would increase
|
|
|
|
* sizeof(LOCKTAG) and so moved here; currently used by userlocks
|
|
|
|
* only.
|
|
|
|
*/
|
|
|
|
OffsetNumber offnum;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
uint16 lockmethod; /* needed by userlocks */
|
|
|
|
} LOCKTAG;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2000-12-22 01:51:54 +01:00
|
|
|
* Per-locked-object lock information:
|
1996-08-28 03:59:28 +02:00
|
|
|
*
|
|
|
|
* tag -- uniquely identifies the object being locked
|
2001-01-16 07:11:34 +01:00
|
|
|
* grantMask -- bitmask for all lock types currently granted on this object.
|
|
|
|
* waitMask -- bitmask for all lock types currently awaited on this object.
|
2002-07-19 02:17:40 +02:00
|
|
|
* lockHolders -- list of PROCLOCK objects for this lock.
|
2001-01-16 07:11:34 +01:00
|
|
|
* waitProcs -- queue of processes waiting for this lock.
|
|
|
|
* requested -- count of each lock type currently requested on the lock
|
|
|
|
* (includes requests already granted!!).
|
|
|
|
* nRequested -- total requested locks of all types.
|
|
|
|
* granted -- count of each lock type currently granted on the lock.
|
|
|
|
* nGranted -- total granted locks of all types.
|
1996-08-28 03:59:28 +02:00
|
|
|
*/
|
1998-06-30 04:33:34 +02:00
|
|
|
typedef struct LOCK
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
/* hash key */
|
2001-01-16 07:11:34 +01:00
|
|
|
LOCKTAG tag; /* unique identifier of lockable object */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* data */
|
2001-01-16 07:11:34 +01:00
|
|
|
int grantMask; /* bitmask for lock types already granted */
|
|
|
|
int waitMask; /* bitmask for lock types awaited */
|
2002-09-04 22:31:48 +02:00
|
|
|
SHM_QUEUE lockHolders; /* list of PROCLOCK objects assoc. with
|
|
|
|
* lock */
|
2002-06-11 15:40:53 +02:00
|
|
|
PROC_QUEUE waitProcs; /* list of PGPROC objects waiting on lock */
|
2001-03-22 05:01:46 +01:00
|
|
|
int requested[MAX_LOCKMODES]; /* counts of requested
|
|
|
|
* locks */
|
2001-01-16 07:11:34 +01:00
|
|
|
int nRequested; /* total of requested[] array */
|
2001-03-22 05:01:46 +01:00
|
|
|
int granted[MAX_LOCKMODES]; /* counts of granted locks */
|
2001-01-16 07:11:34 +01:00
|
|
|
int nGranted; /* total of granted[] array */
|
1997-09-08 23:56:23 +02:00
|
|
|
} LOCK;
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
#define LOCK_LOCKMETHOD(lock) ((lock).tag.lockmethod)
|
1996-08-28 03:59:28 +02:00
|
|
|
|
2000-12-22 01:51:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We may have several different transactions holding or awaiting locks
|
2003-02-18 03:13:24 +01:00
|
|
|
* on the same lockable object. We need to store some per-waiter/holder
|
|
|
|
* information for each such holder (or would-be holder).
|
2000-12-22 01:51:54 +01:00
|
|
|
*
|
2002-07-19 02:17:40 +02:00
|
|
|
* PROCLOCKTAG is the key information needed to look up a PROCLOCK item in the
|
2003-02-18 03:13:24 +01:00
|
|
|
* proclock hashtable. A PROCLOCKTAG value uniquely identifies a lock
|
|
|
|
* holder/waiter.
|
2000-12-22 01:51:54 +01:00
|
|
|
*
|
2003-02-18 03:13:24 +01:00
|
|
|
* There are two possible kinds of proclock tags: a transaction (identified
|
2002-06-11 15:40:53 +02:00
|
|
|
* both by the PGPROC of the backend running it, and the xact's own ID) and
|
|
|
|
* a session (identified by backend PGPROC, with xid = InvalidTransactionId).
|
2000-12-22 01:51:54 +01:00
|
|
|
*
|
2003-02-18 03:13:24 +01:00
|
|
|
* Currently, session proclocks are used for user locks and for cross-xact
|
2000-12-22 01:51:54 +01:00
|
|
|
* locks obtained for VACUUM. We assume that a session lock never conflicts
|
|
|
|
* with per-transaction locks obtained by the same backend.
|
2001-01-16 07:11:34 +01:00
|
|
|
*
|
|
|
|
* The holding[] array counts the granted locks (of each type) represented
|
2003-02-18 03:13:24 +01:00
|
|
|
* by this proclock. Note that there will be a proclock object, possibly with
|
2001-01-16 07:11:34 +01:00
|
|
|
* zero holding[], for any lock that the process is currently waiting on.
|
2003-02-18 03:13:24 +01:00
|
|
|
* Otherwise, proclock objects whose counts have gone to zero are recycled
|
2001-01-16 07:11:34 +01:00
|
|
|
* as soon as convenient.
|
2001-01-22 23:30:06 +01:00
|
|
|
*
|
2002-07-19 02:17:40 +02:00
|
|
|
* Each PROCLOCK object is linked into lists for both the associated LOCK object
|
|
|
|
* and the owning PGPROC object. Note that the PROCLOCK is entered into these
|
2001-01-22 23:30:06 +01:00
|
|
|
* lists as soon as it is created, even if no lock has yet been granted.
|
2002-06-11 15:40:53 +02:00
|
|
|
* A PGPROC that is waiting for a lock to be granted will also be linked into
|
2001-01-22 23:30:06 +01:00
|
|
|
* the lock's waitProcs queue.
|
2000-12-22 01:51:54 +01:00
|
|
|
*/
|
2002-07-19 02:17:40 +02:00
|
|
|
typedef struct PROCLOCKTAG
|
2000-12-22 01:51:54 +01:00
|
|
|
{
|
|
|
|
SHMEM_OFFSET lock; /* link to per-lockable-object information */
|
2002-06-11 15:40:53 +02:00
|
|
|
SHMEM_OFFSET proc; /* link to PGPROC of owning backend */
|
2000-12-22 01:51:54 +01:00
|
|
|
TransactionId xid; /* xact ID, or InvalidTransactionId */
|
2002-07-19 02:17:40 +02:00
|
|
|
} PROCLOCKTAG;
|
2000-12-22 01:51:54 +01:00
|
|
|
|
2002-07-19 02:17:40 +02:00
|
|
|
typedef struct PROCLOCK
|
2000-12-22 01:51:54 +01:00
|
|
|
{
|
|
|
|
/* tag */
|
2003-02-18 03:13:24 +01:00
|
|
|
PROCLOCKTAG tag; /* unique identifier of proclock object */
|
2000-12-22 01:51:54 +01:00
|
|
|
|
|
|
|
/* data */
|
2001-03-22 05:01:46 +01:00
|
|
|
int holding[MAX_LOCKMODES]; /* count of locks currently held */
|
2001-01-16 07:11:34 +01:00
|
|
|
int nHolding; /* total of holding[] array */
|
2003-02-18 03:13:24 +01:00
|
|
|
SHM_QUEUE lockLink; /* list link for lock's list of proclocks */
|
|
|
|
SHM_QUEUE procLink; /* list link for process's list of proclocks */
|
2002-07-19 02:17:40 +02:00
|
|
|
} PROCLOCK;
|
2000-12-22 01:51:54 +01:00
|
|
|
|
2003-02-18 03:13:24 +01:00
|
|
|
#define PROCLOCK_LOCKMETHOD(proclock) \
|
|
|
|
(((LOCK *) MAKE_PTR((proclock).tag.lock))->tag.lockmethod)
|
2000-12-22 01:51:54 +01:00
|
|
|
|
2002-08-17 15:04:19 +02:00
|
|
|
/*
|
2002-08-31 19:14:28 +02:00
|
|
|
* This struct holds information passed from lmgr internals to the lock
|
2002-09-04 22:31:48 +02:00
|
|
|
* listing user-level functions (lockfuncs.c). For each PROCLOCK in the
|
2002-08-31 19:14:28 +02:00
|
|
|
* system, the SHMEM_OFFSET, PROCLOCK itself, and associated PGPROC and
|
|
|
|
* LOCK objects are stored. (Note there will often be multiple copies
|
|
|
|
* of the same PGPROC or LOCK.) We do not store the SHMEM_OFFSET of the
|
|
|
|
* PGPROC or LOCK separately, since they're in the PROCLOCK's tag fields.
|
2002-08-17 15:04:19 +02:00
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
int nelements; /* The length of each of the arrays */
|
2003-02-18 03:13:24 +01:00
|
|
|
SHMEM_OFFSET *proclockaddrs;
|
|
|
|
PROCLOCK *proclocks;
|
2002-09-04 22:31:48 +02:00
|
|
|
PGPROC *procs;
|
|
|
|
LOCK *locks;
|
2002-08-17 15:04:19 +02:00
|
|
|
} LockData;
|
2000-12-22 01:51:54 +01:00
|
|
|
|
1996-08-28 03:59:28 +02:00
|
|
|
/*
|
|
|
|
* function prototypes
|
|
|
|
*/
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void InitLocks(void);
|
2001-01-25 04:31:16 +01:00
|
|
|
extern LOCKMETHODTABLE *GetLocksMethodTable(LOCK *lock);
|
1999-07-15 22:32:30 +02:00
|
|
|
extern LOCKMETHOD LockMethodTableInit(char *tabName, LOCKMASK *conflictsP,
|
2002-08-01 07:18:34 +02:00
|
|
|
int numModes, int maxBackends);
|
1998-08-25 23:20:32 +02:00
|
|
|
extern LOCKMETHOD LockMethodTableRename(LOCKMETHOD lockmethod);
|
|
|
|
extern bool LockAcquire(LOCKMETHOD lockmethod, LOCKTAG *locktag,
|
2001-06-22 02:04:59 +02:00
|
|
|
TransactionId xid, LOCKMODE lockmode, bool dontWait);
|
1998-08-25 23:20:32 +02:00
|
|
|
extern bool LockRelease(LOCKMETHOD lockmethod, LOCKTAG *locktag,
|
2001-03-22 05:01:46 +01:00
|
|
|
TransactionId xid, LOCKMODE lockmode);
|
2002-06-11 15:40:53 +02:00
|
|
|
extern bool LockReleaseAll(LOCKMETHOD lockmethod, PGPROC *proc,
|
2001-03-22 05:01:46 +01:00
|
|
|
bool allxids, TransactionId xid);
|
2001-01-25 04:31:16 +01:00
|
|
|
extern int LockCheckConflicts(LOCKMETHODTABLE *lockMethodTable,
|
2001-03-22 05:01:46 +01:00
|
|
|
LOCKMODE lockmode,
|
2003-02-18 03:13:24 +01:00
|
|
|
LOCK *lock, PROCLOCK *proclock, PGPROC *proc,
|
2001-03-22 05:01:46 +01:00
|
|
|
int *myHolding);
|
2003-02-18 03:13:24 +01:00
|
|
|
extern void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode);
|
2002-06-11 15:40:53 +02:00
|
|
|
extern void RemoveFromWaitQueue(PGPROC *proc);
|
1999-02-21 02:41:55 +01:00
|
|
|
extern int LockShmemSize(int maxBackends);
|
2002-06-11 15:40:53 +02:00
|
|
|
extern bool DeadLockCheck(PGPROC *proc);
|
2003-01-16 22:01:45 +01:00
|
|
|
extern void DeadLockReport(void);
|
|
|
|
extern void RememberSimpleDeadLock(PGPROC *proc1,
|
|
|
|
LOCKMODE lockmode,
|
|
|
|
LOCK *lock,
|
|
|
|
PGPROC *proc2);
|
2001-01-25 04:31:16 +01:00
|
|
|
extern void InitDeadLockChecking(void);
|
2002-08-31 19:14:28 +02:00
|
|
|
extern LockData *GetLockStatusData(void);
|
|
|
|
extern const char *GetLockmodeName(LOCKMODE mode);
|
1998-02-26 05:46:47 +01:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
#ifdef LOCK_DEBUG
|
1997-09-08 04:41:22 +02:00
|
|
|
extern void DumpLocks(void);
|
1998-08-25 23:20:32 +02:00
|
|
|
extern void DumpAllLocks(void);
|
1997-02-12 06:25:13 +01:00
|
|
|
#endif
|
2001-10-28 07:26:15 +01:00
|
|
|
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* LOCK_H */
|