Re: alternative to PG_CATCH

From: David Steele <david(at)pgmasters(dot)net>
To: peter(dot)eisentraut(at)2ndquadrant(dot)com
Cc: Kyotaro HORIGUCHI <horiguchi(dot)kyotaro(at)lab(dot)ntt(dot)co(dot)jp>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: alternative to PG_CATCH
Date: 2018-12-13 13:54:39
Message-ID: f49c0d73-90f3-5a8c-54e1-c68e2f381b46@pgmasters.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 12/13/18 7:26 AM, Kyotaro HORIGUCHI wrote:
> At Thu, 13 Dec 2018 11:33:39 +0100, Peter Eisentraut <peter(dot)eisentraut(at)2ndquadrant(dot)com> wrote in <c170919d-c78b-3dac-5ff6-9bd12f7a38bc(at)2ndquadrant(dot)com>
>>
>> I've played with a way to express this more compactly:
>>
>> PG_TRY();
>> {
>> ... code that might throw ereport(ERROR) ...
>> }
>> PG_FINALLY({
>> cleanup();
>> });
>>
>> See attached patch for how this works out in practice.

+1

> Though I didn't look into individual change, this kind of
> refactoring looks good to me. But the syntax looks
> somewhat.. uh..
>
> I'm not sure it is actually workable, but I suspect that what we
> need here is just a shortcut of 'PG_CATCH();{PG_RE_THROW();}'.
> Something like this:
>
> #define PG_FINALLY() \
> } \
> else \
> { \
> PG_exception_stack = save_exception_stack; \
> error_context_stack = save_context_stack; \
> PG_RE_THROW(); \
> } \
> PG_exception_stack = save_exception_stack; \
> error_context_stack = save_context_stack; \
> {
>
> Which can be used as:
>
> PG_TRY();
> {
> ... code that might throw ereport(ERROR) ...
> }
> PG_FINALLY();
> {
> cleanup();
> }
> PG_TRY_END();

I like this syntax better. We use something very similar in the
pgBackRest project:

TRY_BEGIN()
{
<Do something that might throw an error>
}
CATCH(Error1)
{
<Handle Error1>
}
CATCH(Error2)
{
<Handle Error2>
}
CATCH_ANY()
{
<Handle errors that are not Error1 or Error2>
}
FINALLY()
{
<Cleanup code that runs in all cases>
}
TRY_END();

The syntax works out simpler if the FINALLY is part of the TRY block.

See attached for the implementation.

Regards,
--
-David
david(at)pgmasters(dot)net

Attachment Content-Type Size
error.h text/plain 15.6 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chapman Flack 2018-12-13 13:56:26 Re: 'infinity'::Interval should be added
Previous Message David Steele 2018-12-13 13:35:23 Re: Add timeline to partial WAL segments