postgreSQL 中的 Lock

來源:互聯網
上載者:User

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

/* 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;

 

 

 

LOCALLOCK 是在私人進程中的結構,它有指標指向共用記憶體中實際的鎖

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

/* 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;

共用記憶體中實際的鎖

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

/* 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;

 

LOCALLOCK 和 LOCK 為了方便尋找,全部放在了 hash table 中

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

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

/* 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;

未獲得鎖的等待是通過每個進程的臨界區實現的,當沒獲得鎖時,就進入本進程的臨界區,並把本進程放入LOCK的waitProcs隊列中,當別的進程解鎖後,檢查waitProcs,把裡面儲存的等候的進程出臨界區

鎖的種類

#define NoLock      0

#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 */

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.