CMU SCS Carnegie Mellon Univ. Dept. of Computer Science 15-415 - Database Applications Concurrency Control, II (R&G ch. 17)

Slides:



Advertisements
Similar presentations
Database Systems (資料庫系統)
Advertisements

1 Concurrency Control Chapter Conflict Serializable Schedules  Two actions are in conflict if  they operate on the same DB item,  they belong.
1 Lecture 11: Transactions: Concurrency. 2 Overview Transactions Concurrency Control Locking Transactions in SQL.
Transaction Management: Concurrency Control CS634 Class 17, Apr 7, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Concurrency Control Amol Deshpande CMSC424. Approach, Assumptions etc.. Approach  Guarantee conflict-serializability by allowing certain types of concurrency.
4/16/20151 PSU’s CS Concurrency Control and Recovery (only for DBs with updates…..!)- Review  Concurrency Control  Transaction  ACID  Isolation.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17 Sections
Concurrency Control Part 2 R&G - Chapter 17 The sequel was far better than the original! -- Nobody.
V. Megalooikonomou Concurrency Control – Deadlocks (based on slides by C. Faloutsos at CMU and on notes by Silberchatz,Korth, and Sudarshan) Temple University.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#22: Concurrency Control – Part 2 (R&G.
Transaction Overview and Concurrency Control Chapters 16 & 17.
ICS 421 Spring 2010 Transactions & Concurrency Control (i) Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa.
Concurrency Control and Recovery In real life: users access the database concurrently, and systems crash. Concurrent access to the database also improves.
Concurrency Control R &G - Chapter 19 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
©Silberschatz, Korth and Sudarshan16.1Database System Concepts 3 rd Edition Chapter 16: Concurrency Control Lock-Based Protocols Timestamp-Based Protocols.
Concurrency Control R&G - Chapter 17 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
Concurrency Control. Example Schedules Constraint: The sum of A+B must be the same Before: After: T1 read(A) A = A -50 write(A) read(B)
1 Concurrency Control. 2 Locking: A Technique for C. C. Concurrency control usually done via locking. Lock info maintained by a “lock manager”: –Stores.
Concurrency Control II R &G - Chapter 17 Lecture 20 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17.
Final Exam Review Last Lecture R&G - All Chapters Covered The end crowns all, And that old common arbitrator, Time, Will one day end it. William Shakespeare.
©Silberschatz, Korth and Sudarshan16.1Database System Concepts 3 rd Edition Chapter 16: Concurrency Control Lock-Based Protocols Timestamp-Based Protocols.
Concurrency Control R &G - Chapter 19 Smile, it is the key that fits the lock of everybody's heart. Anthony J. D'Angelo, The College Blue Book.
Concurrency Control. General Overview Relational model - SQL  Formal & commercial query languages Functional Dependencies Normalization Transaction Processing.
Transactions Introduction and Concurrency Control Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein for some slides.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#22: Concurrency Control – Part 2 (R&G.
Chapter 181 Chapter 18: Concurrency Control (Slides by Hector Garcia-Molina,
15.1Database System Concepts - 6 th Edition Chapter 15: Concurrency Control Lock-Based Protocols The Two-Phase Locking Protocol Graph-Based Protocols #Deadlock.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Concurrency Control Chapter 17 Modified by Donghui Zhang.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)
Concurrency Control R &G - Chapter 19. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)
V. Megalooikonomou Concurrency control (based on slides by C. Faloutsos at CMU and on notes by Silberchatz,Korth, and Sudarshan) Temple University – CIS.
Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Concurrency control.
Chapter 11 Concurrency Control. Lock-Based Protocols  A lock is a mechanism to control concurrent access to a data item  Data items can be locked in.
Concurrency control. Lock-based protocols One way to ensure serializability is to require the data items be accessed in a mutually exclusive manner One.
Chapter 15 Concurrency Control Yonsei University 1 st Semester, 2015 Sanghyun Park.
Concurrency Control Concurrency Control By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
TRANSACTION MANAGEMENT R.SARAVANAKUAMR. S.NAVEEN..
1 Concurrency Control II: Locking and Isolation Levels.
Database Management Systems, 2 nd Edition. R. Ramakrishnan and J. Gehrke1 Transaction Management Overview Instructor: Xintao Wu.
1 Concurrency Control Chapter Conflict Serializable Schedules  Two schedules are conflict equivalent if:  Involve the same actions of the same.
CMU SCS Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications C. Faloutsos – A. Pavlo Lecture#21: Concurrency Control (R&G ch. 17)
1 Concurrency Control Lecture 22 Ramakrishnan - Chapter 19.
Transaction Management Overview. Transactions Concurrent execution of user programs is essential for good DBMS performance. – Because disk accesses are.
1 Database Systems ( 資料庫系統 ) December 27, 2004 Chapter 17 By Hao-hua Chu ( 朱浩華 )
CS 440 Database Management Systems Concurrency Control 1.
Database Applications (15-415) DBMS Internals- Part XII Lecture 23, April 12, 2016 Mohammad Hammoud.
SIMPLE CONCURRENCY CONTROL. Correctness criteria: The ACID properties A A tomicity: All actions in the Xact happen, or none happen. C C onsistency: If.
1 Concurrency Control. 2 Why Have Concurrent Processes? v Better transaction throughput, response time v Done via better utilization of resources: –While.
CS 440 Database Management Systems
C. Faloutsos Concurrency control - deadlocks
Concurrency Control More !
Concurrency Control.
Transaction Management
Anthony D. Joseph and John Canny
Database Applications (15-415) DBMS Internals- Part XII Lecture 24, April 11, 2018 Mohammad Hammoud.
April 4, 2011 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 18 Transactions April 4, 2011 Ion.
Anthony D. Joseph and Ion Stoica
Transaction Management
March 9th – Transactions
Lecture#21: Concurrency Control – Part 1
CS162 Operating Systems and Systems Programming Review (II)
Chapter 15 : Concurrency Control
Concurrency Control R&G - Chapter 17
Transaction Management
Temple University – CIS Dept. CIS661 – Principles of Data Management
CONCURRENCY Concurrency is the tendency for different tasks to happen at the same time in a system ( mostly interacting with each other ) .   Parallel.
Database Systems (資料庫系統)
Database Systems (資料庫系統)
Presentation transcript:

