5.4 Redo and Undo

Recovery using the log is done in two ways. The transactions at the moment the server goes down fall into two kinds, and each is handled differently: transactions whose commit record is in the log, and transactions that started but have no commit record.

A committed transaction has already responded to its session with completion, so its result must be reflected in the data pages. But the server may have gone down after the changes were written only as far as the log file and before the dirty pages were written to disk. Dirty pages live in the buffer pool (memory), so their changes vanish on restart. In this case, the log is read from the point not yet finally reflected on disk, and reapplied to the data pages. This is Redo. Depending on the DBMS it is called roll forward, forward recovery, WAL replay, and so on.

A transaction whose commit record was not written is an incomplete transaction and must be canceled (rolled back). But some of this transaction's changes may already have been written to disk. This state violates atomicity. So the data pages are restored to the pre-change values recorded in the log. This is Undo. These are the values that Section 4.3 said must be stored somewhere in order to roll back.

Figure 5-4. Redo and Undo: T1, whose commit record remains, is reapplied; T2, with only a start and a change, is reversed

flowchart TB
  L["Log on disk<br/>T1: start, change, commit<br/>T2: start, change"] --> X["Restart after power loss"]
  X --> Q["For each transaction:<br/>is there a commit record in the log"]
  Q -->|"Yes: T1"| R["Redo<br/>Reapply the post-change values to the data pages"]
  Q -->|"No: T2"| U["Undo<br/>Restore the data pages to the pre-change values"]
  R --> D["Recovery complete<br/>T1 fully applied, T2 not applied at all"]
  U --> D

The top box is what remains in the log on disk at the moment of failure. T1 has start, change, and commit records, and T2 has only start and change records. The restarted server reads the log and checks, for each transaction, whether a commit record exists. T1, which has a commit record, goes down the left path (Redo), and T2, which does not, goes down the right path (Undo). When both paths finish, the data pages hold only T1's changes and none of T2's.

Take the log at the moment of failure as an example. The power went out while three transactions were running interleaved, and the log holds the following.

OrderTransactionContent
1T1Start
2T1Page 17, Hong Gildong's balance 500,000 → 400,000
3T2Start
4T2Page 88, Park Younghee's balance 700,000 → 650,000
5T1Page 42, Kim Cheolsu's balance 200,000 → 300,000
6T1Commit
7T3Start
8T3Page 17, Hong Gildong's balance 400,000 → 350,000
—(power off)

Figure 5-4a. The log at the moment of failure split by transaction: T1 is Redo, T2 and T3 are Undo

block-beta
  columns 10
  sq["seq"] q1["1"] q2["2"] q3["3"] q4["4"] q5["5"] q6["6"] q7["7"] q8["8"] pw["crash"]
  t1["T1"] a1["start"] a2["p17"] space space a5["p42"] a6["COMMIT"] space space v1["Redo"]
  space space a2v["50→40"] space space a5v["20→30"] space space space space
  t2["T2"] space space b3["start"] b4["p88"] space space space space v2["Undo"]
  space space space space b4v["70→65"] space space space space space
  t3["T3"] space space space space space space c7["start"] c8["p17"] v3["Undo"]
  space space space space space space space space c8v["40→35"] space
  class sq,q1,q2,q3,q4,q5,q6,q7,q8 muted
  class pw warn
  class t1,t2,t3 plain
  class a1,a2,a5,a6,a2v,a5v data
  class b3,b4,c7,c8,b4v,c8v hot
  class v1 data
  class v2,v3 warn

This draws the eight lines of the table above, one row per transaction. The numbers in the top row are the order in the log, and values such as 50→40 under the page cells are the pre-change and post-change values (in units of 10,000 won) recorded in that record. The crash column at the far right is where the power failed.

  • T1: It started at 1, changed at 2 and 5, and committed at 6. Its commit record is before crash, so it is a Redo target.
  • T2: It started at 3 and changed at 4, but has no commit record. It is an Undo target.
  • T3: It started at 7 and changed at 8, but has no commit record. It is an Undo target.

The restarted server looks at the records remaining before crash, checks for each transaction whether a commit record exists, and applies Redo and Undo accordingly.

TransactionCommit recordVerdictWhat to do to the data pages
T1Yes (order 6)RedoSet page 17 to 400,000 and page 42 to 300,000
T2NoUndoIf page 88 was written as 650,000, restore it to 700,000
T3NoUndoIf page 17 was written as 350,000, restore it to 400,000

Figure 5-4b. What is done to the data pages by verdict: Redo to the post-change value, Undo to the pre-change value

block-beta
  columns 4
  h1["page"] h2["on disk at crash"] h3["value after recovery"] h4["applied"]
  p1["p17"] d1["50 or 40 or 35"] r1["40"] x1["T1 redo, T3 undo"]
  p2["p42"] d2["20 or 30"] r2["30"] x2["T1 redo"]
  p3["p88"] d3["70 or 65"] r3["70"] x3["T2 undo"]
  class h1,h2,h3,h4 meta
  class p1,p2,p3 slot
  class d1,d2,d3 muted
  class r1,r2,r3 data
  class x1,x2,x3 hot

This redraws the table above by page. The middle-left column shows the values that may be on disk at the moment of failure, and the right column shows the values after recovery completes (in units of 10,000 won). Which value was on disk depends on when the dirty page was written to disk.

  • Page 42: Whether T1's change was written to disk (30) or not (20), T1's Redo makes it the post-change value, 30.
  • Page 88: Whether T2's change was written to disk (65) or not (70), T2's Undo makes it the pre-change value, 70.
  • Page 17: T1's Redo (post-change value 40) and T3's Undo (pre-change value 40) are both applied, so it becomes 40 from any starting value.

T1, which has a commit record, brings pages to the post-change values in the log, and T2 and T3, which have none, bring them to the pre-change values.

Note page 17 in particular. T1 and T3 modified the same page, one a Redo target and one an Undo target. Whether page 17 on disk held 500,000, 400,000, or 350,000 at the moment of failure depends on when the dirty page was written. In any case, the final value must be 400,000, because T1 is confirmed and T3 must be canceled. Only because the log records both the pre-change and post-change values can the page reach 400,000 from any of the three starting states.

One more point. T1's commit record is at order 6, but the user received the "transfer complete" response only after record 6 was stored on disk. If the power had failed while record 6 was only in the log buffer, the log on disk would end at record 5, and T1 would also be an Undo target. In that case the user never received a completion response, so durability is not violated. The core of durability is the ordering in which the moment the commit record is stored on disk comes before the moment the user receives the response, and how this ordering is guaranteed is the subject of Chapter 6.

This is why records 2 and 3 in the log table of Section 5.2 carry both the pre-change and post-change values. The post-change value is needed for Redo and the pre-change value for Undo. Which transaction falls on which side cannot be known until a failure occurs, so both are recorded.

The meaning of the statement in Section 4.5 that "a long transaction holds on to the log" also becomes clear here. The log of an uncommitted transaction may be needed for Undo, so it cannot be deleted. If a transaction stays open for three hours, the log from three hours ago must be kept too.

The actual order in which recovery proceeds, where in the log reading begins, and how the same change is kept from being applied twice are previewed in Section 6.5 and covered in detail in Chapter 10. For now, it is enough to remember the two directions: reapply what was committed (Redo), and reverse what was not (Undo).