CMU SCS Carnegie Mellon Univ Dept of Computer

  • Slides: 99
Download presentation
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15 -415/615 - DB Applications

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15 -415/615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)

CMU SCS Last Class • • Introduction to Transactions ACID Concurrency Control Crash Recovery

CMU SCS Last Class • • Introduction to Transactions ACID Concurrency Control Crash Recovery Faloutsos/Pavlo CMU SCS 15 -415/615 2

CMU SCS Last Class • For Isolation property, serial execution of transactions is safe

CMU SCS Last Class • For Isolation property, serial execution of transactions is safe but slow – We want to find schedules equivalent to serial execution but allow interleaving. • The way the DBMS does this is with its concurrency control protocol. Faloutsos/Pavlo CMU SCS 15 -415/615 3

CMU SCS Today’s Class • • • Serializability Two-Phase Locking Deadlocks Lock Granularities Locking

CMU SCS Today’s Class • • • Serializability Two-Phase Locking Deadlocks Lock Granularities Locking in B+Trees Faloutsos/Pavlo CMU SCS 15 -415/615 4

CMU SCS Formal Properties of Schedules • • Serial Schedule: A schedule that does

CMU SCS Formal Properties of Schedules • • Serial Schedule: A schedule that does not interleave the actions of different transactions. Equivalent Schedules: For any database state, the effect of executing the first schedule is identical to the effect of executing the second schedule. * (*) no matter what the arithmetic operations are! Faloutsos/Pavlo CMU SCS 15 -415/615 5

CMU SCS Formal Properties of Schedules • Serializable Schedule: A schedule that is equivalent

CMU SCS Formal Properties of Schedules • Serializable Schedule: A schedule that is equivalent to some serial execution of the transactions. • Note: If each transaction preserves consistency, every serializable schedule preserves consistency. Faloutsos/Pavlo CMU SCS 15 -415/615 6

CMU SCS Example T 2 T 1 BEGIN A=A+100 B=B– 100 COMMIT BEGIN A=A*1.

CMU SCS Example T 2 T 1 BEGIN A=A+100 B=B– 100 COMMIT BEGIN A=A*1. 06 B=B*1. 06 COMMIT • Consider two txns: – T 1 transfers $100 from B’s account to A’s – T 2 credits both accounts with 6% interest. • Assume at first A and B each have $1000. Faloutsos/Pavlo CMU SCS 15 -415/615 7

CMU SCS Example • Legal outcomes: – A=1166, B=954 →$2120 – A=1160, B=960 →$2120

CMU SCS Example • Legal outcomes: – A=1166, B=954 →$2120 – A=1160, B=960 →$2120 • The outcome depends on whether T 1 executes before T 2 or vice versa. Faloutsos/Pavlo CMU SCS 15 -415/615 8

CMU SCS Interleaving Example (Good) Schedule T 1 T 2 TIME BEGIN A=A+100 BEGIN

CMU SCS Interleaving Example (Good) Schedule T 1 T 2 TIME BEGIN A=A+100 BEGIN A=A*1. 06 B=B– 100 COMMIT B=B*1. 06 COMMIT ≡ BEGIN A=A+100 B=B– 100 COMMIT A=1166, B=954 Faloutsos/Pavlo BEGIN A=A*1. 06 B=B*1. 06 COMMIT A=1166, B=954 CMU SCS 15 -415/615 9

CMU SCS Interleaving Example (Bad) Schedule T 1 T 2 TIME BEGIN A=A+100 BEGIN

CMU SCS Interleaving Example (Bad) Schedule T 1 T 2 TIME BEGIN A=A+100 BEGIN A=A*1. 06 B=B*1. 06 COMMIT B=B– 100 COMMIT A=1166, B=960 Faloutsos/Pavlo ≢ A=1166, B=954 or A=1160, B=960 The bank lost $6! CMU SCS 15 -415/615 10

CMU SCS Formal Properties of Schedules • There are different levels of serializability: –

CMU SCS Formal Properties of Schedules • There are different levels of serializability: – Conflict Serializability – View Serializability All DBMSs support this. This is harder but allows for more concurrency. Faloutsos/Pavlo CMU SCS 15 -415/615 11

CMU SCS Conflicting Operations • We need a formal notion of equivalence that can

CMU SCS Conflicting Operations • We need a formal notion of equivalence that can be implemented efficiently… – Base it on the notion of “conflicting” operations • Definition: Two operations conflict if: – They are by different transactions, – They are on the same object and at least one of them is a write. Faloutsos/Pavlo CMU SCS 15 -415/615 12

CMU SCS Conflict Serializable Schedules • Two schedules are conflict equivalent iff: – They

CMU SCS Conflict Serializable Schedules • Two schedules are conflict equivalent iff: – They involve the same actions of the same transactions, and – Every pair of conflicting actions is ordered the same way. • Schedule S is conflict serializable if: – S is conflict equivalent to some serial schedule. Faloutsos/Pavlo CMU SCS 15 -415/615 13