CMU SCS Carnegie Mellon Univ. Dept. of Computer Science Database Applications Concurrency Control, II (R&G ch. 17)

CMU SCS FaloutsosSCS Review DBMSs support ACID Transaction semantics. Concurrency control and Crash Recovery are key components

CMU SCS FaloutsosSCS 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)

CMU SCS FaloutsosSCS Outline Serializability - concepts and algorithms One solution: Locking –2PL –variations Deadlocks

CMU SCS FaloutsosSCS 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

CMU SCS FaloutsosSCS 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

CMU SCS FaloutsosSCS 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: A:

CMU SCS FaloutsosSCS 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’)

CMU SCS FaloutsosSCS 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.

CMU SCS FaloutsosSCS Outline Serializability - concepts and algorithms One solution: Locking –2PL –variations Deadlocks

CMU SCS FaloutsosSCS 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”. SX S  – X–– Lock Compatibility Matrix

CMU SCS FaloutsosSCS PL THEOREM: if all transactions obey 2PL -> all schedules are serializable

CMU SCS FaloutsosSCS PL THEOREM: if all transactions obey 2PL -> all schedules are serializable (if even one violates 2PL, non-serializability is possible -example?)

CMU SCS FaloutsosSCS 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

CMU SCS FaloutsosSCS PL 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), Abort T2:R(A), W(A)

CMU SCS FaloutsosSCS 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

CMU SCS FaloutsosSCS 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

CMU SCS FaloutsosSCS Next... A few examples

CMU SCS FaloutsosSCS Lock_X(A) Read(A)Lock_S(A) A: = A-50 Write(A) Unlock(A) Read(A) Unlock(A) Lock_S(B) Lock_X(B) Read(B) Unlock(B) PRINT(A+B) Read(B) B := B +50 Write(B) Unlock(B) Non-2PL, A= 1000, B=2000, Output =?

CMU SCS FaloutsosSCS Lock_X(A) Read(A)Lock_S(A) A: = A-50 Write(A) Lock_X(B) Unlock(A) Read(A) Lock_S(B) Read(B) B := B +50 Write(B) Unlock(B)Unlock(A) Read(B) Unlock(B) PRINT(A+B) 2PL, A= 1000, B=2000, Output =?

