Sunteți pe pagina 1din 29

Balochistan University of Information Technology, Engineering & Management Sciences

Introduction to
Database Systems
Mohammad Imran
Lecturer
Department of Information Technology
Balochistan University of Information Technology, Engineering & Management Sciences

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015
Balochistan University of Information Technology, Engineering & Management Sciences

Lecture 10

Database Recovery Techniques

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 2
Balochistan University of Information Technology, Engineering & Management Sciences

Need for Recovery


• Many different types of failures that can affect database
processing
• Some causes of failure
o Natural physical disasters
o Carelessness
o Disk malfunctions- result in loss of stored data
o System crashes due to hardware malfunction-result in loss of
main and cache memory
o System software errors-result in abnormal termination or
damage to the DBMS
o Applications software errors
Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 3
Balochistan University of Information Technology, Engineering & Management Sciences

Possible Effects of Failure


• Loss of main memory, including database buffers
• Loss of the disk copy of the database
• Failure to write data safely to disk

• DBMS recovery subsystem uses techniques that minimize


these effects

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 4
Balochistan University of Information Technology, Engineering & Management Sciences

Recovery Manager
• DBMS subsystem responsible for ensuring atomicity and
durability for transactions in the event of failure
o Atomicity - all of a transaction is performed or none
• Recovery manager ensures that all the effects of committed
transactions reach the database, and that the effects of any
uncommitted transactions are undone
o Durability - effects of a committed transaction are
permanent
• Effects must survive loss of main memory

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 5
Balochistan University of Information Technology, Engineering & Management Sciences

Loss of Disk Data


• Handled by doing frequent backups-making copies of the
database
• In case of disk failure, the backup can be brought up to
date using a log of transactions
• Good practice to have mirrored disks, or remote live
backup site

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 6
Balochistan University of Information Technology, Engineering & Management Sciences

Handling Failure to Write


• Modified pages are written first to the local disk and then to the
mirror or remote disk
• After both writes are complete, the output is considered to be
done
• If a data transfer error is detected, examine both copies
o If they are identical, the data is correct
o If one has an error condition, use the other copy to replace the
faulty data
o If neither has an error condition but they have different values,
replace the first copy with the second

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 7
Balochistan University of Information Technology, Engineering & Management Sciences

System Failure
• If system failure occurs
o Database buffers are lost
o Disk copy of the database survives, but it may be incorrect, due to partial
transactions
• A transaction can commit once its writes are made to the database buffers
• Updates made to buffer are not automatically written to disk, even for
committed transactions
• May be a delay between commit and actual disk writing
• If system fails during this delay, we must ensure that these updates reach
the disk copy of the database

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 8
Balochistan University of Information Technology, Engineering & Management Sciences

Storage Structure
• To understand recovery management of database, we
first need to understand the storage structure of the
system
• You already studied storage structure can be divided into
two categories:
o Volatile storage
o Non-volatile storage

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 9
Balochistan University of Information Technology, Engineering & Management Sciences

Storage Structure (Contd.)


• Volatile storage
o cannot survive system crashes
o Volatile storage devices are placed very close to the
CPU; normally they are embedded onto the chipset itself
o For example, main memory and cache memory
o They are fast but can store only a small amount of
information

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 10
Balochistan University of Information Technology, Engineering & Management Sciences

Storage Structure (Contd.)


• Non-volatile storage
o Made to survive system crashes
o Huge in data storage capacity, but slower in accessibility
o Examples: hard-disks, magnetic tapes, flash memory

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 11
Balochistan University of Information Technology, Engineering & Management Sciences

Log Based Recovery


• The most widely used structure for recording database
modification is the log
• The log is a sequence of log records, recording all the
update activities in the database
• In short Transaction log is a register, which contains
history of all transaction performed
• It is important that the logs are written prior to the actual
modification and stored on a stable storage media, which is
failsafe
Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 12
Balochistan University of Information Technology, Engineering & Management Sciences

Working of Log Based Recovery


1. When a transaction enters the system and starts execution,
it writes a log about it:
<Tn , Start>
2. When the transaction modifies an item X, it write logs as
follows:
<Tn , X , V1 , V2>
o It mean Tn has changed the value of X, from V1 to V2
3. When the transaction finishes, it writes log as:
<Tn, commit>
Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 13
Balochistan University of Information Technology, Engineering & Management Sciences

Log based Recovery Approaches


• In log based recovery, the database can be modified
using two approaches:
o Deferred database modification (Complete  Update it)
o Immediate database modification (Change  Update it)

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 14
Balochistan University of Information Technology, Engineering & Management Sciences

Deferred Database Update


• This scheme records all modifications to the log, but defers all the writes
to after partial commit. We assume that transactions execute serially
1. Transaction starts by writing <Ti start> record to log
2. A write(X) operation results in a log record <Ti, X, V> being written,
where V is the new value for X (Note: old value is not needed for
this scheme)
3. The write is not performed on X at this time, but is deferred
4. When Ti partially commits, <Ti commit> is written to the log
5. Finally, the log records are read and used to actually execute the
previously deferred writes

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 15
Balochistan University of Information Technology, Engineering & Management Sciences

