Skip to content

zstd

Zstandard compression algorithm implementation in PostgreSQL

pg_zstd · Zstandard compression algorithm implementation in PostgreSQL

Overview

IDExtensionPackageVersionCategoryLicenseLanguage
4030zstdpg_zstd1.1.2UTILISCC
AttributeHas BinaryHas LibraryNeed LoadHas DDLRelocatableTrusted
--s-d-rNoYesNoYesyesno
+varatt.h

Packages

TypeRepoVersionPG Major CompatibilityPackage PatternDependencies
EXTPIGSTY1.1.219 18 17 16 15pg_zstd-
RPMPIGSTY1.1.219 18 17 16 15pg_zstd_$v-
DEBPIGSTY1.1.219 18 17 16 15postgresql-$v-zstd-
Linux / PGPG19PG18PG17PG16PG15
el8.x86_64N/APIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
el8.aarch64N/APIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
el9.x86_64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
el9.aarch64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
el10.x86_64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
el10.aarch64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
d12.x86_64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
d12.aarch64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
d13.x86_64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
d13.aarch64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
u22.x86_64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
u22.aarch64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
u24.x86_64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
u24.aarch64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
u26.x86_64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2
u26.aarch64PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2PIGSTY 1.1.2

Source

pig build pkg pg_zstd;		# build rpm/deb

Install

Make sure PGDG and PIGSTY repo available:

pig repo add pgsql -u   # add both repo and update cache

Install this extension with pig:

pig install pg_zstd;		# install by package name
pig install zstd;		# install by extension name

pig install zstd -v 19;   # install for PG 19
pig install zstd -v 18;   # install for PG 18
pig install zstd -v 17;   # install for PG 17
pig install zstd -v 16;   # install for PG 16
pig install zstd -v 15;   # install for PG 15

Create this extension with:

CREATE EXTENSION zstd;

Usage

FunctionReturn Type
zstd_compress(data bytea [, dictionary bytea [, level integer ]])bytea
zstd_decompress(data bytea [, dictionary bytea ])bytea
zstd_length(data bytea)integer

zstd_compress compresses the provided data and returns a Zstandard frame. A preset dictionary may also be provided. The default compression level may also be overriden, valid values range from 1 (best speed) to 22 (best compression). The default level is 3.

If you want to override the compression level without using a dictionary, setdictionary to NULL.

zstd_decompress decompresses the provided Zstandard frame in data and returns the uncompressed data. A preset dictionary, matching the dictionary used to compress the data, may also be provided.

zstd_length returns the decompressed length of the provided Zstandard frame. If ZSTD_getFrameContentSize() is available it returns NULL if the length is unknown. If unavailable, it isn’t possible to distinguish the error, unknown decompressed length and zero decompressed length cases.

Example

Basic compress/decompress example:

CREATE EXTENSION zstd;

SELECT zstd_compress('hello world');
-- zstd_compress
-- --------------------------------------------
-- \x28b52ffd200b59000068656c6c6f20776f726c64

SELECT convert_from(zstd_decompress('\x28b52ffd200b59000068656c6c6f20776f726c64'), 'utf-8');
-- convert_from
-- --------------
--  hello world

Compress with level (1 for best speed, 22 for best compression, default to 3)

CREATE EXTENSION zstd;

SELECT zstd_compress('hello world',  NULL, 10);
-- zstd_compress
-- --------------------------------------------
-- \x28b52ffd200b59000068656c6c6f20776f726c64

SELECT convert_from(zstd_decompress('\x28b52ffd200b59000068656c6c6f20776f726c64'), 'utf-8');
-- convert_from
-- --------------
--  hello world

Table reader