From: | Michael Paquier <michael(dot)paquier(at)gmail(dot)com> |
---|---|
To: | Andreas Kretschmer <akretschmer(at)spamfence(dot)net> |
Cc: | PostgreSQL mailing lists <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: replacing jsonb field value |
Date: | 2015-06-01 04:38:56 |
Message-ID: | CAB7nPqQmpD3_L+m=4BirCq-gpWG35JEnT8MCbTGEbeVn=woJVA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On Sat, May 30, 2015 at 9:10 PM, Andreas Kretschmer
<akretschmer(at)spamfence(dot)net> wrote:
> Michael Paquier <michael(dot)paquier(at)gmail(dot)com> wrote:
>
>>
>> Append the new value to it the existing field, jsonb has as property
>> to enforce key uniqueness, and uses the last value scanned for a given
>> key.
>
> can you show a simple example, how to append a jsonb to an jsonb-field?
> Maybe i'm blind, but i can't find how it works.
You need some extra magic to do it in 9.4, for example that (not the
best performer by far that's simple enough):
=# CREATE FUNCTION jsonb_append(jsonb, jsonb)
RETURNS jsonb AS $$
WITH json_union AS
(SELECT * FROM jsonb_each_text($1)
UNION ALL
SELECT * FROM jsonb_each_text($2))
SELECT json_object_agg(key, value)::jsonb FROM json_union;
$$ LANGUAGE SQL;
CREATE FUNCTION
=# SELECT jsonb_append('{"a1":"v1", "a2":"v2"}', '{"a1":"b1"}');
jsonb_append
--------------------------
{"a1": "b1", "a2": "v2"}
(1 row)
Googling would show up more performant functions for sure, usable with
9.4, and there is even jsonbx.
--
Michael
From | Date | Subject | |
---|---|---|---|
Next Message | Michael Paquier | 2015-06-01 04:45:13 | Re: JSONB matching element count |
Previous Message | Adrian Klaver | 2015-06-01 00:09:02 | Re: Postgresql 9.4 upgrade openSUSE13.1 |