<<

The Issue

• Maintaining correctness when Implementing Isolation many transactions are accessing the database concurrently – Assuming each transaction maintains database Chapter 20 correctness when executed in isolation

1 2

Transaction Schedule T1: begin_transaction(); Isolation …. Transaction schedule ( applies to this) • Serial execution: p1,1; …. p p p – Since each transaction is consistent and isolated from all 1,1 1,2 1,3 others, schedule is guaranteed to be correct for all p1,2; To db applications …. server – Inadequate performance p1,3; local • Since system has multiple asynchronous resources and commit(); variables transaction uses only one at a time • Concurrent execution: • Consistent - performs correctly when executed in – Improved performance (multiprogramming) isolation starting in a consistent database state – Some interleavings produce correct result, others do not – Preserves database consistency – We are interested in concurrent schedules that are equivalent to serial schedules. These are referred to as – Moves database to a new state that corresponds to new real-world state serializable schedules. 3 4

Schedule Schedule

Schedule in which • Representation 1: Arriving schedule requests are serviced (merge of transaction (to preserve isolation) T : p p p p T1 schedules) 1 1 2 3 4 T : p p Concurrency 2 1 2 time → T2 Control database • Representation 2: T3 transaction p1,1 p1,2 p2,1 p1,3 p2,2 p1,4 schedules Database server time →

5 6

1 Concurrency Control Correct Schedules • Interleaved schedules equivalent to serial • Transforms arriving interleaved schedule into a schedules are the only ones guaranteed to be correct interleaved schedule to be submitted to the correct for all applications DBMS • Equivalence based on commutativity of operations – Delays servicing a request (reordering) - causes a transaction to wait • Definition: Database operations p1 and p2 – Refuses to service a request - causes transaction to commute if, for all initial database states, they abort (1) return the same results and • Actions taken by concurrency control have (2) leave the database in the same final state performance costs when executed in either order. – Goal is to avoid delaying or refusing to service a p1 p2 p2 p1 request 7 8

Commutativity of Read and Conventional Operations Write Operations

• Read • p1 commutes with p2 if – r(x, X) - copy the value of database variable x to – They operate on different data items

local variable X • w1(x) commutes with w2(y) and r2(y) • Write – Both are reads – w(x, X) - copy the value of local variable X to • r1(x) commutes with r2(x) database variable x • Operations that do not commute conflict • w (x) conflicts with w (x) • We use r1(x) and w1(x) to mean a read or 1 2 • w1(x) conflicts with r2(x) write of x by transaction T1

9 10

Equivalence of Schedules Equivalence of Schedules

• An interchange of adjacent operations of different • Equivalence is transitive: If S1 can be derived transactions in a schedule creates an equivalent from S by a series of such interchanges, S is schedule if the operations commute 2 1 ≠ equivalent to S S1: S1,1 pi,j pk,l S1,2 where i k 2

S2: S1,1 pk,l pi,j S1,2 – Each transaction computes the same results (since operations return the same values in both schedules) and hence writes the same values to the database. – The database is left in the same final state (since the state seen by S1,2 is the same in both schedules).

11 12

2 Example of Equivalence Example of Equivalence conflict S : r (x) r (x) w (x) r (y) w (y) 1 1 2 2 1 1 T1: begin transaction T2: begin transaction read (x, X); read (x,Y); S2: r1(x) r2(x) r1(y) w2(x) w1(y) X = X+4; write (y,Y); write (x, X); commit; S3: r1(x) r1(y) r2(x) w2(x) w1(y) commit;

S4: r1(x) r1(y) r2(x) w1(y) w2(x) initial state final state

x=1, y=3 r (x) r (x) w (y) w (x) x=5, y=1 S5: r1(x) r1(y) w1(y) r2(x) w2(x) 1 2 2 1 Interchange conflicting operations x=1, y=3 r (x) w (y) r (x) w (x) x=5, y=1 commuting operations 2 2 1 1 ordered in same way T T S1 is equivalent to S5 2 1 S is the serial schedule T , T Interchange 5 1 2 conflicting operations x=1, y=3 r1(x) w1(x) r2(x) w2(y) x=5, y=5 S is serializable 1 T1 T2 S1 is not equivalent to the serial schedule T2, T1 13 14

Serializable Schedules Isolation Levels

provides a conservative definition of • S is serializable if it is equivalent to a serial correctness schedule – For a particular application there might be many • Transactions are totally isolated in a serializable acceptable non-serializable schedules schedule – Requiring serializability might degrade performance • A schedule is correct for any application if it is a

