Skip to content

Checked writes ​

These PostgreSQL 18.6 READ COMMITTED Scenarios support the stock and stale-edit decisions. They assert particular single-row schedules, not an application UI or all possible executions.

Two buyers ​

Two buyers request four units each from a stock of five. B waits for A's UPDATE, then its condition matches no row after A commits. B rolls back; the quantity is 1. A deliberate failed-repair branch then removes the condition and decrements again, leaving -3.

A> BEGIN ISOLATION LEVEL READ COMMITTED;
BEGIN

B> BEGIN ISOLATION LEVEL READ COMMITTED;
BEGIN

A> UPDATE stock SET quantity = quantity - 4 WHERE id = 1 AND quantity >= 4;
UPDATE 1

B> UPDATE stock SET quantity = quantity - 4 WHERE id = 1 AND quantity >= 4;
⏳ B is waiting for a lock…

A> COMMIT;
COMMIT

⏵ B resumes:
UPDATE 0

B> ROLLBACK;
ROLLBACK

A> SELECT quantity FROM stock WHERE id = 1;
 quantity 
----------
        1 
(1 row)

Contrast an unconditional decrement after the checked rejection. This deliberately breaks the stock rule.

B> BEGIN ISOLATION LEVEL READ COMMITTED;
BEGIN

B> UPDATE stock SET quantity = quantity - 4 WHERE id = 1;
UPDATE 1

B> COMMIT;
COMMIT

A> SELECT quantity FROM stock WHERE id = 1;
 quantity 
----------
       -3 
(1 row)

Verified against PostgreSQL 18.6 · Run it yourself · Scenario source

An editing interval ​

Both editors load version 1 before their save transactions. A saves version 2. B's checked replacement affects zero rows, and a fresh read after rollback still shows A's edit. A deliberate failed-repair branch then uses the latest version with B's old draft, replacing A's text. The Scenario does not model a merge UI or an automatic retry policy.

A> SELECT body, version FROM documents WHERE id = 1;
   body   | version 
----------+---------
 Original |       1 
(1 row)

B> SELECT body, version FROM documents WHERE id = 1;
   body   | version 
----------+---------
 Original |       1 
(1 row)

A> BEGIN ISOLATION LEVEL READ COMMITTED;
BEGIN

A> UPDATE documents SET body = 'A edit', version = version + 1 WHERE id = 1 AND version = 1;
UPDATE 1

A> COMMIT;
COMMIT

B> BEGIN ISOLATION LEVEL READ COMMITTED;
BEGIN

B> UPDATE documents SET body = 'B edit', version = version + 1 WHERE id = 1 AND version = 1;
UPDATE 0

B> ROLLBACK;
ROLLBACK

B> SELECT body, version FROM documents WHERE id = 1;
  body  | version 
--------+---------
 A edit |       2 
(1 row)

Contrast blindly substituting the latest version while keeping B's old draft. This deliberately replaces A's edit.

B> BEGIN ISOLATION LEVEL READ COMMITTED;
BEGIN

B> UPDATE documents SET body = 'B edit', version = version + 1 WHERE id = 1 AND version = 2;
UPDATE 1

B> COMMIT;
COMMIT

A> SELECT body, version FROM documents WHERE id = 1;
  body  | version 
--------+---------
 B edit |       3 
(1 row)

Verified against PostgreSQL 18.6 · Run it yourself · Scenario source

Predict related results in two-writer practice. Use the protection guide for writer assumptions and application conflict responses.

MIT Licensed · Every transcript on this site was generated by a real database run against MySQL 8.4.11 and PostgreSQL 18.6 at 8487270; shared YAML has an independent psycopg/PyMySQL check.