Concurrency Control - University of Massachusetts Amherst

Download Report

Transcript Concurrency Control - University of Massachusetts Amherst

Concurrency Control
Yanlei Diao
UMass Amherst
March 27 and 29, 2007
Slides Courtesy of R. Ramakrishnan and J. Gehrke
1
Outline
Serializability & recoverability
 Lock management
 Specialized locking techniques

 Locking for the phantom problem
 Efficient B+tree locking
Levels of Isolation
 Concurrency control without locking

 Optimistic CC
 Timestamp-based CC
 Multiversion CC
2
Conflict Serializability

Two schedules are conflict equivalent if:



Involve the same actions of the same transactions
Every pair of conflicting actions is ordered the
same way
Schedule S is conflict serializable if S is
conflict equivalent to some serial schedule
3
Example
T1:
T2:

R(A), W(A),
R(A), W(A), R(B), W(B)
R(B), W(B)
The schedule is not conflict serializable:
A
T1
T2
Dependency graph
B

The cycle in the graph reveals the problem. The
output of T1 depends on T2, and vice-versa.
4
Dependency Graph

Dependency graph:
 One node per Xact;
 Edge from Ti to Tj if an action of Ti precedes and
conflicts with one of Tj‘s actions (RW, WR, WW).

Theorem: Schedule is conflict serializable if
and only if its dependency graph is acyclic
5
Review: Strict 2PL
#. locks
C

Strict Two-phase Locking (Strict 2PL) Protocol:




time
Each Xact must obtain a S (shared) lock on object before
reading, an X (exclusive) lock on object before writing.
All locks held by a transaction are released when the
transaction completes
If an Xact holds an X lock on an object, no other Xact
can get a lock (S or X) on that object.
Strict 2PL allows only schedules whose
precedence graph is acyclic

Strict 2PL only allows serializable schedules!
6
Nonstrict 2PL

Two-Phase Locking Protocol




#. locks
C
time
Each Xact must obtain a S (shared) lock on object before
reading, an X (exclusive) lock on object before writing.
A transaction can not request additional locks once it
releases any locks.
If an Xact holds an X lock on an object, no other Xact can
get a lock (S or X) on that object.
Nonstrict 2PL ensures acyclicity of precedence
graph is acyclic


Nonstrict 2PL only allows serializable schedules.
An equivalent serial schedule is given by the order of
xacts entering their shrinking phase.
7
Weaker Condition on Serializability

Conflict serializability is sufficient but not
necessary for serializability.
S1: interleaved schedule
T1: R(A)
W(A)
T2:
W(A)
T3:
W(A)
S2: serial schedule
T1: R(A),W(A)
T2:
W(A)
T3:
W(A)
8
View Serializability

Schedules S1 and S2 are view equivalent if:



If Ti reads initial value of A in S1, then Ti also reads
initial value of A in S2
If Ti reads value of A written by Tj in S1, then Ti also
reads value of A written by Tj in S2
If Ti writes final value of A in S1, then Ti also writes
final value of A in S2
T1: R(A)
W(A)
T2:
W(A)
T3:
W(A)
T1: R(A),W(A)
T2:
W(A)
T3:
W(A)
9
View Serializability (Contd.)
A schedule is view serializable if it is view
equivalent to a serial schedule.
 Every conflict serializable schedule is view
serializable.



The converse is not true.
Every view serializable schedule that is not
conflict serializable contains a blind write.
10
Recoverability

A schedule S is recoverable if each xact commits only
after all xacts from which it read have committed.
T1: R(A),W(A)
Abort
T2:
R(A),W(A) Commit
Unrecoverable
T1: R(A),W(A)
Abort
T2:
R(A),W(A)
Abort
Recoverable, but with cascading aborts
11
Recoverability (Contd.)

S avoids cascading rollback if each xact may read only
those values written by committed xacts.
T1:
R(A),W(A)
Abort
T2: R(A)
W(A)
Commit
Recoverable, no cascading aborts,
but update of A by T2 is always lost!
12
Recoverability (Contd.)

