Introduction to Database Systems CSE 444

Similar documents
Pessimistic v.s. Optimistic. Outline. Timestamps. Timestamps. Timestamps. CSE 444: Database Internals. Main invariant:

Lecture 7: Transactions Concurrency Control

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

Introduction to Database Systems CSE 444

CSE544 Transac-ons: Concurrency Control. Lectures #5-6 Thursday, January 20, 2011 Tuesday, January 25, 2011

CSEP 544: Lecture 07. Transactions Part 2: Concurrency Control. CSEP544 - Fall

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

Lecture 5: Transactions in SQL. Tuesday, February 6, 2007

DB2 Lecture 10 Concurrency Control

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

Implementing Isolation

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

Chapter 15 : Concurrency Control

CSE 444: Database Internals. Lectures Transactions

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

Introduction to Data Management CSE 414

Unit 10.5 Transaction Processing: Concurrency Zvi M. Kedem 1

Chapter 16 : Concurrency Control

Chapter 10 : Concurrency Control

Database Systems CSE 414

Optimistic Concurrency Control. April 13, 2017

Outline. Optimistic CC (Kung&Robinson) Carnegie Mellon Univ. Dept. of Computer Science Database Applications

Concurrency Control 9-1

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

14 Concurrency control

Time stamp ordering. 5 Non-locking concurrency Control

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

Chapter 6 Distributed Concurrency Control

Introduction to Data Management CSE 344

CSE 344 MARCH 25 TH ISOLATION

Transaction Management. Concurrency Control (4)

Comp 5311 Database Management Systems. 14. Timestamp-based Protocols

Chapter 10: Concurrency Control! Chapter 10 : Concurrency Control! Intuition of Lock-based Protocols! Lock-Based Protocols!

Chapter 10 : Concurrency Control!

Database Systems CSE 414

Chapter 13 : Concurrency Control

Concurrency Control. Transaction Management. Lost Update Problem. Need for Concurrency Control. Concurrency control

Information Systems (Informationssysteme)

Optimistic Concurrency Control. April 18, 2018

Database Management Systems Concurrency Control

References. Concurrency Control. Administração e Optimização de Bases de Dados 2012/2013. Helena Galhardas e

Announcements. Review of Schedules. Scheduler. Notation. Pessimistic Scheduler. CSE 444: Database Internals. Serializability.

Distributed Databases Systems

Multi-User-Synchronization

Chapter 9: Concurrency Control

Lecture 22 Concurrency Control Part 2

Transaction Concept. Two main issues to deal with:

CSE 344 MARCH 9 TH TRANSACTIONS

12.3 Nonlocking schedulers. Timestamp ordering. TO concurrency control guarantees conflict-serializable schedules

Introduction to Data Management CSE 344

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

Chapter 12 : Concurrency Control

Distributed Transaction Management

Multiversion Schemes to achieve Snapshot Isolation Timestamped Based Protocols

CSE 444: Database Internals. Lectures 13 Transaction Schedules

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

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

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

Database transactions

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

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

CSE 344 MARCH 5 TH TRANSACTIONS

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

Concurrency control 12/1/17

Chapter 14: Transactions

SQL: Transactions. Introduction to Databases CompSci 316 Fall 2017

Transactions: Definition. Concurrent transactions: Problems

6 Multiversion Concurrency Control

CS5412: TRANSACTIONS (I)

Chapter 15: Transactions

SQL: Transactions. Announcements (October 2) Transactions. CPS 116 Introduction to Database Systems. Project milestone #1 due in 1½ weeks

T ransaction Management 4/23/2018 1

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. Lecture 8. Transactions. ACID Properties. Transaction Concept. Example of Fund Transfer. Example of Fund Transfer (Cont.

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

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

Intro to Transactions

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

Introduction to Data Management CSE 344

CSIT5300: Advanced Database Systems

6.830 Lecture Recovery 10/30/2017

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

CS322: Database Systems Transactions

CSIT5300: Advanced Database Systems

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

Outline. Database Tuning. Undesirable Phenomena of Concurrent Transactions. Isolation Guarantees (SQL Standard) Concurrency Tuning.

Database System Concepts

Transactions and Concurrency Control

Foundation of Database Transaction Processing. Copyright 2012 Pearson Education, Inc.

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

Transactions and Concurrency Control. Dr. Philip Cannata

Introduction to Data Management CSE 344

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)

Transaction Management: Concurrency Control, part 2

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

230 Chapter 17. (c) Otherwise, T writes O and WTS(O) is set to TS(T).

Advances in Data Management Transaction Management A.Poulovassilis

Chapter 15: Transactions

Outline. Database Management and Tuning. Isolation Guarantees (SQL Standard) Undesirable Phenomena of Concurrent Transactions. Concurrency Tuning

