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 1hj8Q4-0007GB-S6 for pgsql-general@arkaria.postgresql.org; Thu, 04 Jul 2019 20:33:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hj8Q3-0003Z1-KX for pgsql-general@arkaria.postgresql.org; Thu, 04 Jul 2019 20:33:27 +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 1hj8Q3-0003Qb-5g for pgsql-general@lists.postgresql.org; Thu, 04 Jul 2019 20:33:27 +0000 Received: from 195-159-176-226.customer.powertech.no ([195.159.176.226] helo=blaine.gmane.org) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hj8Q0-0004xd-0r for pgsql-general@postgresql.org; Thu, 04 Jul 2019 20:33:25 +0000 Received: from list by blaine.gmane.org with local (Exim 4.89) (envelope-from ) id 1hj8Px-0002Ms-CR for pgsql-general@postgresql.org; Thu, 04 Jul 2019 22:33:21 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-general@postgresql.org From: Thomas Kellerer Subject: Re: Why does jsonb_set() remove non-mentioned keys? Date: Thu, 4 Jul 2019 22:33:12 +0200 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 In-Reply-To: Content-Language: de-DE List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk David G. Johnston schrieb am 04.07.2019 um 18:20: > On Thu, Jul 4, 2019 at 6:18 AM Thomas Kellerer > wrote: > > Why does > >    select jsonb_set('{"foo": 1}'::jsonb, '{bar}', to_jsonb(null::int), true) > > return NULL when all it should do is to add a second key? > > > Both functions involved are defined as being STRICT (null on null > input). You need to COALSESCE SQL NULL to JSON 'null' to obtain the > result you desire. This is a general truth when dealing with the > JSON type in PostgreSQL. But jsonb_set() doesn't change (or shouldn't) the whole value, only one key. I can understand that the "bar" key would not be set (because of the NULL), but removing a key that isn't even part of the target path looks like a bug to. Thomas