carnegie mellon univ. dept. of computer science 15-415 - database applications
DESCRIPTION
Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications. Concurrency Control, II (R&G ch . 17). Review. DBMSs support ACID Transaction semantics . Concurrency control and Crash Recovery are key components. Review. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/1.jpg)
CMU SCS
Carnegie Mellon Univ.Dept. of Computer Science
15-415 - Database Applications
Concurrency Control, II (R&G ch. 17)
![Page 2: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/2.jpg)
CMU SCS
2Faloutsos SCS 15-415
Review
• DBMSs support ACID Transaction semantics.
• Concurrency control and Crash Recovery are key components
![Page 3: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/3.jpg)
CMU SCS
3Faloutsos SCS 15-415
Review
• For Isolation property, serial execution of transactions is safe but slow– Try to find schedules equivalent to serial
execution• One solution for “conflict serializable”
schedules is Two Phase Locking (2PL)
![Page 4: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/4.jpg)
CMU SCS
4Faloutsos SCS 15-415
Outline
• Serializability - concepts and algorithms• One solution: Locking
– 2PL– variations
• Deadlocks
![Page 5: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/5.jpg)
CMU SCS
5Faloutsos SCS 15-415
View Serializability• Alternative (weaker) notion of serializability.• Schedules S1 and S2 are view equivalent if:
1. If Ti reads initial value of A in S1, then Ti also reads initial value of A in S2
2. If Ti reads value of A written by Tj in S1, then Ti also reads value of A written by Tj in S2
3. 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)
view
![Page 6: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/6.jpg)
CMU SCS
6Faloutsos SCS 15-415
View Serializability
• Basically, allows all conflict serializable schedules + “blind writes”
T1: R(A) W(A)T2: W(A)T3: W(A)
T1: R(A),W(A)T2: W(A)T3: W(A)
view
![Page 7: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/7.jpg)
CMU SCS
7Faloutsos SCS 15-415
View Serializability
• Basically, allows all conflict serializable schedules + “blind writes”
T1: R(A) W(A)T2: W(A)T3: W(A)
T1: R(A),W(A)T2: W(A)T3: W(A)
view
A: 5 10 8 25 A: 5 8 10 25
![Page 8: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/8.jpg)
CMU SCS
8Faloutsos SCS 15-415
Notes on Serializability Definitions
• View Serializability allows (slightly) more schedules than Conflict Serializability does.– Problem is that it is difficult to enforce
efficiently.• Neither definition allows all schedules that
you would consider “serializable”.– This is because they don’t understand the
meanings of the operations or the data (recall example #4’)
![Page 9: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/9.jpg)
CMU SCS
9Faloutsos SCS 15-415
Notes on Serializability Definitions
• In practice, Conflict Serializability is what gets used, because it can be enforced efficiently.– To allow more concurrency, some special cases
do get handled separately, such as for travel reservations, etc.
![Page 10: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/10.jpg)
CMU SCS
10Faloutsos SCS 15-415
Outline
• Serializability - concepts and algorithms• One solution: Locking
– 2PL– variations
• Deadlocks
![Page 11: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/11.jpg)
CMU SCS
11Faloutsos SCS 15-415
Two-Phase Locking (2PL)
• Locking Protocol– ‘S’ (shared) and ‘X’ (eXclusive) locks– A transaction can not request additional locks
once it releases any locks.– Thus, there is a “growing phase” followed by a
“shrinking phase”.
S X
S –
X – –
LockCompatibilityMatrix
![Page 12: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/12.jpg)
CMU SCS
12Faloutsos SCS 15-415
2PL
THEOREM: if all transactions obey 2PL -> all schedules are serializable
![Page 13: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/13.jpg)
CMU SCS
13Faloutsos SCS 15-415
2PL
THEOREM: if all transactions obey 2PL -> all schedules are serializable
(if even one violates 2PL, non-serializability is possible -example?)
![Page 14: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/14.jpg)
CMU SCS
14Faloutsos SCS 15-415
Two-Phase Locking (2PL), cont.
• 2PL on its own is sufficient to guarantee conflict serializability (i.e., schedules whose precedence graph is acyclic), but, it is subject to Cascading Aborts.
time
# locks held
release phaseacquisition phase
![Page 15: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/15.jpg)
CMU SCS
15Faloutsos SCS 15-415
2PL
• Problem: Cascading Aborts• Example: rollback of T1 requires rollback of T2!
• Solution: Strict 2PL, i.e,• keep all locks, until ‘commit’
T1: R(A), W(A), R(B), W(B), AbortT2: R(A), W(A)
![Page 16: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/16.jpg)
CMU SCS
16Faloutsos SCS 15-415
Strict 2PL
• Allows only conflict serializable schedules, but it is actually stronger than needed for that purpose.
# locks held
acquisition phase
time
release all locks at end of xact
![Page 17: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/17.jpg)
CMU SCS
17Faloutsos SCS 15-415
Strict 2PL (continued)
• In effect, “shrinking phase” is delayed until– Transaction commits (commit log record on disk), or– Aborts (then locks can be released after rollback).
# locks held
acquisition phase
time
release all locks at end of xact
![Page 18: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/18.jpg)
CMU SCS
18Faloutsos SCS 15-415
Next ...
• A few examples
![Page 19: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/19.jpg)
CMU SCS
19Faloutsos SCS 15-415
Lock_X(A)Read(A) Lock_S(A)A: = A-50Write(A)Unlock(A)
Read(A)Unlock(A)Lock_S(B)
Lock_X(B)Read(B)Unlock(B)PRINT(A+B)
Read(B)B := B +50Write(B)Unlock(B)
Non-2PL, A= 1000, B=2000, Output =?
![Page 20: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/20.jpg)
CMU SCS
20Faloutsos SCS 15-415
Lock_X(A)Read(A) Lock_S(A)A: = A-50Write(A)
Lock_X(B)Unlock(A)
Read(A)Lock_S(B)
Read(B)B := B +50Write(B)Unlock(B) Unlock(A)
Read(B)Unlock(B)PRINT(A+B)
2PL, A= 1000, B=2000, Output =?
![Page 21: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/21.jpg)
CMU SCS
21Faloutsos SCS 15-415
Lock_X(A)Read(A) Lock_S(A)A: = A-50Write(A)
Lock_X(B)Read(B)B := B +50Write(B)Unlock(A)Unlock(B)
Read(A)Lock_S(B)Read(B)PRINT(A+B)Unlock(A)Unlock(B)
Strict 2PL, A= 1000, B=2000, Output =?
![Page 22: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/22.jpg)
CMU SCS
22Faloutsos SCS 15-415
Venn Diagram for Schedules
All Schedules
Avoid Cascading Abort
Serial
View Serializable
Conflict Serializable
![Page 23: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/23.jpg)
CMU SCS
23Faloutsos SCS 15-415
Q: Which schedules does Strict 2PL allow?
All Schedules
Avoid Cascading Abort
Serial
View Serializable
Conflict Serializable
![Page 24: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/24.jpg)
CMU SCS
24Faloutsos SCS 15-415
Q: Which schedules does Strict 2PL allow?
All Schedules
Avoid Cascading Abort
Serial
View Serializable
Conflict Serializable
![Page 25: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/25.jpg)
CMU SCS
25Faloutsos SCS 15-415
Lock Management
• Lock and unlock requests handled by the Lock Manager (LM).
• LM contains an entry for each currently held lock.• Q: structure of a lock table entry?
![Page 26: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/26.jpg)
CMU SCS
26Faloutsos SCS 15-415
Lock Management
• Lock and unlock requests handled by the Lock Manager (LM).
• LM contains an entry for each currently held lock.• Lock table entry:
– Ptr. to list of transactions currently holding the lock– Type of lock held (shared or exclusive)– Pointer to queue of lock requests
![Page 27: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/27.jpg)
CMU SCS
27Faloutsos SCS 15-415
Lock Management, cont.
• When lock request arrives see if any other xact holds a conflicting lock.– If not, create an entry and grant the lock– Else, put the requestor on the wait queue
• Lock upgrade: transaction that holds a shared lock can be upgraded to hold an exclusive lock
![Page 28: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/28.jpg)
CMU SCS
28Faloutsos SCS 15-415
Lock Management, cont.
• Two-phase locking is simple enough, right?• We’re not done. There’s an important wrinkle …
![Page 29: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/29.jpg)
CMU SCS
29Faloutsos SCS 15-415
Example: Output = ?
Lock_X(A)Lock_S(B)Read(B)Lock_S(A)
Read(A)A: = A-50Write(A)Lock_X(B)
![Page 30: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/30.jpg)
CMU SCS
30Faloutsos SCS 15-415
Example: Output = ?
Lock_X(A)Lock_S(B)Read(B)Lock_S(A)
Read(A)A: = A-50Write(A)Lock_X(B)
lock mgr:grantgrant
wait
wait
![Page 31: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/31.jpg)
CMU SCS
31Faloutsos SCS 15-415
Outline
• Serializability - concepts and algorithms• One solution: Locking
– 2PL– variations
• Deadlocks– detection– prevention
![Page 32: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/32.jpg)
CMU SCS
32Faloutsos SCS 15-415
Deadlocks
• Deadlock: Cycle of transactions waiting for locks to be released by each other.
• Two ways of dealing with deadlocks:– Deadlock prevention– Deadlock detection
• Many systems just punt and use Timeouts– What are the dangers with this approach?
![Page 33: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/33.jpg)
CMU SCS
33Faloutsos SCS 15-415
Deadlock Detection
• Create a waits-for graph:– Nodes are transactions– Edge from Ti to Tj if Ti is waiting for Tj to
release a lock• Periodically check for cycles in waits-for
graph
![Page 34: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/34.jpg)
CMU SCS
34Faloutsos SCS 15-415
Deadlock Detection (Continued)Example:
T1: S(A), S(D), S(B)T2: X(B) X(C)T3: S(D), S(C),
X(A)T4: X(B)T1 T2
T4 T3
![Page 35: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/35.jpg)
CMU SCS
35Faloutsos SCS 15-415
Another example
T1 T2
T3 T4
• is there a deadlock?• if yes, which xacts are
involved?
![Page 36: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/36.jpg)
CMU SCS
36Faloutsos SCS 15-415
Another example
T1 T2
T3 T4
• now, is there a deadlock?• if yes, which xacts are
involved?
![Page 37: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/37.jpg)
CMU SCS
37Faloutsos SCS 15-415
Deadlock detection
• how often should we run the algo?• how many transactions are typically
involved?
![Page 38: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/38.jpg)
CMU SCS
38Faloutsos SCS 15-415
Deadlock handling
T1 T2
T3 T4
• Q: what to do?
![Page 39: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/39.jpg)
CMU SCS
39Faloutsos SCS 15-415
Deadlock handling
T1 T2
T3 T4
• Q0: what to do?• A: select a ‘victim’ & ‘rollback’
• Q1: which/how to choose?
![Page 40: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/40.jpg)
CMU SCS
40Faloutsos SCS 15-415
Deadlock handling
• Q1: which/how to choose?• A1.1: by age• A1.2: by progress• A1.3: by # items locked already...• A1.4: by # xacts to rollback
• Q2: How far to rollback?
T1 T2
T3 T4
![Page 41: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/41.jpg)
CMU SCS
41Faloutsos SCS 15-415
Deadlock handling
• Q2: How far to rollback?• A2.1: completely• A2.2: minimally
• Q3: Starvation??
T1 T2
T3 T4
![Page 42: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/42.jpg)
CMU SCS
42Faloutsos SCS 15-415
Deadlock handling
• Q3: Starvation??• A3.1: include #rollbacks in victim
selection criterion.T1 T2
T3 T4
![Page 43: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/43.jpg)
CMU SCS
43Faloutsos SCS 15-415
Outline
• Serializability - concepts and algorithms• One solution: Locking
– 2PL– variations
• Deadlocks– detection– prevention
![Page 44: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/44.jpg)
CMU SCS
44Faloutsos SCS 15-415
Deadlock Prevention• Assign priorities based on timestamps (older ->
higher priority)• We only allow ‘old-wait-for-young’• (or only allow ‘young-wait-for-old’)• and rollback violators. Specifically:• Say Ti wants a lock that Tj holds - two policies:
Wait-Die: If Ti has higher priority, Ti waits for Tj; otherwise Ti aborts (ie., old wait for young)
Wound-wait: If Ti has higher priority, Tj aborts; otherwise Ti waits (ie., young wait for old)
![Page 45: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/45.jpg)
CMU SCS
45
Deadlock prevention
Faloutsos SCS 15-415
Wait-Die Wound-WaitTi wants Tj has Ti wants Tj has
![Page 46: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/46.jpg)
CMU SCS
46Faloutsos SCS 15-415
Deadlock Prevention• Q: Why do these schemes guarantee no deadlocks?• A:• Q: When a transaction restarts, what is its (new)
priority?• A:
![Page 47: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/47.jpg)
CMU SCS
47Faloutsos SCS 15-415
Deadlock Prevention• Q: Why do these schemes guarantee no deadlocks?• A: only one ‘type’ of direction allowed.• Q: When a transaction restarts, what is its (new)
priority?• A: its original timestamp. -- Why?
![Page 48: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/48.jpg)
CMU SCS
48Faloutsos SCS 15-415
SQL statement
• usually, conc. control is transparent to the user, but
• LOCK <table-name> [EXCLUSIVE|SHARED]
![Page 49: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/49.jpg)
CMU SCS
49Faloutsos SCS 15-415
Concurrency control - conclusions
• (conflict) serializability <-> correctness• automatically correct interleavings:
– locks + protocol (2PL, 2PLC, ...)– deadlock detection + handling
• (or deadlock prevention)
![Page 50: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/50.jpg)
CMU SCS
50Faloutsos SCS 15-415
Quiz:
• is there a serial schedule (= interleaving) that is not serializable?
• is there a serializable schedule that is not serial?
• can 2PL produce a non-serializable schedule? (assume no deadlocks)
![Page 51: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/51.jpg)
CMU SCS
51Faloutsos SCS 15-415
Quiz - cont’d
• is there a serializable schedule that can not be produced by 2PL?
• a xact obeys 2PL - can it be involved in a non-serializable schedule?
• all xacts obey 2PL - can they end up in a deadlock?
![Page 52: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/52.jpg)
CMU SCS
52Faloutsos SCS 15-415
Quiz - hints:
2PL schedules
serializable schedules
serial sch’s
Q: 2PLC??
![Page 53: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/53.jpg)
CMU SCS
53Faloutsos SCS 15-415
Quiz - hints:
2PL schedules
serializable schedules
serial sch’s2PLC
![Page 54: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/54.jpg)
CMU SCS
Faloutsos SCS 15-415 #54
Lock granularity?
- lock granularity- field? record? page? table?
- Pros and cons?- (Ideally, each transaction should obtain a few
locks)
![Page 55: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/55.jpg)
CMU SCS
Faloutsos SCS 15-415 #55
Multiple granularity
• Eg:
attr1 attr1att
r2
record-n
record2
record1
Table2
Table1
DB
![Page 56: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/56.jpg)
CMU SCS
Faloutsos SCS 15-415 #56
What would you do?
• T1: read Smith’s salary,• while T2: give 10% raise to everybody• what locks should they obtain?
record-n
record2
record1
Table2
Table1
DB
![Page 57: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/57.jpg)
CMU SCS
Faloutsos SCS 15-415 #57
What types of locks?
• X/S locks for leaf level +• ‘intent’ locks, for higher levels
![Page 58: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/58.jpg)
CMU SCS
Faloutsos SCS 15-415 #58
What types of locks?
• X/S locks for leaf level +• ‘intent’ locks, for higher levels• IS: intent to obtain S-lock underneath• IX: intent .... X-lock ...• S: shared lock for this level• X: ex- lock for this level• SIX: shared lock here; + IX
![Page 59: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/59.jpg)
CMU SCS
Faloutsos SCS 15-415 #59
Protocol
- each xact obtains appropriate lock at highest level
- proceeds to desirable lower levels
![Page 60: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/60.jpg)
CMU SCS
Faloutsos SCS 15-415 #60
Multiple Granularity Lock Protocol
• Each Xact: lock root.• To get S or IS lock on a node, must hold at least
IS 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 at least IX on parent node.
• Must release locks in bottom-up order.
![Page 61: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/61.jpg)
CMU SCS
Faloutsos SCS 15-415 #61
Multiple granularity protocol
XSIX
IXSIS
stronger (more privileges)
weaker
![Page 62: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/62.jpg)
CMU SCS
Faloutsos SCS 15-415 #62
Examples – 2 level hierarchy
• T1 scans R, and updates a few tuples: TuplesTables
![Page 63: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/63.jpg)
CMU SCS
Faloutsos SCS 15-415 #63
Examples – 2 level hierarchy
• T1 scans R, and updates a few tuples:• T1 gets an SIX lock on R, then get X lock
on tuples that are updated.
![Page 64: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/64.jpg)
CMU SCS
Faloutsos SCS 15-415 #64
Examples – 2 level hierarchy
• T2: find avg salary of ‘Sales’ employees
![Page 65: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/65.jpg)
CMU SCS
Faloutsos SCS 15-415 #65
Examples – 2 level hierarchy
• T2: find avg salary of ‘Sales’ employees• T2 gets an IS lock on R, and repeatedly gets
an S lock on tuples of R.
![Page 66: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/66.jpg)
CMU SCS
Faloutsos SCS 15-415 #66
Examples – 2 level hierarchy
• T3: sum of salaries of everybody in ‘R’:
![Page 67: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/67.jpg)
CMU SCS
Faloutsos SCS 15-415 #67
Examples – 2 level hierarchy
• T3: sum of salaries of everybody in ‘R’:• T3 gets an S lock on R. • OR, T3 could behave like T2; can
use lock escalation to decide which.– Lock escalation dynamically asks for
coarser-grained locks when too manylow level locks acquired
![Page 68: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/68.jpg)
CMU SCS
Faloutsos SCS 15-415 #68
Multiple granularity
• Very useful in practice• each xact needs only a few locks
![Page 69: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/69.jpg)
CMU SCS
Faloutsos SCS 15-415 #69
Outline
• ...• Locking granularity• Tree locking protocols• Phantoms & predicate locking
![Page 70: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/70.jpg)
CMU SCS
Faloutsos SCS 15-415 #70
Locking in B+ Trees
• What about locking indexes?
![Page 71: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/71.jpg)
CMU SCS
Faloutsos SCS 15-415 #71
Example B+tree
• T1 wants to insert in H• T2 wants to insert in I• why not plain 2PL?
G IHFED
CB
A
........
root
![Page 72: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/72.jpg)
CMU SCS
Faloutsos SCS 15-415 #72
Example B+tree
• T1 wants to insert in H• T2 wants to insert in I• why not plain 2PL?• Because: X/S locks for
too long!
G IHFED
CB
A
........
root
![Page 73: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/73.jpg)
CMU SCS
Faloutsos SCS 15-415 #73
Two main ideas:
• ‘crabbing’: get lock for parent; get lock for child; release lock for parent (if ‘safe’)
• ‘safe’ nodes == nodes that won’t split or merge, ie:– not full (on insertion)– more than half-full (on deletion)
![Page 74: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/74.jpg)
CMU SCS
Faloutsos SCS 15-415 #74
Example B+tree
• T1 wants to insert in H• crabbing:
G IHFED
CB
A
........
root
![Page 75: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/75.jpg)
CMU SCS
Faloutsos SCS 15-415 #75
Example B+tree
• T1 wants to insert in H
G IHFED
CB
A
........
root
![Page 76: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/76.jpg)
CMU SCS
Faloutsos SCS 15-415 #76
Example B+tree
• T1 wants to insert in H• (if ‘B’ is ‘safe’)
G IHFED
CB
A
........
root
![Page 77: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/77.jpg)
CMU SCS
Faloutsos SCS 15-415 #77
Example B+tree
• T1 wants to insert in H• continue ‘crabbing’
G IHFED
CB
A
........
root
![Page 78: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/78.jpg)
CMU SCS
Faloutsos SCS 15-415 #78
A Simple Tree Locking Algorithm: “crabbing”
• Search: Start at root and go down; repeatedly,– S lock child– then unlock parent
• 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.
![Page 79: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/79.jpg)
CMU SCS
Faloutsos SCS 15-415 #79
ExampleROOT A
B
C
D E
F
G H I
20
35
20*
38 44
22* 23*24* 35*36* 38*41* 44*
Do:1) Search 38*2) Delete 38*3) Insert 45*4) Insert 25*
23
![Page 80: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/80.jpg)
CMU SCS
Can we do better?
• Yes [Bayer and Schkolnik]:• Idea: hope that the leaf is ‘safe’, and use S-
locks & crabbing to reach it, and verify• (if false, do previous algo)
Faloutsos SCS 15-415 #80
![Page 81: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/81.jpg)
CMU SCS
Can we do better?
• Yes [Bayer and Schkolnik]:
Faloutsos SCS 15-415 #81
Rudolf Bayer, Mario Schkolnick: Concurrency of Operations on B-Trees. Acta Inf. 9: 1-21 (1977)
![Page 82: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/82.jpg)
CMU SCS
Faloutsos SCS 15-415 #82
A Better Tree Locking Algorithm (From 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.
![Page 83: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/83.jpg)
CMU SCS
Faloutsos SCS 15-415 #83
ExampleROOT A
B
C
D E
F
G H I
20
35
20*
38 44
22* 23*24* 35*36* 38*41* 44*
23
Do:1) Delete 38*2) Insert 25*
![Page 84: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/84.jpg)
CMU SCS
Answers:
1. Delete 38*– S A, S B, U A, S C, U B, X D, U C
2. Insert 25*– S A, S B, U A, S F, U B, X H; U H;– X A, X B, U A, X F, U B, X H
Faloutsos SCS 15-415 #84
![Page 85: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/85.jpg)
CMU SCS
Notice:
• Textbook has a third variation, that uses lock-upgrades (and may lead to deadlocks)
Faloutsos SCS 15-415 #85
![Page 86: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/86.jpg)
CMU SCS
Faloutsos SCS 15-415 #86
Outline
• Locking granularity• Tree locking protocols• Phantoms & predicate locking
![Page 87: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/87.jpg)
CMU SCS
Faloutsos SCS 15-415 #87
Dynamic Databases – The “Phantom” Problem
• so far: only reads and updates – no insertions/deletions
• with insertions/deletions, new problems:
![Page 88: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/88.jpg)
CMU SCS
Faloutsos SCS 15-415 #88
The phantom problem
T1
select max(age) ...where rating=1
T2
insert ... age=96 rating=1select max(age) ...where rating=1
time 71
96
![Page 89: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/89.jpg)
CMU SCS
Faloutsos SCS 15-415 #89
Why?
• because T1 locked only *existing* records – not ones under way!
• Solution?
![Page 90: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/90.jpg)
CMU SCS
Faloutsos SCS 15-415 #90
Solution
theoretical solution: • ‘predicate locking’: e.g., lock all records
(current or incoming) with rating=1– VERY EXPENSIVE
![Page 91: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/91.jpg)
CMU SCS
Faloutsos SCS 15-415 #91
Solution
practical solution:• index locking: if an index (on ‘rating’)
exists, lock the appropriate entries (rating=1 in our case)
• otherwise, lock whole table (and thus block insertions/deletions)
![Page 92: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/92.jpg)
CMU SCS
Faloutsos SCS 15-415 #92
Transaction Support in SQL-92
• SERIALIZABLE – No phantoms, all reads repeatable, no “dirty” (uncommited) reads.
• REPEATABLE READS – phantoms may happen.
• READ COMMITTED – phantoms and unrepeatable reads may happen
• READ UNCOMMITTED – all of them may happen.
recommended
![Page 93: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/93.jpg)
CMU SCS
Faloutsos SCS 15-415 #93
Transaction Support in SQL-92
• SERIALIZABLE : obtains all locks first; plus index locks, plus strict 2PL
• REPEATABLE READS – as above, but no index locks
• READ COMMITTED – as above, but S-locks are released immediately
• READ UNCOMMITTED – as above, but allowing ‘dirty reads’ (no S-locks)
![Page 94: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/94.jpg)
CMU SCS
Faloutsos SCS 15-415 #94
Transaction Support in SQL-92
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY
Defaults:SERIALIZABLE READ WRITE
isolation levelaccess mode
![Page 95: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/95.jpg)
CMU SCS
Faloutsos SCS 15-415 #95
• Multiple granularity locking: leads to few locks, at appropriate levels
• Tree-structured indexes:– ‘crabbing’ and ‘safe nodes’
Summary
![Page 96: Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications](https://reader036.vdocument.in/reader036/viewer/2022062305/5681615a550346895dd0e5c9/html5/thumbnails/96.jpg)
CMU SCS
Faloutsos SCS 15-415 #96
• “phantom problem”, if insertions/deletions– (Predicate locking prevents phantoms)– Index locking, or table locking
Summary