For the JSONB fields you can use the concatenation operation ||
.Here is a sample that I made to make an upsert with merging of field:
INSERT INTO table_name (id, title, po_document)VALUES ($1, $2, $3::JSONB)ON CONFLICT (id) DO UPDATE SET title = COALESCE(NULLIF($2, ''), table_name.title), props = table_name.props || $3::JSONB
Here I have a table with a document id, title and it's properties in JSONB.When creating a document I can pass all the 3 values.To update the only title I can pass only it in a second title
parameter and set the third props
parameter to an empty object {}
.To update the properties I can leave the second parameter title
empty (then the current value will remain) and set the third param with an object that contains only fields that should be updated.To remove a field value you can explicitly set it to null