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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>PREPARE TRANSACTION</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css">
<link rev="made" href="pgsql-docs@postgresql.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.70.0">
<link rel="start" href="index.html" title="PostgreSQL 8.1.4 Documentation">
<link rel="up" href="sql-commands.html" title="SQL Commands">
<link rel="prev" href="sql-prepare.html" title="PREPARE">
<link rel="next" href="sql-reindex.html" title="REINDEX">
<link rel="copyright" href="ln-legalnotice.html" title="Legal Notice">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="refentry" lang="en">
<a name="sql-prepare-transaction"></a><div class="titlepage"></div>
<div class="refnamediv">
<h2>Name</h2>
<p>PREPARE TRANSACTION — prepare the current transaction for two-phase commit</p>
</div>
<a name="id779697"></a><div class="refsynopsisdiv">
<h2>Synopsis</h2>
<pre class="synopsis">PREPARE TRANSACTION <em class="replaceable"><code>transaction_id</code></em></pre>
</div>
<div class="refsect1" lang="en">
<a name="id779719"></a><h2>Description</h2>
<p> <code class="command">PREPARE TRANSACTION</code> prepares the current transaction
for two-phase commit. After this command, the transaction is no longer
associated with the current session; instead, its state is fully stored on
disk, and there is a very high probability that it can be committed
successfully, even if a database crash occurs before the commit is
requested.
</p>
<p> Once prepared, a transaction can later be committed or rolled
back with <code class="command">COMMIT PREPARED</code> or
<code class="command">ROLLBACK PREPARED</code>, respectively. Those commands
can be issued from any session, not only the one that executed the
original transaction.
</p>
<p> From the point of view of the issuing session, <code class="command">PREPARE
TRANSACTION</code> is not unlike a <code class="command">ROLLBACK</code> command:
after executing it, there is no active current transaction, and the
effects of the prepared transaction are no longer visible. (The effects
will become visible again if the transaction is committed.)
</p>
<p> If the <code class="command">PREPARE TRANSACTION</code> command fails for any
reason, it becomes a <code class="command">ROLLBACK</code>: the current transaction
is canceled.
</p>
</div>
<div class="refsect1" lang="en">
<a name="id779796"></a><h2>Parameters</h2>
<div class="variablelist"><dl>
<dt><span class="term"><em class="replaceable"><code>transaction_id</code></em></span></dt>
<dd><p> An arbitrary identifier that later identifies this transaction for
<code class="command">COMMIT PREPARED</code> or <code class="command">ROLLBACK PREPARED</code>.
The identifier must be written as a string literal, and must be
less than 200 bytes long. It must not be the same as the identifier
used for any currently prepared transaction.
</p></dd>
</dl></div>
</div>
<div class="refsect1" lang="en">
<a name="id779831"></a><h2>Notes</h2>
<p> This command must be used inside a transaction block. Use
<code class="command">BEGIN</code> to start one.
</p>
<p> It is not currently allowed to <code class="command">PREPARE</code> a transaction that
has executed any operations involving temporary tables or
created any cursors <code class="literal">WITH HOLD</code>. Those features are too tightly
tied to the current session to be useful in a transaction to be prepared.
</p>
<p> If the transaction modified any run-time parameters with <code class="command">SET</code>,
those effects persist after <code class="command">PREPARE TRANSACTION</code>, and will not
be affected by any later <code class="command">COMMIT PREPARED</code> or
<code class="command">ROLLBACK PREPARED</code>. Thus, in this one respect
<code class="command">PREPARE TRANSACTION</code> acts more like <code class="command">COMMIT</code> than
<code class="command">ROLLBACK</code>.
</p>
<p> All currently available prepared transactions are listed in the
<code class="structname">pg_prepared_xacts</code> system view.
</p>
<p> From a performance standpoint, it is unwise to leave transactions in
the prepared state for a long time: this will for instance interfere with
the ability of <code class="command">VACUUM</code> to reclaim storage. Keep in mind also
that the transaction continues to hold whatever locks it held.
The intended
usage of the feature is that a prepared transaction will normally be
committed or rolled back as soon as an external transaction manager
has verified that other databases are also prepared to commit.
</p>
<p> If you make any serious use of prepared transactions, you will probably
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
quite small (to avoid wasting resources for those who don't use it).
It is recommendable to make it at least equal to
<a href="runtime-config-connection.html#guc-max-connections">max_connections</a>, so that every session can have
a prepared transaction pending.
</p>
</div>
<div class="refsect1" lang="en">
<a name="sql-prepare-transaction-examples"></a><h2>Examples</h2>
<p> Prepare the current transaction for two-phase commit, using
<code class="literal">foobar</code> as the transaction identifier:
</p>
<pre class="programlisting">PREPARE TRANSACTION 'foobar';</pre>
<p>
</p>
</div>
<div class="refsect1" lang="en">
<a name="id779997"></a><h2>See Also</h2>
<span class="simplelist"><a href="sql-commit-prepared.html">COMMIT PREPARED</a>, <a href="sql-rollback-prepared.html">ROLLBACK PREPARED</a></span>
</div>
</div></body>
</html>
|