16.7. PL/Python Extension for Autonomous Transactions

In addition to subtransaction method, PL/Python module provides new autonomous method which can be used in WITH clause to start an autonomous transaction:

create or replace function pythonomous() returns void as $$
        plpy.execute("insert into atx_test values ('asd', 123)")

        try:
                with plpy.autonomous():
                        plpy.execute("insert into atx_test values ('bsd', 456)")
        except plpy.SPIError, e:
                print("error: %s" % e.args)

        plpy.execute("insert into atx_test values ('csd', 'csd')")
$$ language plpythonu;

Exception handling for autonomous transaction in PL/Python is done in the same way as for subtransactions.