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