Compression
RasterizedDB can store text, JSON and binary values compressed with zstd, per column or per row. Use it for tables whose variable-width values (TEXT, JSONB, BYTEA, arrays, ranges) take most of the disk: documents, logs, events, payloads.
Nothing is compressed unless you ask for it. A table with no compression setting is stored exactly as it would be without the feature.
How it works
There are two ways to ask, and both leave the fixed-width part of a row (numbers, booleans, dates) uncompressed, so filters and aggregates on those columns run at full speed:
- Column compression. Each value of one column is stored as its own zstd frame. Best for columns that hold large values: JSON documents, long text, blobs.
- Row compression. All the variable-width values of a row (every text, JSONB, BYTEA and array value) are stored together as one frame. Best for tables with many short strings per row, where no single value is long enough to compress on its own.
A value, or a row's combined values, is stored compressed only when it is at least 64 bytes and compression saves at least an eighth of its size. Otherwise it is stored as it is. One column can therefore hold compressed and uncompressed values side by side, and short or incompressible values cost nothing.
When a table has both settings, row compression wins for that row: its frame holds every value.
Choosing a level
A method is written as zstd or zstd(N):
| Method | Meaning |
|---|---|
zstd | zstd at level 3. |
zstd(N) | zstd at level N, from 1 (fastest) to 22 (smallest). |
lz4, pglz | Accepted for compatibility with pg_dump output and stored as zstd (level 3). |
default, none, uncompressed | No compression. |
Low levels (1 to 6) compress quickly and are a good default for tables that are written often. High levels (15 to 22) make writes noticeably slower and suit data that is written once and read many times. Reading is fast at every level.
Compress a column
Add COMPRESSION to a variable-width column, in CREATE TABLE or ADD COLUMN:
CREATE TABLE docs (
id INT PRIMARY KEY,
body JSONB COMPRESSION zstd(9),
note TEXT COMPRESSION zstd
);
ALTER TABLE docs ADD COLUMN summary TEXT COMPRESSION zstd(4);Change or stop it later:
ALTER TABLE docs ALTER COLUMN note SET COMPRESSION zstd(19);
ALTER TABLE docs ALTER COLUMN note SET COMPRESSION default; -- stop compressing new valuesCOMPRESSION on a fixed-width column such as INT is refused, as in PostgreSQL. SET COMPRESSION default on one is accepted.
Compress whole rows
Set the row_compression storage option with WITH (…) when you create the table, or with SET (…) later. The value is a quoted method:
CREATE TABLE events (
id INT PRIMARY KEY,
kind TEXT,
src TEXT,
msg TEXT
) WITH (row_compression = 'zstd(6)');
ALTER TABLE events SET (row_compression = 'zstd');
ALTER TABLE events RESET (row_compression); -- stop compressing new rowsReading one string column of a row-compressed row decompresses all of that row's variable-width values, so prefer column compression when queries usually read one large column.
PostgreSQL's own storage options (fillfactor, parallel_workers, autovacuum_*, toast.* and similar) are accepted and ignored, so pg_dump output loads. Any other option name is refused with unrecognized parameter.
Existing rows: RECOMPRESS
Like PostgreSQL's SET COMPRESSION, every setting above applies to values written from then on. Old rows keep their bytes until they are next updated or the table is rewritten. To rewrite every row under the table's current settings:
ALTER TABLE docs RECOMPRESS;or, as part of compaction:
VACUUM (FULL, RECOMPRESS) docs;RECOMPRESS also undoes compression: after SET COMPRESSION default or RESET (row_compression), it rewrites the old compressed rows uncompressed.
RECOMPRESS rewrites the whole table and holds an exclusive lock on it while it runs, so other sessions wait. Run it in a maintenance window on large tables. Automatic compaction never recompresses (see Maintenance).
Dictionaries
While it rewrites, RECOMPRESS trains a zstd dictionary for each compressed column, and one for row compression, from a sample of the table's own values (up to 4096 values or 8 MiB). A dictionary lets short, similar values, such as small JSON objects that repeat the same keys, compress far better than they would alone.
You do not manage dictionaries yourself:
- one is trained only when there are enough samples (at least 32 values and 8 KiB);
- it is kept only when it shrinks the samples by at least a tenth more than compression without one;
- the next
RECOMPRESStrains a fresh one and retires the old; - renaming a column keeps its setting and dictionary; dropping a column retires them.
To get dictionaries, run RECOMPRESS once the table holds representative data.
See what is set and what it saves
pg_attribute.attcompression shows each column's method (zstd, zstd(19), or empty for none), and pg_class.reloptions shows row compression:
SELECT a.attname, a.attcompression
FROM pg_attribute a JOIN pg_class c ON c.oid = a.attrelid
WHERE c.relname = 'docs';
SELECT reloptions FROM pg_class WHERE relname = 'events'; reloptions
-------------------------
{row_compression=zstd(6)}SHOW TABLE STATS reports what compression achieved:
SHOW TABLE STATS docs;| Metric | What it shows |
|---|---|
compressed_columns | Each compressed column and its method, with +dictionary when one is in use, for example body=zstd(9) +dictionary. |
row_compression | The row compression method, or none. |
compressed_values | Values stored as their own compressed frame. |
compressed_row_tails | Rows stored with row compression. |
compression_raw_bytes | Bytes of the values before compression. |
compression_stored_bytes | Bytes actually stored for them. |
A table with no compression setting has none of these rows.
Declare it from the Rust ORM
The Rust client's Model derive can declare compression, and the migrator keeps it in step with the database. See ORM and Migrations.
#[derive(Model)]
#[rdb(table = "events", row_compression = "zstd(6)")]
struct Event {
#[rdb(primary_key)]
id: i32,
kind: String,
src: String,
msg: String,
}
#[derive(Model)]
struct Doc {
#[rdb(primary_key)]
id: i32,
#[rdb(json, compression = "zstd(9)")]
body: serde_json::Value,
#[rdb(compression)] // zstd at level 3
note: String,
}The migrator changes a compression setting when it finds a difference, but never runs RECOMPRESS: rewriting a table is left to you.
Compatibility
- Compression is invisible to queries, indexes, constraints, replication, backups and branching: every reader sees the value as it was written.
- Partitioned tables compress in every partition.
- Once any column or table in a database uses compression, the data directory is marked as needing it. A server build without compression refuses to open that directory instead of misreading it.
- A server built without compression accepts every
COMPRESSIONclause androw_compressionoption and ignores them, sopg_dumpoutput and the PostgreSQL importer load either way.
Limits
- Only variable-width values are compressed. Numeric, boolean, date and other fixed-width columns are never compressed.
- The 64-byte threshold is fixed.
lz4andpglzare stored as zstd; there is no lz4 codec.- Settings affect new values only. Existing rows change on
RECOMPRESS,VACUUM (FULL, RECOMPRESS)or their next update. RECOMPRESSis an offline rewrite under an exclusive lock; there is no online recompression.pg_column_compression(value)is not available. UseattcompressionandSHOW TABLE STATS.- A single value is limited to 64 MiB, compressed or not.