Transaction Processing: Basics - Transactions

Transcription:

Introduction to Database Systems CSE 444 Lecture 12 Transactions: concurrency control (part 2) CSE 444 - Summer 2010 1

Outline Concurrency control by timestamps (18.8) Concurrency control by validation (18.9) Concurrency control by snapshot isolation But first, a word about Phantoms CSE 444 - Summer 2010 2

Phantom Problem So far we have assumed the database to be a static collection of elements (=tuples) If tuples are inserted/deleted then the phantom problem appears CSE 444 Summer 2010 3

The Phantom Problem Phantom = tuple visible only during some part of the transaction T1: select count(*) from R where price>20................ select count(*) from R where price>20 T2:........ insert into R(name,price) values( Gizmo, 50).... R 1 (X), R 1 (Y), R 1 (Z), W 2 (New), R 1 (X), R 1 (Y), R 1 (Z), R 1 (New) The schedule is conflict-serializable, yet we get different counts! Not serializible because of phantoms. 4

Dealing with Phantoms In a static database: Conflict serializability implies serializability In a dynamic database, this may fail due to phantoms Strict 2PL guarantees conflict serializability, but not serializability Expensive ways of dealing with phantoms: Lock the entire table, or Lock the index entry for price (if index is available) Or use predicate locks (a lock on an arbitrary predicate) Serializable transactions are very expensive 5

Concurrency Control Mechanisms Pessimistic: Locks Optimistic Timestamp based: basic, multiversion Validation Snapshot isolation: a variant of both CSE 444 - Summer 2010 6

Timestamps Each transaction receives a unique timestamp TS(T) Could be: The system s clock A unique counter, incremented by the scheduler CSE 444 - Summer 2010 7

Timestamps Main invariant: The timestamp order defines the serialization order of the transaction Will generate a schedule that is view-equivalent to a serial schedule, and is recoverable CSE 444 - Summer 2010 8

Main Idea For any two conflicting actions, ensure that their order is the serialized order: In each of these cases w U (X)... r T (X) r U (X)... w T (X) w U (X)... w T (X) When T requests r/w T (X), need to check TS(U) <= TS(T) Read too late? Write too late? 9

Timestamps With each element X, associate RT(X) = the highest timestamp of any transaction ti that t read X WT(X) = the highest timestamp of any transaction that wrote X C(X) = the commit bit: true when transaction with highest timestamp that wrote X committed If 1 element = 1 page, these are associated with each page X in the buffer pool CSE 444 - Summer 2010 10

Time-based Scheduling Note: simple version that ignores the commit bit If transactions abort, may result in non-recoverable schedule Transaction wants to read element X If TS(T) < WT(X) then ROLLBACK Else read X and update RT(X) to larger of TS(T) or RT(X) Transaction wants to write element X If TS(T) < RT(X) then ROLLBACK Else if TS(T) < WT(X) ignore write & continue (Thomas Write Rule) Otherwise, write X and update WT(X) to TS(T) CSE 444 - Summer 2010 11

Details Read too late: T wants to read X, and TS(T) < WT(X) START(T) START(U) w U (X)... r T (X) Need to rollback T! CSE 444 - Summer 2010 12

Details Write too late: T wants to write X, and TS(T) < RT(X) START(T) START(U) r U (X)... w T (X) Need to rollback T! CSE 444 - Summer 2010 13

Details Write too late, but we can still handle it: T wants to write X, and TS(T) >= RT(X) but WT(X) > TS(T) START(T) START(V) w V (X)... w T (X) Don t write X at all! (Thomas rule) CSE 444 - Summer 2010 14

Ensuring Recoverable Schedules Recall the definition: if a transaction reads an element, then the transaction that wrote it must have already committed Use the commit bit C(X) to keep track if the transaction ti that t last wrote X has committed CSE 444 - Summer 2010 15

