3 PostgreSQL documentation
6 <refentry id=
"SQL-ROLLBACK-TO">
8 <refentrytitle id=
"SQL-ROLLBACK-TO-TITLE">ROLLBACK TO SAVEPOINT
</refentrytitle>
9 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
13 <refname>ROLLBACK TO SAVEPOINT
</refname>
14 <refpurpose>roll back to a savepoint
</refpurpose>
17 <indexterm zone=
"sql-rollback-to">
18 <primary>ROLLBACK TO SAVEPOINT
</primary>
21 <indexterm zone=
"sql-rollback-to">
22 <primary>savepoints
</primary>
23 <secondary>rolling back
</secondary>
28 ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ]
<replaceable>savepoint_name
</replaceable>
33 <title>Description
</title>
36 Roll back all commands that were executed after the savepoint was
37 established. The savepoint remains valid and can be rolled back to
38 again later, if needed.
42 <command>ROLLBACK TO SAVEPOINT<
/> implicitly destroys all savepoints that
43 were established after the named savepoint.
48 <title>Parameters
</title>
52 <term><replaceable class=
"PARAMETER">savepoint_name<
/></term>
55 The savepoint to roll back to.
66 Use
<xref linkend=
"SQL-RELEASE-SAVEPOINT"
67 endterm=
"SQL-RELEASE-SAVEPOINT-TITLE"> to destroy a savepoint without
68 discarding the effects of commands executed after it was established.
72 Specifying a savepoint name that has not been established is an error.
76 Cursors have somewhat non-transactional behavior with respect to
77 savepoints. Any cursor that is opened inside a savepoint will be closed
78 when the savepoint is rolled back. If a previously opened cursor is
80 <command>FETCH<
/> command inside a savepoint that is later rolled
81 back, the cursor position remains at the position that
<command>FETCH<
/>
82 left it pointing to (that is,
<command>FETCH<
/> is not rolled back).
83 Closing a cursor is not undone by rolling back, either.
84 A cursor whose execution causes a transaction to abort is put in a
85 cannot-execute state, so while the transaction can be restored using
86 <command>ROLLBACK TO SAVEPOINT<
/>, the cursor can no longer be used.
91 <title>Examples
</title>
94 To undo the effects of the commands executed after
<literal>my_savepoint
</literal>
97 ROLLBACK TO SAVEPOINT my_savepoint;
102 Cursor positions are not affected by savepoint rollback:
106 DECLARE foo CURSOR FOR SELECT
1 UNION SELECT
2;
115 ROLLBACK TO SAVEPOINT foo;
130 <title>Compatibility
</title>
133 The
<acronym>SQL<
/> standard specifies that the key word
134 <literal>SAVEPOINT<
/> is mandatory, but
<productname>PostgreSQL<
/>
135 and
<productname>Oracle<
/> allow it to be omitted. SQL allows
136 only
<literal>WORK<
/>, not
<literal>TRANSACTION<
/>, as a noise word
137 after
<literal>ROLLBACK<
/>. Also, SQL has an optional clause
138 <literal>AND [ NO ] CHAIN<
/> which is not currently supported by
139 <productname>PostgreSQL<
/>. Otherwise, this command conforms to
145 <title>See Also
</title>
147 <simplelist type=
"inline">
148 <member><xref linkend=
"sql-begin" endterm=
"sql-begin-title"></member>
149 <member><xref linkend=
"sql-commit" endterm=
"sql-commit-title"></member>
150 <member><xref linkend=
"sql-release-savepoint" endterm=
"sql-release-savepoint-title"></member>
151 <member><xref linkend=
"sql-rollback" endterm=
"sql-rollback-title"></member>
152 <member><xref linkend=
"sql-savepoint" endterm=
"sql-savepoint-title"></member>