S is strict if each xact may read and write only
objects previously written by committed xacts.
 No cascading aborts.
 Actions of aborted xacts can be simply undone by
restoring the original values of modified objects.

Strict 2PL is recoverable, in addition to conflict
serializability.
13
Venn Diagram for Schedules
All schedules
Commited Xacts
View serializable
Conflict serializable
Recoverable
Avoid
cascading Strict
aborts
Serial
Avoids cascading
rollback
Also Aborted Xacts
14
Outline
Serializability & recoverability
 Lock management
 Specialized locking techniques

 Locking for the phantom problem
 Efficient B+tree locking
Levels of Isolation
 Concurrency control without locking

 Optimistic CC
 Timestamp-based CC
 Multiversion CC
15
Lock Management
Lock manager handles the lock and unlock requests
 Transaction table: xact id  <locks held in xact>
 Lock table: object id  lock table entry

 Object can be a page, a record, etc.
 Lock table entry:
•
•
•
Number of transactions currently holding a lock
Type of lock held (shared or exclusive)
Queue of lock requests
16
Lock and Unlock

When an xact requests a lock on an object O
 If an S lock is requested, the queue of requests is empty,
O is not current locked in the X mode, then…
 If an X lock is requested, no xact currently holds a lock
on O, then…
 Otherwise, …

When an xact aborts or commits,


it releases all its locks and
when the lock on an object O’ is released, its lock table
entry is updated and the request at the head of the
queue is answered.
17
Other Lock Management Issues

Lock and unlock have to be atomic operations.
 Important when several instances of the lock manager code can
execute concurrently.
 Implemented using an OS synchronization mechanism such as a
semaphore.

Lock upgrade: An xact that holds a shared lock can be
upgraded to hold an exclusive lock.
 Important for avoiding deadlocks.

Convoys: An xact that holds a heavily used lock can be
suspended by the OS. Every other xact requesting the
lock is queued and the queue can grow very long.
 Drawback of building a DBMS on top of a general-purpose OS
with preemptive scheduling.
18
Deadlocks
Deadlock: Cycle of transactions waiting for
locks to be released by each other.
 Two ways of dealing with deadlocks:



Deadlock prevention
Deadlock detection
19
Deadlock Prevention

Assign priorities based on timestamps.
 The lower the timestamp, the higher the xact’s priority.

Assume 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. Lower priority xacts can never wait.
Wound-wait: If Ti has higher priority, Tj aborts; otherwise
Ti waits. Higher priority xacts never wait.
If a transaction re-starts, make sure it has its
original timestamp.
20
Deadlock Detection

Create a waits-for graph:




Nodes are transactions.
There is an edge from Ti to Tj if Ti is waiting for Tj to
release a lock.
Note the difference from the dependency graph for
conflict serializability.
Periodically check for cycles, indicating
deadlocks, in the waits-for graph.

Resolve a deadlock by aborting a transaction on the
cycle and releasing all its locks.
21
Deadlock Detection (Contd.)
T1: S(A), R(A),
S(B)
T2:
X(B),W(B)
X(C)
T3:
S(C), R(C)
X(A)
T4:
X(B)
T1
T2
T1
T2
T4
T3
T3
T3
22
Multiple-Granularity Locks

Granularity of locks: concurrency vs. overhead
 Finer granularity increases concurrency
 It, however, increases overhead in set/clear operations
and storage of the lock(s)
contains
Hard to decide what granularity to
lock (tuples vs. pages vs. tables).
 Shouldn’t have to decide! Because
data “containers” are nested:

Database
Tables
Pages
Indexes
Value
intervals
Tuples
Fields
23
Solution: New Lock Modes, Protocol

Allow Xacts to lock at each level, but with a
special protocol using new “intention” locks:
Before locking an item, Xact
must set “intention locks”
on all its ancestors. Why?
 SIX mode: Like S & IX at
the same time.
 To lock, request top-down.
 To unlock, release at EOT
or go from specific to
general (i.e., bottom-up).

--
IS IX S
X





IS 



IX 





--
S
X

