Re: jsonb_set() strictness considered harmful to data - Mailing list pgsql-general

From Christoph Moench-Tegeder
Subject Re: jsonb_set() strictness considered harmful to data
Date
Msg-id 20191018215018.GB56427@elch.exwg.net
Whole thread Raw
In response to jsonb_set() strictness considered harmful to data  (Ariadne Conill <ariadne@dereferenced.org>)
Responses Re: jsonb_set() strictness considered harmful to data
Re: jsonb_set() strictness considered harmful to data
List pgsql-general
## Ariadne Conill (ariadne@dereferenced.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



pgsql-general by date:

Previous
From: "Mark Felder"
Date:
Subject: Re: jsonb_set() strictness considered harmful to data
Next
From: Edilmar Alves
Date:
Subject: Replication of Replication