CSE 444: Database Internals. Lectures 13 Transaction Schedules

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

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

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

CSE 444: Database Internals. Lectures Transactions

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

5/17/17. Announcements. Review: Transactions. Outline. Review: TXNs in SQL. Review: ACID. Database Systems CSE 414.

Database Systems CSE 414

CSE 344 MARCH 21 ST TRANSACTIONS

CSE 344 MARCH 5 TH TRANSACTIONS

Lecture 7: Transactions Concurrency Control

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

Introduction to Data Management CSE 344

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

Database Systems CSE 414

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

CSE 344 MARCH 25 TH ISOLATION

Database Systems CSE 414

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

Transaction Management & Concurrency Control. CS 377: Database Systems

Introduction to Data Management CSE 414

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

Overview of Transaction Management

CSE 344 MARCH 9 TH TRANSACTIONS

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

Transaction Management: Introduction (Chap. 16)

Database transactions

COURSE 1. Database Management Systems

Intro to Transaction Management

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

Concurrency Control & Recovery

Database Systems. Announcement

CS122 Lecture 15 Winter Term,

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

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

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

TRANSACTION MANAGEMENT

Transactions and Concurrency Control

Transaction Processing. Introduction to Databases CompSci 316 Fall 2018

Introduction to Transaction Management

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

Intro to Transactions

Database Systems CSE 414

CHAPTER: TRANSACTIONS

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

Overview. Introduction to Transaction Management ACID. Transactions

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

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

Concurrency control 12/1/17

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

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

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

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

CS322: Database Systems Transactions

Transaction Management Overview

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

CMPT 354: Database System I. Lecture 11. Transaction Management

Lecture 20 Transactions

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

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

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

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

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

Concurrency Control & Recovery

Transaction Management. Chapter 14

Database System Concepts

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

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

Transaction Management and Concurrency Control. Chapter 16, 17

Introduction to Database Systems CSE 444. Transactions. Lecture 14: Transactions in SQL. Why Do We Need Transactions. Dirty Reads.

Introduction. Storage Failure Recovery Logging Undo Logging Redo Logging ARIES

CSE 190D Database System Implementation

Introduction to Data Management CSE 344

Databases - Transactions

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

CS122 Lecture 19 Winter Term,

Chapter 22. Transaction Management

Introduction to Data Management. Lecture #18 (Transactions)

Database Management Systems 2010/11

Chapter 13: Transactions

Lectures 8 & 9. Lectures 7 & 8: Transactions

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

Advanced Databases. Transactions. Nikolaus Augsten. FB Computerwissenschaften Universität Salzburg

Roadmap of This Lecture

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

Transaction Concept. Two main issues to deal with:

Lecture 14: Transactions in SQL. Friday, April 27, 2007

Introduction to Data Management. Lecture #24 (Transactions)

