Con Currency Control
Con Currency Control
Database System Concepts - 6th Edition           15.2   ©Silberschatz, Korth and Sudarshan
                                         Lock-Based Protocols
              A lock is a mechanism to control concurrent access to a data item
              Data items can be locked in two modes:
                 1. exclusive (X) mode. Data item can be both read as well as
                    written. X-lock is requested using lock-X instruction.
                 2. shared (S) mode. Data item can only be read. S-lock is
                    requested using lock-S instruction.
              Lock requests are made to concurrency-control manager. Transaction
                  can proceed only after request is granted.
Database System Concepts - 6th Edition             15.3                ©Silberschatz, Korth and Sudarshan
                             Lock-Based Protocols (Cont.)
              Lock-compatibility matrix
Database System Concepts - 6th Edition               15.4                   ©Silberschatz, Korth and Sudarshan
                             Lock-Based Protocols (Cont.)
              Example of a transaction performing locking:
                                    T2: lock-S(A);
                                         read (A);
                                         unlock(A);
                                         lock-S(B);
                                         read (B);
                                         unlock(B);
                                         display(A+B)
              Locking as above is not sufficient to guarantee serializability — if A
                  and B get updated in-between the read of A and B, the displayed
                  sum would be wrong.
              A locking protocol is a set of rules followed by all transactions
                  while requesting and releasing locks. Locking protocols restrict the
                  set of possible schedules.
Database System Concepts - 6th Edition                  15.5              ©Silberschatz, Korth and Sudarshan
                         Pitfalls of Lock-Based Protocols
              Consider the partial schedule
Database System Concepts - 6th Edition              15.6                  ©Silberschatz, Korth and Sudarshan
              Pitfalls of Lock-Based Protocols (Cont.)
Database System Concepts - 6th Edition              15.7                  ©Silberschatz, Korth and Sudarshan
                        The Two-Phase Locking Protocol
             This is a protocol which ensures conflict-serializable schedules.
             Phase 1: Growing Phase
                    z   transaction may obtain locks
                    z   transaction may not release locks
             Phase 2: Shrinking Phase
                    z   transaction may release locks
                    z   transaction may not obtain locks
             The protocol assures serializability. It can be proved that the
                  transactions can be serialized in the order of their lock points (i.e.,
                  the point where a transaction acquired its final lock).
Database System Concepts - 6th Edition                 15.8                 ©Silberschatz, Korth and Sudarshan
              The Two-Phase Locking Protocol (Cont.)
              Two-phase locking does not ensure freedom from deadlocks.
              Cascading roll-back is possible under two-phase locking. To avoid
                  this, follow a modified protocol called strict two-phase locking. Here
                  a transaction must hold all its exclusive locks till it commits/aborts.
              Rigorous two-phase locking is even stricter: here all locks are held
                  till commit/abort. In this protocol transactions can be serialized in the
                  order in which they commit.
Database System Concepts - 6th Edition               15.9                    ©Silberschatz, Korth and Sudarshan
             The Two-Phase Locking Protocol (Cont.)
Database System Concepts - 6th Edition               15.10                   ©Silberschatz, Korth and Sudarshan
                                         Lock Conversions
              Two-phase locking with lock conversions:
                  – First Phase:
                    z   can acquire a lock-S on item
                    z   can acquire a lock-X on item
                    z   can convert a lock-S to a lock-X (upgrade)
                  – Second Phase:
                    z   can release a lock-S
                    z   can release a lock-X
                    z   can convert a lock-X to a lock-S (downgrade)
              This protocol assures serializability. But still relies on the programmer to
                  insert the various locking instructions.
Database System Concepts - 6th Edition                 15.11             ©Silberschatz, Korth and Sudarshan
                          Automatic Acquisition of Locks
              A transaction Ti issues the standard read/write instruction, without
                  explicit locking calls.
              The operation read(D) is processed as:
                                   if Ti has a lock on D
                                         then
                                             read(D)
                                         else begin
                                                if necessary wait until no other
                                                    transaction has a lock-X on D
                                                grant Ti a lock-S on D;
                                              read(D)
                                             end
Database System Concepts - 6th Edition                        15.12                 ©Silberschatz, Korth and Sudarshan
               Automatic Acquisition of Locks (Cont.)
              write(D) is processed as:
                  if Ti has a lock-X on D
                     then
                      write(D)
                    else begin
                        if necessary wait until no other trans. has any lock on D,
                        if Ti has a lock-S on D
                             then
                               upgrade lock on D to lock-X
                            else
                               grant Ti a lock-X on D
                          write(D)
                      end;
              All locks are released after commit or abort
Database System Concepts - 6th Edition              15.13                    ©Silberschatz, Korth and Sudarshan
                                 Implementation of Locking
              A lock manager can be implemented as a separate process to which
                  transactions send lock and unlock requests.
              The lock manager replies to a lock request by sending a lock grant
                  messages (or a message asking the transaction to roll back, in case of
                  a deadlock).
              The requesting transaction waits until its request is answered.
              The lock manager maintains a data-structure called a lock table to
                  record granted locks and pending requests.
              The lock table is usually implemented as an in-memory hash table
                  indexed on the name of the data item being locked.