CMU SCS Conflict Serializability Intuition • A schedule S is conflict serializable if: –

CMU SCS Conflict Serializability Intuition • A schedule S is conflict serializable if: – You are able to transform S into a serial schedule by swapping consecutive nonconflicting operations of different transactions. Faloutsos/Pavlo CMU SCS 15 -415/615 14

CMU SCS Conflict Serializability Intuition TIME Schedule T 1 T 2 BEGIN R(A) W(A)

CMU SCS Conflict Serializability Intuition TIME Schedule T 1 T 2 BEGIN R(A) W(A) R(B) W(B) COMMIT Serial Schedule T 1 T 2 BEGIN R(A) W(A) ≡ BEGIN R(A) W(A) R(B) W(B) COMMIT Faloutsos/Pavlo CMU SCS 15 -415/615 BEGIN R(A) W(A) R(B) W(B) COMMIT 15

CMU SCS Conflict Serializability Intuition Schedule T 1 T 2 TIME BEGIN R(A) W(A)

CMU SCS Conflict Serializability Intuition Schedule T 1 T 2 TIME BEGIN R(A) W(A) COMMIT Faloutsos/Pavlo Serial Schedule T 1 T 2 COMMIT ≢ BEGIN R(A) W(A) COMMIT CMU SCS 15 -415/615 BEGIN R(A) W(A) COMMIT 16

CMU SCS Serializability • Q: Are there any faster algorithms to figure this out

CMU SCS Serializability • Q: Are there any faster algorithms to figure this out other than transposing operations? Faloutsos/Pavlo CMU SCS 15 -415/615 17

CMU SCS Dependency Graphs • One node per txn. • Edge from Ti to

CMU SCS Dependency Graphs • One node per txn. • Edge from Ti to Tj if: Ti Tj – An operation Oi of Ti conflicts with an operation Oj of Tj and – Oi appears earlier in the schedule than Oj. • Also known as a “precedence graph” Faloutsos/Pavlo CMU SCS 15 -415/615 18

CMU SCS Dependency Graphs • Theorem: A schedule is conflict serializable if and only

CMU SCS Dependency Graphs • Theorem: A schedule is conflict serializable if and only if its dependency graph is acyclic. Faloutsos/Pavlo CMU SCS 15 -415/615 19

CMU SCS Example #1 Schedule T 1 T 2 BEGIN R(A) W(A) TIME Faloutsos/Pavlo

CMU SCS Example #1 Schedule T 1 T 2 BEGIN R(A) W(A) TIME Faloutsos/Pavlo A BEGIN T 1 R(A) W(A) R(B) W(B) COMMIT Dependency Graph T 2 B The cycle in the graph reveals the problem. The output of T 1 depends on T 2, and vice-versa. CMU SCS 15 -415/615 20

CMU SCS Example #2 – Lost Update Schedule T 1 T 2 BEGIN R(A)

CMU SCS Example #2 – Lost Update Schedule T 1 T 2 BEGIN R(A) A = A-1 Dependency Graph A BEGIN T 1 TIME R(A) A = A-1 W(A) COMMIT T 2 A W(A) COMMIT Faloutsos/Pavlo CMU SCS 15 -415/615 21

CMU SCS Example #3 – Threesome Schedule T 1 T 2 TIME BEGIN R(A)

CMU SCS Example #3 – Threesome Schedule T 1 T 2 TIME BEGIN R(A) W(A) R(B) W(B) COMMIT Faloutsos/Pavlo BEGIN R(B) W(B) COMMIT Dependency Graph T 3 BEGIN R(A) W(A) COMMIT T 1 B T 2 A T 3 CMU SCS 15 -415/615 22

CMU SCS Example #3 – Threesome • Q: Is this equivalent to a serial

CMU SCS Example #3 – Threesome • Q: Is this equivalent to a serial execution? • A: Yes (T 2, T 1, T 3) – Notice that T 3 should go after T 2, although it starts before it! Faloutsos/Pavlo CMU SCS 15 -415/615 23

CMU SCS Example #4 – Inconsistent Analysis TIME Schedule T 1 T 2 BEGIN

CMU SCS Example #4 – Inconsistent Analysis TIME Schedule T 1 T 2 BEGIN R(A) A = A-10 W(A) R(B) B = B+10 W(B) COMMIT Faloutsos/Pavlo Dependency Graph A BEGIN T 1 R(A) sum = A R(B) sum += B ECHO(sum) COMMIT T 2 B Is it possible to create a schedule similar to this that is “correct” but still not conflict serializable? CMU SCS 15 -415/615 24

CMU SCS Example #4 – Inconsistent Analysis TIME Schedule T 1 T 2 BEGIN

