ROLLBACK_TO_SAVEPOINT(7pg14)
β¨ ROLLBACK TO SAVEPOINT β¨
okay so bestie, we're in the PostgreSQL 14.24 documentation and this little icon is literally the "undo" button for your savepoints π stay with me, it's so good.
πΈ NAME πΈ
ROLLBACK_TO_SAVEPOINT β roll back to a savepoint π
π¦ SYNOPSIS π¦
ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name
look at that. so clean. so iconic. β¨
π DESCRIPTION π
So here's the vibe: ROLLBACK TO SAVEPOINT rolls back all the commands that were executed after the savepoint was established. every single one. gone. π
But β and this is the main character detail β the savepoint itself remains valid and you can roll back to it again later if you need to. it's not going anywhere, it's just sitting there being helpful π«Ά
Also, ROLLBACK TO SAVEPOINT implicitly destroys all savepoints that were established after the named savepoint. so any savepoints you made downstream of it? poof. not sorry π
π PARAMETERS π
savepoint_nameβ the savepoint to roll back to. that's it. that's the whole parameter. queen behavior honestly π
π§ NOTES π§
- Use
RELEASE SAVEPOINTto destroy a savepoint without discarding the effects of commands executed after it was established. different tool, different job, iykyk β¨ - Specifying a savepoint name that has not been established is an error. so don't do that π
Okay now the cursor part, and ngl this is the part where you gotta pay attention because cursors are lowkey non-transactional when it comes to savepoints π let me break it down:
- 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. the cursor said "i'm staying right here" πͺ - 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. tragic. but like, accurate π
fr fr bestie, read the cursor rules twice. they will humble you.
π₯ EXAMPLES π₯
Wanna undo the effects of the commands executed after my_savepoint was established? slay, here you go:
ROLLBACK TO SAVEPOINT my_savepoint;
And here's the glow-up that proves 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;
see how the second FETCH gives you 2? that's the cursor just... continuing. no cap. π
β COMPATIBILITY β
So the SQL standard specifies that the key word SAVEPOINT is mandatory β but PostgreSQL and Oracle allow it to be omitted. we're flexible like that π
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)
and that's the whole page bestie! πΈπππ¦ this was PostgreSQL 14.24, dated 2026, and honestly? go forth and roll back responsibly β¨π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-160000.1.1 Β· PostgreSQL
- opensuse-leap 16.0: postgresql14 14.24-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