Sub topic 3
Atomicity: all or nothing
Assume you know basic SQL, including SELECT, INSERT, and UPDATE, and understand primary keys and constraints. Atomicity is the rule that makes a transaction behave as one unit: either every statement succeeds and its changes become visible, or none of its changes remain.
Why atomicity matters
Consider transferring money from one account to another. The transfer has two changes:
- Subtract money from the sender.
- Add the same money to the recipient.
If the database applies the first change but fails before applying the second, money disappears. Atomicity prevents this partial result.
A transaction starts with BEGIN. It succeeds permanently with COMMIT. If something goes wrong, ROLLBACK removes all changes made since the transaction began.
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance numeric(12,2) NOT NULL CHECK (balance >= 0)
);
INSERT INTO accounts (id, balance)
VALUES (1, 100.00), (2, 50.00);
BEGIN;
UPDATE accounts
SET balance = balance - 30
WHERE id = 1;
UPDATE accounts
SET balance = balance + 30
WHERE id = 2;
COMMIT;
SELECT id, balance
FROM accounts
ORDER BY id;After the commit, account 1 has 70.00 and account 2 has 80.00. Other database sessions see the committed result as a complete transfer, rather than observing only one half.
What happens on failure
Suppose the sender does not have enough money. The CHECK constraint rejects an update that would make the balance negative. The application must roll back the transaction.
BEGIN;
UPDATE accounts
SET balance = balance - 500
WHERE id = 1;
-- The CHECK constraint rejects the invalid balance.
-- PostgreSQL marks the transaction as failed.
ROLLBACK;
SELECT balance
FROM accounts
WHERE id = 1;The balance remains 70.00, its value before this transaction. The failed debit does not leave a partial change.
Some databases and drivers handle statement errors differently, but application code should always define what happens after an error. In PostgreSQL, a failed statement puts the current transaction into an aborted state. The application must issue ROLLBACK before it can run normal statements again.
| Operation | Effect |
|---|---|
| `BEGIN` | Starts an explicit transaction |
| `COMMIT` | Makes all successful changes permanent |
| `ROLLBACK` | Discards changes in the current transaction |
The transaction boundary
Place all mutually dependent changes inside the same transaction. For a transfer, the debit and credit belong together. A report query usually does not need to share that transaction.
Many database clients use autocommit by default. In autocommit mode, each individual statement is committed automatically. That is convenient for independent changes, but it is unsafe for a multi-step operation such as a transfer. Start an explicit transaction when partial completion would be incorrect.
Exercise
Why should the debit and credit in a money transfer use the same transaction?
Show answer
So a failure cannot leave only one change committed. The database commits both changes together or rolls both back.