Database Systems CSE 414

Similar documents
Database Systems CSE 414

CSE 344 MARCH 25 TH ISOLATION

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

CSE 344 MARCH 9 TH TRANSACTIONS

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

CSE 344 MARCH 5 TH TRANSACTIONS

CSE 444: Database Internals. Lectures Transactions

Database Systems CSE 414

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

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

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

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

Lecture 7: Transactions Concurrency Control

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

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

CSE 444: Database Internals. Lectures 13 Transaction Schedules

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

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

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

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

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

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)

CSE 344 MARCH 21 ST TRANSACTIONS

Database Systems CSE 414

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

TRANSACTION MANAGEMENT

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

Concurrency control 12/1/17

Transactions and Concurrency Control. Dr. Philip Cannata

CSE 190D Database System Implementation

Introduction to Database Systems CSE 444

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

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

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

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

Implementing Isolation

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

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

CSC 261/461 Database Systems Lecture 24

Database Systems. Announcement

Chapter 22. Transaction Management

Transaction Management & Concurrency Control. CS 377: Database Systems

Practice and Applications of Data Management CMPSCI 345. Lecture 13: Transactions

CSE 530A ACID. Washington University Fall 2013

Example: Transfer Euro 50 from A to B

DATABASE DESIGN I - 1DL300

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

Database System Concepts

Intro to Transaction Management

Lecture 21. Lecture 21: Concurrency & Locking

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

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

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

Transaction Management: Introduction (Chap. 16)

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

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

Overview. Introduction to Transaction Management ACID. Transactions

Intro to Transactions

6.830 Lecture Transactions October 23, 2017

Information Systems (Informationssysteme)

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

Topics in Reliable Distributed Systems

CS352 Lecture - Concurrency

Transactions and Concurrency Control

Transaction Management Exercises KEY

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

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

Chapter 7 (Cont.) Transaction Management and Concurrency Control

Transactions and Isolation

CS352 Lecture - Concurrency

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

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

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

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

Database System Concepts

Advances in Data Management Transaction Management A.Poulovassilis

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

Database transactions

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

Transaction Overview and Concurrency Control

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

Database Principles: Fundamentals of Design, Implementation, and Management Tenth Edition. Chapter 13 Managing Transactions and Concurrency

Lecture 20 Transactions

Concurrency Control & Recovery

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

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

Overview of Transaction Management

Advanced Databases 2006 Based on slides from Kifer et al., Shasha et al. LOCKING

Chapter 9: Transactions


Database Management Systems 2010/11

2 nd Semester 2009/2010

Transaction Concept. Two main issues to deal with:

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

Database Management Systems

Transcription:

Database Systems CSE 414 Lecture 22: Transaction Implementations CSE 414 - Spring 2017 1

Announcements WQ7 (last!) due on Sunday HW7: due on Wed, May 24 using JDBC to execute SQL from Java using SQL Server via Azure CSE 414 - Spring 2017 2

Recap What are transactions? Why do we need them? Maintain ACID properties via schedules We focus on the isolation property We briefly discussed consistency & durability We do not discuss atomicity Ensure conflict-serializable schedules with locks CSE 414 - Spring 2017 3

Implementing a Scheduler Major differences between database vendors Locking Scheduler Aka pessimistic concurrency control SQLite, SQL Server, DB2 Multiversion Concurrency Control (MVCC) Aka optimistic concurrency control Postgres, Oracle We discuss only locking in 414 CSE 414 - Spring 2017 4

Locking Scheduler Simple idea: Each element has a unique lock Each transaction must first acquire the lock before reading/writing that element If lock is taken by another transaction, then wait The transaction must release the lock(s) By using locks, scheduler can ensure conflict-serializability CSE 414 - Spring 2017 5

What Data Elements are Locked? Major differences between vendors: Lock on the entire database SQLite Lock on individual records SQL Server, DB2, etc. can be even more fine-grained by having different types of locks (allows more txns to run simultaneously) CSE 414 - Spring 2017 6

SQLite begin transaction first write commit requested no more read locks None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE LOCK commit commit executed CSE 414 - Spring 2017 7

Locks in the Abstract CSE 414 - Spring 2017 8

Notation L i (A) = transaction T i acquires lock for element A U i (A) = transaction T i releases lock for element A CSE 414 - Spring 2017 9

A Non-Serializable Schedule 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) CSE 414 - Spring 2017 10

Example T1 L 1 (A); READ(A) A := A+100 WRITE(A); U 1 (A); L 1 (B) READ(B) B := B+100 WRITE(B); U 1 (B); T2 L 2 (A); READ(A) A := A*2 WRITE(A); U 2 (A); L 2 (B); BLOCKED Scheduler has ensured a conflict-serializable schedule CSE 414 - Spring 2017 GRANTED; READ(B) B := B*2 WRITE(B); U 2 (B); 11

