CS Transactions

Similar documents
CSE232: Database System Principles

CS 245: Database System Principles

Concurrency Control. Himanshu Gupta CSE 532 CC 1

CS 525: Advanced Database Organization 14: Concurrency Control

CS 245: Database System Principles

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

Transaction Management Overview

CS377: Database Systems Concurrency Control. Li Xiong Department of Mathematics and Computer Science Emory University

Concurrency Control & Recovery

Concurrency Control & Recovery

Transaction Management and Concurrency Control. Chapter 16, 17

Overview of Transaction Management

Database Management System

Introduction to Data Management. Lecture #18 (Transactions)

Intro to Transaction Management

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

CS 245: Database System Principles

Intro to Transactions

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

COURSE 1. Database Management Systems

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

Introduction to Data Management. Lecture #24 (Transactions)

Chapter 6 Distributed Concurrency Control

Transaction Management. Concurrency Control (2)

Transaction Management: Concurrency Control

Transaction Management. Concurrency Control (4)

CSE 444: Database Internals. Lectures Transactions

Introduction to Transaction Management

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

Transaction Management: Introduction (Chap. 16)

Overview. Introduction to Transaction Management ACID. Transactions

Introduction to Transaction Management

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

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

Transaction Management & Concurrency Control. CS 377: Database Systems

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

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

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

Transaction Overview and Concurrency Control

CS 5614: Transaction Processing 121. Transaction = Unit of Work Recall ACID Properties (from Module 1)

Concurrency Control. Conflict Serializable Schedules. Example. Chapter 17

CS 4604: Introduc0on to Database Management Systems. B. Aditya Prakash Lecture #17: Transac0ons 1: Intro. to ACID

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

CSE 444: Database Internals. Lectures 13 Transaction Schedules

CS 5614: (Big) Data Management Systems. B. Aditya Prakash Lecture #6: Transac/ons 1: Intro. to ACID

Concurrency Control. R &G - Chapter 19

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

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

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

CSE 344 MARCH 5 TH TRANSACTIONS

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

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

Transaction Processing. Introduction to Databases CompSci 316 Fall 2018

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

Concurrency Control CHAPTER 17 SINA MERAJI

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

Why Transac'ons? Database systems are normally being accessed by many users or processes at the same 'me.

Introduction to Data Management CSE 344

Database Management System

CSE 344 MARCH 9 TH TRANSACTIONS

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

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

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

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

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

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

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

Introduction. Storage Failure Recovery Logging Undo Logging Redo Logging ARIES

Transactions Processing (i)

Integrity Constraints, Triggers, Transactions and Procedures

Database Management Systems CSEP 544. Lecture 9: Transactions and Recovery

ACID Properties. Transaction Management: Crash Recovery (Chap. 18), part 1. Motivation. Recovery Manager. Handling the Buffer Pool.

References. Transaction Management. Database Administration and Tuning 2012/2013. Chpt 14 Silberchatz Chpt 16 Raghu

Lecture 7: Transactions Concurrency Control

Introduction to Data Management. Lecture #25 (Transactions II)

Page 1. Goals for Today" What is a Database " Key Concept: Structured Data" CS162 Operating Systems and Systems Programming Lecture 13.

Transaction Management: Crash Recovery (Chap. 18), part 1

some sequential execution crash! Recovery Manager replacement MAIN MEMORY policy DISK

Intro to DB CHAPTER 15 TRANSACTION MNGMNT

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

Chapter 22. Transaction Management

Announcements. Motivating Example. Transaction ROLLBACK. Motivating Example. CSE 444: Database Internals. Lab 2 extended until Monday

Database. Università degli Studi di Roma Tor Vergata. ICT and Internet Engineering. Instructor: Andrea Giglio

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

CSE 344 MARCH 25 TH ISOLATION

Lecture 21. Lecture 21: Concurrency & Locking

Transactions and Concurrency Control

Database Systems. Announcement

Lectures 8 & 9. Lectures 7 & 8: Transactions

Announcements. Transaction. Motivating Example. Motivating Example. Transactions. CSE 444: Database Internals

Database System Concepts

CS122 Lecture 15 Winter Term,

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

The transaction. Defining properties of transactions. Failures in complex systems propagate. Concurrency Control, Locking, and Recovery

Database Systems CSE 414

