Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f30ue-0005Ax-Jk for pgsql-bugs@arkaria.postgresql.org; Mon, 02 Apr 2018 14:58:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f30ud-0008Ci-7k for pgsql-bugs@arkaria.postgresql.org; Mon, 02 Apr 2018 14:58:23 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f30uc-0008CZ-S6 for pgsql-bugs@lists.postgresql.org; Mon, 02 Apr 2018 14:58:23 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f30uY-00030P-Ov for pgsql-bugs@lists.postgresql.org; Mon, 02 Apr 2018 14:58:21 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id B79B521C1C73; Mon, 2 Apr 2018 17:58:15 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.27.104] (gw.postgrespro.ru [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 8A92021C1C4F; Mon, 2 Apr 2018 17:58:15 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1522681095; bh=vQh/yyQlWDqJf2O8mqJv4Nj5GV2uEkG5+nXlYFK4YqQ=; h=Subject:To:References:From:Date:In-Reply-To; b=T2rb3qlQgbJ/r76p9RS1irJ7c1y1HcBD3GXjSaZcqNXz/F7fMq6lUbPeW9PQGX/uB PiQW09PP1KKpBp71Epbnl8WiIzl85zR2HRBG+o4FGy10Uh9rgz5VNWjAY910SIO+pr WVrm7HQkLflc/AZr4UezilT6nwxl0Uo/VPh+3CEg= Subject: Re: BUG #15140: Incorrect jsonb_set behavoir To: Dmitry Dolgov <9erthalion6@gmail.com>, pgsql-bugs@lists.postgresql.org References: <152266540508.1442.16790414733320933622@wrigleys.postgresql.org> From: "Ivan E. Panchenko" Message-ID: <3ab7ce91-9d9e-8b82-e77f-73d2da52c17e@postgrespro.ru> Date: Mon, 2 Apr 2018 17:58:15 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------C72B4EF07455D87DFD1EC19E" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------C72B4EF07455D87DFD1EC19E Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi, 02.04.2018 15:04, Dmitry Dolgov пишет: >> On 2 April 2018 at 12:36, PG Bug reporting form wrote: >> The following bug has been logged on the website: >> >> Bug reference: 15140 >> Logged by: Ivan Panchenko >> Email address: i.panchenko@postgrespro.ru >> PostgreSQL version: 10.3 >> Operating system: any >> Description: >> >> First. Impossible to create a nested structure for a path with more than one >> new keys. >> >> postgres => select jsonb_set('{}'::jsonb, array['x', 'y'], >> to_jsonb(1::text), true); >> jsonb_set >> ----------- >> {} >> (1 строка) >> >> Expected {"x": { "y" : "1" }} >> >> Second. Setting a NULL value nullifies the whole JSON >> >> postgres=> select jsonb_set('{}'::jsonb, array['x'], null, true); >> jsonb_insert >> -------------- >> >> (1 строка) >> >> Expected { "x" : null } > As far as I remember, these are not really bugs, but documented behavior. > > About the first one, here is [1]: > > All the items of the path parameter of jsonb_set as well as jsonb_insert > except the last item must be present in the target. If create_missing is > false, all items of the path parameter of jsonb_set must be present. If > these conditions are not met the target is returned unchanged. > > Although I agree this can be confusing, and we may want to change this. Yes, I think it would be good. Now I see it in the documentation, but unfortunately, it  is  a separate note, which is not referenced from the main jsonb_set description which  says,         ...with /|new_value|/ added if /|create_missing|/ is true ( default is |true|) and the item designated by /|path|/ does not exist. > About the second one, `jsonb_set` is defined as a strict function, which means > `null` arguments will produce null as a result. To avoid this you can do: > > =# select jsonb_set('{}'::jsonb, array['x'], 'null', true); > jsonb_set > ------------- > {"x": null} > (1 row) > > 1: https://www.postgresql.org/docs/devel/static/functions-json.html Thanks for the workaround.  Nevertheless, this behavior looks counter-intuitive, and I would vote for removing strictness from this function. Regards, Ivan --------------C72B4EF07455D87DFD1EC19E Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

Hi,


02.04.2018 15:04, Dmitry Dolgov пишет:
On 2 April 2018 at 12:36, PG Bug reporting form <noreply@postgresql.org> wrote:
The following bug has been logged on the website:

Bug reference:      15140
Logged by:          Ivan Panchenko
Email address:      i.panchenko@postgrespro.ru
PostgreSQL version: 10.3
Operating system:   any
Description:

First. Impossible to create a nested structure for a path with more than one
new keys.

postgres => select jsonb_set('{}'::jsonb, array['x', 'y'],
to_jsonb(1::text), true);
 jsonb_set
-----------
 {}
(1 строка)

Expected {"x": { "y" : "1" }}

Second. Setting a NULL value nullifies the whole JSON

postgres=> select jsonb_set('{}'::jsonb, array['x'], null, true);
 jsonb_insert
--------------

(1 строка)

Expected { "x" : null }
As far as I remember, these are not really bugs, but documented behavior.

About the first one, here is [1]:

    All the items of the path parameter of jsonb_set as well as jsonb_insert
    except the last item must be present in the target. If create_missing is
    false, all items of the path parameter of jsonb_set must be present. If
    these conditions are not met the target is returned unchanged.

Although I agree this can be confusing, and we may want to change this.
Yes, I think it would be good.

Now I see it in the documentation, but unfortunately, it  is  a separate note, which is not referenced from the main jsonb_set description which  says,

        ...with new_value added if create_missing is true ( default is true) and the item designated by path does not exist.



About the second one, `jsonb_set` is defined as a strict function, which means
`null` arguments will produce null as a result. To avoid this you can do:

    =# select jsonb_set('{}'::jsonb, array['x'], 'null', true);
     jsonb_set
    -------------
     {"x": null}
     (1 row)

1: https://www.postgresql.org/docs/devel/static/functions-json.html
Thanks for the workaround.  Nevertheless, this behavior looks counter-intuitive, and I would vote for removing strictness from this function.

Regards,
Ivan
--------------C72B4EF07455D87DFD1EC19E--