In PostgreSQL, when is a wide column value TOASTed, and how do the PLAIN, MAIN, EXTERNAL and EXTENDED storage strategies differ?
answer
- no tuple spans two pages
- a row wider than about 2 kB
- compress first, then move out
- chunks in pg_toast, 18-byte pointer
- default_toast_compression
basics
~20 sWhen a row exceeds about 2 kB, PostgreSQL compresses and/or moves its wide variable-length values into a TOAST table in roughly 2 kB chunks, leaving an 18-byte pointer. EXTENDED allows both, EXTERNAL only moving out, MAIN mainly compression, PLAIN neither.
solid answer
~50 sPostgreSQL never lets a tuple span pages, so when a row about to be stored is wider than `TOAST_TUPLE_THRESHOLD` (about 2 kB with 8 kB pages), the TOAST code compresses and/or moves its largest variable-length values out of line until the row is under `TOAST_TUPLE_TARGET`, also about 2 kB and tunable per table with `toast_tuple_target`. Out-of-line values go to the table's TOAST table, `pg_toast.pg_toast_<oid>`, in chunks of about 2,000 bytes, and the row keeps an 18-byte pointer. The column's strategy decides what is allowed: **EXTENDED** (default for `text`, `bytea`, `jsonb`) compresses first, then moves out; **EXTERNAL** moves out uncompressed, which makes `substr()` on long values cheap; **MAIN** compresses and moves out only as a last resort (default for `numeric`); **PLAIN** does neither. `SET STORAGE` and `SET COMPRESSION` (`pglz`, the PostgreSQL 18 default, or `lz4`) affect only values stored afterwards.
code
sql · 12 lines-- Strategy and compression of each articles column
SELECT attname, attstorage, attcompression
FROM pg_attribute
WHERE attrelid = 'articles'::regclass AND attnum > 0 AND NOT attisdropped;
-- Read article previews in slices: keep body out of line, uncompressed
ALTER TABLE articles ALTER COLUMN body SET STORAGE EXTERNAL;
-- Or keep compression but use lz4 for values stored from now on
ALTER TABLE articles ALTER COLUMN body SET COMPRESSION lz4;
SELECT id, pg_column_compression(body) FROM articles LIMIT 5;go deeper
Recall that big text and bytea values are compressed or moved to a side table automatically, so a row never spans pages.
Explain the 2 kB threshold, the pass order, the four strategies and their type defaults, and that SET STORAGE and SET COMPRESSION affect only future values.
Reason about cost: SELECT star versus narrow columns, updates that do or do not rewrite TOAST, EXTERNAL for slicing, and TOAST tables needing vacuum too.
Weigh whether huge values belong in the database at all, and how row width, compression method and toast_tuple_target shape cache efficiency for a whole schema.
## Why TOAST exists PostgreSQL stores tables in fixed-size pages, 8 kB by default, and **a tuple can never span two pages**. Yet a single `text`, `bytea` or `jsonb` value may be up to 1 GB. **TOAST** (The Oversized-Attribute Storage Technique) closes that gap by compressing large values and/or slicing them into rows of a separate table. Only variable-length (varlena) types take part; fixed-width types such as `bigint` or `timestamptz` are never TOASTed. The same varlena header also saves space at the small end: a value shorter than 127 bytes is stored with a 1-byte length header instead of 4 bytes and without alignment padding. ## When the TOAST code runs It runs only when a row about to be stored is wider than `TOAST_TUPLE_THRESHOLD`, normally about 2 kB, the size that lets four tuples share a page. It then works in passes until the row is under `TOAST_TUPLE_TARGET`, also about 2 kB by default, or nothing more can be gained: 1. Compress inline values of **EXTENDED** columns, largest first; a value still too large on its own goes out of line at once. 2. Move the remaining **EXTENDED** and **EXTERNAL** values out of line. 3. Compress **MAIN** values inline. 4. As a last resort, move **MAIN** values out of line. An out-of-line value is cut into chunks of about 2,000 bytes, stored as rows (`chunk_id`, `chunk_seq`, `chunk_data`) of the table's TOAST table, `pg_toast.pg_toast_<table oid>`, with a unique index on `(chunk_id, chunk_seq)`. The main row keeps an **18-byte TOAST pointer** recording the value's ID, raw size, stored size and compression method. The target can be changed per table with the storage parameter `toast_tuple_target` (128 bytes up to 8160, default 2040); it affects new tuples only. ## The four storage strategies | Strategy | Compress? | Move out of line? | Typical use | |---|---|---|---| | `PLAIN` | No | No | The only option for fixed-width types such as `integer` | | `MAIN` | Yes | Only as a last resort | Default for `numeric` | | `EXTERNAL` | No | Yes | Long `text` or `bytea` read in slices | | `EXTENDED` | Yes, first | Yes | Default for `text`, `bytea`, `jsonb` | Each type has a default strategy (`pg_type.typstorage`) that is copied into `pg_attribute.attstorage` for each column. `ALTER TABLE ... ALTER COLUMN ... SET STORAGE` changes it, but **only for values stored later**; nothing is rewritten. **EXTERNAL** earns its place because uncompressed out-of-line values can be read in slices: `substr(body, 1, 300)` on an EXTERNAL column fetches only the first chunks instead of decompressing the whole value, at the cost of more disk. ## Compression methods - `pglz`, PostgreSQL's own algorithm, is the default of `default_toast_compression` in PostgreSQL 18. - `lz4` is available when the server was built `--with-lz4` and is generally faster; PostgreSQL 19, still in beta, changes the default to it. - `ALTER TABLE ... ALTER COLUMN ... SET COMPRESSION lz4` sets a column's method for values stored from then on. Existing values keep theirs, and `INSERT ... SELECT` from another table may carry compressed values over unchanged; a dump and restore rewrites every value with the configured method. - `pg_column_compression(body)` shows which method a stored value used, or NULL when it is not compressed. - `default_toast_compression` can be set per session or in `postgresql.conf`; a column's own `COMPRESSION` option overrides it. ## What it means for queries and updates - **Narrow queries stay cheap.** `SELECT id, title FROM articles` never reads the TOAST table. A large value that is only selected is usually fetched when the result is sent to the client. - **`SELECT *` is not free** on a table with TOASTed columns: every row's chunks are read and decompressed. - **Updating other columns** normally carries the existing TOAST pointer into the new row version, so bumping a counter does not rewrite a TOASTed body. - **Changing the large value** writes a whole new set of chunks; the old chunks become dead rows of the TOAST table, which VACUUM cleans like any other table. Its autovacuum behaviour can be tuned separately through storage parameters with the `toast.` prefix, such as `toast.autovacuum_enabled`. - **Expressions read the whole value.** A predicate or function on a compressed TOASTed value, such as a `jsonb` field lookup, detoasts and decompresses it first, so filtering on a large column costs far more than filtering on a narrow one. - A value compressed **inline**, small enough after compression to stay in the row, is copied in full into every new row version.
- When would you set a text column to EXTERNAL instead of the default EXTENDED?When the application reads slices of long values, such as the first few hundred characters of an article body for a preview. Uncompressed out-of-line values let `substr()` fetch only the chunks it needs instead of decompressing the whole value. The price is more disk, since nothing is compressed, and the change only applies to values stored afterwards.
- What does the toast_tuple_target storage parameter change, and when would you lower it?It sets the row width the TOAST code aims for once it starts (default 2040 bytes, range 128 to 8160) and also the minimum width before it tries. Lowering it pushes medium-sized values out of line, keeping heap rows small so scans of narrow columns read fewer pages, at the cost of TOAST reads when the value is selected. It affects new tuples only.
TOAST works like a theatre cloakroom. A guest whose coat is too bulky for the seat hands it in, sometimes folded small first, and keeps a numbered ticket: the 18-byte pointer. Moving along the rows is easy without coats, the cloakroom is visited only by someone who actually wants their coat back, and a guest who changes seats keeps the same ticket for the same coat.
- TOAST starts only when a single value is larger than the 8 kB page.
- EXTERNAL storage means the value is kept in a file outside the database.
- SET STORAGE or SET COMPRESSION immediately rewrites existing rows.
- SELECT id, title still reads every article body from the TOAST table.
- PostgreSQL 18 compresses TOASTed values with lz4 by default.
- Updating any column rewrites all of the row's TOASTed values too.
Assumes PostgreSQL 18, where default_toast_compression is pglz and lz4 needs a --with-lz4 build; PostgreSQL 19, still in beta, switches the default to lz4.