Sub topic 13
Storage Strategies and Compression Methods
This chapter assumes basic SQL, familiarity with psql, and a general idea of transactions, files, and bytes. It focuses on choosing how PostgreSQL stores large variable-length column values.
Why storage strategy matters
PostgreSQL gives each column a TOAST storage strategy. TOAST is PostgreSQL's system for storing values that are too large to fit comfortably inside a normal table page.
The strategy controls whether PostgreSQL:
- Keeps a value inline in the table row.
- Compresses the value.
- Stores the value out of line in a TOAST table.
- Uses a combination of compression and out-of-line storage.
The four strategies are:
| Strategy | Compresses | Stores out of line | Typical use |
|---|---|---|---|
| `PLAIN` | No | No | Small values or types that must remain inline |
| `MAIN` | Yes, when useful | As a last resort | Prefer inline compressed values |
| `EXTERNAL` | No | Yes, when needed | Values that should be fetched without decompression |
| `EXTENDED` | Yes, then out of line | Yes | General-purpose default |
EXTENDED is the default for most variable-length types. PostgreSQL first tries to compress a large value. If the compressed value is still too large, PostgreSQL stores it in chunks in the table's associated TOAST table.
Choosing a strategy
Use EXTENDED when values are often repetitive and compression can reduce disk usage. Text containing repeated words, JSON with repeated keys, and many similar document fields may compress well.
Use EXTERNAL when compression is not useful or when faster access to the original bytes matters. PostgreSQL can fetch an externally stored value without first decompressing it. This can help with already-compressed data such as JPEG images, ZIP archives, or encrypted payloads.
MAIN is useful when keeping values in the main heap row is more important than moving them out of line. It still permits out-of-line storage if the row cannot fit, so it is a preference rather than an absolute guarantee.
Use PLAIN cautiously. A value that cannot fit within the page and row limits can make an insert or update fail. This strategy is generally intended for types whose storage rules require inline values.
Compression methods
PostgreSQL traditionally uses pglz, a built-in compression method. PostgreSQL 14 and later can also use lz4 when the server was built with LZ4 support.
LZ4 usually compresses and decompresses faster than pglz, but the compressed result may be larger for some data. pglz may produce a smaller result for certain repetitive values. The best choice depends on the value distribution and whether storage space or CPU time is more important.
The database-wide default is controlled by default_toast_compression. You can override it for an individual column:
CREATE TABLE documents (
id bigint PRIMARY KEY,
body text
);
ALTER TABLE documents
ALTER COLUMN body SET STORAGE EXTENDED;
ALTER TABLE documents
ALTER COLUMN body SET COMPRESSION lz4;
SELECT attname, attstorage, attcompression
FROM pg_attribute
WHERE attrelid = 'documents'::regclass
AND attname = 'body';The attstorage value records the strategy. The attcompression value identifies the selected compression method. An empty compression value can mean the column uses the database default.
Changing these settings does not rewrite values already stored. New values use the new settings. To apply the setting to existing values, rewrite them with an update, provided that the application can tolerate the resulting table and index work:
UPDATE documents
SET body = body
WHERE id = 1;A broad rewrite can create dead row versions and require later vacuuming, so test its space and locking effects before applying it to a large table.
A practical decision process
- Start with
EXTENDED, the general-purpose option. - Measure heap, TOAST, and total relation sizes.
- Check whether values are already compressed or encrypted.
- Test
pglzandlz4on representative rows. - Choose
EXTERNALwhen decompression cost matters more than compression savings. - Rewrite existing values only when the operational cost is acceptable.
Exercise
Which strategy is a sensible first choice for repetitive text that may be too large for a heap row, and what must you do before existing values use a newly selected compression method?
Show answer
Use EXTENDED. After changing the compression setting, rewrite existing values, for example with an UPDATE, because the setting applies to values stored afterward.