ROLLBACK_TO_SAVEPOINT(7pg16)

πŸ“¦ version 16.15, in opensuse-leap 16.0

✨ 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 FETCH or MOVE command inside a savepoint that gets rolled back later, the cursor remains at the position that FETCH left it pointing to β€” that is, the cursor motion caused by FETCH is 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 🌟


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