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

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

CSEP544 Lecture 4: Transactions

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

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

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

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

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

CSE 444: Database Internals. Lectures Transactions

Transactions. 1. Transactions. Goals for this lecture. Today s Lecture

Lectures 8 & 9. Lectures 7 & 8: Transactions

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

CSE 444: Database Internals. Lectures 13 Transaction Schedules

Introduction to Data Management CSE 344

Database Systems CSE 414

Lecture 12: Transactions. Monday, March 2, 2015

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

Introduction to Data Management CSE 344

Principles of Data Management. Lecture #2 (Storing Data: Disks and Files)

DATABASE TRANSACTIONS. CS121: Relational Databases Fall 2017 Lecture 25

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

Lecture 16: Transactions (Recovery) Wednesday, May 16, 2012

CSE 344 MARCH 21 ST TRANSACTIONS

Transaction Management & Concurrency Control. CS 377: Database Systems

Database Systems CSE 414

Database Systems. November 2, 2011 Lecture #7. topobo (mit)

Storing Data: Disks and Files

Transactions. ACID Properties of Transactions. Atomicity - all or nothing property - Fully performed or not at all

CHAPTER 3 RECOVERY & CONCURRENCY ADVANCED DATABASE SYSTEMS. Assist. Prof. Dr. Volkan TUNALI

COURSE 1. Database Management Systems

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

CS122 Lecture 15 Winter Term,

Database Systems CSE 414

Storing Data: Disks and Files. Storing and Retrieving Data. Why Not Store Everything in Main Memory? Database Management Systems need to:

Database Management Systems

Storing and Retrieving Data. Storing Data: Disks and Files. Solution 1: Techniques for making disks faster. Disks. Why Not Store Everything in Tapes?

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

Storing and Retrieving Data. Storing Data: Disks and Files. Solution 1: Techniques for making disks faster. Disks. Why Not Store Everything in Tapes?

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

Introduction to Databases, Fall 2005 IT University of Copenhagen. Lecture 10: Transaction processing. November 14, Lecturer: Rasmus Pagh

Example: Transfer Euro 50 from A to B

SQL: Transactions. Introduction to Databases CompSci 316 Fall 2017

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

TRANSACTION MANAGEMENT

CSE 190D Database System Implementation

CSE 344 MARCH 5 TH TRANSACTIONS

Database Systems CSE 414

Storing Data: Disks and Files. Storing and Retrieving Data. Why Not Store Everything in Main Memory? Chapter 7

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

CSE 344 MARCH 25 TH ISOLATION

Database Management Systems Introduction to DBMS

Outline. Failure Types

TRANSACTION PROPERTIES

Lecture 21. Lecture 21: Concurrency & Locking

Transactions Processing (i)

Database Systems. Announcement

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

Database Technology. Topic 8: Introduction to Transaction Processing

Chapter 22. Transaction Management

XI. Transactions CS Computer App in Business: Databases. Lecture Topics

Lecture 12. Lecture 12: The IO Model & External Sorting

Outlines. Chapter 2 Storage Structure. Structure of a DBMS (with some simplification) Structure of a DBMS (with some simplification)

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

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

Overview of Transaction Management

Concurrency Control & Recovery

Distributed Systems

Transaction Management

CSE 530A ACID. Washington University Fall 2013

In This Lecture. Transactions and Recovery. Transactions. Transactions. Isolation and Durability. Atomicity and Consistency. Transactions Recovery

Transaction Management: Introduction (Chap. 16)

ECE 650 Systems Programming & Engineering. Spring 2018

Lecture 18: Reliable Storage

Topics. File Buffer Cache for Performance. What to Cache? COS 318: Operating Systems. File Performance and Reliability

CSC 261/461 Database Systems Lecture 24

L9: Storage Manager Physical Data Organization

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI

Problems Caused by Failures

Transactions and Locking. Rose-Hulman Institute of Technology Curt Clifton

Introduction to Data Management CSE 414

CSCI-GA Database Systems Lecture 8: Physical Schema: Storage

Databases - Transactions

Storing Data: Disks and Files

Transactions and Isolation

TRANSACTION PROCESSING PROPERTIES OF A TRANSACTION TRANSACTION PROCESSING PROPERTIES OF A TRANSACTION 4/3/2014

Outline. Purpose of this paper. Purpose of this paper. Transaction Review. Outline. Aries: A Transaction Recovery Method

Disks and Files. Jim Gray s Storage Latency Analogy: How Far Away is the Data? Components of a Disk. Disks

Database Management System Prof. D. Janakiram Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

COSC344 Database Theory and Applications. Lecture 21 Transactions

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

Chapter 20 Introduction to Transaction Processing Concepts and Theory

Transaction Management. Pearson Education Limited 1995, 2005

Database Usage (and Construction)

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

Storing Data: Disks and Files

STORING DATA: DISK AND FILES

Introduction to Database Systems CSE 444

1.264 Lecture 8. SQL continued Connecting to database servers

