pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Unexpected behavior of jsonb_set() with a `null` value
3+ messages / 3 participants
[nested] [flat]

* Unexpected behavior of jsonb_set() with a `null` value
@ 2019-09-12 10:55 Марк <kolypto@gmail.com>
  2019-09-12 14:11 ` Re: Unexpected behavior of jsonb_set() with a `null` value Thomas Kellerer <spam_eater@gmx.net>
  0 siblings, 1 reply; 3+ messages in thread

From: Марк @ 2019-09-12 10:55 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org

Dear Support Team,

With `jsonb_set()`, when `new_value` is `NULL`, the function would **erase
the whole object**:

postgres=# SELECT jsonb_set('{"something":1}'::jsonb, '{language}', null,
true);
jsonb_set
-----------

(1 row)

This is really dangerous for it may lead to data loss. Imaging the `
new_value` argument being a function which returns `NULL` values in some
cases.
The expected behavior is, perhaps, to convert an SQL `NULL` value to JSONB
null value: 'null'::jsonb
This way no data would be lost.

Or at the very least, this behavior should be documented.

Thank you in advance,
Mark

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Unexpected behavior of jsonb_set() with a `null` value
  2019-09-12 10:55 Unexpected behavior of jsonb_set() with a `null` value Марк <kolypto@gmail.com>
@ 2019-09-12 14:11 ` Thomas Kellerer <spam_eater@gmx.net>
  2019-09-12 14:32   ` Re: Unexpected behavior of jsonb_set() with a `null` value Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Thomas Kellerer @ 2019-09-12 14:11 UTC (permalink / raw)
  To: Марк <kolypto@gmail.com>; pgsql-bugs@lists.postgresql.org

Марк schrieb am 12.09.2019 um 12:55:
> With `jsonb_set()`, when `new_value` is `NULL`, the function would **erase the whole object**:
> 
> postgres=# SELECT jsonb_set('{"something":1}'::jsonb, '{language}', null, true);         
> jsonb_set  
> -----------
>  
> (1 row)
> 
> This is really dangerous for it may lead to data loss. Imaging the `new_value` argument being a function which returns `NULL` values in some cases.
> The expected behavior is, perhaps, to convert an SQL `NULL` value to JSONB null value: 'null'::jsonb
> This way no data would be lost.
> 
> Or at the very least, this behavior should be documented.

I have asked the same: https://www.postgresql.org/message-id/flat/qfkua9%242q0e%241%40blaine.gmane.org

In short: not a bug ;) 






^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Unexpected behavior of jsonb_set() with a `null` value
  2019-09-12 10:55 Unexpected behavior of jsonb_set() with a `null` value Марк <kolypto@gmail.com>
  2019-09-12 14:11 ` Re: Unexpected behavior of jsonb_set() with a `null` value Thomas Kellerer <spam_eater@gmx.net>
@ 2019-09-12 14:32   ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2019-09-12 14:32 UTC (permalink / raw)
  To: Thomas Kellerer <spam_eater@gmx.net>; +Cc: Марк <kolypto@gmail.com>; pgsql-bugs@lists.postgresql.org

Thomas Kellerer <spam_eater@gmx.net> writes:
> Марк schrieb am 12.09.2019 um 12:55:
>> The expected behavior is, perhaps, to convert an SQL `NULL` value to JSONB null value: 'null'::jsonb

> I have asked the same: https://www.postgresql.org/message-id/flat/qfkua9%242q0e%241%40blaine.gmane.org

There are other more-apropos threads in the archives, though I'm too
lazy to go find them right now.  IMO the problem here is an assumption
that SQL NULL should be interchangeable with a JSON NULL.  It's not,
and we don't intend to make it so, because (a) the definitional quagmire
that would result is bottomless; (b) people asking for this are generally
trying to use SQL NULL as a live data value, which is almost always just
a bad idea.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2019-09-12 14:32 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-09-12 10:55 Unexpected behavior of jsonb_set() with a `null` value Марк <kolypto@gmail.com>
2019-09-12 14:11 ` Thomas Kellerer <spam_eater@gmx.net>
2019-09-12 14:32   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox