Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 52

Transaction Management Overview

Chapter 18

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 1


Components of a DBMS
transaction Data Definition
query

Query Compiler Transaction Manager Schema Manager

Execution Engine Logging/Recovery Concurrency Control

Buffer Manager
LOCK TABLE

Storage Manager
BUFFERS BUFFER POOL

DBMS: a set of cooperating software modules


Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 2
Transactions
 Concurrent execution of user programs is essential for
good DBMS performance.
 Because disk accesses are frequent, and relatively slow, it is
important to keep the cpu humming by working on several
user programs concurrently.
 A user’s program may carry out many operations on
the data retrieved from the database, but the DBMS is
only concerned about what data is read/written
from/to the database.
 A transaction is the DBMS’s abstract view of a user
program: a sequence of reads and writes.
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 3
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 4
Concurrency in a DBMS
 Users submit transactions, and can think of each
transaction as executing by itself.
 Concurrency is achieved by the DBMS, which interleaves
actions (reads/writes of DB objects) of various transactions.
 Each transaction must leave the database in a consistent
state if the DB is consistent when the transaction begins.
 DBMS will enforce some ICs, depending on the ICs
declared in CREATE TABLE statements.
 Beyond this, the DBMS does not really understand the
semantics of the data. (e.g., it does not understand how
the interest on a bank account is computed).
 Issues: Effect of interleaving transactions, and crashes.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 5


Atomicity of Transactions
 A transaction might commit after completing all its
actions, or it could abort (or be aborted by the DBMS)
after executing some actions.
 A very important property guaranteed by the DBMS
for all transactions is that they are atomic. That is, a
user can think of a Xact as always executing all its
actions in one step, or not executing any actions at all.
 DBMS logs all actions so that it can undo the actions of
aborted transactions.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 6


Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 7
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 8
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 9
Example
 Consider two transactions (Xacts):
T1: BEGIN A=A+100, B=B-100 END
T2: BEGIN A=1.06*A, B=1.06*B END

 Intuitively, the first transaction is transferring $100


from B’s account to A’s account. The second is
crediting both accounts with a 6% interest payment.
 There is no guarantee that T1 will execute before T2 or
vice-versa, if both are submitted together. However,
the net effect must be equivalent to these two
transactions running serially in some order.
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 10
Example (Contd.)
 Consider a possible interleaving (schedule):
T1: A=A+100, B=B-100
T2: A=1.06*A, B=1.06*B

 This is OK. But what about:


T1: A=A+100, B=B-100
T2: A=1.06*A, B=1.06*B

 The DBMS’s view of the second schedule:


T1: R(A), W(A), R(B), W(B)
T2: R(A), W(A), R(B), W(B)

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 11


Scheduling Transactions
 Serial schedule: Schedule that does not interleave the
actions of different transactions.
 Equivalent schedules: For any database state, the effect
(on the set of objects in the database) of executing the
first schedule is identical to the effect of executing the
second schedule.
 Serializable schedule: A schedule that is equivalent to
some serial execution of the transactions.
(Note: If each transaction preserves consistency, every
serializable schedule preserves consistency. )
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 12
Anomalies with Interleaved Execution

 Reading Uncommitted Data (WR Conflicts,


“dirty reads”):
T1: R(A), W(A), R(B), W(B), Abort
T2: R(A), W(A), C

 Unrepeatable Reads (RW Conflicts):


T1: R(A), R(A), W(A), C
T2: R(A), W(A), C

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 13


Anomalies (Continued)

 Overwriting Uncommitted Data (WW


Conflicts):
T1: W(A), W(B), C
T2: W(A), W(B), C

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 14


Lock-Based Concurrency Control
 Strict Two-phase Locking (Strict 2PL) Protocol:
 Each Xact must obtain a S (shared) lock on object before
reading, and an X (exclusive) lock on object before writing.
 All locks held by a transaction are released when the
transaction completes
 If an Xact holds an X lock on an object, no other Xact can
get a lock (S or X) on that object.
 Strict 2PL allows only serializable schedules.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 15


Aborting a Transaction
 If a transaction Ti is aborted, all its actions have to be
undone. Not only that, if Tj reads an object last
written by Ti, Tj must be aborted as well!
 Most systems avoid such cascading aborts by releasing
a transaction’s locks only at commit time.
 If Ti writes an object, Tj can read this only after Ti commits.
 In order to undo the actions of an aborted transaction,
the DBMS maintains a log in which every write is
recorded. This mechanism is also used to recover
from system crashes: all active Xacts at the time of the
crash are aborted when the system comes back up.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 16


