Docsity

Database Systems: ACID Transactions, Concurrency Control, and Recovery, Slides of Introduction to Database Management Systems

Toggle sidebar
bg1
1
Transaction Processing
CPS 116
Introduction to Database Systems
2
Announcements
Homework #4 due next Thursday (Dec. 2)
Last call for student presentation on Dec. 2 (on
databases for small devices)
Allows your lowest homework grade to be dropped
Need one more volunteer (talk to me right after class)
Course project demo period coming up (Dec. 3-6)
Sign-up will start in a week
Early demos are encouraged
3
Review
ACID
Atomicity: TX’s are either completely done or not done at all
Consistency: TX’s should leave the database in a consisten t state
Isolation: TX’s must behave as if they are executed in isolation
Durability: Effects of committed TX’s are resilient against failures
SQL transactions
-- Begins implicitly
SELECT …;
UPDATE …;
ROLLBACK | COMMIT;
4
Concurrency control
Goal: ensure the “I” (isolation) in ACID
A B C
T1:
read(A);
write(A);
read(B);
write(B);
commit;
T2:
read(A);
write(A);
read(C);
write(C);
commit;
5
Good versus bad schedules
T1T2
r(A)
w(A)
r(B)
w(B)
r(A)
w(A)
r(C)
w(C)
T1T2
r(A)
w(A)
r(A)
w(A)
r(B)
r(C)
w(B)
w(C)
T1T2
r(A)
r(A)
w(A)
w(A)
r(B)
r(C)
w(B)
w(C)
Good! Good! (But why?)Bad!
Read 400
Read 400
Write
400 – 100 Write
400 – 50
6
Serial schedule
Execute transactions in order, with no interleaving
of operations
T1.r(A), T1.w(A), T1.r(B), T1.w(B), T2.r(A), T2.w(A),
T2.r(C), T2.w(C)
T2.r(A), T2.w(A), T2.r(C), T2.w(C), T1.r(A), T1.w(A),
T1.r(B), T1.w(B)
)Isolation achieved by definition!
Problem: no concurrency at all
Question: how to reorder operations to allow more
concurrency
pf3
pf4

Partial preview of the text

Download Database Systems: ACID Transactions, Concurrency Control, and Recovery and more Slides Introduction to Database Management Systems in PDF only on Docsity!

Transaction Processing

CPS 116

Introduction to Database Systems

Announcements

™ Homework #4 due next Thursday (Dec. 2)

™ Last call for student presentation on Dec. 2 (on

databases for small devices)

ƒ Allows your lowest homework grade to be dropped ƒ Need one more volunteer (talk to me right after class)

™ Course project demo period coming up (Dec. 3-6)

ƒ Sign-up will start in a week ƒ Early demos are encouraged

3

Review

™ ACID

ƒ Atomicity: TX’s are either completely done or not done at all ƒ Consistency: TX’s should leave the database in a consistent state ƒ Isolation: TX’s must behave as if they are executed in isolation ƒ Durability: Effects of committed TX’s are resilient against failures

™ SQL transactions -- Begins implicitly SELECT …; UPDATE …; ROLLBACK | COMMIT;

4

Concurrency control

™ Goal: ensure the “I” (isolation) in ACID

A B C

T 1 : read( A ); write( A ); read( B ); write( B ); commit;

T 2 : read( A ); write( A ); read( C ); write( C ); commit;

5

Good versus bad schedules

T 1 T 2

r( A ) w( A ) r( B ) w( B ) r( A ) w( A ) r( C ) w( C )

T 1 T 2

r( A ) w( A ) r( A ) w( A ) r( B ) r( C ) w( B ) w( C )

T 1 T 2

r( A ) r( A ) w( A ) w( A ) r( B ) r( C ) w( B ) w( C )

Good! (^) Bad! Good! (But why?)

Read 400 Read 400 Write 400 – 100 (^) Write 400 – 50

6

Serial schedule

™ Execute transactions in order, with no interleaving

of operations

ƒ T 1 .r( A ), T 1 .w( A ), T 1 .r( B ), T 1 .w( B ), T 2 .r( A ), T 2 .w( A ), T 2 .r( C ), T 2 .w( C ) ƒ T 2 .r( A ), T 2 .w( A ), T 2 .r( C ), T 2 .w( C ), T 1 .r( A ), T 1 .w( A ), T 1 .r( B ), T 1 .w( B ) )Isolation achieved by definition!

™ Problem: no concurrency at all

™ Question: how to reorder operations to allow more

concurrency

Conflicting operations

™ Two operations on the same data item conflict if at

least one of the operations is a write

ƒ r( X ) and w( X ) conflict ƒ w( X ) and r( X ) conflict ƒ w( X ) and w( X ) conflict ƒ r( X ) and r( X ) do not ƒ r/w( X ) and r/w( Y ) do not

™ Order of conflicting operations matters

ƒ E.g., if T 1 .r( A ) precedes T 2 .w( A ), then conceptually, T 1 should precede T 2

Precedence graph

™ A node for each transaction

™ A directed edge from Ti to Tj if an operation of Ti

precedes and conflicts with an operation of Tj in the

schedule

