(UPDATE:没有必要对这一手,我问PostgreSQL的邮件列表,并且事实证明,这种行为已经实施,由PSQL客户端ON_ERROR_ROLLBACK集)
为了详细说明西蒙回答(+1),在你的场景中,你可以在每个交互式查询之后添加一个保存点,并始终使用相同的名称(如果查询成功,它会覆盖前一个)。在发生错误的情况下,您将返回上次保存的状态并从此处继续。
这种工作模式的一个例子:
db=# select * from test_gral ;
i | t | n
---+------+------
1 | text | 10.0
(1 row)
db=# begin;
BEGIN
db=# insert into test_gral values (2,'xx',20); savepoint sp;
INSERT 0 1
SAVEPOINT
db=# insert into test_gral values (3,'xx',30); savepoint sp;
INSERT 0 1
SAVEPOINT
db=# insert into test_gralxx values (4,'xx',40); savepoint sp;
ERROR: relation "test_gralxx" does not exist
LINE 1: insert into test_gralxx values (4,'xx',40);
^
ERROR: current transaction is aborted, commands ignored until end of transaction block
db=# ROLLBACK TO SAVEPOINT sp;
ROLLBACK
db=# insert into test_gral values (4,'xx',40); savepoint sp;
INSERT 0 1
SAVEPOINT
db=# commit;
COMMIT
db=# select * from test_gral ;
i | t | n
---+------+------
1 | text | 10.0
2 | xx | 20
3 | xx | 30
4 | xx | 40
(4 rows)
ON_ERROR_ROLLBACK变量确实是我想要的:) – fmark 2010-05-01 06:20:03
'ON_ERROR_ROLLBACK'虽然看起来像psql客户端的一个功能,但不是postgres自身(所以你不能从数据库应用程序使用它,只能从命令行使用)。 – Glyph 2012-03-02 17:17:49
@Glyph:是的,这是psql的一项功能,它专门面向交互式使用 - 这是OP的场景。当期望的行为有意义时,我很难想到应用场景。 – leonbloy 2012-03-02 18:27:41