NEW RasterizedDB 1.1.0 is out: sharding and the gateway, the message broker, roles and row-level security, compression, .NET on NuGet, and the enterprise edition. What's new ›
DOCS / RASTERIZEDDB SERVER / COMPRESSION

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):

MethodMeaning
zstdzstd at level 3.
zstd(N)zstd at level N, from 1 (fastest) to 22 (smallest).
lz4, pglzAccepted for compatibility with pg_dump output and stored as zstd (level 3).
default, none, uncompressedNo 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:

sql
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:

sql
ALTER TABLE docs ALTER COLUMN note SET COMPRESSION zstd(19);
ALTER TABLE docs ALTER COLUMN note SET COMPRESSION default;   -- stop compressing new values

COMPRESSION 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:

sql
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 rows

Reading 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:

sql
ALTER TABLE docs RECOMPRESS;

or, as part of compaction:

sql
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 RECOMPRESS trains 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:

sql
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';
text
 reloptions
-------------------------
 {row_compression=zstd(6)}

SHOW TABLE STATS reports what compression achieved:

sql
SHOW TABLE STATS docs;
MetricWhat it shows
compressed_columnsEach compressed column and its method, with +dictionary when one is in use, for example body=zstd(9) +dictionary.
row_compressionThe row compression method, or none.
compressed_valuesValues stored as their own compressed frame.
compressed_row_tailsRows stored with row compression.
compression_raw_bytesBytes of the values before compression.
compression_stored_bytesBytes 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.

rust
#[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 COMPRESSION clause and row_compression option and ignores them, so pg_dump output 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.
  • lz4 and pglz are 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.
  • RECOMPRESS is an offline rewrite under an exclusive lock; there is no online recompression.
  • pg_column_compression(value) is not available. Use attcompression and SHOW TABLE STATS.
  • A single value is limited to 64 MiB, compressed or not.
✓ page rendered in 164 µs axum + tera + htmx · no other javascript