Practical technical guides / Databases

Reading One Key From a Large jsonb Value Detoasts All of It: TOAST, Compression and Hot Fields

Measured on PostgreSQL 18.4: one jsonb key read touched 13,347 buffers against 15 for a plain column, and a one-key update doubled the TOAST table.

By Sandeep Belgavi · Published 2026-11-30

Measured on PostgreSQL 18.4: one jsonb key read touched 13,347 buffers against 15 for a plain column, and a one-key update doubled the TOAST table.

Where a large jsonb value actually lives

PostgreSQL stores rows in 8 kB pages and does not let a row span pages. A value too large to fit is moved out of line by TOAST, The Oversized-Attribute Storage Technique. The PostgreSQL 18 documentation describes the mechanism: when a row would exceed about 2 kB (TOAST_TUPLE_THRESHOLD), wide values are first compressed and, if still too large, split into chunks of about 2 kB stored in the table's separate TOAST relation. The main row keeps only an 18-byte pointer.

That design is why a table of large jsonb documents can be fast to scan for its other columns. It is also why SELECT body->>'status' is not a cheap lookup. To evaluate the operator, PostgreSQL must fetch every chunk of the value from the TOAST relation, reassemble it, decompress it if it was compressed, and only then find the key. A one-key read costs the whole value.

This guide measures that cost on PostgreSQL 18.4 and covers compression choice, what an update of one key writes, and when to move hot fields into ordinary columns. The companion example postgres_toast_jsonb contains the script and the recorded results.

The measurement

The script loads 2,000 synthetic Belgavi's Book Store order documents, each with 150 order lines and a status field. Each document is about 25,400 bytes as JSON text. The same documents go into two tables, one with COMPRESSION pglz (the PostgreSQL 18 default) and one with COMPRESSION lz4, and each table also has a plain status text column. It ran on PostgreSQL 18.4 for Windows, on a local disposable cluster with 256 MB of shared buffers, after warming the cache. Timings are medians of seven runs measured from a Python client, so they include result transfer.

Measurementpglz tablelz4 table
Heap size122,880 bytes122,880 bytes
TOAST relation size60,080,128 bytes43,696,128 bytes
Average stored value28,285 bytes20,469 bytes
pg_column_compression(body)null (not compressed)lz4
Buffers for body->>'status'13,34711,348
Buffers for the status column1515
Median time, body->>'status'about 63 msabout 82 ms
Median time, status columnabout 2.2 msabout 2.2 ms

The heap is 15 pages for 2,000 rows, because the rows hold only pointers. Reading the plain column touches those 15 buffers. Reading one key from the jsonb value touches 13,347 buffers on the pglz table, almost 900 times as many, and takes roughly 28 times as long even with every page in memory. On a cold cache, those buffers become reads.

Why pglz stored the values uncompressed

The pglz table's stored values are larger than the JSON text, and pg_column_compression returns null for them, which means they were stored without compression. The jsonb binary format is somewhat larger than the text, and pglz declined to compress it. PostgreSQL's default pglz strategy, in src/common/pg_lzcompress.c, requires at least a 25 percent size reduction and gives up if it finds nothing to compress within the first kilobyte. These synthetic documents contain random-looking note text, and pglz did not meet its bar. lz4, which keeps any result smaller than the input, saved about 28 percent and shrank the TOAST relation by 27 percent.

Smaller is not automatically faster. The lz4 table read 15 percent fewer buffers for the one-key query but took longer in this run, because every value now has to be decompressed before the key is found, while the pglz values were simply copied. Compression trades I/O for CPU. With a cold cache or storage-bound workload, fewer pages usually wins; with everything in memory, decompression can dominate. Measure with your own documents.

PostgreSQL 19, released on 19 November 2026, changes the default of default_toast_compression from pglz to lz4, according to its release notes. After an upgrade, newly written values in columns without an explicit compression setting will use lz4. Existing values keep their compression until they are rewritten.

Updating one key rewrites the whole value

UPDATE orders_pglz SET body = jsonb_set(body, '{status}', '"shipped"');

jsonb values are immutable. jsonb_set produces a new value, and storing it writes a complete new TOASTed copy; the old chunks remain until VACUUM removes them. In the measurement, this update grew the TOAST relation from 60,080,128 to 120,152,064 bytes on the pglz table, and from 43,696,128 to 87,384,064 bytes on the lz4 table. It doubled.

Updating the plain column instead left the TOAST relation exactly where it was. When an UPDATE does not change a TOASTed column, the new row version reuses the existing TOAST pointer, so no chunks are copied. For a status that changes several times in an order's life, the difference is a full document write per change versus a few bytes.

Moving hot fields out of the document

The pattern is simple: keep the document for what is read rarely and as a whole, and put fields that are filtered on, sorted on or updated often into columns.

ALTER TABLE orders ADD COLUMN status text;
UPDATE orders SET status = body->>'status' WHERE status IS NULL;  -- in batches on a large table
ALTER TABLE orders ALTER COLUMN status SET NOT NULL;
-- Application change: write status to the column; stop writing it into body.

A stored generated column, status text GENERATED ALWAYS AS (body->>'status') STORED, avoids the dual write, but it is computed from the document, so changing the status still rewrites the TOASTed document. That makes it a fit for fields that are read often but rarely changed. For fields that change, the column has to be the source of truth.

Other levers exist. ALTER TABLE ... ALTER COLUMN body SET STORAGE EXTERNAL disables compression for that column, which makes substring access on text and bytea faster but does nothing for jsonb operators, which need the whole value anyway. Splitting a large document into a child table, one row per order line, turns one huge value into many small ones and lets queries read only the lines they need.

Failure modes

  • **SELECT * in hot paths.** Every row read detoasts every large column it returns. Name the columns.
  • Indexes on expressions over large documents. A B-tree on (body->>'status') makes lookups cheap, but every insert, and every update that writes a new document, must detoast the value to compute the key. Prefer a real column for hot keys.
  • Bloat after bulk key updates. A migration that rewrites one key in every document doubles the TOAST relation until VACUUM catches up, and can fill a disk. Batch it and let autovacuum run between batches.
  • Trusting pg_column_size for compression. It reports the stored size, which can exceed the text size when compression was skipped. Use pg_column_compression to see whether a value is compressed.
  • WAL volume from small edits. Each one-key update writes the whole new TOASTed value, and those chunks go through the write-ahead log like any other data, so replication traffic and backup size grow with document size, not with the size of the change.
  • Assuming a compression change rewrites data. ALTER COLUMN ... SET COMPRESSION lz4 affects only values written afterwards. Existing values change only when rewritten, for example by VACUUM FULL or by updating them.

Verification

Run the companion script against a disposable database and check the result with check_result.py. It asserts that the documents live in the TOAST relation, that the one-key read touches over 100 times the buffers of the plain column, that a one-key update at least doubles the TOAST relation, that a plain-column update leaves it unchanged, and that lz4 compressed the values. For your own tables, compare pg_relation_size(reltoastrelid) with the heap size, run EXPLAIN (ANALYZE, BUFFERS) on your common queries, and check pg_column_compression on a sample of rows.

Primary references and authorship

Sources checked 2026-11-30. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/sql/postgres_toast_jsonb. Views are my own and do not represent any employer, past or present.