The type is jsonbdesigned to store entire documents. If you are changing any part of the document, you need to assign a new value to the column. Since Postgres has been keeping the old version for a while, this is an expensive operation.
With that in mind, here is an example of how not to update the columns jsonb:
create table t1 (doc jsonb);
insert into t1 values
('{"param1": 10, "param2": 15}'),
('{"param1": 10, "param2": 5}');
update t1
set doc = ('{"param1": ' ||
((doc->'param1')::text::int + 10)::text ||
', "param2": ' ||
(doc->'param2')::text ||
'}')::jsonb
where (doc->'param2')::text::int > 12;
select * from t1;
Fingerprints:
doc
------------------------------
{"param1": 10, "param2": 5}
{"param1": 20, "param2": 15}
(2 rows)
source
share