• DBMSs offer a variety of isolation levels:

§©¨ ¥ § ¨ ¢ serializable schedule of consistent transactions ¡£¢£¤¦¥ is the most stringent • The schedule : – Lower levels of isolation give better performance r1(x) r2(y) w2(x) w1(y) is not serializable • Might allow incorrect schedules • Might be adequate for some applications 15 16

Serializable Conflict Equivalence

• Theorem - Schedule S can be derived from S 1 2 • Definition- Two schedules, S and S , of the by a sequence of commutative interchanges if 1 2 same set of operations are conflict equivalent if and only if conflicting operations in S and S 1 2 conflicting operations are ordered in the same are ordered in the same way way in both Only If: Commutative interchanges do not reorder – Or (using theorem) if one can be obtained from the conflicting operations other by a series of commutative interchanges If: A sequence of commutative interchanges can be

determined that takes S1 to S2 since conflicting operations do not have to be reordered (see text)

17 18

3 Conflict Equivalence View Equivalence • Result- A schedule is serializable if it is conflict equivalent to a serial schedule • Two schedules of the same set of operations ≡ r1(x) w2(x) w1(y) r2(y) r1(x) w1(y) w2(x) r2(y) are equivalent if: conflict conflict – Corresponding read operations in each return the same values (hence computations are the same)

• If in S transactions T1 and T2 have several pairs of – Both schedules yield the same final database state conflicting operations (p1,1 conflicts with p2,1 and • Conflict equivalence implies view equivalence. p conflicts with p ) then p must precede p 1,2 2,2 1,1 2,1 • View equivalence does not imply conflict and p1,2 must precede p2,2 (or vice versa) in order for S to be serializable. equivalence.

19 20

Conflict vs View Equivalence View Equivalence

set of schedules set of schedules T1: w(y) w(x) that are view that are conflict T2: r(y) w(x) equivalent to equivalent to serial schedules T3: w(x) serial schedules • Schedule is not conflict equivalent to a serial schedule • A concurrency control based on view equivalence should provide better performance than one based • Schedule has same effect as serial schedule on conflict equivalence since less reordering is done T2 T1 T3. It is view equivalent to a serial but … schedule and hence serializable • It is difficult to implement a view equivalence concurrency control 21 22

Conflict Equivalence and Serialization Graph of a Serializability Schedule, S • Serializability is a conservative notion of • Nodes represent transactions correctness and conflict equivalence • There is a directed edge from node Ti to node provides a conservative technique for Tj if Ti has an operation pi,k that conflicts with determining serializability an operation pj,r of Tj and pi,k precedes pj,r in S • However, a concurrency control that • Theorem - A schedule is conflict serializable guarantees conflict equivalence to serial if and only if its serialization graph has no schedules ensures correctness and is easily cycles implemented

23 24

4 Example Conflict (*) Intuition: Serializability and S: … p , …, p , ... 1,i 2,j Nonserializability

T2 T4 S is serializable in order • Consider the nonserializable schedule * T1 T2 T3 T4 T5 T6 T7 r1(x) w2(x) r2(y) w1(y) T1 T2 T1 T5 T6 T7 • Two ways to think about it:

– Because of the conflicts, the operations of T1 and T2 T3 cannot be interchanged to make an equivalent serial S is not serializable due schedule T2 T4 – Because T read x before T wrote it, T must precede to cycle T2 T6 T7 T2 1 2 1 T2 in any ordering, and because T1 wrote y after T2 read it, T1 must follow T2 in any ordering --- clearly an T1 T5 T6 T7 impossibility

T3 25 26

Recoverability: Schedules with Aborted Transactions Cascaded Abort

T1 : r (x) w(y) commit • Recoverable schedules solve abort problem T : w(x) abort 2 but allow cascaded abort: abort of one

• T2 has aborted but has had an indirect effect on the transaction forces abort of another database – schedule is unrecoverable T1: r (y) w(z) abort • Problem: T1 read uncommitted data - dirty read T2: r (x) w(y) abort • Solution: A concurrency control is recoverable if it T3: w(x) abort does not allow T1 to commit until all other • Better solution: prohibit dirty reads transactions that wrote values T1 read have committed request T1 : r (x) w(y) commit abort T2: w(x) abort 27 28

Dirty Write Strict Schedules

