[GENERAL] Concatenating NULL with JSONB value return NULL - Mailing list pgsql-general

From Jong-won Choi
Subject [GENERAL] Concatenating NULL with JSONB value return NULL
Date
Msg-id 0e13e561-a5aa-d040-1d43-60ca3cb50075@ticketsquad.com
Whole thread Raw
Responses Re: [GENERAL] Concatenating NULL with JSONB value return NULL
List 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


pgsql-general by date:

Previous
From: Torsten Förtsch
Date:
Subject: [GENERAL] streaming replication protocol in Perl?
Next
From: John R Pierce
Date:
Subject: Re: [GENERAL] Concatenating NULL with JSONB value return NULL