3
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
4
<title>PREPARE TRANSACTION</title>
5
<link rel="stylesheet" href="stylesheet.css" type="text/css">
6
<link rev="made" href="pgsql-docs@postgresql.org">
7
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
8
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
9
<link rel="up" href="sql-commands.html" title="SQL Commands">
10
<link rel="prev" href="sql-prepare.html" title="PREPARE">
11
<link rel="next" href="sql-reindex.html" title="REINDEX">
12
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
14
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en">
15
<a name="sql-prepare-transaction"></a><div class="titlepage"></div>
16
<div class="refnamediv">
18
<p>PREPARE TRANSACTION — prepare the current transaction for two-phase commit</p>
20
<a name="id779697"></a><div class="refsynopsisdiv">
22
<pre class="synopsis">PREPARE TRANSACTION <em class="replaceable"><code>transaction_id</code></em></pre>
24
<div class="refsect1" lang="en">
25
<a name="id779719"></a><h2>Description</h2>
26
<p> <code class="command">PREPARE TRANSACTION</code> prepares the current transaction
27
for two-phase commit. After this command, the transaction is no longer
28
associated with the current session; instead, its state is fully stored on
29
disk, and there is a very high probability that it can be committed
30
successfully, even if a database crash occurs before the commit is
33
<p> Once prepared, a transaction can later be committed or rolled
34
back with <code class="command">COMMIT PREPARED</code> or
35
<code class="command">ROLLBACK PREPARED</code>, respectively. Those commands
36
can be issued from any session, not only the one that executed the
39
<p> From the point of view of the issuing session, <code class="command">PREPARE
40
TRANSACTION</code> is not unlike a <code class="command">ROLLBACK</code> command:
41
after executing it, there is no active current transaction, and the
42
effects of the prepared transaction are no longer visible. (The effects
43
will become visible again if the transaction is committed.)
45
<p> If the <code class="command">PREPARE TRANSACTION</code> command fails for any
46
reason, it becomes a <code class="command">ROLLBACK</code>: the current transaction
50
<div class="refsect1" lang="en">
51
<a name="id779796"></a><h2>Parameters</h2>
52
<div class="variablelist"><dl>
53
<dt><span class="term"><em class="replaceable"><code>transaction_id</code></em></span></dt>
54
<dd><p> An arbitrary identifier that later identifies this transaction for
55
<code class="command">COMMIT PREPARED</code> or <code class="command">ROLLBACK PREPARED</code>.
56
The identifier must be written as a string literal, and must be
57
less than 200 bytes long. It must not be the same as the identifier
58
used for any currently prepared transaction.
62
<div class="refsect1" lang="en">
63
<a name="id779831"></a><h2>Notes</h2>
64
<p> This command must be used inside a transaction block. Use
65
<code class="command">BEGIN</code> to start one.
67
<p> It is not currently allowed to <code class="command">PREPARE</code> a transaction that
68
has executed any operations involving temporary tables or
69
created any cursors <code class="literal">WITH HOLD</code>. Those features are too tightly
70
tied to the current session to be useful in a transaction to be prepared.
72
<p> If the transaction modified any run-time parameters with <code class="command">SET</code>,
73
those effects persist after <code class="command">PREPARE TRANSACTION</code>, and will not
74
be affected by any later <code class="command">COMMIT PREPARED</code> or
75
<code class="command">ROLLBACK PREPARED</code>. Thus, in this one respect
76
<code class="command">PREPARE TRANSACTION</code> acts more like <code class="command">COMMIT</code> than
77
<code class="command">ROLLBACK</code>.
79
<p> All currently available prepared transactions are listed in the
80
<code class="structname">pg_prepared_xacts</code> system view.
82
<p> From a performance standpoint, it is unwise to leave transactions in
83
the prepared state for a long time: this will for instance interfere with
84
the ability of <code class="command">VACUUM</code> to reclaim storage. Keep in mind also
85
that the transaction continues to hold whatever locks it held.
87
usage of the feature is that a prepared transaction will normally be
88
committed or rolled back as soon as an external transaction manager
89
has verified that other databases are also prepared to commit.
91
<p> If you make any serious use of prepared transactions, you will probably
92
want to increase the value of <a href="runtime-config-resource.html#guc-max-prepared-transactions">max_prepared_transactions</a>, as the default setting is
93
quite small (to avoid wasting resources for those who don't use it).
94
It is recommendable to make it at least equal to
95
<a href="runtime-config-connection.html#guc-max-connections">max_connections</a>, so that every session can have
96
a prepared transaction pending.
99
<div class="refsect1" lang="en">
100
<a name="sql-prepare-transaction-examples"></a><h2>Examples</h2>
101
<p> Prepare the current transaction for two-phase commit, using
102
<code class="literal">foobar</code> as the transaction identifier:
105
<pre class="programlisting">PREPARE TRANSACTION 'foobar';</pre>
109
<div class="refsect1" lang="en">
110
<a name="id779997"></a><h2>See Also</h2>
111
<span class="simplelist"><a href="sql-commit-prepared.html">COMMIT PREPARED</a>, <a href="sql-rollback-prepared.html">ROLLBACK PREPARED</a></span>