SAVEPOINT and Partial Rollbacks
Part of the SQL Roadmap
In short
A long transaction shouldn't be all or nothing when only one small step fails. Savepoints let you undo just that step and keep the rest of your good work.
A SAVEPOINT marks a point inside a transaction that you can roll back to without undoing the whole transaction. It is useful for long jobs where one step may fail but earlier work should be kept.

Think of it like this
Checkpoints on a trek
On the trek to Kedarkantha, guides mark checkpoints along the trail. If your group takes a wrong turn after the third checkpoint, you walk back to that checkpoint, not all the way down to base camp. A SAVEPOINT is a checkpoint inside a transaction. ROLLBACK TO it undoes only what happened after it.
Watch it happen
Placing an order with an optional coupon
Top of the stack is coloured
1/6BEGIN, then insert the order for ₹1,499. This part must survive.
The big ideas
What you need to know
SAVEPOINT name
Marks a point inside an open transaction. You can have several, each with its own name.
ROLLBACK TO SAVEPOINT
Undoes everything after that point but keeps the transaction open. You still need COMMIT or ROLLBACK.
RELEASE SAVEPOINT
Forgets the checkpoint when you no longer need it. The work after it stays.
Rescue after an error
In PostgreSQL, any error aborts the whole transaction. Rolling back to a savepoint is the only way to continue it.
See it in code
Walk through the code
1BEGIN;2 3INSERT INTO orders (order_id, customer_id, amount)4VALUES (5012, 301, 1499.00);5 6SAVEPOINT before_coupon;7 8UPDATE coupons9SET uses_left = uses_left - 110WHERE code = 'DIWALI10';11-- ERROR: violates check constraint "coupons_uses_left_check"12 13ROLLBACK TO SAVEPOINT before_coupon;14 15INSERT INTO payments (order_id, method, amount)16VALUES (5012, 'UPI', 1499.00);17 18COMMIT;1/5Start the transaction and insert the order.
Same syntax in PostgreSQL and MySQL. SQL Server uses SAVE TRANSACTION before_coupon and ROLLBACK TRANSACTION before_coupon.
Save yourself the pain
Common mistakes
Oops
Thinking ROLLBACK TO SAVEPOINT ends the transaction.
Do this instead
It only rewinds to the checkpoint. The transaction stays open until you COMMIT or ROLLBACK.
Oops
Writing plain ROLLBACK when you meant to undo one step.
Do this instead
Plain ROLLBACK throws away everything since BEGIN, including the work before the savepoint.
Oops
Using SAVEPOINT in autocommit mode with no BEGIN.
Do this instead
Savepoints only exist inside a transaction block. Start with BEGIN first.
Your mission
Batch import with savepoints
Quick check
After ROLLBACK TO SAVEPOINT sp1, what happens to statements run before sp1 was created?
Go deeper
- 1Create a savepoint, make a change, and ROLLBACK TO SAVEPOINT to undo only that change.
- 2Use RELEASE SAVEPOINT once a step has succeeded.
- 3Notice that nothing is saved permanently until the outer transaction commits.
More topics at this stop
Stuck on this topic? Ask an Insider
Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.