Transaction
Definition
A transaction is a unit of work started with BEGIN and ended with COMMIT, which makes all its changes visible at once, or ROLLBACK, which discards them.
In practice
Without a transaction each statement commits on its own, so a crash between two related writes leaves half-done changes. Inside one, an error, a violated constraint, a dropped client connection or an explicit rollback discards every write since BEGIN. Writers to the same row wait for each other's row locks until commit or rollback.
A transaction holds a connection and its locks for its whole duration, so long transactions exhaust the pool and block other writers. Keep slow work such as HTTP calls out of them. A database transaction also cannot cover other systems: sending an email or publishing a message needs an outbox or a saga, not a wider BEGIN.
Example
BEGIN; UPDATE accounts SET balance = balance - 50 WHERE id = 1; UPDATE accounts SET balance = balance + 50 WHERE id = 2; COMMIT; either moves 50 or moves nothing.