Database System Concepts - 6th Edition            15.14                 ©Silberschatz, Korth and Sudarshan
                                         Lock Table
                                                   Black rectangles indicate granted locks,
                                                    white ones indicate waiting requests
                                                   Lock table also records the type of lock
                                                    granted or requested
                                                   New request is added to the end of the
                                                    queue of requests for the data item, and
                                                    granted if it is compatible with all earlier
                                                    locks
                                                   Unlock requests result in the request
                                                    being deleted, and later requests are
                                                    checked to see if they can now be
                                                    granted
                                                   If transaction aborts, all waiting or
                                                    granted requests of the transaction are
                                                    deleted
                                                     z   lock manager may keep a list of
                                                         locks held by each transaction, to
                                                         implement this efficiently
Database System Concepts - 6th Edition      15.15                         ©Silberschatz, Korth and Sudarshan
                                         Graph-Based Protocols
             Graph-based protocols are an alternative to two-phase locking.
             Impose a partial ordering → on the set D = {d1, d2 ,..., dh} of all data
                  items.
                    z   If di → dj then any transaction accessing both di and dj must
                        access di before accessing dj.
                    z   Implies that the set D may now be viewed as a directed acyclic
                        graph, called a database graph.
             The tree-protocol is a simple kind of graph protocol.
Database System Concepts - 6th Edition               15.16                  ©Silberschatz, Korth and Sudarshan
                                         Tree Protocol
Database System Concepts - 6th Edition               15.17                  ©Silberschatz, Korth and Sudarshan
                           Graph-Based Protocols (Cont.)
              The tree protocol ensures conflict serializability as well as freedom from
                  deadlock.
              Unlocking may occur earlier in the tree-locking protocol than in the two-
                  phase locking protocol.
                    z   shorter waiting times, and increase in concurrency
                    z   protocol is deadlock-free, no rollbacks are required
              Drawbacks
                    z   Protocol does not guarantee recoverability or cascade freedom
                           Need         to introduce commit dependencies to ensure recoverability
                    z   Transactions may have to lock data items that they do not access.
                           increased        locking overhead, and additional waiting time
                           potential       decrease in concurrency
              Schedules not possible under two-phase locking are possible under tree
                  protocol, and vice versa.
Database System Concepts - 6th Edition                     15.18                  ©Silberschatz, Korth and Sudarshan
                                         Deadlock Handling
              Consider the following two transactions:
                          T1:      write (X)   T2:     write(Y)
                                   write(Y)            write(X)
              Schedule with deadlock
Database System Concepts - 6th Edition               15.19        ©Silberschatz, Korth and Sudarshan
                                         Deadlock Handling
             System is deadlocked if there is a set of transactions such that every
                  transaction in the set is waiting for another transaction in the set.
             Deadlock prevention protocols ensure that the system will never
                  enter into a deadlock state. Some prevention strategies:
                    z   Require that each transaction locks all its data items before it
                        begins execution (predeclaration).
                    z   Impose partial ordering of all data items and require that a
                        transaction can lock data items only in the order specified by the
                        partial order (graph-based protocol).
Database System Concepts - 6th Edition                15.20                   ©Silberschatz, Korth and Sudarshan
                 More Deadlock Prevention Strategies
              Following schemes use transaction timestamps for the sake of deadlock
                  prevention alone.
              wait-die scheme — non-preemptive
                    z   older transaction may wait for younger one to release data item.
                        Younger transactions never wait for older ones; they are rolled back
                        instead.
                    z   a transaction may die several times before acquiring needed data
                        item
              wound-wait scheme — preemptive
                    z   older transaction wounds (forces rollback) of younger transaction
                        instead of waiting for it. Younger transactions may wait for older
                        ones.
                    z   may be fewer rollbacks than wait-die scheme
Database System Concepts - 6th Edition               15.21                  ©Silberschatz, Korth and Sudarshan
                               Deadlock prevention (Cont.)
             Both in wait-die and in wound-wait schemes, a rolled back
                  transactions is restarted with its original timestamp. Older transactions
                  thus have precedence over newer ones, and starvation is hence
                  avoided.
             Timeout-Based Schemes:
                    z   a transaction waits for a lock only for a specified amount of time.
                        After that, the wait times out and the transaction is rolled back.
                    z   thus deadlocks are not possible
                    z   simple to implement; but starvation is possible. Also difficult to
                        determine good value of the timeout interval.
Database System Concepts - 6th Edition                15.22                   ©Silberschatz, Korth and Sudarshan
                                         Deadlock Detection
             Deadlocks can be described as a wait-for graph, which consists of a
                  pair G = (V,E),
                    z   V is a set of vertices (all the transactions in the system)
                    z   E is a set of edges; each element is an ordered pair Ti →Tj.
             If Ti → Tj is in E, then there is a directed edge from Ti to Tj, implying
                  that Ti is waiting for Tj to release a data item.
             When Ti requests a data item currently being held by Tj, then the edge
                  Ti Tj is inserted in the wait-for graph. This edge is removed only when
                  Tj is no longer holding a data item needed by Ti.
             The system is in a deadlock state if and only if the wait-for graph has a
                  cycle. Must invoke a deadlock-detection algorithm periodically to look
                  for cycles.
Database System Concepts - 6th Edition                15.23                    ©Silberschatz, Korth and Sudarshan
                              Deadlock Detection (Cont.)
Database System Concepts - 6th Edition         15.24                ©Silberschatz, Korth and Sudarshan
                                            Deadlock Recovery
             When deadlock is detected:
                    z   Some transaction will have to rolled back (made a victim) to break
                        deadlock. Select that transaction as victim that will incur minimum
                        cost.
                    z   Rollback -- determine how far to roll back transaction
                           Total        rollback: Abort the transaction and then restart it.
                           More    effective to roll back transaction only as far as necessary
                              to break deadlock.
                    z   Starvation happens if same transaction is always chosen as
                        victim. Include the number of rollbacks in the cost factor to avoid
                        starvation
Database System Concepts - 6th Edition                       15.25                   ©Silberschatz, Korth and Sudarshan
                                         Multiple Granularity
             Allow data items to be of various sizes and define a hierarchy of data
                  granularities, where the small granularities are nested within larger
                  ones.
             Can be represented graphically as a tree (but don't confuse with tree-
                  locking protocol)
             When a transaction locks a node in the tree explicitly, it implicitly locks
                  all the node's descendents in the same mode.
             Granularity of locking (level in tree where locking is done):
                    z   fine granularity (lower in tree): high concurrency, high locking
                        overhead
                    z   coarse granularity (higher in tree): low locking overhead, low
                        concurrency
Database System Concepts - 6th Edition               15.26                  ©Silberschatz, Korth and Sudarshan
                        Example of Granularity Hierarchy
Database System Concepts - 6th Edition              15.27                   ©Silberschatz, Korth and Sudarshan
                                         Intention Lock Modes
             In addition to S and X lock modes, there are three additional lock
                  modes with multiple granularity:
                    z   intention-shared (IS): indicates explicit locking at a lower level of
                        the tree but only with shared locks.
                    z   intention-exclusive (IX): indicates explicit locking at a lower level
                        with exclusive or shared locks
                    z   shared and intention-exclusive (SIX): the subtree rooted by that
                        node is locked explicitly in shared mode and explicit locking is
                        being done at a lower level with exclusive-mode locks.
             Intention locks allow a higher level node to be locked in S or X mode
                  without having to check all descendent nodes.
Database System Concepts - 6th Edition                15.28                   ©Silberschatz, Korth and Sudarshan
           Compatibility Matrix with Intention Lock Modes
Database System Concepts - 6th Edition           15.29           ©Silberschatz, Korth and Sudarshan
                  Multiple Granularity Locking Scheme
              Transaction Ti can lock a node Q, using the following rules:
                    1.   The lock compatibility matrix must be observed.
                    2.   The root of the tree must be locked first, and may be locked in
                         any mode.
                    3.   A node Q can be locked by Ti in S or IS mode only if the parent
                         of Q is currently locked by Ti in either IX or IS mode.
                    4.   A node Q can be locked by Ti in X, SIX, or IX mode only if the
                         parent of Q is currently locked by Ti in either IX or SIX mode.
                    5.   Ti can lock a node only if it has not previously unlocked any
                         node (that is, Ti is two-phase).
                    6.   Ti can unlock a node Q only if none of the children of Q are
                         currently locked by Ti.
              Observe that locks are acquired in root-to-leaf order, whereas they
                  are released in leaf-to-root order.
Database System Concepts - 6th Edition               15.30                   ©Silberschatz, Korth and Sudarshan
                               Timestamp-Based Protocols
              Each transaction is issued a timestamp when it enters the system. If
                  an old transaction Ti has time-stamp TS(Ti), a new transaction Tj is
                  assigned time-stamp TS(Tj) such that TS(Ti) <TS(Tj).
              The protocol manages concurrent execution such that the time-
                  stamps determine the serializability order.
              In order to assure such behavior, the protocol maintains for each
                  data Q two timestamp values:
                    z   W-timestamp(Q) is the largest time-stamp of any transaction
                        that executed write(Q) successfully.
                    z   R-timestamp(Q) is the largest time-stamp of any transaction
                        that executed read(Q) successfully.
Database System Concepts - 6th Edition             15.31                  ©Silberschatz, Korth and Sudarshan
                Timestamp-Based Protocols (Cont.)
              The timestamp ordering protocol ensures that any conflicting read and
                  write operations are executed in timestamp order.
              Suppose a transaction Ti issues a read(Q):
                    1.   If TS(Ti) ≤ W-timestamp(Q), then Ti needs to read a value of Q
                         that was already overwritten.
                               Hence, the read operation is rejected, and Ti is rolled back.
                    2.   If TS(Ti)≥ W-timestamp(Q), then the read operation is executed,
                         and R-timestamp(Q) is set to max(R-timestamp(Q), TS(Ti)).
