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

ItemValue
Applicable data typesVariable-length types (text, varchar, bytea, jsonb, arrays, and so on). Fixed-length types such as integer are not applicable
Trigger thresholdWhen a row exceeds about 2KB (TOAST_TUPLE_THRESHOLD, default 2032 bytes)
Target sizeProcesses the largest values first until the row shrinks below about 2KB. Adjustable per table with toast_tuple_target
Chunk sizeAbout 2KB (four chunks per page)
Maximum size of one value1GB
Compressionpglz (default), lz4 (since 14). default_toast_compression, per-column COMPRESSION
Pointer inside the row18 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.

StrategyCompressionMove out of the rowUsed for
PLAINNoNoFixed-length data types
MAINYesOnly as a last resortValues you want to keep in the row
EXTERNALNoYesLarge text and bytea with frequent partial reads (substr)
EXTENDEDYesYesDefault 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

PostgreSQLInnoDBOracleSQL Server
NameTOASTOff-page storageLOBLOB, row overflow
Threshold for moving outRow about 2KBRow near half a pageLOB about 4,000 bytesRow 8,060 bytes, max value 8,000 bytes
CompressionCompresses first by defaultIn the COMPRESSED row formatSecureFiles optionPage and row compression apply only to in-row data
Where it goesPer-table TOAST tableOverflow pages in the same tablespaceLOB segmentLOB 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.

Related pages