Sub topic 12
How Out-of-Line Values Are Chunked and Fetched
Prerequisites and the basic model
This chapter assumes that you know basic SQL, can use psql, understand transactions, and know that PostgreSQL stores tables in files made of pages. You should also know that TOAST handles values that are too large to fit comfortably inside an ordinary heap tuple.
TOAST stands for The Oversized-Attribute Storage Technique. PostgreSQL uses it for variable-length values such as text, bytea, and large arrays. Instead of storing the complete value in the table's heap page, PostgreSQL can compress it or move it into a separate TOAST table.
The heap row keeps a small TOAST pointer. The pointer identifies the value's TOAST table, value identifier, and storage details. PostgreSQL follows that pointer when a query needs the complete value.
One value becomes several chunks
A TOAST table has a structure similar to this:
| Column | Meaning |
|---|---|
| `chunk_id` | Identifies one original toasted value |
| `chunk_seq` | The chunk's position, starting at zero |
| `chunk_data` | A portion of the value, stored as `bytea` |
A large value is divided into chunks of roughly 2 KB. The exact size depends on PostgreSQL's page layout and alignment rules. Each chunk is a separate row in the TOAST table.
The chunks are ordered by chunk_seq. PostgreSQL reads all chunks with the same chunk_id, sorts them by sequence, joins their bytes, and then decompresses the result if compression was used.
The chunk identifier is local to the TOAST table. It is not the same thing as the heap table's ctid, and it is not a user-visible row identifier.
Inspecting chunks with `psql`
The following example creates a value that is large enough to be toasted. The \gexec command is a psql feature. It executes SQL text returned by a query, which lets us use the actual TOAST table name without guessing its internal OID.
DROP TABLE IF EXISTS docs;
CREATE TABLE docs (
id integer PRIMARY KEY,
body text
);
INSERT INTO docs
VALUES (1, repeat('abcdefghij', 30000));
SELECT c.reltoastrelid::regclass AS toast_table
FROM pg_class AS c
WHERE c.oid = 'docs'::regclass;
SELECT format(
'SELECT chunk_id, chunk_seq, octet_length(chunk_data) AS chunk_bytes
FROM %s
ORDER BY chunk_id, chunk_seq
LIMIT 10;',
c.reltoastrelid::regclass
)
FROM pg_class AS c
WHERE c.oid = 'docs'::regclass
AND c.reltoastrelid <> 0
\gexecThe first query reports the internal TOAST table, often with a name such as pg_toast.pg_toast_.... The second query generates a query against that table. You should see multiple rows with the same chunk_id and increasing chunk_seq values.
This particular value is highly repetitive. PostgreSQL will usually compress it before deciding whether it must be stored out of line. Compression can reduce the number of chunks, so a large logical value does not necessarily produce many TOAST rows.
Fetching is transparent
Ordinary SQL does not expose the chunks. When you select body, PostgreSQL fetches and reconstructs the value automatically.
SELECT id,
length(body) AS characters,
octet_length(body) AS bytes
FROM docs;
SELECT body
FROM docs
WHERE id = 1;A query that only needs metadata may avoid fetching the full value. For example, an index-only lookup or a query that does not read body does not need to reconstruct it. A query that returns body, calculates a function on it, or compares its contents may need to fetch and decompress the value.
Storage choices
A column's storage setting controls how PostgreSQL handles large values.
| Strategy | Behavior |
|---|---|
| `EXTENDED` | Try compression, then move the value out of line; the default for many types |
| `EXTERNAL` | Move the value out of line without compression |
| `MAIN` | Prefer inline storage, but move the value if necessary |
| `PLAIN` | Keep the value inline and disable TOAST storage |
Use EXTERNAL when compression wastes CPU or when another layer already compresses the data. Use the default EXTENDED for ordinary text and byte strings that often compress well. Remember that changing the setting affects values stored afterward; existing values are not necessarily rewritten immediately.
Exercise
A TOAST table contains four rows with the same chunkid and chunkseq values 0, 1, 2, and 3. What do these rows represent?
Show answer
They are four ordered pieces of one original toasted value. PostgreSQL concatenates chunk_data in sequence order and decompresses the result when required.