Final Review. May 9, 2017

TRANSACTION MANAGEMENT

Final Review. May 9, 2018 May 11, 2018

DBMS Architecture. Transaction Management. Chapters 16 and 17. Concurrency control and Recovery. Relationship with the mutual exclusion problem

Transactions and Concurrency Control. Dr. Philip Cannata

Transcription:

CS 54100 Transactions Chris Clifton 2 April, 2012 Goal: Integrity Across Sequence of Operations Update should complete entirely update stipend set stipend = stipend*1.03; What if it gets halfway and the machine crashes? What about multiple operations? Withdraw x from Account1 Deposit x into Account2 Simultaneous operations? Print paychecks while stipend being updated 4/2/2012 Chris Clifton - CS541 2 1

Solution: Transaction Sequence of operations grouped into a transaction Externally viewed as Atomic: All happens at once DBMS manages so even the programmer gets this view 4/2/2012 Chris Clifton - CS541 3 ACID properties Transactions have: Atomicity All or nothing Consistency Changes to values maintain integrity Isolation Transaction occurs as if nothing else happening Durability Once completed, changes are permanent 4/2/2012 Chris Clifton - CS541 4 2

Transactions Concurrent execution of user programs is essential for good DBMS performance. Because disk accesses are frequent, and relatively slow, it is important to keep the cpu humming by working on several user programs concurrently. A user s program may carry out many operations on the data retrieved from the database, but the DBMS is only concerned about what data is read/written from/to the database. A transaction is the DBMS s abstract view of a user program: a sequence of reads and writes. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 5 Concurrency in a DBMS Users submit transactions, and can think of each transaction as executing by itself. Concurrency is achieved by the DBMS, which interleaves actions (reads/writes of DB objects) of various transactions. Each transaction must leave the database in a consistent state if the DB is consistent when the transaction begins. DBMS will enforce some ICs, depending on the ICs declared in CREATE TABLE statements. Beyond this, the DBMS does not really understand the semantics of the data. (e.g., it does not understand how the interest on a bank account is computed). Issues: Effect of interleaving transactions, and crashes. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 6 3

Atomicity of Transactions A transaction might commit after completing all its actions, or it could abort (or be aborted by the DBMS) after executing some actions. A very important property guaranteed by the DBMS for all transactions is that they are atomic. That is, a user can think of a Xact as always executing all its actions in one step, or not executing any actions at all. DBMS logs all actions so that it can undo the actions of aborted transactions. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 7 Example Consider two transactions (Xacts): T1: BEGIN A=A+100, B=B-100 END T2: BEGIN A=1.06*A, B=1.06*B END Intuitively, the first transaction is transferring $100 from B s account to A s account. The second is crediting both accounts with a 6% interest payment. There is no guarantee that T1 will execute before T2 or vice-versa, if both are submitted together. However, the net effect must be equivalent to these two transactions running serially in some order. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 8 4

Example (Contd.) 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 9 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 first schedule is identical to the effect of executing the second schedule. 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. ) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 10 5

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 11 Anomalies (Continued) Overwriting Uncommitted Data (WW Conflicts): T1: W(A), W(B), C T2: W(A), W(B), C Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 12 6

Aborting a Transaction If a transaction Ti is aborted, all its actions have to be undone. Not only that, if Tj reads an object last written by Ti, Tj must be aborted as well! Most systems avoid such cascading aborts by releasing a transaction s locks only at commit time. If Ti writes an object, Tj can read this only after Ti commits. In order to undo the actions of an aborted transaction, the DBMS maintains a log in which every write is recorded. This mechanism is also used to recover from system crashes: all active Xacts at the time of the crash are aborted when the system comes back up. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 13 The Log The following actions are recorded in the log: Ti writes an object: the old value and the new value. Log record must go to disk before the changed page! Ti commits/aborts: a log record indicating this action. Log records are chained together by Xact id, so it s easy to undo a specific Xact. Log is often duplexed and archived on stable storage. All log related activities (and in fact, all CC related activities such as lock/unlock, dealing with deadlocks etc.) are handled transparently by the DBMS. Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 14 7

