postgreSQL 中的 Lock

前端之家收集整理的这篇文章主要介绍了postgreSQL 中的 Lock前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

typedef struct LOCK
{
/* hash key */
LOCKTAG tag; /* unique identifier of lockable object */@H_502_4@

/* data */
LOCKMASK grantMask; /* bitmask for lock types already granted */
LOCKMASK waitMask; /* bitmask for lock types awaited */
SHM_QUEUE procLocks; /* list of PROCLOCK objects assoc. with lock */
PROC_QUEUE waitProcs; /* list of PGPROC objects waiting on lock */
int requested[MAX_LOCKMODES]; /* counts of requested locks */
int nRequested; /* total of requested[] array */
int granted[MAX_LOCKMODES]; /* counts of granted locks */
int nGranted; /* total of granted[] array */
} LOCK;@H_502_4@

LOCALLOCK 是在私有进程中的结构,它有指针指向共享内存中实际的锁@H_502_4@

typedef struct LOCALLOCK
{
/* tag */
LOCALLOCKTAG tag; /* unique identifier of locallock entry */@H_502_4@

/* data */
LOCK *lock; /* associated LOCK object in shared mem */ 指向共享内存中的
PROCLOCK *proclock; /* associated PROCLOCK object in shmem */
uint32 hashcode; /* copy of LOCKTAG's hash value */
int64 nLocks; /* total number of times lock is held */
int numLockOwners; /* # of relevant ResourceOwners */
int maxLockOwners; /* allocated size of array */
LOCALLOCKOWNER *lockOwners; /* dynamically resizable array */
} LOCALLOCK;@H_502_4@

共享内存中实际的锁@H_502_4@

typedef struct LOCK
{
/* hash key */
LOCKTAG tag; /* unique identifier of lockable object */@H_502_4@

/* data */
LOCKMASK grantMask; /* bitmask for lock types already granted */
LOCKMASK waitMask; /* bitmask for lock types awaited */
SHM_QUEUE procLocks; /* list of PROCLOCK objects assoc. with lock */
PROC_QUEUE waitProcs; /* list of PGPROC objects waiting on lock */
int requested[MAX_LOCKMODES]; /* counts of requested locks */
int nRequested; /* total of requested[] array */
int granted[MAX_LOCKMODES]; /* counts of granted locks */
int nGranted; /* total of granted[] array */
} LOCK;@H_502_4@

LOCALLOCK 和 LOCK 为了方便查找,全部放在了 hash table 中@H_502_4@

We may have several different backends holding or awaiting locks on the same lockable object. We need to store some per-holder/waiter information for each such holder (or would-be holder). This is kept in a PROCLOCK struct@H_502_4@

typedef struct PROCLOCK
{
/* tag */
PROCLOCKTAG tag; /* unique identifier of proclock object */@H_502_4@

/* data */
LOCKMASK holdMask; /* bitmask for lock types currently held */
LOCKMASK releaseMask; /* bitmask for lock types to be released */
SHM_QUEUE lockLink; /* list link in LOCK's list of proclocks */
SHM_QUEUE procLink; /* list link in PGPROC's list of proclocks */
} PROCLOCK;@H_502_4@

未获得锁的等待是通过每个进程的临界区实现的,当没获得锁时,就进入本进程的临界区,并把本进程放入LOCK的waitProcs队列中,当别的进程解锁后,检查waitProcs,把里面存储的等候的进程出临界区@H_502_4@

锁的种类@H_502_4@

#define NoLock 0@H_502_4@

#define AccessShareLock 1 /* SELECT */#define RowShareLock 2 /* SELECT FOR UPDATE/FOR SHARE */#define RowExclusiveLock 3 /* INSERT,UPDATE,DELETE */#define ShareUpdateExclusiveLock 4 /* VACUUM (non-FULL),ANALYZE,CREATE * INDEX CONCURRENTLY */#define ShareLock 5 /* CREATE INDEX (WITHOUT CONCURRENTLY) */#define ShareRowExclusiveLock 6 /* like EXCLUSIVE MODE,but allows ROW * SHARE */#define ExclusiveLock 7 /* blocks ROW SHARE/SELECT...FOR * UPDATE */#define AccessExclusiveLock 8 /* ALTER TABLE,DROP TABLE,VACUUM * FULL,and unqualified LOCK TABLE */@H_502_4@ 原文链接:https://www.f2er.com/postgresql/197155.html

猜你在找的Postgre SQL相关文章