transaction
SERIALIZABLE
Successful serializable transactions have an outcome equivalent to a serial ordering. Retry the entire transaction on SQLSTATE 40001, with bounded attempts and safe external side effects. Target: PostgreSQL 17. Example is a connection-specific schedule, not a single executable script.
Formal Syntax & Structure
sql syntaxsql
BEGIN ISOLATION LEVEL SERIALIZABLEWorking 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 ISOLATION LEVEL SERIALIZABLE;-- A: SELECT SUM(n) FROM t;-- B: BEGIN ISOLATION LEVEL SERIALIZABLE;-- B: SELECT SUM(n) FROM t;-- A: UPDATE t SET n=0 WHERE id=1;-- B: UPDATE t SET n=0 WHERE id=2;-- A: COMMIT;-- B: COMMIT;-- Roll back any open transactions after observation.