pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Christoph Moench-Tegeder <cmt@burggraben.net>
To: Ariadne Conill <ariadne@dereferenced.org>
Cc: pgsql-general@lists.postgresql.org
Cc: feld@freebsd.org
Subject: Re: jsonb_set() strictness considered harmful to data
Date: Fri, 18 Oct 2019 23:50:18 +0200
Message-ID: <20191018215018.GB56427@elch.exwg.net> (raw)
In-Reply-To: <CAAOiGNwUsgM-UBqsJH_mriCFr-JCPxD+pQoXNvyOEQPc+YHQRQ@mail.gmail.com>
References: <CAAOiGNwUsgM-UBqsJH_mriCFr-JCPxD+pQoXNvyOEQPc+YHQRQ@mail.gmail.com>

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





view thread (83+ messages)  latest in thread

Message-ID: <20191018215018.GB56427@elch.exwg.net>
Permalink:  ../20191018215018.GB56427@elch.exwg.net/
Also on:    postgresql.org/message-id/20191018215018.GB56427@elch.exwg.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: cmt@burggraben.net, ariadne@dereferenced.org, feld@freebsd.org
  Subject: Re: jsonb_set() strictness considered harmful to data
  In-Reply-To: <20191018215018.GB56427@elch.exwg.net>
  References: <CAAOiGNwUsgM-UBqsJH_mriCFr-JCPxD+pQoXNvyOEQPc+YHQRQ@mail.gmail.com> <20191018215018.GB56427@elch.exwg.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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