← Lessons

quiz vs the machine

Gold1450

Databases

Lock Manager Internals

How the engine grants, queues, and detects deadlocks for locks.

5 min read · core · beat Gold to climb

A Central Lock Table

Even MVCC engines lock for writes and for explicit requests. The lock manager maintains a hash table keyed by the resource being locked, such as a row, page, or table.

Modes and Compatibility

Each lock has a mode like shared or exclusive, and a compatibility matrix decides whether a new request can be granted alongside existing holders.

  • Shared and shared are compatible.
  • Shared and exclusive are not.
  • Each resource has a granted group and a wait queue of pending requests.

Granularity and Deadlocks

Coarse locks reduce overhead but cut concurrency, so engines use lock granularity from row to table, often with intention locks. When transactions wait on each other in a cycle, the manager runs deadlock detection over a waits for graph and aborts a victim to break the cycle.

Key idea

The lock manager hashes resources to entries with a granted group and wait queue, uses a compatibility matrix to grant locks, and aborts a victim when deadlock detection finds a cycle.

Check yourself

Answer to earn rating on the learn ladder.

1. What decides whether a new lock request can be granted?

2. How does the lock manager resolve a deadlock?