From: Bryn Llewellyn <bryn@yugabyte.com>
To: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: "A transaction cannot be ended inside a block with exception handlers."
Date: Fri, 6 May 2022 19:46:26 -0700
Message-ID: <4DAF8F15-66DB-4C66-B2D6-FF4922D03C54@yugabyte.com> (raw)
In-Reply-To: <CAKFQuwbjWf2FTUruGzhx--f74ctw0MRLf_DwBXk0JO1LMnLx_A@mail.gmail.com>
References: <40EB34E2-F9CA-4D29-B0BE-6DF3D4D0FBA3@yugabyte.com>
<CAKFQuwbjWf2FTUruGzhx--f74ctw0MRLf_DwBXk0JO1LMnLx_A@mail.gmail.com>
> david.g.johnston@gmail.com wrote:
>
>> bryn@yugabyte.com wrote:
>>
>> Is there simply no way that inserts into table "t" in my example can be encapsulated in PL/pgSQL so that the error from the failing trigger can be handled rather there than escaping, raw, to the client?
>
> Any potential solution to this problem will involve writing a stored procedure (CREATE PROCEDURE) which becomes the API layer for the application and each one probably issues a commit just prior to returning control to the calling application. Its exception handler can transform the at-commit errors to application errors and then return control to the calling application - which then needs to handle a clean return or an application-level error return.
Eh? A stored procedure that has a commit like you suggest cannot have an exception handler like you also suggest. That's what the doc says. I quoted the sentence in my "subject" line. More carefully stated, if you have such a procedure, then any txn control statement that it executes will cause this:
ERROR: 2D000: cannot roll back while a subtransaction is active
Sure, you can handle this. But that gets you nowhere. The procedure will always end this way and never do what you wanted it to do. Sorry if my email wasn't clear.
I'll be delighted if somebody can show me working PL/pgSQL code that uses the setup that I showed and takes this as a starting point:
create procedure do_insert(good in boolean)
language plpgsql
as $body$
begin
case good
when true then
for j in 10..20 loop
insert into t(k) values(j);
end loop;
when false then
insert into t(k) values(42);
end case;
end;
$body$;
As presented, it ends like this when it's called with "false"
ERROR: P0004: tigger trg caused exception
CONTEXT: PL/pgSQL function trg_fn() line 9 at ASSERT
LOCATION: exec_stmt_assert, pl_exec.c:3918
Modify it along the lines that David suggests so that when it's called with "true", it completes silently and makes the intended changes. And when it's called with "false", it reports that it handled the P0004 error via "raise info" and then returns without error.
The rest (presumably with an OUT parameter) is easy.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: bryn@yugabyte.com, david.g.johnston@gmail.com
Subject: Re: "A transaction cannot be ended inside a block with exception handlers."
In-Reply-To: <4DAF8F15-66DB-4C66-B2D6-FF4922D03C54@yugabyte.com>
References: <40EB34E2-F9CA-4D29-B0BE-6DF3D4D0FBA3@yugabyte.com> <CAKFQuwbjWf2FTUruGzhx--f74ctw0MRLf_DwBXk0JO1LMnLx_A@mail.gmail.com> <4DAF8F15-66DB-4C66-B2D6-FF4922D03C54@yugabyte.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox