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 1iLtTP-0000S5-4U for pgsql-general@arkaria.postgresql.org; Sat, 19 Oct 2019 18:29:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLtTM-0007Rz-49 for pgsql-general@arkaria.postgresql.org; Sat, 19 Oct 2019 18:29:04 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLtTK-0007Rs-9Y for pgsql-general@lists.postgresql.org; Sat, 19 Oct 2019 18:29:03 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLtTA-00083m-2K for pgsql-general@lists.postgresql.org; Sat, 19 Oct 2019 18:29:01 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 8024F22006; Sat, 19 Oct 2019 14:28:49 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sat, 19 Oct 2019 14:28:49 -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=t W9B5kK6XtJ1qfEBED0znTkXil942V78VEhHNyRa+/w=; b=RwZaJY/sDCXK21VN+ AmBixHGKV/lkW77rnZ1ik6h0c32dT4TLA0yr6iNctRk0EMfgZFTfgmEc2kh76jvi 1baqICCg88HWjWp1UttAMdvvzUZ9t2Ow6ADqpJf5ePFL3pYlud21fniImVSHdsQs 1LM6UcTwGoMOMaeuZRcHQUwWb/1F63U7tlGLG/tWJxppXrNIk3dDjanWdchoMSmv Qvl8YjF4kephm0xDzcoXrNp52P0Jkzhj++0YlmlwX4QYKKXalqHE85fx5hx5QtDF ojWAwqVfs3gGTIXGaILEN2xEAC8O72BFbqqH+1QoWH6s+Z+e/zxQR4bQwrpfET3L ddBEA== 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=tW9B5kK6XtJ1qfEBED0znTkXil942V78VEhHNyRa+ /w=; b=JHBI2hwGo7RHcM1mcJmQRgDaWxvmq9QVC4WQ7FsLpdUxRBtjwLYEMmxjX VvRVHexxLdA7YE9O8uQm1GZ7lgM0vvXCzxVe3WYO+bFiBAcWk1ieReIEI8GwG2dE k1vehAwSDfBEubJHyTpZfmh+mvdQLkAX5qSyAnslFULQkHo1aG8rM4Gqmq5nBNya wDvoI86dr0u3CKfJSLEfuHx4ZlfWvivnc75VD6EJED2WG7eV1d9lMDFlrDLfAX4C +OrbxeANi3wUZXUUra1sPWpYevYrxefzHIdiIsd5OrWB1gGE5C19ifCDjZgSygGM PVHzTD+nYK0GdS/gtdg8RqKDAwxbg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrkedugdehlecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepudejgedrvddu rddugeehrddvfeehnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrg hvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.2] (174-21-145-235.tukw.qwest.net [174.21.145.235]) by mail.messagingengine.com (Postfix) with ESMTPA id 5FBA5D6005D; Sat, 19 Oct 2019 14:28:48 -0400 (EDT) Subject: Re: jsonb_set() strictness considered harmful to data To: Ariadne Conill Cc: "David G. Johnston" , Christoph Moench-Tegeder , "pgsql-generallists.postgresql.org" , feld@freebsd.org References: <20191018215018.GB56427@elch.exwg.net> <7299c63c-fcdc-20fc-c955-4cd330cc4580@aklaver.com> From: Adrian Klaver Message-ID: <39ed932e-7895-740b-745a-0e1ec93ae1b0@aklaver.com> Date: Sat, 19 Oct 2019 11:28:47 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 10/18/19 7:18 PM, Ariadne Conill wrote: > Hello, > > On Fri, Oct 18, 2019 at 7:04 PM Adrian Klaver wrote: >> >> On 10/18/19 4:31 PM, Ariadne Conill wrote: >>> Hello, >>> >>> On Fri, Oct 18, 2019 at 6:01 PM Adrian Klaver wrote: >>>> >>>> On 10/18/19 3:11 PM, Ariadne Conill wrote: >>>>> Hello, >>>>> >>>>> On Fri, Oct 18, 2019 at 5:01 PM David G. Johnston >>>>> wrote: >>>>>> >>>>>> On Fri, Oct 18, 2019 at 2:50 PM Christoph Moench-Tegeder wrote: >>>>>>> >>>>>>> ## Ariadne Conill (ariadne@dereferenced.org): >>>>>>> >>>>>>>> update users set info=jsonb_set(info, '{bar}', info->'foo'); >>>>>>>> >>>>>>>> Typically, this works nicely, except for cases where evaluating >>>>>>>> info->'foo' results in an SQL null being returned. When that happens, >>>>>>>> jsonb_set() returns an SQL null, which then results in data loss.[3] >>>>>>> >>>>>>> So why don't you use the facilities of SQL to make sure to only >>>>>>> touch the rows which match the prerequisites? >>>>>>> >>>>>>> UPDATE users SET info = jsonb_set(info, '{bar}', info->'foo') >>>>>>> WHERE info->'foo' IS NOT NULL; >>>>>>> >>>>>> >>>>>> There are many ways to add code to queries to make working with this function safer - though using them presupposes one remembers at the time of writing the query that there is danger and caveats in using this function. I agree that we should have (and now) provided sane defined behavior when one of the inputs to the function is null instead blowing off the issue and defining the function as being strict. Whether that is "ignore and return the original object" or "add the key with a json null scalar value" is debatable but either is considerably more useful than returning SQL NULL. >>>>> >>>>> A great example of how we got burned by this last year: Pleroma >>>>> maintains pre-computed counters in JSONB for various types of >>>>> activities (posts, followers, followings). Last year, another counter >>>>> was added, with a migration. But some people did not run the >>>>> migration, because they are users, and that's what users do. This >>>> >>>> So you are more forgiving of your misstep, allowing users to run >>>> outdated code, then of running afoul of Postgres documented behavior: >>> >>> I'm not forgiving of either. >>> >>>> https://www.postgresql.org/docs/11/functions-json.html >>>> " The field/element/path extraction operators return NULL, rather than >>>> failing, if the JSON input does not have the right structure to match >>>> the request; for example if no such element exists" >>> >>> It is known that the extraction operators return NULL. The problem >>> here is jsonb_set() returning NULL when it encounters SQL NULL. >> >> I'm not following. Your original case was: >> >> jsonb_set(info, '{bar}', info->'foo'); >> >> where info->'foo' is equivalent to: >> >> test=# select '{"f1":1,"f2":null}'::jsonb ->'f3'; >> ?column? >> ---------- >> NULL >> >> So you know there is a possibility that a value extraction could return >> NULL and from your wrapper that COALESCE is the way to deal with this. > > You're not following because you don't want to follow. > > It does not matter that info->'foo' is in my example. That's not what > I am talking about. > > What I am talking about is that jsonb_set(..., ..., NULL) returns SQL NULL > > postgres=# \pset null '(null)' > Null display is "(null)". > postgres=# select jsonb_set('{"a":1,"b":2,"c":3}'::jsonb, '{a}', NULL); > jsonb_set > ----------- > (null) > (1 row) > > This behaviour is basically giving an application developer a loaded > shotgun and pointing it at their feet. It is not a good design. It > is a design which has likely lead to many users experiencing > unintentional data loss. create table null_test(fld_1 integer, fld_2 integer); insert into null_test values(1, 2), (3, NULL); select * from null_test ; fld_1 | fld_2 -------+------- 1 | 2 3 | NULL (2 rows) update null_test set fld_1 = fld_1 + fld_2; select * from null_test ; fld_1 | fld_2 -------+------- 3 | 2 NULL | NULL Failure to account for NULL is a generic issue. Given that this only the second post I can find that deals with this, in going on 4 years, I am guessing most users have dealt with it. If you really think this raises to the level of a bug then I would suggest filing a report here: https://www.postgresql.org/account/login/?next=/account/submitbug/ > > Ariadne > -- Adrian Klaver adrian.klaver@aklaver.com