Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe
CHAPTER 21 Cocurrecy Cotrol Techiques Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe
Itroductio Cocurrecy cotrol protocols Set of rules to guaratee serializability Two-phase lockig protocols Lock data items to prevet cocurret access Timestamp Uique idetifier for each trasactio Multiversio currecy cotrol protocols Use multiple versios of a data item Validatio or certificatio of a trasactio Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-3
21.1 Two-Phase Lockig Techiques for Cocurrecy Cotrol Lock Variable associated with a data item describig status for operatios that ca be applied Oe lock for each item i the database Biary locks Two states (values) Locked (1) Item caot be accessed Ulocked (0) Item ca be accessed whe requested Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-4
Two-Phase Lockig Techiques for Cocurrecy Cotrol (cot d.) Trasactio requests access by issuig a lock_item(x) operatio Figure 21.1 Lock ad ulock operatios for biary locks Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-5
Two-Phase Lockig Techiques for Cocurrecy Cotrol (cot d.) Lock table specifies items that have locks Lock maager subsystem Keeps track of ad cotrols access to locks Rules eforced by lock maager module At most oe trasactio ca hold the lock o a item at a give time Biary lockig too restrictive for database items Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-6
Two-Phase Lockig Techiques for Cocurrecy Cotrol (cot d.) Shared/exclusive or read/write locks Read operatios o the same item are ot coflictig Must have exclusive lock to write Three lockig operatios read_lock(x) write_lock(x) ulock(x) Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-7
Figure 21.2 Lockig ad ulockig operatios for two-mode (read/write, or shared/exclusive) locks Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-8
Two-Phase Lockig Techiques for Cocurrecy Cotrol (cot d.) Lock coversio Trasactio that already holds a lock allowed to covert the lock from oe state to aother Upgradig Issue a read_lock operatio the a write_lock operatio Dowgradig Issue a read_lock operatio after a write_lock operatio Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-9
Guarateeig Serializability by Two- Phase Lockig Two-phase lockig protocol All lockig operatios precede the first ulock operatio i the trasactio Phases Expadig (growig) phase New locks ca be acquired but oe ca be released Lock coversio upgrades must be doe durig this phase Shrikig phase Existig locks ca be released but oe ca be acquired Dowgrades must be doe durig this phase Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-10
Figure 21.3 Trasactios that do ot obey two-phase lockig (a) Two trasactios T1 ad T2 (b) Results of possible serial schedules of T1 ad T2 (c) A oserializable schedule S that uses locks Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-11
Guarateeig Serializability by Two- Phase Lockig If every trasactio i a schedule follows the twophase lockig protocol, schedule guarateed to be serializable Two-phase lockig may limit the amout of cocurrecy that ca occur i a schedule Some serializable schedules will be prohibited by two-phase lockig protocol Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-12
Variatios of Two-Phase Lockig Basic 2PL Techique described o previous slides Coservative (static) 2PL Requires a trasactio to lock all the items it accesses before the trasactio begis Predeclare read-set ad write-set Deadlock-free protocol Strict 2PL Trasactio does ot release exclusive locks util after it commits or aborts Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-13
Variatios of Two-Phase Lockig (cot d.) Rigorous 2PL Trasactio does ot release ay locks util after it commits or aborts Cocurrecy cotrol subsystem resposible for geeratig read_lock ad write_lock requests Lockig geerally cosidered to have high overhead Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-14
Dealig with Deadlock ad Starvatio Deadlock Occurs whe each trasactio T i a set is waitig for some item locked by some other trasactio T Both trasactios stuck i a waitig queue Figure 21.5 Illustratig the deadlock problem (a) A partial schedule of T1 ad T2 that is i a state of deadlock (b) A wait-for graph for the partial schedule i (a) Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-15
Dealig with Deadlock ad Starvatio (cot d.) Deadlock prevetio protocols Every trasactio locks all items it eeds i advace Orderig all items i the database Trasactio that eeds several items will lock them i that order Both approaches impractical Protocols based o a timestamp Wait-die Woud-wait Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-16
Dealig with Deadlock ad Starvatio (cot d.) No waitig algorithm If trasactio uable to obtai a lock, immediately aborted ad restarted later Cautious waitig algorithm Deadlock-free Deadlock detectio System checks to see if a state of deadlock exists Wait-for graph Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-17
Dealig with Deadlock ad Starvatio (cot d.) Victim selectio Decidig which trasactio to abort i case of deadlock Timeouts If system waits loger tha a predefied time, it aborts the trasactio Starvatio Occurs if a trasactio caot proceed for a idefiite period of time while other trasactios cotiue ormally Solutio: first-come-first-served queue Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-18
21.2 Cocurrecy Cotrol Based o Timestamp Orderig Timestamp Uique idetifier assiged by the DBMS to idetify a trasactio Assiged i the order submitted Trasactio start time Cocurrecy cotrol techiques based o timestamps do ot use locks Deadlocks caot occur Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-19
Cocurrecy Cotrol Based o Timestamp Orderig (cot d.) Geeratig timestamps Couter icremeted each time its value is assiged to a trasactio Curret date/time value of the system clock Esure o two timestamps are geerated durig the same tick of the clock Geeral approach Eforce equivalet serial order o the trasactios based o their timestamps Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-20
Cocurrecy Cotrol Based o Timestamp Orderig (cot d.) Timestamp orderig (TO) Allows iterleavig of trasactio operatios Must esure timestamp order is followed for each pair of coflictig operatios Each database item assiged two timestamp values read_ts(x) write_ts(x) Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-21
Cocurrecy Cotrol Based o Timestamp Orderig (cot d.) Basic TO algorithm If coflictig operatios detected, later operatio rejected by abortig trasactio that issued it Schedules produced guarateed to be coflict serializable Starvatio may occur Strict TO algorithm Esures schedules are both strict ad coflict serializable Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-22
Cocurrecy Cotrol Based o Timestamp Orderig (cot d.) Thomas s write rule Modificatio of basic TO algorithm Does ot eforce coflict serializability Rejects fewer write operatios by modifyig checks for write_item(x) operatio Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-23
21.3 Multiversio Cocurrecy Cotrol Techiques Several versios of a item are kept by a system Some read operatios that would be rejected i other techiques ca be accepted by readig a older versio of the item Maitais serializability More storage is eeded Multiversio currecy cotrol scheme types Based o timestamp orderig Based o two-phase lockig Validatio ad sapshot isolatio techiques Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-24
Multiversio Cocurrecy Cotrol Techiques (cot d.) Multiversio techique based o timestamp orderig Two timestamps associated with each versio are kept read_ts(x i ) write_ts(x i ) Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-25
Multiversio Cocurrecy Cotrol Techiques (cot d.) Multiversio two-phase lockig usig certify locks Three lockig modes: read, write, ad certify Figure 21.6 Lock compatibility tables (a) Lock compatibility table for read/write lockig scheme (b) Lock compatibility table for read/write/certify lockig scheme Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-26
21.4 Validatio (Optimistic) Techiques ad Sapshot Isolatio Cocurrecy Cotrol Optimistic techiques Also called validatio or certificatio techiques No checkig is doe while the trasactio is executig Updates ot applied directly to the database util fiished trasactio is validated All updates applied to local copies of data items Validatio phase checks whether ay of trasactio s updates violate serializability Trasactio committed or aborted based o result Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 20-27
Cocurrecy Cotrol Based o Sapshot Isolatio Trasactio sees data items based o committed values of the items i the database sapshot Does ot see updates that occur after trasactio starts Read operatios do ot require read locks Write operatios require write locks Temporary versio store keeps track of older versios of updated items Variatio: serializable sapshot isolatio (SSI) Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 20-28
21.5 Graularity of Data Items ad Multiple Graularity Lockig Size of data items kow as graularity Fie (small) Coarse (large) Larger the data item size, lower the degree of cocurrecy permitted Example: etire disk block locked Smaller the data item size, more locks required Higher overhead Best item size depeds o trasactio type Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 20-29
Multiple Graularity Level Lockig Lock ca be requested at ay level Figure 21.7 A graularity hierarchy for illustratig multiple graularity level lockig Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-30
Multiple Graularity Level Lockig (cot d.) Itetio locks are eeded Trasactio idicates alog the path from the root to the desired ode, what type of lock (shared or exclusive) it will require from oe of the ode s descedats Itetio lock types Itetio-shared (IS) Shared locks will be requested o a descedat ode Itetio-exclusive (IX) Exclusive locks will be requested Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-31
Multiple Graularity Level Lockig (cot d.) Itetio lock types (cot d.) Shared-itesio-exclusive (SIX) Curret ode is locked i shared mode but oe or more exclusive locks will be requested o a descedat ode Figure 21.8 Lock compatibility matrix for multiple graularity lockig Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-32
Multiple Graularity Level Lockig (cot d.) Multiple graularity lockig (MGL) protocol rules Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-33
21.6 Usig Locks for Cocurrecy Cotrol i Idexes Two-phase lockig ca be applied to B-tree ad B+ -tree idexes Nodes of a idex correspod to disk pages Holdig locks o idex pages could cause trasactio blockig Other approaches must be used Coservative approach Lock the root ode i exclusive mode ad the access the appropriate child ode of the root Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-34
Usig Locks for Cocurrecy Cotrol i Idexes (cot d.) Optimistic approach Request ad hold shared locks o odes leadig to the leaf ode, with exclusive lock o the leaf B-lik tree approach Siblig odes o the same level are liked at every level Allows shared locks whe requestig a page Requires lock be released before accessig the child ode Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-35
21.7 Other Cocurrecy Cotrol Issues Isertio Whe ew data item is iserted, it caot be accessed util after operatio is completed Deletio operatio o the existig data item Write lock must be obtaied before deletio Phatom problem Ca occur whe a ew record beig iserted satisfies a coditio that a set of records accessed by aother trasactio must satisfy Record causig coflict ot recogized by cocurrecy cotrol protocol Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-36
Other Cocurrecy Cotrol Issues (cot d.) Iteractive trasactios User ca iput a value of a data item to a trasactio T based o some value writte to the scree by trasactio T, which may ot have committed Solutio approach: postpoe output of trasactios to the scree util committed Latches Locks held for a short duratio Do ot follow usual cocurrecy cotrol protocol Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-37
21.8 Summary Cocurrecy cotrol techiques Two-phase lockig Timestamp-based orderig Multiversio protocols Sapshot isolatio Data item graularity Lockig protocols for idexes Phatom problem ad iteractive trasactio issues Copyright 2016 Ramez Elmasri ad Shamkat B. Navathe Slide 21-38