CSE 190D Database System Implementation

Similar documents
Intro to Transaction Management

Overview. Introduction to Transaction Management ACID. Transactions

TRANSACTION MANAGEMENT

Overview of Transaction Management

Intro to Transactions

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

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

Database Systems. Announcement

Transaction Management: Introduction (Chap. 16)

Transaction Management & Concurrency Control. CS 377: Database Systems

COURSE 1. Database Management Systems

Introduction to Data Management CSE 344

CSE 444: Database Internals. Lectures Transactions

Transaction Management Overview

Introduction to Data Management CSE 414

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

Database Systems CSE 414

Transactions. Kathleen Durant PhD Northeastern University CS3200 Lesson 9

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

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

Concurrency Control & Recovery

Introduction to Data Management CSE 344

CSE 344 MARCH 9 TH TRANSACTIONS

CSE 344 MARCH 25 TH ISOLATION

Concurrency Control & Recovery

Database Systems CSE 414

Lectures 8 & 9. Lectures 7 & 8: Transactions

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

Database Tuning and Physical Design: Execution of Transactions

CSC 261/461 Database Systems Lecture 24

Introduction to Data Management. Lecture #18 (Transactions)

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

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

Transaction Management and Concurrency Control. Chapter 16, 17

Databases - Transactions

CSE 444: Database Internals. Lectures 13 Transaction Schedules

Problems Caused by Failures

Database Applications (15-415)

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

Lecture 21. Lecture 21: Concurrency & Locking

CSE 530A ACID. Washington University Fall 2013

Introduction to Data Management. Lecture #24 (Transactions)

Chapter 22. Transaction Management

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

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

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

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

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

CSE 344 MARCH 5 TH TRANSACTIONS

Multi-User-Synchronization

Transaction Management. Pearson Education Limited 1995, 2005

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

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

Transaction Management: Concurrency Control

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

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

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

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

6.830 Lecture Transactions October 23, 2017

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

Chapter 20 Introduction to Transaction Processing Concepts and Theory

T ransaction Management 4/23/2018 1

Lock-based Concurrency Control

Database Applications (15-415)

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

Database transactions

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

Database Management Systems Concurrency Control

CSE 344 MARCH 21 ST TRANSACTIONS

Transactions Processing (i)

UNIT-IV TRANSACTION PROCESSING CONCEPTS

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

Weak Levels of Consistency

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI

Transactions. Concurrency. Consistency ACID. Modeling transactions. Transactions and Concurrency Control

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

Introduction TRANSACTIONS & CONCURRENCY CONTROL. Transactions. Concurrency

Transactions & Update Correctness. April 11, 2018

TRANSACTION PROCESSING CONCEPTS

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

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

Database Management System

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

Transaction Processing. Introduction to Databases CompSci 316 Fall 2018

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

Transaction Management

Introduction to Data Management CSE 344


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

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

RECOVERY CHAPTER 21,23 (6/E) CHAPTER 17,19 (5/E)

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

Information Systems (Informationssysteme)

Chapter 7 (Cont.) Transaction Management and Concurrency Control

Topics in Reliable Distributed Systems

TRANSACTION PROPERTIES

) Intel)(TX)memory):) Transac'onal) Synchroniza'on) Extensions)(TSX))) Transac'ons)

Advances in Data Management Transaction Management A.Poulovassilis

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

Transcription:

CSE 190D Database System Implementation Arun Kumar Topic 6: Transaction Management Chapter 16 of Cow Book Slide ACKs: Jignesh Patel 1

Transaction Management Motivation and Basics The ACID Properties Transaction Schedules Conflicts and Other Issues Concurrency Control SQL/RDBMS Features Basics of Recovery 2

Motivation An RDBMS does not exist in a vacuum! It manages structured data at scale for an application Banking, insurance, finance, retail, telecom, etc. Workload could involve a mix of reads and writes Application determines schema of databases in RDBMS, integrity constraints, instance content, physical design, etc. Application-independence is a key benefit of RDBMSs! Q: But at what level does an application deal with an RDBMS? 3

Motivation An SQL query is often too fine-grained for an application In most real-world applications, a logical unit of work could be coarser / high-level: May need multiple DDL + DML queries together May involve reading and/or writing (update/delete/insert) May need to check for integrity constraints A transaction is a sequence of operations on the database that captures one logical unit of work for an application (NB: Not really specific to the relational data model!) 4

