pg_column_tetris

强制采用最优列对齐顺序,以减少 PostgreSQL 行数据中的填充浪费。

概览

扩展包名版本分类许可证语言
pg_column_tetris0.1.0ADMINMITSQL
ID扩展名BinLibLoadCreateTrustReloc模式
5280pg_column_tetriscolumn_tetris
相关扩展plpgsql pg_repack pgstattuple pg_squeeze pg_rewrite pg_dirtyread

Upstream has no release or tag; source archive is normalized from commit e70f9867c63e932cdaf87b2d34b6504adad9ce12.

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY0.1.01817161514pg_column_tetrisplpgsql
RPMPIGSTY0.1.01817161514pg_column_tetris_$v-
DEBPIGSTY0.1.01817161514postgresql-$v-pg-column-tetris-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
el8.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
el9.x86_64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
el9.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
el10.x86_64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
el10.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
d12.x86_64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
d12.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
d13.x86_64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
d13.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
u22.x86_64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
u22.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
u24.x86_64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
u24.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
u26.x86_64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
u26.aarch64
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0
PIGSTY 0.1.0

构建

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

pig build pkg pg_column_tetris         # 构建 RPM / DEB 包

安装

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

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

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

pig install pg_column_tetris;          # 当前活跃 PG 版本安装
pig ext install -y pg_column_tetris -v 18  # PG 18
pig ext install -y pg_column_tetris -v 17  # PG 17
pig ext install -y pg_column_tetris -v 16  # PG 16
pig ext install -y pg_column_tetris -v 15  # PG 15
pig ext install -y pg_column_tetris -v 14  # PG 14
dnf install -y pg_column_tetris_18       # PG 18
dnf install -y pg_column_tetris_17       # PG 17
dnf install -y pg_column_tetris_16       # PG 16
dnf install -y pg_column_tetris_15       # PG 15
dnf install -y pg_column_tetris_14       # PG 14
apt install -y postgresql-18-pg-column-tetris   # PG 18
apt install -y postgresql-17-pg-column-tetris   # PG 17
apt install -y postgresql-16-pg-column-tetris   # PG 16
apt install -y postgresql-15-pg-column-tetris   # PG 15
apt install -y postgresql-14-pg-column-tetris   # PG 14

创建扩展

CREATE EXTENSION pg_column_tetris CASCADE;  -- 依赖: plpgsql

用法

来源:

pg_column_tetris 0.1.0 是适用于 PostgreSQL 14 至 18 的纯 SQL 扩展。它通过事件触发器在 CREATE TABLE 后估算对齐填充,并可对低效列顺序发出警告或拒绝建表;还提供检查和重写建议函数。

检查并选择执行模式

默认模式为 warnstrict 会拒绝估算结果不佳的新表,off 则禁用事件触发器检查。

CREATE EXTENSION pg_column_tetris;

SELECT column_tetris.mode();
SELECT * FROM column_tetris.check('public.measurement'::regclass);
SELECT column_tetris.padding_wasted('public.measurement'::regclass);

SELECT column_tetris.set_mode('warn');

对不应检查的表使用 column_tetris.exclude()。临时表和系统表会被跳过,事件触发器检查建表操作,而不是每一次后续修改。

将估算与重写视为建议

估算器会模拟元组头和类型对齐,但无法完整预测 null 位图、变长或 TOAST 值、压缩以及工作负载特定行分布的实际存储。因此,报告的字节节省是设计信号,不是实测磁盘回收量。

column_tetris.suggest_rewrite() 返回迁移脚本;它不会保留每个外键、索引、触发器或默认值。生成的流程会重命名原表、创建并复制替代表,最后删除旧表,可能需要排他锁和停机。审查依赖对象、权限、identity 与序列行为、复制、回退并在真实的预演环境测试之前,绝不能执行该输出。列顺序还可能属于应用契约,例如按位置插入和行解码。


最后修改:2026-07-30: extension update 2026-07-30 (7373242)