gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

598 lines
23KB

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