{"Entry":{"collection":"sql","key":"prepare-transaction","name":"PREPARE TRANSACTION","aliases":["prepare-transaction"],"metadata":{"aliases":["prepare-transaction"],"changed_in":[],"changes":[{"from":"8.0","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":[],"removed":[]},"status":"added","synopsis":null,"to":"8.1"},{"from":"8.2","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"8.3"},{"from":"8.3","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"8.4"},{"from":"8.4","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.0"},{"from":"9.1","purpose_changed":false,"renamed":null,"sections":{"added":["compatibility"],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.2"},{"from":"9.5","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"9.6"},{"from":"9.6","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"10"},{"from":"13","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"14"},{"from":"16","purpose_changed":false,"renamed":null,"sections":{"added":[],"changed":["notes"],"removed":[]},"status":"changed","synopsis":null,"to":"17"}],"content_hash":"256fe374959135923ceb94424629bec6aa016f5653b314ead2cb00d886f94d2e","editorial":{},"first_version":"8.1","group":"transaction","imported_at":"2026-09-30T17:43:39.395257+08:00","last_version":"20","name":"PREPARE TRANSACTION","object":"TRANSACTION","position":13012,"present_in":["8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6","10","11","12","13","14","15","16","17","18","19","20"],"purpose":"prepare the current transaction for two-phase commit","purpose_zh":"","related":["commit-prepared","rollback-prepared"],"slug":"prepare-transaction","source_rev":"a709ab85","synopsis":"PREPARE TRANSACTION transaction_id","verb":"PREPARE"}},"Definition":{"Collection":"sql","Key":"prepare-transaction","SourceDatabase":"center","Version":"18","SourceTable":"sqlcmd","SourceKey":"prepare-transaction","SourceRevision":"a709ab85","Facts":{"anchor":"SQL-PREPARE-TRANSACTION","file":"sql-prepare-transaction.html","lang":"en","name":"PREPARE TRANSACTION","purpose":"prepare the current transaction for two-phase commit","purpose_zh":"","related":["commit-prepared","rollback-prepared"],"sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e 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.\u003c/p\u003e\u003cp\u003eOnce prepared, a transaction can later be committed or rolled back with \u003ca href=\"/docs/18/sql-commit-prepared.html\" title=\"COMMIT PREPARED\"\u003e\u003ccode class=\"command\"\u003eCOMMIT PREPARED\u003c/code\u003e\u003c/a\u003e or \u003ca href=\"/docs/18/sql-rollback-prepared.html\" title=\"ROLLBACK PREPARED\"\u003e\u003ccode class=\"command\"\u003eROLLBACK PREPARED\u003c/code\u003e\u003c/a\u003e, respectively. Those commands can be issued from any session, not only the one that executed the original transaction.\u003c/p\u003e\u003cp\u003eFrom the point of view of the issuing session, \u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e is not unlike a \u003ccode class=\"command\"\u003eROLLBACK\u003c/code\u003e 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.)\u003c/p\u003e\u003cp\u003eIf the \u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e command fails for any reason, it becomes a \u003ccode class=\"command\"\u003eROLLBACK\u003c/code\u003e: the current transaction is canceled.\u003c/p\u003e","key":"description","title":"Description"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etransaction_id\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003eAn arbitrary identifier that later identifies this transaction for \u003ccode class=\"command\"\u003eCOMMIT PREPARED\u003c/code\u003e or \u003ccode class=\"command\"\u003eROLLBACK PREPARED\u003c/code\u003e. 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.\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"Parameters"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e 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 \u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eThis command must be used inside a transaction block. Use \u003ca href=\"/docs/18/sql-begin.html\" title=\"BEGIN\"\u003e\u003ccode class=\"command\"\u003eBEGIN\u003c/code\u003e\u003c/a\u003e to start one.\u003c/p\u003e\u003cp\u003eIt is not currently allowed to \u003ccode class=\"command\"\u003ePREPARE\u003c/code\u003e a transaction that has executed any operations involving temporary tables or the session's temporary namespace, created any cursors \u003ccode class=\"literal\"\u003eWITH HOLD\u003c/code\u003e, or executed \u003ccode class=\"command\"\u003eLISTEN\u003c/code\u003e, \u003ccode class=\"command\"\u003eUNLISTEN\u003c/code\u003e, or \u003ccode class=\"command\"\u003eNOTIFY\u003c/code\u003e. Those features are too tightly tied to the current session to be useful in a transaction to be prepared.\u003c/p\u003e\u003cp\u003eIf the transaction modified any run-time parameters with \u003ccode class=\"command\"\u003eSET\u003c/code\u003e (without the \u003ccode class=\"literal\"\u003eLOCAL\u003c/code\u003e option), those effects persist after \u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e, and will not be affected by any later \u003ccode class=\"command\"\u003eCOMMIT PREPARED\u003c/code\u003e or \u003ccode class=\"command\"\u003eROLLBACK PREPARED\u003c/code\u003e. Thus, in this one respect \u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e acts more like \u003ccode class=\"command\"\u003eCOMMIT\u003c/code\u003e than \u003ccode class=\"command\"\u003eROLLBACK\u003c/code\u003e.\u003c/p\u003e\u003cp\u003eAll currently available prepared transactions are listed in the \u003ca href=\"/docs/18/view-pg-prepared-xacts.html\" title=\"53.17. pg_prepared_xacts\"\u003e\u003ccode class=\"structname\"\u003epg_prepared_xacts\u003c/code\u003e\u003c/a\u003e system view.\u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3\u003eCaution\u003c/h3\u003e\u003cp\u003eIt is unwise to leave transactions in the prepared state for a long time. This will interfere with the ability of \u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e to reclaim storage, and in extreme cases could cause the database to shut down to prevent transaction ID wraparound (see \u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND\" title=\"24.1.5. Preventing Transaction ID Wraparound Failures\"\u003eSection 24.1.5\u003c/a\u003e). 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.\u003c/p\u003e\u003cp\u003eIf 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 \u003ca href=\"/docs/18/runtime-config-resource.html#GUC-MAX-PREPARED-TRANSACTIONS\"\u003emax_prepared_transactions\u003c/a\u003e to zero. This will prevent accidental creation of prepared transactions that might then be forgotten and eventually cause problems.\u003c/p\u003e\u003c/div\u003e","key":"notes","title":"Notes"},{"html":"\u003cp\u003ePrepare the current transaction for two-phase commit, using \u003ccode class=\"literal\"\u003efoobar\u003c/code\u003e as the transaction identifier:\u003c/p\u003e\u003cpre class=\"programlisting\"\u003ePREPARE TRANSACTION 'foobar';\n\u003c/pre\u003e","key":"examples","title":"Examples"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e is a \u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e 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.\u003c/p\u003e","key":"compatibility","title":"Compatibility"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/commit-prepared/?v=18\" title=\"COMMIT PREPARED\"\u003e\u003cspan class=\"refentrytitle\"\u003eCOMMIT PREPARED\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/rollback-prepared/?v=18\" title=\"ROLLBACK PREPARED\"\u003e\u003cspan class=\"refentrytitle\"\u003eROLLBACK PREPARED\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"See Also"}],"sections_same_as":"","slug":"18","synopsis_html":"PREPARE TRANSACTION \u003cem class=\"replaceable\"\u003e\u003ccode\u003etransaction_id\u003c/code\u003e\u003c/em\u003e","synopsis_text":"PREPARE TRANSACTION transaction_id"},"ManualEvidence":{},"MeasuredEvidence":{}},"Text":{"Collection":"sql","Key":"prepare-transaction","SourceDatabase":"pgweb","Version":"18","Locale":"zh-Hans","Title":"PREPARE TRANSACTION","Summary":"为两阶段提交准备当前事务","BodyHTML":"\u003cpre\u003ePREPARE TRANSACTION transaction_id\u003c/pre\u003e\u003csection\u003e\u003ch2\u003e描述\u003c/h2\u003e\u003cp\u003e\u003ccode\u003ePREPARE TRANSACTION\u003c/code\u003e为两阶段提交准备当前事务。执行此命令后，该事务将不再与当前会话相关联；相反，它的状态会被完整地存储到磁盘上，因此即使在请求提交之前数据库发生崩溃，它也极有可能成功提交。\u003c/p\u003e\u003cp\u003e事务进入预备状态后，稍后可以分别使用\u003ca href=\"/docs/18/sql-commit-prepared.html\" title=\"COMMIT PREPARED\" rel=\"nofollow\"\u003e\u003ccode\u003eCOMMIT PREPARED\u003c/code\u003e\u003c/a\u003e或\u003ca href=\"/docs/18/sql-rollback-prepared.html\" title=\"ROLLBACK PREPARED\" rel=\"nofollow\"\u003e\u003ccode\u003eROLLBACK PREPARED\u003c/code\u003e\u003c/a\u003e来提交或回滚。发出这些命令的不必是执行原始事务的那个会话，任何会话都可以这样做。\u003c/p\u003e\u003cp\u003e从发出该命令的会话来看，\u003ccode\u003ePREPARE TRANSACTION\u003c/code\u003e颇似\u003ccode\u003eROLLBACK\u003c/code\u003e：执行之后，将不再有活动的当前事务，并且该预备事务的效果也不再可见。（如果该事务随后被提交，这些效果会再次可见。）\u003c/p\u003e\u003cp\u003e如果\u003ccode\u003ePREPARE TRANSACTION\u003c/code\u003e因任何原因失败，它就等同于执行了一次\u003ccode\u003eROLLBACK\u003c/code\u003e：当前事务会被取消。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e参数\u003c/h2\u003e\u003cdiv\u003e\u003cdl\u003e\u003cdt\u003e\u003cspan\u003e\u003cem\u003e\u003ccode\u003etransaction_id\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e一个任意标识符，后续可通过它在\u003ccode\u003eCOMMIT PREPARED\u003c/code\u003e或\u003ccode\u003eROLLBACK PREPARED\u003c/code\u003e中标识该事务。该标识符必须写成字符串字面值，长度必须小于 200 字节，并且不能与当前任何已处于预备状态的事务标识符相同。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e注解\u003c/h2\u003e\u003cp\u003e\u003ccode\u003ePREPARE TRANSACTION\u003c/code\u003e并非供应用程序或交互式会话使用。它的目的是让外部事务管理器能够跨多个数据库或其他事务性资源执行原子性的全局事务。除非是在编写事务管理器，否则通常不应使用\u003ccode\u003ePREPARE TRANSACTION\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e该命令必须在事务块内部使用。请用\u003ca href=\"/docs/18/sql-begin.html\" title=\"BEGIN\" rel=\"nofollow\"\u003e\u003ccode\u003eBEGIN\u003c/code\u003e\u003c/a\u003e启动事务块。\u003c/p\u003e\u003cp\u003e当前不允许对执行过以下任一操作的事务执行\u003ccode\u003ePREPARE\u003c/code\u003e：涉及临时表或会话临时命名空间的操作、创建任何\u003ccode\u003eWITH HOLD\u003c/code\u003e游标，或执行过\u003ccode\u003eLISTEN\u003c/code\u003e、\u003ccode\u003eUNLISTEN\u003c/code\u003e或\u003ccode\u003eNOTIFY\u003c/code\u003e。这些特性与当前会话绑定得过于紧密，因此在要进入预备状态的事务中没有意义。\u003c/p\u003e\u003cp\u003e如果该事务曾用\u003ccode\u003eSET\u003c/code\u003e修改过任何运行时参数（且未使用\u003ccode\u003eLOCAL\u003c/code\u003e选项），那么这些效果在执行\u003ccode\u003ePREPARE TRANSACTION\u003c/code\u003e之后仍会保留，并且不会受到后续任何\u003ccode\u003eCOMMIT PREPARED\u003c/code\u003e或\u003ccode\u003eROLLBACK PREPARED\u003c/code\u003e的影响。因此，仅就这一点而言，\u003ccode\u003ePREPARE TRANSACTION\u003c/code\u003e的行为更像\u003ccode\u003eCOMMIT\u003c/code\u003e而不是\u003ccode\u003eROLLBACK\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e当前所有处于预备状态的事务都列在 \u003ca href=\"/docs/18/view-pg-prepared-xacts.html\" rel=\"nofollow\"\u003e\u003ccode\u003epg_prepared_xacts\u003c/code\u003e\u003c/a\u003e系统视图中。\u003c/p\u003e\u003cdiv\u003e\u003ch3\u003e小心\u003c/h3\u003e\u003cp\u003e让事务长时间停留在预备状态并不明智。这会妨碍\u003ccode\u003eVACUUM\u003c/code\u003e回收存储空间，在极端情况下甚至可能导致数据库为防止事务 ID 回卷而关闭（见\u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND\" rel=\"nofollow\"\u003e第 24.1.5 节\u003c/a\u003e）。还要记住，该事务会继续持有它原本持有的所有锁。此功能的预期用法是：一旦外部事务管理器确认其他数据库也已准备好提交，就尽快提交或回滚该预备事务。\u003c/p\u003e\u003cp\u003e如果没有设置外部事务管理器来跟踪预备事务并确保它们能被及时结束，最好将\u003ca href=\"/docs/18/runtime-config-resource.html#GUC-MAX-PREPARED-TRANSACTIONS\" rel=\"nofollow\"\u003emax_prepared_transactions\u003c/a\u003e设为零，从而禁用预备事务功能。这样可以防止意外创建预备事务，而这些事务随后可能被遗忘并最终引发问题。\u003c/p\u003e\u003c/div\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e示例\u003c/h2\u003e\u003cp\u003e为两阶段提交准备当前事务，并使用\u003ccode\u003efoobar\u003c/code\u003e作为事务标识符：\u003c/p\u003e\u003cpre\u003ePREPARE TRANSACTION \u0026#39;foobar\u0026#39;;\n\u003c/pre\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e兼容性\u003c/h2\u003e\u003cp\u003e\u003ccode\u003ePREPARE TRANSACTION\u003c/code\u003e是\u003cspan\u003ePostgreSQL\u003c/span\u003e扩展。它供外部事务管理系统使用，其中有些系统受标准约束（例如 X/Open XA），但这些系统的 SQL 侧并未标准化。\u003c/p\u003e\u003c/section\u003e\u003csection\u003e\u003ch2\u003e另见\u003c/h2\u003e\u003cspan\u003e\u003ca href=\"/wiki/sql/commit-prepared/?v=18\" title=\"COMMIT PREPARED\" rel=\"nofollow\"\u003e\u003cspan\u003eCOMMIT PREPARED\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/rollback-prepared/?v=18\" title=\"ROLLBACK PREPARED\" rel=\"nofollow\"\u003e\u003cspan\u003eROLLBACK PREPARED\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e\u003c/section\u003e","SourceRevision":"1b5ca64c","ContentHash":"5695d89f1af319f4968a9342088346cce11a29922f7f23e02fcc4f0970cdbf08","Payload":{"purpose_zh":"为两阶段提交准备当前事务","sections":[{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e为两阶段提交准备当前事务。执行此命令后，该事务将不再与当前会话相关联；相反，它的状态会被完整地存储到磁盘上，因此即使在请求提交之前数据库发生崩溃，它也极有可能成功提交。\u003c/p\u003e\u003cp\u003e事务进入预备状态后，稍后可以分别使用\u003ca href=\"/docs/18/sql-commit-prepared.html\" title=\"COMMIT PREPARED\"\u003e\u003ccode class=\"command\"\u003eCOMMIT PREPARED\u003c/code\u003e\u003c/a\u003e或\u003ca href=\"/docs/18/sql-rollback-prepared.html\" title=\"ROLLBACK PREPARED\"\u003e\u003ccode class=\"command\"\u003eROLLBACK PREPARED\u003c/code\u003e\u003c/a\u003e来提交或回滚。发出这些命令的不必是执行原始事务的那个会话，任何会话都可以这样做。\u003c/p\u003e\u003cp\u003e从发出该命令的会话来看，\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e颇似\u003ccode class=\"command\"\u003eROLLBACK\u003c/code\u003e：执行之后，将不再有活动的当前事务，并且该预备事务的效果也不再可见。（如果该事务随后被提交，这些效果会再次可见。）\u003c/p\u003e\u003cp\u003e如果\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e因任何原因失败，它就等同于执行了一次\u003ccode class=\"command\"\u003eROLLBACK\u003c/code\u003e：当前事务会被取消。\u003c/p\u003e","key":"description","title":"描述"},{"html":"\u003cdiv class=\"variablelist\"\u003e\u003cdl class=\"variablelist\"\u003e\u003cdt\u003e\u003cspan class=\"term\"\u003e\u003cem class=\"replaceable\"\u003e\u003ccode\u003etransaction_id\u003c/code\u003e\u003c/em\u003e\u003c/span\u003e\u003c/dt\u003e\u003cdd\u003e\u003cp\u003e一个任意标识符，后续可通过它在\u003ccode class=\"command\"\u003eCOMMIT PREPARED\u003c/code\u003e或\u003ccode class=\"command\"\u003eROLLBACK PREPARED\u003c/code\u003e中标识该事务。该标识符必须写成字符串字面值，长度必须小于 200 字节，并且不能与当前任何已处于预备状态的事务标识符相同。\u003c/p\u003e\u003c/dd\u003e\u003c/dl\u003e\u003c/div\u003e","key":"parameters","title":"参数"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e并非供应用程序或交互式会话使用。它的目的是让外部事务管理器能够跨多个数据库或其他事务性资源执行原子性的全局事务。除非是在编写事务管理器，否则通常不应使用\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e该命令必须在事务块内部使用。请用\u003ca href=\"/docs/18/sql-begin.html\" title=\"BEGIN\"\u003e\u003ccode class=\"command\"\u003eBEGIN\u003c/code\u003e\u003c/a\u003e启动事务块。\u003c/p\u003e\u003cp\u003e当前不允许对执行过以下任一操作的事务执行\u003ccode class=\"command\"\u003ePREPARE\u003c/code\u003e：涉及临时表或会话临时命名空间的操作、创建任何\u003ccode class=\"literal\"\u003eWITH HOLD\u003c/code\u003e游标，或执行过\u003ccode class=\"command\"\u003eLISTEN\u003c/code\u003e、\u003ccode class=\"command\"\u003eUNLISTEN\u003c/code\u003e或\u003ccode class=\"command\"\u003eNOTIFY\u003c/code\u003e。这些特性与当前会话绑定得过于紧密，因此在要进入预备状态的事务中没有意义。\u003c/p\u003e\u003cp\u003e如果该事务曾用\u003ccode class=\"command\"\u003eSET\u003c/code\u003e修改过任何运行时参数（且未使用\u003ccode class=\"literal\"\u003eLOCAL\u003c/code\u003e选项），那么这些效果在执行\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e之后仍会保留，并且不会受到后续任何\u003ccode class=\"command\"\u003eCOMMIT PREPARED\u003c/code\u003e或\u003ccode class=\"command\"\u003eROLLBACK PREPARED\u003c/code\u003e的影响。因此，仅就这一点而言，\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e的行为更像\u003ccode class=\"command\"\u003eCOMMIT\u003c/code\u003e而不是\u003ccode class=\"command\"\u003eROLLBACK\u003c/code\u003e。\u003c/p\u003e\u003cp\u003e当前所有处于预备状态的事务都列在 \u003ca href=\"/docs/18/view-pg-prepared-xacts.html\" title=\"53.17. pg_prepared_xacts\"\u003e\u003ccode class=\"structname\"\u003epg_prepared_xacts\u003c/code\u003e\u003c/a\u003e系统视图中。\u003c/p\u003e\u003cdiv class=\"caution\"\u003e\u003ch3\u003e小心\u003c/h3\u003e\u003cp\u003e让事务长时间停留在预备状态并不明智。这会妨碍\u003ccode class=\"command\"\u003eVACUUM\u003c/code\u003e回收存储空间，在极端情况下甚至可能导致数据库为防止事务 ID 回卷而关闭（见\u003ca href=\"/docs/18/routine-vacuuming.html#VACUUM-FOR-WRAPAROUND\" title=\"24.1.5. 防止事务 ID 回卷失败\"\u003e第 24.1.5 节\u003c/a\u003e）。还要记住，该事务会继续持有它原本持有的所有锁。此功能的预期用法是：一旦外部事务管理器确认其他数据库也已准备好提交，就尽快提交或回滚该预备事务。\u003c/p\u003e\u003cp\u003e如果没有设置外部事务管理器来跟踪预备事务并确保它们能被及时结束，最好将\u003ca href=\"/docs/18/runtime-config-resource.html#GUC-MAX-PREPARED-TRANSACTIONS\"\u003emax_prepared_transactions\u003c/a\u003e设为零，从而禁用预备事务功能。这样可以防止意外创建预备事务，而这些事务随后可能被遗忘并最终引发问题。\u003c/p\u003e\u003c/div\u003e","key":"notes","title":"注解"},{"html":"\u003cp\u003e为两阶段提交准备当前事务，并使用\u003ccode class=\"literal\"\u003efoobar\u003c/code\u003e作为事务标识符：\u003c/p\u003e\u003cpre class=\"programlisting\"\u003ePREPARE TRANSACTION 'foobar';\n\u003c/pre\u003e","key":"examples","title":"示例"},{"html":"\u003cp\u003e\u003ccode class=\"command\"\u003ePREPARE TRANSACTION\u003c/code\u003e是\u003cspan class=\"productname\"\u003ePostgreSQL\u003c/span\u003e扩展。它供外部事务管理系统使用，其中有些系统受标准约束（例如 X/Open XA），但这些系统的 SQL 侧并未标准化。\u003c/p\u003e","key":"compatibility","title":"兼容性"},{"html":"\u003cspan class=\"simplelist\"\u003e\u003ca href=\"/wiki/sql/commit-prepared/?v=18\" title=\"COMMIT PREPARED\"\u003e\u003cspan class=\"refentrytitle\"\u003eCOMMIT PREPARED\u003c/span\u003e\u003c/a\u003e, \u003ca href=\"/wiki/sql/rollback-prepared/?v=18\" title=\"ROLLBACK PREPARED\"\u003e\u003cspan class=\"refentrytitle\"\u003eROLLBACK PREPARED\u003c/span\u003e\u003c/a\u003e\u003c/span\u003e","key":"see_also","title":"另见"}],"sections_same_as":"","synopsis_html":"PREPARE TRANSACTION \u003cem class=\"replaceable\"\u003e\u003ccode\u003etransaction_id\u003c/code\u003e\u003c/em\u003e","synopsis_text":"PREPARE TRANSACTION transaction_id"}},"RequestedLocale":"zh-Hans","Fallback":false,"Versions":["10","11","12","13","14","15","16","17","18","19","20","8.1","8.2","8.3","8.4","9.0","9.1","9.2","9.3","9.4","9.5","9.6"],"Locales":["en","zh-Hans"],"Signatures":null,"Spellings":null,"SQLState":null,"Evidence":null}
