SQLMentor // glossary

Savepoint

A savepoint is a named marker inside a transaction that lets you roll back part of the transaction's work — everything since the savepoint — without undoing the whole transaction.

Useful for "try this step, and if it fails, back out just that step but keep going" patterns inside a longer multi-step transaction. SAVEPOINT sp1; creates the marker; ROLLBACK TO SAVEPOINT sp1; undoes everything after it while the transaction itself stays open, so you can retry or take a different path and still COMMIT the rest.

BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
SAVEPOINT sp1;
UPDATE accounts SET balance = balance + 100 WHERE id = 999; -- fails: no such account
ROLLBACK TO SAVEPOINT sp1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;