Sub topic 7
Partial rollback with savepoints
This chapter assumes you know how COMMIT and a full ROLLBACK end a transaction, and that you have the accounts table from the earlier chapters. The PL/SQL section also assumes you can read a basic anonymous block.
What a savepoint is
A savepoint is a named marker inside the current transaction. ROLLBACK TO SAVEPOINT undoes only the changes made after that marker. The transaction stays open, and the changes made before the marker remain pending until you COMMIT or do a full ROLLBACK.
SAVEPOINT step_one; -- set a marker
-- ... more DML ...
ROLLBACK TO SAVEPOINT step_one; -- undo everything after the marker
ROLLBACK WORK TO step_one; -- same thing; WORK and SAVEPOINT are optional wordsSavepoint names are ordinary identifiers. They belong to the transaction, not to a PL/SQL block or procedure.
Worked example
This is the example from the plan. It assumes accounts holds Alice (1000) and Bob (200).
INSERT INTO accounts (id, owner, balance) VALUES (3, 'Carol', 500);
SAVEPOINT after_carol;
UPDATE accounts SET balance = balance - 50 WHERE id = 3;
SAVEPOINT after_debit;
DELETE FROM accounts WHERE id = 1;
ROLLBACK TO SAVEPOINT after_debit; -- undoes only the DELETE
ROLLBACK TO SAVEPOINT after_carol; -- undoes the UPDATE; after_debit no longer exists
COMMIT; -- Carol's row is saved with balance 500The table shows the uncommitted state your session sees after each step.
| Step | Statement | Carol | Alice | Savepoints that exist |
|---|---|---|---|---|
| 1 | `INSERT` Carol | 500 | 1000 | none |
| 2 | `SAVEPOINT after_carol` | 500 | 1000 | after_carol |
| 3 | `UPDATE` Carol | 450 | 1000 | after_carol |
| 4 | `SAVEPOINT after_debit` | 450 | 1000 | after_carol, after_debit |
| 5 | `DELETE` Alice | 450 | gone | after_carol, after_debit |
| 6 | `ROLLBACK TO after_debit` | 450 | 1000 | after_carol, after_debit |
| 7 | `ROLLBACK TO after_carol` | 500 | 1000 | after_carol |
| 8 | `COMMIT` | 500 | 1000 | none |
The rules
- The target savepoint survives.
ROLLBACK TO after_debitkeepsafter_debit, so you can roll back to it again after more work. - Later savepoints are erased. Rolling back to
after_caroldestroysafter_debitbecause it was created afterafter_carol. Savepoints created before the target are unaffected. - The transaction stays open. Nothing is committed, and a later
COMMITsaves whatever remains. - Locks acquired after the savepoint are released. Locks acquired before it are kept. Row locks therefore follow the surviving changes.
- Reusing a name moves the marker. If you create a savepoint with a name that already exists in the transaction, the earlier one is erased. Only the newest marker with that name remains.
- Transaction ends erase all savepoints.
COMMIT, a fullROLLBACK, and the implicit commit before and after DDL all discard every savepoint. - There is no
RELEASE SAVEPOINT. Some other databases let you discard a savepoint without rolling back. Oracle does not. Unused savepoints simply disappear at the end of the transaction.
Rule 5 is useful for repeated steps:
SAVEPOINT s;
UPDATE accounts SET balance = balance + 1 WHERE id = 2;
SAVEPOINT s; -- the first marker s is gone; s now sits here
UPDATE accounts SET balance = balance + 1 WHERE id = 2;
ROLLBACK TO SAVEPOINT s; -- undoes only the second UPDATESavepoints and statement-level atomicity
You never need a savepoint to recover from a single failed statement. Oracle marks an implicit savepoint before each DML statement. If the statement fails, for example with a constraint violation, Oracle rolls back to that implicit point. The statement's partial effects disappear and earlier statements in the transaction are untouched.
| Row | Implicit (statement) savepoint | Explicit savepoint |
|---|---|---|
| Created by | Oracle, before each statement | Your `SAVEPOINT` command |
| Covers | One statement | Any number of statements |
| Rolled back | Automatically on error | When you run `ROLLBACK TO` |
| Name | None | Yours |
Use an explicit savepoint when a group of statements must succeed or fail as a unit inside a larger transaction.
Savepoints in PL/SQL
PL/SQL accepts SAVEPOINT and ROLLBACK TO SAVEPOINT directly. The common pattern is a savepoint per item in a loop, so one bad item does not discard the rest.
SET SERVEROUTPUT ON
BEGIN
FOR r IN (SELECT id FROM accounts) LOOP
SAVEPOINT before_row;
BEGIN
UPDATE accounts SET balance = balance - 100 WHERE id = r.id;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO SAVEPOINT before_row;
DBMS_OUTPUT.PUT_LINE('Skipped account ' || r.id || ': ' || SQLERRM);
END;
END LOOP;
COMMIT;
END;
/An account with less than 100 violates the CHECK (balance >= 0) constraint and raises ORA-02290. The handler rolls back to before_row and the loop continues. All other debits are committed together by the final COMMIT.
Three details matter here:
- Reusing
before_rowon every iteration is intentional. EachSAVEPOINTmoves the marker (rule 5), so the rollback only reaches back to the current row. - Here the failed
UPDATEwas already undone by statement-level atomicity. With several statements per row, the savepoint is what undoes the earlier ones. - If an exception escapes the whole block unhandled, Oracle rolls back the work done by that block. It does not roll back changes your session made before the block started, and it does not commit.
Common errors and pitfalls
ORA-01086: savepoint was never established
This error appears when you roll back to a savepoint that does not exist. Typical causes are a misspelled name, a savepoint erased by an earlier rollback to an older savepoint, or a savepoint lost to a commit or DDL.
SAVEPOINT a;
SAVEPOINT b;
ROLLBACK TO SAVEPOINT a; -- b is erased
ROLLBACK TO SAVEPOINT b; -- ORA-01086: savepoint 'B' never established in this session or is invalidDDL silently erases savepoints
A CREATE, ALTER, or DROP commits the transaction first. Your earlier work is now permanent, and ROLLBACK TO an earlier savepoint fails with ORA-01086. Keep DDL out of transactions that rely on savepoints.
Autonomous transactions do not share savepoints
An autonomous transaction is a separate transaction. It cannot roll back to a savepoint set by its caller, and the caller cannot roll back to one set inside it.
Rollback only undoes database changes
ROLLBACK TO SAVEPOINT does not reverse side effects outside the transaction's undo data:
- Sequence values consumed by
NEXTVALstay consumed. - Output sent with
DBMS_OUTPUTand files written withUTL_FILEremain. - PL/SQL variables and package state keep their current values.
Do not swallow errors
A handler such as WHEN OTHERS THEN ROLLBACK TO SAVEPOINT x; with no logging hides real failures. Record or re-raise the error as well. Skip only the errors you expect.
Exercises
Exercise
A transaction runs SAVEPOINT a; UPDATE ...; SAVEPOINT b; DELETE ...; ROLLBACK TO SAVEPOINT a; and then ROLLBACK TO SAVEPOINT b;. What happens on the last statement, and which changes are still pending after the first rollback?
Show answer
The last statement fails with ORA-01086, because rolling back to a erased the later savepoint b. After ROLLBACK TO a, both the UPDATE and the DELETE are undone. Only changes made before SAVEPOINT a remain pending, and the transaction is still open.
Exercise
You run INSERT INTO accounts ... for id 5, SAVEPOINT p1, then CREATE TABLE tmp (x NUMBER), then ROLLBACK TO SAVEPOINT p1. Is the insert for id 5 still pending? What does the ROLLBACK TO do?
Show answer
The insert is no longer pending. The DDL committed the transaction, so the insert is permanent. ROLLBACK TO SAVEPOINT p1 fails with ORA-01086, because the commit erased p1.
Exercise
In the PL/SQL loop example, why does SAVEPOINT before_row inside the loop not cause a "duplicate name" error?
Show answer
Oracle does not treat a repeated savepoint name as an error. Creating a savepoint with an existing name moves the marker to the new position and erases the old one. Each iteration therefore rolls back only to the start of its own row.