バージョンごとのドキュメント一覧

PREPARE TRANSACTION

PREPARE TRANSACTION <refpurpose>prepare the current transaction for two-phase commit</refpurpose> — 二相コミット用に現在のトランザクションを準備する

概要

PREPARE TRANSACTION transaction_id

説明

<title>Description</title>

<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. PREPARE TRANSACTIONは、二相コミット用に現在のトランザクションを準備します。 このコマンドの後、トランザクションは現在のセッションと関連しなくなります。 トランザクションの状態は完全にディスク上に保存され、コミット要求前にデータベースがクラッシュしてしまったとしても、ほぼ確実に正常にコミットできるようになります。

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. 準備された後、そのトランザクションをCOMMIT PREPAREDによりコミット、あるいはROLLBACK PREPAREDによりロールバックすることができます。 元のトランザクションを実行したセッションだけではなく、任意のトランザクションからこれらのコマンドを発行することができます。

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.) コマンドを発行したセッションから見ると、PREPARE TRANSACTIONROLLBACKコマンドと似たような動作をします。 実行した後、実行中の現在のトランザクションはなくなり、準備したトランザクションの効果は不可視になります。 (そのトランザクションがコミットされた場合に効果が可視になります。)

If the <command>PREPARE TRANSACTION</command> command fails for any reason, it becomes a <command>ROLLBACK</command>: the current transaction is canceled. 何らかの原因でPREPARE TRANSACTIONコマンドが失敗した場合、ROLLBACKが行われます。 つまり、現在のトランザクションが取り消されます。

パラメータ

<title>Parameters</title>
transaction_id

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. 後でCOMMIT PREPAREDROLLBACK PREPAREDでトランザクションを識別するための任意の識別子です。 この識別子は文字列リテラルでなければなりません。また、200バイト未満でなければなりません。 また、その時点で準備されたトランザクションとして使用されている他の識別子と同じものは使用できません。

注釈

<title>Notes</title>

<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>. PREPARE TRANSACTIONはアプリケーションや対話式のセッションでの使用を目的としていません。 この目的は、外部トランザクションマネージャにより、複数のデータベースやその他のトランザクションを持つリソースを跨るグローバルなトランザクションを原子的に実現できるようにすることです。 トランザクションマネージャを作成しているのでなければ、おそらくPREPARE TRANSACTIONを使用するべきではありません。

This command must be used inside a transaction block. Use <link linkend="sql-begin"><command>BEGIN</command></link> to start one. このコマンドはトランザクションブロック内で使用しなければなりません。 トランザクションブロックを始めるには、BEGINを使用してください。

It is not currently allowed to <command>PREPARE</command> a transaction that has executed any operations involving temporary tables or the session's temporary namespace, created any cursors <literal>WITH HOLD</literal>, or executed <command>LISTEN</command>, <command>UNLISTEN</command>, or <command>NOTIFY</command>. Those features are too tightly tied to the current session to be useful in a transaction to be prepared. 現時点では、一時テーブルもしくはセッションの一時的な名前空間を含む操作を行ったトランザクション、カーソルをWITH HOLDで作成したトランザクション、LISTENUNLISTENまたはNOTIFYを実行したトランザクションをPREPAREさせることはできません。 準備したトランザクションで便利に使用するには、これらの機能は現在のセッションにあまりに強く結びついているためです。

If the transaction modified any run-time parameters with <command>SET</command> (without the <literal>LOCAL</literal> option), those effects persist after <command>PREPARE TRANSACTION</command>, and will not be affected by any later <command>COMMIT PREPARED</command> or <command>ROLLBACK PREPARED</command>. Thus, in this one respect <command>PREPARE TRANSACTION</command> acts more like <command>COMMIT</command> than <command>ROLLBACK</command>. トランザクションで何らかの実行時パラメータが(LOCALオプションなしの)SETで設定されている場合、その影響はPREPARE TRANSACTIONの後も残ります。 また、その後のCOMMIT PREPAREDROLLBACK PREPAREDの影響を受けません。 したがって、この意味では、PREPARE TRANSACTIONROLLBACKよりCOMMITと似た動きであるといえます。

All currently available prepared transactions are listed in the <link linkend="view-pg-prepared-xacts"><structname>pg_prepared_xacts</structname></link> system view. その時点で利用できるすべての準備されたトランザクションはpg_prepared_xactsシステムビューで列挙されます。

注意

It is unwise to leave transactions in the prepared state for a long time. This will interfere with the ability of <command>VACUUM</command> to reclaim storage, and in extreme cases could cause the database to shut down to prevent transaction ID wraparound (see <xref linkend="vacuum-for-wraparound"/>). 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. トランザクションを長期間準備された状態のままとすることは勧められません。 これは格納領域を回収するVACUUM機能を妨害し、極端な場合では、トランザクションの周回(25.1.5参照)を回避するためにデータベースを停止させてしまいます。 またトランザクションが保持しているすべてのロックを保持し続けていることにも注意してください。 この機能の想定している使用方法は、外部トランザクションマネージャが他のデータベースがコミットの準備をしたと検証した後すぐに、準備されたトランザクションは通常コミットまたはロールバックされることです。

If you have not set up an external transaction manager to track prepared transactions and ensure they get closed out promptly, it is best to keep the prepared-transaction feature disabled by setting <xref linkend="guc-max-prepared-transactions"/> to zero. This will prevent accidental creation of prepared transactions that might then be forgotten and eventually cause problems. 準備されたトランザクションを追跡し、それを即座に終了できるように外部トランザクションマネージャを設定していない場合、max_prepared_transactionsをゼロに設定して準備されたトランザクション機能を無効にしておくことが最善です。 こうすれば事故により準備されたトランザクションが作成され、それが忘れられて問題を引き起こすことを防止できます。

<title>Examples</title>

Prepare the current transaction for two-phase commit, using <literal>foobar</literal> as the transaction identifier: 二相コミット用に現在のトランザクションを準備します。 トランザクション識別子としてfoobarを使用します。

PREPARE TRANSACTION 'foobar';

互換性

<title>Compatibility</title>

<command>PREPARE TRANSACTION</command> is a <productname>PostgreSQL</productname> extension. It is intended for use by external transaction management systems, some of which are covered by standards (such as X/Open XA), but the SQL side of those systems is not standardized. PREPARE TRANSACTIONPostgreSQLの拡張です。 これは外部のトランザクション管理システムによる利用を意図したものです。 トランザクション管理システムの一部(X/Open XAなど)は標準化されていますが、こうしたシステムのSQL側は標準化されていません。

関連項目

<title>See Also</title> COMMIT PREPARED, ROLLBACK PREPARED