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

# BEGIN

### BEGIN

BEGIN — 開始一個交易區塊

### 語法

```

BEGIN [ WORK | TRANSACTION ] [ transaction_mode [, ...] ]

where transaction_mode is one of:

    ISOLATION LEVEL { SERIALIZABLE | REPEATABLE READ | READ COMMITTED | READ UNCOMMITTED }
    READ WRITE | READ ONLY
    [ NOT ] DEFERRABLE
```

### 說明

`BEGIN` 會開始一個交易區塊，也就是說，在 `BEGIN` 命令之後的所有陳述式都會在單一交易中執行，直到明確下達 [`COMMIT`](https://docs.postgresql.tw/19/reference/sql-commands/sql-commit.md) 或 [`ROLLBACK`](https://docs.postgresql.tw/19/reference/sql-commands/sql-rollback.md) 為止。預設情況下（沒有 `BEGIN` 時），PostgreSQL 會以「自動提交」（autocommit）模式執行交易，也就是說，每個陳述式都在自己的交易中執行，並在陳述式結束時隱含地執行提交（若執行成功；否則執行回復）。

在交易區塊中執行陳述式會比較快，因為交易的開始／提交需要可觀的 CPU 與磁碟活動。在一個交易內執行多個陳述式，也有助於在進行數項相關變更時確保一致性：其他工作階段將無法看到尚未完成所有相關更新的中間狀態。

若指定了隔離等級、讀寫模式或可延遲模式，新的交易就會具有這些特性，如同執行了 [`SET TRANSACTION`](https://docs.postgresql.tw/19/reference/sql-commands/sql-set-transaction.md) 一樣。

### 參數

`WORK`\
`TRANSACTION` : 可省略的關鍵字，沒有任何效果。

關於此陳述式其他參數的意義，請參閱 [SET TRANSACTION](https://docs.postgresql.tw/19/reference/sql-commands/sql-set-transaction.md)。

### 注意事項

[`START TRANSACTION`](https://docs.postgresql.tw/19/reference/sql-commands/sql-start-transaction.md) 具有與 `BEGIN` 相同的功能。

請使用 [`COMMIT`](https://docs.postgresql.tw/19/reference/sql-commands/sql-commit.md) 或 [`ROLLBACK`](https://docs.postgresql.tw/19/reference/sql-commands/sql-rollback.md) 結束交易區塊。

在已經位於交易區塊內時下達 `BEGIN`，會引發一則警告訊息。交易的狀態不受影響。若要在交易區塊內巢狀使用交易，請使用儲存點（請參閱 [SAVEPOINT](https://docs.postgresql.tw/19/reference/sql-commands/sql-savepoint.md)）。

基於向後相容性的理由，連續的 *`transaction_modes`* 之間的逗號可以省略。

### 範例

開始一個交易區塊：

```

BEGIN;
```

### 相容性

`BEGIN` 是 PostgreSQL 的語言擴充。它等同於 SQL 標準命令 [`START TRANSACTION`](https://docs.postgresql.tw/19/reference/sql-commands/sql-start-transaction.md)，該命令的參考頁面包含更多相容性資訊。

`DEFERRABLE` *`transaction_mode`* 是 PostgreSQL 的語言擴充。

順帶一提，`BEGIN` 關鍵字在嵌入式 SQL 中有不同的用途。移植資料庫應用程式時，建議你謹慎留意交易的語意。

### 另請參閱

[COMMIT](https://docs.postgresql.tw/19/reference/sql-commands/sql-commit.md), [ROLLBACK](https://docs.postgresql.tw/19/reference/sql-commands/sql-rollback.md), [START TRANSACTION](https://docs.postgresql.tw/19/reference/sql-commands/sql-start-transaction.md), [SAVEPOINT](https://docs.postgresql.tw/19/reference/sql-commands/sql-savepoint.md)

***

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


---

# 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/reference/sql-commands/sql-begin.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.
