Motivation • Undo logging: cannot bring backup DB copies up to date • Redo logging: need to keep all modified blocks in memory until commit • Solution: undo/redo logging Update ⇒ <Ti, X, v, w> v = old value, w = new value ICS 214A: Database Management Systems Fall 2002 Lecture 18: Redo/Undo Logging Professor Chen Li 1 ICS214A Notes 18 Example Undo/redo logging T1 : • Suppose xact T changes DB element X from v (old value) to w (new value) – Write log record <T, X, v, w> Read (A,t); t ← t×2 Write (A,t); Read (B,t); t ← t×2 Write (B,t); Output (A); Output (B); A=B • Rule: A:8 16 B:8 16 – Before modifying any db element X (v w) on disk changed by xact T, write the update log record <T,X,v,w> on disk – <T COMMIT> can precede or follow any dbelement change on disk ICS214A 2 Notes 18 A:8 16 B:8 16 <T1, start> <T1, A, 8, 16> <T1, B, 8, 16> <T1, commit> disk memory log Notice: <T1,COMMIT> log record can be before or after the two output operations 3 ICS214A Notes 18 4 1 1 Example 1 Undo/redo Recovery T1 : • Redo all the committed xacts in the order earliest-first (forward) • Undo all the incomplete xacts in the order latest-first (backward) Read (A,t); t ← t×2 Write (A,t); Read (B,t); t ← t×2 Write (B,t); Output (A); Output (B); A:8 B:8 16 16 A=B failure! A:8 16 B:8 16 <T1, start> <T1, A, 8, 16> <T1, B, 8, 16> <T1, commit> disk memory log Recovery: T1 is committed. Redo: write 16 to both A and B on disk. ICS214A Notes 18 Example 2 T1 : Read (A,t); t ← t×2 Write (A,t); Read (B,t); t ← t×2 Write (B,t); Output (A); Output (B); A:8 B:8 16 16 failure! A:8 16 B:8 disk Notes 18 ICS214A Notes 18 Example 3 A=B memory Recovery: same as before. ICS214A 5 T1 : <T1, start> <T1, A, 8, 16> <T1, B, 8, 16> <T1, commit> Read (A,t); t ← t×2 Write (A,t); Read (B,t); t ← t×2 Write (B,t); Output (A); Output (B); A:8 B:8 16 16 6 A=B failure! A:8 16 B:8 <T1, start> <T1, A, 8, 16> <T1, B, 8, 16> disk memory log Recovery: T1 incomplete. Undo: old values (“8”) of A and B are written to disk. Write <T1, abort> to the log. log 7 ICS214A Notes 18 8 2 2 Example 4 T1 : Read (A,t); t ← t×2 Write (A,t); Read (B,t); t ← t×2 Write (B,t); Output (A); Output (B); A:8 16 B:8 16 T1 : failure! <T1, start> <T1, A, 8, 16> <T1, B, 8, 16> A:8 B:8 memory Recovery: same as before. ICS214A Example 5 A=B disk 9 • Write (and flush) <START CKPT(T1,…,Tk)> log record, where T1,…,Tk are active xacts • Flush to disk all modified data elements. ICS214A Redo/Undo Log: <T1, START> <T1, A, 4, 5> <T2, START> <T1, COMMIT> <T2, B, 9, 10> – Recall that in redo logging, we only flush those data elements by committed xacts. <START CKPT(T2)> <T2, C, 14, 15> <T3, START> <T3, D, 19, 20> <END CKPT> <T2, COMMIT> <T3, COMMIT> • Write and flush <END CKPT> log record Notes 18 failure! <T1, start> A:8 B:8 Notes 18 10 Example Nonquiescent checkpoint: flexible ICS214A A:8 16 B:8 A=B disk memory log Recovery: T1 incomplete. Write <T1, abort> to the log. log Notes 18 Read (A,t); t ← t×2 Write (A,t); Read (B,t); t ← t×2 Write (B,t); Output (A); Output (B); 11 ICS214A Start checkpoint. T2 active continue, accept new xacts, make sure B=10 by T2 is on disk, and A = 5 by T1 is on disk. end checkpoint continue Notes 18 12 3 3 Recovery: example 1 Redo/Undo Log: <T1, START> <T1, A, 4,5> <T2, START> <T1, COMMIT> <T2, B, 9, 10> <START CKPT(T2)> <T2, C, 14, 15> <T3, START> <T3, D, 19, 20> • T2 and T3 already committed. • Since we see <END CKPT> first (backward), T1’s changes must be on disk. So T1 can be ignored. • Redo T2 and T3, forward. – Do NOT need to look at the log records before <START CKPT> – Reason: their changes are already on disk <T2, C, 14, 15> <T3, START> <T3, D, 19, 20> <END CKPT> ICS214A T2 committed. T3 incomplete Redo T2 (forward): C = 15 (on disk) Undo T3 (backward): D = 19 (on disk) <END CKPT> <T2, COMMIT> Notes 18 13 Recovery: example 3 <START CKPT(T2)> • • • • <T2, C, 14, 15> <T3, START> <T3, D, 19, 20> <T2, COMMIT> <T3, COMMIT> Redo/Undo Log: <T1, START> <T1, A, 4,5> <T2, START> <T1, COMMIT> <T2, B, 9, 10> Redo/Undo Log: <T1, START> <T1, A, 4,5> <T2, START> <T1, COMMIT> <T2, B, 9, 10> <START CKPT(T2)> <END CKPT> ICS214A Recovery: example 2 Notes 18 14 Recovery: example 3 • T2 and T3 incomplete • Undo T3 (backward): D = 19 (on disk) • Undo T2 (backward): C = 14 and B = 9 (on disk) • Therefore, once we write the <END CKPT> log record, we still need to keep the earliest <Ti, START> log record for those Ti’s that were active (when we started the CKPT) and incomplete during the ckpt. Redo/Undo Log: <T1, START> <T1, A, 4, 5> <T2, START> <T1, COMMIT> <T2, B, 9, 10> <START CKPT(T2)> <T2, C, 14, 15> <T3, START> <T3, D, 19, 20> – In this case: <T2, START> Notes 18 ICS214A 15 ICS214A • See <START CKPT(T2)> first (backward) • T2 active and incomplete. – Undo (backward): C= 14 and B = 9 (on disk) • T3 incomplete – Undo (backward): D = 19 (on disk) • T1 complete – Redo (forward): A = 5 (on disk) Notes 18 16 4 4 Summary • Deal with system failures • Undo logging • Redo logging • Undo/Redo • Checkpoints ICS214A Notes 18 17 5 5