summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/html/sql-savepoint.html
blob: 5f9c764f192a82a7bd8a9c40e8da8a3159931eee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>SAVEPOINT</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-rollback-to.html" title="ROLLBACK TO SAVEPOINT" /><link rel="next" href="sql-security-label.html" title="SECURITY LABEL" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">SAVEPOINT</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-rollback-to.html" title="ROLLBACK TO SAVEPOINT">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 15.6 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-security-label.html" title="SECURITY LABEL">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-SAVEPOINT"><div class="titlepage"></div><a id="id-1.9.3.170.1" class="indexterm"></a><a id="id-1.9.3.170.2" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">SAVEPOINT</span></h2><p>SAVEPOINT — define a new savepoint within the current transaction</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
SAVEPOINT <em class="replaceable"><code>savepoint_name</code></em>
</pre></div><div class="refsect1" id="id-1.9.3.170.6"><h2>Description</h2><p>
   <code class="command">SAVEPOINT</code> establishes a new savepoint within
   the current transaction.
  </p><p>
   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.
  </p></div><div class="refsect1" id="id-1.9.3.170.7"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>savepoint_name</code></em></span></dt><dd><p>
      The name to give to the new savepoint.  If savepoints with the
      same name already exist, they will be inaccessible until newer
      identically-named savepoints are released.
     </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.170.8"><h2>Notes</h2><p>
   Use <a class="link" href="sql-rollback-to.html" title="ROLLBACK TO SAVEPOINT"><code class="command">ROLLBACK TO</code></a> to
   rollback to a savepoint.  Use <a class="link" href="sql-release-savepoint.html" title="RELEASE SAVEPOINT"><code class="command">RELEASE SAVEPOINT</code></a>
   to destroy a savepoint, keeping
   the effects of commands executed after it was established.
  </p><p>
   Savepoints can only be established when inside a transaction block.
   There can be multiple savepoints defined within a transaction.
  </p></div><div class="refsect1" id="id-1.9.3.170.9"><h2>Examples</h2><p>
   To establish a savepoint and later undo the effects of all commands executed
   after it was established:
</p><pre class="programlisting">
BEGIN;
    INSERT INTO table1 VALUES (1);
    SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (2);
    ROLLBACK TO SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (3);
COMMIT;
</pre><p>
   The above transaction will insert the values 1 and 3, but not 2.
  </p><p>
   To establish and later destroy a savepoint:
</p><pre class="programlisting">
BEGIN;
    INSERT INTO table1 VALUES (3);
    SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (4);
    RELEASE SAVEPOINT my_savepoint;
COMMIT;
</pre><p>
   The above transaction will insert both 3 and 4.
  </p><p>
  To use a single savepoint name:
</p><pre class="programlisting">
BEGIN;
    INSERT INTO table1 VALUES (1);
    SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (2);
    SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (3);

    -- rollback to the second savepoint
    ROLLBACK TO SAVEPOINT my_savepoint;
    SELECT * FROM table1;               -- shows rows 1 and 2

    -- release the second savepoint
    RELEASE SAVEPOINT my_savepoint;

    -- rollback to the first savepoint
    ROLLBACK TO SAVEPOINT my_savepoint;
    SELECT * FROM table1;               -- shows only row 1
COMMIT;
</pre><p>
  The above transaction shows row 3 being rolled back first, then row 2.
  </p></div><div class="refsect1" id="id-1.9.3.170.10"><h2>Compatibility</h2><p>
   SQL requires a savepoint to be destroyed automatically when another
   savepoint with the same name is established.  In
   <span class="productname">PostgreSQL</span>, the old savepoint is kept, though only the more
   recent one will be used when rolling back or releasing.  (Releasing the
   newer savepoint with <code class="command">RELEASE SAVEPOINT</code> will cause the older one
   to again become accessible to <code class="command">ROLLBACK TO SAVEPOINT</code> and
   <code class="command">RELEASE SAVEPOINT</code>.) Otherwise, <code class="command">SAVEPOINT</code> is
   fully SQL conforming.
  </p></div><div class="refsect1" id="id-1.9.3.170.11"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-begin.html" title="BEGIN"><span class="refentrytitle">BEGIN</span></a>, <a class="xref" href="sql-commit.html" title="COMMIT"><span class="refentrytitle">COMMIT</span></a>, <a class="xref" href="sql-release-savepoint.html" title="RELEASE SAVEPOINT"><span class="refentrytitle">RELEASE SAVEPOINT</span></a>, <a class="xref" href="sql-rollback.html" title="ROLLBACK"><span class="refentrytitle">ROLLBACK</span></a>, <a class="xref" href="sql-rollback-to.html" title="ROLLBACK TO SAVEPOINT"><span class="refentrytitle">ROLLBACK TO SAVEPOINT</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-rollback-to.html" title="ROLLBACK TO SAVEPOINT">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-security-label.html" title="SECURITY LABEL">Next</a></td></tr><tr><td width="40%" align="left" valign="top">ROLLBACK TO SAVEPOINT </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 15.6 Documentation">Home</a></td><td width="40%" align="right" valign="top"> SECURITY LABEL</td></tr></table></div></body></html>