PostgreSQL Source Code  git master
lock.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * lock.h
4  * POSTGRES low-level lock mechanism
5  *
6  *
7  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/lock.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef LOCK_H_
15 #define LOCK_H_
16 
17 #ifdef FRONTEND
18 #error "lock.h may not be included from frontend code"
19 #endif
20 
21 #include "storage/backendid.h"
22 #include "storage/lockdefs.h"
23 #include "storage/lwlock.h"
24 #include "storage/shmem.h"
25 
26 /* struct PGPROC is declared in proc.h, but must forward-reference it */
27 typedef struct PGPROC PGPROC;
28 
29 typedef struct PROC_QUEUE
30 {
31  SHM_QUEUE links; /* head of list of PGPROC objects */
32  int size; /* number of entries in list */
33 } PROC_QUEUE;
34 
35 /* GUC variables */
36 extern int max_locks_per_xact;
37 
38 #ifdef LOCK_DEBUG
39 extern int Trace_lock_oidmin;
40 extern bool Trace_locks;
41 extern bool Trace_userlocks;
42 extern int Trace_lock_table;
43 extern bool Debug_deadlocks;
44 #endif /* LOCK_DEBUG */
45 
46 
47 /*
48  * Top-level transactions are identified by VirtualTransactionIDs comprising
49  * the BackendId of the backend running the xact, plus a locally-assigned
50  * LocalTransactionId. These are guaranteed unique over the short term,
51  * but will be reused after a database restart; hence they should never
52  * be stored on disk.
53  *
54  * Note that struct VirtualTransactionId can not be assumed to be atomically
55  * assignable as a whole. However, type LocalTransactionId is assumed to
56  * be atomically assignable, and the backend ID doesn't change often enough
57  * to be a problem, so we can fetch or assign the two fields separately.
58  * We deliberately refrain from using the struct within PGPROC, to prevent
59  * coding errors from trying to use struct assignment with it; instead use
60  * GET_VXID_FROM_PGPROC().
61  */
62 typedef struct
63 {
64  BackendId backendId; /* determined at backend startup */
65  LocalTransactionId localTransactionId; /* backend-local transaction id */
67 
68 #define InvalidLocalTransactionId 0
69 #define LocalTransactionIdIsValid(lxid) ((lxid) != InvalidLocalTransactionId)
70 #define VirtualTransactionIdIsValid(vxid) \
71  (((vxid).backendId != InvalidBackendId) && \
72  LocalTransactionIdIsValid((vxid).localTransactionId))
73 #define VirtualTransactionIdEquals(vxid1, vxid2) \
74  ((vxid1).backendId == (vxid2).backendId && \
75  (vxid1).localTransactionId == (vxid2).localTransactionId)
76 #define SetInvalidVirtualTransactionId(vxid) \
77  ((vxid).backendId = InvalidBackendId, \
78  (vxid).localTransactionId = InvalidLocalTransactionId)
79 #define GET_VXID_FROM_PGPROC(vxid, proc) \
80  ((vxid).backendId = (proc).backendId, \
81  (vxid).localTransactionId = (proc).lxid)
82 
83 /* MAX_LOCKMODES cannot be larger than the # of bits in LOCKMASK */
84 #define MAX_LOCKMODES 10
85 
86 #define LOCKBIT_ON(lockmode) (1 << (lockmode))
87 #define LOCKBIT_OFF(lockmode) (~(1 << (lockmode)))
88 
89 
90 /*
91  * This data structure defines the locking semantics associated with a
92  * "lock method". The semantics specify the meaning of each lock mode
93  * (by defining which lock modes it conflicts with).
94  * All of this data is constant and is kept in const tables.
95  *
96  * numLockModes -- number of lock modes (READ,WRITE,etc) that
97  * are defined in this lock method. Must be less than MAX_LOCKMODES.
98  *
99  * conflictTab -- this is an array of bitmasks showing lock
100  * mode conflicts. conflictTab[i] is a mask with the j-th bit
101  * turned on if lock modes i and j conflict. Lock modes are
102  * numbered 1..numLockModes; conflictTab[0] is unused.
103  *
104  * lockModeNames -- ID strings for debug printouts.
105  *
106  * trace_flag -- pointer to GUC trace flag for this lock method. (The
107  * GUC variable is not constant, but we use "const" here to denote that
108  * it can't be changed through this reference.)
109  */
110 typedef struct LockMethodData
111 {
114  const char *const *lockModeNames;
115  const bool *trace_flag;
117 
118 typedef const LockMethodData *LockMethod;
119 
120 /*
121  * Lock methods are identified by LOCKMETHODID. (Despite the declaration as
122  * uint16, we are constrained to 256 lockmethods by the layout of LOCKTAG.)
123  */
125 
126 /* These identify the known lock methods */
127 #define DEFAULT_LOCKMETHOD 1
128 #define USER_LOCKMETHOD 2
129 
130 /*
131  * LOCKTAG is the key information needed to look up a LOCK item in the
132  * lock hashtable. A LOCKTAG value uniquely identifies a lockable object.
133  *
134  * The LockTagType enum defines the different kinds of objects we can lock.
135  * We can handle up to 256 different LockTagTypes.
136  */
137 typedef enum LockTagType
138 {
139  LOCKTAG_RELATION, /* whole relation */
140  LOCKTAG_RELATION_EXTEND, /* the right to extend a relation */
141  LOCKTAG_DATABASE_FROZEN_IDS, /* pg_database.datfrozenxid */
142  LOCKTAG_PAGE, /* one page of a relation */
143  LOCKTAG_TUPLE, /* one physical tuple */
144  LOCKTAG_TRANSACTION, /* transaction (for waiting for xact done) */
145  LOCKTAG_VIRTUALTRANSACTION, /* virtual transaction (ditto) */
146  LOCKTAG_SPECULATIVE_TOKEN, /* speculative insertion Xid and token */
147  LOCKTAG_OBJECT, /* non-relation database object */
148  LOCKTAG_USERLOCK, /* reserved for old contrib/userlock code */
149  LOCKTAG_ADVISORY /* advisory user locks */
150 } LockTagType;
151 
152 #define LOCKTAG_LAST_TYPE LOCKTAG_ADVISORY
153 
154 extern const char *const LockTagTypeNames[];
155 
156 /*
157  * The LOCKTAG struct is defined with malice aforethought to fit into 16
158  * bytes with no padding. Note that this would need adjustment if we were
159  * to widen Oid, BlockNumber, or TransactionId to more than 32 bits.
160  *
161  * We include lockmethodid in the locktag so that a single hash table in
162  * shared memory can store locks of different lockmethods.
163  */
164 typedef struct LOCKTAG
165 {
166  uint32 locktag_field1; /* a 32-bit ID field */
167  uint32 locktag_field2; /* a 32-bit ID field */
168  uint32 locktag_field3; /* a 32-bit ID field */
169  uint16 locktag_field4; /* a 16-bit ID field */
170  uint8 locktag_type; /* see enum LockTagType */
171  uint8 locktag_lockmethodid; /* lockmethod indicator */
172 } LOCKTAG;
173 
174 /*
175  * These macros define how we map logical IDs of lockable objects into
176  * the physical fields of LOCKTAG. Use these to set up LOCKTAG values,
177  * rather than accessing the fields directly. Note multiple eval of target!
178  */
179 
180 /* ID info for a relation is DB OID + REL OID; DB OID = 0 if shared */
181 #define SET_LOCKTAG_RELATION(locktag,dboid,reloid) \
182  ((locktag).locktag_field1 = (dboid), \
183  (locktag).locktag_field2 = (reloid), \
184  (locktag).locktag_field3 = 0, \
185  (locktag).locktag_field4 = 0, \
186  (locktag).locktag_type = LOCKTAG_RELATION, \
187  (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
188 
189 /* same ID info as RELATION */
190 #define SET_LOCKTAG_RELATION_EXTEND(locktag,dboid,reloid) \
191  ((locktag).locktag_field1 = (dboid), \
192  (locktag).locktag_field2 = (reloid), \
193  (locktag).locktag_field3 = 0, \
194  (locktag).locktag_field4 = 0, \
195  (locktag).locktag_type = LOCKTAG_RELATION_EXTEND, \
196  (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
197 
198 /* ID info for frozen IDs is DB OID */
199 #define SET_LOCKTAG_DATABASE_FROZEN_IDS(locktag,dboid) \
200  ((locktag).locktag_field1 = (dboid), \
201  (locktag).locktag_field2 = 0, \
202  (locktag).locktag_field3 = 0, \
203  (locktag).locktag_field4 = 0, \
204  (locktag).locktag_type = LOCKTAG_DATABASE_FROZEN_IDS, \
205  (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
206 
207 /* ID info for a page is RELATION info + BlockNumber */
208 #define SET_LOCKTAG_PAGE(locktag,dboid,reloid,blocknum) \
209  ((locktag).locktag_field1 = (dboid), \
210  (locktag).locktag_field2 = (reloid), \
211  (locktag).locktag_field3 = (blocknum), \
212  (locktag).locktag_field4 = 0, \
213  (locktag).locktag_type = LOCKTAG_PAGE, \
214  (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
215 
216 /* ID info for a tuple is PAGE info + OffsetNumber */
217 #define SET_LOCKTAG_TUPLE(locktag,dboid,reloid,blocknum,offnum) \
218  ((locktag).locktag_field1 = (dboid), \
219  (locktag).locktag_field2 = (reloid), \
220  (locktag).locktag_field3 = (blocknum), \
221  (locktag).locktag_field4 = (offnum), \
222  (locktag).locktag_type = LOCKTAG_TUPLE, \
223  (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
224 
225 /* ID info for a transaction is its TransactionId */
226 #define SET_LOCKTAG_TRANSACTION(locktag,xid) \
227  ((locktag).locktag_field1 = (xid), \
228  (locktag).locktag_field2 = 0, \
229  (locktag).locktag_field3 = 0, \
230  (locktag).locktag_field4 = 0, \
231  (locktag).locktag_type = LOCKTAG_TRANSACTION, \
232  (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
233 
234 /* ID info for a virtual transaction is its VirtualTransactionId */
235 #define SET_LOCKTAG_VIRTUALTRANSACTION(locktag,vxid) \
236  ((locktag).locktag_field1 = (vxid).backendId, \
237  (locktag).locktag_field2 = (vxid).localTransactionId, \
238  (locktag).locktag_field3 = 0, \
239  (locktag).locktag_field4 = 0, \
240  (locktag).locktag_type = LOCKTAG_VIRTUALTRANSACTION, \
241  (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
242 
243 /*
244  * ID info for a speculative insert is TRANSACTION info +
245  * its speculative insert counter.
246  */
247 #define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag,xid,token) \
248  ((locktag).locktag_field1 = (xid), \
249  (locktag).locktag_field2 = (token), \
250  (locktag).locktag_field3 = 0, \
251  (locktag).locktag_field4 = 0, \
252  (locktag).locktag_type = LOCKTAG_SPECULATIVE_TOKEN, \
253  (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
254 
255 /*
256  * ID info for an object is DB OID + CLASS OID + OBJECT OID + SUBID
257  *
258  * Note: object ID has same representation as in pg_depend and
259  * pg_description, but notice that we are constraining SUBID to 16 bits.
260  * Also, we use DB OID = 0 for shared objects such as tablespaces.
261  */
262 #define SET_LOCKTAG_OBJECT(locktag,dboid,classoid,objoid,objsubid) \
263  ((locktag).locktag_field1 = (dboid), \
264  (locktag).locktag_field2 = (classoid), \
265  (locktag).locktag_field3 = (objoid), \
266  (locktag).locktag_field4 = (objsubid), \
267  (locktag).locktag_type = LOCKTAG_OBJECT, \
268  (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD)
269 
270 #define SET_LOCKTAG_ADVISORY(locktag,id1,id2,id3,id4) \
271  ((locktag).locktag_field1 = (id1), \
272  (locktag).locktag_field2 = (id2), \
273  (locktag).locktag_field3 = (id3), \
274  (locktag).locktag_field4 = (id4), \
275  (locktag).locktag_type = LOCKTAG_ADVISORY, \
276  (locktag).locktag_lockmethodid = USER_LOCKMETHOD)
277 
278 
279 /*
280  * Per-locked-object lock information:
281  *
282  * tag -- uniquely identifies the object being locked
283  * grantMask -- bitmask for all lock types currently granted on this object.
284  * waitMask -- bitmask for all lock types currently awaited on this object.
285  * procLocks -- list of PROCLOCK objects for this lock.
286  * waitProcs -- queue of processes waiting for this lock.
287  * requested -- count of each lock type currently requested on the lock
288  * (includes requests already granted!!).
289  * nRequested -- total requested locks of all types.
290  * granted -- count of each lock type currently granted on the lock.
291  * nGranted -- total granted locks of all types.
292  *
293  * Note: these counts count 1 for each backend. Internally to a backend,
294  * there may be multiple grabs on a particular lock, but this is not reflected
295  * into shared memory.
296  */
297 typedef struct LOCK
298 {
299  /* hash key */
300  LOCKTAG tag; /* unique identifier of lockable object */
301 
302  /* data */
303  LOCKMASK grantMask; /* bitmask for lock types already granted */
304  LOCKMASK waitMask; /* bitmask for lock types awaited */
305  SHM_QUEUE procLocks; /* list of PROCLOCK objects assoc. with lock */
306  PROC_QUEUE waitProcs; /* list of PGPROC objects waiting on lock */
307  int requested[MAX_LOCKMODES]; /* counts of requested locks */
308  int nRequested; /* total of requested[] array */
309  int granted[MAX_LOCKMODES]; /* counts of granted locks */
310  int nGranted; /* total of granted[] array */
311 } LOCK;
312 
313 #define LOCK_LOCKMETHOD(lock) ((LOCKMETHODID) (lock).tag.locktag_lockmethodid)
314 #define LOCK_LOCKTAG(lock) ((LockTagType) (lock).tag.locktag_type)
315 
316 
317 /*
318  * We may have several different backends holding or awaiting locks
319  * on the same lockable object. We need to store some per-holder/waiter
320  * information for each such holder (or would-be holder). This is kept in
321  * a PROCLOCK struct.
322  *
323  * PROCLOCKTAG is the key information needed to look up a PROCLOCK item in the
324  * proclock hashtable. A PROCLOCKTAG value uniquely identifies the combination
325  * of a lockable object and a holder/waiter for that object. (We can use
326  * pointers here because the PROCLOCKTAG need only be unique for the lifespan
327  * of the PROCLOCK, and it will never outlive the lock or the proc.)
328  *
329  * Internally to a backend, it is possible for the same lock to be held
330  * for different purposes: the backend tracks transaction locks separately
331  * from session locks. However, this is not reflected in the shared-memory
332  * state: we only track which backend(s) hold the lock. This is OK since a
333  * backend can never block itself.
334  *
335  * The holdMask field shows the already-granted locks represented by this
336  * proclock. Note that there will be a proclock object, possibly with
337  * zero holdMask, for any lock that the process is currently waiting on.
338  * Otherwise, proclock objects whose holdMasks are zero are recycled
339  * as soon as convenient.
340  *
341  * releaseMask is workspace for LockReleaseAll(): it shows the locks due
342  * to be released during the current call. This must only be examined or
343  * set by the backend owning the PROCLOCK.
344  *
345  * Each PROCLOCK object is linked into lists for both the associated LOCK
346  * object and the owning PGPROC object. Note that the PROCLOCK is entered
347  * into these lists as soon as it is created, even if no lock has yet been
348  * granted. A PGPROC that is waiting for a lock to be granted will also be
349  * linked into the lock's waitProcs queue.
350  */
351 typedef struct PROCLOCKTAG
352 {
353  /* NB: we assume this struct contains no padding! */
354  LOCK *myLock; /* link to per-lockable-object information */
355  PGPROC *myProc; /* link to PGPROC of owning backend */
356 } PROCLOCKTAG;
357 
358 typedef struct PROCLOCK
359 {
360  /* tag */
361  PROCLOCKTAG tag; /* unique identifier of proclock object */
362 
363  /* data */
364  PGPROC *groupLeader; /* proc's lock group leader, or proc itself */
365  LOCKMASK holdMask; /* bitmask for lock types currently held */
366  LOCKMASK releaseMask; /* bitmask for lock types to be released */
367  SHM_QUEUE lockLink; /* list link in LOCK's list of proclocks */
368  SHM_QUEUE procLink; /* list link in PGPROC's list of proclocks */
369 } PROCLOCK;
370 
371 #define PROCLOCK_LOCKMETHOD(proclock) \
372  LOCK_LOCKMETHOD(*((proclock).tag.myLock))
373 
374 /*
375  * Each backend also maintains a local hash table with information about each
376  * lock it is currently interested in. In particular the local table counts
377  * the number of times that lock has been acquired. This allows multiple
378  * requests for the same lock to be executed without additional accesses to
379  * shared memory. We also track the number of lock acquisitions per
380  * ResourceOwner, so that we can release just those locks belonging to a
381  * particular ResourceOwner.
382  *
383  * When holding a lock taken "normally", the lock and proclock fields always
384  * point to the associated objects in shared memory. However, if we acquired
385  * the lock via the fast-path mechanism, the lock and proclock fields are set
386  * to NULL, since there probably aren't any such objects in shared memory.
387  * (If the lock later gets promoted to normal representation, we may eventually
388  * update our locallock's lock/proclock fields after finding the shared
389  * objects.)
390  *
391  * Caution: a locallock object can be left over from a failed lock acquisition
392  * attempt. In this case its lock/proclock fields are untrustworthy, since
393  * the shared lock object is neither held nor awaited, and hence is available
394  * to be reclaimed. If nLocks > 0 then these pointers must either be valid or
395  * NULL, but when nLocks == 0 they should be considered garbage.
396  */
397 typedef struct LOCALLOCKTAG
398 {
399  LOCKTAG lock; /* identifies the lockable object */
400  LOCKMODE mode; /* lock mode for this table entry */
401 } LOCALLOCKTAG;
402 
403 typedef struct LOCALLOCKOWNER
404 {
405  /*
406  * Note: if owner is NULL then the lock is held on behalf of the session;
407  * otherwise it is held on behalf of my current transaction.
408  *
409  * Must use a forward struct reference to avoid circularity.
410  */
412  int64 nLocks; /* # of times held by this owner */
414 
415 typedef struct LOCALLOCK
416 {
417  /* tag */
418  LOCALLOCKTAG tag; /* unique identifier of locallock entry */
419 
420  /* data */
421  uint32 hashcode; /* copy of LOCKTAG's hash value */
422  LOCK *lock; /* associated LOCK object, if any */
423  PROCLOCK *proclock; /* associated PROCLOCK object, if any */
424  int64 nLocks; /* total number of times lock is held */
425  int numLockOwners; /* # of relevant ResourceOwners */
426  int maxLockOwners; /* allocated size of array */
427  LOCALLOCKOWNER *lockOwners; /* dynamically resizable array */
428  bool holdsStrongLockCount; /* bumped FastPathStrongRelationLocks */
429  bool lockCleared; /* we read all sinval msgs for lock */
430 } LOCALLOCK;
431 
432 #define LOCALLOCK_LOCKMETHOD(llock) ((llock).tag.lock.locktag_lockmethodid)
433 #define LOCALLOCK_LOCKTAG(llock) ((LockTagType) (llock).tag.lock.locktag_type)
434 
435 
436 /*
437  * These structures hold information passed from lmgr internals to the lock
438  * listing user-level functions (in lockfuncs.c).
439  */
440 
441 typedef struct LockInstanceData
442 {
443  LOCKTAG locktag; /* tag for locked object */
444  LOCKMASK holdMask; /* locks held by this PGPROC */
445  LOCKMODE waitLockMode; /* lock awaited by this PGPROC, if any */
446  BackendId backend; /* backend ID of this PGPROC */
447  LocalTransactionId lxid; /* local transaction ID of this PGPROC */
448  int pid; /* pid of this PGPROC */
449  int leaderPid; /* pid of group leader; = pid if no group */
450  bool fastpath; /* taken via fastpath? */
452 
453 typedef struct LockData
454 {
455  int nelements; /* The length of the array */
456  LockInstanceData *locks; /* Array of per-PROCLOCK information */
457 } LockData;
458 
459 typedef struct BlockedProcData
460 {
461  int pid; /* pid of a blocked PGPROC */
462  /* Per-PROCLOCK information about PROCLOCKs of the lock the pid awaits */
463  /* (these fields refer to indexes in BlockedProcsData.locks[]) */
464  int first_lock; /* index of first relevant LockInstanceData */
465  int num_locks; /* number of relevant LockInstanceDatas */
466  /* PIDs of PGPROCs that are ahead of "pid" in the lock's wait queue */
467  /* (these fields refer to indexes in BlockedProcsData.waiter_pids[]) */
468  int first_waiter; /* index of first preceding waiter */
469  int num_waiters; /* number of preceding waiters */
471 
472 typedef struct BlockedProcsData
473 {
474  BlockedProcData *procs; /* Array of per-blocked-proc information */
475  LockInstanceData *locks; /* Array of per-PROCLOCK information */
476  int *waiter_pids; /* Array of PIDs of other blocked PGPROCs */
477  int nprocs; /* # of valid entries in procs[] array */
478  int maxprocs; /* Allocated length of procs[] array */
479  int nlocks; /* # of valid entries in locks[] array */
480  int maxlocks; /* Allocated length of locks[] array */
481  int npids; /* # of valid entries in waiter_pids[] array */
482  int maxpids; /* Allocated length of waiter_pids[] array */
484 
485 
486 /* Result codes for LockAcquire() */
487 typedef enum
488 {
489  LOCKACQUIRE_NOT_AVAIL, /* lock not available, and dontWait=true */
490  LOCKACQUIRE_OK, /* lock successfully acquired */
491  LOCKACQUIRE_ALREADY_HELD, /* incremented count for lock already held */
492  LOCKACQUIRE_ALREADY_CLEAR /* incremented count for lock already clear */
494 
495 /* Deadlock states identified by DeadLockCheck() */
496 typedef enum
497 {
498  DS_NOT_YET_CHECKED, /* no deadlock check has run yet */
499  DS_NO_DEADLOCK, /* no deadlock detected */
500  DS_SOFT_DEADLOCK, /* deadlock avoided by queue rearrangement */
501  DS_HARD_DEADLOCK, /* deadlock, no way out but ERROR */
502  DS_BLOCKED_BY_AUTOVACUUM /* no deadlock; queue blocked by autovacuum
503  * worker */
504 } DeadLockState;
505 
506 /*
507  * The lockmgr's shared hash tables are partitioned to reduce contention.
508  * To determine which partition a given locktag belongs to, compute the tag's
509  * hash code with LockTagHashCode(), then apply one of these macros.
510  * NB: NUM_LOCK_PARTITIONS must be a power of 2!
511  */
512 #define LockHashPartition(hashcode) \
513  ((hashcode) % NUM_LOCK_PARTITIONS)
514 #define LockHashPartitionLock(hashcode) \
515  (&MainLWLockArray[LOCK_MANAGER_LWLOCK_OFFSET + \
516  LockHashPartition(hashcode)].lock)
517 #define LockHashPartitionLockByIndex(i) \
518  (&MainLWLockArray[LOCK_MANAGER_LWLOCK_OFFSET + (i)].lock)
519 
520 /*
521  * The deadlock detector needs to be able to access lockGroupLeader and
522  * related fields in the PGPROC, so we arrange for those fields to be protected
523  * by one of the lock hash partition locks. Since the deadlock detector
524  * acquires all such locks anyway, this makes it safe for it to access these
525  * fields without doing anything extra. To avoid contention as much as
526  * possible, we map different PGPROCs to different partition locks. The lock
527  * used for a given lock group is determined by the group leader's pgprocno.
528  */
529 #define LockHashPartitionLockByProc(leader_pgproc) \
530  LockHashPartitionLock((leader_pgproc)->pgprocno)
531 
532 /*
533  * function prototypes
534  */
535 extern void InitLocks(void);
536 extern LockMethod GetLocksMethodTable(const LOCK *lock);
537 extern LockMethod GetLockTagsMethodTable(const LOCKTAG *locktag);
538 extern uint32 LockTagHashCode(const LOCKTAG *locktag);
539 extern bool DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2);
540 extern LockAcquireResult LockAcquire(const LOCKTAG *locktag,
541  LOCKMODE lockmode,
542  bool sessionLock,
543  bool dontWait);
544 extern LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag,
545  LOCKMODE lockmode,
546  bool sessionLock,
547  bool dontWait,
548  bool reportMemoryError,
549  LOCALLOCK **locallockp);
550 extern void AbortStrongLockAcquire(void);
551 extern void MarkLockClear(LOCALLOCK *locallock);
552 extern bool LockRelease(const LOCKTAG *locktag,
553  LOCKMODE lockmode, bool sessionLock);
554 extern void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks);
555 extern void LockReleaseSession(LOCKMETHODID lockmethodid);
556 extern void LockReleaseCurrentOwner(LOCALLOCK **locallocks, int nlocks);
557 extern void LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks);
558 extern bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode);
559 #ifdef USE_ASSERT_CHECKING
560 extern HTAB *GetLockMethodLocalHash(void);
561 #endif
562 extern bool LockHasWaiters(const LOCKTAG *locktag,
563  LOCKMODE lockmode, bool sessionLock);
564 extern VirtualTransactionId *GetLockConflicts(const LOCKTAG *locktag,
565  LOCKMODE lockmode, int *countp);
566 extern void AtPrepare_Locks(void);
567 extern void PostPrepare_Locks(TransactionId xid);
568 extern bool LockCheckConflicts(LockMethod lockMethodTable,
569  LOCKMODE lockmode,
570  LOCK *lock, PROCLOCK *proclock);
571 extern void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode);
572 extern void GrantAwaitedLock(void);
573 extern void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode);
574 extern Size LockShmemSize(void);
575 extern LockData *GetLockStatusData(void);
576 extern BlockedProcsData *GetBlockerStatusData(int blocked_pid);
577 
578 extern xl_standby_lock *GetRunningTransactionLocks(int *nlocks);
579 extern const char *GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode);
580 
581 extern void lock_twophase_recover(TransactionId xid, uint16 info,
582  void *recdata, uint32 len);
583 extern void lock_twophase_postcommit(TransactionId xid, uint16 info,
584  void *recdata, uint32 len);
585 extern void lock_twophase_postabort(TransactionId xid, uint16 info,
586  void *recdata, uint32 len);
588  void *recdata, uint32 len);
589 
590 extern DeadLockState DeadLockCheck(PGPROC *proc);
591 extern PGPROC *GetBlockingAutoVacuumPgproc(void);
592 extern void DeadLockReport(void) pg_attribute_noreturn();
593 extern void RememberSimpleDeadLock(PGPROC *proc1,
594  LOCKMODE lockmode,
595  LOCK *lock,
596  PGPROC *proc2);
597 extern void InitDeadLockChecking(void);
598 
599 extern int LockWaiterCount(const LOCKTAG *locktag);
600 
601 #ifdef LOCK_DEBUG
602 extern void DumpLocks(PGPROC *proc);
603 extern void DumpAllLocks(void);
604 #endif
605 
606 /* Lock a VXID (used to wait for a transaction to finish) */
608 extern void VirtualXactLockTableCleanup(void);
609 extern bool VirtualXactLock(VirtualTransactionId vxid, bool wait);
610 
611 #endif /* LOCK_H_ */
struct BlockedProcData BlockedProcData
const char *const LockTagTypeNames[]
Definition: lockfuncs.c:29
PROCLOCKTAG tag
Definition: lock.h:361
static PgChecksumMode mode
Definition: pg_checksums.c:61
#define pg_attribute_noreturn()
Definition: c.h:145
uint32 hashcode
Definition: lock.h:421
void MarkLockClear(LOCALLOCK *locallock)
Definition: lock.c:1799
Size LockShmemSize(void)
Definition: lock.c:3532
void VirtualXactLockTableInsert(VirtualTransactionId vxid)
Definition: lock.c:4391
LOCALLOCKTAG tag
Definition: lock.h:418
bool holdsStrongLockCount
Definition: lock.h:428
void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode)
Definition: lock.c:1918
int * waiter_pids
Definition: lock.h:476
void AbortStrongLockAcquire(void)
Definition: lock.c:1757
int numLockOwners
Definition: lock.h:425
LockInstanceData * locks
Definition: lock.h:456
LOCKTAG lock
Definition: lock.h:399
uint32 TransactionId
Definition: c.h:520
void lock_twophase_postcommit(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: lock.c:4341
int LOCKMODE
Definition: lockdefs.h:26
int first_lock
Definition: lock.h:464
LOCKMODE mode
Definition: lock.h:400
void LockReleaseCurrentOwner(LOCALLOCK **locallocks, int nlocks)
Definition: lock.c:2484
PROCLOCK * proclock
Definition: lock.h:423
int nRequested
Definition: lock.h:308
SHM_QUEUE links
Definition: lock.h:31
int num_waiters
Definition: lock.h:469
LOCKMASK holdMask
Definition: lock.h:365
int64 nLocks
Definition: lock.h:412
LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait, bool reportMemoryError, LOCALLOCK **locallockp)
Definition: lock.c:771
bool fastpath
Definition: lock.h:450
LockMethod GetLocksMethodTable(const LOCK *lock)
Definition: lock.c:487
struct LOCALLOCKOWNER LOCALLOCKOWNER
LOCKTAG tag
Definition: lock.h:300
void PostPrepare_Locks(TransactionId xid)
Definition: lock.c:3340
unsigned char uint8
Definition: c.h:372
Definition: lock.h:164
const LOCKMASK * conflictTab
Definition: lock.h:113
int LockWaiterCount(const LOCKTAG *locktag)
Definition: lock.c:4557
SHM_QUEUE lockLink
Definition: lock.h:367
void LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks)
Definition: lock.c:2579
LockMethod GetLockTagsMethodTable(const LOCKTAG *locktag)
Definition: lock.c:499
BlockedProcData * procs
Definition: lock.h:474
Definition: lock.h:453
LocalTransactionId localTransactionId
Definition: lock.h:65
struct LOCALLOCKTAG LOCALLOCKTAG
LOCKTAG locktag
Definition: lock.h:443
LOCKMODE waitLockMode
Definition: lock.h:445
int maxLockOwners
Definition: lock.h:426
int nGranted
Definition: lock.h:310
void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks)
Definition: lock.c:2180
void DeadLockReport(void) pg_attribute_noreturn()
Definition: deadlock.c:1090
int num_locks
Definition: lock.h:465
int max_locks_per_xact
Definition: lock.c:54
PROC_QUEUE waitProcs
Definition: lock.h:306
void InitLocks(void)
Definition: lock.c:404
uint16 locktag_field4
Definition: lock.h:169
BlockedProcsData * GetBlockerStatusData(int blocked_pid)
Definition: lock.c:3753
int leaderPid
Definition: lock.h:449
Definition: dynahash.c:218
struct LOCALLOCK LOCALLOCK
unsigned short uint16
Definition: c.h:373
struct BlockedProcsData BlockedProcsData
bool LockCheckConflicts(LockMethod lockMethodTable, LOCKMODE lockmode, LOCK *lock, PROCLOCK *proclock)
Definition: lock.c:1420
void lock_twophase_postabort(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: lock.c:4367
LockAcquireResult LockAcquire(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock, bool dontWait)
Definition: lock.c:747
void VirtualXactLockTableCleanup(void)
Definition: lock.c:4414
const char * GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode)
Definition: lock.c:4015
uint32 locktag_field2
Definition: lock.h:167
unsigned int uint32
Definition: c.h:374
bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode)
Definition: lock.c:598
Definition: lock.h:297
uint32 LocalTransactionId
Definition: c.h:522
xl_standby_lock * GetRunningTransactionLocks(int *nlocks)
Definition: lock.c:3933
struct PROCLOCK PROCLOCK
LOCKMASK waitMask
Definition: lock.h:304
int maxlocks
Definition: lock.h:480
uint16 LOCKMETHODID
Definition: lock.h:124
SHM_QUEUE procLocks
Definition: lock.h:305
struct LOCKTAG LOCKTAG
int nelements
Definition: lock.h:455
#define MAX_LOCKMODES
Definition: lock.h:84
SHM_QUEUE procLink
Definition: lock.h:368
LockInstanceData * locks
Definition: lock.h:475
struct PROCLOCKTAG PROCLOCKTAG
const LockMethodData * LockMethod
Definition: lock.h:118
BackendId backend
Definition: lock.h:446
int BackendId
Definition: backendid.h:21
bool LockRelease(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:1975
bool LockHasWaiters(const LOCKTAG *locktag, LOCKMODE lockmode, bool sessionLock)
Definition: lock.c:634
void RememberSimpleDeadLock(PGPROC *proc1, LOCKMODE lockmode, LOCK *lock, PGPROC *proc2)
Definition: deadlock.c:1162
DeadLockState DeadLockCheck(PGPROC *proc)
Definition: deadlock.c:217
struct LOCK LOCK
void lock_twophase_recover(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: lock.c:4128
int first_waiter
Definition: lock.h:468
void GrantAwaitedLock(void)
Definition: lock.c:1786
bool DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2)
Definition: lock.c:583
uint8 locktag_type
Definition: lock.h:170
DeadLockState
Definition: lock.h:496
LocalTransactionId lxid
Definition: lock.h:447
BackendId backendId
Definition: lock.h:64
size_t Size
Definition: c.h:473
bool VirtualXactLock(VirtualTransactionId vxid, bool wait)
Definition: lock.c:4461
LOCALLOCKOWNER * lockOwners
Definition: lock.h:427
LOCK * lock
Definition: lock.h:422
int LOCKMASK
Definition: lockdefs.h:25
struct LockMethodData LockMethodData
LOCKMASK holdMask
Definition: lock.h:444
uint8 locktag_lockmethodid
Definition: lock.h:171
PGPROC * myProc
Definition: lock.h:355
LOCKMASK grantMask
Definition: lock.h:303
Definition: lock.h:358
int64 nLocks
Definition: lock.h:424
struct PROC_QUEUE PROC_QUEUE
LockAcquireResult
Definition: lock.h:487
void InitDeadLockChecking(void)
Definition: deadlock.c:143
struct LockInstanceData LockInstanceData
const bool * trace_flag
Definition: lock.h:115
LockData * GetLockStatusData(void)
Definition: lock.c:3569
int size
Definition: lock.h:32
LOCK * myLock
Definition: lock.h:354
LockTagType
Definition: lock.h:137
struct ResourceOwnerData * owner
Definition: lock.h:411
void lock_twophase_standby_recover(TransactionId xid, uint16 info, void *recdata, uint32 len)
Definition: lock.c:4309
struct LockData LockData
VirtualTransactionId * GetLockConflicts(const LOCKTAG *locktag, LOCKMODE lockmode, int *countp)
Definition: lock.c:2912
PGPROC * GetBlockingAutoVacuumPgproc(void)
Definition: deadlock.c:293
void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode)
Definition: lock.c:1555
uint32 locktag_field1
Definition: lock.h:166
Definition: proc.h:112
uint32 LockTagHashCode(const LOCKTAG *locktag)
Definition: lock.c:517
uint32 locktag_field3
Definition: lock.h:168
void AtPrepare_Locks(void)
Definition: lock.c:3230
bool lockCleared
Definition: lock.h:429
const char *const * lockModeNames
Definition: lock.h:114
PGPROC * groupLeader
Definition: lock.h:364
void LockReleaseSession(LOCKMETHODID lockmethodid)
Definition: lock.c:2454
int numLockModes
Definition: lock.h:112
int maxprocs
Definition: lock.h:478
LOCKMASK releaseMask
Definition: lock.h:366