CMU SCS FaloutsosSCS Lock_X(A) Read(A)Lock_S(A) A: = A-50 Write(A) Lock_X(B) Read(B) B := B +50 Write(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 =?

CMU SCS FaloutsosSCS Venn Diagram for Schedules All Schedules Avoid Cascading Abort Serial View Serializable Conflict Serializable

CMU SCS FaloutsosSCS Q: Which schedules does Strict 2PL allow? All Schedules Avoid Cascading Abort Serial View Serializable Conflict Serializable

CMU SCS FaloutsosSCS Q: Which schedules does Strict 2PL allow? All Schedules Avoid Cascading Abort Serial View Serializable Conflict Serializable

CMU SCS FaloutsosSCS 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?

CMU SCS FaloutsosSCS 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

CMU SCS FaloutsosSCS 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

CMU SCS FaloutsosSCS Lock Management, cont. Two-phase locking is simple enough, right? We’re not done. There’s an important wrinkle …

CMU SCS FaloutsosSCS Example: Output = ? Lock_X(A) Lock_S(B) Read(B) Lock_S(A) Read(A) A: = A-50 Write(A) Lock_X(B)

CMU SCS FaloutsosSCS Example: Output = ? Lock_X(A) Lock_S(B) Read(B) Lock_S(A) Read(A) A: = A-50 Write(A) Lock_X(B) lock mgr: grant wait

CMU SCS FaloutsosSCS Outline Serializability - concepts and algorithms One solution: Locking –2PL –variations Deadlocks –detection –prevention

CMU SCS FaloutsosSCS 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?

CMU SCS FaloutsosSCS 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

CMU SCS FaloutsosSCS 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) T1T2 T4T3

CMU SCS FaloutsosSCS Another example T1T2 T3 T4 is there a deadlock? if yes, which xacts are involved?

CMU SCS FaloutsosSCS Another example T1T2 T3 T4 now, is there a deadlock? if yes, which xacts are involved?

CMU SCS FaloutsosSCS Deadlock detection how often should we run the algo? how many transactions are typically involved?

CMU SCS FaloutsosSCS Deadlock handling T1T2 T3 T4 Q: what to do?

CMU SCS FaloutsosSCS Deadlock handling T1T2 T3 T4 Q0: what to do? A: select a ‘victim’ & ‘rollback’ Q1: which/how to choose?

CMU SCS FaloutsosSCS 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? T1T2 T3 T4

CMU SCS FaloutsosSCS Deadlock handling Q2: How far to rollback? A2.1: completely A2.2: minimally Q3: Starvation?? T1T2 T3 T4

CMU SCS FaloutsosSCS Deadlock handling Q3: Starvation?? A3.1: include #rollbacks in victim selection criterion. T1T2 T3 T4

CMU SCS FaloutsosSCS Outline Serializability - concepts and algorithms One solution: Locking –2PL –variations Deadlocks –detection –prevention

CMU SCS FaloutsosSCS 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)

CMU SCS Deadlock prevention FaloutsosSCS Wait-DieWound-Wait Ti wants Tj has Ti wants Tj has

CMU SCS FaloutsosSCS Deadlock Prevention Q: Why do these schemes guarantee no deadlocks? A: Q: When a transaction restarts, what is its (new) priority? A:

CMU SCS FaloutsosSCS 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?

CMU SCS FaloutsosSCS SQL statement usually, conc. control is transparent to the user, but LOCK [EXCLUSIVE|SHARED]

CMU SCS FaloutsosSCS Concurrency control - conclusions (conflict) serializability correctness automatically correct interleavings: –locks + protocol (2PL, 2PLC,...) –deadlock detection + handling (or deadlock prevention)

CMU SCS FaloutsosSCS 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)

CMU SCS FaloutsosSCS 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?

CMU SCS FaloutsosSCS Quiz - hints: 2PL schedules serializable schedules serial sch’s Q: 2PLC??

CMU SCS FaloutsosSCS Quiz - hints: 2PL schedules serializable schedules serial sch’s2PLC

CMU SCS FaloutsosSCS #54 Lock granularity? -lock granularity -field? record? page? table? -Pros and cons? -(Ideally, each transaction should obtain a few locks)

CMU SCS FaloutsosSCS #55 Multiple granularity Eg: attr 1 att r2 record -n recor d2 recor d1 Table 2 Tabl e1 DBDB

CMU SCS FaloutsosSCS #56 What would you do? T1: read Smith’s salary, while T2: give 10% raise to everybody what locks should they obtain? recor d-n recor d2 recor d1 Tabl e2 Tabl e1 DBDB

CMU SCS FaloutsosSCS #57 What types of locks? X/S locks for leaf level + ‘intent’ locks, for higher levels

CMU SCS FaloutsosSCS #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

CMU SCS FaloutsosSCS #59 Protocol -each xact obtains appropriate lock at highest level -proceeds to desirable lower levels

