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>PREPARE TRANSACTION</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-prepare.html" title="PREPARE" /><link rel="next" href="sql-reassign-owned.html" title="REASSIGN OWNED" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">PREPARE TRANSACTION</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-prepare.html" title="PREPARE">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-reassign-owned.html" title="REASSIGN OWNED">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-PREPARE-TRANSACTION"><div class="titlepage"></div><a id="id-1.9.3.160.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">PREPARE TRANSACTION</span></h2><p>PREPARE TRANSACTION — prepare the current transaction for two-phase commit</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 PREPARE TRANSACTION <em class="replaceable"><code>transaction_id</code></em>
4 </pre></div><div class="refsect1" id="id-1.9.3.160.5"><h2>Description</h2><p>
5 <code class="command">PREPARE TRANSACTION</code> prepares the current transaction
6 for two-phase commit. After this command, the transaction is no longer
7 associated with the current session; instead, its state is fully stored on
8 disk, and there is a very high probability that it can be committed
9 successfully, even if a database crash occurs before the commit is
12 Once prepared, a transaction can later be committed or rolled back
13 with <a class="link" href="sql-commit-prepared.html" title="COMMIT PREPARED"><code class="command">COMMIT PREPARED</code></a>
14 or <a class="link" href="sql-rollback-prepared.html" title="ROLLBACK PREPARED"><code class="command">ROLLBACK PREPARED</code></a>,
15 respectively. Those commands can be issued from any session, not
16 only the one that executed the original transaction.
18 From the point of view of the issuing session, <code class="command">PREPARE
19 TRANSACTION</code> is not unlike a <code class="command">ROLLBACK</code> command:
20 after executing it, there is no active current transaction, and the
21 effects of the prepared transaction are no longer visible. (The effects
22 will become visible again if the transaction is committed.)
24 If the <code class="command">PREPARE TRANSACTION</code> command fails for any
25 reason, it becomes a <code class="command">ROLLBACK</code>: the current transaction
27 </p></div><div class="refsect1" id="id-1.9.3.160.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>transaction_id</code></em></span></dt><dd><p>
28 An arbitrary identifier that later identifies this transaction for
29 <code class="command">COMMIT PREPARED</code> or <code class="command">ROLLBACK PREPARED</code>.
30 The identifier must be written as a string literal, and must be
31 less than 200 bytes long. It must not be the same as the identifier
32 used for any currently prepared transaction.
33 </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.160.7"><h2>Notes</h2><p>
34 <code class="command">PREPARE TRANSACTION</code> is not intended for use in applications
35 or interactive sessions. Its purpose is to allow an external
36 transaction manager to perform atomic global transactions across multiple
37 databases or other transactional resources. Unless you're writing a
38 transaction manager, you probably shouldn't be using <code class="command">PREPARE
41 This command must be used inside a transaction block. Use <a class="link" href="sql-begin.html" title="BEGIN"><code class="command">BEGIN</code></a> to start one.
43 It is not currently allowed to <code class="command">PREPARE</code> a transaction that
44 has executed any operations involving temporary tables or the session's
45 temporary namespace, created any cursors <code class="literal">WITH HOLD</code>, or
46 executed <code class="command">LISTEN</code>, <code class="command">UNLISTEN</code>, or
47 <code class="command">NOTIFY</code>.
48 Those features are too tightly
49 tied to the current session to be useful in a transaction to be prepared.
51 If the transaction modified any run-time parameters with <code class="command">SET</code>
52 (without the <code class="literal">LOCAL</code> option),
53 those effects persist after <code class="command">PREPARE TRANSACTION</code>, and will not
54 be affected by any later <code class="command">COMMIT PREPARED</code> or
55 <code class="command">ROLLBACK PREPARED</code>. Thus, in this one respect
56 <code class="command">PREPARE TRANSACTION</code> acts more like <code class="command">COMMIT</code> than
57 <code class="command">ROLLBACK</code>.
59 All currently available prepared transactions are listed in the
60 <a class="link" href="view-pg-prepared-xacts.html" title="53.17. pg_prepared_xacts"><code class="structname">pg_prepared_xacts</code></a>
62 </p><div class="caution"><h3 class="title">Caution</h3><p>
63 It is unwise to leave transactions in the prepared state for a long time.
64 This will interfere with the ability of <code class="command">VACUUM</code> to reclaim
65 storage, and in extreme cases could cause the database to shut down
66 to prevent transaction ID wraparound (see <a class="xref" href="routine-vacuuming.html#VACUUM-FOR-WRAPAROUND" title="24.1.5. Preventing Transaction ID Wraparound Failures">Section 24.1.5</a>). Keep in mind also that the transaction
67 continues to hold whatever locks it held. The intended usage of the
68 feature is that a prepared transaction will normally be committed or
69 rolled back as soon as an external transaction manager has verified that
70 other databases are also prepared to commit.
72 If you have not set up an external transaction manager to track prepared
73 transactions and ensure they get closed out promptly, it is best to keep
74 the prepared-transaction feature disabled by setting
75 <a class="xref" href="runtime-config-resource.html#GUC-MAX-PREPARED-TRANSACTIONS">max_prepared_transactions</a> to zero. This will
76 prevent accidental creation of prepared transactions that might then
77 be forgotten and eventually cause problems.
78 </p></div></div><div class="refsect1" id="SQL-PREPARE-TRANSACTION-EXAMPLES"><h2>Examples</h2><p>
79 Prepare the current transaction for two-phase commit, using
80 <code class="literal">foobar</code> as the transaction identifier:
82 </p><pre class="programlisting">
83 PREPARE TRANSACTION 'foobar';
84 </pre></div><div class="refsect1" id="id-1.9.3.160.9"><h2>Compatibility</h2><p>
85 <code class="command">PREPARE TRANSACTION</code> is a
86 <span class="productname">PostgreSQL</span> extension. It is intended for use by
87 external transaction management systems, some of which are covered by
88 standards (such as X/Open XA), but the SQL side of those systems is not
90 </p></div><div class="refsect1" id="id-1.9.3.160.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-commit-prepared.html" title="COMMIT PREPARED"><span class="refentrytitle">COMMIT PREPARED</span></a>, <a class="xref" href="sql-rollback-prepared.html" title="ROLLBACK PREPARED"><span class="refentrytitle">ROLLBACK PREPARED</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-prepare.html" title="PREPARE">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-reassign-owned.html" title="REASSIGN OWNED">Next</a></td></tr><tr><td width="40%" align="left" valign="top">PREPARE </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"> REASSIGN OWNED</td></tr></table></div></body></html>