Transactions
Transactions are a fundamental concept of all database system. The essential point of a transaction is that it bundles multiple steps into a single, all-or-nothing operation. The intermediate states between the steps are not visible to other concurrent transaction, and if some failure occurs that prevents the transaction from completing, the none of the steps affect the database at all.
In PostgreSQL, a transaction is set up by surrounding the SQL commands of the transaction with begin and commit commands.
BEGIN;
UPDATE accounts SET balance = balance - 100.00
WHERE name = 'Alice';
SAVEPOINT my_savepoint;
UPDATE accounts SET balance = balance + 100.00
WHERE name = 'Bob';
-- oops ... forget that and use Wally's account
ROLLBACK TO my_savepoint;
UPDATE accounts SET balance = balance + 100.00
WHERE name = 'Wally';
COMMIT;