2004-08-01 19:32:22 +02:00
|
|
|
<!--
|
2010-05-27 08:25:32 +02:00
|
|
|
$PostgreSQL: pgsql/doc/src/sgml/ref/savepoint.sgml,v 1.8 2010/05/27 06:25:32 heikki Exp $
|
2004-08-01 19:32:22 +02:00
|
|
|
PostgreSQL documentation
|
|
|
|
-->
|
|
|
|
|
|
|
|
<refentry id="SQL-SAVEPOINT">
|
|
|
|
<refmeta>
|
2010-04-03 09:23:02 +02:00
|
|
|
<refentrytitle>SAVEPOINT</refentrytitle>
|
2008-11-14 11:22:48 +01:00
|
|
|
<manvolnum>7</manvolnum>
|
2004-08-01 19:32:22 +02:00
|
|
|
<refmiscinfo>SQL - Language Statements</refmiscinfo>
|
|
|
|
</refmeta>
|
|
|
|
|
|
|
|
<refnamediv>
|
|
|
|
<refname>SAVEPOINT</refname>
|
|
|
|
<refpurpose>define a new savepoint within the current transaction</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
|
|
|
|
<indexterm zone="sql-savepoint">
|
|
|
|
<primary>SAVEPOINT</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<indexterm zone="sql-savepoint">
|
|
|
|
<primary>savepoints</primary>
|
|
|
|
<secondary>defining</secondary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<refsynopsisdiv>
|
|
|
|
<synopsis>
|
|
|
|
SAVEPOINT <replaceable>savepoint_name</replaceable>
|
|
|
|
</synopsis>
|
|
|
|
</refsynopsisdiv>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Description</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
<command>SAVEPOINT</command> establishes a new savepoint within
|
|
|
|
the current transaction.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
A savepoint is a special mark inside a transaction that allows all commands
|
|
|
|
that are executed after it was established to be rolled back, restoring
|
|
|
|
the transaction state to what it was at the time of the savepoint.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Parameters</title>
|
|
|
|
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><replaceable>savepoint_name</replaceable></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The name to give to the new savepoint.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Notes</title>
|
|
|
|
|
|
|
|
<para>
|
2010-04-03 09:23:02 +02:00
|
|
|
Use <xref linkend="SQL-ROLLBACK-TO"> to
|
|
|
|
rollback to a savepoint. Use <xref linkend="SQL-RELEASE-SAVEPOINT">
|
|
|
|
to destroy a savepoint, keeping
|
2004-08-01 19:32:22 +02:00
|
|
|
the effects of commands executed after it was established.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Savepoints can only be established when inside a transaction block.
|
|
|
|
There can be multiple savepoints defined within a transaction.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Examples</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To establish a savepoint and later undo the effects of all commands executed
|
|
|
|
after it was established:
|
|
|
|
<programlisting>
|
|
|
|
BEGIN;
|
2005-01-06 21:53:34 +01:00
|
|
|
INSERT INTO table1 VALUES (1);
|
2004-08-01 19:32:22 +02:00
|
|
|
SAVEPOINT my_savepoint;
|
2005-01-06 21:53:34 +01:00
|
|
|
INSERT INTO table1 VALUES (2);
|
2004-08-12 21:12:21 +02:00
|
|
|
ROLLBACK TO SAVEPOINT my_savepoint;
|
2005-01-06 21:53:34 +01:00
|
|
|
INSERT INTO table1 VALUES (3);
|
2004-08-01 19:32:22 +02:00
|
|
|
COMMIT;
|
|
|
|
</programlisting>
|
|
|
|
The above transaction will insert the values 1 and 3, but not 2.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To establish and later destroy a savepoint:
|
|
|
|
<programlisting>
|
|
|
|
BEGIN;
|
2005-01-06 21:53:34 +01:00
|
|
|
INSERT INTO table1 VALUES (3);
|
2004-08-01 19:32:22 +02:00
|
|
|
SAVEPOINT my_savepoint;
|
2005-01-06 21:53:34 +01:00
|
|
|
INSERT INTO table1 VALUES (4);
|
2004-08-12 21:12:21 +02:00
|
|
|
RELEASE SAVEPOINT my_savepoint;
|
2004-08-01 19:32:22 +02:00
|
|
|
COMMIT;
|
|
|
|
</programlisting>
|
|
|
|
The above transaction will insert both 3 and 4.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>Compatibility</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
SQL requires a savepoint to be destroyed automatically when another
|
|
|
|
savepoint with the same name is established. In
|
|
|
|
<productname>PostgreSQL</>, the old savepoint is kept, though only the more
|
|
|
|
recent one will be used when rolling back or releasing. (Releasing the
|
2010-05-27 08:25:32 +02:00
|
|
|
newer savepoint with <command>RELEASE SAVEPOINT</> will cause the older one
|
|
|
|
to again become accessible to <command>ROLLBACK TO SAVEPOINT</> and
|
|
|
|
<command>RELEASE SAVEPOINT</>.) Otherwise, <command>SAVEPOINT</command> is
|
|
|
|
fully SQL conforming.
|
2004-08-01 19:32:22 +02:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1>
|
|
|
|
<title>See Also</title>
|
|
|
|
|
|
|
|
<simplelist type="inline">
|
2010-04-03 09:23:02 +02:00
|
|
|
<member><xref linkend="sql-begin"></member>
|
|
|
|
<member><xref linkend="sql-commit"></member>
|
|
|
|
<member><xref linkend="sql-release-savepoint"></member>
|
|
|
|
<member><xref linkend="sql-rollback"></member>
|
|
|
|
<member><xref linkend="sql-rollback-to"></member>
|
2004-08-01 19:32:22 +02:00
|
|
|
</simplelist>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|