R: Catch exception from plpy

From: "dario(dot)ber(at)libero(dot)it" <dario(dot)ber(at)libero(dot)it>
To: mgainty(at)hotmail(dot)com
Cc: pgsql-general(at)postgresql(dot)org
Subject: R: Catch exception from plpy
Date: 2010-04-25 15:59:12
Message-ID: 14776448.2862691272211152598.JavaMail.defaultUser@defaultHost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Martin,

Thanks for your help ...But I still don' get it!

I edited your code to correct what I think are some typos but I still get the
error message. What am I doing wrong!?

Dario

--
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;
--
select test_tryex();
--
WARNING: plpython: in function test_tryex:
DETAIL: <class 'plpy.SPIError'>: Unknown error in PLy_spi_execute_query
NOTICE: ('FUBAR!-- error return without exception set',)

ERROR: relation "tmp_foo" already exists
CONTEXT: SQL statement "create table tmp_foo (v1 int);"

********** Error **********

ERROR: relation "tmp_foo" already exists
SQL state: 42P07
Context: SQL statement "create table tmp_foo (v1 int);"

--

>----Messaggio originale----
>Da: dario(dot)ber(at)libero(dot)it
>Data: 24/04/2010 14.48
>A: <pgsql-general(at)postgresql(dot)org>
>Ogg: 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

Browse pgsql-general by date

  From Date Subject
Next Message Andre Lopes 2010-04-25 17:31:12 Problem saving emails to database.
Previous Message Raymond O'Donnell 2010-04-25 14:54:57 Re: User with GRANTS only on Views. Lock table on function will work?