Database System Concepts - 6th Edition                  15.32                  ©Silberschatz, Korth and Sudarshan
                    Timestamp-Based Protocols (Cont.)
             Suppose that transaction Ti issues write(Q).
                    1.   If TS(Ti) < R-timestamp(Q), then the value of Q that Ti is
                         producing was needed previously, and the system assumed that
                         that value would never be produced.
                               Hence, the write operation is rejected, and Ti is rolled back.
                    2.   If TS(Ti) < W-timestamp(Q), then Ti is attempting to write an
                         obsolete value of Q.
                               Hence, this write operation is rejected, and Ti is rolled back.
                    3.   Otherwise, the write operation is executed, and W-timestamp(Q)
                         is set to TS(Ti).
Database System Concepts - 6th Edition                   15.33                   ©Silberschatz, Korth and Sudarshan
                           Example Use of the Protocol
              A partial schedule for several data items for transactions with
              timestamps 1, 2, 3, 4, 5
Database System Concepts - 6th Edition            15.34                  ©Silberschatz, Korth and Sudarshan
              Correctness of Timestamp-Ordering Protocol
Database System Concepts - 6th Edition            15.35                    ©Silberschatz, Korth and Sudarshan
                 Recoverability and Cascade Freedom
             Problem with timestamp-ordering protocol:
                    z   Suppose Ti aborts, but Tj has read a data item written by Ti
                    z   Then Tj must abort; if Tj had been allowed to commit earlier, the
                        schedule is not recoverable.
                    z   Further, any transaction that has read a data item written by Tj
                        must abort
                    z   This can lead to cascading rollback --- that is, a chain of
                        rollbacks
                  Solution 1:
                    z   A transaction is structured such that its writes are all performed
                        at the end of its processing
                    z   All writes of a transaction form an atomic action; no transaction
                        may execute while a transaction is being written
                    z   A transaction that aborts is restarted with a new timestamp
             Solution 2: Limited form of locking: wait for data to be committed
                  before reading it
             Solution 3: Use commit dependencies to ensure recoverability
Database System Concepts - 6th Edition                15.36                   ©Silberschatz, Korth and Sudarshan
                                         Thomas’ Write Rule
             Modified version of the timestamp-ordering protocol in which obsolete
                  write operations may be ignored under certain circumstances.
             When Ti attempts to write data item Q, if TS(Ti) < W-timestamp(Q),
                  then Ti is attempting to write an obsolete value of {Q}.
                    z   Rather than rolling back Ti as the timestamp ordering protocol
                        would have done, this {write} operation can be ignored.
             Otherwise this protocol is the same as the timestamp ordering
                  protocol.
             Thomas' Write Rule allows greater potential concurrency.
Database System Concepts - 6th Edition               15.37                   ©Silberschatz, Korth and Sudarshan
                                         View Serializability
               Let S and S´ be two schedules with the same set of transactions. S
                    and S´ are view equivalent if the following three conditions are met,
                    for each data item Q,
                     1.    If in schedule S, transaction Ti reads the initial value of Q, then in
                           schedule S´ also transaction Ti must read the initial value of Q.
                     2.    If in schedule S transaction Ti executes read(Q), and that value
                           was produced by transaction Tj (if any), then in schedule S´ also
                           transaction Ti must read the value of Q that was produced by the
                           same write(Q) operation of transaction Tj .
                     3.    The transaction (if any) that performs the final write(Q) operation
                           in schedule S must also perform the final write(Q) operation in
                           schedule S´.
                    As can be seen, view equivalence is also based purely on reads and
                    writes alone.
Database System Concepts - 6th Edition                  15.38                   ©Silberschatz, Korth and Sudarshan
                                  View Serializability (Cont.)
               A schedule S is view serializable if it is view equivalent to a serial
                    schedule.
               Every conflict serializable schedule is also view serializable.
               Below is a schedule which is view-serializable but not conflict
                    serializable.
Database System Concepts - 6th Edition            15.39                  ©Silberschatz, Korth and Sudarshan
                            Other Notions of Serializability
               The schedule below produces same outcome as the serial schedule
                    < T1, T5 >, yet is not conflict equivalent or view equivalent to it.
Database System Concepts - 6th Edition                15.40                    ©Silberschatz, Korth and Sudarshan
                                 Test for View Serializability
               The precedence graph test for conflict serializability cannot be used
                    directly to test for view serializability.
                     z    Extension to test for view serializability has cost exponential in the
                          size of the precedence graph.
               The problem of checking if a schedule is view serializable falls in the
                    class of NP-complete problems.
                     z     Thus, the existence of an efficient algorithm is extremely unlikely.
               However, practical algorithms that just check some sufficient
                    conditions for view serializability can still be used.
