16.5. PL/Python Extension for Autonomous Transactions #

In addition to the subtransaction method, PL/Python module provides the autonomous method that can be used in the 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;

Note

You cannot redefine isolation level for autonomous transactions within PL/Python blocks.

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