Professional Documents
Culture Documents
1
Example But…
T1 T2
T1 T2
L1(A); READ(A)
L1(A); READ(A)
A := A+100
A := A+100
WRITE(A); U1(A); L1(B)
WRITE(A); U1(A);
L2(A); READ(A)
L2(A); READ(A)
A := A*2
A := A*2
WRITE(A); U2(A);
WRITE(A); U2(A);
L2(B); BLOCKED…
L2(B); READ(B)
READ(B)
B := B*2
B := B+100
WRITE(B); U2(B);
WRITE(B); U1(B);
L1(B); READ(B)
…GRANTED; READ(B)
B := B+100
B := B*2
WRITE(B); U1(B);
WRITE(B); U2(B);
CSE 414 - Springschedule
Scheduler has ensured a conflict-serializable 2018 7 Locks did not enforce conflict-serializability !!! What’s wrong ? 8
A B
T2
11
2
Two Phase Locking (2PL) Two Phase Locking (2PL)
Theorem: 2PL ensures conflict serializability Theorem: 2PL ensures conflict serializability
Proof. Suppose not: then Then there is the Proof. Suppose not: then Then there is the
there exists a cycle following temporal there exists a cycle following temporal
in the precedence graph. cycle in the schedule: in the precedence graph. cycle in the schedule:
U1(A)àL2(A) why?
C C
T1 T3 T1 T3
U1(A) happened
A B A B strictly before L2(A)
T2 T2
13 14
A B L2(A) happened A B
T2 strictly before U1(A) T2
15 16
3
A New Problem:
Two Phase Locking (2PL) Non-recoverable Schedule
T1 T2
L1(A); L1(B); READ(A)
Theorem: 2PL ensures conflict serializability A :=A+100
WRITE(A); U1(A)
Proof. Suppose not: then Then there is the L2(A); READ(A)
A := A*2
there exists a cycle following temporal WRITE(A);
in the precedence graph. cycle in the schedule: L2(B); BLOCKED…
READ(B)
U1(A)àL2(A)
C B :=B+100
T1 T3 L2(A)àU2(B) WRITE(B); U1(B);
…GRANTED; READ(B)
U2(B)àL3(B)
A B B := B*2
L3(B)àU3(C) WRITE(B); U2(A); U2(B);
T2
U3(C)àL1(C) Cycle in time: Commit
Rollback
L1(C)àU1(A)
Contradiction
19 CSE 414 - Spring 2018 20
A New Problem:
Non-recoverable Schedule
Strict 2PL
T1 T2
T1 T2 L1(A); READ(A)
L1(A); L1(B); READ(A) A :=A+100
A :=A+100 WRITE(A);
WRITE(A); U1(A) L2(A); BLOCKED…
L2(A); READ(A) L1(B); READ(B)
A := A*2 B :=B+100
WRITE(A); Dirty reads of WRITE(B);
L2(B); BLOCKED… A, B lead to
READ(B) Rollback & U1(A);U1(B);
incorrect writes.
B :=B+100 …GRANTED; READ(A)
WRITE(B); U1(B); A := A*2
…GRANTED; READ(B) WRITE(A);
B := B*2 L2(B); READ(B)
WRITE(B); U2(A); U2(B); B := B*2
Elements A, B written Commit WRITE(B);
Rollback by T1 are restored Commit & U2(A); U2(B);
to their original CSE
value.
414 - Spring 2018
Can no longer 23
undo! 24
4
Strict 2PL Another problem: Deadlocks
• T1: R(A), W(B)
The Strict 2PL rule: • T2: R(B), W(A)
All locks are held until commit/abort: • T1 holds the lock on A, waits for B
All unlocks are done together with commit/abort. • T2 holds the lock on B, waits for A
Lock compatibility matrix: • Coarse grain locking (e.g., tables, entire database)
– Many false conflicts
None S X
– Less overhead in managing locks
None ✔ ✔ ✔ – E.g., SQL Lite
S ✔ ✔ ✖
X ✔ ✖ ✖ • Solution: lock escalation changes granularity as needed
5
Lock Performance Phantom Problem
Throughput (TPS)
To avoid, use
admission control • So far we have assumed the database to be a
static collection of elements (=tuples)
Why ?
TPS =
Transactions # Active Transactions
per second
CSE 414 - Spring 2018 31 CSE 414 - Spring 2018 32
Suppose there are two blue products, A1, A2: Suppose there are two blue products, A1, A2:
Phantom Problem Phantom Problem
T1 T2 T1 T2
SELECT * SELECT *
FROM Product FROM Product
WHERE color=‘blue’ WHERE color=‘blue’
INSERT INTO Product(name, color) INSERT INTO Product(name, color)
VALUES (‘A3’,’blue’) VALUES (‘A3’,’blue’)
SELECT * SELECT *
FROM Product FROM Product
WHERE color=‘blue’ WHERE color=‘blue’
R1(A1);R1(A2);W2(A3);R1(A1);R1(A2);R1(A3)
Is this schedule serializable ?
CSE 414 - Spring 2018 33 CSE 414 - Spring 2018 34
R1(A1);R1(A2);W2(A3);R1(A1);R1(A2);R1(A3)
6
Dealing With Phantoms Summary of Serializability
• Lock the entire table • Serializable schedule = equivalent to a serial
• Lock the index entry for ‘blue’ schedule
– If index is available • (strict) 2PL guarantees conflict serializability
• Or use predicate locks – What is the difference?
– A lock on an arbitrary predicate • Static database:
– Conflict serializability implies serializability
• Dynamic database:
– This no longer holds
Dealing with phantoms is expensive !
CSE 414 - Spring 2018 37 CSE 414 - Spring 2018 38
3. “Repeatable reads”
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
Why ?
Unrepeatable reads:
When reading same element twice, This is not serializable yet !!!
may get two different values
CSE 414 - Spring 2018 41 CSE 414 - Spring 2018 42
7
4. Isolation Level Serializable Beware!
In commercial DBMSs:
• “Long duration” WRITE locks • Default level is often NOT serializable
– Strict 2PL
• Default level differs between DBMSs
• “Long duration” READ locks
• Some engines support subset of levels!
– Strict 2PL
• Serializable may not be exactly ACID
• Predicate locking
– Locking ensures isolation, not atomicity
– To deal with phantoms
• Also, some DBMSs do NOT use locking and
different isolation levels can lead to different pbs
• Bottom line: RTFM for your DBMS!
CSE 414 - Spring 2018 43 CSE 414 - Spring 2018 44