Goal of Concurrency Control. Concurrency Control. Example. Solution 1. Solution 2. Solution 3

Similar documents
Concurrency Control. Conflict Serializable Schedules. Example. Chapter 17

Concurrency Control CHAPTER 17 SINA MERAJI

Concurrency Control. Chapter 17. Comp 521 Files and Databases Fall

Concurrency Control. [R&G] Chapter 17 CS432 1

Concurrency Control. R &G - Chapter 19

Concurrency Control. Chapter 17. Comp 521 Files and Databases Spring

Transaction Management and Concurrency Control. Chapter 16, 17

Concurrency Control. Chapter 17. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke

Concurrency Control. Chapter 17. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1

Transaction Management: Concurrency Control

Intro to Transactions

Deadlock Prevention (cont d) Deadlock Prevention. Example: Wait-Die. Wait-Die

Intro to Transaction Management

Transaction Management Overview. Transactions. Concurrency in a DBMS. Chapter 16

CS 448 Database Systems. Serializability Issues

CS 541 Database Systems. Serializability Issues

Transaction Management Overview

Concurrency Control! Snapshot isolation" q How to ensure serializability and recoverability? " q Lock-Based Protocols" q Other Protocols"

Transaction Management: Introduction (Chap. 16)

Phantom Problem. Phantom Problem. Phantom Problem. Phantom Problem R1(X1),R1(X2),W2(X3),R1(X1),R1(X2),R1(X3) R1(X1),R1(X2),W2(X3),R1(X1),R1(X2),R1(X3)

What are Transactions? Transaction Management: Introduction (Chap. 16) Major Example: the web app. Concurrent Execution. Web app in execution (CS636)

Concurrency Control & Recovery

Overview of Transaction Management

Introduction to Data Management. Lecture #24 (Transactions)

Conflict Equivalent. Conflict Serializability. Example 1. Precedence Graph Test Every conflict serializable schedule is serializable

Introduction to Data Management. Lecture #18 (Transactions)

Concurrency Control & Recovery

Review. Review. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Lecture #21: Concurrency Control (R&G ch.

Database Management System

Slides Courtesy of R. Ramakrishnan and J. Gehrke 2. v Concurrent execution of queries for improved performance.

Outline. Review questions. Carnegie Mellon Univ. Dept. of Computer Science Database Applications

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

Overview. Introduction to Transaction Management ACID. Transactions

Implementing Isolation

Transaction Overview and Concurrency Control

COURSE 1. Database Management Systems

Transaction Processing: Concurrency Control. Announcements (April 26) Transactions. CPS 216 Advanced Database Systems

Including Aborts in Serializability. Conflict Serializable Schedules. Recall Conflicts. Conflict Equivalent

Database Systems. Announcement

CMSC 424 Database design Lecture 22 Concurrency/recovery. Mihai Pop

Concurrency. Consider two ATMs running in parallel. We need a concurrency manager. r1[x] x:=x-250 r2[x] x:=x-250 w[x] commit w[x] commit

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #17: Transac0ons 2: 2PL and Deadlocks

CAS CS 460/660 Introduction to Database Systems. Transactions and Concurrency Control 1.1

Transactions & Update Correctness. April 11, 2018

Introduction to Data Management. Lecture #26 (Transactions, cont.)

Page 1. CS194-3/CS16x Introduction to Systems. Lecture 8. Database concurrency control, Serializability, conflict serializability, 2PL and strict 2PL

CSE 444: Database Internals. Lectures Transactions

TRANSACTION MANAGEMENT

Transaction Management: Concurrency Control, part 2

Locking for B+ Trees. Transaction Management: Concurrency Control, part 2. Locking for B+ Trees (contd.) Locking vs. Latching

Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Last Class. Last Class. Faloutsos/Pavlo CMU /615

Distributed Transaction Management. Distributed Database System

Chapter 13 : Concurrency Control

2 nd Semester 2009/2010

Chapter 15 : Concurrency Control

Transactions and Locking. April 16, 2018

h p:// Authors: Tomáš Skopal, Irena Holubová Lecturer: Mar n Svoboda, mar

Page 1. Goals of Today s Lecture" Two Key Questions" Goals of Transaction Scheduling"

Lecture 21 Concurrency Control Part 1

CSC 261/461 Database Systems Lecture 21 and 22. Spring 2017 MW 3:25 pm 4:40 pm January 18 May 3 Dewey 1101

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

Page 1. Goals of Todayʼs Lecture" Two Key Questions" Goals of Transaction Scheduling"

Transactions Processing (i)

Database Management System

Chapter 12 : Concurrency Control

Database transactions

Lecture 21. Lecture 21: Concurrency & Locking

doc. RNDr. Tomáš Skopal, Ph.D.

