A SQL savepoint marks a position inside a transaction. If a later statement fails or produces an unwanted result, you can roll back to that position without discarding every earlier change in the transaction.
Savepoints help when one business operation contains several related steps. You can keep the valid work, undo a smaller section, correct the problem, and then decide whether to commit or roll back the complete transaction.
SQL Savepoint Commands
| Command | Purpose |
|---|---|
| SAVEPOINT name | Creates a named marker in the current transaction |
| ROLLBACK TO SAVEPOINT name | Undoes changes made after the marker |
| RELEASE SAVEPOINT name | Removes the marker without committing the transaction |
| COMMIT | Makes the remaining transaction changes permanent |
| ROLLBACK | Undoes the complete uncommitted transaction |
Note: Savepoint syntax and behavior vary slightly across database systems. Check your database version before using savepoints with stored programs, error handlers, or data-definition statements.
Create and Roll Back to a Savepoint
Start a transaction before creating a savepoint. The following example records an order, creates a marker, and then corrects an invalid inventory update without removing the order row.
Example:
-- Start one transaction for the related changes.
START TRANSACTION;
INSERT INTO orders (order_id, customer_name, status)
VALUES (501, 'Aarav Mehta', 'Pending');
-- Keep the order insertion if a later step needs correction.
SAVEPOINT order_created;
UPDATE inventory
SET quantity = quantity - 100
WHERE product_id = 42;
-- Undo only the inventory update made after the marker.
ROLLBACK TO SAVEPOINT order_created;
UPDATE inventory
SET quantity = quantity - 1
WHERE product_id = 42;
COMMIT;
After the partial rollback, the inserted order remains part of the transaction. The update that subtracted 100 is undone, and the corrected update subtracts one before the final commit.
Understand What a Partial Rollback Changes
ROLLBACK TO SAVEPOINT reverses statements executed after the named savepoint. It does not end the outer transaction. You can continue running statements and later use COMMIT or a full ROLLBACK.
In many database systems, rolling back to an earlier savepoint removes savepoints created after it, while the target savepoint remains available. Exact details can differ, so avoid designs that depend on repeatedly reusing a complicated chain of markers.
Example:
START TRANSACTION;
-- First change in the transaction.
UPDATE accounts SET balance = balance - 250 WHERE account_id = 10;
SAVEPOINT debit_complete;
-- Second change will be discarded.
UPDATE accounts SET balance = balance + 250 WHERE account_id = 20;
SAVEPOINT credit_complete;
ROLLBACK TO SAVEPOINT debit_complete;
-- Apply the corrected destination account.
UPDATE accounts SET balance = balance + 250 WHERE account_id = 21;
COMMIT;
Result:
Account 10: balance decreased by 250
Account 20: no committed change
Account 21: balance increased by 250
This example explains savepoint mechanics, but production money transfers require stricter validation, locking, audit records, and error handling.
Release a Savepoint
Use RELEASE SAVEPOINT when you no longer need a marker. Releasing it does not commit any data. It only removes the named recovery point while the outer transaction continues.
Example:
START TRANSACTION;
INSERT INTO shipments (order_id, carrier, status)
VALUES (501, 'Swift Parcel', 'Preparing');
SAVEPOINT shipment_added;
-- Validation succeeded, so remove the unused marker.
RELEASE SAVEPOINT shipment_added;
UPDATE orders SET status = 'Packed' WHERE order_id = 501;
COMMIT;
After release, you cannot roll back to shipment_added. You can still roll back the whole transaction until it is committed.
Savepoints Are Not Independent Transactions
A savepoint does not create a separate transaction and does not make changes permanent. The outer transaction still controls the final result. If you issue a full ROLLBACK, the database undoes changes made before and after every savepoint.
- A savepoint has meaning only inside its transaction.
- A partial rollback does not release database locks automatically in the same way on every system.
- A final commit saves all changes that remain in the outer transaction.
- A final rollback discards all uncommitted changes.
Use Multiple Savepoints Carefully
Multiple markers can make batch processing easier to recover. Give each marker a meaningful name and keep the flow short enough to understand.
Example:
START TRANSACTION;
-- Store the customer change first.
UPDATE customers SET loyalty_level = 'Gold' WHERE customer_id = 88;
SAVEPOINT customer_updated;
-- Add an optional reward.
INSERT INTO rewards (customer_id, points) VALUES (88, 500);
SAVEPOINT reward_added;
-- A business rule rejects the reward, but keeps the customer update.
ROLLBACK TO SAVEPOINT customer_updated;
COMMIT;
The rollback removes the reward insertion because it occurred after customer_updated. The customer update remains and becomes permanent at commit.
Common Savepoint Problems
- Missing transaction: some systems reject a savepoint outside an active transaction, while others start or manage transactions differently.
- Implicit commit: certain schema-changing statements may commit automatically and remove existing savepoints.
- Unsupported table engine: a database may accept the command even when a non-transactional table cannot roll back changes.
- Reused names: creating the same savepoint name again may replace or hide the earlier marker.
- Long transactions: too many operations can hold locks, increase contention, and consume recovery resources.
When to Use Savepoints
Use savepoints when a transaction has optional or recoverable stages, such as importing a batch, creating an order with supplementary records, or applying several administrative updates. Prefer a full rollback when any failed step makes the entire operation invalid.
Keep validation outside the transaction when possible, and keep the transaction short. A savepoint is a recovery tool, not a replacement for constraints, input validation, or a clear transaction boundary.
Conclusion
SQL savepoints give you controlled recovery inside a transaction. Create a marker with SAVEPOINT, undo later work with ROLLBACK TO SAVEPOINT, and remove an unused marker with RELEASE SAVEPOINT. Always finish the outer transaction deliberately and test database-specific behavior before using the pattern in production.