Deferred Database Update


• If transaction fails before reaching commit point, it will not
have changed the database (no need undo)
• During recovery after a crash, a transaction needs to be
redone if and only if both <Ti start> and<Ti commit>
are there in the log
• Redoing a transaction Ti ( redoTi) sets the value of all data
items updated by the transaction to the new values

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 16
Balochistan University of Information Technology, Engineering & Management Sciences

Example – Deferred Updates


• Example transactions T0 and T1 (T0 executes before T1):
T0: read (A) T1 : read (C)
A: - A - 50 C:- C- 100
Write (A) write (C)
read (B)
B:- B + 50
write (B)

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 17
Balochistan University of Information Technology, Engineering & Management Sciences

Example – Deferred Updates


• In case of failure, redo(T0)
must be performed followed
by redo(T1) since <T0 commit>
and <T1 commit> are present

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 18
Balochistan University of Information Technology, Engineering & Management Sciences

Immediate Database Update


• This scheme allows database updates of an uncommitted
transaction to be made as the writes are issued
o since undoing may be needed, update logs must have
both old value and new value
• Update log record must be written before database item
is written

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 19
Balochistan University of Information Technology, Engineering & Management Sciences

Immediate Database Update


• If a transaction fail after recording some change to the
database, but before commit point, the effect of its
operations on the database must be undone
(transaction must be rollback)
• Need both undo and redo in recovery depending upon
the DBMS strategy whether it undoes the previous
operation or redoes the remaining operations of the
transaction

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 20
Balochistan University of Information Technology, Engineering & Management Sciences

Immediate Database Update


• Recovery procedure has two operations instead of one:
o undo(Ti) restores the value of all data items updated by Ti
to their old values, going backwards from the last log
record for Ti
o redo(Ti) sets the value of all data items updated by Ti to
the new values, going forward from the first log record for
Ti
• Both operations must be idempotent
o That is, even if the operation is executed multiple times
the effect is the same as if it is executed once
• Needed since operations may get re-executed during recovery

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 21
Balochistan University of Information Technology, Engineering & Management Sciences

Immediate Database Update


• When recovering after failure:
o Transaction Ti needs to be undone if the log contains the
record
<Ti start>, but does not contain the record <Ti commit>
o Transaction Ti needs to be redone if the log contains both
the record <Ti start> and the record <Ti commit>.
• Undo operations are performed first, then redo operations

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 22
Balochistan University of Information Technology, Engineering & Management Sciences

Example – Immediate Update


Log Write
<T0 start>
<T0 , A, 1000, 950>
<To, B, 2000, 2050>
A = 950
B = 2050
<T0 commit>
<T1 start>
<T1, C, 700, 600>
C = 600
<T1 commit>
Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 23
Balochistan University of Information Technology, Engineering & Management Sciences

Immediate Update Recovery


• We show the log as it appears at
three instances of time
• Recovery actions in each case
above are:
• (a) undo (T0): B is restored to 2000
and A to 1000
• (b) undo (T1) and redo (T0): C is
restored to 700, and then A and B
are set to 950 and 2050 respectively.
• (c) redo (T0) and redo (T1): A and B
are set to 950 and 2050 respectively,
Then C is set to 600

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 24
Balochistan University of Information Technology, Engineering & Management Sciences

Checkpoints
• Problems in recovery procedure:
1. searching the entire log is time-consuming
2. we might unnecessarily redo transactions which have already
output their updates to the database
• Streamline recovery procedure by periodically performing
checkpointing
1. Output all log records currently residing in main memory onto
stable storage.
2. Output all modified buffer blocks to the disk
3. Write a log record < checkpoint> onto stable storage
Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 25
Balochistan University of Information Technology, Engineering & Management Sciences

Checkpoints (Cont.)
• During recovery we need to consider only the most recent transaction Ti that
started before the checkpoint, and transactions that started after Ti
1. Scan backwards from end of log to find the most recent
<checkpoint> record
2. Continue scanning backwards till a record <Ti start> is found
3. Need only consider the part of log following above start record
Earlier part of log can be ignored during recovery
4. For all transactions (starting from Ti or later) with no <Ti commit>,
execute undo(Ti). (Done only in case of immediate modification)
5. Scanning forward in the log, for all transactions starting from Ti or
later with a <Ti commit>, execute redo(Ti).
Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 26
Balochistan University of Information Technology, Engineering & Management Sciences

Example of Checkpoints
Tc Tf
T1
T2
T3
T4

checkpoint system failure

• T1 can be ignored (updates already output to disk due to checkpoint)


• T2 and T3 redone.
• T4 undone

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 27
Balochistan University of Information Technology, Engineering & Management Sciences

One more to go…


Lecture 11: Query Optimization Concepts

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015 28
Balochistan University of Information Technology, Engineering & Management Sciences

Thank you

Introduction to Database Systems Spring 2015 Mohammad Imran July 27, 2015

S-ar putea să vă placă și