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