lock.h 24 KB

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