• Dirty write: A transaction writes a data item • Strict schedule: Dirty writes and dirty reads written by an active transaction are prohibited • Dirty write complicates rollback: • Strict and serializable are two different no rollback necessary properties – Strict, non-serializable schedule: T1: w(x) abort r1(x) w2(x) r2(y) w1(y) c1 c2 T2 : w(x) abort – Serializable, non-strict schedule: what value of x w2(x) r1(x) w2(y) r1(y) c1 c2 should be restored?

29 30

5 Concurrency Control Models of Concurrency Controls • Immediate Update – (the model we have discussed) Strict and – A write updates a database item Arriving schedule serializable schedule Concurrency Control – A read copies value from a database item (from transactions) (to processing engine) – Commit makes updates durable • Concurrency control cannot see entire schedule: – Abort undoes updates – It sees one request at a time and must decide • Deferred Update – (we will discuss this later) whether to allow it to be serviced – A write stores new value in the transaction’s intentions list (does not update the database) • Strategy: Do not service a request if: – A read copies value from the database or the transaction’s – It violates strictness or serializability, or intentions list – There is a possibility that a subsequent arrival – Commit uses intentions list to durably update database might cause a violation of serializability – Abort discards intentions list 31 32

Immediate vs. Deferred Update Models of Concurrency Controls

T’s • Pessimistic – commit database database intentions – A transaction requests permission for each database list (read/write) operation – Concurrency control can: read/write • Grant the operation (submit it for execution) read read/write • Delay it until a subsequent event occurs (commit or abort of another transaction), or • Abort the transaction Transaction Transaction – Decisions are made conservatively so that a commit request T T can always be granted • Takes precautions even if conflicts do not occur Immediate Update Deferred Update

33 34

Immediate-Update Pessimistic Models of Concurrency Controls Control • Optimistic - • The most commonly used control – Request for database operations (read/write) are • Consider first a simple case

always granted – Suppose such a control allowed a transaction, T1 , to – Request to commit might be denied perform some operation and then, while T1 was still active ,it allowed another transaction, T , to perform a • Transaction is aborted if it performed a non-serializable 2 conflicting operation operation – The schedule would not be strict and so this situation – Assumes that conflicts are not likely cannot be allowed • But consider a bit further what might happen …

35 36

6 Immediate-Update Pessimistic Immediate-Update Pessimistic Control Control

• If T1 executes op1(x) and then T2 executes a • Rule: conflicting operation, op2(x), T2 must follow T1 in any – Do not grant a request that imposes an ordering among active equivalent serial schedule. transactions (delay the requesting transaction)

• Problem: If T1 and T2 later make conflicting accesses – Grant a request that does not conflict with previously granted ′ ′ requests of active transactions to y, control cannot allow ordering op 2(y), op 1(y) – control has to use transitive closure of transaction ordering to • Rule can be used as each request arrives prevent loop in serialization graph (too complicated) • If a transaction’s request is delayed, it is forced to wait • Worse problem: (but the transaction is still considered active) w1(x) r2(x) w2(y) commit2 request_r1(y) – Delayed requests are reconsidered when a transaction completes (aborts or commits) since it becomes inactive looks good disaster 37 38

Immediate-Update Pessimistic Immediate-Update Pessimistic Control Control S: op op … op c • Result: Each schedule, S, is equivalent to a serial 1 2 n 1 schedule in which transactions are ordered in the no conflicting first commit order in which they commit in S (and possibly other operations serial schedules as well) S′: T op′ op′ … op′ – Reason: When a transaction commits, none of its 1 1 2 n operations conflict with those of other active transactions. all operations remaining Therefore it can be ordered before all active transactions. of T1 operations of S – Example: The following (non-serializable) schedule is not permitted because T1 was active at the time w2(x) • S and S′ are conflict equivalent (which conflicts with r (x) ) was requested 1 – The argument can be repeated at subsequent commits r1(x) w2(x) r2(y) w1(y) 39 40

Immediate-Update Pessimistic Control

• Commit order is useful since transactions • Problem: Controls that cause transactions to might perform external actions visible to wait can cause w (x) w (y) request request users 1 2 r1(y) r2(x) – After a deposit transaction commits, you expect a • Solution: Abort one transaction in the cycle subsequent transaction to see the new account – Use wait-for graph to detect cycle when a request is balance delayed or – Assume a deadlock when a transaction waits longer than some time-out period

41 42

