> 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/client-interfaces/ecpg/ecpg-commands/ecpg-transactions.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 34.3.3. 管理交易

### 34.3.3. 管理交易 [#](#ecpg-transactions) <a href="#ecpg-transactions" id="ecpg-transactions"></a>

在預設模式下，陳述式只有在發出 `EXEC SQL COMMIT` 時才會提交。嵌入式 SQL 介面也支援交易的自動提交（類似 psql 的預設行為），可以透過 `ecpg` 的 `-t` 命令列選項（請參閱 [ecpg](https://docs.postgresql.tw/19/reference/reference-client/app-ecpg.md)），或透過 `EXEC SQL SET AUTOCOMMIT TO ON` 陳述式啟用。在自動提交模式下，除非命令位於明確的交易區塊內，否則每個命令都會自動提交。此模式可以使用 `EXEC SQL SET AUTOCOMMIT TO OFF` 明確關閉。

可以使用下列交易管理命令：

#### `EXEC SQL COMMIT` [#](#ecpg-transactions-exec-sql-commit) <a href="#ecpg-transactions-exec-sql-commit" id="ecpg-transactions-exec-sql-commit"></a>

提交進行中的交易。

#### `EXEC SQL ROLLBACK` [#](#ecpg-transactions-exec-sql-rollback) <a href="#ecpg-transactions-exec-sql-rollback" id="ecpg-transactions-exec-sql-rollback"></a>

回復進行中的交易。

#### `EXEC SQL PREPARE TRANSACTION` *`transaction_id`* [#](#ecpg-transactions-exec-sql-prepare-transaction) <a href="#ecpg-transactions-exec-sql-prepare-transaction" id="ecpg-transactions-exec-sql-prepare-transaction"></a>

為兩階段提交準備目前的交易。

#### `EXEC SQL COMMIT PREPARED` *`transaction_id`* [#](#ecpg-transactions-exec-sql-commit-prepared) <a href="#ecpg-transactions-exec-sql-commit-prepared" id="ecpg-transactions-exec-sql-commit-prepared"></a>

提交處於已準備狀態的交易。

#### `EXEC SQL ROLLBACK PREPARED` *`transaction_id`* [#](#ecpg-transactions-exec-sql-rollback-prepared) <a href="#ecpg-transactions-exec-sql-rollback-prepared" id="ecpg-transactions-exec-sql-rollback-prepared"></a>

回復處於已準備狀態的交易。

#### `EXEC SQL SET AUTOCOMMIT TO ON` [#](#ecpg-transactions-exec-sql-autocommit-on) <a href="#ecpg-transactions-exec-sql-autocommit-on" id="ecpg-transactions-exec-sql-autocommit-on"></a>

啟用自動提交模式。

#### `EXEC SQL SET AUTOCOMMIT TO OFF` [#](#ecpg-transactions-exec-sql-autocommit-off) <a href="#ecpg-transactions-exec-sql-autocommit-off" id="ecpg-transactions-exec-sql-autocommit-off"></a>

停用自動提交模式。這是預設行為。

[返回 34.3. 執行 SQL 命令](https://docs.postgresql.tw/19/client-interfaces/ecpg/ecpg-commands.md#ecpg-commands)。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/ecpg-commands.html)（原文版本：18.6；核對日期：2026-10-22）


---

# 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/client-interfaces/ecpg/ecpg-commands/ecpg-transactions.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.