24
Multiple Granularity Lock Protocol
Each Xact starts from the root of the hierarchy.
 To get S or IS lock on a node, must hold IS or IX
on parent node.

 What if Xact holds SIX on parent? S on parent?
To get X or IX or SIX on a node, must hold IX or
SIX on parent node.
 Must release locks in bottom-up order.

The protocol is correct in that it is equivalent to directly
setting locks at the leaf levels of the hierarchy.
25
Examples

T1 scans R, and updates a few tuples:
 T1 gets an SIX lock on R, then repeatedly gets an S
lock on tuples of R, and occasionally upgrades to
X on the tuples.

T2 uses an index to read only part of R:
 T2 gets an IS lock on R, and repeatedly
gets an S lock on tuples of R.

T3 reads all of R:
 T3 gets an S lock on R.
--
IS IX S
X





IS 



IX 





--
S
X

26
Outline
Serializability & recoverability
 Lock management
 Specialized locking techniques

 Locking for the phantom problem
 Efficient B+tree locking
Levels of Isolation
 Concurrency control without locking

 Optimistic CC
 Timestamp-based CC
 Multiversion CC
27
The Phantom Problem

If we consider inserts and updates of records in a
DB, even Strict 2PL won’t assure serializability:
 T1 locks all pages containing sailor records with
rating = 1, and finds oldest sailor, say, age = 71.
 T2 next inserts a new sailor: rating = 1, age = 96.
 T1 now reads reads the oldest sailor again; now, age = 96
appears as a phantom!

Problem: T1 assumes that it has locked the set of all
sailor records with rating = 1.
 Assumption only holds if no sailor records are added or
have the rating updated while T1 is executing!
28
The Phantom Problem (Contd.)

The phantom problem can occur even if all xacts
follow strict 2PL, often through index lookup.
 Example with a newly inserted record?
 Example with the update of an existing record?

Need a mechanism to enforce the assumption
that T1 indeed holds locks on all tuples/pages
satisfying a condition.
 Index locking, and
 Predicate locking, a more general solution.
29
Index Locking

Index
r=1
If there is a dense index on the rating field
using Alternative (2), T1 should lock the
index page containing the data entries with
rating = 1.
 If there are no records with rating = 1, T1 must
lock the index page where such a data entry
would be, if it existed!

Data
If there is no suitable index, T1 must lock the entire
file/table to prevent new pages from being added.
 so that no new records with rating = 1 can be added.
30
Predicate Locking

Fundamental reason for the phantom problem:
 The old transaction model consists of reads and writes
to individual data items.
 In practice, transactions include queries that
dynamically define sets of items based on predicates.
 When the query is executing, all the records satisfying
this predicate at a particular time can be locked.
 Locking individual items, however, cannot prevent
later addition of a record satisfying this predicate.

Solution: extend lockable objects to index pages
and further to arbitrary predicates!
31
Predicate Locking

Grant locks on some logical predicate
 e.g. age > 2*sal, or (age > 50 or age <30) and sal >10K

Index locking is a special case of predicate locking
 An existing index matches the predicate.
 It supports efficient implementation of the predicate
lock.

In general, predicate locking has a lot of overhead.
 For each record, we need to check if it satisfies a
complex predicate.
 Therefore , it is not commonly used.
32
Locking in B+ Trees

How can we efficiently lock a particular leaf node?
 BTW, don’t confuse this with multiple granularity
locking!
One solution: Ignore the tree structure, just lock
pages while traversing the tree, following 2PL.
 This has terrible performance!

 Root node (and many higher level nodes) become
bottlenecks because every tree access begins at the root.
33
Two Useful Observations
Higher levels of the tree only direct searches for
leaf pages.
 For inserts, a node on a path from root to leaf
must be locked in X mode, only if a split can
propagate up to it from the modified leaf.

 Similar point holds w.r.t. deletes.

Exploit these observations to design efficient
locking protocols that guarantee serializability
even though they violate 2PL.
34
A Simple Tree Locking Algorithm

Search: Start at root and go down; repeatedly, S lock
child then unlock parent.
 Searches never go back up. “Crabbing”, i.e. holding at most two
locks on the parent and current nodes, is enough.

Insert/Delete: Start at root and go down, obtaining X
locks as needed. Once child is locked, check if it is safe:
 If child is safe, release all locks on ancestors.
 O.w., hold X locks up to the closed safe ancestor or the root.

Safe node: Node such that changes will not propagate
up beyond this node.
 Inserts: Node is not full.
 Deletes: Node is not half-empty.
35
ROOT
A
20
Example
B
35
F
23
H
G
20*
22*
23*
24*
38
44
I
35*
36*
Do:
1) Search 38*
2) Delete 38*
3) Insert 45*
4) Insert 25*
C
D
38*
41*
E
44*
36
A Better Tree Locking Algorithm
(See Bayer-Schkolnick paper)
Search: As before.
 Insert/Delete:
 Set locks as if for Search, get to leaf, and set X
lock on leaf.
 If leaf is not safe, release all locks, and restart
Xact 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. In practice, better than previous alg.

37
ROOT
A
20
Example
B
35
F
23
H
G
20*
22*
23*
24*
38
44
I
35*
36*
Do:
1) Delete 38*
2) Insert 25*
4) Insert 45*
5) Insert 45*,
then 46*
C
D
38*
41*
E
44*
38
Outline
Serializability & recoverability
 Lock management
 Specialized locking techniques

 Locking for the phantom problem
 Efficient B+tree locking
Levels of Isolation
 Concurrency control without locking

 Optimistic CC
 Timestamp-based CC
 Multiversion CC
39
Levels of Isolation of Transactions*
Definition 1 using the notion of dirty data
 A write is committed when an xact is finished; o.w. the
write is dirty.
 Xact T sees level 0 of isolation if:
 T does not overwrite dirty data of other xacts.

Xact T sees level 1 of isolation if it sees level 0 and
 T does not commit any writes before EOT.

Xact T sees level 2 of isolation if it sees level 1 and
 T does not read dirty data of other xacts.

Xact T sees level 3 of isolation if it sees level 2 and
 Other xacts do not dirty any data read by T before T completes.
Called “degrees of consistency” in the paper written by Gray et al.
40
Levels of Isolation (Contd.)
Definition 2 using lock protocols
 Xact T sees level 0 of lock protocol if:
 T sets a (possibly short) X lock on any data it dirties.

Xact T sees level 1 of lock protocol if:
 T sets a long X lock on any data it dirties.

Xact T sees level 2 of lock protocol if:
 T sets a long X lock on any data it dirties.
 T sets a (possibly short) S lock on any data it reads.

Xact T sees level 3 of lock protocol if:
 T sets a long X lock on any data it dirties.
 T sets a long S lock on any data it reads.
41
Levels of Isolation (Contd.)
Definition 2’ using well-formedness and two phase
 T is well-formed w.r.t. to writes (reads) if it always locks an
item in X (S) mode before writing (reading) it.
 T is two phase w.r.t. to writes (reads) if it does not X (S) lock
after unlocking any item.
 Xact T sees level 0 of lock protocol if:
 T is well-formed w.r.t. writes.

Xact T sees level 1 of lock protocol if:
 T is well-formed and two phase w.r.t. writes.

Xact T sees level 2 of lock protocol if:
 T is well-formed w.r.t. reads and writes.
 T is two phase w.r.t. writes.

Xact T sees level 3 of lock protocol if:
 T is well-formed and two phase w.r.t. reads and writes.
42
Assertions

Assertion 1:
 If an xact observes the lock protocol definition of
isolation (Def. 2), it is assured of the definition of
isolation based on committed and dirty data (Def. 1).
 Unless an xact actually sets the locks described by level
1 (2 or 3) of isolation, one can construct xact mixes and
schedules that will cause the xact to run at a lower level
of isolation.

Assertion 2:
 Each xact can choose its level of isolation as long as all
xacts observe at least level 0 protocols.
43
Transaction Support in SQL-92