7 Locking Implementation of an Locking Immediate-Update Pessimistic Control • Request for read on an item is granted if no transaction currently holds write lock on the item • A transaction can read a database item if it – Cannot read an item written by an active transaction holds a read (shared) lock on the item • Request for write lock granted if no transaction holds any lock on item • It can read or update the item if it holds a – Cannot write an item read/written by an active transaction write (exclusive) lock • Transaction is delayed if request cannot be granted • If the transaction does not already hold the required lock, a lock request is automatically Granted mode Requested mode read write made as part of the (read or write) request read x write x x 43 44

Locking Locking • Result: A lock is not granted if the requested • All locks held by a transaction are released access conflicts with a prior access of an active when the transaction completes (commits or transaction; instead the transaction waits. This aborts) enforces the rule: – Delayed requests are re-examined at this time – Do not grant a request that imposes an ordering among active transactions (delay the requesting transaction) • Resulting schedules are serializable and strict

45 46

Locking Implementation Locking • Associate a lock set, L(x), and a wait set, W(x), with each active database item, x r (x) w (x) c 1 1 1 – L(x) contains an entry for each granted lock on x

r1(x) w1(x) c1 w2(x) – W(x) contains an entry for each pending request on x r1(x) w2(x)w1(x) c1 concurrency control – When an entry is removed from L(x) (due to

w2(x) forced transaction termination), promote (non-conflicting) w2(x) w (x) can be to wait since T 2 1 scheduled since entries from W(x) using some scheduling policy holds read lock T releases its locks (e.g., FCFS) on x 1

• Associate a lock list, i , with each transaction, Ti. ¡ i links Ti’s elements in all lock and wait sets – Used to release locks on termination 47 48

8 Locking Implementation Manual Locking

L r r • Better performance possible if transactions are x allowed to release locks before commit – Ex: release lock on item when finished accessing the item W w Ti holds an r lock on x and waits for a w T1: l(x) r(x) l(y) r(y) u(x) w(y) u(y) lock on y T2: l(x) l(z) w(x) w(z) u(x) u(z) L w • However, early lock release can lead to non- y serializable schedules

W r w T1: l(x) r(x) u(x) l(y) r(y) u(y) T2: l(x) l(y) w(x) w(y) u(x) u(y)

commit i 49 50

Two-Phase Locking Two-Phase Locking • Transaction does not release a lock until it has all • Theorem: A concurrency control that uses two phase the locks it will ever require. locking produces only serializable schedules.

• Transaction has a locking phase followed by an – Proof (sketch): Consider two transactions T1 and T2 in unlocking phase schedule S produced by a two-phase locking control and assume T1’s first unlock, t1, precedes T2’s first unlock, t2. T′s first unlock • If they do not access common data items, then all operations Number commute. of locks T commits • Suppose they do. All of T1’s accesses to common items precede all of T ’s. If this were not so, T ’s first unlock must precede a lock request held by T 2 2 of T1. Since both transactions are two-phase, this implies that T2’s first unlock precedes T1’s first unlock, contradicting the assumption. time Hence, all conflicts between T1 and T2 are in the same direction. – It follows that the serialization graph is cycle-free since if • Guarantees serializability when locking is done there is a cycle T1, T2, …Tn then it must be the case that t1 < t2 < … < tn < t1 manually 51 52

Two-Phase Locking Two-Phase Locking

• A schedule produced by a two-phase locking control • A two-phase locking control that holds write locks is: until commit produces strict, serializable schedules – Equivalent to a serial schedule in which • A strict two-phase locking control holds all locks until transactions are ordered by the time of their first commit and produces strict serializable schedules unlock operation – This is automatic locking – Not necessarily recoverable (dirty reads and writes are possible) – Equivalent to a serial schedule in which transactions are ordered by their commit time T1: l(x) r(x) l(y) w(y) u(y) abort • “Strict” is used in two different ways: a control that T2: l(y) r(y) l(z) w(z) u(z) u(y) commit releases read locks early guarantees strictness, but is not strict two-phase locking control 53 54

9 Lock Granularity Lock Granularity • Data item: variable, record, , , file • When an item is accessed, the DBMS locks an entity that contains the item. The size of that entity • Table locking (coarse) determines the granularity of the lock – Lock entire table when a row is accessed. – Coarse granularity (large entities locked) • Row (tuple) locking (fine) • Advantage: If transactions tend to access multiple items in the same entity, fewer lock requests need to be – Lock only the row that is accessed. processed and less lock storage space required • Page locking (compromise) • Disadvantage: Concurrency is reduced since some items are unnecessarily locked – When a row is accessed, lock the containing – Fine granularity (small entities locked) page • Advantages and disadvantages are reversed 55 56

