2 doc/src/sgml/ref/rollback_to.sgml
3 PostgreSQL documentation
6 <refentry id=
"sql-rollback-to">
7 <indexterm zone=
"sql-rollback-to">
8 <primary>ROLLBACK TO SAVEPOINT
</primary>
11 <indexterm zone=
"sql-rollback-to">
12 <primary>savepoints
</primary>
13 <secondary>rolling back
</secondary>
17 <refentrytitle>ROLLBACK TO SAVEPOINT
</refentrytitle>
18 <manvolnum>7</manvolnum>
19 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
23 <refname>ROLLBACK TO SAVEPOINT
</refname>
24 <refpurpose>roll back to a savepoint
</refpurpose>
29 ROLLBACK [ WORK | TRANSACTION ] TO [ SAVEPOINT ]
<replaceable>savepoint_name
</replaceable>
34 <title>Description
</title>
37 Roll back all commands that were executed after the savepoint was
38 established and then start a new subtransaction at the same transaction level.
39 The savepoint remains valid and can be rolled back to again later,
44 <command>ROLLBACK TO SAVEPOINT
</command> implicitly destroys all savepoints that
45 were established after the named savepoint.
50 <title>Parameters
</title>
54 <term><replaceable class=
"parameter">savepoint_name
</replaceable></term>
57 The savepoint to roll back to.
68 Use
<link linkend=
"sql-release-savepoint"><command>RELEASE SAVEPOINT
</command></link> to destroy a savepoint
69 without discarding the effects of commands executed after it was
74 Specifying a savepoint name that has not been established is an error.
78 Cursors have somewhat non-transactional behavior with respect to
79 savepoints. Any cursor that is opened inside a savepoint will be closed
80 when the savepoint is rolled back. If a previously opened cursor is
81 affected by a
<command>FETCH
</command> or
<command>MOVE
</command> command inside a
82 savepoint that is later rolled back, the cursor remains at the
83 position that
<command>FETCH
</command> left it pointing to (that is, the cursor
84 motion caused by
<command>FETCH
</command> is not rolled back).
85 Closing a cursor is not undone by rolling back, either.
86 However, other side-effects caused by the cursor's query (such as
87 side-effects of volatile functions called by the query)
<emphasis>are
</emphasis>
88 rolled back if they occur during a savepoint that is later rolled back.
89 A cursor whose execution causes a transaction to abort is put in a
90 cannot-execute state, so while the transaction can be restored using
91 <command>ROLLBACK TO SAVEPOINT
</command>, the cursor can no longer be used.
96 <title>Examples
</title>
99 To undo the effects of the commands executed after
<literal>my_savepoint
</literal>
102 ROLLBACK TO SAVEPOINT my_savepoint;
107 Cursor positions are not affected by savepoint rollback:
111 DECLARE foo CURSOR FOR SELECT
1 UNION SELECT
2;
120 ROLLBACK TO SAVEPOINT foo;
128 </programlisting></para>
134 <title>Compatibility
</title>
137 The
<acronym>SQL
</acronym> standard specifies that the key word
138 <literal>SAVEPOINT
</literal> is mandatory, but
<productname>PostgreSQL
</productname>
139 and
<productname>Oracle
</productname> allow it to be omitted. SQL allows
140 only
<literal>WORK
</literal>, not
<literal>TRANSACTION
</literal>, as a noise word
141 after
<literal>ROLLBACK
</literal>. Also, SQL has an optional clause
142 <literal>AND [ NO ] CHAIN
</literal> which is not currently supported by
143 <productname>PostgreSQL
</productname>. Otherwise, this command conforms to
149 <title>See Also
</title>
151 <simplelist type=
"inline">
152 <member><xref linkend=
"sql-begin"/></member>
153 <member><xref linkend=
"sql-commit"/></member>
154 <member><xref linkend=
"sql-release-savepoint"/></member>
155 <member><xref linkend=
"sql-rollback"/></member>
156 <member><xref linkend=
"sql-savepoint"/></member>