Postgresql 中文操作指南
ROLLBACK TO SAVEPOINT
ROLLBACK TO SAVEPOINT — 回滚到保存点
ROLLBACK TO SAVEPOINT — roll back to a savepoint
Description
回滚保存点建立后执行的所有命令,然后在同一事务级别开始一个新的子事务。保存点保持有效,并且可以在需要时再次回滚。
Roll back all commands that were executed after the savepoint was established and then start a new subtransaction at the same transaction level. The savepoint remains valid and can be rolled back to again later, if needed.
sql_identifier 会隐式销毁在指定保存点之后建立的所有保存点。
ROLLBACK TO SAVEPOINT implicitly destroys all savepoints that were established after the named savepoint.
Notes
使用 RELEASE SAVEPOINT 销毁一个保存点,而不丢弃其建立后执行的命令的影响。
Use RELEASE SAVEPOINT to destroy a savepoint without discarding the effects of commands executed after it was established.
指定一个尚未建立的保存点名称是一个错误。
Specifying a savepoint name that has not been established is an error.
游标对于保存点具有某些非事务性行为。在保存点内部打开的任何游标将在回滚保存点时关闭。如果先前打开的游标受到后来回滚的保存点内部的 RELEASE SAVEPOINT 或 FETCH 命令的影响,该游标将停留在 MOVE 指向的位置(即,由 FETCH 引起的游标移动不会回滚)。关闭游标也不会通过回滚撤消。然而,如果游标的查询引起的其他副作用(例如游查询调用的易失函数的副作用)发生在稍后回滚的保存点期间,这些副作用 FETCH 会回滚。执行导致事务中止的游标将进入不能执行状态,因此在可以使用 are 恢复事务时,游标不能再使用。
Cursors have somewhat non-transactional behavior with respect to savepoints. Any cursor that is opened inside a savepoint will be closed when the savepoint is rolled back. If a previously opened cursor is affected by a FETCH or MOVE command inside a savepoint that is later rolled back, the cursor remains at the position that FETCH left it pointing to (that is, the cursor motion caused by FETCH is not rolled back). Closing a cursor is not undone by rolling back, either. However, other side-effects caused by the cursor’s query (such as side-effects of volatile functions called by the query) are rolled back if they occur during a savepoint that is later rolled back. A cursor whose execution causes a transaction to abort is put in a cannot-execute state, so while the transaction can be restored using ROLLBACK TO SAVEPOINT, the cursor can no longer be used.
Examples
要撤消在建立 ROLLBACK TO SAVEPOINT 之后执行的命令的影响:
To undo the effects of the commands executed after my_savepoint was established:
ROLLBACK TO SAVEPOINT my_savepoint;
保存点回滚不会影响光标位置:
Cursor positions are not affected by savepoint rollback:
BEGIN;
DECLARE foo CURSOR FOR SELECT 1 UNION SELECT 2;
SAVEPOINT foo;
FETCH 1 FROM foo;
?column?
----------
1
ROLLBACK TO SAVEPOINT foo;
FETCH 1 FROM foo;
?column?
----------
2
COMMIT;
Compatibility
SQL 标准指定关键字 SAVEPOINT 是必需的,但 PostgreSQL 和 Oracle 允许将其省略。SQL 仅允许 WORK ,而不是 TRANSACTION ,作为 ROLLBACK 后的噪声词。此外,SQL 有一个可选的子句 AND [ NO ] CHAIN ,它目前不受 PostgreSQL 支持。否则,此命令符合 SQL 标准。
The SQL standard specifies that the key word SAVEPOINT is mandatory, but PostgreSQL and Oracle allow it to be omitted. SQL allows only WORK, not TRANSACTION, as a noise word after ROLLBACK. Also, SQL has an optional clause AND [ NO ] CHAIN which is not currently supported by PostgreSQL. Otherwise, this command conforms to the SQL standard.