Motivating Example A logical unit of work in a bank s DBMS: Transfer $5000 from Checking to Savings account A sequence of fine-grained operations on the database: C is Checking balance S is Savings balance One logical unit of work 5

Transaction Management Overview Transaction: DBMS abstraction to capture a sequence of database operations that form one logical unit of work Transaction Manager: Software module in a DBMS that manages transactions Benefits of having the transaction abstraction: Enables us to reason about concurrent queries (e.g., DMA, multi-core parallelism) Enables us to reason about recovery from crashes 6

Transaction Management Overview Q: What operations does a transaction (txn) contain? Txn Manager understands only Read (R) and Write (W) Granularity can vary: item, tuple, relation, etc. All SQL queries mapped to a sequence of R/W! Three additional special operations:,, Abort 7

Motivating Example Application s unit of work The abstract transaction R (C) W (C) R (S) W (S) C 8000 3000 S 7000 2000 RAM C 8000 3000 S 2000 7000 Disk 8

Seems to work fine. What is the point of a transaction abstraction then? 9

What if this happens? Application s unit of work The abstract transaction R (C) W (C) R (S) W (S) C 8000 3000 S 7000 2000 RAM C 8000 3000 S 2000 Disk OS crashes! Your $5000 has vanished! 10

Transaction s Application s unit of work The abstract transaction R (C) W (C) R (S) W (S) C 8000 3000 S 7000 2000 RAM C 8000 3000 S 2000 7000 Disk Tells the DBMS that the transaction has finished successfully and all of its changes need to be persisted (on disk)! 11

Transaction Aborts Application s unit of work The abstract transaction R (C) W (C) Abort C -1000 4000 RAM C 4000 S 2000 Disk Tells the DBMS that the transaction failed (for some reason) and it should be terminated without persisting any of its changes! 12

Moral of the Story A transaction must satisfy all or nothing property Application s logical unit of work is indivisible Either all operations get done or none of them get done The database state must remain consistent Application s invariants define what is consistent A transaction is assumed to get the database from one consistent state to another (inconsistency ok in between) C=8000 S=2000 C=3000, S=2000 C=8000, S=7000 C=3000 S=7000 13

Transaction Management Motivation and Basics The ACID Properties Transaction Schedules Conflicts and Other Issues Concurrency Control SQL/RDBMS Features Basics of Recovery 14

The ACID Properties Transaction management should ensure 4 key properties Atomicity Techniques: Logging and Recovery A transaction should be indivisible/ all or nothing Consistency App semantics; Techniques: Schedules Database should not become inconsistent in the end Isolation Techniques: Concurrency Control A transaction should not worry about or interact with other concurrent transactions on the DBMS Durability Techniques: Logging and Recovery All changes of a ted transaction must persist 15

Transaction Management Motivation and Basics The ACID Properties Transaction Schedules Conflicts and Other Issues Concurrency Control SQL/RDBMS Features Basics of Recovery 16

Transaction Schedules Tells us how to interleave concurrent transactions Benefits of interleaving: Lets us exploits disk-cpu and multi-core parallelism Helps avoid starvation of shorter transactions Temporal order of ops in a transaction must be preserved! T1 T2 T1 R T1 (A) T2 W T1 (A) R T1 (A) R T2 (A) R T2 (A) W T1 (A) W T2 (A) W T2 (A) R T1 (B) R T2 (B) R T1 (B) W T1 (B) W T2 (B) W T1 (B) Time A schedule R T2 (B) W T2 (B) 17

Transaction Schedules Complete Schedule Each transaction ends with either a or an Abort Serial Schedule No interleaving of ops from different transactions Any serial schedule is considered acceptable, even if they end up with different database states! T1 R T1 (A) WR T1 (A) WR T1 (B) (A) W T1 (B) R T1 (B) WR T1 (A) (B) WAbort T1 (A) R T1 (B) W T1 (B) T2 R T2 (A) W T2 (A) R T2 (B) WR T2 (A) T2 (B) W T2 (A) R T2 (A) W T2 (A) R T2 (B) W T2 (B) 18

Transaction Schedules Q: What is wrong with this schedule? The update of A by T2 is lost! Bad schedules like this could lead to an inconsistent state! Q: What is a acceptable schedule? T1 R T1 (A) W T1 (A) R T1 (B) W T1 (B) T2 R T2 (A) W T2 (A) R T2 (B) W T2 (B) 19