Recovering From a Crash There are 3 phases in the Aries recovery algorithm: Analysis: Scan the log forward (from the most recent checkpoint) to identify all Xacts that were active, and all dirty pages in the buffer pool at the time of the crash. Redo: Redoes all updates to dirty pages in the buffer pool, as needed, to ensure that all logged updates are in fact carried out and written to disk. Undo: The writes of all Xacts that were active at the crash are undone (by restoring the before value of the update, which is in the log record for the update), working backwards in the log. (Some care must be taken to handle the case of a crash occurring during the recovery process!) Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke 15 CS 54100 Concurrency Control Chris Clifton 2 April, 2012 8

Chapters 16-17 Concurrency Control T1 T2 Tn DB (consistency constraints) 4/2/2012 Chris Clifton - CS541 17 Example: T1: Read(A) T2: Read(A) A A+100 A A 2 Write(A) Write(A) Read(B) Read(B) B B+100 B B 2 Write(B) Write(B) Constraint: A=B 4/2/2012 Chris Clifton - CS541 18 9

Schedule A T1 Read(A); A A+100 Write(A); Read(B); B B+100; Write(B); T2 Read(A);A A 2; Write(A); Read(B);B B 2; Write(B); A B 25 25 125 250 125 250 250 250 T1 Schedule B Read(A); A A+100 Write(A); Read(B); B B+100; Write(B); T2 Read(A);A A 2; Write(A); Read(B);B B 2; Write(B); A B 25 25 50 150 50 150 150 150 10

Schedule C T1 Read(A); A A+100 Write(A); Read(B); B B+100; Write(B); T2 Read(A);A A 2; Write(A); Read(B);B B 2; Write(B); A B 25 25 125 250 125 250 250 250 Schedule D T1 Read(A); A A+100 Write(A); Read(B); B B+100; Write(B); T2 Read(A);A A 2; Write(A); Read(B);B B 2; Write(B); A B 25 25 125 250 50 150 250 150 11

Schedule E T1 T2 Read(A); A A+100 Write(A); Read(B); B B+100; Write(B); Same as Schedule D but with new T2 Read(A);A A 1; Write(A); Read(B);B B 1; Write(B); A B 25 25 125 125 25 125 125 125 Want schedules that are good, regardless of initial state and transaction semantics Only look at order of read and writes Example: Sc=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B) 4/2/2012 Chris Clifton - CS541 24 12

Example: Sc=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B) Sc =r1(a)w1(a) r1(b)w1(b)r2(a)w2(a)r2(b)w2(b) T1 T2 However, for Sd: Sd=r1(A)w1(A)r2(A)w2(A) r2(b)w2(b)r1(b)w1(b) as a matter of fact, T2 must precede T1 in any equivalent schedule, i.e., T2 T1 13

T2 T1 Also, T1 T2 T1 T2 Sd cannot be rearranged into a serial schedule Sd is not equivalent to any serial schedule Sd is bad Returning to Sc Sc=r1(A)w1(A)r2(A)w2(A)r1(B)w1(B)r2(B)w2(B) T1 T2 T1 T2 no cycles Sc is equivalent to a serial schedule (in this case T1,T2) 14

Concepts Transaction: sequence of ri(x), wi(x) actions Conflicting actions: r1(a) w2(a) w1(a) w2(a) r1(a) w2(a) Schedule: represents chronological order in which actions are executed Serial schedule: no interleaving of actions or transactions 4/2/2012 Chris Clifton - CS541 29 What about concurrent actions? Ti issues System Input(X) t x read(x,t) issues completes input(x) time T2 issues write(b,s) System issues input(b) input(b) completes B S System issues output(b) output(b) completes 15

So net effect is either S= r1(x) w2(b) or S= w2(b) r1(x) 4/2/2012 Chris Clifton - CS541 31 What about conflicting, concurrent actions on same object? start r1(a) end r1(a) start w2(a) end w2(a) time Assume equivalent to either r1(a) w2(a) or w2(a) r1(a) low level synchronization mechanism Assumption called atomic actions 16

Definition S1, S2 are conflict equivalent schedules if S1 can be transformed into S2 by a series of swaps on non-conflicting actions. 4/2/2012 Chris Clifton - CS541 33 Definition A schedule is conflict serializable if it is conflict equivalent to some serial schedule. 4/2/2012 Chris Clifton - CS541 34 17

