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 1nnAkt-000446-K5 for pgsql-general@arkaria.postgresql.org; Sat, 07 May 2022 03:05:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nnAkr-00089M-B3 for pgsql-general@arkaria.postgresql.org; Sat, 07 May 2022 03:05:13 +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 1nnAkr-00088o-0a for pgsql-general@lists.postgresql.org; Sat, 07 May 2022 03:05:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nnAkl-00080M-TT for pgsql-general@lists.postgresql.org; Sat, 07 May 2022 03:05:09 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 2473563J3020685; Fri, 6 May 2022 23:05:06 -0400 From: Tom Lane To: Bryn Llewellyn cc: pgsql-general list Subject: Re: "A transaction cannot be ended inside a block with exception handlers." In-reply-to: <40EB34E2-F9CA-4D29-B0BE-6DF3D4D0FBA3@yugabyte.com> References: <40EB34E2-F9CA-4D29-B0BE-6DF3D4D0FBA3@yugabyte.com> Comments: In-reply-to Bryn Llewellyn message dated "Fri, 06 May 2022 16:40:03 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3020683.1651892706.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 06 May 2022 23:05:06 -0400 Message-ID: <3020684.1651892706@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Bryn Llewellyn writes: > I want to demonstrate how to meet this requirement: > =C2=AB > Encapsulate each business function in a user-defined subprogram that hid= es all the implementation details like table names and the SQL statements = that manipulate their contents so the they cannot be seen using SQL issued= from the client. Further, don't allow raw errors to escape to the client.= Rather, if an expected error occurs (like a unique key violation), then r= eport this as an application-specific code that translates to, for example= , "This nickname is already taken. Choose a different one." And if an "oth= ers" error occurs (typically because the programmer forgot to cater for it= =E2=80=94like a too-wide varchar value) insert diagnostic info into an inc= ident log table and return an "unexpected error" application-specific code= together with the incident ID so that it can be reported to Support. > =C2=BB > I've written proof-of-concept code that shows how to meet this requireme= nt for most scenarios. But it seems to be impossible to meet the requireme= nt for errors that occur at commit time. So ... avoid those? It seems like it's only a problem if you use deferred constraints, and that's not a necessary feature. > 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? If you want a strict view of that you probably need to be doing the encapsulation on the client side. There's nothing you can do on the server side that would prevent, say, network-connection failures "escaping" to the client. And that's actually one of the harder cases to deal with: if the connection drops just after you issue COMMIT, you can't tell whether the transaction got committed. regards, tom lane