Sub topic 14
Multiple Granularity Locking and Intention Locks
Assume you know shared and exclusive locks, two-phase locking, and basic transaction schedules. This chapter explains how a database locks objects arranged in a hierarchy, such as a database, table, page, and row.
Why lock at multiple granularities?
A transaction may need to read one row or update every row in a table. Locking every row gives fine control, but it creates many lock entries. Locking the whole table uses fewer entries, but it blocks more transactions.
Multiple granularity locking allows the database to choose an appropriate level:
- A row lock permits high concurrency.
- A table lock reduces lock-management overhead.
- A database lock protects a large operation.
The hierarchy can be represented like this:
A lock on a higher-level object covers all descendants. For example, an exclusive lock on a table conflicts with access to every row in that table.
Intention locks
An intention lock records a transaction's plan to lock descendants. It prevents another transaction from taking a conflicting lock on an ancestor.
The main lock modes are:
| Mode | Meaning | Typical use |
|---|---|---|
| IS | Intention shared | The transaction will take shared locks below |
| IX | Intention exclusive | The transaction will take exclusive locks below |
| SIX | Shared and intention exclusive | Read the whole object and update some descendants |
| S | Shared | Read the object and its descendants |
| X | Exclusive | Modify the object and its descendants |
Suppose transaction T1 wants an exclusive lock on row R. It must first acquire an IX lock on the database, table, and page. It then acquires X on R. The IX locks tell other transactions that a descendant will be modified.
A transaction reading an entire table requests S on the table. S conflicts with IX, so it cannot incorrectly overlap with T1's row update if the table-level reader requires a consistent table-wide view.
Compatibility
The compatibility matrix shows whether two modes can coexist on the same object. Yes means compatible; No means one transaction must wait.
| Requested \ Existing | IS | IX | S | SIX | X |
|---|---|---|---|---|---|
| IS | Yes | Yes | Yes | Yes | No |
| IX | Yes | Yes | No | No | No |
| S | Yes | No | Yes | No | No |
| SIX | Yes | No | No | No | No |
| X | No | No | No | No | No |
Intention locks are compatible with some other intention locks because separate transactions may safely lock different descendants. They do not guarantee that descendants are conflict-free. The database still checks locks at the lower level.
Protocol rules
A standard multiple-granularity protocol follows these rules:
- Lock the root before locking any descendant.
- Acquire locks from higher levels to lower levels.
- Acquire IS or IX on every ancestor before acquiring S or X below it.
- Release locks from lower levels to higher levels.
- Under two-phase locking, do not acquire new locks after releasing the first lock.
For example, to update one row, a transaction might acquire IX(Database), IX(Table), IX(Page), and X(Row). To read one row, it might acquire IS locks on the ancestors and S(Row).
Common errors
Do not place an X lock directly on a row without intention locks on its ancestors. Do not treat IX as equivalent to X: IX permits descendant-level concurrency, while X excludes all conflicting access to the object. Also remember that SIX combines S on the current object with IX below it; it is useful when a transaction reads a whole table but updates selected rows.
Exercise
T1 holds IX on a table. Can T2 acquire S on that same table? Explain.
Show answer
No. IX and S are incompatible. T2 must wait, because S would claim a table-wide read while T1 may modify a descendant.