SAVEPOINTs & Autocommit in SQL
Partial rollbacks inside all-or-nothing
Autocommit wraps every statement in its own txn — convenient, fatal for multi-statement atomicity. Explicit BEGIN groups sequences. SAVEPOINTs mark sub-points: ROLLBACK TO SAVEPOINT undoes part, RELEASE destroys the marker, outer COMMIT still atomic. Long user think-times prefer optimistic versions over held locks.
Autocommit per statement
Each statement its own txn — fine solo, fatal for sequences.
BEGIN groups atomic
Multi-statement all-or-nothing needs explicit bounds.
SAVEPOINT stages
ROLLBACK TO undoes part; RELEASE drops marker; outer COMMIT atomic.
Think-time prefers versions
Held locks across user pauses stall others — optimistic version checks instead.
1BEGIN;2SAVEPOINT lines;3-- insert lines… on error:4ROLLBACK TO SAVEPOINT lines;5COMMIT;
| tool | role |
|---|---|
| SAVEPOINT | partial rollback point |
| COMMIT | outer still atomic |
Order header survives a bad line batch — only lines rewind. Without the savepoint the whole order would roll back on one line error.
Order header survives bad lines
Partial rewind, not full abort.
1BEGIN;2SAVEPOINT lines;3-- lines fail → ROLLBACK TO SAVEPOINT lines;4COMMIT;
| part | fate |
|---|---|
| header | kept |
| bad lines | rewound |
Sequences need BEGIN
Autocommit splits the unit.
1-- autocommit: two commits (splittable)2BEGIN; -- transfer pair -- COMMIT; -- one commit
| mode | atomicity |
|---|---|
| autocommit pair | partial failure possible |
| explicit txn | both-or-neither |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.