Objects and Semantic Abstract Operations and Commutativity Commutativity • Read/write operations have little associated semantics and hence little associated • A concurrency control that deals with operations at an commutativity. abstract level can recognize more commutativity and – Among operations on the same item, only reads achieve more concurrency commute. • Example: operations deposit(acct,n), withdraw(acct,n) • Abstract operations (for example operations on on an account object (where n is the dollar amount) objects) have more semantics, allowing Granted Mode – More commutativity to be recognized Requested Mode deposit( ) withdraw( ) – More concurrency to be achieved deposit( ) X withdraw( ) X X

57 58

A Concurrency Control Based on A Concurrency Control Based on Abstract Operations Abstract Operations

• Since T1 and T2 can both hold a deposit lock on the • Concurrency control grants deposit and same account object their deposit operations do not withdraw locks based on this table delay each other • If one transaction has a deposit lock on an – As a result, the schedule can contain: … deposit ( acct,n) … deposit (acct,m ) … commit account object, another transaction can also 1 2 1 obtain a deposit lock on the object or … deposit2( acct,m) … deposit1(acct,n ) … commit2 • Would not be possible if control viewed – But the two deposit operations must be isolated from each deposit as a read followed by a write and other. Assuming bal is the account balance, the schedule r1(bal) r2(bal) w1(bal) w2(bal) attempted to get read and write locks cannot be allowed

59 60

10 Partial vs. Total Operations Partial Operations

• deposit( ), withdraw( ) are total operations: they are • Example: account object defined in all database states. – deposit( ): defined in all initial states (total) • withdraw( ) has two possible outcomes: OK, NO – withdrawOK(acct,x): defined in all states in which • Partial operations are operations that are not defined bal ≥ x (partial) in all database states – withdrawNO(acct,x): defined in all states in which • withdraw( ) can be decomposed into two partial bal < x (partial) operations, which cover all database states: • When a transaction submits withdraw( ), control • withdrawOK( ) checks balance and converts to either withdrawOK( ) • withdrawNO( ) or withdrawNO( ) and acquires appropriate lock

61 62

Backward Commutativity Partial Operations • p backward commutes through q iff in all states in which the sequence q, p is defined, the sequence • Partial operations allow even more p, q is defined and semantics to be introduced – p and q return the same information in both and – The database is left in the same final state • Insight: while deposit( ) does not commute • Example: with withdraw( ), it does (backward) – deposit(a,m) backward commutes through commute with withdrawOK( ) withdrawOK(a,n) • In all database states in which withdrawOK(a,n), deposit(a,m) is defined, deposit(a,m), withdrawOK(a,n) is also defined. → withdrawOK(a,n) deposit(a,m) deposit(a,m) withdrawOK(a.n) – withdrawOK(a,n) does not backward commute through deposit(a,m) – Backward commute is not symmetric 63 64

A Concurrency Control Based on A Concurrency Control Based on Partial Abstract Operations Partial Abstract Operations

Granted Mode • Advantage: Increased concurrency and hence Requested Mode deposit( ) withdrawOK( ) withdrawNO( ) increased transaction throughput deposit( ) X withdrawOK( ) X • Disadvantage: Concurrency control has to access withdrawNO( ) X the database to determine the return value (hence the operation requested) before consulting table • Control grants deposit, withdrawOK, and withdrawNO locks • Hence (with an immediate update system) if T – Conflict is writes x and later aborts, physical restoration can • not symmetric be used. • based on backward commutativity

65 66

11 Atomicity and Abstract Operations • A write operation (the only conventional A Closer Look at Compensation operation that modifies items) conflicts with all other operations on the same data • We have discussed compensation before, • Physical restoration (restore original value) but now we want to use it in combination does not work with abstract operations since with locking to guarantee serializability and two operations that modify a data item might atomicity commute • We must define compensation more – How do you handle the schedule: …p (x) q (x) 1 2 carefully abort1 … if both operations modify x? • Logical restoration (with compensating operations) must be used – e.g., increment(x) compensates for decrement(x) 67 68

Logical Restoration Requirements for an Operation to (Compensation) Have a Compensating Operation • Consider schedule: p1(x) q2(x) abort1

