CC Part 2 No Quizzes
CC Part 2 No Quizzes
Management
CMPSCI 445
1
Txn Semantics Summary
Transactions are how programmers make
sense of concurrent behavior
ACID properties are a contract between
DB and programmer
A tool to build understandable code
Interleaving execution is vital for
performance
DB has to run operations concurrently, but
Simulate that only a single txn was run at
a time
This is Concurrency Control 2
Anomalies with Interleaved Execution
3
Reading Uncommitted Data
(WR Conflict)
T1: Transfer T2: Interest
“Dirty Read” R(A)
W(A)
R(A)
Inconsistent result of A is W(A)
exposed to transaction T2
R(B)
(We saw this schedule in
the banking example) W(B)
Commit
Transaction is no longer
ISOLATED from other R(B)
transactions’ effects
W(B)
Commit 4
Acceptable Dirty Read
5
Unrepeatable Reads
6
Overwriting Uncommitted Data
(WW Conflicts)
T1 T2
W(A) Here T2 overwrites value of B that
has been modified by T1.
W(B)
Example: Harry and Larry are 2
W(B) employees. Salaries must be
kept equal.
Commit
W(A) T1: set salaries to 1000
T2 set salaries to 2000
Commit
7
Overwriting Uncommitted Data
(WW Conflicts)
Here T2 overwrites value of B that
T1 T2 has been modified by T1.
W(A) Example: Harry and Larry are 2
W(B) employees. Salaries must be
kept equal.
W(B)
T1: set salaries to 1000
Commit T2 set salaries to 2000
T1; T2 => salaries = 2000
W(A) T2; T1 => salaries = 1000
Commit This interleaving, Harry = 1000,
Larry = 2000 (lucky larry)
8
Lock-Based Concurrency Control
Just like in procedural programming
locks can be used to maintain orders of
access to shared objects
Lock - associated with some object
shared (r) or exclusive (w)
Locking protocol - set of rules to be followed
by each transaction to ensure good
properties.
9
Lock Compatibility Matrix
11
Example Txn with locking
T1
lock-X(A)
R(A)
W(A)
unlock(A)
lock-S(B)
R(B)
unlock(B)
12
Goals of a locking protocol
Ensure consistency/isolation
Serializability for this lecture
Enable interleaving of txn operations
13
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.
• This implies two phases:
• growing phase
• shrinking phase
Example of 2PL
T1 T2
X(A)
R(A)
W(A)
X(B)
U(A)
X(A)
R(A)
W(A)
R(B)
W(B)
U(B)
X(B)
R(B)
W(B)
U(A); U(B)
Commit
Commit
15
Example of 2PL
T1 T2
X(A)
growing phase R(A)
W(A)
X(B)
U(A)
X(A)
R(A)
shrinking phase W(A) growing phase
R(B)
W(B)
U(B)
X(B)
R(B)
W(B)
U(A); U(B) shrinking phase
Commit
Commit
16
Not admissible under 2PL
T1 T2
R(A)
W(A)
R(A)
W(A)
R(B)
W(B)
Commit
R(B)
W(B)
Commit
17
Not admissible under 2PL
Why?
T1 T2
R(A)
W(A)
R(A)
W(A)
R(B)
W(B)
Commit
R(B)
W(B)
Commit
18
Not admissible under 2PL
Why?
T1 T2
X(A)
X(A)
X(B)
U(B)
U(A)
Commit
X(B)
U(B)
U(A)
Commit
19
Not admissible under 2PL
Why?
T1 T2
X(A)
T1 T2
X(A) granted
X(B) granted
X(B) queued
X(A) queued
27
Deadlocks
Tend to be rare in practice.*
Two ways of dealing with deadlocks:
Deadlock prevention
lock protocol makes deadlock impossible
transactions may have to ‘restart’
Deadlock detection
a separate process examines txn state to detect
deadlock
and then transactions are aborted to clear the
deadlock
*tend to start cropping up when the load on the system increases greatly.
More txns => More concurrency => more opportunities for deadlock
Deadlock Detection
T1 T2
T4 T3
Deadlock Detection (Continued)
T1 T2 T3 T4
S(A)
R(A)
X(B)
W(B)
B is held by T2 S(B)
S(C)
R(C)
X(C)
X(B)
X(A)
T1 T2
T4 T3
Deadlock Detection (Continued)
T1 T2 T3 T4
S(A)
R(A)
X(B)
W(B)
S(B)
S(C)
R(C)
C is held by T3 X(C)
X(B)
X(A)
T1 T2
T4 T3
Deadlock Detection (Continued)
T1 T2 T3 T4
S(A)
R(A)
X(B)
W(B)
S(B)
S(C)
R(C)
X(C)
X(B) B is held by T2
X(A)
T1 T2
T4 T3
Deadlock Detection (Continued)
T1 T2 T3 T4
S(A)
R(A)
X(B)
W(B)
S(B)
S(C)
R(C)
X(C)
X(B)
X(A) A is held by T1
T1 T2
A waits-for cycle means
deadlock
T4 T3
Deadlock Prevention
Idea 1: Assign priorities based on
timestamps. If Ti wants a lock that Tj holds.
Two policies are possible:
Wait-Die: If Ti has higher priority, Ti waits for Tj;
otherwise Ti aborts
aborts most recent txn until deadlock is broken
Wound-wait: If Ti has higher priority, Tj aborts;
otherwise Ti waits
Aborts txn when lock conflict is detected
37
Performance Graphs
If concurrency is a win, then running more
transactions means higher throughput
Throughput DB linear
# transactions
38
Performance Graphs
If concurrency is a win, then running more
transactions means higher throughput
Throughput DB linear
# transactions
41
Thrashing
Locking causes blocking. The more popular
an object is, the more transactions are trying
to lock it (contention)
More contention means blocking is more
costly
vicious cycle of more contention causing
more slowdowns which lead to even more
contention
When adding more transactions reduces
throughput we say the system is thrashing
42
Transaction support in SQL
Explicit transactions with BEGIN/START
TRANSACTION
‘autocommit mode’ Transaction
automatically started for SELECT, UPDATE,
CREATE
Transaction ends with COMMIT or
ROLLBACK (abort)
SQL 99 supports SAVEPOINTs which are
simple nested transactions
ABORT can roll back to an intermediate
point rather than just to the beginning
43
What should we lock?
T1 T2
SELECT MAX(age)
FROM Sailors S
WHERE rating = 8
46
The Phantom Problem
T1 T2
SELECT MIN(age) No Results, no rows locked
FROM Sailors S
WHERE rating = 8 INSERT Sailors(Name, Rating,
Age) VALUES (“Mary”, 8, 18)
SELECT MAX(age)
COMMIT
FROM Sailors S
WHERE rating = 8
47
The Phantom Problem
T1 T2
SELECT MIN(age) No Results, no rows locked
FROM Sailors S
WHERE rating = 8 INSERT Sailors(Name, Rating,
Age) VALUES (“Mary”, 8, 18)
SELECT MAX(age)
COMMIT
FROM Sailors S
WHERE rating = 8
48
The Phantom Problem
T1 T2
SELECT MIN(age) No Results, no rows locked
FROM Sailors S
WHERE rating = 8 INSERT Sailors(Name, Rating,
Age) VALUES (“Mary”, 8, 18)
SELECT MAX(age)
COMMIT
FROM Sailors S
WHERE rating = 8
49
The Phantom Problem
T1 can only lock rows that already exist
wants to lock all potential rows with rating = 8
how do you lock a row that doesn’t exist?
Idea 1: Lock whole table
no modification can slip by a transaction
no other transaction can write the table
•or read it after eXclusive locking
really bad for performance
Idea 2: use index to lock ranges of values
Idea 3: prevent new rows that match the
selection condition
50
Data
Index
Index Locking
r=1
If there is an index on the rating field, T1
should lock the index page containing the data
entries with rating = 8.
If there are no records with rating = 8, T1 must
lock the index page where such a data entry would
be, if it existed!
If there is no suitable index:
T1 locks all pages when doing table scan
Also locks the file/table to prevent new
pages from being added
Predicate Locking
Exclusive … 20 …
…
…
35 …
23 38 44
leaves
T1
Exclusive … 20 …
…
…
35 …
23 38 44
leaves
T1
Exclusive … 20 …
…
…
T1 35 …
23 38 44
leaves
Exclusive … 20 …
…
…
T1 35 …
23 T1 38 44
leaves
Exclusive T2 … 20 …
…
…
T1 35 …
23 T1 38 44
leaves
Exclusive T2 … 20 …
…
…
35 …
23 T1 38 44
leaves
T1
Exclusive T2 … 20 …
…
…
T2 35 …
23 38 44
leaves
T1
Exclusive T2 … 20 …
…
…
T2 35 …
23 T2 38 44
leaves
T1
Exclusive T2 … 20 …
…
…
T2 35 …
23 T2 38 44
leaves
T1 T2
Exclusive T2 … 20 …
…
…
T2 35 …
23 T2 38 44
leaves
T1 T2
Leaf is Safe!
Tree Locking Example T1: Search 38*
T2: Insert 45*
Shared root T3: Search 22*
…
Exclusive T3 … 20 …
…
…
T2 35 …
23 T2 38 44
leaves
T1 T2
Leaf is Safe!
Tree Locking Example T1: Search 38*
T2: Insert 45*
Shared root T3: Search 22*
…
Exclusive T3 … 20 …
…
…
T3 35 …
23 T2 38 44
leaves
T1 T2
Leaf is Safe!
Tree Locking Example T1: Search 38*
T2: Insert 45*
Shared root T3: Search 22*
…
Exclusive … 20 …
…
…
T3 35 …
T3 23 38 44
leaves
T1 T2
Leaf is Safe!
Tree Locking Example T1: Search 38*
T2: Insert 45*
Shared root T3: Search 22*
…
Exclusive … 20 …
…
…
35 …
T3 23 38 44
leaves
T1 T2
T3
20* 22* 23* 31* 35* 36* 38* 41* 44*
Leaf is Safe!
Tree Locking Example T1: Search 38*
T2: Insert 45*
Shared root T3: Search 22*
…
Exclusive … 20 …
…
…
35 …
23 38 44
leaves
T1 T2
T3
20* 22* 23* 31* 35* 36* 38* 41* 44*
Leaf is Safe!
Tree Locking Example T1: Search 38*
T2: Insert 45*
Shared root T3: Search 22*
…
Exclusive … 20 …
…
…
35 …
23 38 44
leaves
T1 T2
T3
20* 22* 23* 31* 35* 36* 38* 41* 44*
Leaf is Safe!
Only 3 pages are locked, tree can still be used by other transactions!
Specify isolation level
72
Specifying isolation level in SQL
LEVEL = SERIALIZABLE
REPEATABLE READ
Less isolation
READ COMMITTED
READ UNCOMMITED
73
REPEATABLE READ
74
READ COMMITTED
T reads only changes made by committed
transactions
No value read/written by T is changed by
another transaction until T completes.
Value read by T may be modified while T in
progress.
75
READ UNCOMMITTED
76
Specifying Acceptable Isolation Levels
77
Summary of Isolation Levels
REPEATABLE No No Maybe
READ
SERIALIZABLE No No No
78
Non-locking Concurrency Control
Locks are complicated
deadlock detection/prevention
protocol design and implementation
Locks can be expensive
79
Optimistic Concurrency Control
Idea: Don’t lock, buffer writes and validate before
‘publishing’
Transactions are assigned timestamps at start
Each transaction has its own workspace
Reads copy values into workspace
Intermediate writes happen locally in workspace
Validation checks that nothing has changed
81
Optimistic Concurrency Control
Commits may now fail
validation fails
transactions need to restart
Not a good fit for statement-by-statement SQL
Not common in SQL DBs
82
Multi Version Concurrency Control
Idea: Instead of locking reads, just preserve all
versions of records
Concurrent transactions can just read ‘visible’
versions
updates are invisible
updates create new version (no modify in place)
Only need to lock if transactions are really updating
the same record
can do this without locking but requires
transaction restart on conflict
83
MVCC example
Assume A and B have Version/Timestamp 1
T1 TS=2 T2 TS=4
R(A)
X(A); W(A)
R(A)
R(B)
R(B)
X(B); W(B)
Commit
Commit
84
MVCC example
Assume A and B have Version/Timestamp 1
T1 TS=2 T2 TS=4
R(A) read V 1
X(A); W(A)
R(A)
R(B)
R(B)
X(B); W(B)
Commit
Commit
85
MVCC example
Assume A and B have Version/Timestamp 1
T1 TS=2 T2 TS=4
R(A) read V 1
X(A); W(A) create V2
R(A)
R(B)
R(B)
X(B); W(B)
Commit
Commit
86
MVCC example
Assume A and B have Version/Timestamp 1
T1 TS=2 T2 TS=4
R(A) read V 1
X(A); W(A) create V2
R(A) read V1
R(B)
R(B)
X(B); W(B)
Commit
Commit
87
MVCC example
Assume A and B have Version/Timestamp 1
T1 TS=2 T2 TS=4
R(A) read V 1
X(A); W(A) create V2
R(A) read V1
R(B) read V1
R(B)
X(B); W(B)
Commit
Commit
88
MVCC example
Assume A and B have Version/Timestamp 1
T1 TS=2 T2 TS=4
R(A) read V 1
X(A); W(A) create V2
R(A) read V1
R(B) read V1
R(B) read V1
X(B); W(B) create V2
Commit
Commit
89
MVCC example
Assume A and B have Version/Timestamp 1
T1 TS=2 T2 TS=4
R(A) read V 1
X(A); W(A) create V2
R(A) read V1
R(B) read V1
R(B) read V1
X(B); W(B) create V2
Commit B V2 now visible
Commit
90
MVCC example
Assume A and B have Version/Timestamp 1
T1 TS=2 T2 TS=4
R(A) read V 1
X(A); W(A) create V2
R(A) read V1
R(B) read V1
R(B) read V1
X(B); W(B) create V2
Commit B V2 now visible
Commit A V2 now visible
91
Multi Version Concurrency Control
Readers don’t block readers
Readers don’t block writers
Writers don’t block readers
Only writers block writers
Each transaction effectively creates a ‘snapshot’ of DB