Multiple granularity locking


In computer science, multiple granularity locking is a locking method used in database management systems and relational databases.
In multiple granularity locking, locks are set on objects that contain other objects. MGL exploits the hierarchical nature of the contains relationship. For example, a database may have files, which contain pages, which contain records. This can be thought of as a tree of objects, where each node contains its children. A lock on this structure locks the targeted node as well as all of its descendants.
Multiple granularity locking is usually used with non-strict two-phase locking to guarantee serializability.

Lock modes

In addition to shared locks and exclusive locks from other locking schemes, like strict two-phase locking, MGL also uses "intention shared" and "intention exclusive" locks. IS locks conflict with X locks, while IX locks conflict with S and X locks. The null lock is compatible with everything.
To lock a node in S, MGL has the transaction lock on all of its ancestors with IS, so if a transaction locks a node in S, no other transaction can access its ancestors in X. This protocol is shown in the following table:
To GetMust have on all ancestors
IS or SIS or IX
IX, SIX or XIX or SIX

Determining what level of granularity to use for locking is done by locking the finest level possible, and then escalating these locks to higher levels in the file hierarchy to cover more records or file elements as needed in a process known as "lock escalation". MGL locking modes are compatible with each other as defined in the following matrix.
ModeNLISIXSSIXX
NL
IS
IX
S
SIX
X

Following the locking protocol and the compatibility matrix, if one transaction holds a node in S mode, no other transactions can have locked any ancestor in X mode.