Thread: plperl exception catching

plperl exception catching

From
Marc Evans
Date:
Hi -

Is it accurate for me to believe that there is no way to catch exception
within plperl? For example, if an update fails, I'd like to do an insert.
Yes, this is trivial to do in plpgsql, but sadly plpgsql is not usable due
to not supporting variable variables. If plperl can't catch exceptions, is
there a language that can be used?

Thanks in advance - Marc

Re: plperl exception catching

From
Tom Lane
Date:
Marc Evans <Marc@SoftwareHackery.Com> writes:
> Is it accurate for me to believe that there is no way to catch exception
> within plperl?

You do it the same way you trap any other error in perl ...

            regards, tom lane

Re: plperl exception catching

From
Marc Evans
Date:
On Wed, 13 Dec 2006, Tom Lane wrote:

> Marc Evans <Marc@SoftwareHackery.Com> writes:
>> Is it accurate for me to believe that there is no way to catch exception
>> within plperl?
>
> You do it the same way you trap any other error in perl ...

OK, I must be missing something obvious:

c3i=> CREATE OR REPLACE FUNCTION foo_trigger() RETURNS TRIGGER AS $$
c3i$>   eval { spi_exec_query('INSERT INTO FOO_BAR VALUES(1)'); } ||
c3i$>   spi_exec_query('SELECT 1 as foo');
c3i$> $$ LANGUAGE plperl;
ERROR:  creation of Perl function failed: 'eval "string"' trapped by
operation mask at line 2.

- Marc

Re: plperl exception catching

From
Bricklen Anderson
Date:
Marc Evans wrote:
> OK, I must be missing something obvious:
>
> c3i=> CREATE OR REPLACE FUNCTION foo_trigger() RETURNS TRIGGER AS $$
> c3i$>   eval { spi_exec_query('INSERT INTO FOO_BAR VALUES(1)'); } ||
> c3i$>   spi_exec_query('SELECT 1 as foo');
> c3i$> $$ LANGUAGE plperl;
> ERROR:  creation of Perl function failed: 'eval "string"' trapped by
> operation mask at line 2.
>

Try as plperlu

Re: plperl exception catching

From
Tom Lane
Date:
Bricklen Anderson <banderson@presinet.com> writes:
> Marc Evans wrote:
>> OK, I must be missing something obvious:
>> ERROR:  creation of Perl function failed: 'eval "string"' trapped by
>> operation mask at line 2.

> Try as plperlu

This brings up the question of whether it'd be safe to allow eval in
plperl functions.  I'm not sure why it's excluded now ... does it allow
access to untrusted operations?

            regards, tom lane

Re: plperl exception catching

From
Martijn van Oosterhout
Date:
On Wed, Dec 13, 2006 at 05:04:42PM -0500, Tom Lane wrote:
> Bricklen Anderson <banderson@presinet.com> writes:
> > Marc Evans wrote:
> >> OK, I must be missing something obvious:
> >> ERROR:  creation of Perl function failed: 'eval "string"' trapped by
> >> operation mask at line 2.
>
> > Try as plperlu
>
> This brings up the question of whether it'd be safe to allow eval in
> plperl functions.  I'm not sure why it's excluded now ... does it allow
> access to untrusted operations?

ISTM there being something about the Safe module in perl not being able
to enable eval while staying "safe", so to speak.

Looking at the safe module it looks like you can exclude certain
functions from restrictions. The manpage has an example, so a simple
try/catch mechanism could be created if enabling "eval" directly isn't
ok.

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Attachment

Re: plperl exception catching

From
Marc Evans
Date:
On Wed, 13 Dec 2006, Martijn van Oosterhout wrote:

> On Wed, Dec 13, 2006 at 05:04:42PM -0500, Tom Lane wrote:
>> Bricklen Anderson <banderson@presinet.com> writes:
>>> Marc Evans wrote:
>>>> OK, I must be missing something obvious:
>>>> ERROR:  creation of Perl function failed: 'eval "string"' trapped by
>>>> operation mask at line 2.
>>
>>> Try as plperlu
>>
>> This brings up the question of whether it'd be safe to allow eval in
>> plperl functions.  I'm not sure why it's excluded now ... does it allow
>> access to untrusted operations?
>
> ISTM there being something about the Safe module in perl not being able
> to enable eval while staying "safe", so to speak.
>
> Looking at the safe module it looks like you can exclude certain
> functions from restrictions. The manpage has an example, so a simple
> try/catch mechanism could be created if enabling "eval" directly isn't
> ok.

I believe that the BLOCK variation of eval could be considered safe, e.g.
eval { ... } but the EXPR version of eval probably should not be
considered safe, e.g. eval "...".

- Marc