Transactions: BEGIN, COMMIT & ROLLBACK
What is a transaction?
A group of SQL statements that succeed or fail as one unit — all or nothing.
BEGIN;
UPDATE accounts SET balance = balance - 5000 WHERE id = 2; -- withdraw
UPDATE accounts SET balance = balance + 5000 WHERE id = 1; -- deposit
COMMIT;
If anything goes wrong between BEGIN and COMMIT, ROLLBACK undoes everything — no money vanishes.
Commands
BEGIN(orSTART TRANSACTION) – startCOMMIT– save all changes permanentlyROLLBACK– undo all changes since BEGINSAVEPOINT name– a checkpoint inside the transactionROLLBACK TO SAVEPOINT name– undo back to the checkpoint onlyRELEASE SAVEPOINT name– remove the checkpoint
Autocommit
Without BEGIN, each statement is its own transaction and commits immediately (autocommit mode — the default in psql, Supabase and most drivers).
Errors inside a transaction (PostgreSQL)
After any error, the transaction is aborted: every following statement fails with "current transaction is aborted" until you ROLLBACK (or roll back to a savepoint).
Transactions in your app
- Node
pg:await client.query('BEGIN')…COMMIT/ROLLBACKin try/catch - Supabase JS doesn't expose BEGIN/COMMIT over its REST API → put multi-step logic in a database function and call it with
rpc()(functions run inside a transaction automatically) - ORMs:
prisma.$transaction([...])
Best practices
- Keep transactions short — they hold locks and block other writers
- Never wait for user input inside a transaction
- Put every multi-step change that must stay consistent (transfers, orders + stock, signup + profile) in a transaction
Example
SELECT id, user_id, name, balance FROM accounts WHERE id IN (3, 6);
-- 3 | Ravi | Bank | 42000.00
-- 6 | Bikash | Cash | 2500.00
-- ✅ A successful transfer: Ravi sends Rs. 5,000 to Bikash
BEGIN;
UPDATE accounts SET balance = balance - 5000 WHERE id = 3;
UPDATE accounts SET balance = balance + 5000 WHERE id = 6;
COMMIT;
SELECT id, balance FROM accounts WHERE id IN (3, 6); -- 37000.00 | 7500.00
-- ❌ A failing transfer: Bikash tries to send Rs. 50,000 (CHECK balance >= 0 fails)
DO $$
BEGIN
UPDATE accounts SET balance = balance + 50000 WHERE id = 3; -- step 1 succeeds…
UPDATE accounts SET balance = balance - 50000 WHERE id = 6; -- step 2 violates the CHECK
EXCEPTION WHEN check_violation THEN
RAISE NOTICE 'Transfer failed, everything rolled back: %', SQLERRM;
END $$;
SELECT id, balance FROM accounts WHERE id IN (3, 6); -- still 37000.00 | 7500.00 ✅
-- ROLLBACK: try something, then undo it
BEGIN;
DELETE FROM expenses WHERE user_id = 1;
SELECT count(*) FROM expenses WHERE user_id = 1; -- 0
ROLLBACK;
SELECT count(*) FROM expenses WHERE user_id = 1; -- 10 again
-- SAVEPOINT: undo only part of a transaction
BEGIN;
INSERT INTO expenses (user_id, title, amount) VALUES (2, 'Keep me', 100);
SAVEPOINT before_risky;
INSERT INTO expenses (user_id, title, amount) VALUES (2, 'Oops', 999);
ROLLBACK TO SAVEPOINT before_risky; -- removes only 'Oops'
COMMIT;
SELECT title FROM expenses WHERE title IN ('Keep me', 'Oops'); -- Keep me