Before continuing: This entry is part of the transactions guide. If you are new to the topic, start with BEGIN / START TRANSACTION.
SAVEPOINT example
Product 2 starts with 7 units. We reserve 2, set a marker and then try a second reduction that we decide to undo.
BEGIN;
UPDATE products
SET stock = stock - 2
WHERE id = 2;
SAVEPOINT validated_booking;
UPDATE products
SET stock = stock - 5
WHERE id = 2;
ROLLBACK TO SAVEPOINT validated_booking;
SELECT stock AS stock_before_commit
FROM products
WHERE id = 2;
COMMIT;The first reduction is still pending; the second is undone. If you then run COMMIT, the stock of 5 is kept.
How SAVEPOINT is used
Key idea: going back to a savepoint does not close the whole transaction. The work done before the marker is still inside the transaction and still needs a final COMMIT or a ROLLBACK.
What changes from case to case
| Statement | Effect |
|---|---|
ROLLBACK | Abandons the whole transaction. |
ROLLBACK TO SAVEPOINT point | Undoes the work done after the marker and lets you carry on. |
RELEASE SAVEPOINT point | Removes that marker; on its own it does not commit the outer transaction. |
Common mix-ups with SAVEPOINT
Thinking RELEASE always means COMMIT
Inside an outer transaction, releasing a savepoint does not make the changes permanent by itself. If you then run a ROLLBACK on the outer transaction, you also lose the changes that remained after RELEASE.
Key differences between engines
PostgreSQL, SQLite and MySQL all support savepoints, but syntax and behaviour differ in certain situations. SQLite allows nested savepoints; PostgreSQL documents SAVEPOINT, ROLLBACK TO SAVEPOINT and RELEASE SAVEPOINT as operations on the current transaction. Also check whether your framework creates savepoints automatically.
Undoing one stage while keeping the earlier ones
A savepoint marks a position inside a unit of work. It is useful if you import a valid header and then try a detail that might be rejected. Rolling back to the savepoint undoes the later changes, but not what came before. The outer transaction still needs a decision to commit or roll back in full.
In the example, the header is written before the savepoint and the detail after it. Rolling back to it removes the detail, but the header is still there. RELEASE removes the savepoint; inside an open outer transaction it does not turn the header into a separately committed change. A ROLLBACK of the whole transaction could still remove it.
Do not treat savepoints as universal nested transactions. The syntax, error handling and rules for releasing savepoints depend on the engine, and SQL Server uses a different form to save them. In SQLite, a savepoint opened outside any transaction follows different rules when the outermost savepoint is released. Make the BEGIN explicit in the learning example, and check both the intermediate state and the final outcome.
Self-contained case to check in SQLite
The header is kept because only the detail added after the savepoint is discarded.
CREATE TABLE stages_demo (id INTEGER PRIMARY KEY, stage TEXT);
BEGIN;
INSERT INTO stages_demo VALUES (1, 'header');
SAVEPOINT detail;
INSERT INTO stages_demo VALUES (2, 'detail');
ROLLBACK TO detail;
RELEASE detail;
COMMIT;
SELECT id, stage FROM stages_demo ORDER BY id;| id | stage |
|---|---|
| 1 | header |
Only the write made after the savepoint is undone. The header is committed when the outer COMMIT is reached.
Check your interpretation: What is left in the table if you replace the final COMMIT with a full ROLLBACK?
Show answer
Nothing: the table would be empty. A full ROLLBACK undoes the whole transaction, including the header inserted before the SAVEPOINT.
Technical questions: SQLite: savepoints (opens in a new tab).