Precedence graph P(S) (S is schedule) Nodes: transactions in S Arcs: Ti Tj whenever - pi(a), qj(a) are actions in S - pi(a) < S qj(a) - at least one of pi, qj is a write 4/2/2012 Chris Clifton - CS541 35 Exercise: What is P(S) for S = w3(a) w2(c) r1(a) w1(b) r1(c) w2(a) r4(a) w4(d) Is S serializable? 4/2/2012 Chris Clifton - CS541 36 18

CS 54100 Transactions Chris Clifton 4 April, 2012 Lemma S1, S2 conflict equivalent P(S1)=P(S2) Proof: Assume P(S1) P(S2) Ti: Ti Tj in S1 and not in S2 S1 = pi(a)... qj(a) S2 = qj(a) pi(a)... pi, qj conflict S1, S2 not conflict equivalent 4/2/2012 Chris Clifton - CS541 38 19

Note: P(S1)=P(S2) S1, S2 conflict equivalent Counter example: S1=w1(A) r2(a) w2(b) r1(b) S2=r2(A) w1(a) r1(b) w2(b) Theorem P(S1) acyclic S1 conflict serializable ( ) Assume S1 is conflict serializable Ss: Ss, S1 conflict equivalent P(Ss) = P(S1) P(S1) acyclic since P(Ss) is acyclic 4/2/2012 Chris Clifton - CS541 40 20

Theorem P(S1) acyclic S1 conflict serializable ( ) Assume P(S1) is acyclic Transform S1 as follows: (1) Take T1 to be transaction with no incident arcs (2) Move all T1 actions to the front S1 =. qj(a).p1(a).. T2 T1 T4 T3 (3) we now have S1 = < T1 actions ><... rest...> (4) repeat above steps to serialize rest! How to enforce serializable schedules? Option 1: run system, recording P(S); at end of day, check for P(S)cycles and declare if execution was good 4/2/2012 Chris Clifton - CS541 42 21

How to enforce serializable schedules? Option 2: prevent P(S) cycles from occurring T1 T2.. Scheduler Tn DB 4/2/2012 Chris Clifton - CS541 43 A locking protocol Two new actions: lock (exclusive): unlock: li (A) ui (A) T1 T2 scheduler lock table 4/2/2012 Chris Clifton - CS541 44 22

Rule #1: Well-formed transactions Ti: li(a) pi(a) ui(a)... 4/2/2012 Chris Clifton - CS541 45 Rule #2 Legal scheduler S =.. li(a)... ui(a)... no lj(a) 4/2/2012 Chris Clifton - CS541 46 23

Exercise: What schedules are legal? What transactions are well-formed? S1 = l1(a)l1(b)r1(a)w1(b)l2(b)u1(a)u1(b) r2(b)w2(b)u2(b)l3(b)r3(b)u3(b) S2 = l1(a)r1(a)w1(b)u1(a)u1(b) l2(b)r2(b)w2(b)l3(b)r3(b)u3(b) S3 = l1(a)r1(a)u1(a)l1(b)w1(b)u1(b) l2(b)r2(b)w2(b)u2(b)l3(b)r3(b)u3(b) 4/2/2012 Chris Clifton - CS541 47 Exercise: What schedules are legal? What transactions are well-formed? S1 = l1(a)l1(b)r1(a)w1(b)l2(b)u1(a)u1(b) r2(b)w2(b)u2(b)l3(b)r3(b)u3(b) S2 = l1(a)r1(a)w1(b)u1(a)u1(b) l2(b)r2(b)w2(b)l3(b)r3(b)u3(b) S3 = l1(a)r1(a)u1(a)l1(b)w1(b)u1(b) l2(b)r2(b)w2(b)u2(b)l3(b)r3(b)u3(b) 24

Schedule F T1 l1(a);read(a) A A+100;Write(A);u1(A) l1(b);read(b) B B+100;Write(B);u1(B) T2 l2(a);read(a) A Ax2;Write(A);u2(A) l2(b);read(b) B Bx2;Write(B);u2(B) 4/2/2012 Chris Clifton - CS541 49 Schedule F A B T1 T2 25 25 l1(a);read(a) A A+100;Write(A);u1(A) 125 l2(a);read(a) A Ax2;Write(A);u2(A) 250 l2(b);read(b) B Bx2;Write(B);u2(B) 50 l1(b);read(b) B B+100;Write(B);u1(B) 150 250 150 25