CMU SCS Example #4 – Inconsistent Analysis TIME Schedule T 1 T 2 BEGIN R(A) A = A-10 W(A) R(B) B = B+10 W(B) COMMIT Faloutsos/Pavlo Dependency Graph A BEGIN T 1 R(A) if(A>0): sum = A cnt++ R(B) if(B>0): sum += B cnt++ ECHO(cnt) COMMIT T 2 B T 2 counts the number of active accounts. CMU SCS 15 -415/615 25

CMU SCS View Serializability • Alternative (weaker) notion of serializability. • Schedules S 1

CMU SCS View Serializability • Alternative (weaker) notion of serializability. • Schedules S 1 and S 2 are view equivalent if: – If T 1 reads initial value of A in S 1, then T 1 also reads initial value of A in S 2. – If T 1 reads value of A written by T 2 in S 1, then T 1 also reads value of A written by T 2 in S 2. – If T 1 writes final value of A in S 1, then T 1 also writes final value of A in S 2. Faloutsos/Pavlo CMU SCS 15 -415/615 26

CMU SCS View Serializability Schedule T 1 T 2 T 3 TIME BEGIN R(A)

CMU SCS View Serializability Schedule T 1 T 2 T 3 TIME BEGIN R(A) BEGIN W(A) Dependency Graph A T 1 A BEGIN W(A) COMMIT Faloutsos/Pavlo COMMIT W(A) COMMIT CMU SCS 15 -415/615 A A T 3 T 2 A 27

CMU SCS View Serializability Schedule T 1 T 2 T 3 TIME BEGIN R(A)

CMU SCS View Serializability Schedule T 1 T 2 T 3 TIME BEGIN R(A) BEGIN W(A) COMMIT Schedule T 1 T 2 T 3 COMMIT W(A) COMMIT ≡ VIEW BEGIN R(A) W(A) COMMIT Allows all conflict serializable schedules + “blind writes” Faloutsos/Pavlo CMU SCS 15 -415/615 BEGIN W(A) COMMIT 28

CMU SCS Serializability • View Serializability allows (slightly) more schedules than Conflict Serializability does.

CMU SCS Serializability • View Serializability allows (slightly) more schedules than Conflict Serializability does. – But 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) Faloutsos/Pavlo CMU SCS 15 -415/615 29

CMU SCS Serializability • In practice, Conflict Serializability is what gets used, because it

CMU SCS Serializability • In practice, Conflict Serializability is what gets used, because it can be enforced efficiently. – To allow more concurrency, some special cases get handled separately, such as for travel reservations, etc. Faloutsos/Pavlo CMU SCS 15 -415/615 30

CMU SCS Schedules All Schedules View Serializable Conflict Serializable Serial Faloutsos/Pavlo 15 -415/615 31

CMU SCS Schedules All Schedules View Serializable Conflict Serializable Serial Faloutsos/Pavlo 15 -415/615 31

CMU SCS Today’s Class • • • Serializability Two-Phase Locking Deadlocks Lock Granularities Locking

CMU SCS Today’s Class • • • Serializability Two-Phase Locking Deadlocks Lock Granularities Locking in B+Trees Faloutsos/Pavlo CMU SCS 15 -415/615 32

CMU SCS Executing with Locks TIME T 1 T 2 BEGIN X-LOCK(A) R(A) W(A)

CMU SCS Executing with Locks TIME T 1 T 2 BEGIN X-LOCK(A) R(A) W(A) UNLOCK(A) Granted (T 1→A) Released (T 1→A) BEGIN X-LOCK(A) W(A) UNLOCK(A) S-LOCK(A) R(A) UNLOCK(A) COMMIT Faloutsos/Pavlo Lock Manager CMU SCS 15 -415/615 Granted (T 2→A) Released (T 2→A) Granted (T 1→A) Released (T 1→A) 33

CMU SCS Two-Phase Locking • Phase 1: Growing – Each txn requests the locks

CMU SCS Two-Phase Locking • Phase 1: Growing – Each txn requests the locks that it needs from the DBMS’s lock manager. – The lock manager grants/denies lock requests. • Phase 2: Shrinking – The txn is allowed to only release locks that it previously acquired. It cannot acquire new locks. Faloutsos/Pavlo CMU SCS 15 -415/615 34

CMU SCS Two-Phase Locking • The txn is not allowed to acquire/upgrade locks after

CMU SCS Two-Phase Locking • The txn is not allowed to acquire/upgrade locks after the growing phase finishes. Transaction Lifetime Growing Phase Faloutsos/Pavlo Shrinking Phase TIME CMU SCS 15 -415/615 35

CMU SCS Two-Phase Locking • The txn is not allowed to acquire/upgrade locks after

CMU SCS Two-Phase Locking • The txn is not allowed to acquire/upgrade locks after the growing phase finishes. Transaction Lifetime 2 PL Violation! Growing Phase Faloutsos/Pavlo Shrinking Phase TIME CMU SCS 15 -415/615 36