Transactions & Update Correctness. April 11, 2018

CompSci 516: Database Systems

Transcription:

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

Outline Transactions in SQL 2

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 3

Why Do We Need Transactions Concurrency control Recovery In the following examples, think of a transaction as meaning a procedure. A transaction commits when it ends successfully. A transaction rolls back when it aborts. 4

Concurrency control: Three Famous anomalies Dirty read T reads data written by T while T has not committed What can go wrong: T write more data (which T has already read), or T aborts Lost update Two tasks T and T both modify the same data T and T both commit Final state shows effects of only T, but not of T Inconsistent read One task T sees some but not all changes made by T 5

Dirty Reads Client Client 1: 1: /* /* transfer transfer $100 $100 from from account account 1 to to account account 2 */ */ If If Account1.balance > 100 100 then thenaccount1.balance = Account1.balance --100 100 Account2.balance = Account2.balance + 100 100 COMMIT else else ROLLBACK Client Client 2: 2: /* /* Compute total total amount amount */ */ X = Account1.balance; Y = Account2.balance; Z = X + Y; Y; Print(Z); COMMIT What goes wrong? 6

Dirty Reads Client Client 1: 1: /* /* transfer transfer $100 $100 from from account account 1 to to account account 2 */ */ /* /* tentatively move move money money into into account account 2 */ */ Account2.balance = Account2.balance + 100 100 If If Account1.balance > 100 100 then thenaccount1.balance = Account1.balance --100 100 COMMIT else else /* /* oops: oops: remove remove $100 $100 from from Account 2 */ */ Account2.balance = Account2.balance --100 100 ROLLBACK Client Client 2: 2: /* /* withdraw $100 $100 */ */ If If Account2.balance > 100 100 then then Account2.balance = Account2.balance --100; 100; DISPENSE MONEY COMMIT else else ROLLBACK Not needed (done by ROLLBACK) What goes wrong? 7

Lost Updates Client 1: 1: UPDATE Product SET Price = Price 1.99 WHERE pname = Gizmo Client 2: 2: UPDATE Product SET Price = Price*0.5 WHERE pname= Gizmo Two different users attempt to apply a discount. Will it work? 8

Inconsistent Read Client 1: 1: UPDATE Products SET quantity = quantity + 5 WHERE product = gizmo UPDATE Products SET quantity = quantity --5 WHERE product = gadget Client 2: 2: SELECT sum(quantity) FROM Product Note: this is a form of dirty read 9

Protection against crashes Client 1: 1: UPDATE Products SET quantity = quantity + 5 WHERE product = gizmo UPDATE Products SET quantity = quantity --5 WHERE product = gadget Crash! What s wrong? 10

Definition A transaction = one or more operations, which reflects a single real-world transition In the real world, this happened completely or not at all Examples Transfer money between accounts Purchase a group of products Register for a class (either waitlist or allocated) If grouped in transactions, all problems in previous slides disappear 11

Transactions in SQL In ad-hoc SQL: Default: each statement = one transaction In a program: START TRANSACTION [SQL statements] COMMIT or ROLLBACK (=ABORT) May be omitted: first SQL query starts txn 12

Revised Code Client 1: 1: START TRANSACTION UPDATE Product SET SETPrice = Price Price 1.99 1.99 WHERE pname = Gizmo COMMIT Client 2: 2: START TRANSACTION UPDATE Product SET SETPrice = Price*0.5 WHERE pname= Gizmo COMMIT Now it works like a charm 13

Transaction Properties ACID Atomic State shows either all the effects of txn, or none of them Consistent Txn moves from a state where integrity holds, to another where integrity holds Isolated Effect of txns is the same as txns running one after another (ie looks like batch mode) Durable Once a txn has committed, its effects remain in the database 14

ACID: Atomicity Two possible outcomes for a transaction It commits: all the changes are made It aborts: no changes are made That is, transaction s activities are all or nothing 15

ACID: Consistency The state of the tables is restricted by integrity constraints Account number is unique Stock amount can t be negative Sum of debits and of credits is 0 Constraints may be explicit or implicit How consistency is achieved: Programmer makes sure a txn takes a consistent state to a consistent state The system makes sure that the tnx is atomic 16

ACID: Isolation A transaction executes concurrently with other transaction Isolation: the effect is as if each transaction executes in isolation of the others 17

ACID: Durability The effect of a transaction must continue to exists after the transaction, or the whole program has terminated Means: write data to disk 18

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 The database returns to the state without any of the previous changes made by activity of the transaction 19

Reasons for Rollback User changes their mind ( ctl-c /cancel) Explicit in program, when app program finds a problem e.g. when qty on hand < qty being sold System-initiated abort System crash Housekeeping e.g. due to timeouts 20