Database System Concepts - 6th Edition                  15.41                  ©Silberschatz, Korth and Sudarshan
                                  Validation-Based Protocol
              Execution of transaction Ti is done in three phases.
               1. Read and execution phase: Transaction Ti writes only to
                  temporary local variables
               2. Validation phase: Transaction Ti performs a ``validation test''
                  to determine if local variables can be written without violating
                  serializability.
               3. Write phase: If Ti is validated, the updates are applied to the
                  database; otherwise, Ti is rolled back.
              The three phases of concurrently executing transactions can be
                  interleaved, but each transaction must go through the three phases in
                  that order.
                    z   Assume for simplicity that the validation and write phase occur
                        together, atomically and serially
                           i.e.,    only one transaction executes validation/write at a time.
              Also called as optimistic concurrency control since transaction
                  executes fully in the hope that all will go well during validation
Database System Concepts - 6th Edition                    15.42                   ©Silberschatz, Korth and Sudarshan
                        Validation-Based Protocol (Cont.)
             Each transaction Ti has 3 timestamps:
                    z   Start(Ti) : the time when Ti started its execution
                    z   Validation(Ti): the time when Ti entered its validation phase
                    z   Finish(Ti) : the time when Ti finished its write phase
             Serializability order is determined by timestamp given at validation
                  time, to increase concurrency.
                    z   Thus TS(Ti) is given the value of Validation(Ti).
             This protocol is useful and gives greater degree of concurrency if
                  probability of conflicts is low.
                    z   because the serializability order is not pre-decided, and
                    z   relatively few transactions will have to be rolled back.
Database System Concepts - 6th Edition                15.43                      ©Silberschatz, Korth and Sudarshan
                        Validation Test for Transaction Tj
             If for all Ti with TS (Ti) < TS (Tj) either one of the following condition
                  holds:
                    z    finish(Ti) < start(Tj)
                    z    start(Tj) < finish(Ti) < validation(Tj) and the set of data items
                         written by Ti does not intersect with the set of data items read by
                         Tj.
                  then validation succeeds and Tj can be committed. Otherwise,
                  validation fails and Tj is aborted.
             Justification: Either the first condition is satisfied, and there is no
                  overlapped execution, or the second condition is satisfied and
                        the writes of Tj do not affect reads of Ti since they occur after Ti
                         has finished its reads.
                        the writes of Ti do not affect reads of Tj since Tj does not read
                         any item written by Ti.
Database System Concepts - 6th Edition                 15.44                   ©Silberschatz, Korth and Sudarshan
                       Schedule Produced by Validation
              Example of schedule produced using validation
Database System Concepts - 6th Edition        15.45            ©Silberschatz, Korth and Sudarshan
                                         Multiversion Schemes
             Multiversion schemes keep old versions of data item to increase
                  concurrency.
                    z   Multiversion Timestamp Ordering
                    z   Multiversion Two-Phase Locking
             Each successful write results in the creation of a new version of the
                  data item written.
             Use timestamps to label versions.
             When a read(Q) operation is issued, select an appropriate version of
                  Q based on the timestamp of the transaction, and return the value of
                  the selected version.
             reads never have to wait as an appropriate version is returned
                  immediately.
Database System Concepts - 6th Edition            15.46                  ©Silberschatz, Korth and Sudarshan
                        Multiversion Timestamp Ordering
             Each data item Q has a sequence of versions <Q1, Q2,...., Qm>. Each
                  version Qk contains three data fields:
                    z   Content -- the value of version Qk.
                    z   W-timestamp(Qk) -- timestamp of the transaction that created
                        (wrote) version Qk
                    z   R-timestamp(Qk) -- largest timestamp of a transaction that
                        successfully read version Qk
             when a transaction Ti creates a new version Qk of Q, Qk's W-
                  timestamp and R-timestamp are initialized to TS(Ti).
             R-timestamp of Qk is updated whenever a transaction Tj reads Qk, and
                  TS(Tj) > R-timestamp(Qk).
Database System Concepts - 6th Edition              15.47                 ©Silberschatz, Korth and Sudarshan
             Multiversion Timestamp Ordering (Cont)
              Suppose that transaction Ti issues a read(Q) or write(Q) operation. Let
                  Qk denote the version of Q whose write timestamp is the largest write
                  timestamp less than or equal to TS(Ti).
                    1.   If transaction Ti issues a read(Q), then the value returned is the
                         content of version Qk.
                    2.   If transaction Ti issues a write(Q)
                          1.    if TS(Ti) < R-timestamp(Qk), then transaction Ti is rolled back.
                          2.    if TS(Ti) = W-timestamp(Qk), the contents of Qk are overwritten
                          3.    else a new version of Q is created.
              Observe that
                    z    Reads always succeed.
                    z    A write by Ti is rejected if some other transaction Tj that (in the
                         serialization order defined by the timestamp values) should read
                         Ti's write, has already read a version created by a transaction older
                         than Ti.
              Protocol guarantees serializability.
