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 1iLXej-0000Xy-CP for pgsql-general@arkaria.postgresql.org; Fri, 18 Oct 2019 19:11:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLXeh-0003kc-C0 for pgsql-general@arkaria.postgresql.org; Fri, 18 Oct 2019 19:11:19 +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 1iLXeg-0003is-3q; Fri, 18 Oct 2019 19:11:18 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iLXeb-0006lz-OY; Fri, 18 Oct 2019 19:11:16 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 6F9A1222C2; Fri, 18 Oct 2019 15:11:12 -0400 (EDT) Received: from imap38 ([10.202.2.88]) by compute3.internal (MEProxy); Fri, 18 Oct 2019 15:11:12 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=ZYq2D5 43Q7dVsGsfp0BlTQeQ5SclSFpXn4uFKOT2DHU=; b=QukBiM4n6ab5N+vUgHViNj uk8hu63zBp/oAMI9Hx9Ika3/MT9fAXP22AzOxaeAZ5wsw1Y8ecfvLwbn4DagicuF 5y/dCm7l19LXC95+4OSmTaVRlrdpNvclfECQJIvlhg3FSWw6REysbMHE3LFd5jtL 9H8r5S3D2COhCCOlO6OWXjRPMnyKf8RINsbH4XQNBnG43ecdyV21BtET58j6h2UW 7OAoNBGMRhRd6Xm475PkpG+Y+yXAM4Ynjfn07H7HHMuqc53AYq9GYBLPKDW8s3mU Ys1BaJ3+Ha6UPZC4z8lnyXyhXgRRrwYIImXD5CknQ2RDPjCWK+nyyz7E1iOCDRNA == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrjeelgddufedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefofgggkfgjfhffhffvufgtsehttd ertderredtnecuhfhrohhmpedfofgrrhhkucfhvghluggvrhdfuceofhgvlhgusefhrhgv vgeuufffrdhorhhgqeenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgpdhplh gvrhhomhgrrdhsohgtihgrlhenucfrrghrrghmpehmrghilhhfrhhomhepfhgvlhgusefh rhgvvgeuufffrdhorhhgnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id 015794C000A4; Fri, 18 Oct 2019 15:11:11 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.1.7-360-g7dda896-fmstable-20191004v2 Mime-Version: 1.0 Message-Id: In-Reply-To: References: Date: Fri, 18 Oct 2019 14:10:51 -0500 From: "Mark Felder" To: pgsql-general@lists.postgresql.org Cc: pgsql-hackers@lists.postgresql.org Subject: Re: jsonb_set() strictness considered harmful to data Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, Oct 18, 2019, at 12:37, Ariadne Conill wrote: > Hello, > > I am one of the primary maintainers of Pleroma, a federated social > networking application written in Elixir, which uses PostgreSQL in > ways that may be considered outside the typical usage scenarios for > PostgreSQL. > > Namely, we leverage JSONB heavily as a backing store for JSON-LD > documents[1]. We also use JSONB in combination with Ecto's "embedded > structs" to store things like user preferences. > > The fact that we can use JSONB to achieve our design goals is a > testament to the flexibility PostgreSQL has. > > However, in the process of doing so, we have discovered a serious flaw > in the way jsonb_set() functions, but upon reading through this > mailing list, we have discovered that this flaw appears to be an > intentional design.[2] > > A few times now, we have written migrations that do things like copy > keys in a JSONB object to a new key, to rename them. These migrations > look like so: > > 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] > > This is not acceptable. PostgreSQL is a database that is renowned for > data integrity, but here it is wiping out data when it encounters a > failure case. The way jsonb_set() should fail in this case is to > simply return the original input: it should NEVER return SQL null. > > But hey, we've been burned by this so many times now that we'd like to > donate a useful function to the commons, consider it a mollyguard for > the real jsonb_set() function. > > create or replace function safe_jsonb_set(target jsonb, path > text[], new_value jsonb, create_missing boolean default true) returns > jsonb as $$ > declare > result jsonb; > begin > result := jsonb_set(target, path, coalesce(new_value, > 'null'::jsonb), create_missing); > if result is NULL then > return target; > else > return result; > end if; > end; > $$ language plpgsql; > > This safe_jsonb_set() wrapper should not be necessary. PostgreSQL's > own jsonb_set() should have this safety feature built in. Without it, > using jsonb_set() is like playing russian roulette with your data, > which is not a reasonable expectation for a database renowned for its > commitment to data integrity. > > Please fix this bug so that we do not have to hack around this bug. > It has probably ruined countless people's days so far. I don't want > to hear about how the function is strict, I'm aware it is strict, and > that strictness is harmful. Please fix the function so that it is > actually safe to use. > > [1]: JSON-LD stands for JSON Linked Data. Pleroma has an "internal > representation" that shares similar qualities to JSON-LD, so I use > JSON-LD here as a simplification. > > [2]: https://www.postgresql.org/message-id/flat/qfkua9$2q0e$1@blaine.gmane.org > > [3]: https://git.pleroma.social/pleroma/pleroma/issues/1324 is an > example of data loss induced by this issue. > > Ariadne > This should be directed towards the hackers list, too. What will it take to change the semantics of jsonb_set()? MySQL implements safe behavior here. It's a real shame Postgres does not. I'll offer a $200 bounty to whoever fixes it. I'm sure it's destroyed more than $200 worth of data and people's time by now, but it's something. Kind regards, -- Mark Felder ports-secteam & portmgr alumni feld@FreeBSD.org