| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef LOCK_H_ |
| #define LOCK_H_ |
|
|
| #ifdef FRONTEND |
| #error "lock.h may not be included from frontend code" |
| #endif |
|
|
| #include "lib/ilist.h" |
| #include "storage/backendid.h" |
| #include "storage/lockdefs.h" |
| #include "storage/lwlock.h" |
| #include "storage/shmem.h" |
| #include "utils/timestamp.h" |
|
|
| |
| typedef struct PGPROC PGPROC; |
|
|
| |
| extern PGDLLIMPORT int max_locks_per_xact; |
|
|
| #ifdef LOCK_DEBUG |
| extern PGDLLIMPORT int Trace_lock_oidmin; |
| extern PGDLLIMPORT bool Trace_locks; |
| extern PGDLLIMPORT bool Trace_userlocks; |
| extern PGDLLIMPORT int Trace_lock_table; |
| extern PGDLLIMPORT bool Debug_deadlocks; |
| #endif |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct |
| { |
| BackendId backendId; |
| LocalTransactionId localTransactionId; |
| } VirtualTransactionId; |
|
|
| #define InvalidLocalTransactionId 0 |
| #define LocalTransactionIdIsValid(lxid) ((lxid) != InvalidLocalTransactionId) |
| #define VirtualTransactionIdIsValid(vxid) \ |
| (LocalTransactionIdIsValid((vxid).localTransactionId)) |
| #define VirtualTransactionIdIsRecoveredPreparedXact(vxid) \ |
| ((vxid).backendId == InvalidBackendId) |
| #define VirtualTransactionIdEquals(vxid1, vxid2) \ |
| ((vxid1).backendId == (vxid2).backendId && \ |
| (vxid1).localTransactionId == (vxid2).localTransactionId) |
| #define SetInvalidVirtualTransactionId(vxid) \ |
| ((vxid).backendId = InvalidBackendId, \ |
| (vxid).localTransactionId = InvalidLocalTransactionId) |
| #define GET_VXID_FROM_PGPROC(vxid, proc) \ |
| ((vxid).backendId = (proc).backendId, \ |
| (vxid).localTransactionId = (proc).lxid) |
|
|
| |
| #define MAX_LOCKMODES 10 |
|
|
| #define LOCKBIT_ON(lockmode) (1 << (lockmode)) |
| #define LOCKBIT_OFF(lockmode) (~(1 << (lockmode))) |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct LockMethodData |
| { |
| int numLockModes; |
| const LOCKMASK *conflictTab; |
| const char *const *lockModeNames; |
| const bool *trace_flag; |
| } LockMethodData; |
|
|
| typedef const LockMethodData *LockMethod; |
|
|
| |
| |
| |
| |
| typedef uint16 LOCKMETHODID; |
|
|
| |
| #define DEFAULT_LOCKMETHOD 1 |
| #define USER_LOCKMETHOD 2 |
|
|
| |
| |
| |
| |
| |
| |
| |
| typedef enum LockTagType |
| { |
| LOCKTAG_RELATION, |
| LOCKTAG_RELATION_EXTEND, |
| LOCKTAG_DATABASE_FROZEN_IDS, |
| LOCKTAG_PAGE, |
| LOCKTAG_TUPLE, |
| LOCKTAG_TRANSACTION, |
| LOCKTAG_VIRTUALTRANSACTION, |
| LOCKTAG_SPECULATIVE_TOKEN, |
| LOCKTAG_OBJECT, |
| LOCKTAG_USERLOCK, |
| LOCKTAG_ADVISORY, |
| LOCKTAG_APPLY_TRANSACTION |
| |
| } LockTagType; |
|
|
| #define LOCKTAG_LAST_TYPE LOCKTAG_APPLY_TRANSACTION |
|
|
| extern PGDLLIMPORT const char *const LockTagTypeNames[]; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct LOCKTAG |
| { |
| uint32 locktag_field1; |
| uint32 locktag_field2; |
| uint32 locktag_field3; |
| uint16 locktag_field4; |
| uint8 locktag_type; |
| uint8 locktag_lockmethodid; |
| } LOCKTAG; |
|
|
| |
| |
| |
| |
| |
|
|
| |
| #define SET_LOCKTAG_RELATION(locktag,dboid,reloid) \ |
| ((locktag).locktag_field1 = (dboid), \ |
| (locktag).locktag_field2 = (reloid), \ |
| (locktag).locktag_field3 = 0, \ |
| (locktag).locktag_field4 = 0, \ |
| (locktag).locktag_type = LOCKTAG_RELATION, \ |
| (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) |
|
|
| |
| #define SET_LOCKTAG_RELATION_EXTEND(locktag,dboid,reloid) \ |
| ((locktag).locktag_field1 = (dboid), \ |
| (locktag).locktag_field2 = (reloid), \ |
| (locktag).locktag_field3 = 0, \ |
| (locktag).locktag_field4 = 0, \ |
| (locktag).locktag_type = LOCKTAG_RELATION_EXTEND, \ |
| (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) |
|
|
| |
| #define SET_LOCKTAG_DATABASE_FROZEN_IDS(locktag,dboid) \ |
| ((locktag).locktag_field1 = (dboid), \ |
| (locktag).locktag_field2 = 0, \ |
| (locktag).locktag_field3 = 0, \ |
| (locktag).locktag_field4 = 0, \ |
| (locktag).locktag_type = LOCKTAG_DATABASE_FROZEN_IDS, \ |
| (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) |
|
|
| |
| #define SET_LOCKTAG_PAGE(locktag,dboid,reloid,blocknum) \ |
| ((locktag).locktag_field1 = (dboid), \ |
| (locktag).locktag_field2 = (reloid), \ |
| (locktag).locktag_field3 = (blocknum), \ |
| (locktag).locktag_field4 = 0, \ |
| (locktag).locktag_type = LOCKTAG_PAGE, \ |
| (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) |
|
|
| |
| #define SET_LOCKTAG_TUPLE(locktag,dboid,reloid,blocknum,offnum) \ |
| ((locktag).locktag_field1 = (dboid), \ |
| (locktag).locktag_field2 = (reloid), \ |
| (locktag).locktag_field3 = (blocknum), \ |
| (locktag).locktag_field4 = (offnum), \ |
| (locktag).locktag_type = LOCKTAG_TUPLE, \ |
| (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) |
|
|
| |
| #define SET_LOCKTAG_TRANSACTION(locktag,xid) \ |
| ((locktag).locktag_field1 = (xid), \ |
| (locktag).locktag_field2 = 0, \ |
| (locktag).locktag_field3 = 0, \ |
| (locktag).locktag_field4 = 0, \ |
| (locktag).locktag_type = LOCKTAG_TRANSACTION, \ |
| (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) |
|
|
| |
| #define SET_LOCKTAG_VIRTUALTRANSACTION(locktag,vxid) \ |
| ((locktag).locktag_field1 = (vxid).backendId, \ |
| (locktag).locktag_field2 = (vxid).localTransactionId, \ |
| (locktag).locktag_field3 = 0, \ |
| (locktag).locktag_field4 = 0, \ |
| (locktag).locktag_type = LOCKTAG_VIRTUALTRANSACTION, \ |
| (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) |
|
|
| |
| |
| |
| |
| #define SET_LOCKTAG_SPECULATIVE_INSERTION(locktag,xid,token) \ |
| ((locktag).locktag_field1 = (xid), \ |
| (locktag).locktag_field2 = (token), \ |
| (locktag).locktag_field3 = 0, \ |
| (locktag).locktag_field4 = 0, \ |
| (locktag).locktag_type = LOCKTAG_SPECULATIVE_TOKEN, \ |
| (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) |
|
|
| |
| |
| |
| |
| |
| |
| |
| #define SET_LOCKTAG_OBJECT(locktag,dboid,classoid,objoid,objsubid) \ |
| ((locktag).locktag_field1 = (dboid), \ |
| (locktag).locktag_field2 = (classoid), \ |
| (locktag).locktag_field3 = (objoid), \ |
| (locktag).locktag_field4 = (objsubid), \ |
| (locktag).locktag_type = LOCKTAG_OBJECT, \ |
| (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) |
|
|
| #define SET_LOCKTAG_ADVISORY(locktag,id1,id2,id3,id4) \ |
| ((locktag).locktag_field1 = (id1), \ |
| (locktag).locktag_field2 = (id2), \ |
| (locktag).locktag_field3 = (id3), \ |
| (locktag).locktag_field4 = (id4), \ |
| (locktag).locktag_type = LOCKTAG_ADVISORY, \ |
| (locktag).locktag_lockmethodid = USER_LOCKMETHOD) |
|
|
| |
| |
| |
| |
| #define SET_LOCKTAG_APPLY_TRANSACTION(locktag,dboid,suboid,xid,objid) \ |
| ((locktag).locktag_field1 = (dboid), \ |
| (locktag).locktag_field2 = (suboid), \ |
| (locktag).locktag_field3 = (xid), \ |
| (locktag).locktag_field4 = (objid), \ |
| (locktag).locktag_type = LOCKTAG_APPLY_TRANSACTION, \ |
| (locktag).locktag_lockmethodid = DEFAULT_LOCKMETHOD) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct LOCK |
| { |
| |
| LOCKTAG tag; |
|
|
| |
| LOCKMASK grantMask; |
| LOCKMASK waitMask; |
| dlist_head procLocks; |
| dclist_head waitProcs; |
| int requested[MAX_LOCKMODES]; |
| int nRequested; |
| int granted[MAX_LOCKMODES]; |
| int nGranted; |
| } LOCK; |
|
|
| #define LOCK_LOCKMETHOD(lock) ((LOCKMETHODID) (lock).tag.locktag_lockmethodid) |
| #define LOCK_LOCKTAG(lock) ((LockTagType) (lock).tag.locktag_type) |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct PROCLOCKTAG |
| { |
| |
| LOCK *myLock; |
| PGPROC *myProc; |
| } PROCLOCKTAG; |
|
|
| typedef struct PROCLOCK |
| { |
| |
| PROCLOCKTAG tag; |
|
|
| |
| PGPROC *groupLeader; |
| LOCKMASK holdMask; |
| LOCKMASK releaseMask; |
| dlist_node lockLink; |
| dlist_node procLink; |
| } PROCLOCK; |
|
|
| #define PROCLOCK_LOCKMETHOD(proclock) \ |
| LOCK_LOCKMETHOD(*((proclock).tag.myLock)) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct LOCALLOCKTAG |
| { |
| LOCKTAG lock; |
| LOCKMODE mode; |
| } LOCALLOCKTAG; |
|
|
| typedef struct LOCALLOCKOWNER |
| { |
| |
| |
| |
| |
| |
| |
| struct ResourceOwnerData *owner; |
| int64 nLocks; |
| } LOCALLOCKOWNER; |
|
|
| typedef struct LOCALLOCK |
| { |
| |
| LOCALLOCKTAG tag; |
|
|
| |
| uint32 hashcode; |
| LOCK *lock; |
| PROCLOCK *proclock; |
| int64 nLocks; |
| int numLockOwners; |
| int maxLockOwners; |
| LOCALLOCKOWNER *lockOwners; |
| bool holdsStrongLockCount; |
| bool lockCleared; |
| } LOCALLOCK; |
|
|
| #define LOCALLOCK_LOCKMETHOD(llock) ((llock).tag.lock.locktag_lockmethodid) |
| #define LOCALLOCK_LOCKTAG(llock) ((LockTagType) (llock).tag.lock.locktag_type) |
|
|
|
|
| |
| |
| |
| |
|
|
| typedef struct LockInstanceData |
| { |
| LOCKTAG locktag; |
| LOCKMASK holdMask; |
| LOCKMODE waitLockMode; |
| BackendId backend; |
| LocalTransactionId lxid; |
| TimestampTz waitStart; |
| |
| int pid; |
| int leaderPid; |
| bool fastpath; |
| } LockInstanceData; |
|
|
| typedef struct LockData |
| { |
| int nelements; |
| LockInstanceData *locks; |
| } LockData; |
|
|
| typedef struct BlockedProcData |
| { |
| int pid; |
| |
| |
| int first_lock; |
| int num_locks; |
| |
| |
| int first_waiter; |
| int num_waiters; |
| } BlockedProcData; |
|
|
| typedef struct BlockedProcsData |
| { |
| BlockedProcData *procs; |
| LockInstanceData *locks; |
| int *waiter_pids; |
| int nprocs; |
| int maxprocs; |
| int nlocks; |
| int maxlocks; |
| int npids; |
| int maxpids; |
| } BlockedProcsData; |
|
|
|
|
| |
| typedef enum |
| { |
| LOCKACQUIRE_NOT_AVAIL, |
| LOCKACQUIRE_OK, |
| LOCKACQUIRE_ALREADY_HELD, |
| LOCKACQUIRE_ALREADY_CLEAR |
| } LockAcquireResult; |
|
|
| |
| typedef enum |
| { |
| DS_NOT_YET_CHECKED, |
| DS_NO_DEADLOCK, |
| DS_SOFT_DEADLOCK, |
| DS_HARD_DEADLOCK, |
| DS_BLOCKED_BY_AUTOVACUUM |
| |
| } DeadLockState; |
|
|
| |
| |
| |
| |
| |
| |
| #define LockHashPartition(hashcode) \ |
| ((hashcode) % NUM_LOCK_PARTITIONS) |
| #define LockHashPartitionLock(hashcode) \ |
| (&MainLWLockArray[LOCK_MANAGER_LWLOCK_OFFSET + \ |
| LockHashPartition(hashcode)].lock) |
| #define LockHashPartitionLockByIndex(i) \ |
| (&MainLWLockArray[LOCK_MANAGER_LWLOCK_OFFSET + (i)].lock) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #define LockHashPartitionLockByProc(leader_pgproc) \ |
| LockHashPartitionLock((leader_pgproc)->pgprocno) |
|
|
| |
| |
| |
| extern void InitLocks(void); |
| extern LockMethod GetLocksMethodTable(const LOCK *lock); |
| extern LockMethod GetLockTagsMethodTable(const LOCKTAG *locktag); |
| extern uint32 LockTagHashCode(const LOCKTAG *locktag); |
| extern bool DoLockModesConflict(LOCKMODE mode1, LOCKMODE mode2); |
| extern LockAcquireResult LockAcquire(const LOCKTAG *locktag, |
| LOCKMODE lockmode, |
| bool sessionLock, |
| bool dontWait); |
| extern LockAcquireResult LockAcquireExtended(const LOCKTAG *locktag, |
| LOCKMODE lockmode, |
| bool sessionLock, |
| bool dontWait, |
| bool reportMemoryError, |
| LOCALLOCK **locallockp); |
| extern void AbortStrongLockAcquire(void); |
| extern void MarkLockClear(LOCALLOCK *locallock); |
| extern bool LockRelease(const LOCKTAG *locktag, |
| LOCKMODE lockmode, bool sessionLock); |
| extern void LockReleaseAll(LOCKMETHODID lockmethodid, bool allLocks); |
| extern void LockReleaseSession(LOCKMETHODID lockmethodid); |
| extern void LockReleaseCurrentOwner(LOCALLOCK **locallocks, int nlocks); |
| extern void LockReassignCurrentOwner(LOCALLOCK **locallocks, int nlocks); |
| extern bool LockHeldByMe(const LOCKTAG *locktag, LOCKMODE lockmode); |
| #ifdef USE_ASSERT_CHECKING |
| extern HTAB *GetLockMethodLocalHash(void); |
| #endif |
| extern bool LockHasWaiters(const LOCKTAG *locktag, |
| LOCKMODE lockmode, bool sessionLock); |
| extern VirtualTransactionId *GetLockConflicts(const LOCKTAG *locktag, |
| LOCKMODE lockmode, int *countp); |
| extern void AtPrepare_Locks(void); |
| extern void PostPrepare_Locks(TransactionId xid); |
| extern bool LockCheckConflicts(LockMethod lockMethodTable, |
| LOCKMODE lockmode, |
| LOCK *lock, PROCLOCK *proclock); |
| extern void GrantLock(LOCK *lock, PROCLOCK *proclock, LOCKMODE lockmode); |
| extern void GrantAwaitedLock(void); |
| extern void RemoveFromWaitQueue(PGPROC *proc, uint32 hashcode); |
| extern Size LockShmemSize(void); |
| extern LockData *GetLockStatusData(void); |
| extern BlockedProcsData *GetBlockerStatusData(int blocked_pid); |
|
|
| extern xl_standby_lock *GetRunningTransactionLocks(int *nlocks); |
| extern const char *GetLockmodeName(LOCKMETHODID lockmethodid, LOCKMODE mode); |
|
|
| extern void lock_twophase_recover(TransactionId xid, uint16 info, |
| void *recdata, uint32 len); |
| extern void lock_twophase_postcommit(TransactionId xid, uint16 info, |
| void *recdata, uint32 len); |
| extern void lock_twophase_postabort(TransactionId xid, uint16 info, |
| void *recdata, uint32 len); |
| extern void lock_twophase_standby_recover(TransactionId xid, uint16 info, |
| void *recdata, uint32 len); |
|
|
| extern DeadLockState DeadLockCheck(PGPROC *proc); |
| extern PGPROC *GetBlockingAutoVacuumPgproc(void); |
| extern void DeadLockReport(void) pg_attribute_noreturn(); |
| extern void RememberSimpleDeadLock(PGPROC *proc1, |
| LOCKMODE lockmode, |
| LOCK *lock, |
| PGPROC *proc2); |
| extern void InitDeadLockChecking(void); |
|
|
| extern int LockWaiterCount(const LOCKTAG *locktag); |
|
|
| #ifdef LOCK_DEBUG |
| extern void DumpLocks(PGPROC *proc); |
| extern void DumpAllLocks(void); |
| #endif |
|
|
| |
| extern void VirtualXactLockTableInsert(VirtualTransactionId vxid); |
| extern void VirtualXactLockTableCleanup(void); |
| extern bool VirtualXactLock(VirtualTransactionId vxid, bool wait); |
|
|
| #endif |
|
|