zson
zson · Dictionary-trained compressed JSONB-compatible type with transparent casts and JSONB operators.
Overview
Usage
Sources:
- Pinned upstream README
- Pinned version 1.1 installation SQL
- Pinned extension control file
- Pinned official benchmark notes
zson version 1.1 is a dictionary-compressed, JSONB-compatible storage type. It learns frequently repeated strings—including keys, string values, and array elements—from representative JSONB documents, then uses that shared dictionary when encoding new zson values.
Core Workflow
Train a dictionary before inserting compressed values. The training argument is a two-dimensional array of table and JSONB-column names:
Run zson_learn() again after the document vocabulary changes materially. New values use the new dictionary, while existing values retain their original dictionary identifier.
Important Objects
zson: variable-length compressed type with assignment cast fromjsonband implicit cast tojsonb, allowing JSONB operators after decompression.zson_learn(tables_and_columns, max_examples, min_length, max_length, min_count): samples JSONB columns and creates the next dictionary with up to 65,534 frequent strings.zson_dict(dict_id, word_id, word): extension-managed dictionary table included in extension configuration dumps.zson_info(zson): reports format and dictionary-version information for a stored value.zson_extract_strings(jsonb): recursively extracts object keys and string values used during training.
Operational Notes
The extension supports installation only in schema public. zson_learn() constructs dynamic SQL from table and column names and scans sample data, so allow only trusted administrators to run it, bound max_examples, and train on a representative distribution. Compression and throughput gains are data- and workload-dependent; reproduce the official benchmark method with the actual dataset instead of treating published numbers as guarantees.
Dictionaries are cached, and the upstream guide says a new dictionary may take about a minute to become active in all backends. Never delete an old dict_id while any stored value still references it, or those documents can become unreadable; keeping a few kilobytes of old dictionary data is safer. Test backup/restore with zson_dict, mixed dictionary versions, expression indexes that cast to jsonb, and the CPU cost of decompression before replacing JSONB columns with zson.