Isolation in Relational Databases
Isolation in Relational Databases
Databases
Chapter 24
1
What’s Different About Locking in
Relational Databases?
• In the simple databases we have been studying,
accesses are made to named items (e.g. r(x)).
– x can be locked
• In relational databases, accesses are made to
items that satisfy a predicate (for example, a
SELECT statement)
– What should we lock?
– What is a conflict?
2
Conflicts in Relational Databases
Audit: NewAccount:
SELECT SUM (balance) INSERT INTO Accounts
FROM Accounts VALUES (‘123’,‘Mary’,100);
WHERE name = ‘Mary’;
3
What to Lock?
4
Problem with Row Locking
• Audit
(1) Locks and reads Mary’s rows in Accounts
• NewAccount
(2) Inserts and locks new row t, in Accounts
(3) Locks and updates Mary’s row in Depositors
time (4) Commits and releases all locks
• Audit
(5) Locks and reads Mary’s row in Depositors
5
Row Locking (RL)
6
Phantoms in RL
• Example:
T1: UPDATE Table T2: INSERT INTO Table
SET Attr = …. VALUES ( … satisfies P…)
WHERE P
7
Phantoms in RL
8
Predicate Locking (PL)
• TL prevents phantoms; RL does not
• Predicate locking also prevents phantoms
– A predicate describes a set of rows, some are in a
table and some are not; e.g. name = ‘Mary’
• A subset of the rows satisfying name = ‘Mary’ are in Accounts
– Every SQL statement has an associated predicate
– When executing a statement, acquire a (read or write)
lock on the associated predicate
– Two predicate locks conflict if one is a write and there
exists a row (not necessarily in the table) that is
contained in both
9
Phantoms
rows in R satisfying P
(rows that can be locked)
rows satisfying
rows in predicate P
table R
rows satisfying P
all rows that can that do not exist in R
possibly be in table R
10
Preventing Phantoms With PLs
Audit: NewAccount:
SELECT SUM (balance) INSERT INTO Accounts
FROM Accounts VALUES (‘123’,‘Mary’,100)
WHERE name = ‘Mary’
11
Conflicts And Predicate Locks
• Example 1:
SELECT SUM (balance) DELETE
FROM Accounts FROM Accounts
WHERE name = ‘Mary’ WHERE bal < 100
• Statements conflict since:
– Predicates overlap and one is a write
– There might be acc with bal < 100 and name = ‘Mary’
– Locking is conservative: there might be no rows in
Accounts satisfying both predicates
– No phantom involved in this (DELETE) case
12
Conflicts And Predicate Locks
• Example 2:
13
Serializability in Relational DB
• Table locking
– prevents phantoms and
– produces serializable schedules, but
– negatively impacts performance
• Row locking
– does not prevent phantoms and
– can produce nonserializable schedules
– Performance ok
14
Serializability in Relational DB
• Predicate locking
– prevents phantoms and
– produces serializable schedules, but is
– too complex to implement
15
Isolation Levels
• SQL defines several isolation levels weaker than
SERIALIZABLE that allow non-serializable
schedules and hence allow more concurrency
s serializable s
conc. control
s
s
s weaker s
conc. control
Schedules allowed
at a weaker isolation level fewer delays
16
Isolation Levels
17
Anomaly
18
Anomaly: Non-Repeatable Read
T1 T2
SELECT SUM (balance)
FROM Accounts
WHERE name = ‘Mary’
UPDATE Accounts
SET balance = 1.05 * balance
WHERE name = ‘Mary’
SELECT SUM (balance) does not introduce a
FROM Accounts phantom into predicate
WHERE name = ‘Mary’ name=‘Mary’
19
Non-Repeatable Reads and Phantoms
20
SQL Isolation Levels
21
SQL Isolation Levels
22
Statement Isolation
• In addition, statement execution must be isolated
– DBMS might be executing several SQL statements (from
different transactions) concurrently
– The execution of statement involves the execution of a
program implementing that statement’s query plan
• This might be a complex program
23
Locking Implementation of SQL
Isolation Levels
• SQL standard does not say how to implement levels
• Locking implementation is based on:
– Entities locked: rows, predicates, …
– Lock modes: read & write
– Lock duration:
• Short: locks acquired in order to execute a statement
are released when statement completes
• Long: locks acquired in order to execute a statement are
held until transaction completes
• Medium: something in between (we give example later)
24
Locking Implementation of SQL
Isolation Levels
• Write locks are handled identically at all
isolation levels:
– Long-duration predicate write locks are associated
with UPDATE, DELETE, and INSERT statements
• This rules out dirty writes
– In practice, predicate locks are implemented with
table locks or by acquiring locks on an index as
well as the data
• We discuss index locking later
25
Locking Implementation of SQL
Isolation Levels
• Read locks handled differently at each level:
– READ UNCOMMITTED: no read locks
• Hence a transaction can read a write-locked item!
• Allows dirty reads, non-repeatable reads, and phantoms
26
Locking Implementation
– REPEATABLE READ: long-duration read locks on
rows returned by SELECT
• Prevents dirty and non-repeatable reads, but
phantoms are possible
27
Bad Things Can Happen
• Schedules can be
– Non-serializable
– Specifications of transactions might not be met
28
Some Problems at READ UNCOMMITTED
• Since no read locks are obtained, T2 can read a
row t, write locked by T1
T1: w(t) abort T2 uses an aborted
T2: r(t) w(t) commit value to update db
T2 uses an intermediate
T1: w(t) w(t) commit
value to update db
T2: r(t) w(t) commit
T2 does not see a
T1: w(t) w(t) commit committed snapshot
T2: r(t) r(t) commit
• Some DBMSs allow only read-only transactions
to be executed at this level
29
Some Problems at READ COMMITTED
• Non-repeatable reads:
T1: r(t) r(t) commit
T2: w(t) commit
• Lost updates:
30
Problems at REPEATABLE READ
• Phantoms:
– t satisfies pred
– A constraint relates rows satisfying pred and t
31
Implications of Locking Implementation
• Transactions can be assigned to different isolation
levels and can run concurrently.
– Since all write locks are long-duration predicate
locks and SERIALIZABLE transactions have
long-duration predicate read locks, SERIALIZABLE
transactions are serialized with respect to all writes.
• A SERIALIZABLE transaction either sees the entire
effect of another transaction or no effect.
– A transaction at a lower level does not see the
anomalies prohibited at that level.
32
Implications of Locking Implementation
33
CURSOR STABILITY
34
Cursors at READ COMMITTED
• Access by T1 through a cursor C, generally
involves OPEN followed by a sequence of FETCHs
– Each statement is atomic and isolated
– C is INSENSITIVE: rows FETCHed cannot be affected
by concurrent updates (since OPEN is isolated)
– C is not INSENSITIVE: some rows FETCHed might
have been updated by a concurrent transaction T2, and
others might not
• Furthermore, T1 might fetch a row, T2 might update the
row and commit, and then T1 might overwrite the update
35
CURSOR STABILITY
38
Update Locks
Granted mode
Requested mode read write update
read x
write x x x
update x x
39
Update Locks
• Schedule that causes a deadlock at CURSOR
STABILITY:
T1: fetch(t) request_update(t)
T2: fetch(t) request_update(t)
40
OPTIMISTIC READ COMMITTED
41
OPTIMISTIC READ COMMITTED
42
Sometimes Good Things Happen
43
Correct Execution at READ
UNCOMMITTED
• Example: Print_Alumni_Transcript(s)
– Reads Transcript table and prints a transcript
for a student, s, that has graduated. Since no
concurrently executing transaction will be
updating s’s record, the transaction executes
correctly at READ UNCOMMITTED
44
Correct Execution READ COMMITTED
• Example - Register(s,c)
– Step 1: Read table Requires to determine c’s prerequisites
– Step 2: Read table Transcript to check that s has completed all of c’s prerequisites
– Step 3: Read table Transcript to check that s does not enroll for more than 20 credits
– Step 4: If there is room in c, update Class:
UPDATE Class C
SET C.Enrollment = C.Enrollment +1
WHERE C.CrsCode = c AND C.Enrollment < C.MaxEnrollment
45
Possible Interactions
• Register(s,c) executed at READ COMMITTED
concurrently with a transaction that adds/deletes
prerequisite for c
– either Register sees new prerequisite or does not
Add_Prereq: INS(Requires)
47
Possible Interactions
48
Serializable, SERIALIZABLE & Correct
49
Serializable, SERIALIZABLE & Correct
50
Serializable, SERIALIZABLE & Correct
51
Granular Locks
52
Granular Locks
Problem:
• T1 holds a (fine grained) lock on field F1 in record R1.
• T2 requests a conflicting (coarse grained) lock on R1.
53
Granular Locks (GL)
Solution:
• Organize locks hierarchically by containment and
• Require that a transaction to get a fine grained lock it
– must first get a coarse grained lock on the containing item
Hence, T1 must:
• First get a lock on R1
• Before getting a lock on F1.
The conflict with T2 is detected at R1.
54
Intention Locking
55
Conflict Table
Requested Granted mode
mode IS IX S X SIX
IS x
IX x x x
S x x x
X x x x x x
SIX x x x x
• Example 1: T2 denied an IX lock (intends to update
some contained items) since T1 is reading all
contained items
• Example 2: T2 granted IS lock even though T1 holds IX
lock (since they may be accessing different subsets of
contained items)
56
Preventing Phantoms
• Lock entire table - this works
– T1 executes SEL(P): (where P is a predicate),
obtains long-duration S lock on table
– T2 executes INS(t): requires long-duration
X lock on table
– Phantom prevented
• Lock the predicate P - this works but entails too
much overhead
• Can granular locking be used?
57
No Appropriate Index for P
• Assume containment hierarchy is table/pages:
• T1 does SEL(P): obtains long-duration S lock on table
– Since it must read every page to find rows satisfying P
• T2 requests INS(t): obtains long-duration IX lock on
table (lock conflict detected) and X lock on page into
which t is to be inserted.
– Hence (a potential) phantom is prevented
– However other transaction can read parts of the table that
are stored on pages other than the one on which t is
stored
58
Index I exists on attribute P
• T1 obtains long-duration IS lock on table, uses I
to locate pages containing rows satisfying P,
and acquires long-duration S locks on them.
• T2 obtains long-duration IX lock on table (no
conflict) and X lock on page p, into which
t is to be inserted.
– Problem: Since p might not be locked by T1, a
phantom can result.
59
Index Locking
• Solution: lock pages of the index in addition
• Example: I is an unclustered B+ tree.
– T1 obtains:
• long-duration IS lock on table,
• long-duration S locks on the pages containing rows
satisfying P, and
• long-duration S locks on the leaf index pages
containing entries satisfying P
– T2 requests:
• long-duration IX lock on table (granted),
• long-duration X locks on the page into which t is to be
inserted (might be granted), and
• long-duration X lock on the leaf index page into which
the index entry for t will be stored (lock conflict if t
satisfies P)
– The phantom is prevented.
60
Index Locking - Example
T1: SELECT F.Name
FROM Faculty F
WHERE F.Salary > 70000 unclustered
index on
Holds: IS lock on Faculty, Salary
S lock on a, b, d, e
S lock on e
Faculty
a b c d
T2: INSERT INTO Faculty
VALUES (…75000, …)
inserted row
Requests: IX lock on Faculty,
X lock on c, X lock on e 61
Index, Predicate & Key-Range Locks
If a WHERE clause:
• refers to a predicate name = mary and if
• there is an index on name,
then an index lock on index entries for name = mary
• is like a predicate lock on that predicate
62
Key-Range Locking
• Instead of locking index pages, index entries at
the leaf level are locked
– Each such lock is interpreted as a lock on a range
• Suppose the domain of an attribute is A…Z and
suppose at some time the entries in the index are
CGPRX
• A lock on G is interpreted as a lock on the half-
open interval
[G P) which includes G but not P
63
Key-Range Locking (cont)
64
Key-Range Locking (cont)
• Recall the index entries are: C G P R X
• To insert a new key, J, in the index
– Lock G thus locking the interval [G P)
– Insert J thus splitting the interval into [G J) [J P)
– Lock J thus locking [J P)
– Release the lock on G
• If a SELECT statement had a lock on G as part of a
key-range, then the first step of the insert
protocol could not be done
– Thus phantoms are prevented and the key-range
lock is equivalent to a predicate lock
65
Locking a B-Tree Index
• Many operations need to access an index structure
concurrently
– This would be a bottleneck if conventional two-
phase locking mechanisms were used
• Understanding index semantics, we can develop a
more efficient locking algorithm
– Goal is to maintain isolation among different
operations, concurrently accessing the index
– The short term locks on the index are called latches
– The long term locks on leaf entries we have been
discussing are still obtained
66
Read Locks on a B-Tree Index
67
Write Locks on a B-Tree Index
• Obtain a write lock on the root, and work down
the tree locking each entry as it is reached
• When new entry n is locked, if that entry is not
full, the locks on all its parents can be released
– An insert operation might have to go back up the
tree, revisiting and perhaps splitting some nodes
– Even if that occurs, because n is not full, it will
not have to split n and therefore will not have to
go further up the tree
– Thus it can release locks further up in the tree.
68
Granular and Index Locking Summary
69
UPDATE Statement
70
Lock Escalation
• Beware of deadlock
71
GL in an Object Database
72
Granular Locking Protocol for
Object Databases
• Before obtaining a lock on an object instance,
the system must obtain the appropriate
intention locks on the object’s class and all
the ancestor classes
73
Performance Hints
• Use lowest correct isolation level
• Embedding constraints in schema might permit
the use of an even lower level
– Constraint violation due to interleaving detected at
commit time (an optimistic approach)
• No user interaction after a lock has been acquired
• Use indexes and denormalization to support
frequently executed transactions
• Avoid deadlocks by controlling the order in which
locks are acquired
74
Multiversion Controls (MVCs)
• Version: a snapshot of the database containing the
updates of all and only committed transactions
75
Read-Consistency
• All DBMSs guarantee that statements are isolated:
– Each statement sees state produced by the complete
execution of other statements, but state might not be
committed
• A MVC guarantees that each statement sees a committed
state:
– Statement executed in a state whose value is a version
– Referred to as statement-level read consistency
• A MVC can also guarantee that all statements of a
transaction see the same committed state:
– All statements of a transaction access same version
– Referred to as transaction-level read consistency
76
Read-Only MVC
• Distinguishes in advance read-only (R/O)
transactions from read/write (R/W) transactions.
– R/W transactions use a (conventional) immediate-
update, pessimistic control. Hence, transactions
access the most current version of the database.
– All the reads of a particular R/O transaction TRO
are satisfied using the most recent version that
existed when TRO requested its first read.
77
Read-Only MVC
• Assuming R/W transactions are executed at
SERIALIZABLE, all schedules are serializable:
– R/W transactions are serialized in commit order.
– Each R/O transaction is serialized after the
transaction that created the version it read.
– Equivalent serial order is not commit order.
• All transactions see transaction-level read
consistency.
78
Example
79
Implementation
• DBMS maintains a version counter (VC).
– Incremented each time a R/W transaction commits.
• The new version of a data item created by a R/W
transaction is tagged with the value of VC at the
time the transaction commits.
• When a R/O transaction makes its 1st read request,
the value of VC becomes its counter value.
– Each request to read an item is satisfied by the
version of the item having the largest version
number less than or equal to the transaction’s
counter value.
80
Multiversion Database
x y z u
v1 v2 v1 v4
17 a .223 38
v2 v3 v5
22 ab .24
v3 v6
123 abf
82
Read-Consistency MVC
• R/O transactions:
– As before: get transaction-level read consistency.
• R/W transactions:
– Write statements acquire long-duration write locks
(delay other write statements)
– Read statements use most recent (committed)
version at time of read
• Not delayed by write locks, since read locks are
not requested.
83
Example
84
Read-Consistency MVC
85
SNAPSHOT Isolation
• Does not distinguish between R/W and R/O
transactions
• A transaction reads the most recent version that
existed at the time of its first read request
– Guarantees transaction-level read consistency
• The write sets of any two concurrently executing
transactions must be disjoint
• Two implementations of this specification
– First Committer Wins
– Locking implementation
86
First Committer Wins Implementation
87
First Committer Wins
to intentions list
T1: r(xn) w(x) request_commit
T2: r(xn) w(x) request_commit
abort
counter value = n commit and
create w(xn+1)
• Control is optimistic:
– It can be implemented without any locks
– Deadlock not possible
– Validation (write set intersection) is required for
R/W transactions and abort is possible
– Schedules might not be serializable
88
Locking Implementation of
SNAPSHOT Isolation
89
Locking Implementation of
SNAPSHOT Isolation
90
Anomalies at SNAPSHOT Isolation
• Many anomalies are impossible:
– Dirty read, dirty write, non-repeatable read, lost update
• However, schedules might not be serializable.
• Example:
T1: r(a:10) r(b:10) w(a:-5) commit
T2: r(a:10) r(b:10) w(b:-5) commit
91
Phantoms at SNAPSHOT Isolation
Audit:
SELECT SUM (balance)
FROM Accounts NewAccnt:
WHERE name = ‘Mary’ INSERT INTO Accounts
VALUES (‘123’,‘Mary’,100)
UPDATE Depositors
SET totbal = totbal + 100
SELECT totbal WHERE name = ‘Mary’
FROM Depositors
WHERE name = ‘Mary’
• Both transactions commit.
• All reads of a transaction satisfied from the same version.
• Hence Audit works correctly.
92
Phantoms at SNAPSHOT Isolation
93
Phantoms at SNAPSHOT Isolation
• Non-serializable schedules due to phantoms
are possible
• Example: concurrent transactions each execute
SEL(P) and then insert a row satisfying P
– Neither sees the row inserted by the other.
– The schedule is not serializable.
– This would be considered a phantom if it occurred
at REPEATABLE READ.
– Can be considered write skew
94
Correct Execution at
SNAPSHOT Isolation
95
Reserving Seats for a Concert
• A reservation transaction checks the status of two
seats and then reserves one that is free
– Schedule below is non-serializable, but is correct
and preserves the constraint
97