lock.h 23 KB

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