CMU SCS Executing with 2 PL TIME T 1 T 2 BEGIN X-LOCK(A) R(A)

CMU SCS Executing with 2 PL TIME T 1 T 2 BEGIN X-LOCK(A) R(A) W(A) Granted (T 1→A) BEGIN X-LOCK(A) R(A) UNLOCK(A) COMMIT Denied! Released (T 1→A) W(A) UNLOCK(A) COMMIT Faloutsos/Pavlo Lock Manager CMU SCS 15 -415/615 Granted (T 2→A) Released (T 2→A) 37

CMU SCS Two-Phase Locking • 2 PL on its own is sufficient to guarantee

CMU SCS Two-Phase Locking • 2 PL on its own is sufficient to guarantee conflict serializability (i. e. , schedules whose precedence graph is acyclic), but, it is subject to cascading aborts. Growing Phase Faloutsos/Pavlo Shrinking Phase CMU SCS 15 -415/615 TIME 38

CMU SCS 2 PL – Cascading Aborts TIME Schedule T 1 T 2 BEGIN

CMU SCS 2 PL – Cascading Aborts TIME Schedule T 1 T 2 BEGIN X-LOCK(A) X-LOCK(B) R(A) W(A) UNLOCK(A) X-LOCK(A) R(A) W(A) R(B) ⋮ W(B) ABORT Faloutsos/Pavlo This is a permissible schedule in 2 PL, but we have to abort T 2 too. This is all wasted work! CMU SCS 15 -415/615 39

CMU SCS 2 PL Observations • There are schedules that are serializable but would

CMU SCS 2 PL Observations • There are schedules that are serializable but would not be allowed by 2 PL. • Locking limits concurrency. • May lead to deadlocks. • May still have “dirty reads” – Solution: Strict 2 PL Faloutsos/Pavlo CMU SCS 15 -415/615 40

CMU SCS Strict Two-Phase Locking • The txn is not allowed to acquire/upgrade locks

CMU SCS Strict Two-Phase Locking • The txn is not allowed to acquire/upgrade locks after the growing phase finishes. • Allows only conflict serializable schedules, but it is actually stronger than needed. Release all locks at end of txn. Growing Phase Faloutsos/Pavlo Shrinking Phase CMU SCS 15 -415/615 TIME 41

CMU SCS Strict Two-Phase Locking • A schedule is strict if a value written

CMU SCS Strict Two-Phase Locking • A schedule is strict if a value written by a txn is not read or overwritten by other txns until that txn finishes. • Advantages: – Recoverable. – Do not require cascading aborts. – Aborted txns can be undone by just restoring original values of modified tuples. Faloutsos/Pavlo CMU SCS 15 -415/615 42

CMU SCS Examples • T 1: Move $50 from Andy’s account to his bookie’s

CMU SCS Examples • T 1: Move $50 from Andy’s account to his bookie’s account. • T 2: Compute the total amount in all accounts and return it to the application. • Legend: – A → Andy’s account. – B → The bookie’s account. Faloutsos/Pavlo CMU SCS 15 -415/615 43

CMU SCS Non-2 PL Example TIME T 1 T 2 BEGIN X-LOCK(A) R(A) S-LOCK(A)

CMU SCS Non-2 PL Example TIME T 1 T 2 BEGIN X-LOCK(A) R(A) S-LOCK(A) A=A-50 W(A) UNLOCK(A) R(A) UNLOCK(A) S-LOCK(B) X-LOCK(B) R(B) UNLOCK(B) R(B) ECHO(A+B) B=B+50 COMMIT W(B) UNLOCK(B) COMMIT Initial State A=100, B=100 T 2 Output 150 44

CMU SCS 2 PL Example TIME T 1 T 2 BEGIN X-LOCK(A) R(A) S-LOCK(A)

CMU SCS 2 PL Example TIME T 1 T 2 BEGIN X-LOCK(A) R(A) S-LOCK(A) A=A-50 W(A) X-LOCK(B) UNLOCK(A) R(A) S-LOCK(B) R(B) B=B+50 W(B) UNLOCK(B) COMMIT R(B) UNLOCK(A) UNLOCK(B) ECHO(A+B) COMMIT Initial State A=100, B=100 T 2 Output 200 45

CMU SCS Strict 2 PL Example TIME T 1 BEGIN X-LOCK(A) R(A) A=A-50 W(A)

CMU SCS Strict 2 PL Example TIME T 1 BEGIN X-LOCK(A) R(A) A=A-50 W(A) X-LOCK(B) R(B) B=B+50 W(B) UNLOCK(A) UNLOCK(B) COMMIT T 2 BEGIN S-LOCK(A) Initial State A=100, B=100 T 2 Output 200 R(A) S-LOCK(B) R(B) ECHO(A+B) UNLOCK(A) UNLOCK(B) COMMIT 46

CMU SCS Strict Two-Phase Locking • Txns hold all of their locks until commit.