But T1 L 1 (A); READ(A) A := A+100 WRITE(A); U 1 (A); L 1 (B); READ(B) B := B+100 WRITE(B); U 1 (B); T2 L 2 (A); READ(A) A := A*2 WRITE(A); U 2 (A); L 2 (B); READ(B) B := B*2 WRITE(B); U 2 (B); Locks did not enforce conflict-serializability!!! What s wrong?

Two Phase Locking (2PL) The 2PL rule: In every transaction, all lock requests must precede all unlock requests 2PL approach developed by Jim Gray CSE 414 - Spring 2017 13

Example: 2PL transactions T1 L 1 (A); L 1 (B); READ(A) A := A+100 WRITE(A); U 1 (A) READ(B) B := B+100 WRITE(B); U 1 (B); Now it is conflict-serializable T2 CSE 414 - Spring 2017 L 2 (A); READ(A) A := A*2 WRITE(A); L 2 (B); BLOCKED GRANTED; READ(B) B := B*2 WRITE(B); U 2 (A); U 2 (B); 14

Two Phase Locking (2PL) Theorem: 2PL ensures conflict serializability 15

Two Phase Locking (2PL) Theorem: 2PL ensures conflict serializability Proof. Suppose not: then there exists a cycle in the precedence graph. T1 A C T2 T3 B

Two Phase Locking (2PL) Theorem: 2PL ensures conflict serializability Proof. Suppose not: then there exists a cycle in the precedence graph. T1 A C T2 T3 B Then there is the following temporal cycle in the schedule: 17

Two Phase Locking (2PL) Theorem: 2PL ensures conflict serializability Proof. Suppose not: then there exists a cycle in the precedence graph. T1 A C T2 T3 B Then there is the following temporal cycle in the schedule: U 1 (A)àL 2 (A) why? 18

Two Phase Locking (2PL) Theorem: 2PL ensures conflict serializability Proof. Suppose not: then there exists a cycle in the precedence graph. T1 A C T2 T3 B Then there is the following temporal cycle in the schedule: U 1 (A)àL 2 (A) L 2 (A)àU 2 (B) why? 19

Two Phase Locking (2PL) Theorem: 2PL ensures conflict serializability Proof. Suppose not: then there exists a cycle in the precedence graph. T1 A C T2 T3 B Then there is the following temporal cycle in the schedule: U 1 (A)àL 2 (A) L 2 (A)àU 2 (B) U 2 (B)àL 3 (B) L 3 (B)àU 3 (C) U 3 (C)àL 1 (C) L 1 (C)àU 1 (A) Contradiction 20

A New Problem: Non-recoverable Schedule T1 L 1 (A); L 1 (B); READ(A) A :=A+100 WRITE(A); U 1 (A) READ(B) B :=B+100 WRITE(B); U 1 (B); Rollback T2 L 2 (A); READ(A) A := A*2 WRITE(A); L 2 (B); BLOCKED GRANTED; READ(B) B := B*2 WRITE(B); U 2 (A); U 2 (B); Commit CSE 414 - Spring 2017 21

Strict 2PL The Strict 2PL rule: All locks are held until the transaction commits or aborts. With strict 2PL, we will get schedules that are both conflict-serializable and recoverable CSE 414 - Spring 2017 22

T1 L 1 (A); READ(A) A :=A+100 WRITE(A); L 1 (B); READ(B) B :=B+100 WRITE(B); ROLLBACK; U 1 (A),U 1 (B) Strict 2PL T2 L 2 (A); BLOCKED GRANTED; READ(A) A := A*2 WRITE(A); L 2 (B); READ(B) B := B*2 WRITE(B); CSE 414 - Spring 2017 COMMIT; U 2 (A); U 2 (B) 23

Another problem: Deadlocks T 1 waits for a lock held by T 2 ; T 2 waits for a lock held by T 3 ; T 3 waits for....... T n waits for a lock held by T 1 SQL Lite: there is only one exclusive lock; thus, never deadlocks SQL Server: checks periodically for deadlocks and aborts one TXN CSE 414 - Spring 2017 24

Lock Modes S = shared lock (for READ) X = exclusive lock (for WRITE) Lock compatibility matrix: None S X None S X CSE 414 - Spring 2017 25

Lock Modes S = shared lock (for READ) X = exclusive lock (for WRITE) Lock compatibility matrix: None S X None S X CSE 414 - Spring 2017 26