Transaction Schedules: Serializability Equivalence of Schedules: Two schedules are equivalent iff they lead the database to the same end state irrespective of its start state Serializable Schedule: A schedule that is equivalent to some complete serial schedule (for now, assume only ted transactions; Aborts will be discussed later) Only a serializable schedule is an acceptable schedule! 20

Transaction Schedules Q: Is this schedule serializable? T1 R T1 (A) W T1 (A) R T1 (B) W T1 (B) T2 R T2 (A) W T2 (A) R T2 (B) W T2 (B) 21

Transaction Schedules Q: Is this schedule serializable? T1 R T1 (A) W T1 (A) R T1 (B) W T1 (B) T2 R T2 (A) W T2 (A) R T2 (B) W T2 (B) 22

Transaction Schedules Q: What about this one from before? The update of A by T2 is lost! This is called a conflict T1 R T1 (A) W T1 (A) R T1 (B) W T1 (B) T2 R T2 (A) W T2 (A) R T2 (B) W T2 (B) 23

Transaction Management Motivation and Basics The ACID Properties Transaction Schedules Conflicts and Other Issues Concurrency Control SQL/RDBMS Features Basics of Recovery 24

Kinds of Transaction Conflicts WW Conflict (Overwriting Uncommitted Data) WR Conflict (Reading Uncommitted Data aka Dirty Read) RW Conflict (Unrepeatable Reads) 25

WW Conflict (Overwriting Uncommitted Data) T1 overwrites T2 s update without reading new A This schedule is not serializable! If a transaction writes an item without reading it, it is called a Blind Write T1 R T1 (A) W T1 (A) R T1 (B) W T1 (B) T2 R T2 (A) W T2 (A) R T2 (B) W T2 (B) 26

WR Conflict (Reading Uncommitted Data / Dirty Read) T1 s writes of A and B may be related; T2 may read inconsistent database! This schedule is not serializable! T1 R T1 (A) W T1 (A) R T1 (B) W T1 (B) T2 R T2 (A) W T2 (A) R T2 (B) W T2 (B) 27

RW(R) Conflict (Unrepeatable Reads) The two reads of A by T1 may yield different values This schedule is not serializable! T1 R T1 (A) W T1 (C) R T1 (A) W T1 (D) T2 R T2 (A) W T2 (A) R T2 (B) W T2 (B) 28

Surprise Review Qn! Q: Is this a serializable schedule? Q: Does it have any conflicts? T1 R T2 (A) W T2 (A) R T2 (B) W T2 (B) T2 R T1 (A) W T1 (A) R T1 (B) W T1 (B) 29

Okay, what about Aborted transactions? 30

Transactions with Aborts Serializability: only worry about ed transactions and pretend as if Aborted transactions did not even happen! To make the above illusion possible, 2 new issues to deal with if Aborted transactions present: How to undo the effects of an Aborted transaction? All changes made by it should be undone Use Logging and Recovery (Later) What if some other transactions got affected by it? Must undo all affected transactions as well! 31

Cascading Aborts Abort of T1 leads to Abort of T2! T2 s will be replaced with an Abort by the Txn Manager! T1 R T1 (A) W T1 (A) Abort T2 R T2 (A) W T2 (A) Abort 32

Cascading Aborts Q: What about this case? T2 has already ted! Not allowed to Abort it now (why?) Known as an Unrecoverable schedule Recoverable schedule: All transactions (if at all) only after all others that supply dirty data /Abort T1 R T1 (A) W T1 (A) Abort T2 R T2 (A) W T2 (A) 33

Cascading Aborts Q: Is it possible to avoid cascading aborts? Yes! Avoid-Cascading-Aborts (ACA) schedule: No txn is allowed to read dirty data, i.e., all txns read changes of ted txns only Guarantees that cascading aborts will not arise Also guaranteed to be a Recoverable schedule A similar issue arises if a transaction overwrites dirty data! Handled using Concurrency Control 34

Transaction Management Motivation and Basics The ACID Properties Transaction Schedules Conflicts and Other Issues Concurrency Control SQL/RDBMS Features Introduction to Recovery 35

Introduction to Locking To ensure serializability and recoverability, DBMSs use concurrency control (CC); most common way: locking Lock: small bookkeeping object associated with a data item (tuple, attribute, table, etc.); managed by Lock Manager Simplified view: Lock is a 3-tuple: (TxnID, RecID, Mode) -- S X Shared (S) or Exclusive (X) -- Q: When are locks acquired? S During the request to BufMgr! X Q: What if a txn cannot get a lock? Compatibility Matrix Suspended and put on a wait queue 36

