Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

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