• q2(x) must (backward) commute through • For an operation to have a compensating p1(x), since the concurrency control scheduled operation, it must be one-to-one the operation – For each input there is a unique output • This is equivalent to q2(x) p1(x) abort1 – The parameters of the compensating operation • Then abort1 can be implemented with a -1 are the same as the parameters of the operation compensating operation: q2(x) p1(x) p1 (x) being compensated – This is equivalent to q2(x) • increment(x) compensate decrement(x) -1 • Thus p1(x) q2(x) p1 (x) is equivalent to q2(x)

69 70

Logical Restoration (Compensation) Undo Operations

• Example: • Not all operations have compensating operations p1(x) = decrement(x) p -1(x) = increment(x) 1 compensating operation – For example, reset(x), which sets x to 0, is not one-to-one and has no compensating operation ≡ decrement1(x) increment2(x) increment1(x) – It does have an undo operation, set(x, X), which increment2(x) sets the value of x to what it was right before reset(x) was executed.

71 72

12 The Previous Approach Does What to Do with Undo Not Work Operations

reset1(x) reset2(x) set1(x, X1) • One approach is to require that the • Since the two resets commute, we can operation get an exclusive lock, so that no rewrite the schedule as other operation can come between an operation and its undo operation reset2(x) reset1(x) set1(x, X1) • But this schedule does not undo the result of

reset1(x), because the value when reset1(x) starts is different in the second schedule

73 74

Another Approach Another Approach

• Suppose pundo commutes with q. Then • Theorem p q pundo ≡ p pundo q – Serializability and recoverability is guaranteed if the condition under which an operation q • Now p has the same initial value in both does not conflict with a previously granted operation p is schedules, and thus the undo operation • q backward commutes through p, and works correctly. • Either p has a compensating operation, or when a p lock is held, pundo backward commutes through q

75 76

Locking Implementation of Still Another Approach • Sometimes we can decompose an operation that • When T creates a , s, insert a does not have a compensating operation into two i

marker for s in T ’s lock list, , that partial operations, each of which does have a i i compensating operation separates lock entries acquired before – withdraw(x) does not have a compensating operation creation from those acquired after creation • Depending on the initial value of the account, it might perform the withdrawal and decrement that value by x or it might just • When Ti rolls back to s, release all locks return no

preceding marker for s in (in addition to • It has an undo operation, conditionalDeposit(x,y) i – The two partial operations, withdrawOK(x) and undoing all updates made since savepoint withdrawNO(x) are one-to-one and hence do have creation) compensating operations. 77 78

13 Locking Implementation Locking Implementation of...

L r r • Chaining: nothing new • Recoverable queue: Since queue is implemented by a x undo Ti’s update to y and W w release its write lock separate server (different from DBMS), the locking when Ti rolls back to s discipline need not be two-phase; discipline can be s designed to suit the semantics of (the abstract operations) enqueue and dequeue w L i – Lock on head (tail) pointer released when dequeue y (enqueue) operations complete W r w • Hence not strict or isolated – Lock on entry that is enqueued or dequeued held to commit time 79 80

Recoverable Queue Locking Implementation of Nested Transactions begin transaction …. acquire L1, L2 • Nested transactions satisfy: enqueue(x) release L …. 1 – Nested transactions are isolated with respect to commit release L2 one another L2 – A parent does not execute concurrently with its x children

tail head – A child (and its descendants) is isolated from its

L1 siblings (and their descendants)

81 82

Locking Implementation of Intuition Nested Transactions • A request to read x by subtransaction T' of nested transaction T is granted even though an • A request to read x by subtransaction T ′ of nested transaction T is granted if: ancestor of T' holds a write lock on x T: begin transaction T: begin transaction – No other nested transaction holds a write lock on x … … – All other subtransactions of T holding write locks w(x) w(x) T’: begin transaction on x are ancestors of T ′ (hence are not executing) … … r(x) does r(x) r(x) could hold not conflict … … read or write T could hold with w(x) lock read lock commit commit commit T ' T '' without nesting with nesting

83 84

14 Locking Implementation of Locking Implementation of Nested Transactions • A request to write x by subtransaction T ' of nested Nested Transactions transaction T is granted if: • All locks obtained by T' are held until it – No other nested transaction holds a read or write lock on x completes – All other subtransactions of T holding read or write locks on x are ancestors of T ' (and hence are not executing) – If it aborts, all locks are discarded – If it commits, any locks it holds that are not could hold T cannot hold held by its parent are inherited by its parent read or write any locks lock • When top-level transaction (and hence T ' T '' entire nested transaction) commits, all locks are discarded

