skip to content

timestamp ordering and OCC

concurrency control without locks: order by timestamp, or validate at the end and abort.

address
everything.locking.timestamp_ordering_and_occ
part
part 15, locks and concurrency control
chapter
chapter 9 of 10
reading time
10 min

two ways to get serializability without making anyone wait for a lock. timestamp ordering fixes the serial order up front: every transaction gets a timestamp at start, every item remembers the newest timestamp that read it and wrote it, and an operation that arrives too late for its timestamp aborts. optimistic concurrency control lets everyone run on private copies and checks at the end, in a validation phase, whether anything they read changed underneath them. both trade waiting for aborts.

the picture

you know how two-phase locking makes conflicting transactions wait, and then needs a deadlock detector for when the waiting goes in circles? the alternative is to never wait at all. if two transactions conflict in a way that could break serializability, abort one and restart it.

think of a deli counter with numbered tickets. timestamp ordering is "serve strictly in ticket order": if number 12 tries to grab the last loaf after number 15 already touched it, number 12 is sent back to take a new ticket. optimistic control is self-checkout: everyone fills their basket freely, and at the till you check nothing in your basket was changed by someone who paid in the meantime. if it was, put it all back and start again.

timestamp ordering

each transaction T gets a timestamp ts(T) when it starts, and the rule is that the result must equal the serial order by timestamp. each item x keeps two numbers: RTS(x), the largest timestamp that read it, and WTS(x), the largest that wrote it.

  • read x by T: if ts(T) < WTS(x), a younger transaction already overwrote the value T should see. abort T. otherwise read, and raise RTS(x) to ts(T).
  • write x by T: if ts(T) < RTS(x), a younger transaction already read x and should have seen this write. abort T. if ts(T) < WTS(x), a younger write is already there. abort T. otherwise write, and set WTS(x) to ts(T).
schedule =
rule =
  1. 1T1R1(A)
  2. 2T2R2(A)
  3. 3T2W2(A)
  4. 4T1W1(A)
  5. timestamps: T1 = 1, T2 = 2. smaller is older, and goes first in the serial order.
itemRTSWTS
A00

each item starts with RTS = WTS = 0. step through the schedule.

step 1/5
drawn by hand · the textbook model of basic timestamp ordering
pick a schedule and step through. RTS and WTS change in hot. an operation that arrives too late aborts its transaction; with the Thomas write rule on, an obsolete write is skipped instead. try "blind writes" with the rule off, then on.

every precedence-graph edge then goes from an older transaction to a younger one, because any operation that would make an edge point backwards is refused. timestamps only go up, so no cycles. it's the same argument as 2PL with timestamps in place of lock points, but decided at start instead of discovered during the run.

the Thomas write rule

in "blind writes", T2 writes A, then T1 (older) writes A. in timestamp order T1's write comes first and T2's overwrites it, so T1's value would never be seen by anyone, provided nobody younger read A in between (that case is caught by the RTS check). Thomas's observation (1979): just skip the obsolete write instead of aborting. the result is still view serializable, though no longer conflict serializable: the schedule's ww pair is in the "wrong" order, it just doesn't matter.

quick check
T1 (ts 1) and T2 (ts 2). schedule: R2(A), W1(A). what does basic timestamp ordering do with W1(A)?

the weak spots: long transactions are old by the time they write, so they're the ones that keep losing. a reader has to write (RTS) to shared state on every read. and basic TO allows reading uncommitted data, so real implementations add commit bits or buffering. MVCC systems keep the idea but store several versions, so a late reader can be handed the version that matches its timestamp instead of being aborted. that's multiversion timestamp ordering, and a snapshot is a close cousin of "the versions as of my timestamp".

optimistic concurrency control

