Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do SQLite savepoints support partial rollback inside a transaction?

Quick answer

A savepoint is a named mark you can return to within a transaction. ROLLBACK TO name undoes changes made after the most recent matching savepoint, but does not end the whole transaction or remove that savepoint. Later savepoints are canceled. RELEASE name removes that savepoint and any nested ones; releasing an inner savepoint is not a commit. Releasing the outermost savepoint commits. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I want to undo one part of a multi-step SQLite operation without throwing away all the earlier work. How do savepoints help?

Geez

A savepoint marks a position you can return to. If you make several changes, then decide that only the latest section should be undone, use ROLLBACK TO savepoint_name. SQLite restores the state to just after that named mark. The surrounding transaction remains in progress, so earlier work is not discarded. The savepoint itself remains available, and savepoints created after it are canceled. [S1]

Think of it as a bookmark in a draft: rolling back to it removes the edits after the bookmark, not the whole draft. This is useful when one step is optional or may need to be retried, while earlier steps should stay intact.

Follow-up question

Could you show that with a small example, including what happens to the savepoint after rollback?

Geez

Assume a disposable database has an inventory table and the example updates rows with the shown SKUs. The table and rows are illustrative placeholders; adapt them to your own schema. The inner savepoint isolates one update:

SAVEPOINT import_work;

UPDATE inventory
SET quantity = quantity + 5
WHERE sku = 'A1';

SAVEPOINT one_item;

UPDATE inventory
SET quantity = quantity + 2
WHERE sku = 'B2';

ROLLBACK TO one_item;
-- The B2 update is undone; the A1 update remains.
-- one_item still exists, so release it explicitly:
RELEASE one_item;

RELEASE import_work;
-- Because this is the outermost savepoint, this commits.

The key detail is that ROLLBACK TO one_item returns to the state just after one_item was created. It does not remove one_item; the later RELEASE one_item removes that marker. [S1] The final release is included to show the outermost case, so run this example only where committing the illustrated change is acceptable.

Follow-up question

I’m unsure about RELEASE. Does releasing a savepoint undo its work or commit the transaction?

Geez

Neither description is right for every case. RELEASE removes the matching savepoint and any savepoints nested inside it; it does not undo their changes. Releasing an inner savepoint does not itself commit changes to the database file. Releasing the outermost savepoint does commit. [S1]

So, in the example, releasing one_item removes the inner marker, while the earlier A1 change is still part of the larger unit of work. Releasing import_work is different because it is outermost. If you are unsure which savepoint is outermost in your code, trace where each savepoint is created and released before running a committing path.

Follow-up question

What should I check when adding this pattern to a longer sequence of updates?

Geez

Write down the intended boundary before adding commands: which changes must survive a partial rollback, and which later changes should be discarded? Then give each savepoint a distinct, meaningful name and keep its rollback and release paths easy to follow.

A practical control-flow sketch is: create the outer mark; do work that should be retained; create an inner mark; do optional work; if that optional work should be undone, roll back to the inner mark and then release it; finally decide whether to release the outermost mark. The rollback target and the outermost release have different effects, so don’t treat them as interchangeable. [S1]

Try the intended paths with disposable data and inspect the resulting rows before applying the pattern to important data. Savepoint behavior does not determine what your application should retain or whether a particular update is correct; those choices depend on your schema and task.

A practical checklist

  • Mark which earlier changes should survive a partial rollback.

  • Name savepoints so each rollback target is clear.

  • Check which later savepoints a rollback will cancel.

  • After ROLLBACK TO, account for the fact that the target savepoint remains.

  • Confirm whether each RELEASE is inner or outermost before running it.

  • Try the intended control-flow paths with disposable data.

A few more useful answers

Does `ROLLBACK TO` remove the savepoint I named?

No. It restores the state to just after the most recent matching savepoint, but leaves that savepoint available. Later savepoints are canceled. [S1]

Does releasing an inner savepoint commit its changes?

No. Releasing an inner savepoint removes it and nested savepoints, but does not itself commit changes to the database file. Releasing the outermost savepoint commits. [S1]

Can I use the example unchanged in my application?

Treat the table and SKU names as illustrative placeholders, and verify that the updates match your schema and intended behavior. The example explains savepoint control flow, not what your application should change.

Sources

References used for this guide.

  1. [S1] SQLite Savepoints
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work