The idea · Lab 3.4.2TOAST — The Oversized-Attribute Storage Technique — is how PostgreSQL handles a value too large to fit comfortably in a normal 8kB heap page: it compresses the value, and if it is still too large, moves it out to a separate, per-table TOAST relation, leaving only a small pointer behind in the original row. Every text, jsonb, bytea, and similarly variable-length column defaults to EXTENDED storage — compress first, externalize if still necessary — visible directly in pg_attribute.attstorage.
A specific, testable claim is worth checking before teaching it as fact: does a query that never selects a TOASTed column still pay hidden I/O reading that column's TOAST data? Checked directly against a real table with genuinely large, out-of-line values, using pg_statio_all_tables' TOAST relation counters before and after each query — the answer is no. PostgreSQL detoasts lazily: a value's out-of-line data is only fetched when the column is actually referenced in the query's output or evaluated in a condition. The real cost lives somewhere more specific and more useful to know: actually selecting the large column, whether on purpose or by an unnecessary 'SELECT *'.
Checking Storage Strategy Directly
SELECT attname, attstorage FROM pg_attribute WHERE attrelid = 'sql_toast_demo'::regclass AND attnum > 0;
attname | attstorage
---------+------------
id | p
name | x
payload | x
p is PLAIN (never compressed or externalized — used for fixed-size types like int). x is EXTENDED, the default for text and jsonb: compress first, and only move out-of-line if the compressed value is still too large.
A Genuinely TOASTed Table
CREATE TABLE sql_toast_demo (id int PRIMARY KEY, name text, payload text);
INSERT INTO sql_toast_demo SELECT g, 'Item'||g,
(SELECT string_agg(md5(random()::text || gs::text), '') FROM generate_series(1,100) gs)
FROM generate_series(1,2000) g;
main_size | toast_size
-----------+-------------
120 kB | 8152 kB
Genuinely incompressible data (random MD5 hashes concatenated) forces real out-of-line storage — the main table stays tiny at 120 kB while 8+ MB of actual payload content lives in the separate pg_toast relation.
The Claim, Checked Directly
SELECT * FROM pg_statio_all_tables WHERE relid = (SELECT reltoastrelid FROM pg_class WHERE oid = 'sql_toast_demo'::regclass);
-- heap_blks_hit: 5996
SELECT id, name FROM sql_toast_demo; -- payload NOT selected
SELECT * FROM pg_statio_all_tables WHERE relid = (...);
-- heap_blks_hit: 5996 <- unchanged
Zero additional TOAST I/O from a query that never touches payload at all — the claim that an unselected TOASTed column silently costs I/O does not hold up under direct measurement.
SELECT id, name, payload FROM sql_toast_demo; -- payload IS selected
SELECT * FROM pg_statio_all_tables WHERE relid = (...);
-- heap_blks_hit: 7996 <- +2000, genuinely real
The moment the query actually selects payload, TOAST I/O jumps by exactly 2,000 heap block hits (one per row) plus 4,001 index block hits. PostgreSQL detoasts lazily — the cost is tied to actually accessing the value, not merely having it in the table.
Key concepts
Lazy detoasting
PostgreSQL only reconstructs an out-of-line TOASTed value when it is actually needed — referenced in the SELECT list, used in a WHERE condition, or otherwise evaluated. A query that reads a row but never touches a specific TOASTed column's value pays no TOAST-table I/O for it at all; the small in-row pointer is read as part of the normal heap page, but never followed.
EXTENDED vs EXTERNAL storage
EXTENDED (the default for variable-length types) compresses a value first and only moves it out-of-line if still too large; EXTERNAL skips compression entirely and always stores out-of-line once a value exceeds the inline threshold. EXTERNAL trades away compression's space savings for slightly cheaper access (no decompression CPU cost) — a real but often modest difference, and only relevant at all for values that actually get selected.
What to remember
- pg_attribute.attstorage shows each column's real storage strategy directly — p (PLAIN), x (EXTENDED, the default for text/jsonb), or m/e for MAIN/EXTERNAL
- A genuinely TOASTed table shows a dramatic size split between a small main table and a much larger separate TOAST relation, checkable directly with pg_relation_size and pg_total_relation_size
- A specific, testable claim about hidden I/O on unselected TOASTed columns did not survive direct measurement via pg_statio_all_tables — PostgreSQL detoasts lazily, only when a value is actually accessed
- The real, measurable cost is tied to a query that actually selects a large TOASTed column — including an unnecessary SELECT * — not to the column's mere existence in an unselected row
- EXTERNAL storage skips compression entirely, trading potential space savings for lower CPU cost on access — a real win specifically for data that does not compress well in the first place