Kung and Robinson (1981) split each transaction into three phases:

  1. read: run the transaction against private copies. record the read set (what you read) and the write set (what you'd write). no locks.
  2. validate: at the end, check against every transaction that committed after you started. if any of them wrote something in your read set, you may have read a stale value: abort.
  3. write: if valid, install the write set. validation and write happen as one step, so nobody slips in between.
T1read {A}write {A}validate: nothing committed meanwhile, validT2read {A, B}validate: T1 wrote A while T2 read: abortT3read {C}write {C}validate: no overlap, validtime →
read phase: no locks, private writesvalid: the write phase installs the changesinvalid: abort, throw the copy away, restart
drawn by hand · the textbook model of optimistic concurrency control (Kung and Robinson, 1981)
three transactions under OCC. T1 commits A while T2 is still reading, and T2 read A, so T2's validation fails: it may have seen the old A. T3 started after T1 finished writing, so there's nothing to check and it commits.

OCC is cheap when conflicts are rare: no lock manager, no waiting, no deadlocks. when conflicts are common it's expensive: work is thrown away at the very end, after all of it was done, and the same transaction can lose again and again. in-memory databases like it (Hekaton in SQL Server uses an optimistic multiversion scheme), and so do most application caches.

predict the output
under OCC, which transaction pays for a conflict?
text
T1: read ..... validate ✓ write
T2:    read ............... validate ?

the app-level version: a version column

you've probably written OCC already. read a row with its version number, let the user think for a minute (no transaction open, no lock held), then write it back only if the version hasn't moved. the validate-and-write step is a single UPDATE, which postgres runs atomically:

sqlempty db
CREATE TABLE accounts (id int PRIMARY KEY, balance numeric(12,2) NOT NULL, version int NOT NULL);
INSERT INTO accounts VALUES (1, 100.00, 1);
SELECT balance, version FROM accounts WHERE id = 1;     -- the app reads version 1
UPDATE accounts SET balance = 130.00, version = version + 1
WHERE id = 1 AND version = 1;                           -- someone else saves first
UPDATE accounts SET balance = 110.00, version = version + 1
WHERE id = 1 AND version = 1;                           -- our save: validation fails
SELECT balance, version FROM accounts WHERE id = 1;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 1
#3 · SELECT 1
balancenumericversionint4
100.001
(1 row)
#4 · UPDATE 1
#5 · UPDATE 0
#6 · SELECT 1
balancenumericversionint4
130.002
(1 row)
someone else
·
us
·
balance
version
1
SELECT balance, version→ 100.00, version 1
100.001

we read the row and keep version 1 in hand. no transaction stays open and no lock is held while the user thinks. the other app read the same version (its save checks it too).

step 1/4
real output, postgres 18.6 (PGlite 0.5.12) · this page's version-column block, two saves in one session
the same block as two writers. both hold version 1. the first save matches and bumps the version; the second checks version = 1 against a row that says 2, matches nothing and gets UPDATE 0. step through and watch the version column.

UPDATE 1, then UPDATE 0. the second save matched no row because the version moved: that's a failed validation, and the app re-reads and retries (or shows the user the newer data). with two real sessions, the second UPDATE waits for the first one's row lock, then re-checks the WHERE on the new version and finds nothing (lost updates has that capture). so even the "lock-free" version relies on a row lock for the moment of validation.

one level deeperwhere postgres sits

postgres uses no textbook timestamp ordering and no pure OCC. writes take row locks held until commit (strict 2PL for writes). reads use snapshots, which is multiversioning: each transaction effectively reads "as of" its snapshot, like multiversion TO without the read timestamps. its SERIALIZABLE (SSI) is optimistic in spirit: nothing waits for a read, conflicts are recorded as they happen, and a transaction is aborted with 40001 when a dangerous pattern appears, sometimes not until COMMIT, which is exactly where OCC would validate.

keylines

  • timestamp ordering: serial order = start order. RTS and WTS per item, late operations abort.
  • the Thomas write rule skips an obsolete write instead of aborting.
  • OCC: read on private copies, validate at the end, then write. first committer wins.
  • both trade waiting for aborts: great when conflicts are rare, bad when they're common.
  • a version column checked in UPDATE ... WHERE is OCC in your app. UPDATE 0 means retry.