Sub topic 11
TOAST: Handling Values Too Large for a Page
Assume you know basic SQL, can use psql, and understand that a transaction records changes. This chapter explains how PostgreSQL stores a row when its variable-length values do not fit comfortably inside one heap page.
A PostgreSQL heap row cannot simply continue onto a second page. Each page is normally 8 KB, and a row must fit within a page after accounting for the page header, line pointer, tuple header, and other rows. PostgreSQL solves this problem with TOAST, short for The Oversized-Attribute Storage Technique.
TOAST applies mainly to variable-length data types, such as text, bytea, jsonb, arrays, and large varchar values. PostgreSQL tries to keep the main row small by compressing a value or moving it to a separate TOAST table.
What happens to a large value
PostgreSQL usually follows this sequence:
- It stores the value directly if the row fits.
- It compresses the value if compression reduces its size.
- It moves the value out of the main table if the row is still too wide.
- It stores a small TOAST pointer in the main row.
The exact threshold depends on the page size and tuple overhead. With the standard 8 KB page size, PostgreSQL starts taking action when a row is roughly a few kilobytes wide, not only when it reaches 8 KB. This leaves room for other rows and page structures.
The main table still appears to contain the column logically. When a query selects that column, PostgreSQL follows the pointer and reconstructs the original value. This process is called detoasting. It is normally invisible to SQL users.
Inspecting a toasted value
This example creates a repetitive value that PostgreSQL can compress:
CREATE TABLE documents (
id integer PRIMARY KEY,
body text
);
INSERT INTO documents
VALUES (1, repeat('abc', 100000));
SELECT octet_length(body) AS logical_bytes,
pg_column_size(body) AS stored_bytes
FROM documents;
SELECT reltoastrelid::regclass AS toast_table
FROM pg_class
WHERE oid = 'documents'::regclass;octet_length measures the logical text value. pg_column_size reports the storage size of the value as represented in the row. A repetitive value can have a much smaller stored size after compression.
The pg_class query returns the auxiliary TOAST relation associated with the table. Its name is generated by PostgreSQL, so do not write application code that depends on the exact name.
To compare the main heap with all storage used by the table, use size functions:
| Function | What it includes |
|---|---|
| `pg_relation_size` | The main heap relation |
| `pg_table_size` | The heap, TOAST data, and free-space metadata |
| `pg_total_relation_size` | The table, TOAST data, and indexes |
SELECT pg_relation_size('documents') AS heap_bytes,
pg_table_size('documents') AS table_bytes,
pg_total_relation_size('documents') AS total_bytes;A small heap size does not prove that the data uses little disk space. Large values may be stored in the associated TOAST relation.
When TOAST affects performance
Reading a query that does not need a large column can avoid fetching the full value. For example, a query selecting only id may read the heap row without detoasting body.
Selecting, sorting, comparing, or repeatedly processing the large column can require PostgreSQL to fetch and possibly decompress the value. Updates can also create new row versions, so changing another column may cause storage work for a wide row.
TOAST behavior can be controlled per column with storage and compression settings. The default is usually a good choice. Use a different setting only after measuring the workload, because avoiding compression can trade CPU time for disk and I/O usage.
Exercise
Why can a table's heap size be small while its total size is large?
Show answer
Large variable-length values may be compressed or stored out of line in the table's associated TOAST relation. The heap contains only the row and, when needed, a small pointer.