Each transaction has an access mode (Read Only or
not), and an isolation level.
Anomaly Dirty Unrepeatable
Read Read
Isolation level
1. Read Uncommitted Maybe Maybe
Phantom
Problem
2. Read Committed
No
Maybe
Maybe
3. Repeatable Reads
No
No
Maybe
4. Serializable
No
No
No
Maybe
Strict 2PL
Index locking
SET TRANSACTION ISOLATION LEVEL Serializable Read Only
44
Outline
Serializability & recoverability
 Lock management
 Specialized locking techniques

 Locking for the phantom problem
 Efficient B+tree locking
Levels of Isolation
 Concurrency control without locking

 Optimistic CC
 Timestamp-based CC
 Multiversion CC
45
Optimistic CC (Kung-Robinson)

Locking is a conservative approach in which
conflicts are prevented. Disadvantages:





Lock management overhead.
Deadlock detection/resolution.
Phantom problem.
Lock contention for heavily used objects causes blocking.
Idea: If conflicts are rare, we might be able to gain
concurrency by not locking, and instead checking
for conflicts before Xacts commit.
46
Kung-Robinson Model

Xacts have three phases:
 READ: Xacts read from the database, but
make changes to local (private) copies of objects.
 VALIDATE: Check for conflicts. If there is a
conflict, abort (clear the local copy & restart)!
 WRITE: Make local copies of changes public.
old
modified
objects
ROOT
new
47
Validation
Goal: guarantee that only serializable schedules
result.
 Technique: actually find an equivalent serializable
schedule

 assign an Xact id (timestamp) to each xact at the
beginning of the validation phase, and
 check if the timestamp-ordering of xacts is an
equivalent serial order.
ReadSet(Ti): Set of objects read by Xact Ti.
 WriteSet(Ti): Set of objects modified by Ti.
 Three test conditions sufficient to ensure an
equivalent serializable schedule.

48
Test 1

For all i and j such that Ti < Tj, check that Ti
completes before Tj begins.
Ti
R
V
Tj
W
R
V
W
49
Test 2

For all i and j such that Ti < Tj, check that:
 Ti completes before Tj begins its Write phase +
 WriteSet(Ti)
ReadSet(Tj) is empty.
Ti
R
V
W
R
V
W
Tj
Does Tj read dirty data? Does Tj overwrite Ti’s writes?

Is it correct?
Each condition has to guarantee that the three classes of
conflicts (W-R, R-W, W-W) go one way only.
50
Test 3

For all i and j such that Ti < Tj, check that:
 Ti completes Read phase before Tj does +
 WriteSet(Ti)
ReadSet(Tj) is empty +
 WriteSet(Ti)
WriteSet(Tj) is empty.
Ti
R
V
R
W
V
W
Tj
Does Tj read dirty data? Does Tj overwrite Ti’s writes?

Is it correct?
51
Comments on Optimistic CC

Optimistic CC vs. Locking (pessimistic)
 Optimistic CC: assume no conflicts first and only fix
things when conflicts appear, by restarting xacts.
 Locking: xacts are prevented in advance, by blocking,
from (potentially) nonserializable actions.

Works well for certain workloads:
 All xacts are readers.
 The interference among xacts is low. E.g. large amount
of data, each xact accessing a small (likely nonoverlapping) amount of data.
Deadlock free, but may have starvation.
 No phantom problem!

52
Overheads in Optimistic CC

Must record read/write activity in ReadSet and
WriteSet per Xact.
 Must create and destroy these sets as needed.

Must check for conflicts during validation
 Code for validation is in a critical section, and critical
section can reduce concurrency.

Must make validated writes ``global’’.
 Scheme for making writes global can reduce clustering
of objects (by switching pointers to objects).

Optimistic CC restarts Xacts that fail validation.
 Work done so far is wasted; requires clean-up.
 Starvation may occur.
53
Timestamp CC

Idea: (1) give each Xact a timestamp TS when it
begins; (2) give each object a read (write)
timestamp RTS (WTS) from most recent xact that
reads (writes) it.
 If action a_i of Xact Ti conflicts with action a_j
