Quick revision
The short answer
TOAST is how PostgreSQL stores values too big for its 8 KB pages: when a row passes about 2 KB, PostgreSQL first compresses its large values and, if they are still large, moves them in chunks of about 2 KB into a separate TOAST table, leaving only a small pointer in the row. So a 200 KB text value is compressed, with pglz or lz4, and stored as chunks in the table's TOAST relation, and reading that column later fetches and decompresses those chunks. Each column has a storage strategy, PLAIN, MAIN, EXTERNAL or EXTENDED, that decides what is allowed.
What to remember
In this answer, we saw that PostgreSQL rows must fit in 8 KB pages, so TOAST shrinks rows above about 2 KB by compressing their large values with pglz or lz4 and, if still needed, moving them as chunks of about 2 KB into the table's TOAST relation, with an 18-byte pointer left in the row. Reading such a column means fetching and decompressing the chunks, which is why selecting only the needed columns is much cheaper than SELECT *. We also compared the four storage strategies, PLAIN, MAIN, EXTERNAL and EXTENDED, with EXTENDED as the default for text and jsonb.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
TOAST is how PostgreSQL stores values too big for its 8 KB pages: when a row passes about 2 KB, PostgreSQL first compresses its large values and, if they are still large, moves them in chunks of about 2 KB into a separate TOAST table, leaving only a small pointer in the row. So a 200 KB text value is compressed, with pglz or lz4, and stored as chunks in the table's TOAST relation, and reading that column later fetches and decompresses those chunks. Each column has a storage strategy, PLAIN, MAIN, EXTERNAL or EXTENDED, that decides what is allowed.
Why this question is asked
Every PostgreSQL table that has text, jsonb or bytea columns uses TOAST, usually without anyone noticing. It becomes visible when large values make queries slow or updates expensive, or when someone wonders why a table with a few thousand rows takes gigabytes. Interviewers ask about it because the answer explains real behaviour: why SELECT * can be much slower than selecting a few columns, and why some updates write so much WAL. In this answer, we will recap pages and tuples, follow the 200 KB insert step by step, look at what reading the value costs, and compare the four storage strategies.
1A Quick Recap: Pages and Tuples
PostgreSQL stores a table as a file of fixed-size pages, 8 KB each by default. A row, which PostgreSQL calls a tuple, must fit inside one page, and PostgreSQL does not let one row span pages. That would make a 200 KB value impossible to store directly.
The solution is TOAST, short for The Oversized-Attribute Storage Technique. Every table with a column that could hold large values gets a companion table, its TOAST relation, in the pg_toast schema. Large values are moved there, broken into small chunks, and the main row keeps only a pointer to them.
2Following a 200 KB Insert
Suppose we insert a row into articles whose body column holds 200 KB of text. PostgreSQL checks the size of the whole row. Because it is above TOAST_TUPLE_THRESHOLD, about 2 KB on 8 KB pages, PostgreSQL tries to shrink the row until it fits under TOAST_TUPLE_TARGET, also about 2 KB.
The first step is compression. For a column with the default EXTENDED strategy, PostgreSQL compresses the value with the column's method, pglz by default or lz4 if default_toast_compression or the column says so. Typical prose compresses to perhaps a third, so our 200 KB might become about 65 KB.
65 KB is still far too large for the row, so the second step is moving the value out of line. PostgreSQL splits the compressed bytes into chunks of just under 2 KB, so about 33 chunks here, and inserts each chunk as a row in the TOAST table with a chunk id and a sequence number. The main row keeps an 18-byte TOAST pointer that records the chunk id and the sizes.
- Row larger than about 2 KB?YesTry to shrink the rowNoStore the row as it is
- Compress the largest valuespglz or lz4, EXTENDED and MAIN columns
- Row now under about 2 KB?YesStore it inline, compressedNoMove values out of line
- Split into chunks of about 2 KBrows in
pg_toast.pg_toast_<oid> - Main row keeps an 18-byte pointerchunk id and sizes
5 steps in order. Play them, or step through them one at a time.
Notice that repeated text compressed from 200 KB to about 2.4 KB, so in this case only a couple of chunks were needed. Random or already-compressed data barely shrinks, so a 200 KB random value needs about 100 chunks.
3What Reading the Value Costs
When a query reads a TOASTed column, PostgreSQL follows the pointer: it looks up the chunk id in the TOAST table's index, reads every chunk in order, joins them, and decompresses the result. This is called detoasting. For one 200 KB value it might mean dozens of extra page reads plus decompression CPU.
The important point is that this happens only when the column is actually needed. If a query reads only id and title, PostgreSQL never touches the TOAST table. That is why SELECT * can be so much slower than listing columns: it forces every large value to be fetched and decompressed, and then sent over the network. In a Spring service, a list page that loads full entities, including a large body, pays this cost for every row, while a projection with only the needed columns does not.
4The Four Storage Strategies
Each column has a storage strategy that tells TOAST what it may do with the value. PLAIN allows neither compression nor out-of-line storage; it is used for fixed-length types such as integer and timestamp, which can never be large. MAIN allows compression, and moves the value out of line only as a last resort. EXTERNAL allows out-of-line storage but no compression. EXTENDED allows both, compression first, and is the default for most variable-length types, including text, jsonb and bytea.
| Strategy | Compress? | Out of line? | Typical use |
|---|---|---|---|
| PLAIN | no | no | fixed-length types such as int, timestamp |
| MAIN | yes | only as a last resort | keep values inline when possible |
| EXTERNAL | no | yes | fast substring, already-compressed data |
| EXTENDED | yes | yes, after compressing | default for text, jsonb, bytea |
The choice matters for specific workloads. EXTERNAL makes substring operations on large text or bytea fast, because PostgreSQL can fetch only the chunks that hold the requested bytes instead of decompressing the whole value. It also avoids wasting CPU on data that is already compressed, such as images or gzip files.
5Follow-up questions
When would you choose STORAGE EXTERNAL?
When we often read parts of large values, for example substring(body from 1 for 500) on long documents, or byte ranges of a bytea, because without compression PostgreSQL can fetch only the chunks that hold those bytes. It is also right for data that is already compressed, such as images or archives, where compression only costs CPU. The price is more disk space for compressible data.
Is lz4 better than pglz?
lz4 is usually much faster to compress and decompress, with a slightly lower or similar compression ratio, so it saves CPU on TOAST-heavy workloads. It needs PostgreSQL 14 or later built with lz4 support, and changing it affects only new values; existing ones stay pglz until rewritten.
Does TOAST apply to indexes too?
Index entries can be compressed, but they cannot be moved out of line, and an index entry has a size limit of roughly a third of a page. That is why a B-tree index on a large text column fails with an error saying the index row size exceeds the maximum for long values, and why we index a hash or a prefix instead.
6Conclusion
In this answer, we saw that PostgreSQL rows must fit in 8 KB pages, so TOAST shrinks rows above about 2 KB by compressing their large values with pglz or lz4 and, if still needed, moving them as chunks of about 2 KB into the table's TOAST relation, with an 18-byte pointer left in the row. Reading such a column means fetching and decompressing the chunks, which is why selecting only the needed columns is much cheaper than SELECT *. We also compared the four storage strategies, PLAIN, MAIN, EXTERNAL and EXTENDED, with EXTENDED as the default for text and jsonb.
Open with
TOAST is how PostgreSQL stores values that do not fit in a row. Pages are 8 KB and a row must fit in one page, so when a row goes above about 2 KB, the TOAST threshold, PostgreSQL shrinks it.
Then, if they want more
For a 200 KB text column with the default EXTENDED strategy, it first compresses the value, with pglz by default or lz4 if configured.
If the compressed value is still too big, it splits it into chunks of roughly 2 KB and stores them in the table's TOAST relation, a separate table in the pg_toast schema with an index on chunk id and sequence. The main row just keeps a small pointer, about 18 bytes.
When a query reads that column, PostgreSQL follows the pointer, reads all the chunks, joins and decompresses them, so a query that does not need the column never pays for it. That is a concrete reason to avoid SELECT * and use projections when large columns are involved.
Each column has a storage strategy: PLAIN means no compression and no out-of-line storage, for fixed-size types; MAIN compresses and only moves out of line as a last resort; EXTERNAL moves out of line without compression, which is good for fast substrings and already-compressed data; and EXTENDED does both and is the default for text, jsonb and bytea.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 14
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is TOAST in PostgreSQL, and what happens when you insert a row with a 200 KB text...": https://scrivio-interview-library.netlify.app/databases/postgresql/66b06609e5/