BIT202 ยท 2080 ยท 5 marks
What is shadow paging? How it is used for database recovery?
What is shadow paging? How it is used for database recovery? [5]
Quick answer
Shadow paging is a recovery technique used in database systems that maintains two page tables simultaneously: Current Page Table - reflects all changes made by the current (active) transaction; Shadow Page Table - a copy of the page table taken at the start of the transaction; it remains unchanged during...
Shadow Paging
Definition
Shadow paging is a recovery technique used in database systems that maintains two page tables simultaneously:
- Current Page Table - reflects all changes made by the current (active) transaction.
- Shadow Page Table - a copy of the page table taken at the start of the transaction; it remains unchanged during the transaction and points to the stable (committed) state of the database.
Both page tables initially point to the same disk pages. As the transaction modifies pages, new copies of those pages are written to new disk locations, and only the current page table is updated to point to the new pages. The shadow page table continues to point to the original pages.
How Shadow Paging Works
Setup
At transaction start:
Shadow Page Table Current Page Table
(frozen copy) (being updated)
+---------+ +---------+
| Page 1 |----------+----------->| Page 1 |
| Page 2 |-------+ | +---------| Page 2 |
| Page 3 |----+ | | | +------| Page 3 |
+---------+ | | | | | +---------+
v v v v v
+----+----+----+ DISK PAGES
| P1 | P2 | P3 |
+----+----+----+
When the transaction modifies page 2:
Shadow Page Table Current Page Table
| Page 2 |---> [ old P2 ] | Page 2 |---> [ new P2' ]
The old page P2 is never overwritten; a fresh copy P2' is written to a free disk block and only the current page table is made to point at it.
How Shadow Paging is Used for Recovery
On successful commit:
- All modified pages are flushed to disk.
- The current page table is written to disk.
- The pointer held in the database header is switched from the shadow page table to the current page table in a single atomic write.
- The old pages that the shadow table referred to become free space.
On failure or abort (system crash, transaction error):
- The current page table and the new pages are simply discarded.
- The database header still points at the shadow page table, which describes exactly the state of the database before the transaction began.
- The database is therefore already consistent when it restarts, with no undo and no redo required.
Advantages and Disadvantages
| Advantages | Disadvantages |
|---|---|
| No log file is needed, so no undo or redo pass | Data pages are scattered on disk, which destroys locality |
| Recovery is instantaneous after a crash | Copying pages creates fragmentation and needs garbage collection of old pages |
| Commit is a single atomic pointer switch | Hard to extend to concurrent transactions, so it suits single-user systems |
Conclusion
Shadow paging achieves atomicity and durability by never overwriting a page in place. The shadow page table preserves the pre-transaction image of the database while the current page table accumulates the changes, and commit reduces to switching one pointer. Recovery is therefore trivial (either the switch happened and the transaction is committed, or it did not and the transaction never existed), which is why the technique is valued for its simplicity even though the page copying and fragmentation costs keep it out of most multi-user database systems.
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.