CMU SCS Strict Two-Phase Locking • Txns hold all of their locks until commit. • Good: – Avoids “dirty reads” etc • Bad: – Limits concurrency even more – And still may lead to deadlocks Faloutsos/Pavlo CMU SCS 15 -415/615 47

CMU SCS Schedules All Schedules View Serializable Conflict Serializable Avoid Cascading Abort Faloutsos/Pavlo Strict

CMU SCS Schedules All Schedules View Serializable Conflict Serializable Avoid Cascading Abort Faloutsos/Pavlo Strict 2 PL Serial 15 -415/615 48

CMU SCS Today’s Class • • • Serializability Two-Phase Locking Deadlocks Lock Granularities Locking

CMU SCS Today’s Class • • • Serializability Two-Phase Locking Deadlocks Lock Granularities Locking in B+Trees Faloutsos/Pavlo CMU SCS 15 -415/615 49

CMU SCS Two-Phase Locking • 2 PL seems to work well. • Is that

CMU SCS Two-Phase Locking • 2 PL seems to work well. • Is that enough? Can we just go home now? Faloutsos/Pavlo CMU SCS 15 -415/615 50

CMU SCS Shit Just Got Real TIME T 1 Faloutsos/Pavlo T 2 BEGIN X-LOCK(A)

CMU SCS Shit Just Got Real TIME T 1 Faloutsos/Pavlo T 2 BEGIN X-LOCK(A) S-LOCK(B) R(B) S-LOCK(A) R(A) X-LOCK(B) CMU SCS 15 -415/615 Lock Manager Granted (T 1→A) Granted (T 2→B) Denied! 51

CMU SCS Deadlocks • Deadlock: Cycle of transactions waiting for locks to be released

CMU SCS 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? Faloutsos/Pavlo CMU SCS 15 -415/615 52

CMU SCS Deadlock Detection • The DBMS creates a waits-for graph: – Nodes are

CMU SCS Deadlock Detection • The DBMS creates a waits-for graph: – Nodes are transactions – Edge from Ti to Tj if Ti is waiting for Tj to release a lock • The system periodically check for cycles in waits-for graph. Faloutsos/Pavlo CMU SCS 15 -415/615 53

CMU SCS Deadlock Detection TIME Schedule T 1 T 2 Waits-for Graph T 3

CMU SCS Deadlock Detection TIME Schedule T 1 T 2 Waits-for Graph T 3 BEGIN S-LOCK(A) S-LOCK(D) X-LOCK(B) S-LOCK(C) S-LOCK(B) T 1 T 2 T 3 X-LOCK(C) X-LOCK(A) Faloutsos/Pavlo CMU SCS 15 -415/615 54

CMU SCS Deadlock Detection • How often should we run the algorithm? • How

CMU SCS Deadlock Detection • How often should we run the algorithm? • How many txns are typically involved? • What do we do when we find a deadlock? Faloutsos/Pavlo CMU SCS 15 -415/615 55

CMU SCS Deadlock Handling • Q: What do we do? • A: Select a

CMU SCS Deadlock Handling • Q: What do we do? • A: Select a “victim” and rollback it back to break the deadlock. Faloutsos/Pavlo CMU SCS 15 -415/615 56

CMU SCS Deadlock Handling • Q: Which one do we choose? • A: It

CMU SCS Deadlock Handling • Q: Which one do we choose? • A: It depends… – By age (lowest timestamp) – By progress (least/most queries executed) – By the # of items already locked – By the # of txns that we have to rollback with it • We also should consider the # of times a txn has been restarted in the past. Faloutsos/Pavlo CMU SCS 15 -415/615 57

CMU SCS Deadlock Handling • Q: How far do we rollback? • A: It

CMU SCS Deadlock Handling • Q: How far do we rollback? • A: It depends… – Completely – Minimally (i. e. , just enough to release locks) Faloutsos/Pavlo CMU SCS 15 -415/615 58

CMU SCS Deadlock Prevention • When a txn tries to acquire a lock that

CMU SCS Deadlock Prevention • When a txn tries to acquire a lock that is held by another txn, kill one of them to prevent a deadlock. • No waits-for graph or detection algorithm. Faloutsos/Pavlo CMU SCS 15 -415/615 59

CMU SCS Deadlock Prevention • Assign priorities based on timestamps: – Older → higher

CMU SCS Deadlock Prevention • Assign priorities based on timestamps: – Older → higher priority (e. g. , T 1 > T 2) • Two different prevention policies: – Wait-Die: If T 1 has higher priority, T 1 waits for T 2; otherwise T 1 aborts (“old wait for young”) – Wound-Wait: If T 1 has higher priority, T 2 aborts; otherwise T 1 waits (“young wait for old”) Faloutsos/Pavlo CMU SCS 15 -415/615 60