85 86

Locking Implementation of Example - Switch Sections transaction (sequential), Multilevel Transactions moves student from one section to another, Move(s1, s2) • Generalization of strict two-phase locking uses TestInc, Dec concurrency control Section abstr. L2 TestInc(s2) Dec(s1) – Uses semantics of operations at each level to determine commutativity Tuple abstr.

– Uses different concurrency control at each level L1 Sel(t2) Upd(t2) Upd(t1) Page abstr.

L0 Rd(p2) Rd(p2) Wr(p2) Rd(p1) Wr(p1)

87 time 88

Multilevel Transactions Multilevel Control • Example: – Move(s1,s2) produces TestInc(s2), Dec(s1) • Problem: A control assumes that the execution of – Move (s1,s2), Move (s1, s3) might produce 1 2 operations it schedules is isolated: If op and op do TestInc (s2), TestInc (s3), Dec (s1), Dec (s1) 1 2 1 2 2 1 not conflict, they can be executed concurrently and – Since two Dec operations on the same object commute (they do not impose an ordering among transactions), this the result will be either op1, op2 or op2, op1 schedule is equivalent to – Not true in a multilevel control where an operation is implemented as a program at the next lower level that TestInc1(s2), Dec1(s1), TestInc2(s3), Dec2(s1) and hence could be allowed by a multilevel control, but ... might invoke multiple operations at the level below. Hence, concurrent operations at one level might not be totally ordered at the next

89 90

15 Multilevel Transactions Guaranteeing Operation Isolation • Solution: Use a concurrency control at each Dec (s1) and Dec (s1) 1 2 level commute at L2 and L2 Dec1(s1) Dec2(s1) hence can execute – Li receives a request from Li+1to execute op concurrently, but – Concurrency control at Li, CCi, schedules op to be their implementation executed; it assumes execution is isolated at L0 is interleaved – op is implemented as a program, P, in Li L1 Upd1(t1) Upd2(t1) – P is executed as a subtransaction so that it is serializable with respect to other operations scheduled by CCi L Rd (p1) Rd (p1) Wr (p1) Wr (p1) 0 1 2 1 2 – Serializability guaranteed by CCi-1

91 92

Guaranteeing Operation Isolation A Multilevel Concurrency L request op request op i+1 1 2 Control for the Example

grants op , op Li 1 2 CCi • The control at L2 uses TestInc and Dec locks subtransaction at L i locks implementing op1 subtransactions at Li should (executed if op 1 • The control at L1 uses Sel and Upd locks be serializable (if op1 commutes lock granted) with op then execution of sub- 2 • The control at L uses Rd and Wr locks transactions equivalent to 0

op1, op2 or op2, op1)

Li-1 guarantees serializability of subtransactions at Li CCi-1 93 94

Timestamp-Ordered Concurrency Timestamp-Ordered Concurrency Control Control • Each transaction given a (unique) timestamp • Associated with each database item, x, are (current clock value) when initiated two timestamps: • Uses the immediate update model – wt(x), the largest timestamp of any transaction • Guarantees equivalent serial order based on that has written x, timestamps (initiation order) – rt(x), the largest timestamp of any transaction that has read x, – Control is static (as opposed to dynamic, in which the equivalent serial order is determined as the – and an indication of whether or not the last write schedule progresses) to that item is from a committed transaction

95 96

16 Timestamp-Ordered Concurrency Timestamp-Ordered Concurrency Control Control • If T requests to write x : • If T requests to read x: – W1: If TS(T) < rt(x), then T is too old; abort T – R1: if TS(T) < wt(x), then T is too old; abort T – W2: If rt(x) < TS(T) < wt(x), then no transaction that read x should have read the value T is attempting to write – R2: if TS(T) > wt(x), then and no transaction will read that value (See R1) • if the value of x is committed, grant T’s read and if • If x is committed, grant the request but do not do the write TS(T) > rt(x) assign TS(T) to rt(x) – This is called the Thomas Write Rule • if the value of x is not committed, T waits (to avoid • If x is not committed, T waits to see if newer value will commit. If it does, discard T’s write, else perform it a dirty read) – W3: If wt(x), rt(x) < TS(T), then if x is committed, grant the request and assign TS(T) to wt(x), else T waits

97 98

