Lecture: Lecture 13 - Buffer Manager
Related: DBMS Architecture and Query Processing · Transactions and ACID Properties · Reliability Manager Log Records Undo Redo and Checkpoints · Write-Ahead Logging Commit Precedence and Disk-Write Protocols
Why a buffer manager is needed
Disk storage is much slower than main memory. A DBMS therefore reserves a memory area called the buffer and moves data in fixed-size pages between disk and that buffer. Keeping recently used pages nearby exploits data locality: recently referenced data is likely to be referenced again.
The buffer manager is responsible for page movement and buffer space. It does not decide the logical meaning of a transaction or replace concurrency control.
State tracked for each page
| State | Meaning |
|---|---|
| File identifier and block number | The page’s disk location |
Count | Number of active transactions currently using it |
| Dirty bit | Memory contains modifications not yet written to disk |
Count = 0 means no transaction currently holds the page for use. It does not mean the page is empty or unchanged.
The five operations
1. Fix
fix(P) obtains page P for a transaction.
- If P is already in the buffer, return its memory address and increment
Count. - Otherwise choose a free slot or a victim with
Count = 0. - If the victim is dirty, write it to disk before replacing it.
- Read P into the buffer, then return its address.
The operation may avoid disk I/O when locality keeps P resident.
2. Unfix
unfix(P) tells the manager that one use of P has ended. It decrements Count. It does not itself write P to disk.
3. Set dirty
set dirty(P) records that a transaction modified P. The dirty bit becomes 1, so the memory copy must eventually be written to disk.
4. Force
force(P) synchronously writes P to disk. The requesting transaction waits until the write finishes. It always entails a disk write.
5. Flush
flush is background buffer-manager work. It writes suitable pages independently of a transaction request, often when the CPU is idle. The source describes pages with Count = 0 or pages not recently accessed as candidates.
One page trace
Assume P is already in memory with Count = 0 and dirty = 0.
| Step | Action | Count | Dirty | Meaning |
|---|---|---|---|---|
| 1 | T1 calls fix(P) | 1 | 0 | T1 is using P |
| 2 | T1 changes P and calls set dirty(P) | 1 | 1 | Memory is newer than disk |
| 3 | T1 calls unfix(P) | 0 | 1 | No active user, but changes remain |
| 4 | Manager later chooses P as victim | 0 | 1 | P is eligible because no transaction uses it |
| 5 | Manager writes P, then replaces it | — | — | Disk is made current before eviction |
This explains why unfix and set dirty are separate. A transaction can stop using a page while its updated contents still need persistence.
Steal and no-steal
These policies describe which pages may become victims.
- Steal: a page released by the active user (
Count = 0) may be written and replaced even though its transaction has not committed. If a crash occurs, an uncommitted change that reached disk must be undone. - No-steal: pages belonging to active transactions are protected from replacement. This simplifies recovery but can require more buffer space.
Force and no-force
These policies describe commit behavior.
- Force: changed active pages are synchronously written during commit. Committed changes are already on disk.
- No-force: commit can finish while changed pages remain in memory. Later flushing is more efficient, but a crash may require redo of committed changes that had not reached disk.
Steal/no-force is common because it uses memory flexibly and avoids making commit wait for every write. Its recovery system must support both undo and redo.
Common confusions
unfixreleases a use. It is not a disk write.- Dirty means modified, not locked. Locks belong to concurrency control.
Count = 0means no current user, not an empty page.forceis synchronous and requested.flushis internal background work.- Steal/no-steal concerns victim selection. Force/no-force concerns commit-time writing.
Retrieval questions with answers
Why can the manager not normally replace a page with Count = 1?
A transaction is still using it. Replacing it could invalidate the address returned by fix and disrupt the transaction.
A transaction calls unfix(P) after modifying P. Is P safe to discard immediately?
Only if the manager first preserves the dirty contents by writing them to disk or otherwise retaining them. unfix makes P eligible, while the dirty bit signals that its changes must not be lost.
Why does no-force require redo after some crashes?
A transaction may have committed while its changed pages were still only in memory. Recovery re-applies those committed changes after restart.
What extra problem does steal create?
A dirty page from an uncommitted transaction may be written before the crash. Recovery must undo that change so an incomplete transaction does not become visible.