pg.ddx.io pgsql-admin@postgresql.org mailing list archive
help / color / mirror / Atom feedplpythonu: how to catch plpy.execute() exceptions
6+ messages / 4 participants
[nested] [flat]
* plpythonu: how to catch plpy.execute() exceptions
@ 2010-10-29 13:02 Dragos Valentin Moinescu <dragos.moinescu@gmail.com>
2010-10-29 13:59 ` Re: plpythonu: how to catch plpy.execute() exceptions Sergey Konoplev <gray.ru@gmail.com>
2010-10-29 14:03 ` Re: plpythonu: how to catch plpy.execute() exceptions Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 6+ messages in thread
From: Dragos Valentin Moinescu @ 2010-10-29 13:02 UTC (permalink / raw)
To: pgsql-admin
Hello all,
I am trying to create a plpythonu function that will execute some SQLs
stored in a table (because it should work much faster than executing
from remote).
The thing is that I cannot catch the exception raised by plpy.execute().
I tried this with the following code:
CREATE OR REPLACE FUNCTION __syncpg_execute() RETURNS boolean
AS $$
try:
plpy.execute("SELECT * FROM not_existing_table", 5)
except Exception, exc:
plpy.notice("Exception %s" % str(exc) )
return True
$$ LANGUAGE plpythonu;
The result is:
# select __syncpg_execute();
WARNING: PL/Python: plpy.SPIError: unrecognized error in PLy_spi_execute_query
CONTEXT: PL/Python function "__syncpg_execute"
NOTICE: Exception error return without exception set
CONTEXT: PL/Python function "__syncpg_execute"
ERROR: relation "not_existing_table" does not exist
LINE 1: SELECT * FROM not_existing_table
^
QUERY: SELECT * FROM not_existing_table
CONTEXT: PL/Python function "__syncpg_execute"
I am using Postgresql9.0 with python 2.4.3
Thank you
--
Best regards,
Dragos Moinescu
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: plpythonu: how to catch plpy.execute() exceptions
2010-10-29 13:02 plpythonu: how to catch plpy.execute() exceptions Dragos Valentin Moinescu <dragos.moinescu@gmail.com>
@ 2010-10-29 13:59 ` Sergey Konoplev <gray.ru@gmail.com>
2010-10-29 14:40 ` Re: plpythonu: how to catch plpy.execute() exceptions Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 1 reply; 6+ messages in thread
From: Sergey Konoplev @ 2010-10-29 13:59 UTC (permalink / raw)
To: Dragos Valentin Moinescu <dragos.moinescu@gmail.com>; +Cc: pgsql-admin
Hi,
On 29 October 2010 17:02, Dragos Valentin Moinescu
<dragos.moinescu@gmail.com> wrote:
> The thing is that I cannot catch the exception raised by plpy.execute().
I came to this plpython function template. It handles errors properly
and works faster then usual function because of the code caching.
CREATE OR REPLACE FUNCTION some_plpython_function()
RETURNS integer
LANGUAGE plpythonu
AS $function$
""" Exaple of function's core cache and error handling """
sdNamespace = 'some_plpython_function'
if sdNamespace not in SD:
def main():
""" The function is assumed to be cached in SD and reused """
result = None
# Do whatever you need here
return result
# Cache body in SD
SD[sdNamespace] = main
try:
return SD[sdNamespace]()
except Exception, e:
import traceback
plpy.info(traceback.format_exc())
$function$;
>
> I tried this with the following code:
>
> CREATE OR REPLACE FUNCTION __syncpg_execute() RETURNS boolean
> AS $$
> try:
> plpy.execute("SELECT * FROM not_existing_table", 5)
> except Exception, exc:
> plpy.notice("Exception %s" % str(exc) )
> return True
> $$ LANGUAGE plpythonu;
>
> The result is:
> # select __syncpg_execute();
> WARNING: PL/Python: plpy.SPIError: unrecognized error in PLy_spi_execute_query
> CONTEXT: PL/Python function "__syncpg_execute"
> NOTICE: Exception error return without exception set
> CONTEXT: PL/Python function "__syncpg_execute"
> ERROR: relation "not_existing_table" does not exist
> LINE 1: SELECT * FROM not_existing_table
> ^
> QUERY: SELECT * FROM not_existing_table
> CONTEXT: PL/Python function "__syncpg_execute"
>
> I am using Postgresql9.0 with python 2.4.3
>
> Thank you
>
> --
> Best regards,
> Dragos Moinescu
>
> --
> Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-admin
>
--
Sergey Konoplev
Blog: http://gray-hemp.blogspot.com /
Linkedin: http://ru.linkedin.com/in/grayhemp /
JID/GTalk: gray.ru@gmail.com / Skype: gray-hemp
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: plpythonu: how to catch plpy.execute() exceptions
2010-10-29 13:02 plpythonu: how to catch plpy.execute() exceptions Dragos Valentin Moinescu <dragos.moinescu@gmail.com>
2010-10-29 13:59 ` Re: plpythonu: how to catch plpy.execute() exceptions Sergey Konoplev <gray.ru@gmail.com>
@ 2010-10-29 14:40 ` Tom Lane <tgl@sss.pgh.pa.us>
2010-10-29 14:55 ` Re: plpythonu: how to catch plpy.execute() exceptions Sergey Konoplev <gray.ru@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Tom Lane @ 2010-10-29 14:40 UTC (permalink / raw)
To: Sergey Konoplev <gray.ru@gmail.com>; +Cc: Dragos Valentin Moinescu <dragos.moinescu@gmail.com>; pgsql-admin
Sergey Konoplev <gray.ru@gmail.com> writes:
> On 29 October 2010 17:02, Dragos Valentin Moinescu
> <dragos.moinescu@gmail.com> wrote:
>> The thing is that I cannot catch the exception raised by plpy.execute().
> I came to this plpython function template. It handles errors properly
> and works faster then usual function because of the code caching.
Really? As far as I can see, it's entirely impossible for a plpython
function to trap and recover from an error in plpy.execute. It can
continue to run plain python code, but it won't be allowed to call
plpy.execute again, and the error will be rethrown when control exits
the function. There's no way to fix that short of setting up
subtransactions, which is what the other PLs do.
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: plpythonu: how to catch plpy.execute() exceptions
2010-10-29 13:02 plpythonu: how to catch plpy.execute() exceptions Dragos Valentin Moinescu <dragos.moinescu@gmail.com>
2010-10-29 13:59 ` Re: plpythonu: how to catch plpy.execute() exceptions Sergey Konoplev <gray.ru@gmail.com>
2010-10-29 14:40 ` Re: plpythonu: how to catch plpy.execute() exceptions Tom Lane <tgl@sss.pgh.pa.us>
@ 2010-10-29 14:55 ` Sergey Konoplev <gray.ru@gmail.com>
0 siblings, 0 replies; 6+ messages in thread
From: Sergey Konoplev @ 2010-10-29 14:55 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Dragos Valentin Moinescu <dragos.moinescu@gmail.com>; pgsql-admin
On 29 October 2010 18:40, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Sergey Konoplev <gray.ru@gmail.com> writes:
>> On 29 October 2010 17:02, Dragos Valentin Moinescu
>> <dragos.moinescu@gmail.com> wrote:
>>> The thing is that I cannot catch the exception raised by plpy.execute().
>
>> I came to this plpython function template. It handles errors properly
>> and works faster then usual function because of the code caching.
>
> Really? As far as I can see, it's entirely impossible for a plpython
> function to trap and recover from an error in plpy.execute. It can
> continue to run plain python code, but it won't be allowed to call
> plpy.execute again, and the error will be rethrown when control exits
> the function. There's no way to fix that short of setting up
> subtransactions, which is what the other PLs do.
Oh I am sorry for bothering you and other members. I have not read the
problem properly - my fault.
>
> regards, tom lane
>
--
Sergey Konoplev
Blog: http://gray-hemp.blogspot.com /
Linkedin: http://ru.linkedin.com/in/grayhemp /
JID/GTalk: gray.ru@gmail.com / Skype: gray-hemp
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: plpythonu: how to catch plpy.execute() exceptions
2010-10-29 13:02 plpythonu: how to catch plpy.execute() exceptions Dragos Valentin Moinescu <dragos.moinescu@gmail.com>
@ 2010-10-29 14:03 ` Tom Lane <tgl@sss.pgh.pa.us>
2010-10-29 16:27 ` Re: plpythonu: how to catch plpy.execute() exceptions Joshua D. Drake <jd@commandprompt.com>
1 sibling, 1 reply; 6+ messages in thread
From: Tom Lane @ 2010-10-29 14:03 UTC (permalink / raw)
To: Dragos Valentin Moinescu <dragos.moinescu@gmail.com>; +Cc: pgsql-admin
Dragos Valentin Moinescu <dragos.moinescu@gmail.com> writes:
> The thing is that I cannot catch the exception raised by plpy.execute().
Yeah, plpython's error handling is fundamentally broken. Somebody
needs to rewrite it to be more like the other PLs. In the meantime,
I'd suggest using plperl, which has gotten a lot more love than
plpython ever did.
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: plpythonu: how to catch plpy.execute() exceptions
2010-10-29 13:02 plpythonu: how to catch plpy.execute() exceptions Dragos Valentin Moinescu <dragos.moinescu@gmail.com>
2010-10-29 14:03 ` Re: plpythonu: how to catch plpy.execute() exceptions Tom Lane <tgl@sss.pgh.pa.us>
@ 2010-10-29 16:27 ` Joshua D. Drake <jd@commandprompt.com>
0 siblings, 0 replies; 6+ messages in thread
From: Joshua D. Drake @ 2010-10-29 16:27 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Dragos Valentin Moinescu <dragos.moinescu@gmail.com>; pgsql-admin
On Fri, 2010-10-29 at 10:03 -0400, Tom Lane wrote:
> Dragos Valentin Moinescu <dragos.moinescu@gmail.com> writes:
> > The thing is that I cannot catch the exception raised by plpy.execute().
>
> Yeah, plpython's error handling is fundamentally broken. Somebody
> needs to rewrite it to be more like the other PLs. In the meantime,
> I'd suggest using plperl, which has gotten a lot more love than
> plpython ever did.
Or take a look at pg-python:
http://pgfoundry.org/projects/python/
Which is much more actively maintained and still Python.
JD
--
PostgreSQL.org Major Contributor
Command Prompt, Inc: http://www.commandprompt.com/ - 509.416.6579
Consulting, Training, Support, Custom Development, Engineering
http://twitter.com/cmdpromptinc | http://identi.ca/commandprompt
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2010-10-29 16:27 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-10-29 13:02 plpythonu: how to catch plpy.execute() exceptions Dragos Valentin Moinescu <dragos.moinescu@gmail.com>
2010-10-29 13:59 ` Sergey Konoplev <gray.ru@gmail.com>
2010-10-29 14:40 ` Tom Lane <tgl@sss.pgh.pa.us>
2010-10-29 14:55 ` Sergey Konoplev <gray.ru@gmail.com>
2010-10-29 14:03 ` Tom Lane <tgl@sss.pgh.pa.us>
2010-10-29 16:27 ` Joshua D. Drake <jd@commandprompt.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox