Docsity

Concurrency Control and Locking for Isolation in Advanced DB Systems, Slides of Database Management Systems (DBMS)

Toggle sidebar
bg1
1
Transaction Processing:
Concurrency Control
CPS 216
Advanced Database Systems
2
Announcements (April 26)
Homework #4 due this Thursday (April 28)
Sample solution will be available on Thursday
Project demo period: April 28 – May 1
Remember to email me to sign up for a 30-minute slot
Final exam on Monday, May 2, 2-5pm
3 hours—no time pressure!
Open book, open notes
Comprehensive, but with emphasis on the second half of the
course and materials exercised in homework
Sample final (from last year) available
Solution will be available on Thursday
3
Transactions
Transaction: a sequence of operations with ACID properties
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;
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Concurrency Control and Locking for Isolation in Advanced DB Systems and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

Transaction Processing:

Concurrency Control

CPS 216

Advanced Database Systems

2

Announcements (April 26)

™ Homework #4 due this Thursday (April 28) ƒ Sample solution will be available on Thursday

™ Project demo period: April 28 – May 1 ƒ Remember to email me to sign up for a 30-minute slot

™ Final exam on Monday, May 2, 2-5pm ƒ 3 hours—no time pressure! ƒ Open book, open notes ƒ Comprehensive, but with emphasis on the second half of the course and materials exercised in homework

™ Sample final (from last year) available ƒ Solution will be available on Thursday

3

Transactions

™ Transaction: a sequence of operations with ACID properties ƒ 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;

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 )

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

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

Problem of 2PL

™ T 2 has read uncommitted

data written by T 1

™ If T 1 aborts, then T 2 must

abort as well

™ Cascading aborts possible if

other transactions have read

data written by T 2

™ Even worse, what if T 2 commits before T 1?

ƒ Schedule is not recoverable if the system crashes right after T 2 commits

T 1 T 2 r( A ) w( A ) r( A ) w( A ) r( B ) w( B ) r( B ) w( B ) … Abort!

14

Strict 2PL

™ Only release locks at commit/abort time

ƒ A writer will block all other readers until the writer commits or aborts

) Used in most commercial DBMS (except Oracle)

15

Deadlocks

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

T 1

T 2

Deadlock: cycle in the wait-for graph

T 1 is waiting for T 2 T 2 is waiting for T 1

lock-X( A ) lock-X( B )

lock-X( B ) lock-X( A )Deadlock!

Multiple-granularity locking protocol

™ Lock: before locking an item, T must acquire intention locks on all ancestors of the item ƒ To get S or IS, must hold IS or IX on parent

  • What if T holds S or SIX on parent? ƒ To get X or IX or SIX, must hold IX or SIX on parent ™ Unlock: release locks bottom-up ™ 2PL must also be observed

Mode of lock(s) currently held by other transactions

Mode of the lock requested

Grant the lock?

S X IS IX SIX S Yes Yes X IS Yes Yes Yes Yes IX Yes Yes SIX Yes Compatibility matrix

20

Examples

™ T 1 reads all of R

ƒ T 1 gets an S lock on R

™ T 2 scans R and updates a few rows

ƒ T 2 gets an SIX lock on R , and then occasionally get an X lock for some rows

™ T 3 uses an index to read only part of R

ƒ T 3 gets an IS lock on R , and then repeatedly gets an S lock on rows it needs to access

21

Phantom problem revisited

™ Lock everything read by a transaction → reads are

repeatable, but may see phantoms

™ Example: different average

ƒ -- T1: -- T2: SELECT AVG(GPA) FROM Student WHERE age = 10; INSERT INTO Student VALUES(789, ‘Nelson’, 10, 1.0); COMMIT; SELECT AVG(GPA) FROM Student WHERE age = 10; COMMIT;

) How do you lock something that does not exist yet?

Solutions

™ Index locking

ƒ Use the index on Student ( age ) ƒ T 2 locks the index block(s) with entries for age = 10

  • If there are no entries for age = 10, T 2 must lock the index block where such entries would be, if they existed!

™ Predicate locking

ƒ “Lock” the predicate ( age = 10) ƒ Reason with predicates to detect conflicts ƒ Expensive to implement

23

Concurrency control without locking

™ Optimistic (validation-based)

™ Timestamp-based

™ Multi-version (Oracle, PostgreSQL)

24

Optimistic concurrency control

™ Locking is pessimistic

ƒ Use blocking to avoid conflicts ƒ Overhead of locking even if contention is low

™ Optimistic concurrency control

ƒ Assume that most transactions do not conflict ƒ Let them execute as much as possible ƒ If it turns out that they conflict, abort and restart

Multi-version concurrency control

™ Maintain versions for each database object

ƒ Each write creates a new version ƒ Each read is directed to an appropriate version ƒ Conflicts are detected in a similar manner as timestamp concurrency control

™ In addition to the problems inherited from

timestamp concurrency control

ƒ Pro: Reads are never blocked ƒ Con: Multiple versions need to be maintained

) Oracle and PostgreSQL use variants of this scheme

29

Summary

™ Covered

ƒ Conflict-serializability ƒ 2PL, strict 2PL ƒ Deadlocks ƒ Multiple-granularity locking ƒ Predicate locking and tree locking ƒ Overview of other concurrency-control methods

™ Not covered

ƒ View-serializability ƒ Concurrency control for search trees (not the same as multiple-granularity locking and tree locking)