Postgresql 中文操作指南

RELEASE SAVEPOINT

RELEASE SAVEPOINT — 释放先前定义的保存点

RELEASE SAVEPOINT — release a previously defined savepoint

Synopsis

RELEASE [ SAVEPOINT ] savepoint_name

Description

RELEASE SAVEPOINT 释放指定的保存点,以及在这个指定保存点之后创建的所有活动保存点,并释放这些保存点的资源。从创建保存点到尚未撤销的所有更改都将合并到在创建指定保存点时处于活动状态的事务或保存点中。 RELEASE SAVEPOINT 之后进行的更改也将成为此活动事务或保存点的一部分。

RELEASE SAVEPOINT releases the named savepoint and all active savepoints that were created after the named savepoint, and frees their resources. All changes made since the creation of the savepoint that didn’t already get rolled back are merged into the transaction or savepoint that was active when the named savepoint was created. Changes made after RELEASE SAVEPOINT will also be part of this active transaction or savepoint.

Parameters

  • savepoint_name

    • The name of the savepoint to release.

Notes

指定尚未定义的保存点名称是一个错误。

Specifying a savepoint name that was not previously defined is an error.

当事务处于中止状态时,无法释放保存点;要执行此操作,请使用 ROLLBACK TO SAVEPOINT

It is not possible to release a savepoint when the transaction is in an aborted state; to do that, use ROLLBACK TO SAVEPOINT.

如果多个保存点具有相同的名称,则仅释放最近定义的未释放保存点。重复执行的命令将释放越来越旧的保存点。

If multiple savepoints have the same name, only the most recently defined unreleased one is released. Repeated commands will release progressively older savepoints.

Examples

建立并稍后释放保存点:

To establish and later release a savepoint:

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

以上事务将插入 3 和 4。

The above transaction will insert both 3 and 4.

一个包含多个嵌套子事务的更复杂的示例:

A more complex example with multiple nested subtransactions:

BEGIN;
    INSERT INTO table1 VALUES (1);
    SAVEPOINT sp1;
    INSERT INTO table1 VALUES (2);
    SAVEPOINT sp2;
    INSERT INTO table1 VALUES (3);
    RELEASE SAVEPOINT sp2;
    INSERT INTO table1 VALUES (4))); -- generates an error

在此示例中,应用程序要求释放保存点 sp2 ,该保存点插入了 3。这会将插入的事务上下文更改为 sp1 。当尝试插入值 4 的语句引发错误时,将丢失 2 和 4 的插入,因为它们处于同一个(当前已回滚的)保存点中,而值 3 处于同一个事务上下文中。应用程序现在只能选择这两个命令中的一个,因为它将忽略所有其他命令:

In this example, the application requests the release of the savepoint sp2, which inserted 3. This changes the insert’s transaction context to sp1. When the statement attempting to insert value 4 generates an error, the insertion of 2 and 4 are lost because they are in the same, now-rolled back savepoint, and value 3 is in the same transaction context. The application can now only choose one of these two commands, since all other commands will be ignored:

   ROLLBACK;
   ROLLBACK TO SAVEPOINT sp1;

选择 ROLLBACK 会中止所有内容(包括值 1),而 ROLLBACK TO SAVEPOINT sp1 会保留值 1 并允许事务继续。

Choosing ROLLBACK will abort everything, including value 1, whereas ROLLBACK TO SAVEPOINT sp1 will retain value 1 and allow the transaction to continue.

Compatibility

此命令符合 SQL 标准。该标准指定关键字 SAVEPOINT 为必需关键字,但 PostgreSQL 允许省略它。

This command conforms to the SQL standard. The standard specifies that the key word SAVEPOINT is mandatory, but PostgreSQL allows it to be omitted.