Database System Concepts - 6th Edition                   15.48                  ©Silberschatz, Korth and Sudarshan
                        Multiversion Two-Phase Locking
              Differentiates between read-only transactions and update transactions
              Update transactions acquire read and write locks, and hold all locks up
                  to the end of the transaction. That is, update transactions follow rigorous
                  two-phase locking.
                    z   Each successful write results in the creation of a new version of the
                        data item written.
                    z   each version of a data item has a single timestamp whose value is
                        obtained from a counter ts-counter that is incremented during
                        commit processing.
              Read-only transactions are assigned a timestamp by reading the
                  current value of ts-counter before they start execution; they follow the
                  multiversion timestamp-ordering protocol for performing reads.
Database System Concepts - 6th Edition               15.49                  ©Silberschatz, Korth and Sudarshan
             Multiversion Two-Phase Locking (Cont.)
              When an update transaction wants to read a data item:
                    z   it obtains a shared lock on it, and reads the latest version.
              When it wants to write an item
                    z   it obtains X lock on; it then creates a new version of the item and
                        sets this version's timestamp to ∞.
              When update transaction Ti completes, commit processing occurs:
                    z   Ti sets timestamp on the versions it has created to ts-counter + 1
                    z   Ti increments ts-counter by 1
              Read-only transactions that start after Ti increments ts-counter will see
                  the values updated by Ti.
              Read-only transactions that start before Ti increments the
                  ts-counter will see the value before the updates by Ti.
              Only serializable schedules are produced.
Database System Concepts - 6th Edition                15.50                   ©Silberschatz, Korth and Sudarshan
                            MVCC: Implementation Issues
             Creation of multiple versions increases storage overhead
                    z   Extra tuples
                    z   Extra space in each tuple for storing version information
             Versions can, however, be garbage collected
                    z   E.g., if Q has two versions Q5 and Q9, and the oldest active
                        transaction has timestamp > 9, than Q5 will never be required
                        again
Database System Concepts - 6th Edition               15.51                  ©Silberschatz, Korth and Sudarshan
                                          Snapshot Isolation
            Motivation: Decision support queries that read large amounts of data
                have concurrency conflicts with OLTP transactions that update a few
                rows
                  z    Poor performance results
            Solution 1: Give logical “snapshot” of database state to read only
                transactions, read-write transactions use normal locking
                  z    Multiversion 2-phase locking
                  z    Works well, but how does system know a transaction is read only?
            Solution 2: Give snapshot of database state to every transaction,
                updates alone use 2-phase locking to guard against concurrent
                updates
                  z    Problem: variety of anomalies such as lost update can result
                  z    Partial solution: snapshot isolation level (next slide)
                         Proposed        by Berenson et al, SIGMOD 1995
                         Variants       implemented in many database systems
                              – E.g., Oracle, PostgreSQL, SQL Server 2005
Database System Concepts - 6th Edition                  15.52                    ©Silberschatz, Korth and Sudarshan
                                          Snapshot Isolation
            A transaction T1 executing with Snapshot                       T1               T2             T3
             Isolation
                                                                         W(Y := 1)
               z    takes snapshot of committed data at
                    start                                                Commit
               z    always reads/modifies data in its own                            Start
                    snapshot                                                         R(X) Æ 0
               z    updates of concurrent transactions are                           R(Y)Æ 1
                    not visible to T1
               z    writes of T1 complete when it commits                                              W(X:=2)
               z    First-committer-wins rule:                                                         W(Z:=3)
                         Commits only if no other concurrent                                          Commit
                          transaction has already written data                       R(Z) Æ 0
                          that T1 intends to write.
                                                                                     R(Y) Æ 1
                                                                                     W(X:=3)
                                     Concurrent updates not visible
                                            Own updates are visible                  Commit-Req
                                             Not first-committer of X                Abort
                                Serialization error, T2 is rolled back
Database System Concepts - 6th Edition                      15.53                      ©Silberschatz, Korth and Sudarshan
                                               Benefits of SI
             Reading is never blocked
                    z   and also doesn’t block other txns activities
             Performance similar to Read Committed
             Avoids the usual anomalies
                    z   No dirty read
                    z   No lost update
                    z   No non-repeatable read
                    z   Predicate based selects are repeatable (no phantoms)
             Problems with SI
                    z   SI does not always give serializable executions
                           Serializable:    among two concurrent txns, one sees the effects
                              of the other
                           In    SI: neither sees the effects of the other
                    z   Result: Integrity constraints can be violated
Database System Concepts - 6th Edition                    15.54                ©Silberschatz, Korth and Sudarshan
                                             Snapshot Isolation
             E.g., of problem with SI
                    z   T1: x:=y
                    z   T2: y:= x
                    z   Initially x = 3 and y = 17
                           Serial        execution: x = ??, y = ??
                           if  both transactions start at the same time, with snapshot
                              isolation: x = ?? , y = ??
             Called skew write
             Skew also occurs with inserts
                    z   E.g.,:
                           Find         max order number among all orders
                           Create         a new order with order number = previous max + 1
