Professional Documents
Culture Documents
w11.2 Concurrency
w11.2 Concurrency
Slides adapted from “Database System Concepts” Seventh Edition, Miao Qiao
RECAP: Two-Phase Locking Protocol
Locks
e1
as
• Transaction may obtain locks e2
as
lock points
Ph
• Transaction may not release locks
2
RECAP: Variations of Two-Phase Locking
3
OUTLINE
Part 7: Transaction Management
• Chap17: Transactions
• Chap18: Concurrency
• Binary Locks
• Shared/Exclusive Locks
• Two-phase Locks
• Update Locks
• Increment Locks
• Locks with Multiple Granularity
• Locking scheduler
• Starvation and deadlock
• Chap 19: Recovery
5
Using Locks for Concurrency Control in Indexes
• Insertion: When new data item is inserted, it cannot be accessed until after the
operation is completed
• Deletion operation on the existing data item: write lock must be obtained before
deletion
Phantom revisited
• Phantom can occur when a new record being inserted satisfies a condition that a set
of records accessed by another transaction must satisfy
• The record that causes conflict is not recognized by concurrency control protocol
9
OUTLINE
12
Starvation and Deadlock
• Starvation: a transaction cannot proceed for an indefinite period of time while other
transactions continue normally.
13
Deadlock Detection
• Detect deadlock by timeout. If the system waits for a transaction T longer than a
predefined threshold, abort T and then restart T. Drawback: no perfect time limit.
• Detect deadlock with a wait-for graph. A set S of transactions has a deadlock if there
is a cycle in the wait-for graph of S.
• An edge from 𝑇! to 𝑇" if 𝑇! is waiting for a lock currently hold by 𝑇" .
14
Victim selection: Abort one transaction T in case of a deadlock and restart T .
Deadlock Detection
Wait-For Graph after Step (7) Wait-For Graph after Step (8) 15
Deadlock Detection
• The system is in a deadlock state if and only if the wait-for graph has a cycle.
• Invoke a deadlock-detection algorithm periodically to look for cycles.
T17 T17
T19 T19
17
Exercise
18
Deadlock & Starvation Prevention Protocols
Transaction Timestamp
• Assign, for each transaction 𝑇, a unique identifier 𝑇𝑆(𝑇) related to the starting time
of the transaction (a smaller 𝑇𝑆(𝑇) means an older transaction).
19
Deadlock & Starvation Prevention Protocols
20
Deadlock & Starvation Prevention Protocols
• In both schemes, a rolled back transactions is restarted with its original timestamp.
• Ensures that older transactions have precedence over newer ones, and starvation is thus
avoided.
• The aborted transaction will be resumed with the original timestamp, so young transactions
that are killed will eventually be old.
21
Deadlock & Starvation Prevention Protocols
3. Timeout-Based Schemes:
• 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.
• Ensures that deadlocks get resolved by timeout if they occur
• Simple to implement
• But may roll back transaction unnecessarily in absence of deadlock
• Difficult to determine good value of the timeout interval.
• Starvation is also possible
22
SUMMARY: Deadlock Prevention Protocols
• Deadlock is like a traffic jam in a database where transactions are stopped because they are
waiting for each other to move.
• To overcome this, we need to design transactions carefully, use smart strategies to detect
deadlocks, and have a plan to untangle deadlocks so as to avoid traffic jams.
23
SUMMARY
Part 7: Transaction Management
• Chap17: Transactions
• Chap18: Concurrency
• Binary Locks
• Shared/Exclusive Locks
• Two-phase Locks
• Update Locks
• Increment Locks
• Locks with Multiple Granularity
• Locking scheduler
• Starvation and deadlock
• Chap 19: Recovery
Source: https://helloworld957536278.wordpress.com/2021/09/06/understand-deadlock-in-
24
postgres-and-how-to-fix-it/