Lock Granularity and Consistency Levels (Lecture 7, cs262a) Ali Ghodsi and Ion Stoica, UC Berkeley February 7, 2018

Lock-based Concurrency Control

! A lock is a mechanism to control concurrent access to a data item! Data items can be locked in two modes :

Database Applications (15-415)

Last Class Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications

Page 1. Goals of Today s Lecture. The ACID properties of Transactions. Transactions

Homework 5 (by Joy Arulraj) Due: Monday Nov 13, 11:59pm

Database design and implementation CMPSCI 645. Lectures 18: Transactions and Concurrency

Concurrency Control Serializable Schedules and Locking Protocols

L i (A) = transaction T i acquires lock for element A. U i (A) = transaction T i releases lock for element A

CSC 261/461 Database Systems Lecture 24

Integrity Constraints, Triggers, Transactions and Procedures

Database Management Systems

Databases - Transactions II. (GF Royle, N Spadaccini ) Databases - Transactions II 1 / 22

Transaction Management & Concurrency Control. CS 377: Database Systems

Graph-based protocols are an alternative to two-phase locking Impose a partial ordering on the set D = {d 1, d 2,..., d h } of all data items.

Transactions. Transaction. Execution of a user program in a DBMS.

CSE 344 MARCH 9 TH TRANSACTIONS

Today s Class. Carnegie Mellon Univ. Dept. of Computer Science /615 - DB Applications. Formal Properties of Schedules. Conflicting Operations

Homework 5 (by Joy Arulraj) Solutions Due: Monday Nov 13, 11:59pm

Lecture 7: Transactions Concurrency Control

Lecture 5: Transactions Concurrency Control. Wednesday October 26 th, 2011

A lock is a mechanism to control concurrent access to a data item Data items can be locked in two modes:

Chapter 5. Concurrency Control Techniques. Adapted from the slides of Fundamentals of Database Systems (Elmasri et al., 2006)

Transaction Processing: Concurrency Control ACID. Transaction in SQL. CPS 216 Advanced Database Systems. (Implicit beginning of transaction)

Concurrency Control - Two-Phase Locking

Database Tuning and Physical Design: Execution of Transactions

Concurrency Control. Concurrency Control Ensures interleaving of operations amongst concurrent transactions result in serializable schedules

Multi-User-Synchronization

Concurrency Control Overview. COSC 404 Database System Implementation. Concurrency Control. Lock-Based Protocols. Lock-Based Protocols (2)

Final Review. CS634 May 11, Slides based on Database Management Systems 3 rd ed, Ramakrishnan and Gehrke

Lecture 13 Concurrency Control

Transcription:

Goal of Concurrency Control Concurrency Control Transactions should be executed so that it is as though they executed in some serial order Also called Isolation or Serializability Weaker variants also possible Lower degrees of isolation Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 1 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 2 Consider two transactions (Xacts): T1: BEGIN A=A+100, B=B-100 END T2: BEGIN A=1.06*A, B=1.06*B END T1 transfers $100 from B s account to A s account T2 credits both accounts with 6% interest If submitted concurrently, net effect should be equivalent to Xacts running in some serial order guarantee that T1 logically occurs before T2 (or vice-versa) but one of them is true Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 3 Solution 1 1) Get exclusive lock on entire database 2) Execute transaction 3) Release exclusive lock Similar to critical sections in operating systems Serializability guaranteed because execution is serial! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 4 Solution 2 Solution 3 1) Get exclusive locks on accessed data items 2) Execute transaction 3) Release exclusive locks 1) Get exclusive locks on data items that are modified; get shared locks on data items that are only read 2) Execute transaction S X 3) Release all locks S Yes Conservative Strict Two Phase Locking (2PL) X Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6 1

Solution 4 Solution 5 1) Get exclusive locks on data items that are modified and get shared locks on data items that are read 2) Execute transaction and release locks on objects no longer needed during execution 1) Get exclusive locks on data items that are modified and get shared locks on data items that are read, but do this during execution of transaction (as needed) 2) Release all locks Conservative Two Phase Locking (2PL) Strict Two Phase Locking (2PL) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8 Solution 6 Summary of Alternatives 1) Get exclusive locks on data items that are modified and get shared locks on data items that are read, but do this during execution of transaction (as needed) 2) Release locks on objects no longer needed during execution of transaction 3) Cannot acquire locks once any lock has been released Hence two-phase (acquiring phase and releasing phase) Two Phase Locking (2PL) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 9 Conservative Strict 2PL deadlocks, no cascading aborts But need to know objects a priori, when to release locks Conservative 2PL deadlocks, more concurrency than Conservative Strict 2PL But need to know objects a priori, when to release locks, cascading aborts Strict 2PL cascading aborts, no need to know objects a priori or when to release locks, more concurrency than Conservative Strict 2PL But deadlocks 2PL Most concurrency, no need to know object a priori But need to know when to release locks, cascading aborts, deadlocks Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10 Method of Choice Lock Management Strict 2PL cascading aborts, no need to know objects a priori or when to release locks, more concurrency than Conservative Strict 2PL But deadlocks Reason for choice Cannot know objects a priori, so no Conservative options Thus only 2PL and Strict 2PL left 2PL needs to know when to release locks (main problem) Also has cascading aborts Hence Strict 2PL Implication Need to deal with deadlocks! Lock and unlock requests are handled by the lock manager Lock table entry: Number of transactions currently holding a lock Type of lock held (shared or exclusive) Pointer to queue of lock requests Locking and unlocking have to be atomic operations Lock upgrade: transaction that holds a shared lock can be upgraded to hold an exclusive lock Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 11 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12 2

