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