Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
sql-prepare-transaction.html90 linesDownload Raw Back to 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>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 16.3 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">3PREPARE 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 transaction6   for two-phase commit. After this command, the transaction is no longer7   associated with the current session; instead, its state is fully stored on8   disk, and there is a very high probability that it can be committed9   successfully, even if a database crash occurs before the commit is10   requested.11  </p><p>12   Once prepared, a transaction can later be committed or rolled back13   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, not16   only the one that executed the original transaction.17  </p><p>18   From the point of view of the issuing session, <code class="command">PREPARE19   TRANSACTION</code> is not unlike a <code class="command">ROLLBACK</code> command:20   after executing it, there is no active current transaction, and the21   effects of the prepared transaction are no longer visible.  (The effects22   will become visible again if the transaction is committed.)23  </p><p>24   If the <code class="command">PREPARE TRANSACTION</code> command fails for any25   reason, it becomes a <code class="command">ROLLBACK</code>: the current transaction26   is canceled.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 for29      <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 be31      less than 200 bytes long.  It must not be the same as the identifier32      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 applications35   or interactive sessions. Its purpose is to allow an external36   transaction manager to perform atomic global transactions across multiple37   databases or other transactional resources. Unless you're writing a38   transaction manager, you probably shouldn't be using <code class="command">PREPARE39   TRANSACTION</code>.40  </p><p>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.42  </p><p>43   It is not currently allowed to <code class="command">PREPARE</code> a transaction that44   has executed any operations involving temporary tables or the session's45   temporary namespace, created any cursors <code class="literal">WITH HOLD</code>, or46   executed <code class="command">LISTEN</code>, <code class="command">UNLISTEN</code>, or47   <code class="command">NOTIFY</code>.48   Those features are too tightly49   tied to the current session to be useful in a transaction to be prepared.50  </p><p>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 not54   be affected by any later <code class="command">COMMIT PREPARED</code> or55   <code class="command">ROLLBACK PREPARED</code>.  Thus, in this one respect56   <code class="command">PREPARE TRANSACTION</code> acts more like <code class="command">COMMIT</code> than57   <code class="command">ROLLBACK</code>.58  </p><p>59   All currently available prepared transactions are listed in the60   <a class="link" href="view-pg-prepared-xacts.html" title="54.16. pg_prepared_xacts"><code class="structname">pg_prepared_xacts</code></a>61   system view.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 reclaim65    storage, and in extreme cases could cause the database to shut down66    to prevent transaction ID wraparound (see <a class="xref" href="routine-vacuuming.html#VACUUM-FOR-WRAPAROUND" title="25.1.5. Preventing Transaction ID Wraparound Failures">Section 25.1.5</a>).  Keep in mind also that the transaction67    continues to hold whatever locks it held.  The intended usage of the68    feature is that a prepared transaction will normally be committed or69    rolled back as soon as an external transaction manager has verified that70    other databases are also prepared to commit.71   </p><p>72    If you have not set up an external transaction manager to track prepared73    transactions and ensure they get closed out promptly, it is best to keep74    the prepared-transaction feature disabled by setting75    <a class="xref" href="runtime-config-resource.html#GUC-MAX-PREPARED-TRANSACTIONS">max_prepared_transactions</a> to zero.  This will76    prevent accidental creation of prepared transactions that might then77    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, using80   <code class="literal">foobar</code> as the transaction identifier:81 82</p><pre class="programlisting">83PREPARE 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 a86   <span class="productname">PostgreSQL</span> extension.  It is intended for use by87   external transaction management systems, some of which are covered by88   standards (such as X/Open XA), but the SQL side of those systems is not89   standardized.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 16.3 Documentation">Home</a></td><td width="40%" align="right" valign="top"> REASSIGN OWNED</td></tr></table></div></body></html>
codekingpro/portable-devtools · Team Ai