ACID Properties
A – Atomicity
All or nothing. Every statement in a transaction succeeds, or none of them take effect. A failure halfway through (error, crash, power cut) leaves no partial changes. → The money transfer either fully happens or doesn't happen at all.
C – Consistency
A transaction moves the database from one valid state to another. All rules — constraints (CHECK, FK, UNIQUE, NOT NULL) and triggers — hold at commit time. Invalid data makes the transaction fail.
→ A balance can never go negative if there's a CHECK (balance >= 0).
I – Isolation
Concurrent transactions don't interfere with each other; each behaves as if it were running alone (to a degree set by the isolation level — next lesson). → Two people booking the last seat at the same moment don't both get it.
D – Durability
Once COMMIT returns, the data is permanent — it survives crashes and power loss. → PostgreSQL writes changes to the WAL (Write-Ahead Log) on disk before confirming the commit. After a crash, it replays the WAL.
How PostgreSQL implements ACID
| Property | Mechanism |
|---|---|
| Atomicity | transaction status + MVCC row versions |
| Consistency | constraints, triggers, deferred constraints |
| Isolation | MVCC snapshots + locks + isolation levels |
| Durability | WAL + fsync (+ replicas and backups) |
ACID vs BASE
Many NoSQL systems choose BASE (Basically Available, Soft state, Eventually consistent): faster and more scalable, but data may be briefly inconsistent. Fine for likes and view counts — not for payments.
Deferred constraints
DEFERRABLE INITIALLY DEFERRED foreign keys are checked at COMMIT instead of after each statement — useful when rows reference each other in a cycle.
Example
-- Atomicity + Consistency: a constraint failure undoes the whole statement
SELECT sum(balance) AS total_money FROM accounts; -- 258000.00
-- One UPDATE touching several rows: either all change or none do
-- UPDATE accounts SET balance = balance - 4000;
-- ↑ Sita's eSewa wallet (3500) and Bikash's cash (2500) would go negative, so:
-- ERROR: new row for relation "accounts" violates check constraint "accounts_balance_check"
-- The same statement inside a DO block, so we can catch the error and continue:
DO $$ BEGIN
UPDATE accounts SET balance = balance - 4000;
EXCEPTION WHEN check_violation THEN RAISE NOTICE 'Atomic: no account was changed';
END $$;
SELECT sum(balance) AS total_money FROM accounts; -- still 258000.00
-- Consistency with a deferred constraint: check at COMMIT, not per statement
CREATE TABLE people (
id INT PRIMARY KEY,
partner INT REFERENCES people(id) DEFERRABLE INITIALLY DEFERRED
);
BEGIN;
INSERT INTO people VALUES (1, 2); -- 2 doesn't exist yet — allowed for now
INSERT INTO people VALUES (2, 1);
COMMIT; -- checked here: both exist ✅
-- Durability: the WAL position moves forward as changes are written
SELECT pg_current_wal_lsn() AS wal_position_before;
INSERT INTO expenses (user_id, title, amount) VALUES (1, 'Durable coffee', 180);
SELECT pg_current_wal_lsn() AS wal_position_after; -- a later position
DROP TABLE people;