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