CMU SCS Deadlock Prevention T 1 T 2 BEGIN X-LOCK(A) ⋮ ⋮ T 1

CMU SCS Deadlock Prevention T 1 T 2 BEGIN X-LOCK(A) ⋮ ⋮ T 1 Wound-Wait T 1 waits T 2 aborted Wait-Die Wound-Wait T 2 aborted T 2 waits T 2 BEGIN X-LOCK(A) ⋮ Faloutsos/Pavlo Wait-Die CMU SCS 15 -415/615 61

CMU SCS Deadlock Prevention • Q: Why do these schemes guarantee no deadlocks? •

CMU SCS 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? Faloutsos/Pavlo CMU SCS 15 -415/615 62

CMU SCS Today’s Class • • • Serializability Two-Phase Locking Deadlocks Lock Granularities Locking

CMU SCS Today’s Class • • • Serializability Two-Phase Locking Deadlocks Lock Granularities Locking in B+Trees Faloutsos/Pavlo CMU SCS 15 -415/615 63

CMU SCS Lock Granularities • When we say that a txn acquires a “lock”,

CMU SCS Lock Granularities • When we say that a txn acquires a “lock”, what does that actually mean? – On a field? Record? Page? Table? • Ideally, each txn should obtain fewest number of locks that is needed… Faloutsos/Pavlo CMU SCS 15 -415/615 64

CMU SCS Database Lock Hierarchy Database Table 1 Tuple 1 Attr 1 Faloutsos/Pavlo Table

CMU SCS Database Lock Hierarchy Database Table 1 Tuple 1 Attr 1 Faloutsos/Pavlo Table 2 … Tuple 2 Attr 2 … CMU SCS 15 -415/615 Tuple n Attr n 65

CMU SCS Example • T 1: Get the balance of Andy’s shady offshore bank

CMU SCS Example • T 1: Get the balance of Andy’s shady offshore bank account. • T 2: Increase all account balances by 1%. • Q: What locks should they obtain? Faloutsos/Pavlo CMU SCS 15 -415/615 66

CMU SCS Example • Q: What locks should they obtain? • A: Multiple –

CMU SCS Example • Q: What locks should they obtain? • A: Multiple – Exclusive + Shared for leafs of lock tree. – Special Intention locks for higher levels Faloutsos/Pavlo CMU SCS 15 -415/615 67

CMU SCS Intention Locks • Intention locks allow a higher level node to be

CMU SCS Intention Locks • Intention locks allow a higher level node to be locked in S or X mode without having to check all descendent nodes. • If a node is in an intention mode, then explicit locking is being done at a lower level in the tree. Faloutsos/Pavlo CMU SCS 15 -415/615 68

CMU SCS Intention Locks • Intention-Shared (IS): Indicates explicit locking at a lower level

CMU SCS Intention Locks • Intention-Shared (IS): Indicates explicit locking at a lower level with shared locks. • Intention-Exclusive (IX): Indicates locking at lower level with exclusive or shared locks. Faloutsos/Pavlo CMU SCS 15 -415/615 69

CMU SCS Intention Locks • Shared+Intention-Exclusive (SIX): The subtree rooted by that node is

CMU SCS Intention Locks • Shared+Intention-Exclusive (SIX): The subtree rooted by that node is locked explicitly in shared mode and explicit locking is being done at a lower level with exclusive-mode locks. Faloutsos/Pavlo CMU SCS 15 -415/615 70

CMU SCS Compatibility Matrix T 2 Wants T 1 Holds IS IX S SIX

CMU SCS Compatibility Matrix T 2 Wants T 1 Holds IS IX S SIX X Faloutsos/Pavlo IS IX S SIX X ✔ ✔ X X X ✔ X X X X X CMU SCS 15 -415/615 71

CMU SCS Multiple Granularity Protocol Privileges Weaker IS IX S SIX X Stronger Faloutsos/Pavlo

CMU SCS Multiple Granularity Protocol Privileges Weaker IS IX S SIX X Stronger Faloutsos/Pavlo CMU SCS 15 -415/615 72

CMU SCS Locking Protocol • Each txn obtains appropriate lock at highest level of

CMU SCS Locking Protocol • Each txn obtains appropriate lock at highest level of the database hierarchy. • To get S or IS lock on a node, the txn must hold at least IS on parent node. – What if txn holds SIX on parent? S on parent? • To get X, IX, or SIX on a node, must hold at least IX on parent node. Faloutsos/Pavlo CMU SCS 15 -415/615 73

CMU SCS Example – Two-level Hierarchy Read a single record in R. Update a

CMU SCS Example – Two-level Hierarchy Read a single record in R. Update a single record in R. T 1 T 2 T 1 IS T 2 T 1 IX S Tuple 1 Table R T 2 X Tuple 2 … Read Faloutsos/Pavlo Tuple n Write CMU SCS 15 -415/615 74

CMU SCS Example – Threesome • Assume three txns execute at same time: –

