2021/4/28 Implementing Recovery
Implementing Recovery
Recovery
Logging
Undo Logging Checkpointing
Redo Logging Undo/Redo Logging Recovery in PostgreSQL
>>
COMP9315 21T1 ♢ Implementing Recovery ♢ [0/20]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tx-recovery/slides.html
1/22
2021/4/28 Implementing Recovery
∧ >>
❖ Recovery
For a DBMS to recover from a system failure, it
needs
a mechanism to record what updates were “in train” at failure time
methods for restoring the database(s) to a valid state afterwards
Assume multiple transactions are running when failure occurs
uncommitted transactions need to be rolled back (ABORT)
committed, but not yet nalised, tx’s need to be completed
A critical mechanism in achieving this is the
transaction (event) log
COMP9315 21T1 ♢ Implementing Recovery ♢ [1/20]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tx-recovery/slides.html
2/22
2021/4/28 Implementing Recovery
❖ Logging
Three “styles” of logging
undo … removes changes by any uncommitted tx’s
redo … repeats changes by any committed tx’s undo/redo … combines aspects of both
All approaches require:
a sequential le of log records
each log record describes a change to a data item
log records are written before changes to data actual changes to data are written later
Known as write-ahead logging (PostgreSQL uses WAL)
COMP9315 21T1 ♢ Implementing Recovery ♢ [2/20]
<< ∧ >>
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tx-recovery/slides.html
3/22
2021/4/28 Implementing Recovery
❖ Undo Logging
Simple form of logging which ensures atomicity. Log le consists of a sequence of small records:
successfully
Notes:
we refer to
update log entry created for each WRITE (not OUTPUT)
update log entry contains old value (new value is not recorded)
COMP9315 21T1 ♢ Implementing Recovery ♢ [3/20]
<< ∧ >>
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tx-recovery/slides.html
4/22
2021/4/28 Implementing Recovery
<< ∧ >>
❖ Undo Logging (cont)
Data must be written to disk in the following
order:
1.
2.
Note: suf cient to have
COMP9315 21T1 ♢ Implementing Recovery ♢ [4/20]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tx-recovery/slides.html
5/22
2021/4/28 Implementing Recovery
<< ∧ >>
❖ Undo Logging (cont)
For the example transaction, we would get:
t Action v B(A) B(B) D(A) D(B) Log ——————————————————– (0)BEGIN . . . 8 5
(2)v=v*2 16 8 . 8 5 (3) WRITE(A,v) 16 16 . 8 5 (4) READ(B,v) 5 16 5 8 5 (5)v=v+1 616585
(6) WRITE(B,v)
(7) FlushLog
(8) StartCommit
(9) OUTPUT(A)
(10) OUTPUT(B)
(11) EndCommit
(12) FlushLog
6 16 68 5
6 16 616 5 6 16 616 6
Note that T is not regarded as committed until (12) completes.
COMP9315 21T1 ♢ Implementing Recovery ♢ [5/20]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tx-recovery/slides.html
6/22
2021/4/28 Implementing Recovery
<< ∧ >>
❖ Undo Logging (cont)
Simpli ed view of recovery using UNDO logging:
scan backwards through log
if
if
if
Assumes we scan entire log; use checkpoints to limit scan.
COMP9315 21T1 ♢ Implementing Recovery ♢ [6/20]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tx-recovery/slides.html
7/22
2021/4/28 Implementing Recovery
<< ∧ >>
❖ Undo Logging (cont)
Algorithmic view of recovery using UNDO logging:
committedTrans = abortedTrans = startedTrans = {}
for each log record from most recent to oldest {
switch (log record) {
// don’t undo committed changes else // roll-back changes
{ WRITE(X,v); OUTPUT(X) }
}}
for each T in startedTrans {
if (T in committedTrans) ignore
else if (T in abortedTrans) ignore
else write
}
flush log
COMP9315 21T1 ♢ Implementing Recovery ♢ [7/20]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tx-recovery/slides.html
8/22
2021/4/28 Implementing Recovery
<< ∧ >>
❖ Checkpointing
Simple view of recovery implies reading entire log
le.
Since log le grows without bound, this is infeasible.
Eventually we can delete “old” section of log.
i.e. where all prior transactions have committed
This point is called a checkpoint.
all of log prior to checkpoint can be ignored for
recovery
COMP9315 21T1 ♢ Implementing Recovery ♢ [8/20]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tx-recovery/slides.html
9/22
2021/4/28 Implementing Recovery
<< ∧ >>
❖ Checkpointing (cont)
Problem: many concurrent/overlapping
transactions.
How to know that all have nished?
1. periodically, write log record
(contains references to all active transactions ⇒ active tx table)
2. continue normal processing (e.g. new tx’s can start)
3. when all of T1,..,Tk have completed,
write log record
Note: tx manager maintains chkpt and active tx
information
COMP9315 21T1 ♢ Implementing Recovery ♢ [9/20]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/tx-recovery/slides.html
10/22
2021/4/28 Implementing Recovery
<< ∧ >>
❖ Checkpointing (cont)
Recovery: scan backwards through log le
processing as before.
Determining where to stop depends on …
whether we meet
If we encounter
we know that all incomplete tx’s come after
prev
thus, can stop backward scan when we reach
Ifweencounter