嵌套事务的实现是基于SAVEPOINT、ROLLBACK TO SAVEPOINT和RELEASE SAVEPOINT的,也就是设置一个保存点,能够回滚到保存点和释放保存点。html
测试表的初始状态以下:sql
postgres=# \d test Table "public.test" Column | Type | Modifiers --------+---------+----------- id | integer | name | text | postgres=# select * from test ; id | name ----+------ (0 rows)
开始测试post
postgres=# begin ; BEGIN postgres=# insert into test values (1, 'a'); INSERT 0 1 postgres=# savepoint insert_a; SAVEPOINT postgres=# select * from test ; id | name ----+------ 1 | a (1 row) postgres=# insert into test values (2, 'b'); INSERT 0 1 postgres=# savepoint insert_b; SAVEPOINT postgres=# select * from test ; id | name ----+------ 1 | a 2 | b (2 rows) postgres=# insert into test values (3, 'c'); INSERT 0 1 postgres=# select * from test ; id | name ----+------ 1 | a 2 | b 3 | c (3 rows)
如今定义了两个SAVEPOINT,而且插入了3条数据,如今测试ROLLBACK TO SAVEPOINT测试
postgres=# rollback to insert_b; ROLLBACK postgres=# select * from test ; id | name ----+------ 1 | a 2 | b (2 rows) postgres=# rollback to insert_a; ROLLBACK postgres=# select * from test ; id | name ----+------ 1 | a (1 row)
可见回滚到前面定义的保存点成功了。spa
若是回滚到前面的保存点,后面的更改就丢失了,包括保存点,好比回滚到insert_a,那么在insert_a以后的数据就没有了,insert_b这个保存点也不存在了。postgresql
postgres=# rollback to insert_a; ROLLBACK postgres=# select * from test ; id | name ----+------ 1 | a (1 row) postgres=# rollback to insert_b; ERROR: no such savepoint
测试RELEASE SAVEPOINTcode
postgres=# select * from test ; id | name ----+------ (0 rows) postgres=# begin ; BEGIN postgres=# insert into test values (1, 'a'); INSERT 0 1 postgres=# savepoint insert_a; SAVEPOINT postgres=# select * from test ; id | name ----+------ 1 | a (1 row) postgres=# insert into test values (2, 'b'); INSERT 0 1 postgres=# savepoint insert_b; SAVEPOINT postgres=# select * from test ; id | name ----+------ 1 | a 2 | b (2 rows) postgres=# release insert_a; RELEASE postgres=# select * from test ; id | name ----+------ 1 | a 2 | b (2 rows) postgres=# rollback to insert_a; ERROR: no such savepoint
保存点被释放后就不能再回滚到该保存点了。htm