Sub topic 7
View serializability and how it relates to conflict serializability
This chapter assumes you can build a precedence graph and test a schedule for conflict serializability. You should also know that a schedule is an interleaving of the read and write operations of several transactions. Notation: R1(A) is a read of item A by T1, and W2(A) is a write of A by T2.
Why a weaker test is needed
Conflict serializability is a sufficient test. If the precedence graph has no cycle, the schedule is safe. A cycle does not always mean the schedule is wrong, though. Some schedules fail the conflict test but still produce exactly the same result as a serial schedule. View serializability is a more permissive definition that accepts those schedules.
The cost is that the view test is much harder to run.
View equivalence
Two schedules S and S' over the same set of transactions are view equivalent if all three conditions hold.
- Initial reads. If a transaction in S reads the initial value of item X, the same transaction reads the initial value of X in S'.
- Read-from. If Tj reads a value of X written by Ti in S, then Tj reads the value of X written by Ti in S'.
- Final writes. If Ti performs the last write of X in S, then Ti performs the last write of X in S'.
A schedule is view serializable if it is view equivalent to some serial schedule.
The three conditions guarantee that every transaction sees the same values and that the database ends in the same state. Reads decide what each transaction computes, and the final writes decide the stored result. The test never asks about the order of two writes that nobody reads and that are not final.
A shorthand for reasoning
Add two imaginary transactions. Tb writes every item before the schedule starts. Tf reads every item after the schedule ends. Conditions 1 and 3 then become special cases of read-from. An initial read reads from Tb, and a final write is the one that Tf reads from. Many textbooks use this trick, and it is handy when you list "reads-from" pairs on paper.
Worked example
Take three transactions. T1 reads A and then writes A. T2 and T3 each write A without reading it first.
S: R1(A) W2(A) W1(A) W3(A)Is S conflict serializable? The conflicting pairs give these edges:
R1(A)beforeW2(A)gives T1 → T2.W2(A)beforeW1(A)gives T2 → T1.- Other pairs give T1 → T3 and T2 → T3.
T1 → T2 and T2 → T1 form a cycle, so S is not conflict serializable.
Is S view serializable? Try the serial order T1, T2, T3:
S': R1(A) W1(A) W2(A) W3(A)| Condition | In S | In S' | Match |
|---|---|---|---|
| Initial read of A | T1 reads the initial value | T1 reads the initial value | yes |
| Read-from pairs | none besides the initial read | none besides the initial read | yes |
| Final write of A | T3 | T3 | yes |
S is view equivalent to the serial schedule T1, T2, T3. It is view serializable and not conflict serializable.
The reason is that W2(A) and W1(A) are both overwritten by W3(A) before anyone reads them. The T2 → T1 edge records a conflict, but the order of those two writes cannot change any value that a transaction reads or that remains in the database.
The relationship between the two classes
Every conflict serializable schedule is view serializable. The reverse is false, as the example shows.
The containment is strict at each step:
- A serial schedule is trivially conflict serializable.
- A conflict serializable schedule is view serializable, because a conflict-equivalent serial schedule preserves every reads-from pair and every final write.
- Some view serializable schedules are not conflict serializable. Our schedule S is one of them.
- Some schedules are not view serializable at all, such as
R1(A) W2(A) W1(A).
Blind writes
A blind write is a write to an item by a transaction that has not read that item. In S, T2 and T3 both perform blind writes.
Rule: if a schedule is view serializable but not conflict serializable, it contains at least one blind write. Turning it around: if a schedule has no blind writes, view serializable and conflict serializable coincide. A schedule with no blind writes and a precedence graph cycle is not view serializable.
This rule is a major shortcut in GATE questions.
Testing view serializability
There is no simple graph test comparable to the precedence graph. Deciding view serializability is NP-complete. The direct method tries every serial order of the n transactions, which means n! candidates, and checks the three conditions for each.
A practical procedure by hand:
- Build the precedence graph. If it is acyclic, the schedule is conflict serializable, so it is also view serializable. Stop.
- If there is a cycle, look for blind writes. If there are none, the schedule is not view serializable. Stop.
- Otherwise, list the initial reads, the reads-from pairs and the final writer of each item. Use them to prune serial orders. A transaction that reads the initial value must come before every transaction that writes that item, unless that writer's write is overwritten before the read. A transaction that reads from Ti must come after Ti. The final writer of an item must come last among that item's writers.
- Check any remaining candidate serial order against the three conditions.
A brute-force checker
The code below follows the definition directly. It records, for every read, which transaction supplied the value ('INIT' for the initial value), and it records the last writer of every item. It then compares this information with every serial order.
from itertools import permutations
def view_info(schedule):
# schedule: list of (txn, op, item), op is 'R' or 'W'
last_writer = {}
reads = []
for t, op, x in schedule:
if op == 'R':
reads.append((t, x, last_writer.get(x, 'INIT')))
else:
last_writer[x] = t
return sorted(reads), last_writer
def view_serializable(schedule):
target = view_info(schedule)
txns = sorted({t for t, _, _ in schedule})
for order in permutations(txns):
serial = [step for t in order for step in schedule if step[0] == t]
if view_info(serial) == target:
return True, list(order)
return False, []
s = [('T1', 'R', 'A'), ('T2', 'W', 'A'), ('T1', 'W', 'A'), ('T3', 'W', 'A')]
print(view_serializable(s)) # (True, ['T1', 'T2', 'T3'])
s2 = [('T1', 'R', 'A'), ('T2', 'W', 'A'), ('T1', 'W', 'A')]
print(view_serializable(s2)) # (False, [])The first schedule is view serializable, even though the conflict_serializable function from the previous chapter reports it as not conflict serializable. The second schedule has no blind write for T1, and it fails both tests.
The permutations loop is the n! cost. This is fine for exam-size inputs and unusable for a database engine.
Edge cases and common errors
- Comparing only the final state. Two schedules can leave the database in the same state and still not be view equivalent. For example, they may differ in which value a transaction read. View equivalence requires all three conditions.
- Forgetting that a read can come after another transaction's write. Reads-from is about the most recent preceding write to that item, not about any earlier write by anybody.
- Treating a cycle as the end of the analysis. A cycle only rules out conflict serializability. Check for blind writes before concluding.
- Missing a transaction that never writes. A read-only transaction has no effect on final writes, but its reads still need to see the same sources.
- Several reads of one item in a transaction. Each read has its own source. The checker above keeps them as separate entries.
- Ignoring aborts. View serializability concerns the committed transactions only. It says nothing about recoverability, which is a separate property covered later.
How this connects to later topics
Timestamp ordering with the Thomas write rule accepts some schedules that basic timestamp ordering rejects. It does so by ignoring an obsolete write, which is a write that a later write would overwrite before anyone reads it. The accepted schedules are view serializable but not always conflict serializable. This is the same blind-write situation as in schedule S.
Exercise
Schedule S = R1(A) W2(A) W1(A) W3(A). Is S view equivalent to the serial schedule T1, T3, T2? Is it view equivalent to any other serial order?
Show answer
Not to T1, T3, T2. In S the final write of A is by T3, but in T1 T3 T2 the last writer is T2, so condition 3 fails. T1 reads the initial value in both, so condition 1 holds. Other serial orders: T2 T1 T3 makes T1 read from T2, so it fails. T2 T3 T1 and T3 T1 T2 and T3 T2 T1 end with T1 or T2 as final writer, so they fail. T3 first would also have T1 read from T3, so it fails. Only T1, T2, T3 matches.
Exercise
A schedule has a cycle in its precedence graph, and no transaction writes an item it has not first read. Is the schedule view serializable? Give the reason.
Show answer
No. A schedule that is view serializable but not conflict serializable must contain a blind write. With no blind writes, the two classes coincide, so the cycle shows that the schedule is not conflict serializable and therefore not view serializable.
Exercise
Give a three-line argument for why every conflict serializable schedule is view serializable.
Show answer
A conflict serializable schedule is conflict equivalent to a serial schedule, which means the order of every conflicting pair is the same in both. Each reads-from pair and each initial read is determined by the order of a write-read pair or a read-write pair on the same item, so both are preserved. Each final write is determined by the order of write-write pairs, so it is preserved too. Hence all three view conditions hold.