CMU SCS Example – Threesome • Assume three txns execute at same time: – T 1: Scan R and update a few tuples. – T 2: Scan a portion of tuples in R. – T 3: Scan all tuples in R. Table R Tuple 1 Faloutsos/Pavlo Tuple 2 … CMU SCS 15 -415/615 Tuple n 75

CMU SCS Example – Threesome Scan R and update a few tuples. Scan all

CMU SCS Example – Threesome Scan R and update a few tuples. Scan all tuples in R. Scan a portion of tuples in R. T 1 T 2 IS S Faloutsos/Pavlo T 2 T 3 SIX T 2 T 3 S Table R T 1 X Tuple 1 Tuple 2 Read … CMU SCS 15 -415/615 Tuple n Read+Write Read 76

CMU SCS Example – Threesome • T 1: Get an SIX lock on R,

CMU SCS Example – Threesome • T 1: Get an SIX lock on R, then get X lock on tuples that are updated. • T 2: Get an IS lock on R, and repeatedly get an S lock on tuples of R. • T 3: Two choices: – T 3 gets an S lock on R. – OR, T 3 could behave like T 2; can use lock escalation to decide which. Faloutsos/Pavlo CMU SCS 15 -415/615 77

CMU SCS Lock Escalation • Lock escalation dynamically asks for coarser-grained locks when too

CMU SCS Lock Escalation • Lock escalation dynamically asks for coarser-grained locks when too many low level locks acquired. • Reduces the number of requests that the lock manager has to process. Faloutsos/Pavlo CMU SCS 15 -415/615 78

CMU SCS Multiple Lock Granularities • Useful in practice as each txn only needs

CMU SCS Multiple Lock Granularities • Useful in practice as each txn only needs a few locks. • Intention locks help improve concurrency: – Intention-Shared (IS): Intent to get S lock(s) at finer granularity. – Intention-Exclusive (IX): Intent to get X lock(s) at finer granularity. – Shared+Intention-Exclusive (SIX): Like S and IX at the same time. Faloutsos/Pavlo CMU SCS 15 -415/615 79

CMU SCS Today’s Class • • • Serializability Two-Phase Locking Deadlocks Lock Granularities Locking

CMU SCS Today’s Class • • • Serializability Two-Phase Locking Deadlocks Lock Granularities Locking in B+Trees Faloutsos/Pavlo CMU SCS 15 -415/615 80

CMU SCS Locking in B+Trees • Q: What about locking indexes? • A: They

CMU SCS Locking in B+Trees • Q: What about locking indexes? • A: They are not quite like other database elements so we can treat them differently: – It’s okay to have non-serializable concurrent access to an index as long as the accuracy of the index is maintained. Faloutsos/Pavlo CMU SCS 15 -415/615 81

CMU SCS Example T 1 • • T 1 wants to insert in H

CMU SCS Example T 1 • • T 1 wants to insert in H T 2 wants to insert in I Q: Why not plain 2 PL? A: Because txns have to hold on to their locks for too long! T 1 CMU SCS 15 -415/615 X A X B C X T 1 G Faloutsos/Pavlo root D X E H F . . . I . . . 82

CMU SCS Lock Crabbing • Improves concurrency for B+Trees. • Get lock for parent;

CMU SCS Lock Crabbing • Improves concurrency for B+Trees. • Get lock for parent; get lock for child; release lock for parent if “safe”. • Safe Nodes: Any node that won’t split or merge when updated. – Not full (on insertion) – More than half-full (on deletion) Faloutsos/Pavlo CMU SCS 15 -415/615 83

CMU SCS Lock Crabbing • Search: Start at root and go down; repeatedly, –

CMU SCS Lock 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. Faloutsos/Pavlo CMU SCS 15 -415/615 84

CMU SCS Example #1 – Search 38 S A 20 S 10 It’s safe

CMU SCS Example #1 – Search 38 S A 20 S 10 It’s safe to release the lock on A. 6 B 35 12 23 S F 38 44 C S 3 4 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44 G Faloutsos/Pavlo CMU SCS 15 -415/615 H I D E 85

CMU SCS Example #2 – Delete 38 X 20 A X 10 We may

CMU SCS Example #2 – Delete 38 X 20 A X 10 We may need to coalesce B, so we can’t release the lock on 12 23 A. 6 3 4 35 X F B 38 44 C We know that C will not X need to merge with F, so 6 9 10 11 12 13 20 22 23 31 35 36 38 41 44 it’s safe to release A+B. G H I D E Faloutsos/Pavlo CMU SCS 15 -415/615 86

CMU SCS Example #3 – Insert 45 X 20 A X 10 35 We

CMU SCS Example #3 – Insert 45 X 20 A X 10 35 We know that if C needs to split, B has room so it’s A. 12 safe to release 23 6 X F B 38 44 C X 3 4 6 9 10 11 12 13 Faloutsos/Pavlo 20 22 23 31 35 36 38 41 44 45 room so. I it won’t GE has H D split, so we can CMU SCS 15 -415/615 release B+C. E 87

