DBMS PPT Unit-5
DBMS PPT Unit-5
DBMS PPT Unit-5
II B.Tech II Semester
1
Transaction Management:
The ACID Properties
Transactions and Schedules
Concurrent Execution of Transactions
Lock Based Concurrency Control.
Concurrency Control:
Serializability
Recoverability
Introduction to Lock
Crash Recovery:
Introduction to ARIES
the Log
other Recovery related Structures
the Write-Ahead Log Protocol
Check pointing
Recovering from a System Crash.
2
Transaction Concept
7
Transaction State
• Active – the initial state; the transaction stays in this state while it
is executing
• Partially committed – after the final statement has been executed.
• Failed -- after the discovery that normal execution can no longer
proceed.
• Aborted – after the transaction has been rolled back and the
database restored to its state prior to the start of the transaction.
Two options after it has been aborted:
– restart the transaction
• can be done only if no internal logical error
– kill the transaction
• Committed – after successful completion.
8
Transaction State (Cont.)
9
Implementation of Atomicity and Durability
• The recovery-management component of a database system implements the
support for atomicity and durability.
• E.g. the shadow-database scheme:
– all updates are made on a shadow copy of the database
• db_pointer is made to point to the updated shadow copy after
– the transaction reaches partial commit and
– all updated pages have been flushed to disk.
10
Implementation of Atomicity and Durability (Cont.)
• db_pointer always points to the current consistent copy of
the database.
– In case transaction fails, old consistent copy pointed to by
db_pointer can be used, and the shadow copy can be
deleted.
• The shadow-database scheme:
– Assumes that only one transaction is active at a time.
– Assumes disks do not fail
– Useful for text editors, but
• extremely inefficient for large databases (why?)
– Variant called shadow paging reduces copying of
data, but is still not practical for large databases
– Does not handle concurrent transactions
11
Concurrent Executions
• Multiple transactions are allowed to run concurrently in the system.
Advantages are:
– increased processor and disk utilization, leading to better
transaction throughput
• E.g. one transaction can be using the CPU while another is
reading from or writing to the disk
– reduced average response time for transactions: short
transactions need not wait behind long ones.
• Concurrency control schemes – mechanisms to achieve isolation
– that is, to control the interaction among the concurrent
transactions in order to prevent them from destroying the
consistency of the database
• Will study in Chapter 16, after studying notion of correctness
of concurrent executions.
12
Schedules
• Schedule – a sequences of instructions that specify the
chronological order in which instructions of concurrent
transactions are executed
– a schedule for a set of transactions must consist of all
instructions of those transactions
– must preserve the order in which the instructions
appear in each individual transaction.
• A transaction that successfully completes its execution will
have a commit instructions as the last statement
– by default transaction assumed to execute commit
instruction as its last step
• A transaction that fails to successfully complete its
execution will have an abort instruction as the last
statement
13
Schedule 1
• Let T1 transfer $50 from A to B, and T2 transfer 10% of the balance from A to B.
• A serial schedule in which T1 is followed by T2 :
14
Schedule 2
15
Schedule 3
• Let T1 and T2 be the transactions defined previously. The following schedule is
not a serial schedule, but it is equivalent to Schedule 1.
17
Serializability
• Basic Assumption – Each transaction preserves database
consistency.
• Thus serial execution of a set of transactions preserves database
consistency.
• A (possibly concurrent) schedule is serializable if it is equivalent to
a serial schedule. Different forms of schedule equivalence give
rise to the notions of:
1. conflict serializability 2. view serializability
• Simplified view of transactions
– We ignore operations other than read and write instructions
– We assume that transactions may perform arbitrary
computations on data in local buffers in between reads and
writes.
– Our simplified schedules consist of only read and write
instructions.
18
Conflicting Instructions
20
Conflict Serializability (Cont.)
• Schedule 3 can be transformed into Schedule 6, a serial
schedule where T2 follows T1, by series of swaps of non-
conflicting instructions.
– Therefore Schedule 3 is conflict serializable.
Schedule 3 Schedule 6
21
Conflict Serializability (Cont.)
24
Other Notions of Serializability
• The schedule below produces same outcome as
the serial schedule < T1, T5 >, yet is not conflict
equivalent or view equivalent to it.
• If T8 should abort, T9 would have read (and possibly shown to the user)
an inconsistent database state. Hence, database must ensure that
schedules are recoverable.
26
Cascading Rollbacks
29
Levels of Consistency in SQL-92
• Serializable — default
• Repeatable read — only committed records to be read,
repeated reads of same record must return same value.
However, a transaction may not be serializable – it may find
some records inserted by a transaction but not find others.
• Read committed — only committed records can be read,
but successive reads of record may return different (but
committed) values.
• Read uncommitted — even uncommitted records may be
read.
32
Figure 15.6
33
Testing for Serializability
34
Example Schedule (Schedule A) + Precedence Graph
T1 T2 T3 T4 T5
read(X)
read(Y)
read(Z)
read(V)
read(W) T T
read(W)
read(Y) 1 2
write(Y)
write(Z)
read(U)
read(Y)
write(Y)
T T
read(Z)
write(Z) 4
3
read(U)
write(U)
T
5
35
Lock-Based Protocols
• Lock-compatibility matrix
37
Lock-Based Protocols (Cont.)
• Example of a transaction performing locking:
T2: lock-S(A);
read (A);
unlock(A);
lock-S(B);
read (B);
unlock(B);
display(A+B)
• Locking as above is not sufficient to guarantee
serializability — if A and B get updated in-between the
read of A and B, the displayed sum would be wrong.
• A locking protocol is a set of rules followed by all
transactions while requesting and releasing locks.
Locking protocols restrict the set of possible
schedules. 38
Pitfalls of Lock-Based Protocols
• Consider the partial schedule
39
Pitfalls of Lock-Based Protocols (Cont.)
41
The Two-Phase Locking Protocol (Cont.)
42
The Two-Phase Locking Protocol (Cont.)
• There can be conflict serializable schedules that
cannot be obtained if two-phase locking is used.
• However, in the absence of extra information
(e.g., ordering of access to data), two-phase
locking is needed for conflict serializability in the
following sense:
Given a transaction Ti that does not follow two-
phase locking, we can find a transaction Tj that
uses two-phase locking, and a schedule for Ti and
Tj that is not conflict serializable.
43
Lock Conversions
• Two-phase locking with lock conversions:
– First Phase:
– can acquire a lock-S on item
– can acquire a lock-X on item
– can convert a lock-S to a lock-X (upgrade)
– Second Phase:
– can release a lock-S
– can release a lock-X
– can convert a lock-X to a lock-S (downgrade)
• This protocol assures serializability. But still
relies on the programmer to insert the various
locking instructions.
44
Automatic Acquisition of Locks
• A transaction Ti issues the standard read/write
instruction, without explicit locking calls.
• The operation read(D) is processed as:
if Ti has a lock on D
then
read(D)
else begin
if necessary wait until no other
transaction has a lock-X on D
grant Ti a lock-S on D;
read(D)
end
45
Automatic Acquisition of Locks (Cont.)
• write(D) is processed as:
if Ti has a lock-X on D
then
write(D)
else begin
if necessary wait until no other trans. has any
lock on D,
if Ti has a lock-S on D
then
upgrade lock on D to lock-X
else
grant Ti a lock-X on D
write(D)
end;
• All locks are released after commit or abort
46
Implementation of Locking
• A lock manager can be implemented as a separate
process to which transactions send lock and unlock
requests
• The lock manager replies to a lock request by sending a
lock grant messages (or a message asking the
transaction to roll back, in case of a deadlock)
• The requesting transaction waits until its request is
answered
• The lock manager maintains a data-structure called a
lock table to record granted locks and pending
requests
• The lock table is usually implemented as an in-memory
hash table indexed on the name of the data item being
locked
47
Lock Table
• Black rectangles indicate granted locks,
white ones indicate waiting requests
• Lock table also records the type of lock
granted or requested
• New request is added to the end of the
queue of requests for the data item,
and granted if it is compatible with all
earlier locks
• Unlock requests result in the request
being deleted, and later requests are
checked to see if they can now be
granted
• If transaction aborts, all waiting or
Granted granted requests of the transaction are
deleted
Waiting – lock manager may keep a list of
locks held by each transaction, to
implement this efficiently
48
Graph-Based Protocols
• Graph-based protocols are an alternative to two-
phase locking
• Impose a partial ordering on the set D = {d1,
d2 ,..., dh} of all data items.
– If di dj then any transaction accessing both di and dj
must access di before accessing dj.
– Implies that the set D may now be viewed as a
directed acyclic graph, called a database graph.
• The tree-protocol is a simple kind of graph
protocol.
49
Timestamp-Based Protocols
• Each transaction is issued a timestamp when it enters
the system. If an old transaction Ti has time-stamp
TS(Ti), a new transaction Tj is assigned time-stamp TS(Tj)
such that TS(Ti) <TS(Tj).
• The protocol manages concurrent execution such that
the time-stamps determine the serializability order.
• In order to assure such behavior, the protocol maintains
for each data Q two timestamp values:
– W-timestamp(Q) is the largest time-stamp of any
transaction that executed write(Q) successfully.
– R-timestamp(Q) is the largest time-stamp of any
transaction that executed read(Q) successfully.
50
Timestamp-Based Protocols (Cont.)
52
Example Use of the Protocol
53
Recovery and Atomicity
55
Recovery Algorithms
• Recovery algorithms are techniques to ensure
database consistency and transaction
atomicity and durability despite failures
– Focus of this chapter
• Recovery algorithms have two parts
1. Actions taken during normal transaction
processing to ensure enough information exists to
recover from failures
2. Actions taken after a failure to recover the
database contents to a state that ensures
atomicity, consistency and durability
56
Log-Based Recovery
• A log is kept on stable storage.
– The log is a sequence of log records, and maintains a record of
update activities on the database.
• When transaction Ti starts, it registers itself by writing a
<Ti start>log record
• Before Ti executes write(X), a log record <Ti, X, V1, V2> is written,
where V1 is the value of X before the write, and V2 is the value to be
written to X.
– Log record notes that Ti has performed a write on data item Xj Xj
had value V1 before the write, and will have value V2 after the
write.
• When Ti finishes it last statement, the log record <Ti commit> is
written.
• We assume for now that log records are written directly to stable
storage (that is, they are not buffered)
• Two approaches using logs
– Deferred database modification
– Immediate database modification 57
Deferred Database Modification
• 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 <Ti start> record to log.
• 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
• The write is not performed on X at this time, but is
deferred.
• When Ti partially commits, <Ti commit> is written to
the log
• Finally, the log records are read and used to actually
execute the previously deferred writes.
58
Deferred Database Modification (Cont.)
• 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.
• Crashes can occur while
– the transaction is executing the original updates, or
– while recovery action is being taken
• example transactions T0 and T1 (T0 executes before T1):T : 0
read (A) T1 : read (C)
A: - A - 50 C:- C- 100
Write (A) write (C)
read (B)
B:- B + 50
write (B)
59
Deferred Database Modification (Cont.)
• Below we show the log as it appears at three instances of
time.
61
Checkpoints
• Problems in recovery procedure as discussed
earlier :
1. searching the entire log is time-consuming
2. we might unnecessarily redo transactions which have
already
3. 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.
62
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, and can be
erased whenever desired.
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).
63
Example of Checkpoints
Tc Tf
T1
T2
T3
T4
65
Recovery With Concurrent Transactions
• The checkpointing technique and actions taken on
recovery have to be changed
– since several transactions may be active when a
checkpoint is performed.
66
Recovery With Concurrent Transactions (Cont.)
69
Log Record Buffering
• Log record buffering: log records are buffered in
main memory, instead of of being output
directly to stable storage.
– Log records are output to stable storage when a
block of log records in the buffer is full, or a log force
operation is executed.
• Log force is performed to commit a transaction
by forcing all its log records (including the
commit record) to stable storage.
• Several log records can thus be output using a
single output operation, reducing the I/O cost.
70
Log Record Buffering (Cont.)
71
Advanced Recovery: Key Features
73
Advanced Recovery: Physical Redo
74
Advanced Recovery: Crash Recovery
The following actions are taken when recovering from system crash
1. (Redo phase): Scan log forward from last < checkpoint L> record till end
of log
1. Repeat history by physically redoing all updates of all transactions,
2. Create an undo-list during the scan as follows
• undo-list is set to L initially
• Whenever <Ti start> is found Ti is added to undo-list
• Whenever <Ti commit> or <Ti abort> is found, Ti is deleted from
undo-list
This brings database to state as of crash, with committed as well as
uncommitted transactions having been redone.
Now undo-list contains transactions that are incomplete, that is, have
neither committed nor been fully rolled back.
75
Advanced Recovery: Crash Recovery (Cont.)
Recovery from system crash (cont.)
2. (Undo phase): Scan log backwards, performing
undo on log records of transactions found in undo-
list.
– Log records of transactions being rolled back are
processed as described earlier, as they are found
• Single shared scan for all transactions being undone
– When <Ti start> is found for a transaction Ti in undo-
list, write a <Ti abort> log record.
– Stop scan when <Ti start> records have been found
for all Ti in undo-list
• This undoes the effects of incomplete transactions
(those with neither commit nor abort log records).
Recovery is now complete.
76
Advanced Recovery: Checkpointing
77
ARIES
• ARIES is a state of the art recovery method
– Incorporates numerous optimizations to reduce overheads
during normal processing and to speed up recovery
– The “advanced recovery algorithm” we studied earlier is
modeled after ARIES, but greatly simplified by removing
optimizations
• Unlike the advanced recovery algorithm, ARIES
1. Uses log sequence number (LSN) to identify log records
• Stores LSNs in pages to identify what updates have already
been applied to a database page
2. Physiological redo
3. Dirty page table to avoid unnecessary redos during recovery
4. Fuzzy checkpointing that only records information about dirty
pages, and does not require dirty pages to be written out at
checkpoint time
• More coming up on each of the above …
78
ARIES Optimizations
• Physiological redo
– Affected page is physically identified, action
within page can be logical
• Used to reduce logging overheads
– e.g. when a record is deleted and all other records have to
be moved to fill hole
» Physiological redo can log just the record deletion
» Physical redo would require logging of old and new
values for much of the page
• Requires page to be output to disk atomically
– Easy to achieve with hardware RAID, also supported by
some disk systems
– Incomplete page output can be detected by checksum
techniques,
» But extra actions are required for recovery
» Treated as a media failure
79
ARIES Data Structures
80
ARIES Data Structures: Page LSN
81
ARIES Data Structures: Log Record
• Each log record contains LSN of previous log record of the same
transaction
LSN TransID PrevLSN RedoInfo UndoInfo
– LSN in log record may be implicit
• Special redo-only log record called compensation log record (CLR) used
to log actions taken during recovery that never need to be undone
– Serves the role of operation-abort log records used in advanced
recovery algorithm
– Has a field UndoNextLSN to note next (earlier) record to be undone
• Records in between would have already been undone
• Required to avoid repeated undo of already undone actions
LSN TransID UndoNextLSN
RedoInfo 3'
1 2 3 4 4' 2' 1'
82
ARIES Data Structures: Checkpoint Log
83
ARIES Recovery Algorithm
ARIES recovery involves three passes
• Analysis pass: Determines
– Which transactions to undo
– Which pages were dirty (disk version not up to date) at
time of crash
– RedoLSN: LSN from which redo should start
• Redo pass:
– Repeats history, redoing all actions from RedoLSN
• RecLSN and PageLSNs are used to avoid redoing actions already
reflected on page
• Undo pass:
– Rolls back all incomplete transactions
• Transactions whose abort was complete earlier are not undone
– Key idea: no need to undo these transactions: earlier undo actions
were logged, and are redone as required
84
Text Books: (1) DBMS by Raghu Ramakrishnan
(2) DBMS by Sudarshan and Korth
85