ROLLBACK TO SAVEPOINT
是一个事务命令,用于将事务回滚到之前定义的 savepoint
。
ROLLBACK TO SAVEPOINT
添加于 PostgreSQL 8.0。
更改历史记录
- PostgreSQL 8.0
- 添加(初始提交 cc813fc2)
示例
ROLLBACK TO SAVEPOINT
的基本用法
postgres=# BEGIN; BEGIN postgres=*# SELECT 1; ?column? ---------- 1 (1 row) postgres=*# SAVEPOINT s1; SAVEPOINT postgres=*# SELECT foo; ERROR: column "foo" does not exist LINE 1: SELECT foo; ^ postgres=!# ROLLBACK TO SAVEPOINT s1; ROLLBACK postgres=*# SELECT 2; ?column? ---------- 2 (1 row) postgres=*# COMMIT; COMMIT
可以多次回滚到同一个保存点
postgres=*# SAVEPOINT s1; SAVEPOINT postgres=*# ROLLBACK TO SAVEPOINT s1; ROLLBACK postgres=*# ROLLBACK TO SAVEPOINT s1; ROLLBACK
无法回滚到之前已释放的保存点
postgres=*# RELEASE SAVEPOINT s1; RELEASE postgres=*# ROLLBACK TO SAVEPOINT s1; ERROR: savepoint "s1" does not exist
尝试回滚到不存在的保存点
postgres=*# ROLLBACK TO SAVEPOINT foo; ERROR: savepoint "foo" does not exist
参考文献
- PostgreSQL 文档: ROLLBACK TO SAVEPOINT