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 1iLcBD-0002SH-FH for pgsql-general@arkaria.postgresql.org; Sat, 19 Oct 2019 00:01:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLcBA-0000Ja-3p for pgsql-general@arkaria.postgresql.org; Sat, 19 Oct 2019 00:01:08 +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 1iLcB9-0000JE-OU for pgsql-general@lists.postgresql.org; Sat, 19 Oct 2019 00:01:07 +0000 Received: from tamriel.snowman.net ([2001:470:e38f::11]) by magus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLcB6-0003CY-Mq for pgsql-general@lists.postgresql.org; Sat, 19 Oct 2019 00:01:06 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id AACE65F79F; Fri, 18 Oct 2019 20:01:03 -0400 (EDT) Date: Fri, 18 Oct 2019 20:01:03 -0400 From: Stephen Frost To: Ariadne Conill Cc: Christoph Moench-Tegeder , "pgsql-generallists.postgresql.org" , feld@freebsd.org Subject: Re: jsonb_set() strictness considered harmful to data Message-ID: <20191019000103.GH6962@tamriel.snowman.net> References: <20191018215018.GB56427@elch.exwg.net> <20191018225752.GC56427@elch.exwg.net> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="peDDqO5FReJKJwLy" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --peDDqO5FReJKJwLy Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Greetings, * Ariadne Conill (ariadne@dereferenced.org) wrote: > On Fri, Oct 18, 2019 at 5:57 PM Christoph Moench-Tegeder > wrote: > > ## Ariadne Conill (ariadne@dereferenced.org): > > > Why don't we fix the database engine to not eat data when the > > > jsonb_set() operation fails? > > > > It didn't fail, it worked like SQL (you've been doing SQL for too > > long when you get used to the NULL propagation, but that's still > > what SQL does - check "+" for example). > > And changing a function will cause fun for everyone who relies on > > the current behaviour - so at least it shouldn't be done on a whim > > (some might argue that a whim was what got us into this situation > > in the first place). >=20 > NULL propagation makes sense in the context of traditional SQL. What > users expect from the JSONB support is for it to behave as JSON > manipulation behaves everywhere else. It makes sense that 2 + NULL > returns NULL -- it's easily understood as a type mismatch. It does > not make sense that jsonb_set('{}'::jsonb, '{foo}', NULL) returns NULL > because a *value* was SQL NULL. In this case, it should, at the > least, automatically coalesce to 'null'::jsonb. 2 + NULL isn't a type mismatch, just to be clear, it's "2 + unknown =3D unknown", which is pretty reasonable, if you accept the general notion of what NULL is to begin with. And as such, what follows with "set this blob of stuff to include this unknown thing ... implies ... we don't know what the result of the set is and therefore it's unknown" isn't entirely unreasonable, but I can agree that in this specific case, because what we're dealing with regarding JSONB is a complex data structure, not an individual value, that it's surprising to a developer and there can be an argument made there that we should consider changing it. > > Continuing along that thought, I'd even argue that your are > > writing code which relies on properties of the data which you never > > guaranteed. There is a use case for data types and constraints. >=20 > There is a use case, but this frequently comes up as a question people > ask. At some point, you have to start pondering whether the behaviour > does not make logical sense in the context that people frame the JSONB > type and it's associated manipulation functions. It is not *obvious* > that jsonb_set() will trash your data, but that is what it is capable > of doing. In a database that is advertised as being durable and not > trashing data, even. Having the result of a call to a strict function be NULL isn't "trashing" your data. > > Not that I'm arguing for maximum surprise in programming, but > > I'm a little puzzled when people eschew thew built-in tools and > > start implmenting them again side-to-side with what's already > > there. >=20 > If you read the safe_jsonb_set() function, all we do is coalesce any > SQL NULL to 'null'::jsonb, which is what it should be doing anyway, I'm not convinced that this is at all the right answer, particularly since we don't do that generally. We don't return the string 'null' when you do: NULL || 'abc', we return NULL. There might be something we can do here that doesn't result in the whole jsonb document becoming NULL though. > and then additionally handling any *unanticipated* failure case on top > of that. While you are arguing that we should use tools to work > around unanticipated effects (that are not even documented -- in no > place in the jsonb_set() documentation does it say "if you pass SQL > NULL to this function as a value, it will return SQL NULL"), I am > arguing that jsonb_set() shouldn't set people up for their data to be > trashed in the first place. The function is marked as strict, and the meaning of that is quite clearly defined in the documentation. I'm not against including a comment regarding this in the documentation, to be clear, though I seriously doubt it would actually have changed anything in this case. Thanks, Stephen --peDDqO5FReJKJwLy Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJdqlI/AAoJEO1sijiDR2RVH8YP+waRscamcgKJmOST+8NBpFL5 kt/cARYlbdW8iZCD3jVvXmJRt02y3uMaywDql89zzZt8x+COXSbcSKy5YJpiPDZT rAHbsoUZGZJ+vhQICunrss5Y+4wtT3HDj9XQL4CNY6dPelI6J5eyF24ELcjRgs5b FiRCFS44Sbdq784dhoIEs0vRZBHRVSgOmsBltoMBwtGezzyCF4FvBb29Itfh5iGy gxAZCq8ruO+uF1/DUCCiOBIjcsIIWmW+XjbjYZIt2R64kY/y96N0dVLPhgtArQ4o YycM5AWLLPD1u/R/nhNsQlrLe5C3NB/n4RvbV4rP8BMCi/adbcNWzeHFB48o0tnO yorL5d+DrzeOz85BYFSSIcV0nuOkKaqLOvS1PcFDEDfqaLJlFzm9g+iJ28uoC9Ek ZjsDE2Ob+bly2LY535r/ffiQ+wCLAK6zXT96SIITZCXJZok5KOLgR/q6BVb79ASK 25xo05fQfBQRulqJDUUzVQcnTMbYH0fmENsbOkMKW2V4ln5ANiAEB6ZYfRrCniH1 nrqwJ/ny1nIRF9TCyS0wGwkauXLgkirTUJmYim/Jv95Dg8soSMbG/pZQ8+qoRU21 Z0CGnFIbZzgKnkNcdFsBSOhjMkQj1JhQqD1pbPxz8Vv+mj+jBHR5UeoCDHXXIiWZ uxyugLPlT68JPbH7b3JD =zCVr -----END PGP SIGNATURE----- --peDDqO5FReJKJwLy--