How to perform update operations on columns of type JSONB in Postgres 9.4

243,501

Solution 1

Ideally, you don't use JSON documents for structured, regular data that you want to manipulate inside a relational database. Use a normalized relational design instead.

JSON is primarily intended to store whole documents that do not need to be manipulated inside the RDBMS. Related:

Updating a row in Postgres always writes a new version of the whole row. That's the basic principle of Postgres' MVCC model. From a performance perspective, it hardly matters whether you change a single piece of data inside a JSON object or all of it: a new version of the row has to be written.

Thus the advice in the manual:

JSON data is subject to the same concurrency-control considerations as any other data type when stored in a table. Although storing large documents is practicable, keep in mind that any update acquires a row-level lock on the whole row. Consider limiting JSON documents to a manageable size in order to decrease lock contention among updating transactions. Ideally, JSON documents should each represent an atomic datum that business rules dictate cannot reasonably be further subdivided into smaller datums that could be modified independently.

The gist of it: to modify anything inside a JSON object, you have to assign a modified object to the column. Postgres supplies limited means to build and manipulate json data in addition to its storage capabilities. The arsenal of tools has grown substantially with every new release since version 9.2. But the principal remains: You always have to assign a complete modified object to the column and Postgres always writes a new row version for any update.

Some techniques how to work with the tools of Postgres 9.3 or later:

This answer has attracted about as many downvotes as all my other answers on SO together. People don't seem to like the idea: a normalized design is superior for regular data. This excellent blog post by Craig Ringer explains in more detail:

Another blog post by Laurenz Albe, another official Postgres contributor like Craig and myself:

Solution 2

If you're able to upgrade to Postgresql 9.5, the jsonb_set command is available, as others have mentioned.

In each of the following SQL statements, I've omitted the where clause for brevity; obviously, you'd want to add that back.

Update name:

UPDATE test SET data = jsonb_set(data, '{name}', '"my-other-name"');

Replace the tags (as oppose to adding or removing tags):

UPDATE test SET data = jsonb_set(data, '{tags}', '["tag3", "tag4"]');

Replacing the second tag (0-indexed):

UPDATE test SET data = jsonb_set(data, '{tags,1}', '"tag5"');

Append a tag (this will work as long as there are fewer than 999 tags; changing argument 999 to 1000 or above generates an error. This no longer appears to be the case in Postgres 9.5.3; a much larger index can be used):

UPDATE test SET data = jsonb_set(data, '{tags,999999999}', '"tag6"', true);

Remove the last tag:

UPDATE test SET data = data #- '{tags,-1}'

Complex update (delete the last tag, insert a new tag, and change the name):

