PostgreSQL Delete Property

Source: Internet
Author: User
Tags postgresql

PostgreSQL Update and delete property from Jsonb column
Up vote 2 down vote favorite

From this article, I tried to update or delete property of a JSONB column:

CREATE TABLE xxx (id BIGSERIAL, data JSONB);INSERT INTO xxx(data) VALUES( ‘{"a":1,"b":2}‘ );SELECT * FROM data; id |       data       ----+------------------  1 | {"a": 1, "b": 2}

Create the update function:

CREATE FUNCTION jsonb_merge(JSONB, JSONB) RETURNS JSONB AS $$WITH json_union AS (    SELECT * FROM JSONB_EACH($1)    UNION ALL    SELECT * FROM JSONB_EACH($2)) SELECT JSON_OBJECT_AGG(key, value)::JSONB FROM json_union;$$ LANGUAGE SQL;

Testing:

-- replaceUPDATE xxx SET data = jsonb_merge(data,‘{"b":3}‘) WHERE id = 1;SELECT * FROM xxx; id |       data       ----+------------------  1 | {"a": 1, "b": 3}-- appendUPDATE xxx SET data = jsonb_merge(data,‘{"c":4}‘) WHERE id = 1;SELECT * FROM xxx; id |           data       ----+-------------------------  1 | {"a": 1, "b": 3, "c": 4}

The question is:

    1. Is there any drawback of using JSONB_EACH (Jsonb_merge) instead of (from the article) in this case JSONB_EACH_TEXT ?

    2. How to modify the and jsonb_merge if the second parameter property value are null (something like {"b":null} ) the value would be erase D?

.

-- removeUPDATE xxx SET data = jsonb_merge(data,‘{"b":null}‘) WHERE id = 1;SELECT * FROM xxx; id |       data       ----+-----------------  1 | {"a": 1, "c": 4}
PostgreSQL postgresql-9.4
Share|improve this question Edited Mar ' at 10:23 Asked Mar ' at 6:06 kokizzu2733
Add a Comment |
1 Answer1Active oldest votes
Up vote 3 down vote accepted

Question 1
There should be no signicant drawbacks. As the is value converted back to jsonb anyhow I would guess it would was more efficient to keep it's that's the whole time .


Question 2
Just Replace your function with the following (only the part WHERE key NOT IN ... added):

CREATE FUNCTION jsonb_merge(JSONB, JSONB) RETURNS JSONB AS $$WITH json_union AS (    SELECT * FROM JSONB_EACH($1)    UNION ALL    SELECT * FROM JSONB_EACH($2)) SELECT JSON_OBJECT_AGG(key, value)::JSONB     FROM json_union     WHERE key NOT IN (SELECT key FROM json_union WHERE value =‘null‘);$$ LANGUAGE SQL;

PostgreSQL Delete Property

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.