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

# SAVEPOINT

### SAVEPOINT

SAVEPOINT — 在目前交易中定義一個新的儲存點

### 語法

```

SAVEPOINT savepoint_name
```

### 說明

`SAVEPOINT` 會在目前交易中建立一個新的儲存點。

儲存點是交易內的一個特殊標記，讓在建立之後執行的所有命令 都能被回復，將交易狀態還原成建立該儲存點時的狀態。

### 參數

*`savepoint_name`* : 要賦予新儲存點的名稱。若已存在同名的儲存點， 在較新的同名儲存點被釋放之前，這些同名儲存點都將無法存取。

### 注意事項

使用 [`ROLLBACK TO`](https://docs.postgresql.tw/19/reference/sql-commands/sql-rollback-to.md) 可回復到某個儲存點。使用 [`RELEASE SAVEPOINT`](https://docs.postgresql.tw/19/reference/sql-commands/sql-release-savepoint.md) 可銷毀某個儲存點，同時保留該儲存點建立之後所執行命令的效果。

儲存點只能在交易區塊內建立。一個交易內可以定義多個儲存點。

### 範例

建立一個儲存點，之後再復原自其建立以來所有命令的效果：

```

BEGIN;
    INSERT INTO table1 VALUES (1);
    SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (2);
    ROLLBACK TO SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (3);
COMMIT;
```

上述交易會插入 1 與 3，但不會插入 2。

建立一個儲存點，之後再將其銷毀：

```

BEGIN;
    INSERT INTO table1 VALUES (3);
    SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (4);
    RELEASE SAVEPOINT my_savepoint;
COMMIT;
```

上述交易會插入 3 與 4 兩者。

使用單一儲存點名稱：

```

BEGIN;
    INSERT INTO table1 VALUES (1);
    SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (2);
    SAVEPOINT my_savepoint;
    INSERT INTO table1 VALUES (3);

    -- rollback to the second savepoint
    ROLLBACK TO SAVEPOINT my_savepoint;
    SELECT * FROM table1;               -- shows rows 1 and 2

    -- release the second savepoint
    RELEASE SAVEPOINT my_savepoint;

    -- rollback to the first savepoint
    ROLLBACK TO SAVEPOINT my_savepoint;
    SELECT * FROM table1;               -- shows only row 1
COMMIT;
```

上述交易顯示值為 3 的資料列先被回復，接著值為 2 的資料列也被回復。

### 相容性

SQL 標準要求當另一個同名的儲存點被建立時，原本的儲存點必須自動銷毀。 在 PostgreSQL 中，舊的儲存點會被保留，只是在回復或釋放時只會用到 較新的那一個。（以 `RELEASE SAVEPOINT` 釋放較新的儲存點後， 較舊的儲存點就會再次可供 `ROLLBACK TO SAVEPOINT` 與 `RELEASE SAVEPOINT` 使用。）除此之外，`SAVEPOINT` 完全符合 SQL 標準。

### 另請參閱

[BEGIN](https://docs.postgresql.tw/19/reference/sql-commands/sql-begin.md)、[COMMIT](https://docs.postgresql.tw/19/reference/sql-commands/sql-commit.md)、[RELEASE SAVEPOINT](https://docs.postgresql.tw/19/reference/sql-commands/sql-release-savepoint.md)、[ROLLBACK](https://docs.postgresql.tw/19/reference/sql-commands/sql-rollback.md)、[ROLLBACK TO SAVEPOINT](https://docs.postgresql.tw/19/reference/sql-commands/sql-rollback-to.md)

***

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


---

# 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-savepoint.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.