T 1 T 2 r( A ) w( A ) r( A ) w( A ) r( B ) r( C ) w( B ) w( C )

T 1 T 2 r( A ) r( A ) w( A ) w( A ) r( B ) r( C ) w( B ) w( C )

T 1

T 2

Good: no cycle

T 1

T 2

Bad: cycle

9

Conflict-serializable schedule

™ A schedule is conflict-serializable iff its precedence

graph has no cycles

™ A conflict-serializable schedule is equivalent to some

serial schedule (and therefore is “good”)

ƒ In that serial schedule, transactions are executed in the topological order of the precedence graph ƒ You can get to that serial schedule by repeatedly swapping adjacent, non-conflicting operations from different transactions

10

Locking

™ Rules

ƒ If a transaction wants to read an object, it must first request a shared lock (S mode) on that object ƒ If a transaction wants to modify an object, it must first request an exclusive lock (X mode) on that object ƒ Allow one exclusive lock, or multiple shared locks

Mode of lock(s) currently held by other transactions

Mode of the lock requested

Grant the lock?

S X S Yes No X No No Compatibility matrix

11

Basic locking is not enough

T 1 T 2

r( A ) w( A )

r( A ) w( A )

r( B ) w( B )

r( B ) w( B )

lock-X( A )

lock-X( B )

unlock( B )

unlock( A ) lock-X( A )

unlock( A )

unlock( B ) lock-X( B )

Possible schedule under locking But still not conflict-serializable!

T 1

T 2

Read 100 Write 100+

Read 101 Write 101*

Read 100 Write 100*

Read 200 Write 200+

Add 1 to both A and B (preserve A = B )

Multiply both A and B by 2 (preserves A = B )

A ≠ B!

12

Two-phase locking (2PL)

™ All lock requests precede all unlock requests

ƒ Phase 1: obtain locks, phase 2: release locks T 1 T 2 r( A ) w( A )

r( A ) w( A ) r( B ) w( B ) r( B ) w( B )

lock-X( A )

lock-X( B )

unlock( B )

unlock( A ) (^) lock-X( A )

lock-X( B )

Cannot obtain the lock on B until T 1 unlocks

T 1 T 2 r( A ) w( A ) r( A ) w( A ) r( B ) w( B ) r( B ) w( B )

2PL guarantees a conflict-serializable schedule

Undo/redo logging rules

™ Record values before and after each modification: h Ti , X , old_value_of_X , new_value_of_X i

™ A transaction T (^) i is committed when its commit log record h Ti , commit i is written to disk

™ Write-ahead logging (WAL): Before X is modified on disk, the log record pertaining to X must be flushed ƒ Without WAL, system might crash after X is modified on disk but before its log record is written to disk—no way to undo ™ No force: A transaction can commit even if its modified memory blocks have not be written to disk (since redo information is logged) ™ Steal: Modified memory blocks can be flushed to disk anytime (since undo information is logged)

Undo/redo logging example

read( A , a ); a = a – 100; write( A , a ); read( B , b ); b = b + 100; write( B , b );

A = 800 B = 400

700 500

< T 1 , start> < T 1 , A , 800, 700 > < T 1 , B , 400, 500 > < T 1 , commit>

T 1 (balance transfer of $100 from A to B )

Memory

A = 800 B = 400

Disk Log

Steal: can flush^700 before commit

commit;

500 No force: can flush after commit No restriction on when memory blocks can/should be flushed

21

Checkpointing

™ Naïve approach: ƒ Stop accepting new transactions (lame!) ƒ Finish all active transactions ƒ Take a database dump ƒ Now safe to truncate the log

™ Fuzzy checkpointing ƒ Determine S , the set of currently active transactions, and log h begin-checkpoint S i ƒ Flush all modified memory blocks at your leisure ƒ Log h end-checkpoint begin-checkpoint_location i ƒ Between begin and end, continue processing old and new transactions

22

Recovery: analysis and redo phase

™ Need to determine U , the set of active transactions at time of crash ™ Scan log backward to find the last end-checkpoint record and follow the pointer to find the corresponding h start-checkpoint S i ™ Initially, let U be S ™ Scan forward from that start-checkpoint to end of the log ƒ For a log record h T , start i, add T to U ƒ For a log record h T , commit | abort i, remove T from U ƒ For a log record h T , X , old , new i, issue write( X , new ) )Basically repeats history!

23

Recovery: undo phase

™ Scan log backward

ƒ Undo the effects of transactions in U ƒ That is, for each log record h T , X , old , new i where T is in U , issue write( X , old ), and log this operation too (part of the repeating-history paradigm) ƒ Log h T , abort i when all effects of T have been undone

) An optimization

ƒ Each log record stores a pointer to the previous log record for the same transaction; follow the pointer chain during undo

24

Summary

™ Concurrency control

ƒ Serial schedule: no interleaving ƒ Conflict-serializable schedule: no cycles in the precedence graph; equivalent to a serial schedule ƒ 2PL: guarantees a conflict-serializable schedule ƒ Strict 2PL: also guarantees recoverability

™ Recovery: undo/redo logging with fuzzy

checkpointing

ƒ Normal operation: write-ahead logging, no force, steal ƒ Recovery: first redo (forward), and then undo (backword)