Thread: Catch exception from plpy
Hello, Could someone show me how to catch exceptions generated by plpy.execute()? From the documentation and other posts I understand that you need to call plpy. error() but I still cannot figure out how to use it. For example, say I need a function that creates table foo if it doesn't exists, otherwise returns the rows in foo. Initially I thought the following shouldl work... but it doesn't! -- create or replace function test_tryex() returns void AS $$ try: plpy.execute('create table tmp_foo (v1 int);') except: plpy.execute('select * from tmp_foo;') $$ language plpythonu; -- -- If tmp_foo already exists I get: select test_tryex(); ********** Error ********** ERROR: relation "tmp_foo" already exists SQL state: 42P07 Context: SQL statement "create table tmp_foo (v1 int);" Many thanks in advance Dario
use try with exception e.g.
create or replace function test_tryex() returns void AS
'
try:
plpy.execute('create table tmp_foo (v1 int);
plpy.execute('select * from tmp_foo);
except Exception, ex:
plpy.notice("FUBAR!-- %s" % str(ex))
return "FUBAR!"
return "test_tryex function has succeeded"'
'
LANGUAGE plpythonu;
Martin Gainty
______________________________________________
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
> Date: Sat, 24 Apr 2010 14:48:33 +0200
> From: dario.ber@libero.it
> To: pgsql-general@postgresql.org
> Subject: [GENERAL] Catch exception from plpy
>
> Hello,
>
> Could someone show me how to catch exceptions generated by plpy.execute()?
> From the documentation and other posts I understand that you need to call plpy.
> error() but I still cannot figure out how to use it.
>
> For example, say I need a function that creates table foo if it doesn't
> exists, otherwise returns the rows in foo. Initially I thought the following
> shouldl work... but it doesn't!
>
> --
> create or replace function test_tryex() returns void AS $$
> try:
> plpy.execute('create table tmp_foo (v1 int);')
> except:
> plpy.execute('select * from tmp_foo;')
> $$
> language plpythonu;
> --
> -- If tmp_foo already exists I get:
> select test_tryex();
>
> ********** Error **********
>
> ERROR: relation "tmp_foo" already exists
> SQL state: 42P07
> Context: SQL statement "create table tmp_foo (v1 int);"
>
>
> Many thanks in advance
> Dario
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
Hotmail has tools for the New Busy. Search, chat and e-mail from your inbox. Learn more.
create or replace function test_tryex() returns void AS
'
try:
plpy.execute('create table tmp_foo (v1 int);
plpy.execute('select * from tmp_foo);
except Exception, ex:
plpy.notice("FUBAR!-- %s" % str(ex))
return "FUBAR!"
return "test_tryex function has succeeded"'
'
LANGUAGE plpythonu;
Martin Gainty
______________________________________________
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité
Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.
> Date: Sat, 24 Apr 2010 14:48:33 +0200
> From: dario.ber@libero.it
> To: pgsql-general@postgresql.org
> Subject: [GENERAL] Catch exception from plpy
>
> Hello,
>
> Could someone show me how to catch exceptions generated by plpy.execute()?
> From the documentation and other posts I understand that you need to call plpy.
> error() but I still cannot figure out how to use it.
>
> For example, say I need a function that creates table foo if it doesn't
> exists, otherwise returns the rows in foo. Initially I thought the following
> shouldl work... but it doesn't!
>
> --
> create or replace function test_tryex() returns void AS $$
> try:
> plpy.execute('create table tmp_foo (v1 int);')
> except:
> plpy.execute('select * from tmp_foo;')
> $$
> language plpythonu;
> --
> -- If tmp_foo already exists I get:
> select test_tryex();
>
> ********** Error **********
>
> ERROR: relation "tmp_foo" already exists
> SQL state: 42P07
> Context: SQL statement "create table tmp_foo (v1 int);"
>
>
> Many thanks in advance
> Dario
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
Hotmail has tools for the New Busy. Search, chat and e-mail from your inbox. Learn more.