Lock Granularity Fine granularity locking (e.g., tuples) High concurrency High overhead in managing locks E.g. SQL Server Coarse grain locking (e.g., tables, entire database) Many false conflicts Less overhead in managing locks E.g. SQL Lite Solution: lock escalation changes granularity as needed CSE 414 - Spring 2017 27

Lock Performance Throughput (TPS) To avoid, use admission control thrashing Why? TPS = Transactions per second # Active Transactions CSE 414 - Spring 2017 28

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 414 - Spring 2017 29

Suppose there are two blue products, A1, A2: Phantom Problem T1 SELECT * FROM Product WHERE color= blue SELECT * FROM Product WHERE color= blue T2 INSERT INTO Product(name, color) VALUES ( A3, blue ) Is this schedule serializable? CSE 414 - Spring 2017 30

Suppose there are two blue products, A1, A2: Phantom Problem T1 SELECT * FROM Product WHERE color= blue SELECT * FROM Product WHERE color= blue T2 INSERT INTO Product(name, color) VALUES ( A3, blue ) R1(A1),R1(A2),W2(A3),R1(A1),R1(A2),R1(A3) CSE 414 - Spring 2017 31

Suppose there are two blue products, A1, A2: Phantom Problem T1 SELECT * FROM Product WHERE color= blue SELECT * FROM Product WHERE color= blue T2 INSERT INTO Product(name, color) VALUES ( A3, blue ) R1(A1),R1(A2),W2(A3),R1(A1),R1(A2),R1(A3) W2(A3),R1(A1),R1(A2),R1(A1),R1(A2),R1(A3) CSE 414 - Spring 2017 32

Phantom Problem A phantom is a tuple that is invisible during part of a transaction execution but not invisible during the entire execution In our example: T1: reads list of products T2: inserts a new product T1: re-reads: a new product appears! CSE 414 - Spring 2017 33

Dealing With Phantoms Lock the entire table Lock the index entry for blue If index is available Or use predicate locks A lock on an arbitrary predicate Dealing with phantoms is expensive! CSE 414 - Spring 2017 34

Locking & SQL CSE 414 - Spring 2017 35

Isolation Levels in SQL 1. Dirty reads SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 2. Committed reads SET TRANSACTION ISOLATION LEVEL READ COMMITTED 3. Repeatable reads SET TRANSACTION ISOLATION LEVEL REPEATABLE READ 4. Serializable transactions SET TRANSACTION ISOLATION LEVEL SERIALIZABLE ACID CSE 414 - Spring 2017 36

1. Isolation Level: Dirty Reads Long duration WRITE locks Strict 2PL No READ locks Read-only transactions are never delayed Possible problems: dirty and inconsistent reads CSE 414 - Spring 2017 37

2. Isolation Level: Read Committed Long duration WRITE locks Strict 2PL Short duration READ locks Only acquire lock while reading (not 2PL) Unrepeatable reads When reading same element twice, may get two different values CSE 414 - Spring 2017 38

3. Isolation Level: Repeatable Read Long duration WRITE locks Strict 2PL Long duration READ locks Strict 2PL Why? This is not serializable yet!!! CSE 414 - Spring 2017 39

4. Isolation Level Serializable Long duration WRITE locks Strict 2PL Long duration READ locks Strict 2PL Predicate locking To deal with phantoms CSE 414 - Spring 2017 40

Beware! In commercial DBMSs: Default level is often NOT serializable (SQL Server!) Default level differs between DBMSs Some engines support subset of levels Serializable may not be exactly ACID Locking ensures isolation, not atomicity Also, some DBMSs do NOT use locking and different isolation levels can lead to different probs Bottom line: Read the doc for your DBMS! CSE 414 - Spring 2017 41

Next two slides: try them on Azure CSE 414 - Spring 2017 42

Demonstration with SQL Server Application 1: create table R(a int); insert into R values(1); set transaction isolation level serializable; begin transaction; select * from R; -- get a shared lock waitfor delay '00:01'; -- wait for one minute Application 2: set transaction isolation level serializable; begin transaction; select * from R; -- get a shared lock insert into R values(2); -- blocked waiting on exclusive lock -- App 2 unblocks and executes insert after app 1 commits/aborts CSE 414 - Spring 2017 43

Demonstration with SQL Server Application 1: create table R(a int); insert into R values(1); set transaction isolation level repeatable read; begin transaction; select * from R; -- get a shared lock waitfor delay '00:01'; -- wait for one minute Application 2: set transaction isolation level repeatable read; begin transaction; select * from R; -- get a shared lock insert into R values(3); -- gets an exclusive lock on new tuple -- If app 1 reads now, it blocks because read dirty -- If app 1 reads after app 2 commits, app 1 sees new value CSE 414 - Spring 2017 44