Lock in PostgreSQL

Source: Internet
Author: User
Tags bitmask

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 is a structure in a private process. It has a pointer to the actual lock in the shared memory.

Typedef struct locallock
{
/* Tag */
Locallocktag tag;/* unique identifier of locallock entry */

/* Data */
Lock * lock;/* associated Lock Object in shared mem */points to
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;

Actual lock in shared memory

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 and lock are all stored in the hash table for convenient search.

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 wocould-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;

The waiting for unobtained locks is implemented through the critical section of each process. When the lock is not obtained, it enters the critical section of the process and puts the process into the lock waitprocs queue, when other processes are unlocked, check waitprocs and take the waiting processes stored in it out of the critical section.

Lock type

# Define nolock 0

# Define accesssharelock 1/* select */
# Define rowsharelock 2/* select for update/for share */
# Define rowexclusivelock 3/* Insert, update, delete */
# Define updateexclusivelock 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...
* Update */
# Define accessexclusivelock 8/* alter table, drop table, vacuum
* Full, and unqualified lock table */

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.