Transactions — Complete Guide
Transactions — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of PostgreSQL Tutorial on Toolliyo Academy.
On this page
PostgreSQL Tutorial · Lesson 33 of 100
Transactions
SQL → Advanced
SQL · 1 — Queries · ~6 min · PostgreSQL — Transactions & MVCC
What is this?
A transaction groups SQL into one atomic unit — COMMIT makes all changes permanent, ROLLBACK undoes them. BEGIN starts explicit transaction; autocommit wraps single statements by default.
Why should you care?
Swiggy payment plus order status update must both succeed or neither — PostgresVerse uses transactions so money is not captured without an order row.
See it live — copy this example
Run in pgAdmin or psql.
BEGIN;
UPDATE accounts SET balance = balance - 500 WHERE account_id = 10;
UPDATE accounts SET balance = balance + 500 WHERE account_id = 20;
INSERT INTO transfers (from_acct, to_acct, amount) VALUES (10, 20, 500);
COMMIT;
What happened?
- Debit, credit, and audit insert share one transaction.
- If INSERT fails, ROLLBACK reverses both balance updates — no partial transfer.
Practice next
- Create accounts and transfers tables with sample balances.
- Run BEGIN ... COMMIT successfully.
- Repeat with intentional error before COMMIT and ROLLBACK.
- Use START TRANSACTION READ ONLY for reporting snapshot.
- Wrap multi-table order insert in one transaction in psql script.
Remember
ACID atomicity = all or nothing. Explicit BEGIN gives control over commit timing. Keep transactions short in web apps.
PostgresVerse UPI transfer
API service uses transaction block for debit/credit/ledger insert on payment success.
Outcome: Reconciliation finds zero orphan debits after gateway timeout retries.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!