Learn SQL
-- performance.savepoint_autocommit_tcl

SAVEPOINTs & Autocommit in SQL

Partial rollbacks inside all-or-nothing

--concept

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.

--how it works
1

Autocommit per statement

Each statement its own txn — fine solo, fatal for sequences.

2

BEGIN groups atomic

Multi-statement all-or-nothing needs explicit bounds.

3

SAVEPOINT stages

ROLLBACK TO undoes part; RELEASE drops marker; outer COMMIT atomic.

4

Think-time prefers versions

Held locks across user pauses stall others — optimistic version checks instead.

--visualization
BEGINSAVEPOINTPARTIAL ROLLBACKCOMMIT
Stage 1 / 4
BEGIN — outer atomic unit opened (autocommit off for the sequence).
--example
query.sql
1BEGIN;
2SAVEPOINT lines;
3-- insert lines… on error:
4ROLLBACK TO SAVEPOINT lines;
5COMMIT;
Result
toolrole
SAVEPOINTpartial rollback point
COMMITouter 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.

--variants

Order header survives bad lines

Partial rewind, not full abort.

query.sql
1BEGIN;
2SAVEPOINT lines;
3-- lines fail → ROLLBACK TO SAVEPOINT lines;
4COMMIT;
Result
partfate
headerkept
bad linesrewound
1Header insert stands.
2Lines rewind to marker.
3Retry lines without remaking header.

Sequences need BEGIN

Autocommit splits the unit.

query.sql
1-- autocommit: two commits (splittable)
2BEGIN; -- transfer pair -- COMMIT; -- one commit
Result
modeatomicity
autocommit pairpartial failure possible
explicit txnboth-or-neither
1Crash between autocommits leaves half-moved money.
2Explicit bounds move together.
3Habit: always-BEGIN for sequences.
--key facts
Autocommitper-statement txns — sequences need BEGIN
SAVEPOINTstaged writes with partial undo
Think-timeversions over held locks
--use cases
Staged order/ledger writes with fallible lines
Interactive flows without long-held locks
--under the hood
SAVEPOINTs are sub-txn markers — cheap until rollback work scales with undone writes.
psql AUTOCOMMIT off changes tool default, not server — code must still BEGIN.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.