Quick revision
The short answer
When an UPDATE leaves a TOASTed column untouched, PostgreSQL reuses its TOAST pointer and copies nothing, but if the statement assigns the 500 KB jsonb, even with the same content, PostgreSQL compresses it again and writes a full new set of TOAST chunks and their WAL. Hibernate does exactly that by default, because its UPDATE statements set every column, so a status change rewrites the whole document. The fix is @DynamicUpdate, or a separate table for the large document, plus lz4 compression to save CPU and monitoring of the TOAST table's bloat.
What to remember
In this answer, we saw that an UPDATE writes a new row version, and that PostgreSQL reuses a TOASTed value's pointer when the column is not assigned but rewrites all its chunks when it is, without comparing old and new values. Hibernate's default UPDATE sets every column, so a status change was rewriting the 500 KB jsonb document and its WAL. We fixed it with @DynamicUpdate or by moving the document to its own table, made remaining rewrites cheaper with lz4, and monitored dead tuples of the TOAST relation in pg_stat_all_tables.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
When an UPDATE leaves a TOASTed column untouched, PostgreSQL reuses its TOAST pointer and copies nothing, but if the statement assigns the 500 KB jsonb, even with the same content, PostgreSQL compresses it again and writes a full new set of TOAST chunks and their WAL. Hibernate does exactly that by default, because its UPDATE statements set every column, so a status change rewrites the whole document. The fix is @DynamicUpdate, or a separate table for the large document, plus lz4 compression to save CPU and monitoring of the TOAST table's bloat.
Why this question is asked
This is a common performance surprise in Spring and Hibernate services that store large JSON documents. The code changes one small field, the SQL looks harmless, and yet each update takes tens of milliseconds and the WAL volume, which drives replication traffic and backup size, is far higher than expected. Interviewers ask it because finding the cause means connecting PostgreSQL's MVCC and TOAST behaviour with what the ORM sends. In this answer, we will recap how an UPDATE writes a new row version, see when TOAST data is reused and when it is rewritten, find the Hibernate behaviour that triggers the rewrite, and then fix it.
1A Quick Recap: An UPDATE Writes a New Row Version
PostgreSQL never changes a row in place. An UPDATE writes a new version of the row, called a tuple, and marks the old one as dead, to be removed later by vacuum. This is how MVCC lets other transactions keep reading the old version.
For a row with a 500 KB jsonb document, the document is not in the row itself. TOAST compressed it and stored it as chunks of about 2 KB in the table's TOAST relation, and the row holds an 18-byte pointer to those chunks. So the question is what happens to the chunks when the row gets a new version.
2When TOAST Data Is Reused and When It Is Rewritten
If the UPDATE does not assign the jsonb column, the new row version simply copies the old 18-byte pointer. Both versions point to the same chunks, and nothing in the TOAST table is written. The update costs about as much as updating a small row.
If the UPDATE does assign the column, PostgreSQL treats the value as new data. It does not compare it with the old document to see whether they are equal. It compresses the 500 KB again, writes perhaps 100 new chunks into the TOAST table, writes WAL for all of them, and the old chunks become dead rows that vacuum must clean up later. And because jsonb is stored as one value, even changing one key inside it rewrites the whole document; there is no partial update of a TOASTed value.
| UPDATE statement | TOAST chunks written | WAL written |
|---|---|---|
| SET status = ? only | none, pointer reused | a few hundred bytes |
| SET status = ?, document = ? | about 100 new chunks | hundreds of KB, more with full-page writes |
| jsonb_set on one key | the whole document again | hundreds of KB |
3Finding What Hibernate Sends
So the next question is what the application really sends. By default, Hibernate builds one UPDATE statement per entity class that sets every mapped column, and binds the current values of all of them. Changing order.setStatus(...) produces an UPDATE that also sets document to the full document, which, as we just saw, rewrites all the TOAST chunks. The document also travels from the service to the database on every update, 500 KB over the network for a status change.
We can confirm it with the SQL log, spring.jpa.show-sql or the org.hibernate.SQL logger, and on the database side by comparing WAL generated per update, for example with pg_stat_statements' wal_bytes column. A second trap is dirty checking: if the JSON mapping makes Hibernate think the document changed even when it did not, the full rewrite happens even with dynamic updates, so we check that the document column does not appear in the log when only status changes.
4Fix 1: Send Only Changed Columns With @DynamicUpdate
@DynamicUpdate tells Hibernate to build the UPDATE statement at flush time with only the columns that actually changed.
The trade-off is small: Hibernate can no longer reuse one pre-built UPDATE string for the entity and builds statements per combination of changed columns. For an entity with a large document, that cost is trivial compared with rewriting 500 KB.
5Fix 2: Move the Document to Its Own Table
If the document is rarely changed but the order row is updated often, a cleaner design is to put the document in its own table, order_documents, with order_id as the key. Status updates then touch only the small orders row, and the document is loaded only when a page actually needs it, for example through a lazy one-to-one association or a separate query. This also keeps the orders table and its row versions small, which helps vacuum and the buffer cache. It is a schema change, so it fits when the access pattern clearly separates the two.
- Does the SQL log show document in status updates?YesFind why it is sentNoLook elsewhere, for example triggers
- Is it sent only because all columns are set?YesAdd @DynamicUpdateNoFix dirty checking on the JSON mapping
- Is the row updated far more often than the document?YesMove the document to its own tableNoKeep one table
- Set lz4 compression and watch TOAST bloatpg_stat_all_tables for the pg_toast relation
4 steps in order. Play them, or step through them one at a time.
6Compression and Monitoring
When the document really does change, we can make the rewrite cheaper. lz4 compresses and decompresses much faster than the default pglz, so switching the column saves CPU on every write and read. It only applies to values written afterwards; existing documents stay pglz until they are rewritten.
Every rewrite leaves dead chunks, so we watch the TOAST table's dead tuples and size. Notice that we look it up in pg_stat_all_tables: the TOAST relation lives in the pg_toast schema, and pg_stat_user_tables leaves that schema out. Many dead chunks mean autovacuum is behind, and lowering the table's autovacuum_vacuum_scale_factor, which also applies to its TOAST table, helps it keep up.
7Follow-up questions
Does changing one key with jsonb_set avoid the rewrite?
No. jsonb is one value, so jsonb_set produces a new complete document, which TOAST compresses and stores as a full new set of chunks. If parts of the document change often, those parts belong in ordinary columns or a separate table.
Why is the WAL volume even larger than the document size?
Because the first change to each page after a checkpoint writes the whole 8 KB page into the WAL, a full-page write, to protect against torn pages. Writing about 100 new TOAST chunks touches many pages, so the WAL can be well above the compressed document size, and it is copied to every replica and into backups.
Would HOT updates help here?
A HOT update avoids new index entries when no indexed column changes and the page has free space, which helps the main table. It does not prevent the TOAST rewrite when the document is assigned, so @DynamicUpdate is still the fix.
8Conclusion
In this answer, we saw that an UPDATE writes a new row version, and that PostgreSQL reuses a TOASTed value's pointer when the column is not assigned but rewrites all its chunks when it is, without comparing old and new values. Hibernate's default UPDATE sets every column, so a status change was rewriting the 500 KB jsonb document and its WAL. We fixed it with @DynamicUpdate or by moving the document to its own table, made remaining rewrites cheaper with lz4, and monitored dead tuples of the TOAST relation in pg_stat_all_tables.
Open with
I would first look at what the UPDATE actually assigns. The jsonb document is TOASTed: compressed and stored as chunks in the TOAST table, with a pointer in the row.
Then, if they want more
An UPDATE writes a new row version, and if it does not assign the document, the new version just reuses the pointer, so it is cheap.
If it does assign it, PostgreSQL does not compare it with the old value: it compresses the 500 KB again and writes a whole new set of chunks plus their WAL, and the old chunks become dead. Even a jsonb_set on one key rewrites the whole document.
Hibernate by default sends an UPDATE that sets every column, so changing the status also sends and rewrites the document. I would confirm it in the Hibernate SQL log and with wal_bytes in pg_stat_statements.
The fix is @DynamicUpdate, so only status is in the SET clause, and I would check that dirty checking on the JSON mapping does not mark the document as changed. If the order is updated much more often than the document, I would move the document into its own table and load it only when needed.
For updates that really change the document, I would switch the column to lz4 compression, which is much cheaper on CPU, and I would monitor the TOAST relation's dead tuples in pg_stat_all_tables, since the toast schema is not in pg_stat_user_tables.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "An API reads orders with a 500 KB jsonb document each. Updating only the status column...": https://scrivio-interview-library.netlify.app/databases/postgresql/6ac981fdd6/