UPDATE test SET data = jsonb_set(
    jsonb_set(data #- '{tags,-1}', '{tags,999999999}', '"tag3"', true), 
    '{name}', '"my-other-name"');

It's important to note that in each of these examples, you're not actually updating a single field of the JSON data. Instead, you're creating a temporary, modified version of the data, and assigning that modified version back to the column. In practice, the result should be the same, but keeping this in mind should make complex updates, like the last example, more understandable.

In the complex example, there are three transformations and three temporary versions: First, the last tag is removed. Then, that version is transformed by adding a new tag. Next, the second version is transformed by changing the name field. The value in the data column is replaced with the final version.

Solution 3

This is coming in 9.5 in the form of jsonb_set by Andrew Dunstan based on an existing extension jsonbx that does work with 9.4

Solution 4

For those that run into this issue and want a very quick fix (and are stuck on 9.4.5 or earlier), here is a potential solution:

Creation of test table

CREATE TABLE test(id serial, data jsonb);
INSERT INTO test(data) values ('{"name": "my-name", "tags": ["tag1", "tag2"]}');

Update statement to change jsonb value

UPDATE test 
SET data = replace(data::TEXT,': "my-name"',': "my-other-name"')::jsonb 
WHERE id = 1;

Ultimately, the accepted answer is correct in that you cannot modify an individual piece of a jsonb object (in 9.4.5 or earlier); however, you can cast the jsonb column to a string (::TEXT) and then manipulate the string and cast back to the jsonb form (::jsonb).

There are two important caveats

  1. this will replace all values equaling "my-name" in the json (in the case you have multiple objects with the same value)
  2. this is not as efficient as jsonb_set would be if you are using 9.5

Solution 5

update the 'name' attribute:

UPDATE test SET data=data||'{"name":"my-other-name"}' WHERE id = 1;

and if you wanted to remove for example the 'name' and 'tags' attributes:

UPDATE test SET data=data-'{"name","tags"}'::text[] WHERE id = 1;
Share:
243,501

Related videos on Youtube

jvous
Author by

jvous

Updated on October 24, 2021

Comments

  • jvous
    jvous over 2 years

    Looking through the documentation for the Postgres 9.4 datatype JSONB, it is not immediately obvious to me how to do updates on JSONB columns.

    Documentation for JSONB types and functions:

    http://www.postgresql.org/docs/9.4/static/functions-json.html http://www.postgresql.org/docs/9.4/static/datatype-json.html

    As an examples, I have this basic table structure:

    CREATE TABLE test(id serial, data jsonb);
    

    Inserting is easy, as in:

    INSERT INTO test(data) values ('{"name": "my-name", "tags": ["tag1", "tag2"]}');
    

    Now, how would I update the 'data' column? This is invalid syntax:

    UPDATE test SET data->'name' = 'my-other-name' WHERE id = 1;
    

    Is this documented somewhere obvious that I missed? Thanks.

  • fiatjaf
    fiatjaf almost 9 years
    This answer only concerns with the type JSON and ignores JSONB.
  • Erwin Brandstetter
    Erwin Brandstetter almost 9 years
    @fiatjaf: This answer is fully applicable to the data types json and jsonb alike. Both store JSON data, jsonb does it in a normalized binary form that has some advantages (and few disadvantages). stackoverflow.com/a/10560761/939860 Neither data type is good for being manipulating a lot inside the database. No document type is. Well, it's fine for small, hardly structured JSON documents. But big, nested documents would be a folly that way.
  • kometen
    kometen over 8 years
    Worked for me too, on postgresql 9.4.5. The whole record is rewritten so one can't update a single field atm.
  • Peter Krauss
    Peter Krauss about 8 years
    Another issue in this line, is the use of jsonb_build_object(), because x->key, not returns key-object pair, to populate you need jsonb_set(target, path, jsonb_build_object('key',x->key)).
  • Michael Wasser
    Michael Wasser almost 8 years
    "Instructions how to work with the tools of Postgres 9.3" really aught to be first in your answer as it answers the question asked.. sometimes it makes sense to update json for maintenance / schema changes etc and the reasons not to do update json don't really apply
  • chadrik
    chadrik almost 8 years
    you get bonus points for showing how to update a column in a table as the OP requested
  • chadrik
    chadrik almost 8 years
    I know this was not in the original request, but I'd love to see an example of how to chain these, e.g. remove two keys and set two keys in one UPDATE.
  • Database
    Database almost 8 years
    @chadrik: I added a more complex example. It doesn't do exactly what you requested, but it should give you an idea. Note that the input to the outer jsonb_set call is the output from the inner call, and that the input to that inner call is the result of data #- '{tags,-1}'. I.e., the original data with the last tag removed.
  • Pranay Soni
    Pranay Soni almost 8 years
    i dont kow tag index. how to delete the tag by value ?
  • Database
    Database over 7 years
    @PranaySoni: For that purpose, I'd probably use a stored procedure or, if the overhead isn't a concern, bring that data back, manipulate it in the application's language, then write it back. This sounds heavy, but keep in mind, in all the examples I gave, you're not still not updating a single field in the JSON(B): you're overwriting the whole column either way. So a stored proc is really no different.
  • Joshua Robinson
    Joshua Robinson over 7 years
    Good lord, I have been searching for how to do an update to jsonb for like two hours so I could replace all \u0000 null characters, example showed the complete picture. Thanks for this!
  • Alex
    Alex over 7 years
    @Jimothy is "'{tags,999999999}'" an array append hack? How does it work?
  • Database
    Database over 7 years
    @Alex: Yes, a bit of a hack. If I said {tags,0}, that would mean "the first element of array tags", allowing me to give a new value to that element. By using a large number instead of 0, instead of it replacing an existing element in the array, it adds a new element to the array. However, if the array actually had more than 999,999,999 elements in it, this would replace the last element instead of add a new one.
  • davidicus
    davidicus over 7 years
    looks good! btw the second argument to replace in your example includes the colon and the third does not. Looks like your call should be replace(data::TEXT, '"name":', '"my-other-name":')::jsonb
  • Arthur
    Arthur about 6 years
    I don't think it is correct that PostgreSQL always rewrites the whole row when an update is performed. This is only the case for rows that fit within a single physical page (normally 8kb). Rows that are larger are split internally and as far as I understand only the page(s) affected by the update will be rewritten. postgresql.org/docs/9.6/static/storage.html
  • stackdave
    stackdave almost 6 years
    what about if field contains null? looks dont' work. Eg info jsonb field is null: "UPDATE organizer SET info = jsonb_set(info, '{country}', '"FRA"') where info->>'country'::text IS NULL; " I get UPDATE 105 record but no changes on db
  • Greg
    Greg almost 6 years
    This doesn't work in 9.4, because jsonb_build_object was introduced in 9.5
  • Chad Capra
    Chad Capra almost 6 years
    Thank you @davidicus! Sorry for the very delayed update, but I appreciate you sharing for others!
  • kevlarr
    kevlarr over 5 years
    Not really sure if it's related to the downvotes (I didn't downvote), but the issue might be more because it's a "you shouldn't be doing that" answer rather than a "people don't seem to like the idea: a normalized..." problem. I think you're right, storing a lot in JSON/B probably indicates a larger problem and is far from the ideal, but a lot of us have to work with pre-existing systems and need to learn how to work with what we have until we're able to improve those systems.
  • J. Raczkiewicz
    J. Raczkiewicz almost 5 years
    @Greg You are right, I just checked and I'm running PostgreSQL 9.5 now - this is why it works. Thanks for pointing that out - my solution will not work in 9.4.
  • Ppp
    Ppp almost 5 years
    Answer the question first before adding your own comment/opinion/discussion.
  • taleodor
    taleodor over 4 years
    This answer is largely irrelevant with current generation of postgresql (versions 11, 12). There is much better support of Jsonb operations and there are many reasons to switch to that as being mainstream type for many applications.
  • Erwin Brandstetter
    Erwin Brandstetter over 4 years
    @taleodor: JSON support has been improved with every version and is pretty excellent by now. Has been for some time. And very useful for certain applications. But my answer is still fully applicable - especially for "update operations" this question asks about - as it addresses a principle limitation of document types. For regular data, proper columns in a more or less normalized db schema are typically much more efficient. That is not going to change. The Postgres project advises accordingly, like I quoted above - unaltered up to Postgres 13 devel manual.
  • Jeremiah Adams
    Jeremiah Adams over 4 years
    In addition to @MichaelWasser comment - the concept of normalization is irrelevant to the OP's question. Normalization is entirely out of scope and brings its own pros, cons and justifications.
  • Brain90
    Brain90 about 4 years
    People should know first why doing things that way. Love the philosophy.
  • Steve
    Steve over 3 years
    @J.Raczkiewicz Function works great! How do I enhance your function to add an insert if value doesn't exist? This is needed in case of null column value (e.g. empty column that doesn't yet have a {}) Similar to the create if missing boolean in jsonb_set function. jsonb_set ( target jsonb, path text[], new_value jsonb [, create_if_missing boolean ] ) postgresql.org/docs/13/functions-json.html.
  • McKayla
    McKayla almost 3 years
    If you go this route just be very careful to sanitize your user input so that they can't pollute your data.
  • jiminikiz
    jiminikiz about 2 years
    Be very careful when reading this answer. For each blog post listed in this answer, there are equal and opposite views on this.
  • Adrián Rodriguez
    Adrián Rodriguez about 2 years
    What if the jsonb contains a sub json? Something like "nested_tag":{"subtag1":true, "subtag2":"value"}. i'm trying to modify/add a subtag. I'm trying Set data = jsonb_set(data, '{nested_tag}->{subtag3}', true). But I get ERROR: function jsonb_set(jsonb, unknown, boolean) does not exist. Any idea?