Transactions. Lecture 8. Transactions. ACID Properties. Transaction Concept. Example of Fund Transfer. Example of Fund Transfer (Cont.

Transactions and Concurrency Control. Dr. Philip Cannata

Implementing Isolation

Database System Concepts

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

Chapter 14: Transactions

Chapter 9: Transactions

Database Management Systems

Chapter 15: Transactions

Advanced Databases (SE487) Prince Sultan University College of Computer and Information Sciences. Dr. Anis Koubaa. Spring 2014

Transcription:

CSE 444: Database Internals Lectures 13 Transaction Schedules CSE 444 - Winter 2018 1

About Lab 3 In lab 3, we implement transactions Focus on concurrency control Want to run many transactions at the same time Transactions want to read and write same pages Will use locks to ensure conflict serializable execution Use strict 2PL Build your own lock manager Understand how locking works in depth Ensure transactions rather than threads hold locks Many threads can execute different pieces of the same transaction Need to detect deadlocks and resolve them by aborting a transaction But use Java synchronization to protect your data structures CSE 444 - Winter 2018 2

Motivating Example Client 1: UPDATE Budget SET money=money-100 WHERE pid = 1 Client 2: SELECT sum(money) FROM Budget UPDATE Budget SET money=money+60 WHERE pid = 2 UPDATE Budget SET money=money+40 WHERE pid = 3 Would like to treat each group of instructions as a unit CSE 444 - Winter 2018 3

Transaction Definition: a transaction is a sequence of updates to the database with the property that either all complete, or none completes (all-or-nothing). START TRANSACTION [SQL statements] May be omitted if autocommit is off: first SQL query starts txn COMMIT or ROLLBACK (=ABORT) In ad-hoc SQL: each statement = one transaction This is referred to as autocommit CSE 444 - Winter 2018 4

Motivating Example START TRANSACTION UPDATE Budget SET money=money-100 WHERE pid = 1 SELECT sum(money) FROM Budget UPDATE Budget SET money=money+60 WHERE pid = 2 UPDATE Budget SET money=money+40 WHERE pid = 3 COMMIT (or ROLLBACK) With autocommit and without START TRANSACTION, each SQL command is a transaction CSE 444 - Winter 2018 5

ROLLBACK If the app gets to a place where it can t complete the transaction successfully, it can execute ROLLBACK This causes the system to abort the transaction Database returns to a state without any of the changes made by the transaction Several reasons: user, application, system CSE 444 - Winter 2018 6

Transactions Major component of database systems Critical for most applications; arguably more so than SQL Turing awards to database researchers: Charles Bachman 1973 Edgar Codd 1981 for inventing relational dbs Jim Gray 1998 for inventing transactions Mike Stonebraker 2015 for INGRES and Postgres And many other ideas after that CSE 444 - Winter 2018 7

ACID Properties Atomicity: Either all changes performed by transaction occur or none occurs Consistency: A transaction as a whole does not violate integrity constraints Isolation: Transactions appear to execute one after the other in sequence Durability: If a transaction commits, its changes will survive failures CSE 444 - Winter 2018 8

What Could Go Wrong? Why is it hard to provide ACID properties? Concurrent operations Isolation problems We saw one example earlier Failures can occur at any time Atomicity and durability problems Later lectures Transaction may need to abort CSE 444 - Winter 2018 9

Terminology Needed For Lab 3 Buffer Manager Policies STEAL or NO-STEAL Can an update made by an uncommitted transaction overwrite the most recent committed value of a data item on disk? FORCE or NO-FORCE Should all updates of a transaction be forced to disk before the transaction commits? Easiest for recovery: NO-STEAL/FORCE (lab 3) Highest performance: STEAL/NO-FORCE (lab 4) We will get back to this next week CSE 444 - Winter 2018 10

Transaction Isolation CSE 444 - Winter 2018 11

Concurrent Execution Problems Write-read conflict: dirty read, inconsistent read A transaction reads a value written by another transaction that has not yet committed Read-write conflict: unrepeatable read A transaction reads the value of the same object twice. Another transaction modifies that value in between the two reads Write-write conflict: lost update Two transactions update the value of the same object. The second one to write the value overwrites the first change CSE 444 - Winter 2018 12

Schedules A schedule is a sequence of interleaved actions from all transactions CSE 444 - Winter 2018 13

Example A and B are elements in the database t and s are variables in tx source code T1 T2 READ(A, t) READ(A, s) t := t+100 s := s*2 WRITE(A, t) WRITE(A,s) READ(B, t) READ(B,s) t := t+100 s := s*2 WRITE(B,t) WRITE(B,s) CSE 444 - Winter 2018 14

A Serial Schedule T1 READ(A, t) t := t+100 WRITE(A, t) READ(B, t) t := t+100 WRITE(B,t) T2 READ(A,s) s := s*2 WRITE(A,s) READ(B,s) s := s*2 WRITE(B,s) CSE 444 - Winter 2018 15

Serializable Schedule A schedule is serializable if it is equivalent to a serial schedule CSE 444 - Winter 2018 16

A Serializable Schedule T1 READ(A, t) t := t+100 WRITE(A, t) READ(B, t) t := t+100 WRITE(B,t) This is a serializable schedule. This is NOT a serial schedule T2 READ(A,s) s := s*2 WRITE(A,s) READ(B,s) s := s*2 WRITE(B,s) CSE 444 - Winter 2018 17

A Non-Serializable Schedule T1 READ(A, t) t := t+100 WRITE(A, t) READ(B, t) t := t+100 WRITE(B,t) T2 READ(A,s) s := s*2 WRITE(A,s) READ(B,s) s := s*2 WRITE(B,s) CSE 444 - Winter 2018 18

Serializable Schedules The role of the scheduler is to ensure that the schedule is serializable Q: Why not run only serial schedules? I.e. run one transaction after the other? CSE 444 - Winter 2018 19

Serializable Schedules The role of the scheduler is to ensure that the schedule is serializable Q: Why not run only serial schedules? I.e. run one transaction after the other? A: Because of very poor throughput due to disk latency. Lesson: main memory databases may schedule TXNs serially CSE 444 - Winter 2018 20

Still Serializable, but T1 READ(A, t) t := t+100 WRITE(A, t) Schedule is serializable because t=t+100 and s=s+200 commute READ(B, t) t := t+100 WRITE(B,t) T2 READ(A,s) s := s + 200 WRITE(A,s) READ(B,s) s := s + 200 WRITE(B,s) CSE 444 - Winter 2018 21 we don t expect the scheduler to schedule this

To Be Practical Assume worst case updates: Assume cannot commute actions done by transactions Therefore, we only care about reads and writes Transaction = sequence of R(A) s and W(A) s T 1 : r 1 (A); w 1 (A); r 1 (B); w 1 (B) T 2 : r 2 (A); w 2 (A); r 2 (B); w 2 (B) CSE 444 - Winter 2018 22

Conflicts Write-Read WR Read-Write RW Write-Write WW CSE 444 - Winter 2018 23

Conflict Serializability Conflicts: Two actions by same transaction T i : r i (X); w i (Y) Two writes by T i, T j to same element w i (X); w j (X) w i (X); r j (X) Read/write by T i, T j to same element r i (X); w j (X) CSE 444 - Winter 2018 24

Conflict Serializability Definition A schedule is conflict serializable if it can be transformed into a serial schedule by a series of swappings of adjacent non-conflicting actions Every conflict-serializable schedule is serializable The converse is not true in general CSE 444 - Winter 2018 25

Conflict Serializability Example: r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B) CSE 444 - Winter 2018 26