of Xact Tj, and TS(Ti) < TS(Tj), then a_i must
occur before a_j.
 Otherwise, restart violating Xact.
54
When Xact T wants to “Read” Object O

If TS(T) < WTS(O), this violates timestamp order
between T and most recent writer of O.
 So, abort T and restart it with a new, larger TS. (If
restarted with same TS, T will fail again!)
 Contrast use of timestamps for deadlock prevention.
If TS(T) > WTS(O):
 Allow T to read O.
 Reset RTS(O) to max(RTS(O), TS(T))
 Change to RTS(O) on reads must be written to
disk! This and restarts represent overheads.

55
When Xact T wants to “Write” Object O
1.
If TS(T) < RTS(O), this violates timestamp
order of T w.r.t. reader of O.

2.
Abort and restart T.
If TS(T) < WTS(O) (TS(T) >= RTS(O)), violates
timestamp order of T w.r.t. writer of O.
Thomas Write Rule: Can safely ignore such an
outdated write by T; need not restart T! (T’s write is
effectively followed by another
T
T2
write, with no intervening reads.)
 Allows some serializable but non R(A)
W(A)
conflict serializable schedules:
Commit
3. Else, allow T to write O.
W(A)
Commit

56
Timestamp CC and Recoverability


Unfortunately, unrecoverable
schedules are allowed:
Timestamp CC is modified to
allow only recoverable schedules:
T1
W(A)
T2
R(A)
W(B)
Commit
 Buffer all writes until writer commits (but update
WTS(O) when the write is allowed.)
 Block readers T (where TS(T) > WTS(O)) until writer of
O commits.
Similar to writers holding X locks until commit,
but still not quite 2PL.
 Mostly used in distributed DB systems.

57
Summary
There are several lock-based concurrency control
schemes (Strict 2PL, 2PL). Conflicts between
xacts can be detected in the dependency graph.
 The lock manager keeps track of the locks issued.
Deadlocks can either be prevented or detected.
 Multiple granularity locking reduces the
overhead involved in setting locks for nested
collections of objects (e.g., a file of pages).
 Naïve locking strategies may have the phantom
problem.

58
Summary (Contd.)

Index locking is common, and affects
performance significantly.
 Needed when accessing records via index.
 Needed for locking logical sets of records (index
locking/predicate locking).

Tree-structured indexes:
 Straightforward use of 2PL very inefficient.
 Bayer-Schkolnick illustrates potential for
improvement.

SQL-92 provides different isolation levels that
control the degree of concurrency.
59
Summary (Contd.)
Optimistic CC aims to minimize CC overheads in
an ``optimistic’’ environment where reads are
common and writes are rare.
 Optimistic CC has its own overheads however;
most real systems use locking.
 Timestamp CC is another alternative to 2PL;
allows some serializable schedules that 2PL does
not (although converse is also true).
 Ensuring recoverability with Timestamp CC
requires ability to block Xacts, similar to locking.

60
Questions
61
Even Better Algorithm
Search: As before.
 Insert/Delete:
 Use original Insert/Delete protocol, but set
IX locks instead of X locks at all nodes.
 Once leaf is locked, convert all IX locks to X
locks top-down: i.e., starting from node
nearest to root. (Top-down reduces chances
of deadlock.)

(Contrast use of IX locks here with their use in
multiple-granularity locking.)
62
Hybrid Algorithm
The likelihood that we really need an X lock
decreases as we move up the tree.
 Hybrid approach:

Set S locks
Set SIX locks
Set X locks
63
Applying Tests 1 & 2: Serial Validation

To validate Xact T:
valid = true;
// S = set of Xacts that committed after Begin(T)
< foreach Ts in S do {
if WriteSet(Ts) intersects ReadSet(Ts)
then valid = false;
}
if valid then { install updates; // Write phase
Commit T } >
else Restart T
end of critical section
64
Comments on Serial Validation
Applies Test 2, with T playing the role of Tj and
each Xact in Ts (in turn) being Ti.
 Assignment of Xact id, validation, and the Write
phase are inside a critical section!

 I.e., Nothing else goes on concurrently.
 If Write phase is long, major drawback.

