← Back to all stagesStage 1Debug Command:
Stage 31: Lock Manager
Concept
Implement a lock manager that controls concurrent access to database resources (rows and pages), preventing data corruption when multiple transactions access the same data.
What It Teaches
- Shared vs Exclusive locks: A shared lock (S) allows concurrent reads. An exclusive lock (X) blocks all other access. Multiple readers can hold S locks simultaneously, but X is exclusive.
- Lock granularity: Page-level locks are coarse (lock the entire page, simpler but more contention). Row-level locks are fine (lock individual rows, less contention but more overhead). The student starts with page-level locks.
- Wait queue: When a transaction requests a lock held by another transaction, it enters a wait queue and blocks until the lock is released.
- Lock table: A hash table mapping resource IDs (page numbers or row keys) to lock entries. Each lock entry tracks the lock mode and the list of holders/waiters.
- Two-Phase Locking (2PL): A transaction acquires locks as needed (growing phase) and releases them all at COMMIT/ROLLBACK (shrinking phase). This guarantees serializability.
Learning Objectives
- Implement a lock table data structure mapping resource IDs to lock entries.
- Implement
lock_acquire(tx_id, resource_id, mode)with shared/exclusive semantics. - Implement
lock_release(tx_id, resource_id)that wakes up waiting transactions. - On COMMIT/ROLLBACK, release all locks held by the transaction.
- Implement page-level locking for B-tree operations.
- (Stretch) Implement row-level locking.
Debug Command: lock status
[LOCKS] Active locks: 3
[LOCKS] Page 0: EXCLUSIVE held by tx=1
[LOCKS] Page 1: SHARED held by tx=2, tx=3
[LOCKS] Page 2: EXCLUSIVE held by tx=1, waiters=[tx=4]