Outline Consider a possible interleaving (schedule): T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B This is OK. But what about: T1: A=A+100, B=B-100 T2: A=1.06*A, B=1.06*B The DBMS s view of the second schedule: T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14 Scheduling Transactions Serial schedule: Schedule that does not interleave the actions of different transactions. Equivalent schedules: For any database state The effect (on the set of objects in the database) of executing the schedules is the same The values read by transactions is the same in the schedules Assume no knowledge of transaction logic Serializable schedule: A schedule that is equivalent to some serial execution of the transactions. (te: If each transaction preserves consistency, every serializable schedule preserves consistency. ) Anomalies with Interleaved Execution Reading Uncommitted Data (WR Conflicts, dirty reads ): T1: R(A), W(A), R(B), W(B), Abort T2: R(A), W(A), C Unrepeatable Reads (RW Conflicts): T1: R(A), R(A), W(A), C T2: R(A), W(A), C Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 16 Anomalies (Continued) Conflict Serializable Schedules Overwriting Uncommitted Data (WW Conflicts): T1: W(A), W(B), C T2: W(A), W(B), C Two schedules are conflict equivalent if: Involve the same actions of the same transactions Every pair of conflicting actions is ordered the same way Schedule S is conflict serializable if S is conflict equivalent to some serial schedule Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 17 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 18 3

A schedule that is not conflict serializable: T1: R(A), W(A), R(B), W(B) T2: R(A), W(A), R(B), W(B) T1 A B The cycle in the graph reveals the problem. The output of T1 depends on T2, and viceversa. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 19 T2 Dependency graph Dependency Graph Dependency graph: One node per Xact; edge from Ti to Tj if Tj reads/writes an object last written by Ti. Theorem: Schedule is conflict serializable if and only if its dependency graph is acyclic Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 20 Lock-Based Concurrency Control Strict Two-phase Locking (Strict 2PL) Protocol: Each Xact must obtain a S (shared) lock on object before reading, and an X (exclusive) lock on object before writing All locks held by a transaction are released when the transaction completes If an Xact holds an X lock on an object, no other Xact can get a lock (S or X) on that object. Strict 2PL allows only serializable schedules Dependency graph is always acyclic Returning to Definition of Serializability A schedule S is serializable if there exists a serial order SO such that: The state of the database after S is the same as the state of the database after SO The values read by each transaction in S is the same as that returned by each transaction in SO Database does not know anything about the internal structure of the transaction programs Under this definition, certain serializable executions are not conflict serializable! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 21 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 22 T1: R(A) W(A) T2: W(A) T3: W(A) T1: R(A),W(A) T2: W(A) T3: W(A) View Serializability Schedules S1 and S2 are view equivalent if: If Ti reads initial value of A in S1, then Ti also reads initial value of A in S2 If Ti reads value of A written by Tj in S1, then Ti also reads value of A written by Tj in S2 If Ti writes final value of A in S1, then Ti also writes final value of A in S2 T1: R(A) W(A) T2: W(A) T3: W(A) T1: R(A),W(A) T2: W(A) T3: W(A) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 23 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 24 4

Outline Deadlocks Deadlock: Cycle of transactions waiting for locks to be released by each other. Two ways of dealing with deadlocks: Deadlock prevention Deadlock detection Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 25 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 26 Deadlock Prevention Deadlock Detection Assign priorities based on timestamps. Assume Ti wants a lock that Tj holds. Two policies are possible: Wait-Die: It Ti has higher priority, Ti waits for Tj; otherwise Ti aborts Wound-wait: If Ti has higher priority, Tj aborts; otherwise Ti waits If a transaction re-starts, make sure it has its original timestamp Create a waits-for graph: des are transactions There is an edge from Ti to Tj if Ti is waiting for Tj to release a lock Periodically check for cycles in the waits-for graph Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 27 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 28 Deadlock Detection Outline : T1: S(A), R(A), S(B) T2: X(B),W(B) X(C) T3: S(C), R(C) X(A) T4: X(B) T1 T2 T1 T2 T4 T3 T3 T3 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 29 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 30 5

