Sub topic 6
Testing Conflict Serializability with a Precedence Graph
Prerequisites and the core idea
Assume you know basic database schedules, transactions, and directed graphs. A schedule is an order of operations from two or more transactions. The operations may be interleaved.
A schedule is conflict serializable if it has the same conflict ordering as some serial schedule. A serial schedule runs one complete transaction before starting the next. Conflict serializability lets a database run transactions concurrently while preserving the important ordering of conflicting operations.
Finding conflicts
Two operations conflict when all three conditions hold:
- They belong to different transactions.
- They use the same data item.
- At least one operation writes that item.
The order in the schedule determines the direction of the graph edge.
| Earlier operation | Later operation | Conflict? |
|---|---|---|
| `R1(A)` | `W2(A)` | Yes, read-write |
| `W1(A)` | `R2(A)` | Yes, write-read |
| `W1(A)` | `W2(A)` | Yes, write-write |
| `R1(A)` | `R2(A)` | No, read-read |
For every conflicting pair, add an edge from the earlier operation's transaction to the later operation's transaction. This graph is called the precedence graph, or serialization graph.
The decision rule
- If the precedence graph is acyclic, the schedule is conflict serializable.
- If the graph contains a directed cycle, the schedule is not conflict serializable.
When the graph is acyclic, a topological order gives an equivalent serial order. A topological order is an ordering in which every edge points from an earlier transaction to a later transaction.
Worked example
Consider this schedule:
R1(A), W2(A), W1(B), R2(B)
The first two operations conflict on A. Since R1(A) occurs first, add T1 -> T2.
The last two operations conflict on B. Since W1(B) occurs first, add another edge T1 -> T2.
The graph has no cycle. Therefore, the schedule is conflict serializable, and its equivalent serial order is:
T1, T2
Now consider:
R1(A), W2(A), W1(A)
R1(A) before W2(A) gives T1 -> T2. W2(A) before W1(A) gives T2 -> T1.
These edges form a cycle, so no serial order can preserve all conflicts.
Automating the test
The following function builds the graph and uses topological sorting. It returns the serial order when one exists.
def conflict_serializable(schedule):
edges = set()
nodes = {txn for txn, _, _ in schedule}
for i, (ti, oi, item_i) in enumerate(schedule):
for tj, oj, item_j in schedule[i + 1:]:
if ti != tj and item_i == item_j and (oi == "W" or oj == "W"):
edges.add((ti, tj))
indegree = {txn: 0 for txn in nodes}
for _, destination in edges:
indegree[destination] += 1
ready = [txn for txn in nodes if indegree[txn] == 0]
order = []
while ready:
txn = ready.pop()
order.append(txn)
for source, destination in edges:
if source == txn:
indegree[destination] -= 1
if indegree[destination] == 0:
ready.append(destination)
return len(order) == len(nodes), order
schedule = [
("T1", "R", "A"),
("T2", "W", "A"),
("T1", "W", "B"),
("T2", "R", "B"),
]
print(conflict_serializable(schedule))A result beginning with True means the schedule is conflict serializable. The second value is one possible equivalent serial order.
Exercise
The schedule is R1(A), W2(A), W2(B), R1(B). Is it conflict serializable, and why?
Show answer
No. R1(A) before W2(A) gives T1 -> T2, while W2(B) before R1(B) gives T2 -> T1. The graph contains a cycle.