TOAST
TOAST (The Oversized-Attribute Storage Technique) is how PostgreSQL stores large values. A row cannot exceed one 8KB page, so when a row exceeds a certain size, PostgreSQL first compresses the large values; if the row is still large, it slices them and moves them into the TOAST table that each table has separately, leaving only a pointer in the original row. Users do not need to be aware of this process: they insert and read large values directly in columns such as text, bytea, and jsonb.
Key Specifications
| Item | Value |
|---|---|
| Applicable data types | Variable-length types (text, varchar, bytea, jsonb, arrays, and so on). Fixed-length types such as integer are not applicable |
| Trigger threshold | When a row exceeds about 2KB (TOAST_TUPLE_THRESHOLD, default 2032 bytes) |
| Target size | Processes the largest values first until the row shrinks below about 2KB. Adjustable per table with toast_tuple_target |
| Chunk size | About 2KB (four chunks per page) |
| Maximum size of one value | 1GB |
| Compression | pglz (default), lz4 (since 14). default_toast_compression, per-column COMPRESSION |
| Pointer inside the row | 18 bytes |
How It Works
Figure. How a large value moves to the TOAST table
flowchart LR
A["INSERT a row<br/>over about 2KB"] --> B["① Compress compressible large values first"]
B --> C{"Reduced below 2KB?"}
C -- "Yes" --> D["Store the compressed value in the row"]
C -- "No" --> E["② Slice the large value into chunks of about 2KB<br/>and store in pg_toast.pg_toast_tableOID"]
E --> F["18-byte pointer in the original row"]
Diagram notes: when a row exceeds the threshold, ① PostgreSQL compresses the compressible columns, largest first. If the row is still large, ② it slices the value into chunks and puts them in the TOAST table, leaving in the original row only a pointer to the TOAST table and the value number. The TOAST table has a (chunk_id, chunk_seq) index, which is used to find the chunks in order and join them.
Column storage strategy. Each column decides how it uses TOAST.
| Strategy | Compression | Move out of the row | Used for |
|---|---|---|---|
PLAIN | No | No | Fixed-length data types |
MAIN | Yes | Only as a last resort | Values you want to keep in the row |
EXTERNAL | No | Yes | Large text and bytea with frequent partial reads (substr) |
EXTENDED | Yes | Yes | Default for most variable-length data types |
Updates. If an UPDATE does not change the TOASTed column, the new row version reuses the same pointer. The large value is not copied again, so the update cost does not grow much even when a large column and a frequently changing small column are in the same table.
Read cost. A TOASTed value is fetched from the TOAST table and decompressed (detoast) only when that column is actually read. So SELECT * becomes slow because it reads large values that are not needed, and selecting only the needed columns is faster.
Configuration and Checks
-- The TOAST table of a table and its size
SELECT c.relname, t.relname AS toast_table,
pg_size_pretty(pg_relation_size(c.reltoastrelid)) AS toast_size
FROM pg_class c JOIN pg_class t ON t.oid = c.reltoastrelid
WHERE c.relname = 'orders';
-- Size one value actually occupies (after compression) and its original length
SELECT pg_column_size(memo) AS stored_bytes, octet_length(memo) AS raw_bytes FROM orders LIMIT 5;
-- Change the strategy and compression method
ALTER TABLE docs ALTER COLUMN body SET STORAGE EXTERNAL;
ALTER TABLE docs ALTER COLUMN body SET COMPRESSION lz4; -- applies to values written from now on
ALTER TABLE docs SET (toast_tuple_target = 4080);
- TOAST tables are also targets of
VACUUM. If large values change often, dead chunks accumulate in the TOAST table. - The buffer counts in
EXPLAIN (ANALYZE, BUFFERS)include the amount read from the TOAST table.
Comparison with Other DBMSs
| PostgreSQL | InnoDB | Oracle | SQL Server | |
|---|---|---|---|---|
| Name | TOAST | Off-page storage | LOB | LOB, row overflow |
| Threshold for moving out | Row about 2KB | Row near half a page | LOB about 4,000 bytes | Row 8,060 bytes, max value 8,000 bytes |
| Compression | Compresses first by default | In the COMPRESSED row format | SecureFiles option | Page and row compression apply only to in-row data |
| Where it goes | Per-table TOAST table | Overflow pages in the same tablespace | LOB segment | LOB and row-overflow pages in the same file |
From the Articles
NOTE The threshold sizes and compression methods can vary with version and build settings.