zstd

ZSTD压缩解压缩函数包

概览

扩展包名版本分类许可证语言
pg_zstd1.1.2UTILISCC
ID扩展名BinLibLoadCreateTrustReloc模式
4030zstd-
相关扩展gzip bzip http pg_net pg_curl pgjq pgjwt pg_smtp_client

+varatt.h

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.1.21817161514pg_zstd-
RPMPIGSTY1.1.21817161514pg_zstd_$v-
DEBPIGSTY1.1.21817161514postgresql-$v-zstd-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
PIGSTY 1.1.2
PIGSTY 1.1.2
PIGSTY 1.1.2
PIGSTY 1.1.2
PIGSTY 1.1.2
u24.x86_64
u24.aarch64
PIGSTY 1.1.2
PIGSTY 1.1.2
PIGSTY 1.1.2
PIGSTY 1.1.2
PIGSTY 1.1.2

构建

您可以使用 pig build 命令构建 pg_zstd 扩展的 RPM / DEB 包:

pig build pkg pg_zstd         # 构建 RPM / DEB 包

安装

您可以直接安装 pg_zstd 扩展包的预置二进制包,首先确保 PGDGPIGSTY 仓库已经添加并启用:

pig repo add pgsql -u          # 添加仓库并更新缓存

使用 pig 或者是 apt/yum/dnf 安装扩展:

pig install pg_zstd;          # 当前活跃 PG 版本安装
pig ext install -y pg_zstd -v 18  # PG 18
pig ext install -y pg_zstd -v 17  # PG 17
pig ext install -y pg_zstd -v 16  # PG 16
pig ext install -y pg_zstd -v 15  # PG 15
pig ext install -y pg_zstd -v 14  # PG 14
dnf install -y pg_zstd_18       # PG 18
dnf install -y pg_zstd_17       # PG 17
dnf install -y pg_zstd_16       # PG 16
dnf install -y pg_zstd_15       # PG 15
dnf install -y pg_zstd_14       # PG 14
apt install -y postgresql-18-zstd   # PG 18
apt install -y postgresql-17-zstd   # PG 17
apt install -y postgresql-16-zstd   # PG 16
apt install -y postgresql-15-zstd   # PG 15
apt install -y postgresql-14-zstd   # PG 14

创建扩展

CREATE EXTENSION zstd;

用法

函数返回类型
zstd_compress(data bytea [, dictionary bytea [, level integer ]])bytea
zstd_decompress(data bytea [, dictionary bytea ])bytea
zstd_length(data bytea)integer

zstd_compress 对提供的 data 进行压缩,返回一个 Zstandard 帧。可以选择提供预设的 dictionary(字典)。也可以覆盖默认的压缩 level(级别),有效值范围为 1(最快速度)到 22(最高压缩率),默认级别为 3

如果需要在不使用字典的情况下覆盖压缩级别,请将 dictionary 设置为 NULL

zstd_decompressdata 中提供的 Zstandard 帧进行解压缩,返回解压后的数据。可以选择提供与压缩时相同的预设 dictionary(字典)。

zstd_length 返回给定 Zstandard 帧的解压后长度。如果 ZSTD_getFrameContentSize() 可用,当长度未知时返回 NULL。如果该函数不可用,则无法区分错误、未知解压长度和零解压长度这几种情况。

示例

基本的压缩/解压缩示例:

CREATE EXTENSION zstd;

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

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

指定压缩级别(1 为最快速度,22 为最高压缩率,默认为 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

最后修改 2026-03-08: add extension catalog (baacba6)