Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id C84801337B3E for ; Fri, 19 Nov 2010 04:35:49 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 22039-04 for ; Fri, 19 Nov 2010 08:35:43 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail-iw0-f174.google.com (mail-iw0-f174.google.com [209.85.214.174]) by mail.postgresql.org (Postfix) with ESMTP id 09E761335B36 for ; Fri, 19 Nov 2010 04:35:41 -0400 (AST) Received: by iwn35 with SMTP id 35so4556008iwn.19 for ; Fri, 19 Nov 2010 00:35:41 -0800 (PST) Received: by 10.231.16.75 with SMTP id n11mr1764991iba.97.1290155741526; Fri, 19 Nov 2010 00:35:41 -0800 (PST) MIME-Version: 1.0 Received: by 10.231.15.205 with HTTP; Fri, 19 Nov 2010 00:35:11 -0800 (PST) In-Reply-To: <4CE5EF2E.6040108@2ndquadrant.com> References: <4CDAA73A.6090409@timbira.com> <6240.1289852723@sss.pgh.pa.us> <4CE5EF2E.6040108@2ndquadrant.com> From: Marti Raudsepp Date: Fri, 19 Nov 2010 10:35:11 +0200 Message-ID: Subject: Re: [PATCH] Fix documentation about PL/Python exception handling To: Greg Smith Cc: Tom Lane , Euler Taveira de Oliveira , pgsql-docs Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201011/32 X-Sequence-Number: 6230 On Fri, Nov 19, 2010 at 05:29, Greg Smith wrote: > The open question for this one then is the right way for exceptions to ac= t. > =C2=A0Marti, since you've looked at this a bit already, any thoughts on w= hat that > should look like? =C2=A0You initially commented that what happens was sur= prising, > but I'm not clear on what you expected to happen instead. The documented behavior would make a lot more sense. For instance, currently, when you call plpy.error(), the exception that's thrown can be caught, but even if you handle it, the function still ends up failing. Similarly you CANNOT handle SQL errors -- at all! All errors are fatal. CREATE FUNCTION foo() RETURNS text AS 'try: plpy.execute("SELECT syntax error") except: plpy.notice("exception caught but cannot ignore") return "retval" ' LANGUAGE plpythonu; WARNING: PL/Python: plpy.SPIError: unrecognized error in PLy_spi_execute_q= uery CONTEXT: PL/Python function "foo" NOTICE: exception caught but cannot ignore CONTEXT: PL/Python function "foo" ERROR: column "syntax" does not exist LINE 1: SELECT syntax error ^ QUERY: SELECT syntax error CONTEXT: PL/Python function "foo" Notice how the return value in the above is ignored, although the NOTICE is still output. Any new exceptions raised in the exception handler -- such as coding errors -- are also silently ignored. Another surprise is that plpy.Error and plpy.Fatal are just empty exception subclasses -- they behave nothing like plpy.error()/plpy.fatal(). Since they're normal Python exceptions, they can be caught and silenced. But an unhandled plpy.Fatal still results in an ERROR level message, go figure. I'm surprised that PL/Python got merged into Postgres at all without proper error handling. Regards, Marti