Rule #3 Two phase locking (2PL) for transactions Ti =. li(a)... ui(a)... no unlocks no locks 4/2/2012 Chris Clifton - CS541 51 # locks held by Ti Growing Phase Shrinking Phase Time 26

Schedule G T1 l1(a);read(a) A A+100;Write(A) l1(b); u1(a) T2 l2(a);read(a) delayed A Ax2;Write(A);l2(B) Schedule G T1 l1(a);read(a) A A+100;Write(A) l1(b); u1(a) Read(B);B B+100 Write(B); u1(b) T2 delayed l2(a);read(a) A Ax2;Write(A);l2(B) 27

Schedule G T1 l1(a);read(a) A A+100;Write(A) l1(b); u1(a) Read(B);B B+100 Write(B); u1(b) T2 delayed l2(a);read(a) A Ax2;Write(A);l2(B) l2(b); u2(a);read(b) B Bx2;Write(B);u2(B); Schedule H (T2 reversed) T1 l1(a); Read(A) A A+100;Write(A) l1(b) T2 l2(b);read(b) B Bx2;Write(B) l2(a) delayed delayed 28

Assume deadlocked transactions are rolled back They have no effect They do not appear in schedule E.g., Schedule H = This space intentionally left blank! Next step: Show that rules #1,2,3 conflictserializable schedules 29

Conflict rules for li(a), ui(a): li(a), lj(a) conflict li(a), uj(a) conflict Note: no conflict < ui(a), uj(a)>, < li(a), rj(a)>,... Theorem Rules #1,2,3 conflict (2PL) serializable schedule To help in proof: Definition Shrink(Ti) = SH(Ti) = first unlock action of Ti 30

Lemma Ti Tj in S SH(Ti) < S SH(Tj) Proof of lemma: Ti Tj means that S = pi(a) qj(a) ; p,q conflict By rules 1,2: S = pi(a) ui(a) lj(a)... qj(a) By rule 3: SH(Ti) SH(Tj) So, SH(Ti) < S SH(Tj) Theorem Rules #1,2,3 conflict (2PL) serializable schedule Proof: (1) Assume P(S) has cycle T1 T2. Tn T1 (2) By lemma: SH(T1) < SH(T2) <... < SH(T1) (3) Impossible, so P(S) acyclic (4) S is conflict serializable 31

Beyond this simple 2PL protocol, it is all a matter of improving performance and allowing more concurrency. Shared locks Multiple granularity Inserts, deletes and phantoms Other types of C.C. mechanisms Shared locks So far: S =...l1(a) r1(a) u1(a) l2(a) r2(a) u2(a) Do not conflict Instead: S=... ls1(a) r1(a) ls2(a) r2(a). us1(a) us2(a) 32

Lock actions l-ti(a): lock A in t mode (t is S or X) u-ti(a): unlock t mode (t is S or X) Shorthand: ui(a): unlock whatever modes Ti has locked A Rule #1 Well formed transactions Ti =... l-s1(a) r1(a) u1 (A) Ti =... l-x1(a) w1(a) u1 (A) 33

What about transactions that read and write same object? Option 1: Request exclusive lock Ti =...l-x1(a) r1(a)... w1(a)... u(a) What about transactions that read and write same object? Option 2: Upgrade (E.g., need to read, but don t know if will write ) Ti=... l-s1(a) r1(a)... l-x1(a) w1(a)...u(a) Think of - Get 2nd lock on A, or - Drop S, get X lock 34

Rule #2 Legal scheduler S =...l-si(a) ui(a) no l-xj(a) S =... l-xi(a) ui(a) no l-xj(a) no l-sj(a) A way to summarize Rule #2 Compatibility matrix Comp S X S true false X false false 35

Rule # 3 2PL transactions No change except for upgrades: (I) If upgrade gets more locks (e.g., S {S, X}) then no change! (II) If upgrade releases read (shared) lock (e.g., S X) - can be allowed in growing phase Theorem Rules 1,2,3 Conf.serializable for S/X locks schedules Proof: similar to X locks case Detail: l-ti(a), l-rj(a) do not conflict if comp(t,r) l-ti(a), u-rj(a) do not conflict if comp(t,r) 36

