Home Explore Blog CI



postgresql

1st chunk of `doc/src/sgml/ref/prepare_transaction.sgml`
30f8de3dc080acaefcb5260ffb28fc8651280d753c12638e0000000100000cc0
<!--
doc/src/sgml/ref/prepare_transaction.sgml
PostgreSQL documentation
-->

<refentry id="sql-prepare-transaction">
 <indexterm zone="sql-prepare-transaction">
  <primary>PREPARE TRANSACTION</primary>
 </indexterm>

 <refmeta>
  <refentrytitle>PREPARE TRANSACTION</refentrytitle>
  <manvolnum>7</manvolnum>
  <refmiscinfo>SQL - Language Statements</refmiscinfo>
 </refmeta>

 <refnamediv>
  <refname>PREPARE TRANSACTION</refname>
  <refpurpose>prepare the current transaction for two-phase commit</refpurpose>
 </refnamediv>

 <refsynopsisdiv>
<synopsis>
PREPARE TRANSACTION <replaceable class="parameter">transaction_id</replaceable>
</synopsis>
 </refsynopsisdiv>

 <refsect1>
  <title>Description</title>

  <para>
   <command>PREPARE TRANSACTION</command> 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.
  </para>

  <para>
   Once prepared, a transaction can later be committed or rolled back
   with <link linkend="sql-commit-prepared"><command>COMMIT PREPARED</command></link>
   or <link linkend="sql-rollback-prepared"><command>ROLLBACK PREPARED</command></link>,
   respectively.  Those commands can be issued from any session, not
   only the one that executed the original transaction.
  </para>

  <para>
   From the point of view of the issuing session, <command>PREPARE
   TRANSACTION</command> is not unlike a <command>ROLLBACK</command> 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.)
  </para>

  <para>
   If the <command>PREPARE TRANSACTION</command> command fails for any
   reason, it becomes a <command>ROLLBACK</command>: the current transaction
   is canceled.
  </para>
 </refsect1>

 <refsect1>
  <title>Parameters</title>

  <variablelist>
   <varlistentry>
    <term><replaceable class="parameter">transaction_id</replaceable></term>
    <listitem>
     <para>
      An arbitrary identifier that later identifies this transaction for
      <command>COMMIT PREPARED</command> or <command>ROLLBACK PREPARED</command>.
      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.
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1>
  <title>Notes</title>

  <para>
   <command>PREPARE TRANSACTION</command> is not intended for use in applications
   or interactive sessions. Its purpose is to allow an external
   transaction manager to perform atomic global transactions across multiple
   databases or other transactional resources. Unless you're writing a
   transaction manager, you probably shouldn't be using <command>PREPARE
   TRANSACTION</command>.
  </para>

  <para>
   This command must be used inside a transaction block. Use <link
   linkend="sql-begin"><command>BEGIN</command></link>

Title: PREPARE TRANSACTION
Summary
The `PREPARE TRANSACTION` command prepares the current transaction for a two-phase commit. This stores the transaction's state on disk, ensuring a high probability of successful commit even if a database crash occurs. After preparation, the transaction is no longer associated with the current session and can be committed or rolled back using `COMMIT PREPARED` or `ROLLBACK PREPARED` from any session. If the command fails, it rolls back the current transaction. The `transaction_id` parameter is an identifier (string literal, <200 bytes, unique) used to identify the transaction for later commit or rollback. This command is primarily intended for external transaction managers performing atomic global transactions.