Locking Protocols Determines when locks should be acquired/released by txns Strict 2-Phase Locking (Strict 2PL): 1. Request S (resp. X) lock before R (resp. W) on item 2. Release all locks only when txn finishes (Non-strict) 2PL: Relax 2: if any lock released, cannot acquire new locks! Both protocols guarantee serializability! Strict 2PL also guarantees ACA schedules and thus, recoverability! growing phase # locks Time lock point Note: Lock acquire/release done automatically by DBMS Shrinking phase 37

Locking Example Q: Is this schedule serializable? Q: Is this schedule recoverable? X(A) Suppose we use Strict 2PL DBMS makes T1 acquire X lock on A T2 forced to wait before R(A)! Q: When will T1 release X lock on A? -- S Q: Is this new schedule serializable? -- UX(A) Q: What if we Suse (non-strict) 2PL? T2 X resumed T1 R T1 (A) W T1 (A) R T1 (B) W T1 (B) T2 R T2 (A) W T2 (A) RWait! T2 (B) W T2 (B) R T2 (A) 38

Deadlocks Deadlocks arise when two txns wait on each other System made to wait and do nothing forever! X T1 (B), X T2 (A), S T1 (A), S T2 (B), Unlocks Q: Does this satisfy 2PL? Strict 2PL? Deadlock prevention Avoid schedules that could cause deadlocks Deadlock detection and breaking A naive solution : timeout and abort txns More sophisticated solutions exist (not in syllabus) T1 A B T2 39

Granularity of Locking What is the precise data item a transaction needs to lock? Tuple? Attribute? Whole table? Index? Affects both efficiency (throughput) and correctness UPDATE Students SET Grade = A WHERE StudentID=123; Locking whole table might be an overkill! Thrashing: too many txns asking X lock on same data item; causes throughput (number txns done/sec) to plummet Some ways to avoid thrashing: lock smallest granularity needed for txn, and reduce the time a txn holds a lock 40

Granularity of Locking What is the precise data item a transaction needs to lock? Tuple? Attribute? Whole table? Index? Affects both efficiency (throughput) and correctness DBMSs allow for predicate locks too. Bizarre correctness issues can arise on interleaving granularity-mode combos! T1: S lock on table, X lock on predicate; read table, update only tuples matching predicate T2 comes in to insert new tuple, but it satisfies predicate T1 reads again: sees hitherto unseen tuple! Phantom Problem 41

Transaction Management Motivation and Basics The ACID Properties Transaction Schedules Conflicts and Other Issues Concurrency Control SQL/RDBMS Features Introduction to Recovery 42

SQL/RDBMS Features Keywords: is implicit; COMMIT; ROLLBACK (Abort); to save intermediate work of long-running txns: SAVEPOINT Access modes: READ ONLY vs. READ WRITE Isolation Levels: determines precise locking protocol READ UNCOMMITTED READ COMMITTED (Long X locks; short S locks) WR conflicts do not arise; RWR might REPEATABLE READ (Long X and S locks on real objects) Neither WR nor RWR arise; but phantom problem might! SERIALIZABLE (Long X locks only; no S locks!) Vulnerable to inconsistency; WR and RWR might arise! (Long X and S locks on phantoms too) No conflicts, no phantom. Perfect! Default in most RDBMSs! 43

Transaction Management Motivation and Basics The ACID Properties Transaction Schedules Conflicts and Other Issues Concurrency Control SQL/RDBMS Features Introduction to Recovery 44

Introduction to Recovery Recovery helps persist ted txns changes and undo effects of Aborted txns (ensures Durability and Atomicity) Log: A file in which any changes to DBMS are recorded Precise entry depends on kind of change Write-Ahead Logging (WAL) Protocol: Ensure change is written to Log first before actual data! Upon, force all Log records to disk first During Recovery, Log tells what to undo and/or redo Two mechanisms crucial to enable WAL: Stealing Frames Forcing Pages 45

Introduction to Recovery Stealing Frames: Allow stealing buffer frames from uncommitted txns Helps improve throughout, but challenge for Atomicity Forcing Pages Every page write is sent to disk immediately Hurts latency, but nice for Durability Ideal: Steal + No Force To steal frame, write summary to Log; helps undo To avoid forcing, write summary to Log; helps redo 46

Transaction Management Motivation and Basics The ACID Properties Transaction Schedules Conflicts and Other Issues Concurrency Control SQL/RDBMS Features Introduction to Recovery 47