CS 245: Database System Principles

CS4432: Database Systems II
Lecture #21
Concurrency Control
Professor Elke A. Rundensteiner
cs4432
concurrency control
1
Concepts
Transaction: sequence of ri(x), wi(x) actions
Schedule: represents chronological order in which
actions are executed
Serial schedule: no interleaving of trans/actions
cs4432
concurrency control
2
What about concurrent actions?
Ti issues System
read(x,t) issues
input(x)
cs4432
CS
245
Input(X)
t
completes
concurrency
Notes 09
control

x
time
4
What about concurrent actions?
Ti issues System
read(x,t) issues
input(x)
Input(X)
t
completes

x
time
T2 issues
write(B,S)
input(B)
System
completes
issues
output(B)
System
output(B)
issues
BS
completes
input(B)
cs4432
CS
245
concurrency
Notes 09
control
5
So net effect is either
• S=…r1(x)…w2(b)… or
• S=…w2(b)…r1(x)…
cs4432
CS
245
concurrency
Notes 09
control
6
What about conflicting, concurrent
actions on same object?
start r1(A)
start w2(A)
cs4432
CS
245
end r1(A)
end w2(A)
concurrency
Notes 09
control
time
7
What about conflicting, concurrent
actions on same object?
start r1(A)
start w2(A)
end r1(A)
end w2(A)
time
• Assume equivalent to either r1(A) w2(A)
or w2(A) r1(A)
•  low level synchronization mechanism
• Assumption called “atomic actions”
cs4432
CS
245
concurrency
Notes 09
control
8
Concepts
Conflicting actions: read/write on same resource A:
r1(A) w2(A) w1(A)
w2(A) r1(A) w2(A)
cs4432
concurrency control
9
Anomalies with Interleaving
Reading Uncommitted Data (WR Conflicts, “dirty reads”):
e.g. T1: A+100, B+100, T2: A*1.06, B*1.06
T1:
T2:
R(A), W(A),
R(A), W(A), C
R(B), W(B), Abort
Unrepeatable Reads (RW Conflicts):
E.g., T1: R(A), check if A >0, decrement, T2: R(A), decrement
T1:
T2:
R(A),
R(A), W(A), C
R(A), W(A), C
Overwriting Uncommitted Data (WW Conflicts):
T1:
T2:
W(A),
W(A), W(B), C
W(B), C
Schedule C
T1
Read(A); A  A+100
Write(A);
T2
A
25
125
Read(A);A  A2;
Write(A);
250
Read(B); B  B+100;
Write(B);
125
Read(B);B  B2;
Write(B);
cs4432
B
25
concurrency control
250
250
250
11
Example:
Sc=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)
Sc’=r1(A)w1(A) r1(B)w1(B)r2(A)w2(A)r2(B)w2(B)
T1
cs4432
T2
concurrency control
12
Returning to Sc
Sc=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B)
T1  T2
T1  T 2
 no cycles  Sc is “equivalent” to a
serial schedule,
I.e., in this case (T1,T2).
cs4432
concurrency control
13
Schedule D
T1
Read(A); A  A+100
Write(A);
T2
A
25
125
Read(A);A  A2;
Write(A);
250
Read(B);B  B2;
Write(B);
Read(B); B  B+100;
Write(B);
cs4432
B
25
50
250
concurrency control
150
150
14
Now for Sd:
Sd=r1(A)w1(A)r2(A)w2(A) r2(B)w2(B)r1(B)w1(B)
Sd=r1(A)w1(A) r1(B)w1(B)r2(A)w2(A)r2(B)w2(B)
T2
T1
cs4432
concurrency control
15
Or, let’s try for Sd:
Sd=r1(A)w1(A)r2(A)w2(A) r2(B)w2(B)r1(B)w1(B)
Sd=r2(A)w2(A)r2(B)w2(B) r1(A)w1(A)r1(B)w1(B)
T2
cs4432
T1
concurrency control
16
In short, Schedule D cannot be “fixed” :
Sd=r1(A)w1(A)r2(A)w2(A) r2(B)w2(B)r1(B)w1(B)
• as a matter of fact, there seems to
be no save way to transform this Sd
into an equivalent serial schedule !?
cs4432
concurrency control
17
For Schedule D:
Sd=r1(A)w1(A)r2(A)w2(A) r2(B)w2(B)r1(B)w1(B)
T1  T2
T1
T2  T1
T2
Sd cannot be rearranged into serial schedule
cs4432
concurrency control
18
Idea: Swap non-conflicting
operation pairs to see if you
can go to a serial schedule.
cs4432
concurrency control
19
Definition
S1, S2 are conflict equivalent schedules
if S1 can be transformed into S2 by a
series of swaps of non-conflicting
actions.
cs4432
concurrency control
20
Definition
A schedule is conflict serializable
if it is conflict equivalent
to some serial schedule.
cs4432
concurrency control
21
How determine this ?
Answer: A Precedence Graph !
cs4432
concurrency control
22
Precedence graph P(S) (S
is schedule)
Nodes: transactions in S
Arcs: Ti  Tj whenever
- pi(A), qj(A) are actions in S
- pi(A) <S qj(A)
- at least one of pi, qj is a write
cs4432
concurrency control
23
Exercise:
• What is P(S) for
S = w3(A) w2(C) r1(A) w1(B) r1(C) w2(A) r4(A) w4(D)
• Is S conflict-serializable?
cs4432
concurrency control
24
Another Exercise:
• What is P(S) for
S = w1(A) r2(A) r3(A) w4(A) ?
• Is S conflict-serializable?
cs4432
concurrency control
25
Lemma
S1, S2 conflict equivalent  P(S1)=P(S2)
Proof:
Assume P(S1)  P(S2)
  Ti: Ti  Tj in S1 and not in S2
 S1 = …pi(A)... qj(A)…
pi, qj
S2 = …qj(A)…pi(A)...
conflict
 S1, S2 not conflict equivalent
cs4432
concurrency control
26
Note: P(S1)=P(S2)  S1, S2 conflict equivalent?
cs4432
concurrency control
27
Note: P(S1)=P(S2)  S1, S2 conflict equivalent
Counter example:
S1=w1(A) r2(A)
w2(B) r1(B)
S2=r2(A) w1(A)
r1(B) w2(B)
cs4432
concurrency control
28
Theorem
P(S1) acyclic  S1 conflict serializable
() Assume S1 is conflict serializable
  Ss: Ss, S1 conflict equivalent
 P(Ss) = P(S1)
 P(S1) acyclic since P(Ss) is acyclic
cs4432
concurrency control
29
Theorem
P(S1) acyclic  S1 conflict serializable
T1
() Assume P(S1) is acyclic
T2 T3
Transform S1 as follows:
(1) Take T1 to be transaction with no incident arcs T4
(2) Move all T1 actions to the front
S1 = ……. qj(A)…….p1(A)…..
(3) we now have S1 = < T1 actions ><... rest ...>
(4) repeat above steps to serialize rest!
cs4432
concurrency control
30
Theorem holds:
P(S1) acyclic  S1 conflict serializable
cs4432
concurrency control
31