Example Timestamp-Ordered Concurrency • Assume TS(T ) < TS(T ), at t x and y are committed, 1 2 0 Control and x’s and y’s read and write timestamps are less • Control accepts schedules that are not conflict than TS(T1) equivalent to any serial schedule and would not T : r(y) w(x) commit 1 be accepted by a two-phase locking control T2: w(y) w(x) commit t0 t1 t2 t3 t4 – Previous example equivalent to T1, T2

t : (R2) TS(T ) > wt(y); assign TS(T ) to rt(y) • But additional space required in database for 1 1 1 storing timestamps and time for managing t2: (W3) TS(T2) > rt(y), wt(y); assign TS(T2) to wt(y) t3: (W3) TS(T2) > rt(x), wt(x); assign TS(T2) to wt(x) timestamps t4: (W2) rt(x) < TS(T1) < wt(x); grant request, but do not – Reading a data item now implies writing back a new do the write value of its timestamp

99 100

Optimistic Algorithms Optimistic Concurrency Control • Under the optimistic assumption that conflicts do not • Do task under simplifying (optimistic) assumption occur, read and write requests are always granted – Example: Operations rarely conflict (no locking, no overhead!) • Check afterwards if assumption was true. • Since conflicts might occur: – Example: Did a conflict occur? – Database might be corrupted if writes were immediate, hence a deferred-update model is used • Redo task if assumption was false – Transaction has to be “validated” when it completes – Example: If a conflict has occurred rollback, else commit • If a conflict has occurred abort (but no rollback is necessary) and • Performance benefit if assumption is generally true redo transaction and check can be done efficiently • Approach contrasts with pessimistic control which assumes conflicts are likely, takes preventative measures (locking), and does no validation

101 102

17 Optimistic Concurrency Control Optimistic Concurrency Control • Guarantees an equivalent serial schedule in which the • Transaction has three phases: order of transactions is the order in which they enter – Begin transaction validation (dynamic) • Read Phase - transaction executes: reads from database, writes to intentions list (deferred-update, no changes to database) • For simplicity, we will assume that validation and – Request commit write phases form a single critical section (only one • Validation Phase - check whether conflicts occurred during read transaction is in its validation/write phase at a time) phase; if yes abort (discard intentions list)

– Commit T1 enters T2 enters T3 enters • Write Phase - write intentions list to database (deferred update) if validation validation validation validation successful – For simplicity, we assume here that validation and write phases form a single critical section (only one transaction is validation/ equivalent serial order = T , T , T in its validation/write phase at a time) write phase 1 2 3 103 104

Validation Validation • When T enters validation, a check is made to 1 • Case 1: T1’s read phase started after T2 finished see if T1 conflicted with any transaction, T2, its validation/write phase that entered validation at an earlier time – T1 follows T2 in all conflicts, hence commit T1 (T1 follows T2 in equivalent serial order) • Check uses two sets constructed during read read validation/write phase: T1 starts phase T1 phase T1 – R(T1): identity of all database items T1 read time – W(T1): identity of all database items T1 wrote validation/write T2 ends

phase T2

105 106

Validation Validation

• Case 2: T1’s read phase overlaps T2’s validation/write phase ∩ ≠ Φ • Case 3: T ’s validation/write phase overlaps – If WS(T2) RS(T1) , then abort T1 1 • A read of T might have preceded a write of T – a possible violation 1 2 T2’s validation/write phase of equivalent serial order – Cannot happen since we have assumed that – Else commit T1 (T1 follows T2 in equivalent serial order) read validation/write validation/write phases do not overlap

T starts phase T1 phase T1 1 • Hence, all possible overlaps of T1 and T2 have been considered time read validation/write T2 ends phase T2 phase T2

107 108

18 Validation • A more practical optimistic control allows case 3 and Optimistic Concurrency Control avoids the bottleneck implied by only allowing only one transaction at a time in the validation/write phase. • No locking (and hence no waiting) means

• Case 3: T1’s validation/write phase overlaps T2’s deadlocks are not possible validation/write phase • Rollback is a problem if optimistic ∩ ∪ ≠ Φ – If WS(T2) (WS(T1) RS(T1)) , then abort T1 assumption is not valid: work of entire • A read or write of T1 might have preceded a write of T2 – a violation of equivalent serial order transaction is lost – Else commit T (T follows T in equivalent serial order) – With two-phase locking, rollback occurs only 1 1 2 with deadlock T1 starts read phase T1 valid/write phase T1 – With timestamp-ordered control, rollback is detected before transaction completes

read phase T2 valid/write phase T2 T2 ends 109 110

19