ROLLBACK_TO_SAVEPOINT(7pg17)
ROLLBACK TO SAVEPOINT π (PostgreSQL 17.11 docs, section 7)
okay so this is the PostgreSQL 17.11 Documentation page for ROLLBACK TO SAVEPOINT, and Iβm gonna explain it like weβre in the group chat. Section 7, bestie. π«Ά
NAME
ROLLBACK_TO_SAVEPOINT β roll back to a savepoint. Thatβs the name, thatβs the vibe. π
SYNOPSIS β¨
Exact spell, no cap:
ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name
DESCRIPTION π¦
Okay so picture this: youβve got a savepoint, and you run some commands after it. ROLLBACK TO SAVEPOINT rolls back all commands that were executed after the savepoint was established, then starts a new subtransaction at the same transaction level. The savepoint stays valid and can be rolled back to again later, if needed. Iconic. π
Also β and this is the plot twist β ROLLBACK TO SAVEPOINT implicitly destroys all savepoints that were established after the named savepoint. So itβs not just rolling back, itβs also clearing out the newer savepoints. Not me being dramatic, thatβs literally the spec. π
PARAMETERS π
savepoint_nameβ the savepoint to roll back to. Thatβs it, thatβs the parameter. π
NOTES π
Use RELEASE SAVEPOINT to destroy a savepoint without discarding the effects of commands executed after it was established. Different move, different vibe. β¨
Specifying a savepoint name that has not been established is an error. No cap, it will not vibe-check as valid. π
Cursors have somewhat non-transactional behavior with respect to savepoints β and this part is a whole saga:
- Any cursor that is opened inside a savepoint will be closed when the savepoint is rolled back.
- If a previously opened cursor is affected by a
FETCHorMOVEcommand inside a savepoint that is later rolled back, the cursor remains at the position thatFETCHleft it pointing to (that is, the cursor motion caused byFETCHis not rolled back). - Closing a cursor is not undone by rolling back, either.
- However, other side-effects caused by the cursorβs query (such as side-effects of volatile functions called by the query) are rolled back if they occur during a savepoint that is later rolled back.
- A cursor whose execution causes a transaction to abort is put in a cannot-execute state, so while the transaction can be restored using
ROLLBACK TO SAVEPOINT, the cursor can no longer be used. Thatβs the tragic part, bestie. π
EXAMPLES πΈ
To undo the effects of the commands executed after my_savepoint was established:
ROLLBACK TO SAVEPOINT my_savepoint;
Cursor positions are not affected by savepoint rollback:
BEGIN;
DECLARE foo CURSOR FOR SELECT 1 UNION SELECT 2;
SAVEPOINT foo;
FETCH 1 FROM foo;
?column?
----------
1
ROLLBACK TO SAVEPOINT foo;
FETCH 1 FROM foo;
?column?
----------
2
COMMIT;
COMPATIBILITY π
The SQL standard specifies that the key word SAVEPOINT is mandatory, but PostgreSQL and Oracle allow it to be omitted. SQL allows only WORK, not TRANSACTION, as a noise word after ROLLBACK. Also, SQL has an optional clause AND [ NO ] CHAIN which is not currently supported by PostgreSQL. Otherwise, this command conforms to the SQL standard. Periodt. π
SEE ALSO π
BEGIN(7)COMMIT(7)RELEASE SAVEPOINT (RELEASE_SAVEPOINT(7))ROLLBACK(7)SAVEPOINT(7)
xoxo, your PostgreSQL 17.11 docs bestie, dated 2026 β this was ROLLBACK TO SAVEPOINT(7), and yes she ate. π
ππ«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql17 17.11-160000.1.1 Β· PostgreSQL
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms