From 0c5933d0104c1788479592a84cca53da357381f9 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 2 Feb 2011 22:06:10 +0200 Subject: Wrap PL/Python SPI calls into subtransactions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This allows the language-specific try/catch construct to catch and handle exceptions arising from SPI calls, matching the behavior of other PLs. As an additional bonus you no longer get all the ugly "unrecognized error in PLy_spi_execute_query" errors. Jan UrbaƄski, reviewed by Steve Singer --- doc/src/sgml/plpython.sgml | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'doc/src') diff --git a/doc/src/sgml/plpython.sgml b/doc/src/sgml/plpython.sgml index befd6afb15e..e05c2937b1f 100644 --- a/doc/src/sgml/plpython.sgml +++ b/doc/src/sgml/plpython.sgml @@ -858,6 +858,9 @@ $$ LANGUAGE plpythonu; plpy.foo. + + Database Access Functions + The plpy module provides two functions called execute and @@ -937,6 +940,33 @@ CREATE FUNCTION usesavedplan() RETURNS trigger AS $$ $$ LANGUAGE plpythonu; + + + + + Trapping Errors + + + Functions accessing the database might encounter errors, which + will cause them to abort and raise an exception. Both + plpy.execute and + plpy.prepare can raise an instance of + plpy.SPIError, which by default will terminate + the function. This error can be handled just like any other + Python exception, by using the try/except + construct. For example: + +CREATE FUNCTION try_adding_joe() RETURNS text AS $$ + try: + plpy.execute("INSERT INTO users(username) VALUES ('joe')") + except plpy.SPIError: + return "something went wrong" + else: + return "Joe added" +$$ LANGUAGE plpythonu; + + + -- cgit v1.2.3