5.1 Power Loss During a Page Write

As described earlier, a database modifies pages in the buffer pool and does not write the modified pages (dirty pages) to disk immediately. It collects them and flushes them to disk periodically. This is fast, because it reduces disk I/O and separates a transaction's commit from disk writes, but it is risky from the standpoint of data loss.

Figure 5-0. Buffer pool and disk page state before and after a transfer transaction

block-beta
  columns 5
  space t1["t1 · UPDATE p17"] t2["t2 · UPDATE p42"] t3["t3 · COMMIT -> OK"] t4["t4 · power off"]
  bp["buffer pool (memory)"] a1["p17: 50 -> 40"] a2["p17: 40 dirty"] a3["p17: 40 dirty"] a4["p17: lost"]
  space b1["p42: 20"] b2["p42: 20 -> 30"] b3["p42: 30 dirty"] b4["p42: lost"]
  df["data file (disk)"] c1["p17: 50"] c2["p17: 50"] c3["p17: 50"] c4["p17: 50"]
  space d1["p42: 20"] d2["p42: 20"] d3["p42: 20"] d4["p42: 20"]
  class t1,t2,t3 meta
  class t4 warn
  class bp,df plain
  class a1,b2 hot
  class a2,a3,b3 slot
  class b1 data
  class a4,b4 warn
  class c1,c2,c3,c4,d1,d2,d3,d4 muted

p17 is page 17, which holds Hong Gildong's balance, and p42 is page 42, which holds Kim Cheolsu's balance. The numbers are balances (in units of 10,000 won). Time flows from left to right. The top two rows show page state in the buffer pool, and the bottom two rows show page state on disk (the data file).

  • t1, t2: The withdrawal and deposit UPDATEs run. The changes happen only in the buffer pool, and pages 17 and 42 become dirty pages. The two pages on disk are unchanged.
  • t3: The commit response is sent to the user. Even at this point, the two pages on disk still hold the pre-transfer values (500,000 and 200,000).
  • t4: The power goes out. The buffer pool is memory, so the modifications in the dirty pages are all gone after reboot. The data file holds only the pre-transfer values, even though the commit completed.

In this case the transaction's durability is not guaranteed.

Would it work, then, to write all dirty pages to disk at commit? There are two problems.

First, the power can fail while multiple pages are being written. If it fails right after page 17 is written, the disk reflects the withdrawal but not the deposit. This is exactly the accident from the introduction to Chapter 4. The two pages are at different locations on disk, so they cannot be written at exactly the same instant.

Second, even a single page may not be written in full. A database page is 8KB, but the unit a disk device writes atomically in one operation is usually smaller: 512 bytes on traditional devices, and typically 4KB today. If the power fails while an 8KB page is being written, the page is left with new content in the first part and old content in the rest. This is called a torn page. The slot directory that points to record locations within the page may no longer match the actual records, so the page is corrupted.

Figure 5-1. Torn page: the first 4KB of 8KB is new, the last 4KB is old

block-beta
  columns 4
  h1["(1) on disk (before)"] h2["(2) to be written"] h3["(3) after power loss"] space
  a1["header + slot dir (old)"] a2["header + slot dir (new)"] a3["header + slot dir (new)"] z1["0 - 4KB"]
  b1["records (old)"] b2["records (new)"] b3["records (old)"] z2["4 - 8KB"]
  space space n3["new header + old records"] space
  class h1,h2,h3,z1,z2 plain
  class a1,b1,b3 muted
  class a2,b2,a3 hot
  class n3 warn

(1) to (3) in the figure correspond to ① to ③ in the text. ① is the page on disk before modification, and ② is the page that was modified in the buffer pool and is about to be written to disk. Each page is drawn as two halves: the first 4KB (header and slot directory) and the last 4KB (record area).

If the power fails while ② is being written and only the first 4KB has been recorded, ③ is left on disk. The header and slot directory of ③ are the new content from ②, but the record area is the old content from ①. The positions and count in the slot directory no longer match the actual record area. This kind of corruption arises because of the page structure (Section 1.2), with the header at the front and the records at the back.

Writing data pages to disk alone cannot preserve page consistency, which means it cannot guarantee the atomicity and durability of transactions either.