introduction to data management cse 344€¦ · – sql server, db2, etc cse 344 - winter 2016 6....
TRANSCRIPT
![Page 1: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/1.jpg)
Introduction to Data ManagementCSE 344
Lecture 22: Transaction Implementations
CSE 344 - Winter 2016 1
![Page 2: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/2.jpg)
Announcements
• WQ6 (last!) due on Wednesday evening
• HW7 due on Monday– Make sure you can run starter code– Start early, there is little time!
CSE 344 - Winter 2016 2
![Page 3: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/3.jpg)
Recap• What are transactions
– And why do we need them
• How to maintain ACID properties via schedules– We focus on the isolation property– We learn about atomicity & durability in 444
• How to ensure conflict-serializable schedules with locks
CSE 344 - Winter 2016 3
![Page 4: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/4.jpg)
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 344
4CSE 344 - Winter 2016
![Page 5: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/5.jpg)
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)
CSE 344 - Winter 2016 5
By using locks scheduler ensures conflict-serializability
![Page 6: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/6.jpg)
What Data Elements are Locked?
Major differences between vendors:
• Lock on the entire database– SQLite
• Lock on individual records– SQL Server, DB2, etc
CSE 344 - Winter 2016 6
![Page 7: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/7.jpg)
SQLite
CSE 344 - Winter 2016 7
None READLOCK
RESERVEDLOCK
PENDINGLOCK
EXCLUSIVELOCK
commit executed
begin transaction first write no more read lockscommit requested
commit
![Page 8: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/8.jpg)
Now for something more serious…
8CSE 344 - Winter 2016
![Page 9: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/9.jpg)
Notation
Li(A) = transaction Ti acquires lock for element A
Ui(A) = transaction Ti releases lock for element A
9CSE 344 - Winter 2016
![Page 10: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/10.jpg)
A Non-Serializable ScheduleT1 T2READ(A)A := A+100WRITE(A)
READ(A)A := A*2WRITE(A)READ(B)B := B*2WRITE(B)
READ(B)B := B+100WRITE(B)
10CSE 344 - Winter 2016
![Page 11: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/11.jpg)
ExampleT1 T2L1(A); READ(A)A := A+100WRITE(A); U1(A); L1(B)
L2(A); READ(A)A := A*2WRITE(A); U2(A); L2(B); BLOCKED…
READ(B)B := B+100WRITE(B); U1(B);
…GRANTED; READ(B)B := B*2WRITE(B); U2(B);
11CSE 344 - Winter 2016Scheduler has ensured a conflict-serializable schedule
![Page 12: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/12.jpg)
But…T1 T2L1(A); READ(A)A := A+100WRITE(A); U1(A);
L2(A); READ(A)A := A*2WRITE(A); U2(A);L2(B); READ(B)B := B*2WRITE(B); U2(B);
L1(B); READ(B)B := B+100WRITE(B); U1(B);
12CSE 344 - Winter 2016Locks did not enforce conflict-serializability !!! What’s wrong ?
![Page 13: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/13.jpg)
Two Phase Locking (2PL)
CSE 344 - Winter 2016 13
In every transaction, all lock requests must precede all unlock requests
The 2PL rule:
![Page 14: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/14.jpg)
Example: 2PL transactionsT1 T2L1(A); L1(B); READ(A)A := A+100WRITE(A); U1(A)
L2(A); READ(A)A := A*2WRITE(A); L2(B); BLOCKED…
READ(B)B := B+100WRITE(B); U1(B);
…GRANTED; READ(B)B := B*2WRITE(B); U2(A); U2(B); Now it is conflict-serializable
14CSE 344 - Winter 2016
![Page 15: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/15.jpg)
A New Problem: Non-recoverable Schedule
T1 T2L1(A); L1(B); READ(A)A :=A+100WRITE(A); U1(A)
L2(A); READ(A)A := A*2WRITE(A); L2(B); BLOCKED…
READ(B)B :=B+100WRITE(B); U1(B);
…GRANTED; READ(B)B := B*2WRITE(B); U2(A); U2(B); Commit
Rollback15CSE 344 - Winter 2016
![Page 16: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/16.jpg)
Strict 2PL
CSE 344 - Winter 2016 16
All locks are held until the transactioncommits or aborts.
The Strict 2PL rule:
With strict 2PL, we will get schedules thatare both conflict-serializable and recoverable
![Page 17: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/17.jpg)
Strict 2PLT1 T2L1(A); READ(A)A :=A+100WRITE(A);
L2(A); BLOCKED…L1(B); READ(B)B :=B+100WRITE(B); U1(A),U1(B); Rollback
…GRANTED; READ(A)A := A*2WRITE(A); L2(B); READ(B)B := B*2WRITE(B); U2(A); U2(B); Commit
17CSE 344 - Winter 2016
![Page 18: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/18.jpg)
Another problem: Deadlocks• T1 waits for a lock held by T2;• T2 waits for a lock held by T3;• T3 waits for . . . .• . . .• Tn waits for a lock held by T1
18CSE 344 - Winter 2016
SQL Lite: there is only one exclusive lock; thus, never deadlocks
SQL Server: checks periodically for deadlocks and aborts one TXN
![Page 19: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/19.jpg)
Lock Modes
• S = shared lock (for READ)• X = exclusive lock (for WRITE)
19CSE 344 - Winter 2016
None S XNone
SX
Lock compatibility matrix:
![Page 20: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/20.jpg)
Lock Modes
• S = shared lock (for READ)• X = exclusive lock (for WRITE)
20CSE 344 - Winter 2016
None S XNone ✔ ✔ ✔
S ✔ ✔ ✖
X ✔ ✖ ✖
Lock compatibility matrix:
![Page 21: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/21.jpg)
21
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 344 - Winter 2016
![Page 22: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/22.jpg)
Lock Performance
CSE 344 - Winter 2016 22
Thro
ughp
ut (T
PS)
# Active Transactions
thrashing
Why ?
TPS =Transactionsper second
To avoid, use admission control
![Page 23: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/23.jpg)
23
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 344 - Winter 2016
![Page 24: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/24.jpg)
Phantom Problem
Is this schedule serializable ?
T1 T2SELECT *FROM ProductWHERE color=‘blue’
INSERT INTO Product(name, color)VALUES (‘A3’,’blue’)
SELECT *FROM ProductWHERE color=‘blue’
Suppose there are two blue products, A1, A2:
CSE 344 - Winter 2016 24
![Page 25: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/25.jpg)
Phantom Problem
25
R1(A1),R1(A2),W2(A3),R1(A1),R1(A2),R1(A3)
T1 T2SELECT *FROM ProductWHERE color=‘blue’
INSERT INTO Product(name, color)VALUES (‘A3’,’blue’)
SELECT *FROM ProductWHERE color=‘blue’
CSE 344 - Winter 2016
Suppose there are two blue products, A1, A2:
![Page 26: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/26.jpg)
Phantom Problem
26
R1(A1),R1(A2),W2(A3),R1(A1),R1(A2),R1(A3)
T1 T2SELECT *FROM ProductWHERE color=‘blue’
INSERT INTO Product(name, color)VALUES (‘A3’,’blue’)
SELECT *FROM ProductWHERE color=‘blue’
Suppose there are two blue products, A1, A2:
CSE 344 - Winter 2016W2(A3),R1(A1),R1(A2),R1(A1),R1(A2),R1(A3)
![Page 27: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/27.jpg)
27
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 344 - Winter 2016
![Page 28: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/28.jpg)
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 344 - Winter 2016 28
![Page 29: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/29.jpg)
29
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 transactionsSET TRANSACTION ISOLATION LEVEL SERIALIZABLE
ACID
CSE 344 - Winter 2016
![Page 30: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/30.jpg)
1. Isolation Level: Dirty Reads
• “Long duration” WRITE locks– Strict 2PL
• No READ locks– Read-only transactions are never delayed
30
Possible problems: dirty and inconsistent reads
CSE 344 - Winter 2016
![Page 31: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/31.jpg)
2. Isolation Level: Read Committed
• “Long duration” WRITE locks– Strict 2PL
• “Short duration” READ locks– Only acquire lock while reading (not 2PL)
31
Unrepeatable reads When reading same element twice, may get two different values
CSE 344 - Winter 2016
![Page 32: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/32.jpg)
3. Isolation Level: Repeatable Read
• “Long duration” WRITE locks– Strict 2PL
• “Long duration” READ locks– Strict 2PL
32
This is not serializable yet !!!
Why ?
CSE 344 - Winter 2016
![Page 33: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/33.jpg)
4. Isolation Level Serializable
• “Long duration” WRITE locks– Strict 2PL
• “Long duration” READ locks– Strict 2PL
• Predicate locking– To deal with phantoms
33CSE 344 - Winter 2016
![Page 34: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/34.jpg)
Beware!In commercial DBMSs:• Default level is often NOT serializable• 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 pbs• Bottom line: Read the doc for your DBMS!
CSE 344 - Winter 2016 34
![Page 35: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/35.jpg)
Demonstration with SQL ServerApplication 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
Application 2:set transaction isolation level serializable;begin transaction;select * from R; -- get a shared lockinsert into R values(2); -- blocked waiting on exclusive lock
-- App 2 unblocks and executes insert after app 1 commits/aborts
CSE 344 - Winter 2016 35
![Page 36: Introduction to Data Management CSE 344€¦ · – SQL Server, DB2, etc CSE 344 - Winter 2016 6. SQLite CSE 344 - Winter 2016 7 None READ LOCK RESERVED LOCK PENDING LOCK EXCLUSIVE](https://reader034.vdocument.in/reader034/viewer/2022052104/603f2419a49eab18ec345a55/html5/thumbnails/36.jpg)
Demonstration with SQL ServerApplication 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
Application 2:set transaction isolation level repeatable read;begin transaction;select * from R; -- get a shared lockinsert 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 344 - Winter 2016 36