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 1iLcER-0002dr-H1 for pgsql-general@arkaria.postgresql.org; Sat, 19 Oct 2019 00:04:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLcEQ-0003m1-6C for pgsql-general@arkaria.postgresql.org; Sat, 19 Oct 2019 00:04:30 +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 1iLcEO-0003lH-1b for pgsql-general@lists.postgresql.org; Sat, 19 Oct 2019 00:04:29 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iLcEK-0000mz-Df for pgsql-general@lists.postgresql.org; Sat, 19 Oct 2019 00:04:26 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 203CB20CB2; Fri, 18 Oct 2019 20:04:23 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 18 Oct 2019 20:04:23 -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=o 7viiOR7blEODcqN1F2cC7vUun4nY1G3437W2tFPRJ8=; b=g9dDHFF7k76YYCZkj aCv5F5FKEBp2Vii8OVAu6QEJY1NtWLbqbU6HBKflIWlUclYID5gOeePjKOt/4WN1 w462ApNpZviZm8f0zobfSjFTHwnZp/1Ss+opI791Cwxw8t7FoEZxB3TASk9iwYtU pSLRa17nYZzSOquai/V3J108WpdVs47KdaiwNqvhgbFFGm02n0Mpt+NIj82mKyJO iPuTSSlikgRNBvgdeC6qyEkC0bPujyATE8dAevoUM+4Jrub3b8JO+GLEejBcbN4p 9B7FLhN8N9oSa6AXfZ4/yPr6BFsgpbRFPEL7WJO9lSIC0vy9yFrnrjf/LiQRRagt 61JPw== 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=o7viiOR7blEODcqN1F2cC7vUun4nY1G3437W2tFPR J8=; b=l56aR+7i34Y/5bjHsiJJ2viyZ10fs+n1NTIDijB002ufTK4h4739DtlD+ EIs5GW5gifdxXFICLanInCQcO2igmYBfztfFw0u6JQp3YC0JqqMDVyQuNmjuwalS ufUr+9aAC0pOUjchIHHom3EXtOh5MRaAXhl54JFJt6KU99N6AzkUyZTf2lKt4V9I rX/4ltsIc1th1uTA126IJdICob8faMw6mlJ4ZZVMpNVbDPWJVyAKDgidGmECsGW1 fGaH53Oqe+RdkvEte3TbaLMYCISqaE5sSdod8ue4GHOC52DpxK6pHTrKd5gaiEUv zg+1s2ateOL06Jia5/gsHBC/X+aEg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrkedtgdeftdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghenucfkphepudejgedrvddu rddufedvrdehudenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvh gvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.2] (174-21-132-51.tukw.qwest.net [174.21.132.51]) by mail.messagingengine.com (Postfix) with ESMTPA id 1A32DD60057; Fri, 18 Oct 2019 20:04:22 -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> From: Adrian Klaver Message-ID: <7299c63c-fcdc-20fc-c955-4cd330cc4580@aklaver.com> Date: Fri, 18 Oct 2019 17:04:21 -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 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. > >> Just trying to figure why one is worse then the other. > > Any time a user loses data, it is worse. The preference for not > having data loss is why Pleroma uses PostgreSQL as it's database of > choice, as PostgreSQL has traditionally valued durability. If we > should not use PostgreSQL, just say so. There are any number of ways you can make Postgres lose data that are not related to durability e.g build the following in code: DELETE FROM some_table; and forget the WHERE. > > Ariadne > >> >>> resulted in Pleroma blanking out the `info` structure for users as >>> they performed new activities that incremented that counter. At that >>> time, Pleroma maintained various things like private keys used to sign >>> things in that JSONB column (we no longer do this because of being >>> burned by this several times now), which broke federation temporarily >>> for the affected accounts with other servers for up to a week as those >>> servers had to learn new public keys for those accounts (since the >>> original private keys were lost). >>> >>> I believe that anything that can be catastrophically broken by users >>> not following upgrade instructions precisely is a serious problem, and >>> can lead to serious problems. I am sure that this is not the only >>> project using JSONB which have had users destroy their own data in >>> such a completely preventable fashion. >>> >>> Ariadne >>> >>> >>> >> >> >> -- >> Adrian Klaver >> adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com