Lock types beyond S/X Examples: (1) increment lock (2) update lock Example (1): increment lock Atomic increment action: INi(A) {Read(A); A A+k; Write(A)} INi(A), INj(A) do not conflict! INi(A) +2 +10 A=7 A=5 +10 A=17 INj(A) A=15 INj(A) +2 INi(A) 37

Comp S X I S X I Comp S X I S T F F X F F F I F F T 38

Update locks A common deadlock problem with upgrades: T1 T2 l-s1(a) l-s2(a) l-x1(a) l-x2(a) --- Deadlock --- Solution If Ti wants to read A and knows it may later want to write A, it requests update lock (not shared) 39

New request Comp S X U Lock already held in S X U New request Comp S X U Lock already held in S T F T X F F F U TorF F F -> symmetric table? 40

Note: object A may be locked in different modes at the same time... S1=...l-S1(A) l-s2(a) l-u3(a) l-s4(a)? l-u4(a)? To grant a lock in mode t, mode t must be compatible with all currently held locks on object How does locking work in practice? Every system is different (E.g., may not even provide CONFLICT-SERIALIZABLE schedules) But here is one (simplified) way... 41

Sample Locking System: (1) Don t trust transactions to request/release locks (2) Hold all locks until transaction commits # locks time Ti Read(A),Write(B) lock table Scheduler, part I l(a),read(a),l(b),write(b) Scheduler, part II Read(A),Write(B) DB 42

Every possible object Lock table Conceptually A B C If null, object is unlocked Lock info for B Lock info for C... But use hash table: A H A...... Lock info for A If object not found in hash table, it is unlocked 43

Lock info for A - example transaction mode wait? Next T_link Object:A Group mode:u Waiting:yes List: T1 S no T2 U no T3 X yes To other T3 records What are the objects we lock? Relation A Relation B... Tuple A Tuple B Tuple C... Disk block A Disk block B?... DB DB DB 44

Locking works in any case, but should we choose small or large objects? If we lock large objects (e.g., Relations) Need few locks Low concurrency If we lock small objects (e.g., tuples,fields) Need more locks More concurrency We can have it both ways!! Ask any janitor to give you the solution... Stall 1 Stall 2 Stall 3 Stall 4 restroom hall 45

Example T1(IS), T2(S) R1 t1 t2 t3 t4 T1(S) Example R1 T1(IS), T2(IX) t1 t2 t3 t4 T1(S) T2(IX) 46

Multiple granularity Comp IS Holder IX S SIX Requestor IS IX S SIX X X Multiple granularity Comp IS Holder IX S SIX X Requestor IS IX S SIX X T T T T F T T T F T F F F F T F F F F F F F F F F 47

Parent locked in IS IX S SIX X Child can be locked in P C Parent locked in IS IX S SIX X Child can be locked in IS, S IS, S, IX, X, SIX [S, IS] not necessary X, IX, [SIX] none P C 48

Rules (1) Follow multiple granularity comp function (2) Lock root of tree first, any mode (3) Node Q can be locked by Ti in S or IS only if parent(q) locked by Ti in IX or IS (4) Node Q can be locked by Ti in X,SIX,IX only if parent(q) locked by Ti in IX,SIX (5) Ti is two-phase (6) Ti can unlock node Q only if none of Q s children are locked by Ti End 11/4 49

Exercise: Can T2 access object f2.2 in X mode? What locks will T2 get? T1(IX) R1 t1 T1(IX) t2 t3 t4 T1(X) f2.1 f2.2 f3.1 f3.2 Exercise: Can T2 access object f2.2 in X mode? What locks will T2 get? T1(IX) R1 t1 T1(X) t2 t3 t4 f2.1 f2.2 f3.1 f3.2 50

Exercise: Can T2 access object f3.1 in X mode? What locks will T2 get? T1(IS) R1 t1 T1(S) t2 t3 t4 f2.1 f2.2 f3.1 f3.2 Exercise: Can T2 access object f2.2 in S mode? What locks will T2 get? T1(SIX) R1 t1 T1(IX) t2 t3 t4 T1(X) f2.1 f2.2 f3.1 f3.2 51

Exercise: Can T2 access object f2.2 in X mode? What locks will T2 get? T1(SIX) R1 t1 T1(IX) t2 t3 t4 T1(X) f2.1 f2.2 f3.1 f3.2 Insert + delete operations A... Z a Insert 52

