1ROLLBACK TO SAVEPOINT(7) PostgreSQL 16.1 DocumentationROLLBACK TO SAVEPOINT(7)
2
3
4
6 ROLLBACK_TO_SAVEPOINT - roll back to a savepoint
7
9 ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ] savepoint_name
10
12 Roll back all commands that were executed after the savepoint was
13 established and then start a new subtransaction at the same transaction
14 level. The savepoint remains valid and can be rolled back to again
15 later, if needed.
16
17 ROLLBACK TO SAVEPOINT implicitly destroys all savepoints that were
18 established after the named savepoint.
19
21 savepoint_name
22 The savepoint to roll back to.
23
25 Use RELEASE SAVEPOINT to destroy a savepoint without discarding the
26 effects of commands executed after it was established.
27
28 Specifying a savepoint name that has not been established is an error.
29
30 Cursors have somewhat non-transactional behavior with respect to
31 savepoints. Any cursor that is opened inside a savepoint will be closed
32 when the savepoint is rolled back. If a previously opened cursor is
33 affected by a FETCH or MOVE command inside a savepoint that is later
34 rolled back, the cursor remains at the position that FETCH left it
35 pointing to (that is, the cursor motion caused by FETCH is not rolled
36 back). Closing a cursor is not undone by rolling back, either. However,
37 other side-effects caused by the cursor's query (such as side-effects
38 of volatile functions called by the query) are rolled back if they
39 occur during a savepoint that is later rolled back. A cursor whose
40 execution causes a transaction to abort is put in a cannot-execute
41 state, so while the transaction can be restored using ROLLBACK TO
42 SAVEPOINT, the cursor can no longer be used.
43
45 To undo the effects of the commands executed after my_savepoint was
46 established:
47
48 ROLLBACK TO SAVEPOINT my_savepoint;
49
50 Cursor positions are not affected by savepoint rollback:
51
52 BEGIN;
53
54 DECLARE foo CURSOR FOR SELECT 1 UNION SELECT 2;
55
56 SAVEPOINT foo;
57
58 FETCH 1 FROM foo;
59 ?column?
60 ----------
61 1
62
63 ROLLBACK TO SAVEPOINT foo;
64
65 FETCH 1 FROM foo;
66 ?column?
67 ----------
68 2
69
70 COMMIT;
71
73 The SQL standard specifies that the key word SAVEPOINT is mandatory,
74 but PostgreSQL and Oracle allow it to be omitted. SQL allows only WORK,
75 not TRANSACTION, as a noise word after ROLLBACK. Also, SQL has an
76 optional clause AND [ NO ] CHAIN which is not currently supported by
77 PostgreSQL. Otherwise, this command conforms to the SQL standard.
78
80 BEGIN(7), COMMIT(7), RELEASE SAVEPOINT (RELEASE_SAVEPOINT(7)),
81 ROLLBACK(7), SAVEPOINT(7)
82
83
84
85PostgreSQL 16.1 2023 ROLLBACK TO SAVEPOINT(7)