TOAST
a 1 MB value in an 8 KB page: compressed first, then sliced into a side table.
- address
- everything.storage.toast
- part
- part 12, pages and the buffer pool
- chapter
- chapter 5 of 12
- reading time
- 9 min
a row has to fit in an 8 KB page, and postgres would like four of them to fit. so when a row grows past about 2 KB, its big values get compressed first. if that's still not enough, they're sliced into chunks of just under 2 KB and stored in a hidden side table, the TOAST table, and the row keeps an 18-byte pointer.
that's how a text column holds a megabyte. TOAST is "the oversized-attribute storage technique", and yes, someone was pleased with that.
the picture
you know how a text or jsonb value can be up to 1 GB, and a page is 8 KB? something has to give.
think of a filing cabinet with thin folders, four to a drawer. a normal letter goes straight in the folder. a thick report gets vacuum-packed first (compressed) and usually fits. if it's still too thick, it goes to the archive room in numbered envelopes, and the folder keeps a slip that says "archive item 24,584, six envelopes". people who only flip through the folders never touch the archive.
too wide for comfort, so it was compressed with pglz: 2,100 bytes became 35, small enough to stay in the row.
three regimes. under about 2 KB, nothing happens. above it, compression (and a megabyte of a becomes 11 KB). if the compressed value is still over the limit, or doesn't compress, out of line it goes, and the row shrinks to about 50 bytes however big the value was.
watch it happen
five values in a docs table, from small to a megabyte, some compressible and some not:
CREATE TABLE docs (id int PRIMARY KEY, title text, body text);
INSERT INTO docs VALUES (1, 'one', repeat('a', 1000)),
(2, 'two', repeat('a', 3000)),
(3, 'three', repeat('a', 1000000));
INSERT INTO docs SELECT 4, 'four', string_agg(md5(g::text), '' ORDER BY g)
FROM generate_series(1, 100) AS g;
INSERT INTO docs SELECT 5, 'five', string_agg(md5(g::text), '' ORDER BY g)
FROM generate_series(1, 31250) AS g;
SELECT d.id, octet_length(d.body) AS bytes, pg_column_size(d.body) AS stored,
pg_column_compression(d.body) AS method, h.lp_len AS row_bytes
FROM docs d JOIN heap_page_items(get_raw_page('docs', 0)) h ON h.t_ctid = d.ctid
ORDER BY d.id;| idint4 | bytesint4 | storedint4 | methodtext | row_bytesint2 |
|---|---|---|---|---|
| 1 | 1000 | 1004 | null | 1036 |
| 2 | 3000 | 44 | pglz | 76 |
| 3 | 1000000 | 11452 | pglz | 52 |
| 4 | 3200 | 3200 | null | 51 |
| 5 | 1000000 | 1000000 | null | 51 |
read it row by row. 1,000 as stay as they are: a 1,036-byte row, no compression. 3,000 as are over the line, so pglz squeezes them to 44 bytes, and the row is 76 bytes again. a million as compress to 11,452 bytes, which is still too big for the row, so that goes out of line. 3,200 bytes of md5 hex won't compress (pglz finds no repeats worth encoding in random hex), so it goes out of line uncompressed, and the same for the megabyte of it. every out-of-line row is about the same small size, 51 or 52 bytes: the header, the int, the title, and an 18-byte toast pointer.
the side table
every table that has a column that could need it gets a toast table when it's created. it has a name, an oid, its own file, and its own index:
SELECT reltoastrelid::regclass AS toast_table,
pg_relation_size('docs') AS heap_bytes,
pg_relation_size(reltoastrelid) AS toast_bytes
FROM pg_class WHERE relname = 'docs';| toast_tableregclass | heap_bytesint8 | toast_bytesint8 |
|---|---|---|
| pg_toast.pg_toast_24576 | 8192 | 1048576 |
one 8 KB page for the five rows, and over a megabyte next door. the toast table is an ordinary heap table with three columns: chunk_id (one per toasted value), chunk_seq (0, 1, 2, ...) and chunk_data. you can read it like any table:
SELECT chunk_id, count(*) AS chunks, sum(length(chunk_data)) AS bytes,
max(length(chunk_data)) AS biggest_chunk
FROM pg_toast.pg_toast_24576
GROUP BY chunk_id ORDER BY chunk_id;| chunk_idoid | chunksint8 | bytesint8 | biggest_chunkint4 |
|---|---|---|---|
| 24584 | 6 | 11452 | 1996 |
| 24585 | 2 | 3200 | 1996 |
| 24586 | 502 | 1000000 | 1996 |
- ├── id 5, title 'five'
- └── the value lives elsewhere
- └──
- ├── chunk_seq 0: 1,996 bytes
- ├── chunk_seq 1: 1,996 bytes
- ├── ... 499 more of 1,996 bytes
- └── chunk_seq 501: the last 4 bytes501 × 1,996 = 999,996
three values went out of line: 6 chunks for the compressed megabyte of a, 2 for the 3,200 bytes of hex, and 502 for the million bytes of hex. no chunk is over 1,996 bytes: that size is picked so four chunk rows fit on a toast page. (the 24576 in the name is the oid of docs. it's what the browser engine hands out first after loading a dataset; on another server it will be some other number, so look it up with reltoastrelid like above.)
updates don't copy the big value
a toast pointer is just a value in the row. if an UPDATE doesn't change the toasted column, the new row version gets the same pointer, and the chunks aren't touched.
UPDATE docs SET title = 'five, renamed' WHERE id = 5;SELECT ctid, pg_column_toast_chunk_id(body) AS chunk_id,
pg_relation_size('pg_toast.pg_toast_24576') AS toast_bytes
FROM docs WHERE id = 5;
UPDATE docs SET title = 'five, renamed' WHERE id = 5;
SELECT ctid, pg_column_toast_chunk_id(body) AS chunk_id,
pg_relation_size('pg_toast.pg_toast_24576') AS toast_bytes
FROM docs WHERE id = 5;| ctidtid | chunk_idoid | toast_bytesint8 |
|---|---|---|
| (0,5) | 24586 | 1048576 |
| ctidtid | chunk_idoid | toast_bytesint8 |
|---|---|---|
| (0,6) | 24586 | 1048576 |
new ctid, same chunk_id, same toast table size. pg 17+ pg_column_toast_chunk_id() is new in postgres 17; before that you had to dig the pointer out of the raw tuple.
four strategies per column
each column has a storage strategy, attstorage, which decides what TOAST may do with it:
SELECT attname, atttypid::regtype AS type, attstorage
FROM pg_attribute
WHERE attrelid = 'docs'::regclass AND attnum > 0
ORDER BY attnum;| attnamename | typeregtype | attstoragechar |
|---|---|---|
| id | integer | p |
| title | text | x |
| body | text | x |
- p, plain
- never compressed, never moved out. fixed-width types like
inthave no choice. - x, extended
- compress, then move out if still needed. the default for
text,jsonb,bytea. - e, external
- move out, but never compress.
- m, main
- compress, and move out only as a last resort.
EXTERNAL sounds pointless until you need part of a big value. substr() on an uncompressed out-of-line value only fetches the chunks it covers, while a compressed one has to be decompressed from the start:
ALTER TABLE docs ALTER COLUMN body SET STORAGE EXTERNAL;
INSERT INTO docs VALUES (6, 'six', repeat('a', 1000000));
SELECT id, pg_column_size(body) AS stored, pg_column_compression(body) AS method,
pg_column_toast_chunk_id(body) IS NOT NULL AS out_of_line
FROM docs WHERE id IN (3, 6) ORDER BY id;| idint4 | storedint4 | methodtext | out_of_linebool |
|---|---|---|---|
| 3 | 11452 | pglz | t |
| 6 | 1000000 | null | t |
the same million as: 11,452 bytes compressed in row 3, the full 1,000,000 uncompressed in row 6. the setting only affects values written after it.
pglz, and lz4
postgres ships two compression methods. pglz is built in and is the default. lz4 arrived in postgres 14 pg 14+ and needs a server compiled with it (--with-lz4). the browser engine wasn't:
SHOW default_toast_compression;
SELECT enumvals FROM pg_settings WHERE name = 'default_toast_compression';| default_toast_compressiontext |
|---|
| pglz |
| enumvalstext[] |
|---|
| {pglz} |
a native postgres 18.6 (Postgres.app here) has both, and you can pick per column:
CREATE TABLE z (body text);
INSERT INTO z VALUES (repeat('a', 1000000));
ALTER TABLE z ALTER COLUMN body SET COMPRESSION lz4;
INSERT INTO z VALUES (repeat('a', 1000000));
SELECT pg_column_compression(body) AS method, pg_column_size(body) AS stored
FROM z ORDER BY 1;| methodtext | storedint4 |
|---|---|
| lz4 | 3936 |
| pglz | 11452 |
checked on a real postgres 18.6 server. the browser engine can't run this one.
same megabyte: 11,452 bytes with pglz, 3,936 with lz4. a run of one letter is a cartoon of real data, though. which method wins for your documents, in size and in CPU, is a measurement you make on your documents.
keylines
- rows wider than about 2 KB get their big values compressed first, then moved out of line if still needed.
- out of line means chunks of up to 1,996 bytes in pg_toast.pg_toast_<oid>, and an 18-byte pointer in the row.
- values that don't compress (random data) go out of line at full size.
- an UPDATE that doesn't touch the big column copies only the pointer.
- attstorage picks the strategy per column: plain, main, external, extended.
- don't SELECT * from tables with big values you don't need.