Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nnASp-0002sj-JG for pgsql-general@arkaria.postgresql.org; Sat, 07 May 2022 02:46:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nnASo-0000xF-6U for pgsql-general@arkaria.postgresql.org; Sat, 07 May 2022 02:46:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nnASn-0000x6-La for pgsql-general@lists.postgresql.org; Sat, 07 May 2022 02:46:33 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nnASj-0007tl-Eb for pgsql-general@lists.postgresql.org; Sat, 07 May 2022 02:46:32 +0000 Received: by mail-pl1-x635.google.com with SMTP id j14so9114768plx.3 for ; Fri, 06 May 2022 19:46:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=GpyWjLKCwdZbzFbG/YCV/JjvlYDHc60ntCMf/bEItyk=; b=RV2gQdOJWLeVndbByI+zShRpNZFMx+WGxdqyukfbeHAr7Ul1AeMA5PPOqeGbTi+raC GjM7EASz8W7TKq6yCCiJiSZvE2d9poYqxg2eq5gOrDHIEWZKNnqz8T8SUXxUCSs7etGW MvQn2JCTEQucvi7S8Ub/EswOXcrU6zJ6e1RSo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=GpyWjLKCwdZbzFbG/YCV/JjvlYDHc60ntCMf/bEItyk=; b=R8fSJRzF2jc9y06xstGl4rgnxsoG1cY+JXzyIbrQHfWNvbWtoiLZ9LDEvl8zl/ZsT0 5OP3vdsJryyM1CJ1N3qKvtcmYjszXVG9TY1e/gDk63qkyT2kVXa8PLEN6Ushk50iXLxM 4+z7c/DoNmBymRH4HqFCk1giAGF+Xy4XzlDlS3v36oOqK+deiJek9xnqek1qOgKR9GKS 5ImOdasIJs1xhqbOx0tlTIYIQo12DymEq3V+TcRxp7yqSnfueTNTrOX0gfWaZHlhyGqn RnToWwuuxFS5/aWRfMxQ5DToAvkyQoKeK9TrljwJj7x3xeieLg+gp3QmUnWLTFWVHt9L eqJQ== X-Gm-Message-State: AOAM531kc34lAkwSOlPrtsjv7TQV69naMHUMnBNEQPstAvMBUHCHz0Qc E/+y94B+SPsQ14qW2eUsU584bg== X-Google-Smtp-Source: ABdhPJz3wmUINaTf5mHqiZPueV27fibnN17UyFWqquKiexqWooPNOLwHfnIbjwKQ6163Azh2VeLItA== X-Received: by 2002:a17:90b:4a84:b0:1dc:67e9:a92c with SMTP id lp4-20020a17090b4a8400b001dc67e9a92cmr7522098pjb.141.1651891588368; Fri, 06 May 2022 19:46:28 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:a90f:1be4:88a2:85c4]) by smtp.gmail.com with ESMTPSA id bt21-20020a17090af01500b001d967248885sm8039298pjb.29.2022.05.06.19.46.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 May 2022 19:46:27 -0700 (PDT) From: Bryn Llewellyn Message-Id: <4DAF8F15-66DB-4C66-B2D6-FF4922D03C54@yugabyte.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_DCB31201-424D-4856-912D-4DE1253B9B6E" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: "A transaction cannot be ended inside a block with exception handlers." Date: Fri, 6 May 2022 19:46:26 -0700 In-Reply-To: Cc: pgsql-general list To: "David G. Johnston" References: <40EB34E2-F9CA-4D29-B0BE-6DF3D4D0FBA3@yugabyte.com> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_DCB31201-424D-4856-912D-4DE1253B9B6E Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > david.g.johnston@gmail.com wrote: >=20 >> bryn@yugabyte.com wrote: >>=20 >> 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? >=20 > 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. --Apple-Mail=_DCB31201-424D-4856-912D-4DE1253B9B6E Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii
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.


= --Apple-Mail=_DCB31201-424D-4856-912D-4DE1253B9B6E--