> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/internals/transactions/two-phase.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 67.4. 兩階段交易

### 67.4. 兩階段交易 [#](#two-phase) <a href="#two-phase" id="two-phase"></a>

PostgreSQL 支援兩階段提交（2PC）協定，讓多個分散式系統以交易方式共同運作。相關命令為 `PREPARE TRANSACTION`、`COMMIT PREPARED` 與 `ROLLBACK PREPARED`。兩階段交易是供外部交易管理系統使用的。PostgreSQL 遵循 X/Open XA 標準提出的功能與模型，但未實作部分較少使用的項目。

使用者執行 `PREPARE TRANSACTION` 後，接下來能用來處理該交易的命令只有 `COMMIT PREPARED` 或 `ROLLBACK PREPARED`。一般而言，這種已備妥狀態應該只持續很短的時間，但外部可用性問題可能使交易長時間停留在此狀態。短暫存在的已備妥交易只儲存在共享記憶體與 WAL 中。跨越檢查點的交易會記錄在 `pg_twophase` 目錄中。目前已備妥的交易可以使用 [`pg_prepared_xacts`](https://docs.postgresql.tw/19/internals/views/view-pg-prepared-xacts.md) 檢視。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/two-phase.html)（原文版本：18.6；核對日期：2026-09-07）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/internals/transactions/two-phase.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
