storage_engine
概览
| 扩展包名 | 版本 | 分类 | 许可证 | 语言 |
|---|---|---|---|---|
storage_engine | 1.3.4 | OLAP | AGPL-3.0 | C |
| ID | 扩展名 | Bin | Lib | Load | Create | Trust | Reloc | 模式 |
|---|---|---|---|---|---|---|---|---|
| 2450 | storage_engine | 否 | 是 | 是 | 是 | 否 | 否 | engine |
release 1.2.3; SQL v1.2.1
版本
| 类型 | 仓库 | 版本 | PG 大版本 | 包名 | 依赖 |
|---|---|---|---|---|---|
| EXT | PIGSTY | 1.3.4 | 1817161514 | storage_engine | - |
| RPM | PIGSTY | 1.3.4 | 1817161514 | storage_engine_$v | - |
| DEB | PIGSTY | 1.3.4 | 1817161514 | postgresql-$v-storage-engine | - |
构建
您可以使用 pig build 命令构建 storage_engine 扩展的 RPM / DEB 包:
pig build pkg storage_engine # 构建 RPM / DEB 包
安装
您可以直接安装 storage_engine 扩展包的预置二进制包,首先确保 PGDG 和 PIGSTY 仓库已经添加并启用:
pig repo add pgsql -u # 添加仓库并更新缓存
使用 pig 或者是 apt/yum/dnf 安装扩展:
pig install storage_engine; # 当前活跃 PG 版本安装
pig ext install -y storage_engine -v 18 # PG 18
pig ext install -y storage_engine -v 17 # PG 17
pig ext install -y storage_engine -v 16 # PG 16
pig ext install -y storage_engine -v 15 # PG 15
pig ext install -y storage_engine -v 14 # PG 14
dnf install -y storage_engine_18 # PG 18
dnf install -y storage_engine_17 # PG 17
dnf install -y storage_engine_16 # PG 16
dnf install -y storage_engine_15 # PG 15
dnf install -y storage_engine_14 # PG 14
apt install -y postgresql-18-storage-engine # PG 18
apt install -y postgresql-17-storage-engine # PG 17
apt install -y postgresql-16-storage-engine # PG 16
apt install -y postgresql-15-storage-engine # PG 15
apt install -y postgresql-14-storage-engine # PG 14
预加载配置:
shared_preload_libraries = 'storage_engine';
创建扩展:
CREATE EXTENSION storage_engine;
用法
来源:README,release 1.3.4,PGXN 1.3.4,PGXN changelog,META.json
storage_engine 在 engine schema 中提供两种 PostgreSQL table access method:
colcompress:面向列式压缩存储,支持 vectorized execution、min/max pruning 与 parallel scan。rowcompress:面向行批压缩,支持 parallel scan。
CREATE EXTENSION storage_engine;
快速开始
使用任一 access method 建表:
CREATE TABLE events (
ts timestamptz NOT NULL,
user_id bigint,
event_type text,
value float8
) USING colcompress;
CREATE TABLE logs (
id bigserial,
logged_at timestamptz NOT NULL,
message text
) USING rowcompress;
主要调优旋钮
上游文档列出的 session 级 GUC 包括:
storage_engine.enable_parallel_executionstorage_engine.min_parallel_processesstorage_engine.enable_vectorizationstorage_engine.enable_custom_scanstorage_engine.enable_column_cachestorage_engine.enable_columnar_index_scanstorage_engine.enable_dmlstorage_engine.stripe_row_limitstorage_engine.chunk_group_row_limitstorage_engine.compression_level
README 说明这些 GUC 会在库被加载后可见;如果希望每个 session 一开始就可用,可把 storage_engine 加入 shared_preload_libraries。
类型与操作符
engine.uint8 为需要完整 0 到 2^64 - 1 范围的 colcompress 工作负载存储无符号 64 位值。上游文档列出了 comparison operators(=、<>、<、<=、>、>=)、B-tree 与 hash opclasses、与 bigint、numeric、text 的双向 casts,以及 engine.min、engine.max、engine.sum aggregates。
常用管理函数
用于 colcompress 表:
SELECT engine.alter_colcompress_table_set(
'events'::regclass,
orderby => 'ts ASC, user_id ASC',
compression => 'zstd',
compression_level => 9
);
SELECT engine.colcompress_merge('events');
CALL engine.colcompress_repack('events');
CALL engine.colcompress_repack('events', 0.7);
在 1.3.4 中,engine.colcompress_repack(table_name regclass, min_fill_ratio float8 DEFAULT 0.9) 是用于 colcompress 表在线逐 stripe defragmentation 的 procedure。它会 repack live-row ratio 低于阈值的 stripes。需要按 orderby key 做旧式 full-table rewrite 和全局排序时,使用 engine.colcompress_merge()。
用于 rowcompress 表:
SELECT engine.alter_rowcompress_table_set(
'logs'::regclass,
batch_size => 10000,
compression => 'zstd',
compression_level => 5
);
SELECT engine.rowcompress_repack('logs');
何时选择哪种 AM
colcompress适合分析扫描、聚合与范围谓词,这类场景能受益于列裁剪、vectorization 以及 stripe/chunk pruning。rowcompress适合追加型日志或通常会整行一起读取的宽表,此时压缩收益往往高于列投影。- 对
colcompress的点查,上游建议启用storage_engine.enable_columnar_index_scan或 per-tableindex_scan。
注意事项
- 使用
ALTER EXTENSION storage_engine UPDATE TO '1.3.4';升级已有安装。 - 旧的
FUNCTION engine.colcompress_repack(regclass)alias 在 1.3.4 中已移除。已有调用方应改用CALL engine.colcompress_repack('table')做 stripe defragmentation,或用SELECT engine.colcompress_merge('table')执行旧的 full rewrite 行为。 colcompress与rowcompress都不支持 foreign key 或AFTER ROWtriggers。- 不支持
VACUUM FULL,也不支持CREATE UNLOGGED TABLE ... USING colcompress;上游建议改用扩展自带的 repack 函数。 - 在
colcompress上,orderby与 B-tree index 组合可能禁用 sort-on-write 路径,而 ordered columns 上的 B-tree indexes 可能让 range query 失去 stripe pruning 收益。全局顺序重要时,请在装载数据后运行engine.colcompress_merge();分析型表优先使用index_scan => false。