Postgresql 中文操作指南

PREPARE TRANSACTION

PREPARE TRANSACTION — 准备当前事务用于两阶段提交

PREPARE TRANSACTION — prepare the current transaction for two-phase commit

Synopsis

PREPARE TRANSACTION transaction_id

Description

PREPARE TRANSACTION 准备当前事务用于两阶段提交。此命令后,事务将不再与当前会话关联,而是会将状态完全存储在磁盘中,并且几率非常高,即使在请求提交之前发生数据库崩溃,也能成功地提交事务。

PREPARE TRANSACTION 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.

一旦准备就绪,稍后可以用 COMMIT PREPAREDROLLBACK PREPARED 分别提交或回滚事务。这些命令可以从任何会话发出,而不仅仅是由执行原始事务的会话发出。

Once prepared, a transaction can later be committed or rolled back with COMMIT PREPARED or ROLLBACK PREPARED, respectively. Those commands can be issued from any session, not only the one that executed the original transaction.

从发出事务的会话的角度来看, PREPARE TRANSACTIONROLLBACK 命令非常相似:执行该命令后,将没有处于活动状态的当前事务,并且已准备事务的效果也将不再可见。(提交事务后,效果将再次变得可见。)

From the point of view of the issuing session, PREPARE TRANSACTION is not unlike a ROLLBACK 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 TRANSACTION 命令由于任何原因而失败,则会变成 ROLLBACK :取消当前事务。

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

Parameters

  • transaction_id

    • An arbitrary identifier that later identifies this transaction for COMMIT PREPARED or ROLLBACK PREPARED. 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.

Notes

PREPARE TRANSACTION 并非旨在在应用程序或交互式会话中使用。其目的是允许外部事务管理器在多个数据库或其他事务资源上执行原子全局事务。除非您正在编写一个事务管理器,否则您可能不应该使用 PREPARE TRANSACTION

PREPARE TRANSACTION 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 PREPARE TRANSACTION.

此命令必须在事务块内使用。使用 BEGIN 以开启事务块。

This command must be used inside a transaction block. Use BEGIN to start one.

当前不允许 PREPARE 执行涉及临时表或会话的临时命名空间的任何操作、创建任何游标 WITH HOLD 或执行 LISTENUNLISTENNOTIFY 的事务。这些特性与当前会话联系得太紧密,没有办法将其用于需要准备的事务中。

It is not currently allowed to PREPARE a transaction that has executed any operations involving temporary tables or the session’s temporary namespace, created any cursors WITH HOLD, or executed LISTEN, UNLISTEN, or NOTIFY. Those features are too tightly tied to the current session to be useful in a transaction to be prepared.

如果事务使用 SET 对任何运行时参数进行修改(无 LOCAL 选项),则这些修改在 PREPARE TRANSACTION 后仍然存在,并且不受任何 COMMIT PREPAREDROLLBACK PREPARED 影响。因此,仅就这一点而言, PREPARE TRANSACTION 的行为更类似于 COMMIT 而不是 ROLLBACK

If the transaction modified any run-time parameters with SET (without the LOCAL option), those effects persist after PREPARE TRANSACTION, and will not be affected by any later COMMIT PREPARED or ROLLBACK PREPARED. Thus, in this one respect PREPARE TRANSACTION acts more like COMMIT than ROLLBACK.

所有当前可用的准备好的事务都会被列在 pg_prepared_xacts 系统视图中。

All currently available prepared transactions are listed in the pg_prepared_xacts system view.

Caution

将事务置于已准备状态并长时间放置是明智的做法。这将影响 VACUUM 回收存储的能力,在极端情况下,会导致数据库关闭以防止事务 ID 环绕(请参见 Section 25.1.5 )。另外请记住,事务将继续持有其持有的任何锁。该功能的预期用法是,在外部事务管理器验证其他数据库也准备提交后,准备好的事务通常会提交或回滚。

It is unwise to leave transactions in the prepared state for a long time. This will interfere with the ability of VACUUM to reclaim storage, and in extreme cases could cause the database to shut down to prevent transaction ID wraparound (see Section 25.1.5). 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.

如果您尚未设置外部事务管理器来跟踪已准备事务并确保其及时关闭,则最好通过将 max_prepared_transactions 设置为零来禁用已准备事务功能。这将防止意外创建已准备事务,避免忘记这些事务并最终导致问题。

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 max_prepared_transactions to zero. This will prevent accidental creation of prepared transactions that might then be forgotten and eventually cause problems.

Examples

使用 foobar 作为事务标识符,准备当前事务以进行两阶段提交:

Prepare the current transaction for two-phase commit, using foobar as the transaction identifier:

PREPARE TRANSACTION 'foobar';

Compatibility

PREPARE TRANSACTION 是 PostgreSQL 的扩展。旨在供外部事务管理系统使用,其中一些系统受标准(如 X/Open XA)的约束,但这些系统的 SQL 端未标准化。

PREPARE TRANSACTION is a PostgreSQL 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.