Re: Assertions in PL/PgSQL

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Marko Tiikkaja <marko(at)joh(dot)to>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Assertions in PL/PgSQL
Date: 2013-09-18 15:11:53
Message-ID: CAFj8pRCQnUCJXn43rjHLd1CCCCcHpCBk++FpvW12EBmOJ6FjLA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello

2013/9/18 Marko Tiikkaja <marko(at)joh(dot)to>

> On 2013-09-16 21:24, Pavel Stehule wrote:
>
>> 2. a failed assert should to raise a exception, that should not be handled
>> by any exception handler - similar to ERRCODE_QUERY_CANCELED - see
>> exception_matches_conditions.
>>
>
> I'm not sure what I think about that idea. I see decent arguments for it
> working either way. Care to unravel yours a bit more?
>

yes

so

CREATE OR REPLACE FUNCTION foo(a int) RETURNS int
BEGIN
ASSERT a BETWEEN 1 AND 100;
RETURNS a;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION proc()
RETURNS int AS $$
BEGIN
do some complex logic that exec a foo function

EXCEPTION WHEN OTHERS THEN
-- log some errors
INSERT INTO log VALUES(...)
END;
$$ LANGUAGE plpgsql;

In this code a assert fail can be lost in app log. Or can be knowingly
handled and ignored - what is wrong, and should not be allowed.

When I wrote a little bit complex procedures, I had to use a EXCEPTION WHEN
OTHERS clause - because I would not to lost a transaction. It worked, but
searching a syntax errors was significantly harder - so on base of this
experience I am thinking so some errors can be handled (related to database
usage) and others not - like syntax errors in PL/pgSQL or possible
assertions (although we can handle syntax error, but I don't think so it is
practical). It significantly increase a work that is necessary for error
identification.

Regards

Pavel

>
>
> Regards,
> Marko Tiikkaja
>

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Pavel Stehule 2013-09-18 15:19:05 Re: Performance problem in PLPgSQL
Previous Message Robert Haas 2013-09-18 15:06:50 Re: record identical operator