Database Reference
In-Depth Information
plpy.execute('SAVEPOINT foo;')
plpy.execute('insert into ttable values(2)')
plpy.execute('fail!')
except:
pass
plpy.execute('insert into ttable values(3)')
$$ LANGUAGE plpythonu;
hannu=# select syntax_error_rollback_test()
syntax_error_rollback_test
---------------------------
(1 row)
When the SAVEPOINT foo; command is executed in PL/Python, an SQL error will
not cause full "ROLLBACK;" but an equivalent of "ROLLBACK TO SAVEPOINT
foo;" , so only the effects of commands between SAVEPOINT and the error are
rolled back:
hannu=# select * from ttable ;
id
----
1
3
(2 rows)
In Version 9.1, there are two important changes in how PostgreSQL exceptions
are handled. If no SAVEPOINT or subtransaction is used, each invocation of
plpy.prepare() and plpy.execute() is run in it's own subtransaction, so that
an error will only rollback this subtransaction and not all of the current transaction.
Since using a separate subtransactions for each database interaction involves extra
costs, and you may want to control the subtransaction boundaries anyway, a new
Python context manager, plpy.subtransaction() , is provided.
For an explanation of Python's context managers, refer to http://docs.python.org/lib-
rary/stdtypes.html#context-manager-types so that you can use the with statement in
Search WWH ::




Custom Search