Re: jsonb_set() strictness considered harmful to data

From: Christoph Moench-Tegeder <cmt(at)burggraben(dot)net>
To: Ariadne Conill <ariadne(at)dereferenced(dot)org>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org, feld(at)freebsd(dot)org
Subject: Re: jsonb_set() strictness considered harmful to data
Date: 2019-10-18 21:50:18
Message-ID: 20191018215018.GB56427@elch.exwg.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

## Ariadne Conill (ariadne(at)dereferenced(dot)org):

> 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]

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 = jsonb_set(info, '{bar}', info->'foo')
WHERE info->'foo' IS NOT NULL;

No special wrappers required.

Regards,
Christoph

--
Spare Space

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Edilmar Alves 2019-10-18 21:54:34 Replication of Replication
Previous Message Mark Felder 2019-10-18 19:10:51 Re: jsonb_set() strictness considered harmful to data

Browse pgsql-hackers by date

  From Date Subject
Next Message David G. Johnston 2019-10-18 22:00:50 Re: jsonb_set() strictness considered harmful to data
Previous Message Mark Felder 2019-10-18 19:10:51 Re: jsonb_set() strictness considered harmful to data