The Log
 The following actions are recorded in the log:
 Ti writes an object: the old value and the new value.
 Log record must go to disk before the changed page!
 Ti commits/aborts: a log record indicating this action.
 Log records are chained together by Xact id, so it’s
easy to undo a specific Xact.
 Log is often duplexed and archived on stable storage.
 All log related activities (and in fact, all CC related
activities such as lock/unlock, dealing with deadlocks
etc.) are handled transparently by the DBMS.
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 17
Recovering From a Crash
 There are 3 phases in the Aries recovery algorithm:
 Analysis: Scan the log forward (from the most recent
checkpoint) to identify all Xacts that were active, and all dirty
pages in the buffer pool at the time of the crash.
 Redo: Redoes all updates to dirty pages in the buffer pool,
as needed, to ensure that all logged updates are in fact
carried out and written to disk.
 Undo: The writes of all Xacts that were active at the crash
are undone (by restoring the before value of the update,
which is in the log record for the update), working
backwards in the log. (Some care must be taken to handle
the case of a crash occurring during the recovery process!)
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 18
Summary
 Concurrency control and recovery are among the
most important functions provided by a DBMS.
 Users need not worry about concurrency.
 System automatically inserts lock/unlock requests and
schedules actions of different Xacts in such a way as to
ensure that the resulting execution is equivalent to
executing the Xacts one after the other in some order.
 Write-ahead logging (WAL) is used to undo the
actions of aborted transactions and to restore the
system to a consistent state after a crash.
 Consistent state: Only the effects of commited Xacts seen.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 19


Concurrency Control

Chapter 19

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 20


Conflict Serializable Schedules

 Two schedules are conflict equivalent if:


 Involve the same actions of the same transactions
 Every pair of conflicting actions is ordered the
same way
 Schedule S is conflict serializable if S is
conflict equivalent to some serial schedule

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 21


Example
 A schedule that is not conflict serializable:

T1: R(A), W(A), R(B), W(B)


T2: R(A), W(A), R(B), W(B)

A
T1 T2 Dependency graph
B
 The cycle in the graph reveals the problem.
The output of T1 depends on T2, and vice-
versa.
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 22
Dependency Graph

 Dependency graph: One node per Xact; edge


from Ti to Tj if Tj reads/writes an object last
written by Ti.
 Theorem: Schedule is conflict serializable if
and only if its dependency graph is acyclic

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 23


Review: Strict 2PL

 Strict Two-phase Locking (Strict 2PL) Protocol:


 Each Xact must obtain a S (shared) lock on object
before reading, and an X (exclusive) lock on object
before writing.
 All locks held by a transaction are released when
the transaction completes
 If an Xact holds an X lock on an object, no other
Xact can get a lock (S or X) on that object.
 Strict 2PL allows only schedules whose
precedence graph is acyclic

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 24


Two-Phase Locking (2PL)

 Two-Phase Locking Protocol


 Each Xact must obtain a S (shared) lock on object
before reading, and an X (exclusive) lock on object
before writing.
 A transaction can not request additional locks
once it releases any locks.
 If an Xact holds an X lock on an object, no other
Xact can get a lock (S or X) on that object.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 25


View Serializability
 Schedules S1 and S2 are view equivalent if:
 If Ti reads initial value of A in S1, then Ti also reads
initial value of A in S2
 If Ti reads value of A written by Tj in S1, then Ti also
reads value of A written by Tj in S2
 If Ti writes final value of A in S1, then Ti also writes
final value of A in S2
T1: R(A) W(A) T1: R(A),W(A)
T2: W(A) T2: W(A)
T3: W(A) T3: W(A)

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 26


Crash Recovery
Chapter 20

If you are going to be in the logging business, one of the


things that you have to do is to learn about heavy
equipment.
Robert VanNatta,
Logging History of Columbia County
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 27
Review: The ACID properties

 A tomicity: All actions in the Xact happen, or none happen.


 C onsistency: If each Xact is consistent, and the DB starts
consistent, it ends up consistent.
 I solation: Execution of one Xact is isolated from that of
other Xacts.
 D urability: If a Xact commits, its effects persist.

 The Recovery Manager guarantees Atomicity & Durability.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 28


Motivation
 Atomicity:
 Transactions may abort (“Rollback”).
 Durability:
 What if DBMS stops running? (Causes?)

• Desired Behavior after


system restarts: crash!
T1
– T1, T2 & T3 should be T2
durable. T3
– T4 & T5 should be T4
aborted (effects not seen). T5
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 29
Assumptions

 Concurrency control is in effect.


 Strict 2PL, in particular.
 Updates are happening “in place”.
 i.e. data is overwritten on (deleted from) the disk.

 A simple scheme to guarantee Atomicity &


