PostgreSQL Performance – JSONB_Set Performance in PostgreSQL

postgresqlpostgresql-performance

I searched the internet and PG documentation for hours and couldn't find any benchmark for it.

In MySQL 8.0 they specifically state that now the optimizer is smart and can do in-place modification of JSON column without updating the whole column. But I couldn't find any similar article about PG. jsonb is around for a long time already and it would be really expected that such common thing as updating a single field of large JSON would be optimized, but I couldn't find any notion about it. Moreover I found here an answer from 3 year ago (PostgreSQL update JSONB without jsonb_set) that proves that updating whole column as text is faster than using jsonb_set.

I'm trying to model a table with 2M rows, each will have 2 JSON fields with 3600 simple numeric fields (date: value). I will need to add 1 field to each JSON daily, meaning 4M json_sets.
Is it feasible, or I need to switch to some NoSQL like MongoDB for this feature?

Thanks

Best Answer

Every UPDATE in PostgreSQL creates a new version of the row. There is no in-place update. So not only would a new JSON be created, but also all other columns in the table would be copied.

Updating part of a JSON is not common in relational databases, or at least it shouldn't be. If you feel the need to do so, you have chosen the wrong data model, and you would be much better off using table columns instead of JSON attributes. If you then split the data across several tables using a process called “normalization”, an UPDATE doesn't hurt quite as much.

Related Question