2 doc/src/sgml/ref/savepoint.sgml
3 PostgreSQL documentation
6 <refentry id=
"sql-savepoint">
7 <indexterm zone=
"sql-savepoint">
8 <primary>SAVEPOINT
</primary>
11 <indexterm zone=
"sql-savepoint">
12 <primary>savepoints
</primary>
13 <secondary>defining
</secondary>
17 <refentrytitle>SAVEPOINT
</refentrytitle>
18 <manvolnum>7</manvolnum>
19 <refmiscinfo>SQL - Language Statements
</refmiscinfo>
23 <refname>SAVEPOINT
</refname>
24 <refpurpose>define a new savepoint within the current transaction
</refpurpose>
29 SAVEPOINT
<replaceable>savepoint_name
</replaceable>
34 <title>Description
</title>
37 <command>SAVEPOINT
</command> establishes a new savepoint within
38 the current transaction.
42 A savepoint is a special mark inside a transaction that allows all commands
43 that are executed after it was established to be rolled back, restoring
44 the transaction state to what it was at the time of the savepoint.
49 <title>Parameters
</title>
53 <term><replaceable>savepoint_name
</replaceable></term>
56 The name to give to the new savepoint. If savepoints with the
57 same name already exist, they will be inaccessible until newer
58 identically-named savepoints are released.
69 Use
<link linkend=
"sql-rollback-to"><command>ROLLBACK TO
</command></link> to
70 rollback to a savepoint. Use
<link linkend=
"sql-release-savepoint"><command>RELEASE SAVEPOINT
</command></link>
71 to destroy a savepoint, keeping
72 the effects of commands executed after it was established.
76 Savepoints can only be established when inside a transaction block.
77 There can be multiple savepoints defined within a transaction.
82 <title>Examples
</title>
85 To establish a savepoint and later undo the effects of all commands executed
86 after it was established:
89 INSERT INTO table1 VALUES (
1);
90 SAVEPOINT my_savepoint;
91 INSERT INTO table1 VALUES (
2);
92 ROLLBACK TO SAVEPOINT my_savepoint;
93 INSERT INTO table1 VALUES (
3);
96 The above transaction will insert the values
1 and
3, but not
2.
100 To establish and later destroy a savepoint:
103 INSERT INTO table1 VALUES (
3);
104 SAVEPOINT my_savepoint;
105 INSERT INTO table1 VALUES (
4);
106 RELEASE SAVEPOINT my_savepoint;
109 The above transaction will insert both
3 and
4.
113 To use a single savepoint name:
116 INSERT INTO table1 VALUES (
1);
117 SAVEPOINT my_savepoint;
118 INSERT INTO table1 VALUES (
2);
119 SAVEPOINT my_savepoint;
120 INSERT INTO table1 VALUES (
3);
122 -- rollback to the second savepoint
123 ROLLBACK TO SAVEPOINT my_savepoint;
124 SELECT * FROM table1; -- shows rows
1 and
2
126 -- release the second savepoint
127 RELEASE SAVEPOINT my_savepoint;
129 -- rollback to the first savepoint
130 ROLLBACK TO SAVEPOINT my_savepoint;
131 SELECT * FROM table1; -- shows only row
1
134 The above transaction shows row
3 being rolled back first, then row
2.
140 <title>Compatibility
</title>
143 SQL requires a savepoint to be destroyed automatically when another
144 savepoint with the same name is established. In
145 <productname>PostgreSQL
</productname>, the old savepoint is kept, though only the more
146 recent one will be used when rolling back or releasing. (Releasing the
147 newer savepoint with
<command>RELEASE SAVEPOINT
</command> will cause the older one
148 to again become accessible to
<command>ROLLBACK TO SAVEPOINT
</command> and
149 <command>RELEASE SAVEPOINT
</command>.) Otherwise,
<command>SAVEPOINT
</command> is
150 fully SQL conforming.
155 <title>See Also
</title>
157 <simplelist type=
"inline">
158 <member><xref linkend=
"sql-begin"/></member>
159 <member><xref linkend=
"sql-commit"/></member>
160 <member><xref linkend=
"sql-release-savepoint"/></member>
161 <member><xref linkend=
"sql-rollback"/></member>
162 <member><xref linkend=
"sql-rollback-to"/></member>