Carnegie Mellon Carnegie Mellon Univ. Dept. of Computer Science Database Applications C. Faloutsos Concurrency control
Carnegie Mellon C. Faloutsos2 General Overview Relational model - SQL Functional Dependencies & Normalization Physical Design &Indexing Query optimization Transaction processing –concurrency control –recovery
Carnegie Mellon C. Faloutsos3 Transactions - dfn = unit of work, eg. move $10 from savings to checking Atomicity (all or none) Consistency Isolation (as if alone) Durability recovery concurrency control
Carnegie Mellon C. Faloutsos4 Concurrency – overview why we want it? what does it mean ‘correct’ interleaving? –precedence graph how to achieve correct interleavings automatically? –concurrency control
Carnegie Mellon C. Faloutsos5 Problem concurrent access to data (consider ‘lost update’ problem) how to solve it?
Carnegie Mellon C. Faloutsos6 Read(N) Lost update problem – no locks time
Carnegie Mellon C. Faloutsos7 Solution – part 1 locks! (most popular solution) lock manager: grants/denies lock requests
Carnegie Mellon C. Faloutsos8 Lost update problem – with locks time T1 lock(N) Read(N) N=N-1 Write(N) Unlock(N) T2 lock(N) lock manager grants lock denies lock T2: waits grants lock to T2 Read(N)...
Carnegie Mellon C. Faloutsos9 Locks but, what if we all just want to read ‘N’?
Carnegie Mellon C. Faloutsos10 Solution – part 1 Locks and their flavors –exclusive (or write-) locks –shared (or read-) locks – compatibility matrix T2 wants T1 has SX S X
Carnegie Mellon C. Faloutsos11 Solution – part 1 transactions request locks (or upgrades) lock manager grants or blocks requests transactions release locks lock manager updates lock-table
Carnegie Mellon C. Faloutsos12 Solution – part 2 locks are not enough – eg., ‘inconsistent analysis’
Carnegie Mellon C. Faloutsos13 ‘Inconsistent analysis’ Precedence graph? time
Carnegie Mellon C. Faloutsos14 ‘Inconsistent analysis’ – w/ locks time T1 L(A) Read(A)... U(A) T2 L(A).... L(B).... the problem remains! Solution??
Carnegie Mellon C. Faloutsos15 General solution: Protocol(s) Most popular protocol: 2 Phase Locking (2PL)
Carnegie Mellon C. Faloutsos16 2PL X-lock version: transactions issue no lock requests, after the first ‘unlock’ THEOREM: if all transactions obey 2PL -> all schedules are serializable
Carnegie Mellon C. Faloutsos17 2PL – example ‘inconsistent analysis’ – why not 2PL? how would it be under 2PL?
Carnegie Mellon C. Faloutsos18 2PL – X/S lock version transactions issue no lock/upgrade request, after the first unlock/downgrade In general: ‘growing’ and ‘shrinking’ phase
Carnegie Mellon C. Faloutsos19 2PL – observations -limits concurrency -may lead to deadlocks -2PLC (keep locks until ‘commit’)
Carnegie Mellon C. Faloutsos20 Concurrency – overview what does it mean ‘correct’ interleaving? –precedence graph how to achieve correct interleavings automatically? concurrency control –locks + protocols 2PL, 2PLC graph protocols multiple granularity locks –
Carnegie Mellon C. Faloutsos21 Other protocols than 2-PL – graph -assumption: there is a (hierarchical) ordering on the data items, like, eg., pages of a B-tree A B C
Carnegie Mellon C. Faloutsos22 Eg., tree protocol (X-lock version) -a xact can request any item, on its first lock request -from then on, it can only request items for which it holds the parent lock -it can release locks at any time -it can NOT request an item twice
Carnegie Mellon C. Faloutsos23 Tree protocol - example T1 T2 L(B) L(D) L(H) U(D) L(E) U(E) L(D) U(B) U(H) L(G) U(D) U(G) GIH FED CB A -2PL? -‘correct’?
Carnegie Mellon C. Faloutsos24 Tree protocol -equivalent to 2PL? -deadlocks?
Carnegie Mellon C. Faloutsos25 More protocols -lock granularity – field? record? page? table? -Pros and cons? -(Ideally, each transaction should obtain a few locks)
Carnegie Mellon C. Faloutsos26 Multiple granularity Eg: attr1 attr2 record-nrecord2record1 Table2 Table1 DB
Carnegie Mellon C. Faloutsos27 what types of locks? X/S locks for leaf level + ‘intent’ locks, for higher levels IS: intent to obtain S-lock underneath IX: intent.... X-lock... S: shared lock for this level X: ex- lock for this level SIX: shared lock here; + IX
Carnegie Mellon C. Faloutsos28 Protocol -each xact obtains appropriate lock at highest level -proceeds to desirable lower levels
Carnegie Mellon C. Faloutsos29 Compatibility matrix T2 wants T1 has ISIXSSIXX IS IX S SIX X
Carnegie Mellon C. Faloutsos30 Conclusions -‘ACID’ for transactions -concurrency: -serializability (precedence graph) -one (popular) solution: locks + -2PL(C) protocol -graph protocols; multiple gran.