Conflict Serializability Example: r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 1 (B); w 1 (B); r 2 (A); w 2 (A); r 2 (B); w 2 (B) CSE 444 - Winter 2018 27

Conflict Serializability Example: r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 1 (B); w 1 (B); r 2 (A); w 2 (A); r 2 (B); w 2 (B) CSE 444 - Winter 2018 28

Conflict Serializability Example: r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 2 (A); r 1 (B); w 2 (A); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 1 (B); w 1 (B); r 2 (A); w 2 (A); r 2 (B); w 2 (B) CSE 444 - Winter 2018 29

Conflict Serializability Example: r 1 (A); w 1 (A); r 2 (A); w 2 (A); r 1 (B); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 2 (A); r 1 (B); w 2 (A); w 1 (B); r 2 (B); w 2 (B) r 1 (A); w 1 (A); r 1 (B); r 2 (A); w 2 (A); w 1 (B); r 2 (B); w 2 (B). r 1 (A); w 1 (A); r 1 (B); w 1 (B); r 2 (A); w 2 (A); r 2 (B); w 2 (B) CSE 444 - Winter 2018 30

Testing for Conflict-Serializability Precedence graph: A node for each transaction T i, An edge from T i to T j whenever an action in T i conflicts with, and comes before an action in T j The schedule is serializable iff the precedence graph is acyclic CSE 444 - Winter 2018 31

Example 1 r 2 (A); r 1 (B); w 2 (A); r 3 (A); w 1 (B); w 3 (A); r 2 (B); w 2 (B) 1 2 3 CSE 444 - Winter 2018 32

Example 1 r 2 (A); r 1 (B); w 2 (A); r 3 (A); w 1 (B); w 3 (A); r 2 (B); w 2 (B) B A 1 2 3 This schedule is conflict-serializable CSE 444 - Winter 2018 33

Example 2 r 2 (A); r 1 (B); w 2 (A); r 2 (B); r 3 (A); w 1 (B); w 3 (A); w 2 (B) 1 2 3 CSE 444 - Winter 2018 34

