Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iMeup-00063K-8k for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Oct 2019 21:08:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iMeun-000526-3S for pgsql-hackers@arkaria.postgresql.org; Mon, 21 Oct 2019 21:08:33 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iMeuk-0004yI-O4; Mon, 21 Oct 2019 21:08:32 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iMeuh-0006rO-Fe; Mon, 21 Oct 2019 21:08:29 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id D9F09215B2; Mon, 21 Oct 2019 17:08:25 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Mon, 21 Oct 2019 17:08:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=q cN71gyJn5zBFtExa+0xzMa0q9z1FISs17XnzA5kN2c=; b=vGNLEG9kODBAvdIJF +sVG7pLVaJcl9ZvAQVUDZqaqm3r0usct3Ty4gBbNX9gWTOVzKUWdGH0OvvE66jen 9mbZpTyqctrgrYAA6A1Q66613575SzgqJweppDfCyX83QXwi/Ve+jxGnTVeUhgi1 E5FRi7Fl5oYVh31SfAHhVd9HLe2ss6aGoT+6khs3o1IqMAvtkz/P4oefrt/TrijB OHYHO/c+3zJRrceGTn6qgne8SI1JxrllpBq9wGORjJ7T6jJRVEHP7MXSgD7XrCGI SCzPnAlDyrl/pDwjZe9WtPpDO29rQ+VdQ/ZggxS2ahNzIFuaSIg9E/lTZTvw7xiO dThoA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=qcN71gyJn5zBFtExa+0xzMa0q9z1FISs17XnzA5kN 2c=; b=h0tLKn9sdpTFwBE4FwMTjvX2hy8R8NcDiCbk0YaSmHaXkFZOUkcx0eJFc teek0TQup7ozY6yAAItQ5kal+zbz1ccixkCG4bv7zuqk8m4ZmXON7EtDUpBq9KEj xC9s4cMLGDxjEA+MrTeusYUIGsPQZSgFmS3fyZiFSUSNLMtAuDAOaxCXz+a52PxK Iq+EbFbsBEIsh9ng6zKB3sDze4sC8OmTCD4vEYZu+7ZU8gCUvwbo1RUqfAtbiiEF IGfChp/c1tWMSgj+Hd6A2HM4PovsEZWz4BBL90wDTL6QTLrmGsDGjhfl1fMDdzTy bZkNPiETGiaHel6fYR9okEdQzyRug== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrkeehgdduiedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucfkphepjeefrddvfeelrdduudejrddvfeeknecurfgrrhgrmhepmhgrihhlfhhr ohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsth gvrhfuihiivgeptd X-ME-Proxy: Received: from ranger.suse (c-73-239-117-238.hsd1.wa.comcast.net [73.239.117.238]) by mail.messagingengine.com (Postfix) with ESMTPA id BB87BD6005D; Mon, 21 Oct 2019 17:08:23 -0400 (EDT) Subject: Re: jsonb_set() strictness considered harmful to data To: Tomas Vondra Cc: Andrew Dunstan , "David G. Johnston" , Floris Van Nee , Ariadne Conill , Mark Felder , "pgsql-generallists.postgresql.org" , PostgreSQL Hackers References: <20191019192723.luarxhq57qkzuncw@development> <1571560798575.47024@Optiver.com> <34a7ff88-0d86-0990-4eac-48159df8d7f2@2ndQuadrant.com> <5db07acf-bc75-edf8-60af-905c0adadb68@2ndQuadrant.com> <20191020201839.olpuyuzixagcvvud@development> <5b8a26bf-6b46-71f3-a99e-3fb9a8095232@2ndQuadrant.com> <20191021060701.or6a6a7lg3asixom@development> <20191021195031.v4hzx5ytkjjsbfbv@development> From: Adrian Klaver Message-ID: <3a866bdd-8a04-c428-9f77-9768c04ceae6@aklaver.com> Date: Mon, 21 Oct 2019 14:08:22 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.1 MIME-Version: 1.0 In-Reply-To: <20191021195031.v4hzx5ytkjjsbfbv@development> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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