Database System Concepts - 6th Edition                      15.55                ©Silberschatz, Korth and Sudarshan
                            Snapshot Isolation Anomalies
             SI breaks serializability when txns modify different items, each based
                  on a previous state of the item the other modified
                    z   Not very common in practice
                           E.g.,        the TPC-C benchmark runs correctly under SI
                           when    txns conflict due to modifying different data, there is
                              usually also a shared item they both modify too (like a total
                              quantity) so SI will abort one of them
                    z   But does occur
                           Application        developers should be careful about write skew
             SI can also cause a read-only transaction anomaly, where read-only
                  transaction may see an inconsistent state even if updaters are
                  serializable
                    z   We omit details
Database System Concepts - 6th Edition                      15.56                 ©Silberschatz, Korth and Sudarshan
                               SI In Oracle and PostgreSQL
         Warning: SI used when isolation level is set to serializable, by Oracle and
             PostgreSQL
               z     PostgreSQL’s implementation of SI described in Section 26.4.1.3
               z     Oracle implements “first updater wins” rule (variant of “first committer
                     wins”)
                          concurrent writer check is done at time of write, not at commit time
                          Allows transactions to be rolled back earlier
               z     Neither supports true serializable execution
         Can sidestep for specific queries by using select .. for update in Oracle
             and PostgreSQL
               z     Locks the data which is read, preventing concurrent updates
               z     E.g.,
                      1.   select max(orderno) from orders for update
                      2.   read value into local variable maxorder
                      3.   insert into orders (maxorder+1, …)
Database System Concepts - 6th Edition                15.57                   ©Silberschatz, Korth and Sudarshan
                               Insert and Delete Operations
             If two-phase locking is used :
                    z   A delete operation may be performed only if the transaction
                        deleting the tuple has an exclusive lock on the tuple to be deleted.
                    z   A transaction that inserts a new tuple into the database is given an
                        X-mode lock on the tuple
             Insertions and deletions can lead to the phantom phenomenon.
                    z   A transaction that scans a relation
                           (e.g.,       find sum of balances of all accounts in Perryridge)
                          and a transaction that inserts a tuple in the relation
                           (e.g.,       insert a new account at Perryridge)
                              (conceptually) conflict in spite of not accessing any tuple in
                              common.
                    z   If only tuple locks are used, non-serializable schedules can result
                           E.g.,  the scan transaction does not see the new account, but
                              reads some other tuple written by the update transaction
Database System Concepts - 6th Edition                      15.58                   ©Silberschatz, Korth and Sudarshan
                   Insert and Delete Operations (Cont.)
              The transaction scanning the relation is reading information that
                  indicates what tuples the relation contains, while a transaction
                  inserting a tuple updates the same information.
                    z    The information should be locked.
              One solution:
                    z   Associate a data item with the relation, to represent the
                        information about what tuples the relation contains.
                    z   Transactions scanning the relation acquire a shared lock in the
                        data item.
                    z   Transactions inserting or deleting a tuple acquire an exclusive
                        lock on the data item. (Note: locks on the data item do not conflict
                        with locks on individual tuples.)
              Above protocol provides very low concurrency for insertions/
                  deletions.
              Index locking protocols provide higher concurrency while preventing
                  the phantom phenomenon, by requiring locks
                  on certain index buckets.
Database System Concepts - 6th Edition               15.59                   ©Silberschatz, Korth and Sudarshan
                               Weak Levels of Consistency
              Degree-two consistency: differs from two-phase locking in that S-locks
                  may be released at any time, and locks may be acquired at any time
                    z   X-locks must be held till end of transaction
                    z   Serializability is not guaranteed, programmer must ensure that no
                        erroneous database state will occur]
              Cursor stability:
                    z   For reads, each tuple is locked, read, and lock is immediately
                        released
                    z   X-locks are held till end of transaction
                    z   Special case of degree-two consistency
Database System Concepts - 6th Edition                15.60                 ©Silberschatz, Korth and Sudarshan
                    Weak Levels of Consistency in SQL
              SQL allows non-serializable executions
                    z   Serializable: is the default
                    z   Repeatable read: allows only committed records to be read, and
                        repeating a read should return the same value (so read locks should
                        be retained)
                           However,      the phantom phenomenon need not be prevented
                                – T1 may see some records inserted by T2, but may not see
                                  others inserted by T2
                    z   Read committed: same as degree two consistency, but most
                        systems implement it as cursor-stability
                   Read uncommitted: allows even uncommitted data to be read
                    z
              In many database systems, read committed is the default consistency
               level
                    z   has to be explicitly changed to serializable when required
                           set     isolation level serializable