Example 2 r 2 (A); r 1 (B); w 2 (A); r 2 (B); r 3 (A); w 1 (B); w 3 (A); w 2 (B) B 1 A 2 B 3 This schedule is NOT conflict-serializable CSE 444 - Winter 2018 35

View Equivalence A serializable schedule need not be conflict serializable, even under the worst case update assumption w 1 (X); w 2 (X); w 2 (Y); w 1 (Y); w 3 (Y); Is this schedule conflict-serializable? CSE 444 - Winter 2018 36

View Equivalence A serializable schedule need not be conflict serializable, even under the worst case update assumption w 1 (X); w 2 (X); w 2 (Y); w 1 (Y); w 3 (Y); Is this schedule conflict-serializable? No CSE 444 - Winter 2018 37

View Equivalence A serializable schedule need not be conflict serializable, even under the worst case update assumption Lost write w 1 (X); w 2 (X); w 2 (Y); w 1 (Y); w 3 (Y); w 1 (X); w 1 (Y); w 2 (X); w 2 (Y); w 3 (Y); Equivalent, but not conflict-equivalent CSE 444 - Winter 2018 38

View Equivalence T1 T2 T3 W1(X) W2(X) W2(Y) CO2 W1(Y) CO1 Lost W3(Y) CO3 T1 T2 T3 W1(X) W1(Y) CO1 W2(X) W2(Y) CO2 W3(Y) CO3 Serializable, but not conflict serializable CSE 444 - Winter 2018 39

View Equivalence Two schedules S, S are view equivalent if: If T reads an initial value of A in S, then T reads the initial value of A in S If T reads a value of A written by T in S, then T reads a value of A written by T in S If T writes the final value of A in S, then T writes the final value of A in S CSE 444 - Winter 2018 40

View-Serializability A schedule is view serializable if it is view equivalent to a serial schedule Remark: If a schedule is conflict serializable, then it is also view serializable But not vice versa CSE 444 - Winter 2018 41

Schedules with Aborted Transactions When a transaction aborts, the recovery manager undoes its updates But some of its updates may have affected other transactions! CSE 444 - Winter 2018 42

Schedules with Aborted Transactions T1 R(A) W(A) Abort T2 R(A) W(A) R(B) W(B) Commit What s wrong? CSE 444 - Winter 2018 43

Schedules with Aborted Transactions T1 R(A) W(A) Abort T2 R(A) W(A) R(B) W(B) Commit What s wrong? Cannot abort CSE T1 444 because - Winter 2018 cannot undo T244

Recoverable Schedules A schedule is recoverable if: It is conflict-serializable, and Whenever a transaction T commits, all transactions who have written elements read by T have already committed CSE 444 - Winter 2018 45

Recoverable Schedules T1 R(A) W(A)? T2 R(A) W(A) R(B) W(B) Commit T1 R(A) W(A) Commit T2 R(A) W(A) R(B) W(B) Commit Nonrecoverable CSE 444 - Winter 2018 Recoverable 46

Recoverable Schedules T1 T2 T3 T4 R(A) W(A) R(A) W(A) R(B) W(B) R(B) W(B) R(C) W(C) R(C) W(C) R(D) W(D) Abort CSE 444 - Winter 2018 How do we recover? 47

Cascading Aborts If a transaction T aborts, then we need to abort any other transaction T that has read an element written by T A schedule avoids cascading aborts if whenever a transaction reads an element, the transaction that has last written it has already committed. CSE 444 - Winter 2018 48

Avoiding Cascading Aborts T1 R(A) W(A)... T2 R(A) W(A) R(B) W(B)... With cascading aborts T1 R(A) W(A) Commit T2 R(A) W(A) R(B) W(B)... Without cascading aborts CSE 444 - Winter 2018 49

Review of Schedules Serializability Recoverability Serial Serializable Conflict serializable View serializable Recoverable Avoids cascading deletes CSE 444 - Winter 2018 50

Scheduler The scheduler: Module that schedules the transaction s actions, ensuring serializability Two main approaches Pessimistic: locks Optimistic: timestamps, multi-version, validation CSE 444 - Winter 2018 51