From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Tomas Vondra <tomas.vondra@2ndquadrant.com>
Cc: Andrew Dunstan <andrew.dunstan@2ndquadrant.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: Floris Van Nee <florisvannee@optiver.com>
Cc: Ariadne Conill <ariadne@dereferenced.org>
Cc: Mark Felder <feld@freebsd.org>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: jsonb_set() strictness considered harmful to data
Date: Mon, 21 Oct 2019 14:08:22 -0700
Message-ID: <3a866bdd-8a04-c428-9f77-9768c04ceae6@aklaver.com> (raw)
In-Reply-To: <20191021195031.v4hzx5ytkjjsbfbv@development>
References: <20191019192723.luarxhq57qkzuncw@development>
<CAAOiGNxmXSUdOzBg+7vWMzTjDELmiZi_dqGz355tKy+OgbCZpA@mail.gmail.com>
<1571560798575.47024@Optiver.com>
<34a7ff88-0d86-0990-4eac-48159df8d7f2@2ndQuadrant.com>
<CAKFQuwbkKPYhT0S6481L7temmiN+VNrMkExsUjp6FjJnCynd5A@mail.gmail.com>
<5db07acf-bc75-edf8-60af-905c0adadb68@2ndQuadrant.com>
<20191020201839.olpuyuzixagcvvud@development>
<5b8a26bf-6b46-71f3-a99e-3fb9a8095232@2ndQuadrant.com>
<20191021060701.or6a6a7lg3asixom@development>
<f66263f4-7dce-610a-5dbe-060375a8d840@aklaver.com>
<20191021195031.v4hzx5ytkjjsbfbv@development>
On 10/21/19 12:50 PM, Tomas Vondra wrote:
> On Mon, Oct 21, 2019 at 08:06:46AM -0700, Adrian Klaver wrote:
>> On 10/20/19 11:07 PM, Tomas Vondra wrote:
>>> On Sun, Oct 20, 2019 at 06:51:05PM -0400, Andrew Dunstan wrote:
>>
>>>
>>> True. And AFAIK catching exceptions is not really possible in some code,
>>> e.g. in stored procedures (because we can't do subtransactions, so no
>>> exception blocks).
>>>
>>
>> Can you explain the above to me as I thought there are exception
>> blocks in stored functions and now sub-transactions in stored procedures.
>>
>
> Sorry for the confusion - I've not been particularly careful when
> writing that response.
>
> Let me illustrate the issue with this example:
>
> CREATE TABLE t (a int);
>
> CREATE OR REPLACE PROCEDURE test() LANGUAGE plpgsql AS $$
> DECLARE
> msg TEXT;
> BEGIN
> -- SAVEPOINT s1;
> INSERT INTO t VALUES (1);
> -- COMMIT;
> EXCEPTION
> WHEN others THEN
> msg := SUBSTR(SQLERRM, 1, 100);
> RAISE NOTICE 'error: %', msg;
> END; $$;
>
> CALL test();
>
> If you uncomment the SAVEPOINT, you get
>
> NOTICE: error: unsupported transaction command in PL/pgSQL
>
> because savepoints are not allowed in stored procedures. Fine.
>
> If you uncomment the COMMIT, you get
>
> NOTICE: error: cannot commit while a subtransaction is active
>
> which happens because the EXCEPTION block creates a subtransaction, and
> we can't commit when it's active.
>
> But we can commit outside the exception block:
>
> CREATE OR REPLACE PROCEDURE test() LANGUAGE plpgsql AS $$
> DECLARE
> msg TEXT;
> BEGIN
> BEGIN
> INSERT INTO t VALUES (1);
> EXCEPTION
> WHEN others THEN
> msg := SUBSTR(SQLERRM, 1, 100);
> RAISE NOTICE 'error: %', msg;
> END;
> COMMIT;
> END; $$;
You can do something like the below though:
CREATE TABLE t (a int PRIMARY KEY);
CREATE OR REPLACE PROCEDURE public.test()
LANGUAGE plpgsql
AS $procedure$
DECLARE
msg TEXT;
BEGIN
BEGIN
INSERT INTO t VALUES (1);
EXCEPTION
WHEN others THEN
msg := SUBSTR(SQLERRM, 1, 100);
RAISE NOTICE 'error: %', msg;
UPDATE t set a = 2;
END;
COMMIT;
END; $procedure$
test_(postgres)# CALL test();
CALL
test_(postgres)# select * from t;
a
---
1
(1 row)
test_(postgres)# CALL test();
NOTICE: error: duplicate key value violates unique constraint "t_pkey"
CALL
test_(postgres)# select * from t;
a
---
2
(1 row)
>
>
> regards
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: adrian.klaver@aklaver.com, tomas.vondra@2ndquadrant.com, andrew.dunstan@2ndquadrant.com, david.g.johnston@gmail.com, florisvannee@optiver.com, ariadne@dereferenced.org, feld@freebsd.org, pgsql-hackers@lists.postgresql.org
Subject: Re: jsonb_set() strictness considered harmful to data
In-Reply-To: <3a866bdd-8a04-c428-9f77-9768c04ceae6@aklaver.com>
References: <20191019192723.luarxhq57qkzuncw@development> <CAAOiGNxmXSUdOzBg+7vWMzTjDELmiZi_dqGz355tKy+OgbCZpA@mail.gmail.com> <1571560798575.47024@Optiver.com> <34a7ff88-0d86-0990-4eac-48159df8d7f2@2ndQuadrant.com> <CAKFQuwbkKPYhT0S6481L7temmiN+VNrMkExsUjp6FjJnCynd5A@mail.gmail.com> <5db07acf-bc75-edf8-60af-905c0adadb68@2ndQuadrant.com> <20191020201839.olpuyuzixagcvvud@development> <5b8a26bf-6b46-71f3-a99e-3fb9a8095232@2ndQuadrant.com> <20191021060701.or6a6a7lg3asixom@development> <f66263f4-7dce-610a-5dbe-060375a8d840@aklaver.com> <20191021195031.v4hzx5ytkjjsbfbv@development> <3a866bdd-8a04-c428-9f77-9768c04ceae6@aklaver.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