]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/sql-savepoint.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / sql-savepoint.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!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 18.0 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">
3 SAVEPOINT <em class="replaceable"><code>savepoint_name</code></em>
4 </pre></div><div class="refsect1" id="id-1.9.3.170.6"><h2>Description</h2><p>
5    <code class="command">SAVEPOINT</code> establishes a new savepoint within
6    the current transaction.
7   </p><p>
8    A savepoint is a special mark inside a transaction that allows all commands
9    that are executed after it was established to be rolled back, restoring
10    the transaction state to what it was at the time of the savepoint.
11   </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>
12       The name to give to the new savepoint.  If savepoints with the
13       same name already exist, they will be inaccessible until newer
14       identically-named savepoints are released.
15      </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.170.8"><h2>Notes</h2><p>
16    Use <a class="link" href="sql-rollback-to.html" title="ROLLBACK TO SAVEPOINT"><code class="command">ROLLBACK TO</code></a> to
17    rollback to a savepoint.  Use <a class="link" href="sql-release-savepoint.html" title="RELEASE SAVEPOINT"><code class="command">RELEASE SAVEPOINT</code></a>
18    to destroy a savepoint, keeping
19    the effects of commands executed after it was established.
20   </p><p>
21    Savepoints can only be established when inside a transaction block.
22    There can be multiple savepoints defined within a transaction.
23   </p></div><div class="refsect1" id="id-1.9.3.170.9"><h2>Examples</h2><p>
24    To establish a savepoint and later undo the effects of all commands executed
25    after it was established:
26 </p><pre class="programlisting">
27 BEGIN;
28     INSERT INTO table1 VALUES (1);
29     SAVEPOINT my_savepoint;
30     INSERT INTO table1 VALUES (2);
31     ROLLBACK TO SAVEPOINT my_savepoint;
32     INSERT INTO table1 VALUES (3);
33 COMMIT;
34 </pre><p>
35    The above transaction will insert the values 1 and 3, but not 2.
36   </p><p>
37    To establish and later destroy a savepoint:
38 </p><pre class="programlisting">
39 BEGIN;
40     INSERT INTO table1 VALUES (3);
41     SAVEPOINT my_savepoint;
42     INSERT INTO table1 VALUES (4);
43     RELEASE SAVEPOINT my_savepoint;
44 COMMIT;
45 </pre><p>
46    The above transaction will insert both 3 and 4.
47   </p><p>
48   To use a single savepoint name:
49 </p><pre class="programlisting">
50 BEGIN;
51     INSERT INTO table1 VALUES (1);
52     SAVEPOINT my_savepoint;
53     INSERT INTO table1 VALUES (2);
54     SAVEPOINT my_savepoint;
55     INSERT INTO table1 VALUES (3);
56
57     -- rollback to the second savepoint
58     ROLLBACK TO SAVEPOINT my_savepoint;
59     SELECT * FROM table1;               -- shows rows 1 and 2
60
61     -- release the second savepoint
62     RELEASE SAVEPOINT my_savepoint;
63
64     -- rollback to the first savepoint
65     ROLLBACK TO SAVEPOINT my_savepoint;
66     SELECT * FROM table1;               -- shows only row 1
67 COMMIT;
68 </pre><p>
69   The above transaction shows row 3 being rolled back first, then row 2.
70   </p></div><div class="refsect1" id="id-1.9.3.170.10"><h2>Compatibility</h2><p>
71    SQL requires a savepoint to be destroyed automatically when another
72    savepoint with the same name is established.  In
73    <span class="productname">PostgreSQL</span>, the old savepoint is kept, though only the more
74    recent one will be used when rolling back or releasing.  (Releasing the
75    newer savepoint with <code class="command">RELEASE SAVEPOINT</code> will cause the older one
76    to again become accessible to <code class="command">ROLLBACK TO SAVEPOINT</code> and
77    <code class="command">RELEASE SAVEPOINT</code>.) Otherwise, <code class="command">SAVEPOINT</code> is
78    fully SQL conforming.
79   </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 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> SECURITY LABEL</td></tr></table></div></body></html>