Durability?

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 30


Handling the Buffer Pool

 Force every write to disk?


 Poor response time. No Steal Steal
 But provides durability.
Force Trivial
 Steal buffer-pool frames
from uncommited Xacts?
 If not, poor throughput. Desired
No Force
 If so, how can we ensure
atomicity?

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 31


More on Steal and Force
 STEAL (why enforcing Atomicity is hard)
 To steal frame F: Current page in F (say P) is written to disk;
some Xact holds lock on P.
 What if the Xact with the lock on P aborts?
 Must remember the old value of P at steal time (to
support UNDOing the write to page P).
 NO FORCE (why enforcing Durability is hard)
 What if system crashes before a modified page is written to
disk?
 Write as little as possible, in a convenient place, at commit
time,to support REDOing modifications.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 32


Basic Idea: Logging

 Record REDO and UNDO information, for every


update, in a log.
 Sequential writes to log (put it on a separate disk).
 Minimal info (diff) written to log, so multiple updates fit in a
single log page.
 Log: An ordered list of REDO/UNDO actions
 Log record contains:
<XID, pageID, offset, length, old data, new data>
 and additional control info (which we’ll see soon).

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 33


Write-Ahead Logging (WAL)

 The Write-Ahead Logging Protocol:


 Must force the log record for an update before the
corresponding data page gets to disk.
 Must write all log records for a Xact before commit.
 #1 guarantees Atomicity.
 #2 guarantees Durability.

 Exactly how is logging (and recovery!) done?


 We’ll study the ARIES algorithms.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 34


DB RAM
WAL & the Log LSNs pageLSNs flushedLSN

 Each log record has a unique Log Sequence Number


(LSN). Log records
 LSNs always increasing. flushed to disk
 Each data page contains a pageLSN.
 The LSN of the most recent log record
for an update to that page.
 System keeps track of flushedLSN.
 The max LSN flushed so far.
 WAL: Before a page is written, pageLSN “Log tail”
 pageLSN flushedLSN in RAM

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 35


Log Records
Possible log record types:
LogRecord fields:  Update
prevLSN  Commit
XID
 Abort
type
pageID  End (signifies end of
update length commit or abort)
records offset  Compensation Log
only before-image
Records (CLRs)
after-image
 for UNDO actions

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 36


Other Log-Related State

 Transaction Table:
 One entry per active Xact.
 Contains XID, status (running/commited/aborted),
and lastLSN.
 Dirty Page Table:
 One entry per dirty page in buffer pool.
 Contains recLSN -- the LSN of the log record which
first caused the page to be dirty.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 37


Normal Execution of an Xact

 Series of reads & writes, followed by commit or


abort.
 We will assume that write is atomic on disk.
 In practice, additional details to deal with non-
atomic writes.
 Strict 2PL.
 STEAL, NO-FORCE buffer management, with
Write-Ahead Logging.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 38


Checkpointing
 Periodically, the DBMS creates a checkpoint, in order to
minimize the time taken to recover in the event of a system
crash. Write to log:
 begin_checkpoint record: Indicates when chkpt began.
 end_checkpoint record: Contains current Xact table and dirty page
