3. Log-Based Recovery (Cont..)
DeferredDatabaseModification
•The deferred database modification
scheme records all modifications to the log,
but defers all the writes to after partial
commit.
•Assume that transactions execute serially
•Transaction starts by writing <T
istart>
record to log.
3. Log-Based Recovery (Cont..)
Immediate DatabaseModificationCont…
•Output of updated blocks can take place
at any time before or after transaction
commit
•Order in which blocks are output can be
different from the order in which they
are written.
3. Log-Based Recovery (Cont..)
Immediate DatabaseModificationCont…
Log Write Output
<T
0start>
<T
0,A, 1000, 950>
T
o,B, 2000, 2050
A= 950
B= 2050
<T
0commit>
<T
1start>
<T
1, C, 700, 600>
C= 600
B
B, B
C
<T
1commit>
B
A
•Note: B
Xdenotes block containing X.
3. Log-Based Recovery (Cont..)
Immediate DatabaseModificationCont…
•Recoveryprocedurehastwooperations
insteadofone:
–undo(T
i)restoresthevalueofalldataitems
updatedbyT
itotheiroldvalues,goingbackwards
fromthelastlogrecordforT
i
–redo(T
i)setsthevalueofalldataitemsupdated
byT
itothenewvalues,goingforwardfromthe
firstlogrecordforT
i
•Bothoperationsmustbeidempotent
–Thatis,eveniftheoperationisexecutedmultiple
timestheeffectisthesameasifitisexecuted
once
•Neededsinceoperationsmaygetre-executedduring
recovery
Checkpoints (Cont.)
•T
1can be ignored (updates already output to disk due to
checkpoint)
•T
2and T
3redone.
•T
4undone
T
c
T
f
T
1
T
2
T
3
T
4
checkpoint system failure
5.Recovery With Concurrent Transactions Cont..
•Logging is done as described earlier.
–Log records of different transactions may
be interspersed in the log.
•The checkpointing technique and
actions taken on recovery have to be
changed
–since several transactions may be active
when a checkpoint is performed.
5.Recovery With Concurrent Transactions Cont..
•Checkpointsareperformedasbefore,exceptthatthe
checkpointlogrecordisnowoftheform
< checkpoint L>
whereListhelistoftransactionsactiveatthetimeof
thecheckpoint
–Weassumenoupdatesareinprogresswhilethecheckpointis
carriedout(willrelaxthislater)
•Whenthesystemrecoversfromacrash,itfirstdoes
thefollowing:
1.Initializeundo-listandredo-listtoempty
2.Scanthelogbackwardsfromtheend,stoppingwhenthefirst
<checkpoint L> record is found.
Foreachrecordfoundduringthebackwardscan:
iftherecordis<T
icommit>,addT
itoredo-list
iftherecordis<T
istart>,thenifT
iisnotinredo-list,addT
ito
undo-list
3.ForeveryT
iinL,ifT
iisnotinredo-list,addT
itoundo-list
5.Recovery With Concurrent Transactions Cont..
•ExampleofRecovery
•Go over the steps of the recovery algorithm
on the following log:
<T
0start>
<T
0, A, 0, 10>
<T
0commit>
<T
1start>
<T
1, B, 0, 10>
<T
2start> /* Scan in Step 4 stops
here */
<T
2, C, 0, 10>
<T
2, C, 10, 20>
<checkpoint {T
1, T
2}>
<T
3start>
<T
3,A, 10, 20>
<T
3, D, 0, 10>
<T
3commit>
6.Log Record Buffering
•Logrecordbuffering:logrecordsare
bufferedinmainmemory,insteadofofbeing
outputdirectlytostablestorage.
–Logrecordsareoutputtostablestoragewhena
blockoflogrecordsinthebufferisfull,oralog
forceoperationisexecuted.
•Logforceisperformed tocommit a
transactionbyforcingallitslogrecords
(includingthecommitrecord)tostable
storage.
•Severallogrecordscanthusbeoutputusing
asingleoutputoperation,reducingtheI/O
cost.
6.Log Record Buffering
•Therulesbelowmustbefollowediflog
recordsarebuffered:
–Logrecordsareoutputtostablestorageinthe
orderinwhichtheyarecreated.
–TransactionT
ientersthecommitstateonlywhen
the log record
<T
icommit>hasbeenoutputtostablestorage.
–Beforeablockofdatainmainmemoryisoutputto
thedatabase,alllogrecordspertainingtodatain
thatblockmusthavebeenoutputtostable
storage.
•Thisruleiscalledthewrite-aheadloggingorWALrule
–StrictlyspeakingWALonlyrequiresundoinformationtobe
output
6.Database Buffering
•Database maintains an in-memory buffer of data
blocks
–When a new block is needed, if buffer is full an existing block
needs to be removed from buffer
–If the block chosen for removal has been updated, it must be
output to disk
•As a result of the write-ahead logging rule, if a block
with uncommitted updates is output to disk, log
records with undo information for the updates are
output to the log on stable storage first.
•No updates should be in progress on a block when it is
output to disk. Can be ensured as follows.
–Before writing a data item, transaction acquires exclusive lock
on block containing the data item
–Lock can be released once the write is completed.
•Such locks held for short duration are called latches.
–Before a block is output to disk, the system acquires an
exclusive latch on the block
•Ensures no update can be in progress on the block