Optimization for Read-only Xacts:
 Don’t need critical section (because there is no Write
phase).
65
Serial Validation (Contd.)

Multistage serial validation: Validate in stages, at
each stage validating T against a subset of the Xacts
that committed after Begin(T).
 Only last stage has to be inside critical section.
Starvation: Run starving Xact in a critical section (!!)
 Space for WriteSets: To validate Tj, must have
WriteSets for all Ti where Ti < Tj and Ti was active
when Tj began. There may be many such Xacts, and
we may run out of space.

 Tj’s validation fails if it requires a missing WriteSet.
 No problem if Xact ids assigned at start of Read phase.
66
``Optimistic’’ 2PL
If desired, we can do the following:
 Set S locks as usual.
 Make changes to private copies of objects.
 Obtain all X locks at end of Xact, make
writes global, then release all locks.
 In contrast to Optimistic CC as in KungRobinson, this scheme results in Xacts being
blocked, waiting for locks.

 However, no validation phase, no restarts
(modulo deadlocks).
67
Multiversion Timestamp CC

Idea: Let writers make a “new” copy while
readers use an appropriate “old” copy:
MAIN
SEGMENT
(Current
versions of
DB objects)

O
O’
O’’
VERSION
POOL
(Older versions that
may be useful for
some active readers.)
Readers are always allowed to proceed.
– But may be blocked until writer commits.
68
Multiversion CC (Contd.)
Each version of an object has its writer’s TS as
its WTS, and the TS of the Xact that most
recently read this version as its RTS.
 Versions are chained backward; we can
discard versions that are “too old to be of
interest”.
 Each Xact is classified as Reader or Writer.

 Writer may write some object; Reader never will.
 Xact declares whether it is a Reader when it begins.
69
Reader Xact
WTS timeline old
new
T
For each object to be read:
 Finds newest version with WTS < TS(T).
(Starts with current version in the main
segment and chains backward through
earlier versions.)
 Assuming that some version of every object
exists from the beginning of time, Reader
Xacts are never restarted.

 However, might block until writer of the
appropriate version commits.
70
Writer Xact
To read an object, follows reader protocol.
 To write an object:
 Finds newest version V s.t. WTS < TS(T).
 If RTS(V) < TS(T), T makes a copy CV of V,
with a pointer to V, with WTS(CV) = TS(T),
RTS(CV) = TS(T). (Write is buffered until T
commits; other Xacts can see TS values but
can’t read version CV.)
old
new
WTS
 Else, reject write.
CV

V
RTS(V)
T
71
Summary
There are several lock-based concurrency control
schemes (Strict 2PL, 2PL). Conflicts between
transactions can be detected in the dependency
graph
 The lock manager keeps track of the locks issued.
Deadlocks can either be prevented or detected.
 Naïve locking strategies may have the phantom
problem

72
Summary (Contd.)

Index locking is common, and affects
performance significantly.
 Needed when accessing records via index.
 Needed for locking logical sets of records (index
locking/predicate locking).

Tree-structured indexes:
 Straightforward use of 2PL very inefficient.
 Bayer-Schkolnick illustrates potential for
improvement.

In practice, better techniques now known; do
record-level, rather than page-level locking.
73
Summary (Contd.)
Multiple granularity locking reduces the
overhead involved in setting locks for nested
collections of objects (e.g., a file of pages);
should not be confused with tree index locking!
 Optimistic CC aims to minimize CC overheads
in an ``optimistic’’ environment where reads are
common and writes are rare.
 Optimistic CC has its own overheads however;
most real systems use locking.
 SQL-92 provides different isolation levels that
control the degree of concurrency

74
Summary (Contd.)
Timestamp CC is another alternative to 2PL;
allows some serializable schedules that 2PL
does not (although converse is also true).
 Ensuring recoverability with Timestamp CC
requires ability to block Xacts, which is similar
to locking.
 Multiversion Timestamp CC is a variant which
ensures that read-only Xacts are never restarted;
they can always read a suitable older version.
Additional overhead of version maintenance.

75