transaction

Lost update

A stale application read followed by an absolute overwrite can erase a concurrent change. Atomic relative updates or version checks avoid this particular pattern. Target: PostgreSQL 17. Example is a connection-specific schedule, not a single executable script.

Formal Syntax & Structure

sql syntaxsql
UPDATE t SET n = ...

Working Example

Example Snippetsql
-- Fresh fixture: CREATE TABLE t(id INT PRIMARY KEY, n INT NOT NULL);-- INSERT INTO t VALUES (1,10),(2,10);-- Use two independent connections A and B; setup committed first.-- A: BEGIN;-- A: SELECT n FROM t WHERE id=1;-- B: UPDATE t SET n=n+5 WHERE id=1;-- A: UPDATE t SET n=11 WHERE id=1;-- A: COMMIT;-- B: SELECT n FROM t WHERE id=1;-- Roll back any open transactions after observation.
SQL Reference