TechLead
Lesson 8 of 10
5 min read
PostgreSQL

Transactions & Concurrency

Keep data consistent with ACID and isolation levels

ACID Transactions

Transactions group multiple changes into a single unit of work. If anything fails, you can roll back to keep data consistent.

BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

Isolation Levels

  • Read Committed (default)
  • Repeatable Read
  • Serializable (strongest)

Row Locks

SELECT * FROM accounts
WHERE id = 1
FOR UPDATE;

Locks prevent other transactions from changing the same rows while you update them.

Continue Learning