1
Lecture 5 Transactions
Wednesday October 27th, 2010
Dan Suciu -- CSEP544 Fall 2010
Lecture 5 Transactions Wednesday October 27 th , 2010 Dan Suciu -- - - PowerPoint PPT Presentation
Lecture 5 Transactions Wednesday October 27 th , 2010 Dan Suciu -- CSEP544 Fall 2010 1 Announcement HW3: due next week Each customer has exactly one rental plan A many-one relationship: NO NEW TABLE ! Postgres
1
Dan Suciu -- CSEP544 Fall 2010
Dan Suciu -- CSEP544 Fall 2010 2
Dan Suciu -- CSEP544 Fall 2010 3
Dan Suciu -- CSEP544 Fall 2010 4
Dan Suciu -- CSEP544 Fall 2010 5
Dan Suciu -- CSEP544 Fall 2010 6
7 Dan Suciu -- CSEP544 Fall 2010
8 Dan Suciu -- CSEP544 Fall 2010
9 Dan Suciu -- CSEP544 Fall 2010
10 Dan Suciu -- CSEP544 Fall 2010
11
12
13 Dan Suciu -- CSEP544 Fall 2010
14
15
16
17 Dan Suciu -- CSEP544 Fall 2010
Dan Suciu -- CSEP544 Fall 2010 18
19
20
– High concurrency – High overhead in managing locks
– Many false conflicts – Less overhead in managing locks
– Hierarchical locking (and intentional locks) [commercial DBMSs] – Lock escalation
Dan Suciu -- CSEP544 Fall 2010
21 Dan Suciu -- CSEP544 Fall 2010
22
Dan Suciu -- CSEP544 Fall 2010
23 Dan Suciu -- CSEP544 Fall 2010
– Grant, or add the transaction to the element’s wait list
24 Dan Suciu -- CSEP544 Fall 2010
Dan Suciu -- CSEP544 Fall 2010 25
26
Dan Suciu -- CSEP544 Fall 2010
27
Rules:
transaction holds a lock on its parent B
– First lock parent then lock child – Keep parent locked only if may need to update it – Release lock on parent if child is not full
!
Dan Suciu -- CSEP544 Fall 2010
28
Dan Suciu -- CSEP544 Fall 2010
30
31
32
Dan Suciu -- CSEP544 Fall 2010
33
Dan Suciu -- CSEP544 Fall 2010 34
35
– Golden standard – Requires strict 2PL and predicate locking – But often too inefficient – Imagine there are few update operations and many long read operations
– Sacrifice correctness for efficiency – Often used in practice (often default) – Sometimes are hard to understand
Dan Suciu -- CSEP544 Fall 2010
36
Dan Suciu -- CSEP544 Fall 2010
37
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
Dan Suciu -- CSEP544 Fall 2010
38
Always read docs!
39
Dan Suciu -- CSEP544 Fall 2010
40
Dan Suciu -- CSEP544 Fall 2010
41
Dan Suciu -- CSEP544 Fall 2010
Dan Suciu -- CSEP544 Fall 2010 42
43
Client 1: START TRANSACTION INSERT INTO SmallProduct(name, price) SELECT pname, price FROM Product WHERE price <= 0.99 DELETE FROM Product WHERE price <=0.99 COMMIT Client 2: SET TRANSACTION READ ONLY START TRANSACTION SELECT count(*) FROM Product SELECT count(*) FROM SmallProduct COMMIT
Dan Suciu -- CSEP544 Fall 2010
Dan Suciu -- CSEP544 Fall 2010 44
45
Dan Suciu -- CSEP544 Fall 2010
46
Dan Suciu -- CSEP544 Fall 2010
Dan Suciu -- CSEP544 Fall 2010 47
48
– Why is this faster than FORCE policy?
Dan Suciu -- CSEP544 Fall 2010
Dan Suciu -- CSEP544 Fall 2010 49
50
Dan Suciu -- CSEP544 Fall 2010
51
– Lists all running transactions (active transactions) – For each txn: lastLSN = most recent update by transaction
– Lists all dirty pages – For each dirty page: recoveryLSN (recLSN)= first LSN that caused page to become dirty
– LSN, prevLSN = previous LSN for same transaction – other attributes
Dan Suciu -- CSEP544 Fall 2010
pageID recLSN P5 102 P6 103 P7 101 LSN prevLSN transID pageID Log entry 101
P7 102
P5 103 102 T200 P6 104 101 T100 P5
transID lastLSN T100 104 T200 103
P5 PageLSN=104 P6 PageLSN=103 P7 PageLSN=101
53
Dan Suciu -- CSEP544 Fall 2010
54
Dan Suciu -- CSEP544 Fall 2010
55
Dan Suciu -- CSEP544 Fall 2010
56
– Figure out what was going on at time of crash – List of dirty pages and active transactions
– Redo all operations, even for transactions that will not commit – Get back to state at the moment of the crash
– Remove effects of all uncommitted transactions – Log changes during undo in case of another crash during undo
Dan Suciu -- CSEP544 Fall 2010
57
Dan Suciu -- CSEP544 Fall 2010
58
– Determine point in log where to start REDO – Determine set of dirty pages when crashed
– Identify active transactions when crashed
– Rebuild active transactions table and dirty pages table – Reprocess the log from the checkpoint
– Compute: firstLSN = smallest of all recoveryLSN
Dan Suciu -- CSEP544 Fall 2010
pageID recLSN pageID transID lastLSN transID
pageID recLSN pageID transID lastLSN transID pageID recLSN pageID transID lastLSN transID
Dan Suciu -- CSEP544 Fall 2010 61
62
Dan Suciu -- CSEP544 Fall 2010
Dan Suciu -- CSEP544 Fall 2010 63
Dan Suciu -- CSEP544 Fall 2010 64
65
Dan Suciu -- CSEP544 Fall 2010
66
Dan Suciu -- CSEP544 Fall 2010
Dan Suciu -- CSEP544 Fall 2010 67
68 Dan Suciu -- CSEP544 Fall 2010
69
Dan Suciu -- CSEP544 Fall 2010
T
70 Dan Suciu -- CSEP544 Fall 2010
71
72
If TS(T) < WT(X) then ROLLBACK Else READ and update RT(X) to larger of TS(T) or RT(X)
If TS(T) < RT(X) then ROLLBACK Else if TS(T) < WT(X) ignore write & continue (Thomas Write Rule) Otherwise, WRITE and update WT(X) =TS(T)
Dan Suciu -- CSEP544 Fall 2010
U T
73 Dan Suciu -- CSEP544 Fall 2010
U T
74 Dan Suciu -- CSEP544 Fall 2010
V T
75 Dan Suciu -- CSEP544 Fall 2010
Dan Suciu -- CSEP544 Fall 2010 76
77 Dan Suciu -- CSEP544 Fall 2010
U T
78 Dan Suciu -- CSEP544 Fall 2010
U T
79 Dan Suciu -- CSEP544 Fall 2010
80
Dan Suciu -- CSEP544 Fall 2010
81 Dan Suciu -- CSEP544 Fall 2010
t t-1 t-2
82 Dan Suciu -- CSEP544 Fall 2010
– WT(Xt) = t and it never changes – RT(Xt) must still be maintained to check legality of writes
83 Dan Suciu -- CSEP544 Fall 2010
– Read all elements in RS(T). Time = START(T) – Validate (may need to rollback). Time = VAL(T) – Write all elements in WS(T). Time = FIN(T)
84 Dan Suciu -- CSEP544 Fall 2010
conflicts
85 Dan Suciu -- CSEP544 Fall 2010
conflicts
86 Dan Suciu -- CSEP544 Fall 2010
87 Dan Suciu -- CSEP544 Fall 2010
88 Dan Suciu -- CSEP544 Fall 2010
89 Dan Suciu -- CSEP544 Fall 2010
90 Dan Suciu -- CSEP544 Fall 2010
92
– Great when there are many conflicts – Poor when there are few conflicts
– Poor when there are many conflicts (rollbacks) – Great when there are few conflicts
– READ ONLY transactions → timestamps – READ/WRITE transactions → locks
93 Dan Suciu -- CSEP544 Fall 2010
94