Multiple-Granularity Locks Solution: New Lock Modes, Protocol Hard to decide what granularity to lock (tuples vs. pages vs. tables). Shouldn t have to decide! Data containers are nested: contains Database Tables Pages Tuples Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 31 Allow Xacts to lock at each level, but with a special protocol using new intention locks: Before locking an item, Xact must set intention locks on all its ancestors. For unlock, go from specific to general (i.e., bottom-up). SIX mode: Like S & IX at the same time. -- IS IX Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 32 -- IS IX S X S X Multiple Granularity Lock Protocol s Each Xact starts from the root of the hierarchy. To get S or IS lock on a node, must hold IS or IX on parent node. What if Xact holds SIX on parent? S on parent? To get X or IX or SIX on a node, must hold IX or SIX on parent node. Must release locks in bottom-up order. Protocol is correct in that it is equivalent to directly setting locks at the leaf levels of the hierarchy. T1 scans R, and updates a few tuples: T1 gets an SIX lock on R, then repeatedly gets an S lock on tuples of R, and occasionally upgrades to X on the tuples. T2 uses an index to read only part of R: T2 gets an IS lock on R, and repeatedly -- IS IX gets an S lock on tuples of R. -- T3 reads all of R: T3 gets an S lock on R. OR, T3 could behave like T2; can use lock escalation to decide which. IS IX S X S X Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 33 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 34 Dynamic Databases If we relax the assumption that the DB is a fixed collection of objects, even Strict 2PL will not assure serializability: T1 locks all pages containing sailor records with rating = 1, and finds oldest sailor (say, age = 71). Next, T2 inserts a new sailor; rating = 1, age = 96. T2 also deletes oldest sailor with rating = 2 (and, say, age = 80), and commits. T1 now locks all pages containing sailor records with rating = 2, and finds oldest (say, age = 63). consistent DB state where T1 is correct! The Problem T1 implicitly assumes that it has locked the set of all sailor records with rating = 1. Assumption only holds if no sailor records are added while T1 is executing! Need some mechanism to enforce this assumption. (Index locking and predicate locking.) shows that conflict serializability guarantees serializability only if the set of objects is fixed! Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 35 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 36 6

Index Locking Index r=1 If there is a dense index on the rating field using Alternative (2), T1 should lock the index page containing the data entries with rating = 1. If there are no records with rating = 1, T1 must lock the index page where such a data entry would be, if it existed! If there is no suitable index, T1 must lock all pages, and lock the file/table to prevent new pages from being added, to ensure that no new records with rating = 1 are added. Data Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 37 Predicate Locking Grant lock on all records that satisfy some logical predicate, e.g. age > 2*salary. Index locking is a special case of predicate locking for which an index supports efficient implementation of the predicate lock. What is the predicate in the sailor example? In general, predicate locking has a lot of locking overhead. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 38 Outline Transaction Support in SQL-92 Each transaction has an access mode, a diagnostics size, and an isolation level. Isolation Level Read Uncommitted Read Committed Dirty Read Unrepeatable Read Phantom Problem Repeatable Reads Serializable Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 39 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 40 Outline Locking in B+ Trees How can we efficiently lock a particular leaf node? Btw, don t confuse this with multiple granularity locking! One solution: Ignore the tree structure, just lock pages while traversing the tree, following 2PL. This has terrible performance! Root node (and many higher level nodes) become bottlenecks because every tree access begins at the root. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 41 Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 42 7

Two Useful Observations Higher levels of the tree only direct searches for leaf pages. For inserts, a node on a path from root to modified leaf must be locked (in X mode, of course), only if a split can propagate up to it from the modified leaf. (Similar point holds w.r.t. deletes.) We can exploit these observations to design efficient locking protocols that guarantee serializability even though they violate 2PL. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 43 A Simple Tree Locking Algorithm 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. Safe node: de such that changes will not propagate up beyond this node. Inserts: de is not full. Deletes: de is not half-empty. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 44 20* ROOT 23 20 F Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 45 35 G H I A B 38 44 D Do: 1) Search 38* 2) Delete 38* 3) Insert 45* 4) Insert 25* 22* 23* 24* 35* 36* 38* 41* 44* C E A Better Tree Locking Algorithm 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. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 46 ROOT 20 35 A B Do: 1) Delete 38* 2) Insert 25* 4) Insert 45* 5) Insert 45*, then 46* 23 F 38 44 C 20* G H I D 22* 23* 24* 35* 36* 38* 41* 44* E Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 47 8