1 /*-------------------------------------------------------------------------
4 * POSTGRES low-level lock mechanism
7 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
10 * $PostgreSQL: pgsql/src/include/storage/lock.h,v 1.101 2006/10/04 00:30:10 momjian Exp $
12 *-------------------------------------------------------------------------
17 #include "nodes/pg_list.h"
18 #include "storage/itemptr.h"
19 #include "storage/lwlock.h"
20 #include "storage/shmem.h"
23 /* struct PGPROC is declared in proc.h, but must forward-reference it */
24 typedef struct PGPROC PGPROC;
26 typedef struct PROC_QUEUE
28 SHM_QUEUE links; /* head of list of PGPROC objects */
29 int size; /* number of entries in list */
33 extern int max_locks_per_xact;
36 extern int Trace_lock_oidmin;
37 extern bool Trace_locks;
38 extern bool Trace_userlocks;
39 extern int Trace_lock_table;
40 extern bool Debug_deadlocks;
41 #endif /* LOCK_DEBUG */
45 * LOCKMODE is an integer (1..N) indicating a lock type. LOCKMASK is a bit
46 * mask indicating a set of held or requested lock types (the bit 1<<mode
47 * corresponds to a particular lock mode).
52 /* MAX_LOCKMODES cannot be larger than the # of bits in LOCKMASK */
53 #define MAX_LOCKMODES 10
55 #define LOCKBIT_ON(lockmode) (1 << (lockmode))
56 #define LOCKBIT_OFF(lockmode) (~(1 << (lockmode)))
60 * This data structure defines the locking semantics associated with a
61 * "lock method". The semantics specify the meaning of each lock mode
62 * (by defining which lock modes it conflicts with), and also whether locks
63 * of this method are transactional (ie, are released at transaction end).
64 * All of this data is constant and is kept in const tables.
66 * numLockModes -- number of lock modes (READ,WRITE,etc) that
67 * are defined in this lock method. Must be less than MAX_LOCKMODES.
69 * transactional -- TRUE if locks are released automatically at xact end.
71 * conflictTab -- this is an array of bitmasks showing lock
72 * mode conflicts. conflictTab[i] is a mask with the j-th bit
73 * turned on if lock modes i and j conflict. Lock modes are
74 * numbered 1..numLockModes; conflictTab[0] is unused.
76 * lockModeNames -- ID strings for debug printouts.
78 * trace_flag -- pointer to GUC trace flag for this lock method.
80 typedef struct LockMethodData
84 const LOCKMASK *conflictTab;
85 const char *const * lockModeNames;
86 const bool *trace_flag;
89 typedef const LockMethodData *LockMethod;
92 * Lock methods are identified by LOCKMETHODID. (Despite the declaration as
93 * uint16, we are constrained to 256 lockmethods by the layout of LOCKTAG.)
95 typedef uint16 LOCKMETHODID;
97 /* These identify the known lock methods */
98 #define DEFAULT_LOCKMETHOD 1
99 #define USER_LOCKMETHOD 2
102 * These are the valid values of type LOCKMODE for all the standard lock
103 * methods (both DEFAULT and USER).
106 /* NoLock is not a lock mode, but a flag value meaning "don't get a lock" */
109 #define AccessShareLock 1 /* SELECT */
110 #define RowShareLock 2 /* SELECT FOR UPDATE/FOR SHARE */
111 #define RowExclusiveLock 3 /* INSERT, UPDATE, DELETE */
112 #define ShareUpdateExclusiveLock 4 /* VACUUM (non-FULL) */
113 #define ShareLock 5 /* CREATE INDEX */
114 #define ShareRowExclusiveLock 6 /* like EXCLUSIVE MODE, but allows ROW
116 #define ExclusiveLock 7 /* blocks ROW SHARE/SELECT...FOR
118 #define AccessExclusiveLock 8 /* ALTER TABLE, DROP TABLE, VACUUM
119 * FULL, and unqualified LOCK TABLE */
123 * LOCKTAG is the key information needed to look up a LOCK item in the
124 * lock hashtable. A LOCKTAG value uniquely identifies a lockable object.
126 * The LockTagType enum defines the different kinds of objects we can lock.
127 * We can handle up to 256 different LockTagTypes.
129 typedef enum LockTagType
131 LOCKTAG_RELATION, /* whole relation */
132 /* ID info for a relation is DB OID + REL OID; DB OID = 0 if shared */
133 LOCKTAG_RELATION_EXTEND, /* the right to extend a relation */
134 /* same ID info as RELATION */
135 LOCKTAG_PAGE, /* one page of a relation */
136 /* ID info for a page is RELATION info + BlockNumber */
137 LOCKTAG_TUPLE, /* one physical tuple */
138 /* ID info for a tuple is PAGE info + OffsetNumber */
139 LOCKTAG_TRANSACTION, /* transaction (for waiting for xact done) */
140 /* ID info for a transaction is its TransactionId */
141 LOCKTAG_OBJECT, /* non-relation database object */
142 /* ID info for an object is DB OID + CLASS OID + OBJECT OID + SUBID */
145 * Note: object ID has same representation as in pg_depend and
146 * pg_description, but notice that we are constraining SUBID to 16 bits.
147 * Also, we use DB OID = 0 for shared objects such as tablespaces.
149 LOCKTAG_USERLOCK, /* reserved for old contrib/userlock code */
150 LOCKTAG_ADVISORY /* advisory user locks */
154 * The LOCKTAG struct is defined with malice aforethought to fit into 16
155 * bytes with no padding. Note that this would need adjustment if we were
156 * to widen Oid, BlockNumber, or TransactionId to more than 32 bits.
158 * We include lockmethodid in the locktag so that a single hash table in
159 * shared memory can store locks of different lockmethods.
161 typedef struct LOCKTAG
163 uint32 locktag_field1; /* a 32-bit ID field */
164 uint32 locktag_field2; /* a 32-bit ID field */
165 uint32 locktag_field3; /* a 32-bit ID field */
166 uint16 locktag_field4; /* a 16-bit ID field */
167 uint8 locktag_type; /* see enum LockTagType */
168 uint8 locktag_lockmethodid; /* lockmethod indicator */
172 * These macros define how we map logical IDs of lockable objects into
173 * the physical fields of LOCKTAG. Use these to set up LOCKTAG values,
174 * rather than accessing the fields directly. Note multiple eval of target!
176 #define SET_LOCKTAG_RELATION(locktag,dboid,reloid) \
177 ((locktag).locktag_field1 = (dboid), \
178 (locktag).locktag_field2 = (reloid), \
179 (locktag).locktag_field3 = 0, \
180 (locktag).locktag_field4 = 0, \
181 (locktag).locktag_type = LOCKTAG_RELATION, \
182 (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
184 #define SET_LOCKTAG_RELATION_EXTEND(locktag,dboid,reloid) \
185 ((locktag).locktag_field1 = (dboid), \
186 (locktag).locktag_field2 = (reloid), \
187 (locktag).locktag_field3 = 0, \
188 (locktag).locktag_field4 = 0, \
189 (locktag).locktag_type = LOCKTAG_RELATION_EXTEND, \
190 (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
192 #define SET_LOCKTAG_PAGE(locktag,dboid,reloid,blocknum) \
193 ((locktag).locktag_field1 = (dboid), \
194 (locktag).locktag_field2 = (reloid), \
195 (locktag).locktag_field3 = (blocknum), \
196 (locktag).locktag_field4 = 0, \
197 (locktag).locktag_type = LOCKTAG_PAGE, \
198 (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
200 #define SET_LOCKTAG_TUPLE(locktag,dboid,reloid,blocknum,offnum) \
201 ((locktag).locktag_field1 = (dboid), \
202 (locktag).locktag_field2 = (reloid), \
203 (locktag).locktag_field3 = (blocknum), \
204 (locktag).locktag_field4 = (offnum), \
205 (locktag).locktag_type = LOCKTAG_TUPLE, \
206 (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
208 #define SET_LOCKTAG_TRANSACTION(locktag,xid) \
209 ((locktag).locktag_field1 = (xid), \
210 (locktag).locktag_field2 = 0, \
211 (locktag).locktag_field3 = 0, \
212 (locktag).locktag_field4 = 0, \
213 (locktag).locktag_type = LOCKTAG_TRANSACTION, \
214 (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
216 #define SET_LOCKTAG_OBJECT(locktag,dboid,classoid,objoid,objsubid) \
217 ((locktag).locktag_field1 = (dboid), \
218 (locktag).locktag_field2 = (classoid), \
219 (locktag).locktag_field3 = (objoid), \
220 (locktag).locktag_field4 = (objsubid), \
221 (locktag).locktag_type = LOCKTAG_OBJECT, \
222 (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
224 #define SET_LOCKTAG_ADVISORY(locktag,id1,id2,id3,id4) \
225 ((locktag).locktag_field1 = (id1), \
226 (locktag).locktag_field2 = (id2), \
227 (locktag).locktag_field3 = (id3), \
228 (locktag).locktag_field4 = (id4), \
229 (locktag).locktag_type = LOCKTAG_ADVISORY, \
230 (locktag).locktag_lockmethodid = USER_LOCKMETHOD)
234 * Per-locked-object lock information:
236 * tag -- uniquely identifies the object being locked
237 * grantMask -- bitmask for all lock types currently granted on this object.
238 * waitMask -- bitmask for all lock types currently awaited on this object.
239 * procLocks -- list of PROCLOCK objects for this lock.
240 * waitProcs -- queue of processes waiting for this lock.
241 * requested -- count of each lock type currently requested on the lock
242 * (includes requests already granted!!).
243 * nRequested -- total requested locks of all types.
244 * granted -- count of each lock type currently granted on the lock.
245 * nGranted -- total granted locks of all types.
247 * Note: these counts count 1 for each backend. Internally to a backend,
248 * there may be multiple grabs on a particular lock, but this is not reflected
249 * into shared memory.
254 LOCKTAG tag; /* unique identifier of lockable object */
257 LOCKMASK grantMask; /* bitmask for lock types already granted */
258 LOCKMASK waitMask; /* bitmask for lock types awaited */
259 SHM_QUEUE procLocks; /* list of PROCLOCK objects assoc. with lock */
260 PROC_QUEUE waitProcs; /* list of PGPROC objects waiting on lock */
261 int requested[MAX_LOCKMODES]; /* counts of requested locks */
262 int nRequested; /* total of requested[] array */
263 int granted[MAX_LOCKMODES]; /* counts of granted locks */
264 int nGranted; /* total of granted[] array */
267 #define LOCK_LOCKMETHOD(lock) ((LOCKMETHODID) (lock).tag.locktag_lockmethodid)
271 * We may have several different backends holding or awaiting locks
272 * on the same lockable object. We need to store some per-holder/waiter
273 * information for each such holder (or would-be holder). This is kept in
276 * PROCLOCKTAG is the key information needed to look up a PROCLOCK item in the
277 * proclock hashtable. A PROCLOCKTAG value uniquely identifies the combination
278 * of a lockable object and a holder/waiter for that object. (We can use
279 * pointers here because the PROCLOCKTAG need only be unique for the lifespan
280 * of the PROCLOCK, and it will never outlive the lock or the proc.)
282 * Internally to a backend, it is possible for the same lock to be held
283 * for different purposes: the backend tracks transaction locks separately
284 * from session locks. However, this is not reflected in the shared-memory
285 * state: we only track which backend(s) hold the lock. This is OK since a
286 * backend can never block itself.
288 * The holdMask field shows the already-granted locks represented by this
289 * proclock. Note that there will be a proclock object, possibly with
290 * zero holdMask, for any lock that the process is currently waiting on.
291 * Otherwise, proclock objects whose holdMasks are zero are recycled
292 * as soon as convenient.
294 * releaseMask is workspace for LockReleaseAll(): it shows the locks due
295 * to be released during the current call. This must only be examined or
296 * set by the backend owning the PROCLOCK.
298 * Each PROCLOCK object is linked into lists for both the associated LOCK
299 * object and the owning PGPROC object. Note that the PROCLOCK is entered
300 * into these lists as soon as it is created, even if no lock has yet been
301 * granted. A PGPROC that is waiting for a lock to be granted will also be
302 * linked into the lock's waitProcs queue.
304 typedef struct PROCLOCKTAG
306 /* NB: we assume this struct contains no padding! */
307 LOCK *myLock; /* link to per-lockable-object information */
308 PGPROC *myProc; /* link to PGPROC of owning backend */
311 typedef struct PROCLOCK
314 PROCLOCKTAG tag; /* unique identifier of proclock object */
317 LOCKMASK holdMask; /* bitmask for lock types currently held */
318 LOCKMASK releaseMask; /* bitmask for lock types to be released */
319 SHM_QUEUE lockLink; /* list link in LOCK's list of proclocks */
320 SHM_QUEUE procLink; /* list link in PGPROC's list of proclocks */
323 #define PROCLOCK_LOCKMETHOD(proclock) \
324 LOCK_LOCKMETHOD(*((proclock).tag.myLock))
327 * Each backend also maintains a local hash table with information about each
328 * lock it is currently interested in. In particular the local table counts
329 * the number of times that lock has been acquired. This allows multiple
330 * requests for the same lock to be executed without additional accesses to
331 * shared memory. We also track the number of lock acquisitions per
332 * ResourceOwner, so that we can release just those locks belonging to a
333 * particular ResourceOwner.
335 typedef struct LOCALLOCKTAG
337 LOCKTAG lock; /* identifies the lockable object */
338 LOCKMODE mode; /* lock mode for this table entry */
341 typedef struct LOCALLOCKOWNER
344 * Note: if owner is NULL then the lock is held on behalf of the session;
345 * otherwise it is held on behalf of my current transaction.
347 * Must use a forward struct reference to avoid circularity.
349 struct ResourceOwnerData *owner;
350 int nLocks; /* # of times held by this owner */
353 typedef struct LOCALLOCK
356 LOCALLOCKTAG tag; /* unique identifier of locallock entry */
359 LOCK *lock; /* associated LOCK object in shared mem */
360 PROCLOCK *proclock; /* associated PROCLOCK object in shmem */
361 uint32 hashcode; /* copy of LOCKTAG's hash value */
362 int nLocks; /* total number of times lock is held */
363 int numLockOwners; /* # of relevant ResourceOwners */
364 int maxLockOwners; /* allocated size of array */
365 LOCALLOCKOWNER *lockOwners; /* dynamically resizable array */
368 #define LOCALLOCK_LOCKMETHOD(llock) ((llock).tag.lock.locktag_lockmethodid)
372 * This struct holds information passed from lmgr internals to the lock
373 * listing user-level functions (in lockfuncs.c). For each PROCLOCK in
374 * the system, copies of the PROCLOCK object and associated PGPROC and
375 * LOCK objects are stored. Note there will often be multiple copies
376 * of the same PGPROC or LOCK --- to detect whether two are the same,
377 * compare the PROCLOCK tag fields.
379 typedef struct LockData
381 int nelements; /* The length of each of the arrays */
388 /* Result codes for LockAcquire() */
391 LOCKACQUIRE_NOT_AVAIL, /* lock not available, and dontWait=true */
392 LOCKACQUIRE_OK, /* lock successfully acquired */
393 LOCKACQUIRE_ALREADY_HELD /* incremented count for lock already held */
398 * The lockmgr's shared hash tables are partitioned to reduce contention.
399 * To determine which partition a given locktag belongs to, compute the tag's
400 * hash code with LockTagHashCode(), then apply one of these macros.
401 * NB: NUM_LOCK_PARTITIONS must be a power of 2!
403 #define LockHashPartition(hashcode) \
404 ((hashcode) % NUM_LOCK_PARTITIONS)
405 #define LockHashPartitionLock(hashcode) \
406 ((LWLockId) (FirstLockMgrLock + LockHashPartition(hashcode)))
410 * function prototypes
412 extern void InitLocks(void);
413 extern LockMethod GetLocksMethodTable(const LOCK *lock);
414 extern uint32 LockTagHashCode(const LOCKTAG *locktag);
415 extern LockAcquireResult LockAcquire(const LOCKTAG *locktag,
419 extern bool LockRelease(const LOCKTAG *locktag,
420 LOCKMODE lockmode, bool sessionLock);
421 extern void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks);
422 extern void LockReleaseCurrentOwner(void);
423 extern void LockReassignCurrentOwner(void);
424 extern List *GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode);
425 extern void AtPrepare_Locks(void);
426 extern void PostPrepare_Locks(TransactionId xid);
427 extern int LockCheckConflicts(LockMethod lockMethodTable,
429 LOCK *lock, PROCLOCK *proclock, PGPROC *proc);
430 extern void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode);
431 extern void GrantAwaitedLock(void);
432 extern void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode);
433 extern Size LockShmemSize(void);
434 extern LockData *GetLockStatusData(void);
435 extern const char *GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode);
437 extern void lock_twophase_recover(TransactionId xid, uint16 info,
438 void *recdata, uint32 len);
439 extern void lock_twophase_postcommit(TransactionId xid, uint16 info,
440 void *recdata, uint32 len);
441 extern void lock_twophase_postabort(TransactionId xid, uint16 info,
442 void *recdata, uint32 len);
444 extern bool DeadLockCheck(PGPROC *proc);
445 extern void DeadLockReport(void);
446 extern void RememberSimpleDeadLock(PGPROC *proc1,
450 extern void InitDeadLockChecking(void);
453 extern void DumpLocks(PGPROC *proc);
454 extern void DumpAllLocks(void);