Chapter 2 Database Recovery Techiniques Sem II 2022
Chapter 2 Database Recovery Techiniques Sem II 2022
Database Recovery
Techniques
Database Recovery
Process of restoring database to a correct state in the event of a failure.
Example:
If the system crashes before a fund transfer transaction completes its execution, then
either one or both accounts may have incorrect value.
Thus, the database must be restored to the state before the transaction modified any of
the accounts.
Slide 17- 3
Database Recovery Techniques
4. Concurrency control enforcement:
The concurrency control method may decide to abort the transaction, to be restarted
later, because it violates serializability or because several transactions are in a state
of deadlock
5.Disk failure:
Some disk blocks may lose their data because of a read or write malfunction or
because of a disk read/write head crash. This may happen during a read or a write
operation of the transaction.
6. Physical problems and catastrophes:
This refers to an endless list of problems that includes power or air-conditioning
failure, fire, theft, sabotage, overwriting disks or tapes by mistake, and mounting of
a wrong tape by the operator.
•end_transaction: This specifies that read and write transaction operations have
ended and marks the end limit of transaction execution.
•At this point it may be necessary to check whether the changes introduced by the
transaction can be permanently applied to the database or whether the transaction
has to be aborted because it violates concurrency control or for some other reason.
•commit_transaction: This signals a successful end of the transaction so that any
changes (updates) executed by the transaction can be safely committed to the
database and will not be undone.
<Tn, commit>
•rollback (or abort): This signals that the transaction has ended unsuccessfully, so
that any changes or effects that the transaction may have applied to the database
must be undone. Database Recovery Techniques 7
The System Log
Log or Journal: The log keeps track of all transaction operations that affect the
values of database items.
This information may be needed to permit recovery from transaction failures.
The log is kept on disk, so it is not affected by any type of failure except for
disk or catastrophic failure.
In addition, the log is periodically backed up to archival storage (tape) to
guard against such catastrophic failures.
• T in the following discussion refers to a unique transaction-id that is generated
automatically by the system and is used to identify each transaction:
• Types of log record:
• [start_transaction,T]: Records that transaction T has started execution.
• [write_item,T,X,old_value,new_value]: Records that transaction T has
changed the value of database item X from old_value to new_value.
• [read_item,T,X]: Records that transaction T has read the value of database
item X.
• [commit,T]: Records that transaction T has completed successfully, and
affirms that its effect can be committed (recorded permanently) to the
database.
• [abort,T]: Records that transaction T has been aborted.
Database Recovery Techniques 8
Sample log file
Back P and Next P point to the previous and next log records of the same
transaction.
Before-image of data item (BIMG)
After-image of data item (AFIMG)
Slide 19- 15
Database Recovery Techniques
Write-Ahead Logging (WAL)
• Any change to a database object is first recorded in the log; the record in the log must
be written to stable storage before the change to the database object is written to
disk.
• When in-place update (immediate or deferred) is used then log is necessary for
recovery and it must be available to recovery manager. This is achieved by Write-
Ahead Logging (WAL) protocol.
WAL states that:
• For Undo: Before a data item’s AFIM is flushed to the database disk (overwriting
the BFIM) its BFIM must be written to the log and the log must be saved on a
stable store (log disk).
• For Redo: Before a transaction executes its commit operation, all its AFIMs must
be written to the log and the log must be saved on a stable store.
Slide 19- 18
Database Recovery Techniques
Recovery Schemes
RECOVERY TECHNIQUES BASED ON DEFERRED UPDATE (No Undo/Redo)
• The idea behind deferred update techniques is to defer or postpone any actual updates
to the database until the transaction completes its execution successfully and reaches its
commit point.
• During transaction execution, the updates are recorded only in the log and in the cache
buffers
• After the transaction reaches its commit point and the log is force-written to disk, the
updates are recorded in the database.
• We can state a typical deferred update protocol as follows:
1. A transaction cannot change the database on disk until it reaches its commit point.
2. A transaction does not reach its commit point until all its update operations are
recorded in the log and the log is force-written to disk ( write-ahead logging (WAL)
protocol)
Slide 19- 19
• The data update goes as follows:
• A set of transactions records their updates in the log.
• At commit point under WAL scheme these updates are saved on database disk.
• After reboot from a failure the log is used to redo all the transactions affected by this
failure.
• No undo is required because no AFIM is flushed to the disk before a transaction
commits.
• Because the database is never updated on disk until after the transaction commits, there
is never a need to UNDO any operations.
• Hence, this is known as the NO UNDO/ REDO recovery algorithm
• REDO is needed in case the system fails after a transaction commits but before all its
changes are recorded in the database on disk.
• In this case, the transaction operations are redone from the log entries.
Slide 19- 21
• Deferred Update in a single-user Environment
• Uses two lists of transactions:
• List of all committed transactions since the last checkpoint
• List of all active transactions since the last checkpoint (At most one active transaction would have
failed in a single user environment)
• Logs maintained in the form:
write_item(T,X,new_value)
Algorithm:
1.Redo all committed transactions since the last checkpoint(Update all the corresponding
data elements that have been logged)
2. Restart all the active transactions(Note that none of the active transactions would have
actually changed data elements due to deferred update policy)
Slide 19- 23
Deferred Update with concurrent users
•This environment requires some concurrency control mechanism to guarantee isolation
property of transactions.
• In a system recovery transactions which were recorded in the log after the last checkpoint
were redone.
• The recovery manager may scan some of the transactions recorded before the checkpoint to
get the AFIMs.
•Two tables are required for implementing this protocol:
• Active table: All active transactions are entered in this table.
• Commit table: The committed Transactions since the last checkpoint are entered in this table.
•During recovery, all transactions of the commit table are redone and all transactions of
active tables are ignored since none of their AFIMs reached the database.
•It is possible that a commit table transaction may be redone twice but this does not create
any inconsistency because of a redone is “idempotent”, that is, one redone for an AFIM is
equivalent to multiple redone for the same AFIM.
Slide 19- 24
Recovery in a multiuser Environment using Strict 2PL
Algorithm:
Make 2 lists:
• List of all committed transactions since the last checkpoint
• List of all active transactions since the last checkpoint (At most one active transaction
would have failed in a single user environment)
1.Redo the write operations of committed transactions in the same order as they
appear in the log
2. Release all the locks held by active transactions and Restart theses
transactions once again.(Note that none of the active transactions would have
actually changed data elements due to deferred update policy)
Database Recovery Techniques 26
Slide 19- 27
RECOVERY TECHNIQUES BASED ON IMMEDIATE UPDATE
(Undo/Redo Algorithm)
In these techniques, when a transaction issues an update command, the database can
be updated "immediately," without any need to wait for the transaction to reach its
commit point.
Update operation must still be recorded in the log (on disk) before it is applied to the
database-using the write-ahead logging protocol-so that we can recover in case of
failure.
The DB may be updated by some operations of a transaction before the transaction
reaches it’s commit point.
The updates are recorded in the log must contain the old values and new values.
These techniques need to undo the operations of the uncommitted transactions and
redo the operations of the committed transactions .
Slide 19- 31
Recovery in single environments
1.Use two lists of transactions :list of all committed transactions since the last checkpoint and list of all
active transactions since the last checkpoint
2.Undo the writes of all active transactions using the undo policy
3.Redo the wrote operations of all the committed transactions
Slide 19- 33
Shadow paging recovery technique
Shadow paging considers the database to be made up of a number of fixed-size disk
pages (or disk blocks)
The AFIM(new data) does not overwrite its BFIM(old data) but recorded at
another place on the disk.
Thus, at any time a data item has AFIM (new )and BFIM(old) (Shadow copy of
the data item) at two different places on the disk.
Slide 19- 37
To recover from a failure:
• The state of the database before transaction execution is available through the
shadow page table
• Free modified pages
• Discard current page table
• That state is recovered by reinstating the shadow page table to become the
current page table once mОre
• Committing a transaction
• Discard previous shadow page
• Free old page tables that it references
•To recover from a failure during transaction execution, it is sufficient to free the
modified database pages and to discard the current directory.
•Since recovery involves neither undoing nor redoing data items, this technique can
be categorized as a NO-UNDO/NO-REDO technique for recovery.
The ARIES Recovery Algorithm
• ARIES is a recovery algorithm that is designed to
work with a steal, no-force approach.
• Steal: if a frame is dirty and chosen for replacement,
the page it contains is written to disk even if the
modifying transaction is still active.
• No-force: Pages in the buffer pool that are modified by
a transaction are not forced to disk when the
transaction commits.
Slide 19- 39
The ARIES Recovery Algorithm’s main principles:
WAL (Write Ahead Logging): any change to DB element is first recorded in
log. The log record is written to stable storage before DB’s element change is
written to disk.
Repeating history during redo:
On restart following crash, retrace all actions of DBMS before
crash so system is back to the exact state it was at crash time.
Then undo (abort) transactions still active at time of crash.
Logging changes during undo:
Changes to DB while undoing a transaction are logged to ensure
such an action is not repeated in the event of repeated restarts
(from repeated failures).
Slide 19- 41
Example:
ANALYSIS:
LSN Log Record
T1, T3 active (-> undo)
10 - update: T1 writes P5 T2 committed (all its
20 --- update: T2 writes P3 actions therefore to be
30 - T2 commit written to disk)
40 --- T2 end P1, P2, P3 possibly dirty
50 - update: T3 writes P1
60 - update: T3 writes P3 REDO:
do updates 1, 2, 5, 6
X CRASH, RESTART UNDO:
undo 6, 5, 2
Execution History with a Crash
Slide 19- 44
(c) transaction abort: When a transaction is aborted, an abort type log
record containing the transaction id is appended to the log and Undo
is initiated for this transaction
(d) transaction end: when a transaction is aborted or committed, some
additional actions must be taken beyond writing the abort or commit
log record such as removing the transaction's entry in the transaction
table. After all these additional steps are completed, an end type log
record containing the transaction id is appended to the log.
Slide 19- 47
ARIES update log record
• prevLSN
• transID
• type
• pageID // page id of modified page
• Length // length of change
• Offset // offset to change
• before-image // value before change
• after-image // value after change
Slide 19- 50
Database Recovery
The ARIES Recovery Algorithm (contd.)
• The following steps are performed for recovery
• Analysis phase: Start at the begin_checkpoint record and proceed to the end_checkpoint
record. Access transaction table and dirty page table are appended to the end of the log.
Note that during this phase some other log records may be written to the log and transaction
table may be modified. The analysis phase compiles the set of redo and undo to be
performed and ends.
• Redo phase: Starts from the point in the log up to where all dirty pages have been flushed,
and move forward to the end of the log. Any change that appears in the dirty page table is
redone.
• Undo phase: Starts from the end of the log and proceeds backward while performing
appropriate undo. For each undo it writes a compensating record in the log.
• The recovery completes at the end of undo phase.
Slide 19- 51
Database Recovery
Slide 19- 52