CMU SCS FaloutsosSCS #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.

CMU SCS FaloutsosSCS #61 Multiple granularity protocol X SIX IXS IS stronger (more privileges) weaker

CMU SCS FaloutsosSCS #62 Examples – 2 level hierarchy T1 scans R, and updates a few tuples: Tuples Tables

CMU SCS FaloutsosSCS #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.

CMU SCS FaloutsosSCS #64 Examples – 2 level hierarchy T2: find avg salary of ‘Sales’ employees

CMU SCS FaloutsosSCS #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.

CMU SCS FaloutsosSCS #66 Examples – 2 level hierarchy T3: sum of salaries of everybody in ‘R’:

CMU SCS FaloutsosSCS #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 many low level locks acquired

CMU SCS FaloutsosSCS #68 Multiple granularity Very useful in practice each xact needs only a few locks

CMU SCS FaloutsosSCS #69 Outline... Locking granularity Tree locking protocols Phantoms & predicate locking

CMU SCS FaloutsosSCS #70 Locking in B+ Trees What about locking indexes?

CMU SCS FaloutsosSCS #71 Example B+tree T1 wants to insert in H T2 wants to insert in I why not plain 2PL? GIH FED CB A.... root

CMU SCS FaloutsosSCS #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! GIH FED CB A.... root

CMU SCS FaloutsosSCS #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)

CMU SCS FaloutsosSCS #74 Example B+tree T1 wants to insert in H crabbing: GIH FED CB A.... root

CMU SCS FaloutsosSCS #75 Example B+tree T1 wants to insert in H GIH FED CB A.... root

CMU SCS FaloutsosSCS #76 Example B+tree T1 wants to insert in H (if ‘B’ is ‘safe’) GIH FED CB A.... root

CMU SCS FaloutsosSCS #77 Example B+tree T1 wants to insert in H continue ‘crabbing’ GIH FED CB A.... root

CMU SCS FaloutsosSCS #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.

CMU SCS FaloutsosSCS #79 Example ROOT A B C DE F G H I * *23*24*35*36*38*41*44* Do: 1) Search 38* 2) Delete 38* 3) Insert 45* 4) Insert 25* 23

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) FaloutsosSCS #80

CMU SCS Can we do better? Yes [Bayer and Schkolnik]: FaloutsosSCS #81 Rudolf Bayer, Mario Schkolnick: Concurrency of Operations on B-Tree s. Acta Inf. 9: 1-21 (1977) Rudolf Bayer, Mario Schkolnick: Concurrency of Operations on B-Tree s. Acta Inf. 9: 1-21 (1977)

CMU SCS FaloutsosSCS #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.

CMU SCS FaloutsosSCS #83 Example ROOT A B C DE F G H I * *23*24*35*36*38*41*44* 23 Do: 1) Delete 38* 2) Insert 25*

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 FaloutsosSCS #84

CMU SCS Notice: Textbook has a third variation, that uses lock-upgrades (and may lead to deadlocks) FaloutsosSCS #85

CMU SCS FaloutsosSCS #86 Outline Locking granularity Tree locking protocols Phantoms & predicate locking

CMU SCS FaloutsosSCS #87 Dynamic Databases – The “Phantom” Problem so far: only reads and updates – no insertions/deletions with insertions/deletions, new problems:

CMU SCS FaloutsosSCS #88 The phantom problem T1 select max(age)... where rating=1 T2 insert... age=96 rating=1 select max(age)... where rating=1 time 71 96

CMU SCS FaloutsosSCS #89 Why? because T1 locked only *existing* records – not ones under way! Solution?

CMU SCS FaloutsosSCS #90 Solution theoretical solution: ‘predicate locking’: e.g., lock all records (current or incoming) with rating=1 –VERY EXPENSIVE

CMU SCS FaloutsosSCS #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)

CMU SCS FaloutsosSCS #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

CMU SCS FaloutsosSCS #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)

CMU SCS FaloutsosSCS #94 Transaction Support in SQL-92 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY Defaults: SERIALIZABLE READ WRITE isolation level access mode

CMU SCS FaloutsosSCS #95 Multiple granularity locking: leads to few locks, at appropriate levels Tree-structured indexes: –‘crabbing’ and ‘safe nodes’ Summary

CMU SCS FaloutsosSCS #96 “phantom problem”, if insertions/deletions –(Predicate locking prevents phantoms) –Index locking, or table locking Summary