ROLLBACK_TO_SAVEPOINT(7pg13)

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

✨ ROLLBACK TO SAVEPOINT(7) ✨

okay bestie, welcome to the ROLLBACK_TO_SAVEPOINT(7) page from the PostgreSQL 13.23 Documentation. if your transaction is doing too much and you need to rewind to a savepoint, this is literally the main character command. πŸŽ€

πŸŽ€ NAME πŸŽ€

ROLLBACK_TO_SAVEPOINT - roll back to a savepoint. that's the name, that's the vibe, and it ate. πŸ’…

πŸ’– SYNOPSIS πŸ’–

ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so here's the tea: it rolls back all commands that were executed after the savepoint was established. the savepoint remains valid and can be rolled back to again later, if needed. no cap, that's iconic. ✨

ROLLBACK TO SAVEPOINT implicitly destroys all savepoints that were established after the named savepoint. not me nuking the later savepoints, but that's the vibe. πŸ’€

🌸 PARAMETERS 🌸

  • savepoint_name β€” the savepoint to roll back to. that's it, that's the parameter, and it understood the assignment. 🫢

πŸ”₯ NOTES πŸ”₯

  • use RELEASE SAVEPOINT (RELEASE_SAVEPOINT(7)) to destroy a savepoint without discarding the effects of commands executed after it was established. yep, different move, different slay. πŸ’–
  • > 🚨 specifying a savepoint name that has not been established is an error. no cap, bestie. πŸ™ˆ
  • cursors have somewhat non-transactional behavior with respect to savepoints. 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 is later rolled back, 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. 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. 😭

πŸ‘‘ 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 this little beauty:

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 πŸ¦‹


okay that's the page, ily, bye! πŸ’– this one is from PostgreSQL 13.23, dated 2025, and it's literally the ROLLBACK TO SAVEPOINT(7) page. πŸŽ€

πŸ“¦ where this page comes from