CMU SCS Example #4 – Insert 25 X A 20 X 10 B 35

CMU SCS Example #4 – Insert 25 X A 20 X 10 B 35 X 6 12 23 31 F 38 44 C X 3 4 6 9 10 11 12 13 20 22 23 31 25 35 36 38 41 44 G H We need to split H so we need to keep the lock on 31 Faloutsos/Pavlo CMU SCS 15 -415/615 its parent node. I D E 88

CMU SCS Problems • Q: What was the first step that all of the

CMU SCS Problems • Q: What was the first step that all of the update examples did on the B+Tree? Delete 38 X 20 Faloutsos/Pavlo Insert 45 A X 20 CMU SCS 15 -415/615 Insert 25 A X A 20 89

CMU SCS Problems • Q: What was the first step that all of the

CMU SCS Problems • Q: What was the first step that all of the update examples did on the B+Tree? • A: Locking the root every time becomes a bottleneck with higher concurrency. • Can we do better? Faloutsos/Pavlo CMU SCS 15 -415/615 90

CMU SCS Better Tree Locking Algorithm • Main Idea: – Assume that the leaf

CMU SCS Better Tree Locking Algorithm • Main Idea: – Assume that the leaf is ‘safe’, and use S-locks & crabbing to reach it, and verify. – If leaf is not safe, then do previous algorithm. • Rudolf Bayer, Mario Schkolnick: Concurrency of Operations on B-Trees. Acta Inf. 9: 1 -21 (1977) Faloutsos/Pavlo CMU SCS 15 -415/615 91

CMU SCS Example #2 – Delete 38 S 20 A S 10 6 35

CMU SCS Example #2 – Delete 38 S 20 A S 10 6 35 12 23 S F B 38 44 C X 3 4 6 9 10 11 12 13 Faloutsos/Pavlo 20 22 23 31 35 36 38 41 44 G H I D D will not need to CMU SCS 15 -415/615 coalesce, so we’re safe! E 92

CMU SCS Example #4 – Insert 25 S A 20 S 10 B 35

CMU SCS Example #4 – Insert 25 S A 20 S 10 B 35 S 6 12 23 31 F 38 44 C X 3 4 6 9 10 11 12 13 20 22 23 31 25 35 36 38 41 44 G H We need to split H so we have to restart and re. Faloutsos/Pavlo CMU SCS 15 -415/615 execute like before. I D E 93

CMU SCS Better Tree Locking Algorithm • Search: Same as before. • Insert/Delete: –

CMU SCS Better Tree Locking Algorithm • Search: Same 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 txn 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. Faloutsos/Pavlo CMU SCS 15 -415/615 94

CMU SCS Additional Points • Q: Which order to release locks in multiplegranularity locking?

CMU SCS Additional Points • Q: Which order to release locks in multiplegranularity locking? • A: From the bottom up • Q: Which order to release locks in treelocking? • A: As early as possible to maximize concurrency. Faloutsos/Pavlo CMU SCS 15 -415/615 95

CMU SCS Locking in Practice • You typically don’t set locks manually. • Sometimes

CMU SCS Locking in Practice • You typically don’t set locks manually. • Sometimes you will need to provide the DBMS with hints to help it to improve concurrency. • Also useful for doing major changes. Faloutsos/Pavlo CMU SCS 15 -415/615 96

CMU SCS LOCK TABLE Postgres LOCK TABLE <table> IN <mode> MODE; My. SQL LOCK

CMU SCS LOCK TABLE Postgres LOCK TABLE <table> IN <mode> MODE; My. SQL LOCK TABLE <table> <mode>; • Explicitly locks a table. • Not part of the SQL standard. – Postgres Modes: SHARE, EXCLUSIVE – My. SQL Modes: READ, WRITE Faloutsos/Pavlo CMU SCS 15 -415/615 97

CMU SCS SELECT. . . FOR UPDATE SELECT * FROM <table> WHERE <qualification> FOR

CMU SCS SELECT. . . FOR UPDATE SELECT * FROM <table> WHERE <qualification> FOR UPDATE; • Perform a select and then sets an exclusive lock on the matching tuples. • Can also set shared locks: – Postgres: FOR SHARE – My. SQL: LOCK IN SHARE MODE Faloutsos/Pavlo CMU SCS 15 -415/615 98

CMU SCS Concurrency Control Summary • Conflict Serializability ↔ Correctness • Automatically correct interleavings:

CMU SCS Concurrency Control Summary • Conflict Serializability ↔ Correctness • Automatically correct interleavings: – Locks + protocol (2 PL, S 2 PL. . . ) – Deadlock detection + handling – Deadlock prevention Faloutsos/Pavlo CMU SCS 15 -415/615 99