Database System Concepts - 6th Edition                   15.61              ©Silberschatz, Korth and Sudarshan
                                         Index Locking Protocol
             Index locking protocol:
                    z   Every relation must have at least one index.
                    z   A transaction can access tuples only after finding them through
                        one or more indices on the relation
                    z   A transaction Ti that performs a lookup must lock all the index
                        leaf nodes that it accesses, in S-mode
                           Even    if the leaf node does not contain any tuple satisfying
                              the index lookup (e.g., for a range query, no tuple in a leaf is
                              in the range)
                    z   A transaction Ti that inserts, updates or deletes a tuple ti in a
                        relation r
                           must         update all indices to r
                           must    obtain exclusive locks on all index leaf nodes affected
                              by the insert/update/delete
                    z   The rules of the two-phase locking protocol must be observed
             Guarantees that phantom phenomenon won’t occur
Database System Concepts - 6th Edition                        15.62              ©Silberschatz, Korth and Sudarshan
                        Concurrency in Index Structures
              Indices are unlike other database items in that their only job is to help in
                  accessing data.
              Index-structures are typically accessed very often, much more than
                  other database items.
                    z   Treating index-structures like other database items, e.g., by 2-phase
                        locking of index nodes can lead to low concurrency.
              There are several index concurrency protocols where locks on internal
                  nodes are released early, and not in a two-phase fashion.
                    z   It is acceptable to have nonserializable concurrent access to an
                        index as long as the accuracy of the index is maintained.
                           In  particular, the exact values read in an internal node of a
                              B+-tree are irrelevant so long as we land up in the correct leaf
                              node.
Database System Concepts - 6th Edition                  15.63                   ©Silberschatz, Korth and Sudarshan
             Concurrency in Index Structures (Cont.)
                 Example of index concurrency protocol:
                 Use crabbing instead of two-phase locking on the nodes of the B+-tree, as
                  follows. During search/insertion/deletion:
                    z   First lock the root node in shared mode.
                    z   After locking all required children of a node in shared mode, release the lock
                        on the node.
                    z   During insertion/deletion, upgrade leaf node locks to exclusive mode.
                    z   When splitting or coalescing requires changes to a parent, lock the parent in
                        exclusive mode.
                 Above protocol can cause excessive deadlocks
                    z   Searches coming down the tree deadlock with updates going up the tree
                    z   Can abort and restart search, without affecting transaction
                  Better protocols are available; see Section 16.9 for one such protocol, the B-link
                  tree protocol
                    z   Intuition: release lock on parent before acquiring lock on child
                             And deal with changes that may have happened between lock release
                              and acquire
Database System Concepts - 6th Edition                   15.64                      ©Silberschatz, Korth and Sudarshan
                                         Next-Key Locking
             Index-locking protocol to prevent phantoms required locking entire leaf
                    z   Can result in poor concurrency if there are many inserts
             Alternative: for an index lookup
                    z   Lock all values that satisfy index lookup (match lookup value, or
                        fall in lookup range)
                    z   Also lock next key value in index
                    z   Lock mode: S for lookups, X for insert/delete/update
             Ensures that range queries will conflict with inserts/deletes/updates
                    z   Regardless of which happens first, as long as both are concurrent
Database System Concepts - 6th Edition               15.65                  ©Silberschatz, Korth and Sudarshan
              End of Chapter
Database System Concepts - 6th Edition        15.67                ©Silberschatz, Korth and Sudarshan
                   Snapshot Write: First Committer Wins
                        z    Variant: “First-updater-wins”
                                 Check for concurrent updates when write occurs
                                 (Oracle uses this plus some extra features)
                                 Differs only in when abort occurs, otherwise equivalent
Database System Concepts - 6th Edition                     15.68                     ©Silberschatz, Korth and Sudarshan
                                         Figure 15.01
Database System Concepts - 6th Edition       15.69      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.04
Database System Concepts - 6th Edition       15.70      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.07
Database System Concepts - 6th Edition       15.71      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.08
Database System Concepts - 6th Edition       15.72      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.09
Database System Concepts - 6th Edition       15.73      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.10
Database System Concepts - 6th Edition       15.74      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.11
Database System Concepts - 6th Edition       15.75      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.12
Database System Concepts - 6th Edition       15.76      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.13
Database System Concepts - 6th Edition       15.77      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.14
Database System Concepts - 6th Edition       15.78      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.15
Database System Concepts - 6th Edition       15.79      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.16
Database System Concepts - 6th Edition       15.80      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.17
Database System Concepts - 6th Edition       15.81      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.18
Database System Concepts - 6th Edition       15.82      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.19
Database System Concepts - 6th Edition       15.83      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.20
Database System Concepts - 6th Edition       15.84      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.21
Database System Concepts - 6th Edition       15.85      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.22
Database System Concepts - 6th Edition       15.86      ©Silberschatz, Korth and Sudarshan
                                         Figure 15.23
Database System Concepts - 6th Edition       15.87      ©Silberschatz, Korth and Sudarshan
                                         Figure in-15.1
Database System Concepts - 6th Edition 15.88 ©Silberschatz, Korth and Sudarshan