READ-ONLY Transactions Client Client 1: 1: START START TRANSACTION INSERT INTO INTOSmallProduct(name, price) price) SELECT pname, pname, price price FROM FROMProduct WHERE price price <= <= 0.99 0.99 DELETE Product Product WHERE price price <=0.99 <=0.99 COMMIT Client Client 2: 2: SET SET TRANSACTION READ READ ONLY ONLY START START TRANSACTION SELECT count(*) FROM FROMProduct Makes it faster SELECT count(*) FROM FROMSmallProduct COMMIT 21

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 (default): SET TRANSACTION ISOLATION LEVEL SERIALIZABLE 22

Isolation Level: Dirty Reads Plane seat allocation function AllocateSeat( %request) SET SET ISOLATION LEVEL LEVEL READ READ UNCOMMITED What can go wrong? What can go wrong if only the function AllocateSeat modifies Seat? START START TRANSACTION Let Let x = SELECT Seat.occupied FROM FROMSeat WHERE Seat.number = %request If If (x (x == == 1) 1) /* /* occupied */ */ ROLLBACK UPDATE Seat Seat SET SEToccupied = 1 WHERE Seat.number = %request COMMIT 23

function TransferMoney( %amount, %acc1, %acc1,%acc2) %acc2) START START TRANSACTION Are dirty reads OK here? Let Let x = SELECT Account.balance FROM FROMAccount WHERE Account.number = %acc1 %acc1 If If (x (x < %amount) ROLLBACK What if we switch the two updates? UPDATE Account SET SETbalance = balance+%amount WHERE Account.number = %acc2 %acc2 UPDATE Account SET SETbalance = balance-%amount WHERE Account.number = %acc1 %acc1 COMMIT 24

Isolation Level: Read Committed Stronger than READ UNCOMMITTED It is possible to read twice, and get different values SET SET ISOLATION LEVEL LEVEL READ READ COMMITED Let Let x = SELECT Seat.occupied FROM FROMSeat WHERE Seat.number = %request /* /*...... More More stuff stuff here here.....*/ */ Let Let y = SELECT Seat.occupied FROM FROMSeat WHERE Seat.number = %request /* /* we we may may have have x y!!*/ */ 25

Isolation Level: Repeatable Read Stronger than READ COMMITTED May see incompatible values: another txn transfers from acc. 55555 to 77777 SET SET ISOLATION LEVEL LEVEL REPEATABLE READ READ Let Let x = SELECT Account.amount FROM FROMAccount WHERE Account.number = 555555 /* /*...... More More stuff stuff here here.....*/ */ Let Let y = SELECT Account.amount FROM FROMAccount WHERE Account.number = 777777 /* /* we we may may have have a a wrong wrong x+y x+y!!*/ */ 26

Isolation Level: Serializable Strongest level SET SET ISOLATION LEVEL LEVEL SERIALIZABLE........ Default WILL STUDY IN DETAILS IN A WEEK 27

The Mechanics of Disk Mechanical characteristics: Rotation speed (5400RPM) Number of platters (1-30) Number of tracks (<=10000) Number of bytes/track(10 5 ) Disk head Cylinder Spindle Tracks Sector Unit of read or write: disk block Once in memory: page Typically: 4k or 8k or 16k Arm assembly Arm movement 28 Platters

Disk Access Characteristics Disk latency = time between when command is issued and when data is in memory Disk latency = seek time + rotational latency Seek time = time for the head to reach cylinder 10ms 40ms Rotational latency = time for the sector to rotate Rotation time = 10ms Average latency = 10ms/2 Transfer time = typically 40MB/s Disks read/write one block at a time 29

RAID Several disks that work in parallel Redundancy: use parity to recover from disk failure Speed: read from several disks at once Various configurations (called levels): RAID 1 = mirror RAID 4 = n disks + 1 parity disk RAID 5 = n+1 disks, assign parity blocks round robin RAID 6 = Hamming codes 30

Buffer Management in a DBMS Page Requests from Higher Levels BUFFER POOL READ WRITE disk page free frame MAIN MEMORY DISK DB choice of frame dictated by replacement policy INPUT OUTUPT Data must be in RAM for DBMS to operate on it! Table of <frame#, pageid> pairs is maintained 31

Buffer Manager Needs to decide on page replacement policy LRU Clock algorithm Both work well in OS, but not always in DB Enables the higher levels of the DBMS to assume that the needed data is in main memory. 32

Least Recently Used (LRU) Order pages by the time of last accessed Always replace the least recently accessed P5, P2, P8, P4, P1, P9, P6, P3, P7 Access P6 P6, P5, P2, P8, P4, P1, P9, P3, P7 LRU is expensive (why?); the clock algorithm is good approx 33

Buffer Manager Why not use the Operating System for the task?? Main reason: need fine grained control for transactions Other reasons: - DBMS may be able to anticipate access patterns - Hence, may also be able to perform prefetching -DBMS needs the ability to force pages to disk, for recovery purposes 34

Transaction Management and the Buffer Manager The transaction manager operates on the buffer pool Recovery: log-file write-ahead, then careful policy about which pages to force to disk Concurrency control: locks at the page level, multiversion concurrency control Will discuss details during the next few lectures 35