Modifications to locking rules: (1) Get exclusive lock on A before deleting A (2) At insert A operation by Ti, Ti is given exclusive lock on A Still have a problem: Phantoms Example: relation R (E#,name, ) constraint: E# is key use tuple locking R E# Name. o1 55 Smith o2 75 Jones 53

T1: Insert <99,Gore, > into R T2: Insert <99,Bush, > into R T1 S1(o1) S1(o2) Check Constraint... Insert o3[99,gore,..] T2 S2(o1) S2(o2) Check Constraint... Insert o4[99,bush,..] Solution Use multiple granularity tree Before insert of node Q, lock parent(q) in X mode R1 t1 t2 t3 54

Back to example T1: Insert<99,Gore> T2: Insert<99,Bush> T1 T2 X1(R) X2(R) delayed Check constraint Insert<99,Gore> U(R) X2(R) Check constraint Oops! e# = 99 already in R! Instead of using R, can use index on R: Example: R Index 0<E#<100 Index 100<E#<200... E#=2 E#=5... E#=107 E#=109... 55

This approach can be generalized to multiple indexes... CS 541 Concurrency Control November 13, 2007 Fall 2007 Chris Clifton - CS541 112 56

Next: Tree-based concurrency control Validation concurrency control Example all objects accessed through root, following pointers A T1 lock T1 lock D B T1 lock C E F can we release A lock if we no longer need A?? 57

Idea: traverse like Monkey Bars T1 lock A T1 lock T1 lock D B T1 lock C E F Why does this work? Assume all Ti start at root; exclusive lock Ti Tj Ti locks root before Tj Root Q Ti Tj Actually works if we don t always start at root 58

Rules: tree protocol (exclusive locks) (1) First lock by Ti may be on any item (2) After that, item Q can be locked by Ti only if parent(q) locked by Ti (3) Items may be unlocked at any time (4) After Ti unlocks Q, it cannot relock Q Tree-like protocols are used typically for B-tree concurrency control Root E.g., during insert, do not release parent lock, until you are certain child does not have to split 59

Validation Transactions have 3 phases: (1) Read all DB values read writes to temporary storage no locking (2) Validate check if schedule so far is serializable (3) Write if validate ok, write to DB Key idea Make validation atomic If T1, T2, T3, is validation order, then resulting schedule will be conflict equivalent to Ss = T1 T2 T3... 60

To implement validation, system keeps two sets: FIN = transactions that have finished phase 3 (and are all done) VAL = transactions that have successfully finished phase 2 (validation) Example of what validation must prevent: RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C} = T2 start T3 start T2 validated T3 validated time 61

allow Example of what validation must prevent: RS(T2)={B} RS(T3)={A,B} WS(T2)={B,D} WS(T3)={C} = T2 start T3 start T2 validated T3 validated T2 finish phase 3 T3 start time Another thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D} T2 validated T3 validated BAD: w3(d) w2(d) finish T2 time 62

allow Another thing validation must prevent: RS(T2)={A} RS(T3)={A,B} WS(T2)={D,E} WS(T3)={C,D} T2 validated finish T2 T3 validated finish T2 time Validation rules for Tj: (1) When Tj starts phase 1: ignore(tj) FIN (2) at Tj Validation: if check (Tj) then [ VAL VAL U {Tj}; do write phase; FIN FIN U {Tj} ] 63

Check (Tj): For Ti VAL - IGNORE (Tj) DO IF [ WS(Ti) RS(Tj) OR Ti FIN ] THEN RETURN false; RETURN true; Is this check too restrictive? Improving Check(Tj) For Ti VAL - IGNORE (Tj) DO IF [ WS(Ti) RS(Tj) OR (Ti FIN AND WS(Ti) WS(Tj) )] THEN RETURN false; RETURN true; 64

Exercise: start validate finish U: RS(U)={B} WS(U)={D} W: RS(W)={A,D} WS(W)={A,C} T: RS(T)={A,B} WS(T)={A,C} V: RS(V)={B} WS(V)={D,E} Validation (also called optimistic concurrency control) is useful in some cases: - Conflicts rare - System resources plentiful - Have real time constraints 65

Summary Have studied C.C. mechanisms used in practice - 2 PL - Multiple granularity - Tree (index) protocols - Validation 66