table. This is a `fuzzy checkpoint’:
 Other Xacts continue to run; so these tables accurate only as of
the time of the begin_checkpoint record.
 No attempt to force dirty pages to disk; effectiveness of
checkpoint limited by oldest unwritten change to a dirty page.
(So it’s a good idea to periodically flush dirty pages to disk!)
 Store LSN of chkpt record in a safe place (master record).

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 39


The Big Picture: What’s Stored Where

LOG RAM
DB
LogRecords
prevLSN Xact Table
XID Data pages lastLSN
type each status
pageID with a
length pageLSN Dirty Page Table
offset recLSN
before-image master record
after-image flushedLSN

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 40


Simple Transaction Abort

 For now, consider an explicit abort of a Xact.


 No crash involved.
 We want to “play back” the log in reverse
order, UNDOing updates.
 Get lastLSN of Xact from Xact table.
 Can follow chain of log records backward via the
prevLSN field.
 Before starting UNDO, write an Abort log record.
 For recovering from crash during UNDO!

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 41


Abort, cont.

 To perform UNDO, must have a lock on data!


 No problem!
 Before restoring old value of a page, write a CLR:
 You continue logging while you UNDO!!
 CLR has one extra field: undonextLSN
 Points to the next LSN to undo (i.e. the prevLSN of the record
we’re currently undoing).
 CLRs never Undone (but they might be Redone when repeating
history: guarantees Atomicity!)
 At end of UNDO, write an “end” log record.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 42


Transaction Commit

 Write commit record to log.


 All log records up to Xact’s lastLSN are
flushed.
 Guarantees that flushedLSN  lastLSN.
 Note that log flushes are sequential, synchronous
writes to disk.
 Many log records per log page.
 Commit() returns.
 Write end record to log.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 43


Crash Recovery: Big Picture
Oldest log
rec. of Xact  Start from a checkpoint (found
active at crash
via master record).
Smallest  Three phases. Need to:
recLSN in
dirty page  Figure out which Xacts
table after committed since checkpoint,
Analysis
which failed (Analysis).
 REDO all actions.
Last chkpt  (repeat history)
 UNDO effects of failed Xacts.
CRASH
A R U
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 44
Recovery: The Analysis Phase

 Reconstruct state at checkpoint.


 via end_checkpoint record.
 Scan log forward from checkpoint.
 End record: Remove Xact from Xact table.
 Other records: Add Xact to Xact table, set
lastLSN=LSN, change Xact status on commit.
 Update record: If P not in Dirty Page Table,
 Add P to D.P.T., set its recLSN=LSN.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 45


Recovery: The REDO Phase
 We repeat History to reconstruct state at crash:
 Reapply all updates (even of aborted Xacts!), redo CLRs.
 Scan forward from log rec containing smallest recLSN in
D.P.T. For each CLR or update log rec LSN, REDO the action
unless:
 Affected page is not in the Dirty Page Table, or
 Affected page is in D.P.T., but has recLSN > LSN, or
 pageLSN (in DB) LSN.
 To REDO an action:
 Reapply logged action.
 Set pageLSN to LSN. No additional logging!

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 46


Recovery: The UNDO Phase
ToUndo={ l | l a lastLSN of a “loser” Xact}
Repeat:
 Choose largest LSN among ToUndo.
 If this LSN is a CLR and undonextLSN==NULL
 Write an End record for this Xact.
 If this LSN is a CLR, and undonextLSN != NULL
 Add undonextLSN to ToUndo
 Else this LSN is an update. Undo the update, write a CLR,
add prevLSN to ToUndo.
Until ToUndo is empty.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 47


Example of Recovery
LSN LOG

RAM 00 begin_checkpoint
05 end_checkpoint
Xact Table 10 update: T1 writes P5 prevLSNs
lastLSN 20 update T2 writes P3
status
30 T1 abort
Dirty Page Table
recLSN 40 CLR: Undo T1 LSN 10
flushedLSN 45 T1 End
50 update: T3 writes P1
ToUndo 60 update: T2 writes P5
CRASH, RESTART

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 48


Example: Crash During Restart!
LSN LOG
00,05 begin_checkpoint, end_checkpoint
RAM 10 update: T1 writes P5
20 update T2 writes P3
undonextLSN
Xact Table 30 T1 abort
lastLSN
40,45 CLR: Undo T1 LSN 10, T1 End
status
Dirty Page Table 50 update: T3 writes P1
recLSN 60 update: T2 writes P5
flushedLSN CRASH, RESTART
70 CLR: Undo T2 LSN 60
ToUndo 80,85 CLR: Undo T3 LSN 50, T3 end
CRASH, RESTART
90 CLR: Undo T2 LSN 20, T2 end
Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 49
Additional Crash Issues
 What happens if system crashes during
Analysis? During REDO?
 How do you limit the amount of work in
REDO?
 Flush asynchronously in the background.
 Watch “hot spots”!
 How do you limit the amount of work in
UNDO?
 Avoid long-running Xacts.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 50


Summary of Logging/Recovery

 Recovery Manager guarantees Atomicity &


Durability.
 Use WAL to allow STEAL/NO-FORCE w/o
sacrificing correctness.
 LSNs identify log records; linked into
backwards chains per transaction (via
prevLSN).
 pageLSN allows comparison of data page and
log records.

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 51


Summary, Cont.

 Checkpointing: A quick way to limit the


amount of log to scan on recovery.
 Recovery works in 3 phases:
 Analysis: Forward from checkpoint.
 Redo: Forward from oldest recLSN.
 Undo: Backward from end to first LSN of oldest
Xact alive at crash.
 Upon Undo, write CLRs.
 Redo “repeats history”: Simplifies the logic!

Transaction Management and Recovery, 2nd Edition. R. Ramakrishnan and J. Gehrke 52

You might also like