ROLLBACK_TO_SAVEPOINT(7pg16)
β¨ ROLLBACK TO SAVEPOINT π
okay so this is the <pink>ROLLBACK TO SAVEPOINT</pink> page from the <purple>PostgreSQL 16.15</purple> docs, bestie β it's a <lavender>7</lavender>-section manual page and honestly it's kind of a hero. it's the "undo button but make it transactional" moment, and you're gonna love it. π
πΈ NAME πΈ
ROLLBACK_TO_SAVEPOINT β roll back to a savepoint. literally what it says on the tin. π«Ά
π¦ SYNOPSIS π¦
ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name
look at her. optional noise words, optional SAVEPOINT, one very important name. iconic. β¨
π DESCRIPTION π
so here's the vibe: <pink>ROLLBACK TO SAVEPOINT</pink> rolls back all the commands that were executed after the savepoint was established, and then it starts a brand-new subtransaction at the same transaction level. no cap. π
and the best part? the savepoint stays valid β like, she's still there, still cute β so you can roll back to her again later if you need to. obsessed. π§
one thing you gotta know though (and this is important, fr fr): ROLLBACK TO SAVEPOINT implicitly destroys every savepoint that was established after the named savepoint. gone. not coming back. π
π PARAMETERS π
savepoint_nameβ the savepoint to roll back to. that's it. that's the parameter. the main character. π
β NOTES β
- use
<mint>RELEASE SAVEPOINT</mint>when you wanna destroy a savepoint without throwing away the effects of the commands executed after it was established. different energy, different command. π₯ - specifying a savepoint name that has not been established is an error. it's giving "who? never met her." π
- k so cursors have "somewhat non-transactional behavior" with respect to savepoints, which is the most lowkey ominous way to phrase that, honestly:
- 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 gets rolled back later, 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. nope. π
- 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. π
- and if a cursor whose execution causes a transaction to abort, that cursor 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. she's done. retired. π
like, the transaction gets better but the cursor does not. that's the tea. π«
π§ 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 β watch her go:
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;
see how the FETCH came back 2 and not 1? yeah. that's the whole thing. slay. π
π COMPATIBILITY π
so the SQL standard specifies that the key word SAVEPOINT is mandatory, but <pink>PostgreSQL</pink> and <peach>Oracle</peach> allow it to be omitted. we're flexible like that. β¨
SQL allows only WORK, not TRANSACTION, as a noise word after ROLLBACK. noted.
also, SQL has an optional clause AND [ NO ] CHAIN which is not currently supported by PostgreSQL. so don't come for her about that one. π
otherwise? this command conforms to the SQL standard. periodt. π
π SEE ALSO π
BEGIN(7)COMMIT(7)RELEASE SAVEPOINT (RELEASE_SAVEPOINT(7))ROLLBACK(7)SAVEPOINT(7)
and that's the page, bestie π«Ά this was the <rainbow>PostgreSQL 16.15</rainbow> docs, dated <yellow>2026</yellow>, and the page is <hotpink>ROLLBACK TO SAVEPOINT(7)</hotpink> β go forth and savepoint responsibly. πβ¨π
π¦ where this page comes from
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql16 16.15-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