Concatenating NULL with JSONB value return NULL

From: Jong-won Choi <jongwon(at)ticketsquad(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Concatenating NULL with JSONB value return NULL
Date: 2016-12-18 22:52:18
Message-ID: 0e13e561-a5aa-d040-1d43-60ca3cb50075@ticketsquad.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

PostgreSQL 9.6.1

Hi

I have a NULL-able JSONB type column and want to perform upsert,
concatenating with the existing value.

The query looks like (campaigns and facts columns are JSONB type, in the
below) :

INSERT INTO Fan (oid, campaigns, facts) VALUES (189,'{"campaign-id":
"12345"}','{"attended": false}') ON CONFLICT (oid)
DO UPDATE SET campaigns = EXCLUDED.campaigns, facts = fan.facts ||
EXCLUDED.facts RETURNING *;

And this does not work when the existing JSONB type column has NULL
value. For example:

select NULL::JSONB || '{"A": "b"}'::JSONB;

I would expect the above returns '{"A": "b"}', but PostgreSQL does not
work as I expected.

What's the best way to make concatenating with NULL returns the
right-hand side value?
(One way I can think of is, giving a default value of '{}' instead of
NULL, but I'd like to know any alternatives)

Thanks

- Jong-won

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John R Pierce 2016-12-18 23:08:24 Re: Concatenating NULL with JSONB value return NULL
Previous Message Torsten Förtsch 2016-12-18 20:07:03 streaming replication protocol in Perl?