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