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 1iLbjG-0001SI-2j for pgsql-general@arkaria.postgresql.org; Fri, 18 Oct 2019 23:32:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLbjE-0002JS-Mz for pgsql-general@arkaria.postgresql.org; Fri, 18 Oct 2019 23:32:16 +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 1iLbjE-0002Ip-7a for pgsql-general@lists.postgresql.org; Fri, 18 Oct 2019 23:32:16 +0000 Received: from mail-io1-xd44.google.com ([2607:f8b0:4864:20::d44]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLbj7-0002wf-4I for pgsql-general@lists.postgresql.org; Fri, 18 Oct 2019 23:32:15 +0000 Received: by mail-io1-xd44.google.com with SMTP id c25so9345236iot.12 for ; Fri, 18 Oct 2019 16:32:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dereferenced-org.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=08vzST5ESwBaop8paFAufWiyIEzeq+55Xmr9y1j2aks=; b=2SmXhftSvZ2xcvvgE/bcphktIiaPghwFJm+y1Xq1XFkvLzDn3uP3tLAiyPS++uGQLb 9RuonGF7147N6sFil257pdWvHEC2wCtV8X5o0H95oTXEZxm8U/NBmUzgwmd9R6wBYq6S cCIdRwl3xdcl1HSFOa2J2Cieyck5+3huPuC1BQUn9ve61IJs6ogfUHZTYAU6lgU+mla3 vsKUA+UOji9k7pyfT6PyDmVyZ/AOoRtGQh3ecDraSHdnjOjvrobd5h1GqQEftb8+73DE P38qProauoguZES5JyyFyxGD7WW/CH13Tb3LCKVI0kUmKwLTy5cava+MwZ+ib2LR0hJh E0pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=08vzST5ESwBaop8paFAufWiyIEzeq+55Xmr9y1j2aks=; b=b595V5APzm/i7Ovy7L+W2Jvl63gkLxC4U8zgur2n9us72wPObVNG3+/s3fOb619777 nlQhyM1vHFUmKaFvEh3pUFlS/5pQL2T6thV/fAGTv1fNggDdoQiVMlLTpmy848I+xwNp QjdyV6/lAo/kkPgNwNrWu4qcC0enlmHAqoElDwn/tI2Cpn8RPJenxFQMc0M5RlZ4Q3NV rM9JSteV1T+pjhwhKvbvAjlQ/r/sjECsZDRZ0twnsi9eYBVbD2O6WoteCPPs+v6EaqqQ 5jSod0Ze6ZPQ/mDFL1cWX9wqsFN/XxUOKYi3k2I6azBj8P/qoiBgm+hs40zV3KiDPbP/ D/mg== X-Gm-Message-State: APjAAAVadaBp4jPkQcevzKTLdDWBcRlDpetG0T/Q3UL1O5waG/v64BVZ svSsXnyujYN3q1V/UObn50qufEAGcDIn4ef1DYbTXg== X-Google-Smtp-Source: APXvYqxmJhL8IGOz3QNkUbmKPTZ90xYujQevVpCKiaGnIZ30kce2HM7QQWuY8PaRl5yjseK6nENeWXkPyfucAtG4G88= X-Received: by 2002:a5d:8450:: with SMTP id w16mr10542476ior.11.1571441527204; Fri, 18 Oct 2019 16:32:07 -0700 (PDT) MIME-Version: 1.0 References: <20191018215018.GB56427@elch.exwg.net> In-Reply-To: From: Ariadne Conill Date: Fri, 18 Oct 2019 18:31:56 -0500 Message-ID: Subject: Re: jsonb_set() strictness considered harmful to data To: Adrian Klaver Cc: "David G. Johnston" , Christoph Moench-Tegeder , "pgsql-generallists.postgresql.org" , feld@freebsd.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello, On Fri, Oct 18, 2019 at 6:01 PM Adrian Klaver w= rote: > > 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=3Djsonb_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 happen= s, > >>>> 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 =3D 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 f= unction 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 deb= atable 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. > 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. 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