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

# 45.4. 交易管理

### 45.4. 交易管理 [#](#spi-transaction) <a href="#spi-transaction" id="spi-transaction"></a>

[SPI\_commit](https://docs.postgresql.tw/19/server-programming/spi/spi-transaction/spi-spi-commit.md) — 提交目前交易

[SPI\_rollback](https://docs.postgresql.tw/19/server-programming/spi/spi-transaction/spi-spi-rollback.md) — 中止目前交易

[SPI\_start\_transaction](https://docs.postgresql.tw/19/server-programming/spi/spi-transaction/spi-spi-start-transaction.md) — 已淘汰函式

無法透過 `SPI_execute` 等 SPI 函式執行 `COMMIT` 與 `ROLLBACK` 等交易控制命令。不過，另有獨立介面函式允許透過 SPI 控制交易。

若不考量呼叫情境，在任意使用者定義的 SQL 可呼叫函式中開始及結束交易，通常既不安全也不合理。舉例來說，在 SQL 命令中複雜 SQL 運算式的一部分函式中間設置交易邊界，可能造成難以理解的內部錯誤或當機。此處介紹的介面函式主要供程序語言實作使用，以支援由 `CALL` 命令呼叫的 SQL 層級程序中的交易管理，並考量 `CALL` 呼叫情境。以 C 實作的使用 SPI 程序可實作相同邏輯，但細節超出本文範圍。

***

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


---

# 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/server-programming/spi/spi-transaction.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.
