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 1iLqdB-00021Z-Gt for pgsql-general@arkaria.postgresql.org; Sat, 19 Oct 2019 15:27:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLqd9-00025C-1z for pgsql-general@arkaria.postgresql.org; Sat, 19 Oct 2019 15:26:59 +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 1iLqd8-000255-OW for pgsql-general@lists.postgresql.org; Sat, 19 Oct 2019 15:26:58 +0000 Received: from mail-oi1-x244.google.com ([2607:f8b0:4864:20::244]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLqd4-0006Q7-8x for pgsql-general@lists.postgresql.org; Sat, 19 Oct 2019 15:26:58 +0000 Received: by mail-oi1-x244.google.com with SMTP id a15so7707110oic.0 for ; Sat, 19 Oct 2019 08:26:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=from:subject:to:cc:references:openpgp:autocrypt:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=otXycA8zYuCJoRNFuQoPlFj1AuAP8CNMql/k/Mcfs8E=; b=n5eiS5BHiu292hfkOvcXjJjxpFRJYUs6mBGbEwXWqxzBVxot4AEQ1JjJ/h6oB3EQql 01qFLOgAphTS5jig9GFU7ipdx2d2Piyhe8o452GR896c9nQsuA1jcO9I5kj3kMrUaCyW 6SZLjSrvVvTbdzbADNySt4N4HMKcD4crV/PzJVOcqsWOtDu6phKXenL5ahOAweWxxQuo gvEhCb2SvGbaKubU0zS6S3VnWdP6yPPNXYdcWrzKqXnhaFHBdPa32HAbR0q1l5kaG+wW 431w2CUlBnApa760wKReBGIXuriVWQfuEJqSLheKNKqcbInmetYtA0dJxrNLAZ7plL6F pV/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:openpgp:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-transfer-encoding:content-language; bh=otXycA8zYuCJoRNFuQoPlFj1AuAP8CNMql/k/Mcfs8E=; b=ou0VtJvFByCL2hgzpQPIKuHkvdV31Q9jXCxu2jTTPP7+g5cnu73OryD/mP+X0ZYc05 DPyzlN6eT+gjHweWX/HcPRIVwfZS1QCcjkoFg6L+R1GFiQTZ/vj1EjGi3cywrq+K1GsR WEE1vYgD6Q0wYfvB733L5tNzpHgi6RGcxZhemJzmIfCcc1vX7AYthIlPokymYhx9quZb FlXdSqtpWOQOumjrTtlLQ8HBnDCQ7NVKLnbhBFJ0irOaTkfV2KQN1ZuWOYUZfaJngOXm DoGu4Y/rN/9fg+YPYuxtuzfdugblgX46jYI99DZrlK9C1wsbeQNWdNLJhyWMdScQJuPv uTrA== X-Gm-Message-State: APjAAAUlGhd6Ih6gQAgLL+9Upiyy+forX0vo2wmaYR6CIBK35kXm0xwB MDjTGWugI6zwG3vc5d3VGg87 X-Google-Smtp-Source: APXvYqwceJceaikaKvptzocSBWU7dTkTSVEBLsjF2KAzP63UTsT0vddsu0zX1ZlT/MOsA99ep/OrKA== X-Received: by 2002:a05:6808:114:: with SMTP id b20mr7448587oie.114.1571498811398; Sat, 19 Oct 2019 08:26:51 -0700 (PDT) Received: from [192.168.10.146] (99-10-92-30.lightspeed.rlghnc.sbcglobal.net. [99.10.92.30]) by smtp.gmail.com with ESMTPSA id z5sm2427780oto.77.2019.10.19.08.26.50 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 19 Oct 2019 08:26:50 -0700 (PDT) From: Andrew Dunstan X-Google-Original-From: Andrew Dunstan Subject: Re: jsonb_set() strictness considered harmful to data To: Mark Felder , pgsql-general@lists.postgresql.org Cc: pgsql-hackers@lists.postgresql.org References: Openpgp: preference=signencrypt Autocrypt: addr=andrew.dunstan@2ndQuadrant.com; prefer-encrypt=mutual; keydata= xsBNBE7KWFkBCAClridxur2AIc7eW2AR7izbfp3EnNefie2HbLF0izW5Ik5UjX2HBXBx4syI gY6b0ugohXrr274+baoAlvSbq6cAoQuEVrk5IZFzt20b1Xkx65FwGSEj526yiKLocqkJceSq Xr9xcA5SGY+FZv441chh5SU92v4q6z+6LPpoHOh97ptAVXZYNTtU0LevyvD5lja0TzbvJm6C eFXitJfnm1pLEr0DGJCR/iUOl/N62Kh4855zZC7NHIjQHPOvV5Stz/l5ilDhvGVk+xkXFPys SjZoUr1rXhYLpiyi5sR0X9FHXT0KnGuz1F5ERO7ZTLSSQ6fJwPj6gOk9K+vvoKvoeql5ABEB AAHNL0FuZHJldyBEdW5zdGFuIDxhbmRyZXcuZHVuc3RhbkAybmRRdWFkcmFudC5jb20+wsCY BBMBAgBCAhsDBgsJCAcDAgYVCAIJCgsEFgIDAQIeAQIXgAUJDyV6hBYhBOQ+WEYd/Hy/RGkV pZn6f8tZ/DuBBQJcH/EVAhkBAAoJEJn6f8tZ/DuB7BEH/iDWZEbVft2m2MT6SPjARzTupt/l xLLZ/Cl6n2US2hov5+fBbnR5xvECk/YCOfG9ICvKNlpFjceqNLRKStd8cmndnI+kDw91uNM2 hpL4aJMeW65XCHvqXlOLSwv5chBpXz9NG7FMaJ+q+59sWEWjcTNmjXgkEHn53y+1pc7nSLDe cr5ZI1aB3nGXRm1SoDY5PYZkUKMykW+YBZwgcs/Q5tRaE+siN7DKbz8E628+PerwAkmHPVXN MdJFD6gs4S0gDFA8IxcyIwZU2v4By+brzcJ9Se3FK5BYxBDHtaYZxgSrJAvOUOrLIZPQTQfL TtnVs3C3JTfrxCH1ceHtnA6dRuTOwE0ETspYWQEIANGc4zQULOxhbqO2dyD51YhqCNRmm9oK Waqf+wmW4tpDe/VVcxAnNizd4LWCHfzpb5cHAtGkOPePMfzWVf6nvdF7d3eglbtf59+zG7O7 llV0xSSoFiieQBsrGvqDInXYX/4mRRXMtyhM353/tixC9RWLs1oofyYmCPPXXY7h9R7en3B8 BoVrRFcdzlIY/NFNhFGW/9dkEiGjgna2Rk6e15kln4ZvFBWUg23p93w/pqXcxY6+k/8TEk+C 4R+M6w7o2PLGOjdZ+kPiUcw5H85zf/yZJwQXzisXaNduwWB6Vads9YC9dj6kPR1c4VGRqAaY L++LAEOqrlvm2TvqQqZRtnEAEQEAAcLAZQQYAQIADwIbDAUCXA6fpAUJDyV6xAAKCRCZ+n/L Wfw7geU4B/9vFFsVmWxerDWgNjyiK80zIPmlw/iwdUlU6FIu0JWLMg4093kvxBs7RMZV9hBq n3ekBvD4IvFsVLJrw9qzSI2FH3UeYy5WXpEs01Ppia60NOlvRDqXbryzuvfe+iYo6bVP3Ikr TPTNYL/BePUTD2DPvSI82SbGTyzuA7SJv6NRqqQDMIy1fU6KyQFLmQJp/QJyhKWzm9jWh8ee w8jVaoMY0Kbq5N7RqisEx1Lu/Iw646UphdYzQNymf+kbiU5Kf4I6DhzV8w9sie1Bo1juNj3I c5/fFJsmxljbnaDbXPcnFsur1Oo9mIHFakgWgOBMuxoWfjONiLjjLh/9+ZIqSSLX Message-ID: <7dc242bd-b78f-2e0d-0c38-7b4b299f11bf@2ndQuadrant.com> Date: Sat, 19 Oct 2019 11:26:50 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Content-Language: en-MW List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 10/18/19 3:10 PM, Mark Felder wrote: > > 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. > > The hyperbole here is misplaced. There is a difference between a bug and a POLA violation. This might be the latter, but it isn't the former. So please tone it down a bit. It's not the function that's unsafe, but the ill-informed use of it. We invented jsonb_set() (credit to Dmitry Dolgov). And we've had it since 9.5. That's five releases ago.  So it's a bit late to be coming to us telling us it's not safe (according to your preconceptions of what it should be doing). We could change it prospectively (i.e. from release 13 on) if we choose. But absent an actual bug (i.e. acting contrary to documented behaviour) we do not normally backpatch such changes, especially when there is a simple workaround for the perceived problem. And it's that policy that is in large measure responsible for Postgres' deserved reputation for stability. Incidentally, why is your function written in plpgsql? Wouldn't a simple SQL wrapper be better? create or replace function safe_jsonb_set     (target jsonb, path text[], new_value jsonb, create_missing boolean default true) returns jsonb as $func$     select case when new_value is null then target else jsonb_set(target, path, new_value, create_missing) end $func$ language sql; And if we were to change it I'm not at all sure that we should do it the way that's suggested here, which strikes me as no more intuitive than the current behaviour. Rather I think we should possibly fill in a json null in the indicated place. cheers andrew -- Andrew Dunstan https://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services