transactions

Groups multiple SQL statements into an atomic unit. All statements commit together or all roll back on error.

Syntax

postgresql
BEGIN;
-- statements --
COMMIT; -- or ROLLBACK;

Example

postgresql
BEGIN;

UPDATE accounts
SET balance = balance - 100
WHERE id = $1;

UPDATE accounts
SET balance = balance + 100
WHERE id = $2;

-- Check for errors then:
COMMIT;
-- Or on error: ROLLBACK;