Sub topic 5
Conflicting Operations and Conflict Equivalence
This chapter assumes you know what a transaction is, what a schedule is, and the difference between serial and concurrent schedules. It also assumes basic SQL and basic graph terms. Here you learn which pairs of operations in a schedule matter, and how those pairs define when two schedules are equivalent. The next chapter uses this to test conflict serializability with a precedence graph.
Why only some pairs of operations matter
A schedule interleaves the reads and writes of several transactions. If you swap two adjacent operations, the final database state and what each transaction reads may or may not change. Two operations that can be swapped without changing anything are harmless. Two operations that cannot be swapped are the ones that fix the outcome.
We write R_i(X) for a read of item X by transaction Ti, and W_i(X) for a write of X by Ti.
The definition of a conflict
Two operations in a schedule conflict if all three conditions hold:
- They belong to different transactions.
- They access the same data item.
- At least one of them is a write.
If any condition fails, the operations do not conflict, and swapping them is safe.
| First operation | Second operation | Conflict? | Name |
|---|---|---|---|
| `R_i(X)` | `R_j(X)` | No | Two reads never change each other's result |
| `R_i(X)` | `W_j(X)` | Yes | Read-write (RW) |
| `W_i(X)` | `R_j(X)` | Yes | Write-read (WR) |
| `W_i(X)` | `W_j(X)` | Yes | Write-write (WW) |
| Any | Any on a different item | No | Different items are independent |
| Any | Any in the same transaction | No | Order inside one transaction is fixed by the program |
The three conflict types are the source of the anomalies from the earlier chapter:
- WR conflict:
Tjreads whatTiwrote. IfTihas not committed, this is a dirty read. - RW conflict:
Tjoverwrites whatTialready read. IfTireadsXagain, it sees a different value, which is an unrepeatable read. - WW conflict: two transactions write the same item. A blind overwrite can produce a lost update.
Why the order of a conflicting pair matters
Take X = 10. Let T1 do X = X + 5 and T2 do X = X * 2. If W1(X) comes before W2(X), the final value reflects T2's write. Reversed, it reflects T1's. The order of the pair decides the final state. For a RW or WR pair, the order decides which value the reader sees.
Conflict equivalence
Two schedules S and S' are conflict equivalent if:
- They contain the same transactions with the same operations, and
- Every pair of conflicting operations appears in the same order in both schedules.
Only the order of conflicting pairs must match. Non-conflicting operations may appear in any relative order.
This definition gives an operational method. You can turn S into S' by repeatedly swapping adjacent non-conflicting operations from different transactions. If you reach S', the schedules are conflict equivalent. Each swap keeps every conflicting pair in its original order, because only non-conflicting pairs are swapped.
Worked example
Consider schedule S:
R1(A) W1(A) R2(A) W2(A) R1(B) W1(B) R2(B) W2(B)List the conflicting pairs, ordered by their position in S. They involve A or B and cross transactions:
- On
A:W1(A)beforeR2(A),W1(A)beforeW2(A),R1(A)beforeW2(A). - On
B:W1(B)beforeR2(B),W1(B)beforeW2(B),R1(B)beforeW2(B).
In every pair, T1's operation comes first. Now compare with the serial schedule T1 then T2:
R1(A) W1(A) R1(B) W1(B) R2(A) W2(A) R2(B) W2(B)In this serial schedule, T1's operations also precede T2's in all the same pairs. So S is conflict equivalent to the serial schedule T1, T2.
You can also get there by swapping. In S, move R1(B) left past W2(A) and R2(A). These are on different items and different transactions, so they do not conflict. Then move W1(B) the same way. The result is the serial schedule above.
A pair that cannot be moved
Now take schedule S2:
R1(A) W2(A) W1(A)The pair R1(A), W2(A) conflicts, and T1 is first. The pair W2(A), W1(A) conflicts, and T2 is first. A serial schedule T1, T2 would put W1(A) before W2(A), which reverses the second pair. A serial schedule T2, T1 would put W2(A) before R1(A), which reverses the first pair. Neither serial schedule is conflict equivalent to S2. This is the situation the next chapter detects with a cycle in the precedence graph.
Checking conflicts in code
The code below lists the conflicting pairs of a schedule, in the order they occur. Comparing two such lists tests conflict equivalence.
def conflicting_pairs(schedule):
# schedule: list of (txn, op, item), op is 'R' or 'W'
pairs = []
for i, (ti, oi, xi) in enumerate(schedule):
for j in range(i + 1, len(schedule)):
tj, oj, xj = schedule[j]
if ti != tj and xi == xj and 'W' in (oi, oj):
pairs.append(((ti, oi, xi), (tj, oj, xj)))
return pairs
def conflict_equivalent(s1, s2):
# Same operations, and conflicting pairs in the same order.
if sorted(s1) != sorted(s2):
return False
return sorted(conflicting_pairs(s1)) == sorted(conflicting_pairs(s2))
S = [('T1', 'R', 'A'), ('T1', 'W', 'A'), ('T2', 'R', 'A'), ('T2', 'W', 'A'),
('T1', 'R', 'B'), ('T1', 'W', 'B'), ('T2', 'R', 'B'), ('T2', 'W', 'B')]
serial = [('T1', 'R', 'A'), ('T1', 'W', 'A'), ('T1', 'R', 'B'), ('T1', 'W', 'B'),
('T2', 'R', 'A'), ('T2', 'W', 'A'), ('T2', 'R', 'B'), ('T2', 'W', 'B')]
print(conflict_equivalent(S, serial)) # TrueThe check sorted(s1) != sorted(s2) assumes each transaction does not repeat an identical operation on the same item. If a transaction reads the same item twice, label the operations (for example R1(A)#1) to tell them apart.
Common errors
- Counting read-read as a conflict. Two reads of the same item never conflict.
- Ignoring the transaction condition.
R1(A)andW1(A)are in the same transaction. They are ordered by the program, so they are not a conflicting pair. - Checking only adjacent operations. Conflicts apply to any two operations in the schedule, even if other operations sit between them. The pair
W1(A)andW2(A)conflicts even whenR3(B)lies between them. - Confusing conflict equivalence with equal results. Two schedules can leave the same final state by chance while having a different order of conflicting pairs. They are not conflict equivalent. Conflict equivalence is a stricter, sufficient condition for equal outcomes, based only on operation order.
- Forgetting commit and abort. Conflict equivalence looks only at reads and writes. Commit and abort order matters for recoverability, which is a separate chapter.
Relation to serial schedules
A schedule is conflict serializable if it is conflict equivalent to some serial schedule. This chapter gave you the "conflict equivalent" part. A serial schedule has no interleaving, so all of Ti's operations come before all of Tj's, for each ordered pair of transactions. For a serial order to match a schedule, every conflicting pair between Ti and Tj in the schedule must run in the direction the serial order gives.
The diagram shows how two operations are classified.
Counting conflicting pairs
For a quick GATE check, list conflicts item by item. For each item, scan the operations on that item in schedule order. For every write, pair it with every earlier operation from another transaction and every later operation from another transaction. Do this per item and you avoid cross-item mistakes.
Example schedule: R1(X) R2(X) W1(X) W2(Y) R1(Y).
- Item
X: operations areR1,R2,W1. Conflicting pairs:R2(X)thenW1(X)(RW, T2 before T1).R1(X)andW1(X)are the same transaction, andR1(X),R2(X)are both reads. - Item
Y: operations areW2,R1. Conflicting pair:W2(Y)thenR1(Y)(WR, T2 before T1).
So there are two conflicting pairs, and both put T2 before T1. Only the direction T2 → T1 appears, which matters in the next chapter.
Exercise
Schedule S is R1(A) R2(A) W1(A) R2(B) W2(B) R1(B). List every conflicting pair in order, and say whether S is conflict equivalent to the serial schedule T1 then T2.
Show answer
Item A has R1, R2, W1. The conflicting pair is R2(A) before W1(A) (RW, T2 before T1). R1(A) and W1(A) share a transaction, and R1(A), R2(A) are both reads. Item B has R2, W2, R1. The conflicting pair is W2(B) before R1(B) (WR, T2 before T1). R2(B) and W2(B) share a transaction, and R2(B), R1(B) are both reads. Both pairs put T2 before T1. In serial T1 then T2, T1's operations would all come first, reversing both pairs. So S is not conflict equivalent to T1, T2. It is conflict equivalent to T2 then T1.
Exercise
Are R1(X) and W2(Y) a conflicting pair? Are W1(X) and W1(Y)? Are W1(X) and R2(X)?
Show answer
R1(X) and W2(Y) do not conflict, because they access different items. W1(X) and W1(Y) do not conflict, because they belong to the same transaction and access different items. W1(X) and R2(X) conflict, because they are from different transactions, access the same item, and one is a write (a WR conflict if W1 comes first).