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