Can I ask Postgresql to ignore errors within a transaction Can I ask Postgresql to ignore errors within a transaction postgresql postgresql

Can I ask Postgresql to ignore errors within a transaction


(UPDATE: No need to to this by hand, I asked in the postgresql mailing lists, and it turned that this behaviour is already implemented, by the ON_ERROR_ROLLBACK set in the psql client)

To elaborate on Simon's answer (+1) , in your scenario you could rutinarily add a savepoint after each interactive query, always with the same name (it ovewrites the previous if the query is succesfull). In the case of error, you go back to the last saved one and continue from there.

An example of this working pattern:

db=# select * from test_gral ; i |  t   |  n---+------+------ 1 | text | 10.0(1 row)db=# begin;BEGINdb=#  insert into test_gral values (2,'xx',20); savepoint sp;INSERT 0 1SAVEPOINTdb=#  insert into test_gral values (3,'xx',30); savepoint sp;INSERT 0 1SAVEPOINTdb=#  insert into test_gralxx values (4,'xx',40); savepoint sp;ERROR:  relation "test_gralxx" does not existLINE 1: insert into test_gralxx values (4,'xx',40);                    ^ERROR:  current transaction is aborted, commands ignored until end of transaction blockdb=# ROLLBACK TO SAVEPOINT sp;ROLLBACKdb=#  insert into test_gral values (4,'xx',40); savepoint sp;INSERT 0 1SAVEPOINTdb=# commit;COMMITdb=# select * from test_gral ; i |  t   |  n---+------+------ 1 | text | 10.0 2 | xx   |   20 3 | xx   |   30 4 | xx   |   40(4 rows)


Switching that off is not possible however you can use something different. There is something like savepoint:

http://www.postgresql.org/docs/8.4/interactive/sql-savepoint.html

so you can rollback your transaction to some earlier point without rolling black the whole transaction.


It's possible to write a function that takes a string argument, executes it, and uses an exception clause in order to not abort your transaction, but it's a huge pain to then have to call that function for each statement you wish to execute.