So I wanted to try jsonb
of PostgreSQL. In my table, I have a column called extras
of jsonb
type.
Sample data in extras
looks like {"param1": 10, "param2": 15}
I would like to modify the JSON using sql statements only. I want to do something like this:
Update param1
of extras
field by adding 10 to its value if param2
of extras
exceeds 12.
How can I write a SQL statement like this? I know I can easily do this in the application layer but I would like to do this in the SQL layer itself as the number of rows I would be potentially dealing with would be huge and I do not want to waste time in db-application-db roundtrip
This should do it with PostgreSQL 9.5:
The
jsonb
type is meant to store whole documents. If you change any part of the document, you'll need to assign a new value to the column. Because Postgres keeps the old version around for a while that is an expensive operation.With that in mind, here's an example of how not to update
jsonb
columns:This prints: