<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >PREPARE TRANSACTION</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 8.3.1 Documentation" HREF="index.html"><LINK REL="UP" TITLE="SQL Commands" HREF="sql-commands.html"><LINK REL="PREVIOUS" TITLE="PREPARE" HREF="sql-prepare.html"><LINK REL="NEXT" TITLE="REASSIGN OWNED" HREF="sql-reassign-owned.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"><META NAME="creation" CONTENT="2008-03-15T03:19:10"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 8.3.1 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-prepare.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="sql-prepare.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-reassign-owned.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="sql-reassign-owned.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="SQL-PREPARE-TRANSACTION" ></A >PREPARE TRANSACTION</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN58712" ></A ><H2 >Name</H2 >PREPARE TRANSACTION -- prepare the current transaction for two-phase commit</DIV ><A NAME="AEN58715" ></A ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN58717" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >PREPARE TRANSACTION <TT CLASS="REPLACEABLE" ><I >transaction_id</I ></TT ></PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN58720" ></A ><H2 >Description</H2 ><P > <TT CLASS="COMMAND" >PREPARE TRANSACTION</TT > 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 <A HREF="sql-commit-prepared.html" ><I >COMMIT PREPARED</I ></A > or <A HREF="sql-rollback-prepared.html" ><I >ROLLBACK PREPARED</I ></A >, 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, <TT CLASS="COMMAND" >PREPARE TRANSACTION</TT > is not unlike a <TT CLASS="COMMAND" >ROLLBACK</TT > 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 <TT CLASS="COMMAND" >PREPARE TRANSACTION</TT > command fails for any reason, it becomes a <TT CLASS="COMMAND" >ROLLBACK</TT >: the current transaction is canceled. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN58733" ></A ><H2 >Parameters</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="REPLACEABLE" ><I >transaction_id</I ></TT ></DT ><DD ><P > An arbitrary identifier that later identifies this transaction for <TT CLASS="COMMAND" >COMMIT PREPARED</TT > or <TT CLASS="COMMAND" >ROLLBACK PREPARED</TT >. 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" ><A NAME="AEN58743" ></A ><H2 >Notes</H2 ><P > This command must be used inside a transaction block. Use <A HREF="sql-begin.html" ><I >BEGIN</I ></A > to start one. </P ><P > It is not currently allowed to <TT CLASS="COMMAND" >PREPARE</TT > a transaction that has executed any operations involving temporary tables, created any cursors <TT CLASS="LITERAL" >WITH HOLD</TT >, or executed <TT CLASS="COMMAND" >LISTEN</TT > or <TT CLASS="COMMAND" >UNLISTEN</TT >. 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 <TT CLASS="COMMAND" >SET</TT > (without the <TT CLASS="LITERAL" >LOCAL</TT > option), those effects persist after <TT CLASS="COMMAND" >PREPARE TRANSACTION</TT >, and will not be affected by any later <TT CLASS="COMMAND" >COMMIT PREPARED</TT > or <TT CLASS="COMMAND" >ROLLBACK PREPARED</TT >. Thus, in this one respect <TT CLASS="COMMAND" >PREPARE TRANSACTION</TT > acts more like <TT CLASS="COMMAND" >COMMIT</TT > than <TT CLASS="COMMAND" >ROLLBACK</TT >. </P ><P > All currently available prepared transactions are listed in the <A HREF="view-pg-prepared-xacts.html" ><TT CLASS="STRUCTNAME" >pg_prepared_xacts</TT ></A > 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 <TT CLASS="COMMAND" >VACUUM</TT > 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" ><A NAME="SQL-PREPARE-TRANSACTION-EXAMPLES" ></A ><H2 >Examples</H2 ><P > Prepare the current transaction for two-phase commit, using <TT CLASS="LITERAL" >foobar</TT > as the transaction identifier: </P><PRE CLASS="PROGRAMLISTING" >PREPARE TRANSACTION 'foobar';</PRE ><P> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN58774" ></A ><H2 >See Also</H2 ><A HREF="sql-commit-prepared.html" ><I >COMMIT PREPARED</I ></A >, <A HREF="sql-rollback-prepared.html" ><I >ROLLBACK PREPARED</I ></A ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="sql-prepare.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="sql-reassign-owned.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >PREPARE</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="sql-commands.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >REASSIGN OWNED</TD ></TR ></TABLE ></DIV ></BODY ></HTML >