7 - ConcurrencyControl
7 - ConcurrencyControl
Outline
● Example:
○ In concurrent execution environment if T1 conflicts with T2
over a data item A, then the existing concurrency control
decides if T1 or T2 should get the A and if the other
transaction is rolled-back or waits.
Lock-Based Concurrency Control
• DBMS should ensure that only serializable and recoverable
schedules are allowed
– No actions of committed transactions are lost while undoing aborted
transactions
• Locking protocol:
– a set of rules to be followed by each transaction
Two-Phase Locking Techniques
● Two Phases:
○ (a) Locking (Growing)
○ (b) Unlocking (Shrinking).
● Locking (Growing) Phase:
○ A transaction applies locks (read or write) on desired data items
one at a time.
● Unlocking (Shrinking) Phase:
○ A transaction unlocks its locked data items one at a time.
● Requirement:
○ For a transaction these two phases must be mutually exclusively,
that is, during locking phase unlocking phase must not start and
during unlocking phase locking phase must not begin.
# locks
held by
Ti
Time
Growing Shrinking
Phase Phase
2P Locking Technique Example
T1 T2
read_lock (Y); read_lock (X);
write_lock (X); Write_lock (Y);
read_item (Y); read_item (X);
read_item (X); read_item (Y);
X:=X+Y; Y:=X+Y;
write_item (X); write_item (Y);
unlock (Y); unlock (X);
unlock (X); unlock (Y);
2P Locking Technique Example
T1 T2 Result
read_lock (Y);
read_item (Y); Nonserializable because it.
unlock (Y); violated two-phase policy.
read_lock (X);
read_item (X);
unlock (X);
write_lock (Y);
read_item (Y);
Y:=X+Y;
write_item (Y);
unlock (Y);
write_lock (X);
read_item (X);
X:=X+Y;
write_item (X);
unlock (X);
2P Locking Technique Example
T’1 T’2
read_lock (Y); read_lock (X); T1 and T2 follow two-phase
read_item (Y); read_item (X); policy but they are subject to
write_lock (X); Write_lock (Y); deadlock, which must be
unlock (Y); unlock (X); dealt with.
read_item (X); read_item (Y);
X:=X+Y; Y:=X+Y;
write_item (X); write_item (Y);
unlock (X); unlock (Y);
Limitations of Concurrency Control
• Deadlock
T’1 T’2
read_lock (Y); T1 and T2 did follow two-phase
read_item (Y); policy but they are deadlock
read_lock (X);
read_item (X);
write_lock (X);
(waits for X) write_lock (Y);
(waits for Y)
T1 T2 T1 T2
T4 T3 T4 T3
Deadlock Prevention
• Assign priorities based on timestamps
• Assume Ti wants a lock that Tj holds. Two policies are possible:
– Wait-Die: It Ti has higher priority, Ti waits for Tj; otherwise Ti aborts
– Wound-wait: If Ti has higher priority, Tj aborts; otherwise Ti waits
• Convince yourself that no cycle is possible
• If a transaction re-starts, make sure it has its original timestamp
– each transaction will be the oldest one and have the highest priority at
some point
• If
– action ai of Ti conflicts with action aj of Tj,
– and TS(Ti) < TS(Tj)
• then
– ai must occur before aj
• Otherwise, abort and restart violating transaction
Request for a read: RT(X)
1. If TS(T) >= WT(X)
– last written by a previous transaction –- OK (i.e. “physically
realizable”)
– If C(X) is true –- check if previous transaction has committed
• Grant the read request by T
• if TS(T) > RT(X)
– set RT(X) = TS(T)
– If C(X) is false
• Delay T until C(X) becomes true, or the transaction that wrote X aborts
2. If TS(T) < WT(X)
– write is not realizable -- already written by a later trans.
– Abort (or, Rollback) T --i.e. abort and restart with a larger
timestamp
Request for a write: WT(X)
1. If TS(T) >= RT(X) and TS(T) >= WT(X)
– last written/read by a previous transaction – OK
– Grant the write request by T
• write the new value of X
– Set WT(X) = TS(T)
– Set C(X) = false -- T not committed yet
2. If TS(T) >= RT(X) but TS(T)< WT(X)
– write is still realizable –-but already a later value in X
– If C(X) is true
• previous writer of X has committed
• simply ignore the write request by T
• but allow T to proceed without making changes to the database
– If C(X) is false
• Delay T until C(X) becomes true, or the transaction that wrote X aborts
• If TS(T) < RT(X)
– write is not realizable -- already read by a later transaction
– Abort (or, Rollback) T
Example
• Three transactions T1 (TS = 200), T2 (TS = 150), T3 (TS = 175)
• Three objects A, B, C
– initially all have RT = WT = 0, C = 1 (i.e. true)
• Sequence of actions
– R1(B), R2(A), R3(C), W1(B), W1(A), W2(C), W3(A)
MAIN VERSION
SEGMENT O’ POOL
O
(Current (Older versions that
versions of may be useful for
O’’
DB objects) some active readers.)
old new
WTS timeline
T
Writer Transaction
• To read an object, follows reader protocol
• To write an object:
– must make sure that the object has not been read by a ”later” transaction
– Finds newest version V s.t. WT(V) <= TS(T).
• If RT(V) <= TS(T)
– T makes a copy CV of V, with a pointer to V,
with WT(CV) = TS(T), RT(CV) = TS(T)
– Write is buffered until T commits; other transactions can see TS values but
can’t read version CV
• Else
– reject write
Example
• Four transactions T1 (TS = 150), T2 (TS = 200), T3 (TS =
175), T4(TS = 225)
• One object A
– Initial version is A0
• Sequence of actions
– R1(A), W1(A), R2(A), W2(A), R3(A), R4(A)
Step T1 T2 T3 T4 A0
150 200 175 225 RT=0,
WT=0
1 R1(A)
2 W1(A)
3 R2(A)
4 W2(A)
5 R3(A)
6 R4(A)
After Step 1
A0 is the newest version with WT <= TS(T1)
Read A0
Step T1 T2 T3 T4 A0
150 200 175 225 RT=0,
WT=0
1 R1(A) Read
RT = 150
2 W1(A)
3 R2(A)
4 W2(A)
5 R3(A)
6 R4(A)
After Step 2
• A0 is the newest version with WT <= TS(T1)
• RT(A0) <= TS(T1)
• Create a new version A150
• Set its WT, RT to TS(T1) = 150 (A150 named accordingly)
Step T1 T2 T3 T4 A0 A150
150 200 175 225 RT=150 RT=150
WT=0 WT=150
1 R1(A) Read
RT = 150
2 W1(A) Create
RT=150
WT=150
3 R2(A)
4 W2(A)
5 R3(A)
6 R4(A)
After Step 3
• A150 is the newest version with WT <= TS(T2)
• Read A150
• Update RT
Step T1 T2 T3 T4 A0 A150
150 200 175 225 RT=15 RT=200
0 WT=150
WT=0
1 R1(A) Read
2 W1(A) Create
RT=150
WT=150
3 R2(A) Read
RT=200
4 W2(A)
5 R3(A)
6 R4(A)
After Step 4
• A150 is the newest version with WT <= TS(T2)
• RT(A150) <= TS(T2)
• Create a new version A200
• Set its WT, RT to TS(T2) = 200 (A200 named accordingly)
Step T1 T2 T3 T4 A0 A150 A200
150 200 175 225 RT=15 RT=200 RT=200
0 WT=150 WT=200
WT=0
1 R1(A) Read
2 W1(A) Create
RT=150
WT=150
3 R2(A) Read
RT=200
4 W2(A) Create
RT=200
WT=200
5 R3(A)
6 R4(A)
After Step 5
• A150 is the newest version with WT <= TS(T3)
• Read A150
• DO NOT Update RT
2. If transaction T issues read_item (X), find the version i of X that has the
highest write_TS(Xi) of all versions of X that is also less than or equal to TS(T),
then return the value of Xi to T, and set the value of read_TS(Xi) to the largest
of TS(T) and the current read_TS(Xi).
Rule 2 guarantees that a read will never be rejected.
Multi-version Two-Phase Locking
Using Certify Locks
● Concept
○ Allow a transaction T’ to read a data item X while it is write
locked by a conflicting transaction T.
○ This is accomplished by maintaining two versions of each data
item X where one version must always have been written by
some committed transaction. This means a write operation
always creates a new version of X.
Multi-version Two-Phase Locking
Using Certify Locks
l Steps
■ X is the committed version of a data item.
■ T creates a second version X’ after obtaining a write lock on X.
■ Other transactions continue to read X.
■ T is ready to commit so it obtains a certify lock on X’.
■ The committed version X becomes X’.
■ T releases its certify lock on X’, which is X now.
Schema
What should we lock?
Tuples, pages, tables, ... Table
But there is an implicit containment
containment Page
T1 implicitly assumes that it has locked the set of all sailor records
with rating = 1
• The assumption only holds if no sailor records are added while T1 is
executing!
• We need some mechanism to enforce this assumption
• Index locking
• Predicate locking
The example shows that conflict serialisability guarantees
serialisability only if the set of objects is fixed!
Hierarchical locks and new locking
modes
Allow transactions to lock at each level of the hierarchy
Introduce “intention” locks: IS and IX
• Before locking an item, a transaction must introduce intention locks on
all the item’s ancestors in the hierarchy
• Release locks in reverse order
One extra lock: SIX — “share, with intention to write”
Compatibility matrix
held lock
NL IS IX SIX S X
NL Y Y Y Y Y Y
wanted lock
IS Y Y Y Y Y N
IX Y Y Y N N N
SIX Y Y N N N N
S Y Y N N Y N
X Y N N N N N
In more detail
Grant lock on all records that satisfy some logical predicate, e.g.,
salary > 2000
I Index locking is a special case of predicate locking for which an index
supports efficient implementation of the predicate lock
In general, predicate locking imposes a lot of locking overhead
B+tree locking
S lock
20
S lock
10 35
S lock
6 12 23 38 44
S lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
S lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
S lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
S lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
S lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
S lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
S lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
S lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain X-locks while descending; release them top-down once the node is designated safe
Example: delete 38*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain X-locks while descending; release them top-down once the node is designated safe
Example: delete 38*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain X-locks while descending; release them top-down once the node is designated safe
Example: delete 38*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain X-locks while descending; release them top-down once the node is designated safe
Example: delete 38*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain X-locks while descending; release them top-down once the node is designated safe
Example: delete 38*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain X-locks while descending; release them top-down once the node is designated safe
Example: delete 38*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain X-locks while descending; release them top-down once the node is designated safe
Example: delete 38*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain X-locks while descending; release them top-down once the node is designated safe
Example: delete 38*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 41* 44*
Obtain X-locks while descending; release them top-down once the node is designated safe
Example: insert 25*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
X lock
Obtain X-locks while descending; leaf-node is not safe so create a new one and lock it in X-mode; first release locks on leaves
and then the rest top-down
Example: insert 25*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
X lock
Obtain X-locks while descending; leaf-node is not safe so create a new one and lock it in X-mode; first release locks on leaves
and then the rest top-down
Example: insert 25*
X lock
20
X lock
10 35
X lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
X lock
Obtain X-locks while descending; leaf-node is not safe so create a new one and lock it in X-mode; first release locks on leaves
and then the rest top-down
Example: insert 25*
X lock
20
X lock
10 35
X lock
6 12 23 25 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 25* 35* 36* 38* 41* 44*
X lock
31*
Obtain X-locks while descending; leaf-node is not safe so create a new one and lock it in X-mode; first release locks on leaves
and then the rest top-down
Example: insert 25*
X lock
20
X lock
10 35
X lock
6 12 23 25 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 25* 35* 36* 38* 41* 44*
X lock
31*
Obtain X-locks while descending; leaf-node is not safe so create a new one and lock it in X-mode; first release locks on leaves
and then the rest top-down
Example: insert 25*
X lock
20
X lock
10 35
X lock
6 12 23 25 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 25* 35* 36* 38* 41* 44*
X lock
31*
Obtain X-locks while descending; leaf-node is not safe so create a new one and lock it in X-mode; first release locks on leaves
and then the rest top-down
Optimistic B+tree locking
Search: as before
Insert/delete: set locks as if for search, get to the leaf, and set X lock
on the leaf
I If the leaf is not safe, release all locks, and restart transaction, using
previous insert/delete protocol
“Gambles” that only leaf node will be modified; if not, S locks set on
the first pass to leaf are wasteful
I In practice, better than previous algorithm
Example: insert 25*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain S-locks while descending, and X-lock at leaf; the leaf is not safe, so abort, release all locks and restart using the previous
algorithm
Example: insert 25*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain S-locks while descending, and X-lock at leaf; the leaf is not safe, so abort, release all locks and restart using the previous
algorithm
Example: insert 25*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain S-locks while descending, and X-lock at leaf; the leaf is not safe, so abort, release all locks and restart using the previous
algorithm
Example: insert 25*
S lock
20
S lock
10 35
S lock
6 12 23 38 44
X lock
3* 4* 6* 9* 10* 11* 12* 13* 20* 22* 23* 31* 35* 36* 38* 41* 44*
Obtain S-locks while descending, and X-lock at leaf; the leaf is not safe, so abort, release all locks and restart using the previous
algorithm
Even better algorithm
Search: as before
Insert/delete: use original insert/delete protocol, but set IX locks
instead of X locks at all nodes
I Once leaf is locked, convert all IX locks to X locks top-down: i.e.,
starting from the unsafe node nearest to root
I Top-down reduces chances of deadlock
F Remember, this is not the same as multiple granularity locking!
Hybrid approach
S locks
The likelihood that we will
need an X lock decreases
SIX locks
as we move up the tree
Set S locks at high levels,
SIX locks at middle levels,
X locks X locks at low levels
Summary
W1(X)R2(X)W2(X)R3(X)R4(X)W4(X)W3(X)