Sub topic 12
Timestamp Ordering and the Thomas Write Rule
This chapter assumes you know transactions, schedules, read-write conflicts, conflict serializability, and basic directed graphs. You should also recognize simple SELECT and UPDATE statements.
The timestamp-ordering idea
Timestamp ordering assigns each transaction a unique timestamp when it starts. A smaller timestamp means an older transaction. The database then accepts operations only if they preserve this timestamp order.
For every data item X, the system stores:
read_TS(X): the largest timestamp of a transaction that has successfully readX.write_TS(X): the largest timestamp of a transaction that has successfully writtenX.
Let TS(T) be the timestamp of transaction T.
Basic timestamp-ordering rules
For read(X) by transaction T:
- If
TS(T) < write_TS(X), reject and abortT. - Otherwise, allow the read and set
read_TS(X)to the larger of its old value andTS(T).
The first case means a newer transaction has already written X. Allowing the older transaction to read that value would place its read after a newer write, violating timestamp order.
For write(X) by transaction T:
- If
TS(T) < read_TS(X), reject and abortT. - If
TS(T) < write_TS(X), reject and abortTunder basic timestamp ordering. - Otherwise, perform the write and set
write_TS(X) = TS(T).
The second test prevents an older write from appearing after a newer write.
The Thomas write rule
The Thomas write rule changes only the second write test. If:
TS(T) < write_TS(X)
then T’s write is obsolete because a newer transaction has already written X. Instead of aborting T, the system ignores that write.
The first test remains unchanged:
- If
TS(T) < read_TS(X), abortT. - Else if
TS(T) < write_TS(X), ignore the write. - Else perform the write.
Ignoring an obsolete write is safe because a later write already determines the current version of X. Thomas’s rule can therefore allow more schedules than basic timestamp ordering while preserving conflict serializability.
| Rule | Condition | Action |
|---|---|---|
| Basic read | `TS(T) < write_TS(X)` | Abort `T` |
| Basic write | `TS(T) < read_TS(X)` or `TS(T) < write_TS(X)` | Abort `T` |
| Thomas write | `TS(T) < read_TS(X)` | Abort `T` |
| Thomas write | `TS(T) < write_TS(X)` only | Ignore the write |
Worked example
Suppose TS(T1)=5, TS(T2)=10, and initially read_TS(X)=write_TS(X)=0.
If T2 writes X, then write_TS(X)=10. If T1 later tries to write X, its timestamp is less than write_TS(X).
- Basic timestamp ordering aborts
T1. - Thomas write rule ignores
T1’s write.
If T1 tries to read X instead, it aborts under both protocols because TS(T1) < write_TS(X).
def thomas_write(ts, read_ts, write_ts):
if ts < read_ts:
return "abort"
if ts < write_ts:
return "ignore obsolete write"
return "perform write"
print(thomas_write(5, 0, 10))
print(thomas_write(5, 8, 10))
print(thomas_write(12, 8, 10))The output is:
ignore obsolete write
abort
perform writeTimestamp ordering avoids deadlocks because transactions do not wait for locks. However, it may abort transactions repeatedly. Basic timestamp ordering also does not automatically guarantee strict schedules, so separate commit or recovery rules may be needed.
Exercise
T1 has timestamp 4. For item X, readTS(X)=3 and writeTS(X)=9. Under Thomas write rule, what happens when T1 writes X?
Show answer
The write is ignored because 4 < 9, but 4 is not less than read_TS(X)=3.