Ensuring Recoverable Schedules Read dirty data: T wants to read X, and WT(X) < TS(T) Seems OK, but START(U) START(T) w U U( (X)... r T T( (X) ABORT(U) If C(X)=false, T needs to wait for it to become true CSE 444 - Summer 2010 16

Ensuring Recoverable Schedules Need to revise Thomas rule: T wants to write X, and WT(X) > TS(T) Seems OK not to write at all, but START(T) START(U) w U U( (X)... w T T( (X) ABORT(U) If C(X)=false, T needs to wait for it to become true CSE 444 - Summer 2010 17

Timestamp-based Scheduling When a transaction T requests r(x) or w(x), the scheduler examines RT(X), WT(X), C(X), and decides one of: To grant the request, or To rollback T (and restart with later timestamp) To delay T until C(X) = true CSE 444 - Summer 2010 18

Timestamp-based Scheduling Transaction wants to READ element X If TS(T) < WT(X) then ROLLBACK Else If C(X) = false, then WAIT Else READ and update RT(X) to larger of TS(T) or RT(X) Transaction wants to WRITE element X If TS(T) < RT(X) then ROLLBACK Else if TS(T) < WT(X) Then If C(X) = false then WAIT else IGNORE write (Thomas Write Rule) Otherwise, WRITE, and update WT(X)=TS(T), C(X)=false See book sec. 18.8.4 8 for detailed rules CSE 444 Summer 2010 19

Summary of Timestamp-based Conflict-serializable Scheduling Recoverable Even avoids cascading aborts Does NOT handle phantoms CSE 444 - Summer 2010 20

Multiversion Timestamp When transaction T requests r(x) but WT(X) > TS(T), then T must rollback Idea: keep multiple versions of X: X t, X t-1, X t-2,... TS(X t ) > TS(X t-1 ) > TS(X t-2 ) >... Let T read an older version, with appropriate timestamp CSE 444 - Summer 2010 21

Details When w T (X) occurs, create a new version, denoted X t where t = TS(T) When r T (X) occurs, find most recent version X t such that t < TS(T) Notes: WT(X t ) = t and it never changes RT(X t ) must still be maintained to check legality of writes Can delete X t if we have a later version X t1 and all active transactions T have TS(T) > t1 CSE 444 - Summer 2010 22

Concurrency Control by Validation Each transaction T defines a read set RS(T) and a write set WS(T) Each transaction ti proceeds in three phases: Read all elements in RS(T). Time = START(T) Validate (may need to rollback). Time = VAL(T) Write all elements in WS(T). Time = FIN(T) Main invariant: the serialization order is VAL(T) CSE 444 - Summer 2010 23

Avoid r T (X) - w U (X) Conflicts START(U) VAL(U) FIN(U) U: Read phase Validate Write phase conflicts T: Read phase Validate? START(T) IF RS(T) WS(U) and FIN(U) > START(T) (U has validated and U has not finished before T begun) Then ROLLBACK(T) CSE 444 - Summer 2010 24

Avoid w T (X) - w U (X) Conflicts START(U) VAL(U) FIN(U) U: Read phase Validate Write phase conflicts T: Read phase Validate Write phase? START(T) VAL(T) IF WS(T) WS(U) and FIN(U) > VAL(T) (U has validated and U has not finished before T validates) Then ROLLBACK(T) CSE 444 - Summer 2010 25

Snapshot Isolation Another optimistic concurrency control method Very efficient, and very popular Oracle, PostgreSQL, SQL Server 2005 Prevents many classical anomalies BUT Not serializable (!), yet ORACLE uses it even for SERIALIZABLE transactions! CSE 444 - Summer 2010 26

Snapshot Isolation Rules Each transactions receives a timestamp TS(T) Transaction T sees database snapshot at time TS(T) When T commits, updated pages are written to disk Write/write conflicts resolved by first committer wins rule Read/write conflicts are ignored CSE 444 - Summer 2010 27

Snapshot Isolation (Details) Multiversion concurrency control: Versions of X: Xt1, Xt2, Xt3,... When T reads X, return X TS(T). When T writes X: if other transaction updated X, abort Not faithful to first committer rule, because the other transaction U might have committed after T. But once we abort T, U becomes the first committer CSE 444 - Summer 2010 28

What Works and What Not No dirty reads (Why? ) No inconsistent reads (Why?) A: Each transaction reads a consistent snapshot No lost updates ( first committer wins ) Moreover: no reads are ever delayed However: read-write conflicts not caught! CSE 444 - Summer 2010 29

Write Skew T1: READ(X); if X >= 50 then Y = -50; WRITE(Y) COMMIT T2: READ(Y); if Y >= 50 then X = -50; WRITE(X) COMMIT In our notation: R 1 (X), R 2 (Y), W 1 (Y), W 2 (X), C 1,C 2 Starting with X=50,Y=50, we end with X=-50, Y=-50. Non-serializable!!! CSE 444 - Summer 2010 30

Write Skews Can Be Serious Acidicland had two viceroys, Delta and Rho Budget had two registers: taxes, and spendyng They had high taxes and low spending Delta: READ(taXes); if taxes = High then { spendyng = Raise ; WRITE(spendYng) } COMMIT Rho: READ(spendYng); if spendyng = Low then {taxes = Cut ; WRITE(taXes) } COMMIT and they ran a deficit ever since. 31

Tradeoffs Pessimistic Concurrency Control (Locks): Great when there are many conflicts Poor when there are few conflicts (overhead) Optimistic Concurrency Control (Timestamps): Poor when there are many conflicts (rollbacks) Great when there are few conflicts Compromise READ ONLY transactions timestamps READ/WRITE transactions locks CSE 444 - Summer 2010 32