pg_disorder

扰动无 ORDER BY 查询的行序以暴露依赖隐式顺序的测试

概览

扩展包名版本分类许可证语言
pg_disorder0.1.0FEATPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
2880pg_disorder-
相关扩展plan_filter pg_hint_plan pg_mockable pgtap pg_simula pg_fiu pg_crash

Headless loadable module with no control file and no CREATE EXTENSION step; intended only for test databases; load per session with session_preload_libraries and never enable globally in production.

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY0.1.01817161514pg_disorder-
RPMPIGSTY0.1.01817161514pg_disorder_$v-
DEBPIGSTY0.1.01817161514postgresql-$v-pg-disorder-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
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
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_disorder 扩展的 RPM / DEB 包:

pig build pkg pg_disorder         # 构建 RPM / DEB 包

安装

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

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

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

pig install pg_disorder;          # 当前活跃 PG 版本安装
pig ext install -y pg_disorder -v 18  # PG 18
pig ext install -y pg_disorder -v 17  # PG 17
pig ext install -y pg_disorder -v 16  # PG 16
pig ext install -y pg_disorder -v 15  # PG 15
pig ext install -y pg_disorder -v 14  # PG 14
dnf install -y pg_disorder_18       # PG 18
dnf install -y pg_disorder_17       # PG 17
dnf install -y pg_disorder_16       # PG 16
dnf install -y pg_disorder_15       # PG 15
dnf install -y pg_disorder_14       # PG 14
apt install -y postgresql-18-pg-disorder   # PG 18
apt install -y postgresql-17-pg-disorder   # PG 17
apt install -y postgresql-16-pg-disorder   # PG 16
apt install -y postgresql-15-pg-disorder   # PG 15
apt install -y postgresql-14-pg-disorder   # PG 14

预加载配置

shared_preload_libraries = 'pg_disorder';

用法

来源:

pg_disorder 是一个仅用于测试的 PostgreSQL 可加载模块,它会有意改变符合条件的 SELECT 查询输出顺序,用于发现无意中依赖未指定行顺序的应用和测试。它是一个无扩展对象的模块:没有控制文件、SQL 安装脚本,也不需要执行 CREATE EXTENSION pg_disorder

为测试数据库启用

在会话启动时加载该模块,以便其规划器钩子可用:

ALTER DATABASE regression_db
  SET session_preload_libraries = 'pg_disorder';

ALTER DATABASE regression_db
  SET pg_disorder.mode = 'reverse';

修改 session_preload_libraries 后应重新连接。不要将此模块加入生产环境全局的 shared_preload_libraries 设置。

模式

SET pg_disorder.mode = 'off';
SET pg_disorder.mode = 'reverse';
SET pg_disorder.mode = 'shuffle';
SET pg_disorder.seed = 42;
SET pg_disorder.force_serial = on;
  • off 不改变执行计划。
  • reverse 以确定性方式反转符合条件的输出。
  • shuffle 在会话种子、提交的查询文本和执行计划固定时产生确定性排列。使用默认种子零时,每个会话会先选择并记录一个随机种子。
  • force_serial 禁止并行计划,使乱序测试能够复现。

修复失败查询时,应添加语义正确的 ORDER BY;不要编码在 off 模式下偶然观察到的顺序。

适用条件与注意事项

该钩子面向没有 ORDER BY 的顶层 SELECT 语句。它会有意跳过那些重新排序不安全或会改变 SQL 语义的查询形态,包括聚合、分组、DISTINCT、集合操作、窗口函数、递归查询、行锁,以及没有 FROM 关系的查询。

  • pg_disorder 是故障注入工具,而不是生产查询功能。
  • 乱序测试通过并不能证明每个无序查询都安全;被排除的查询形态和规划器路径不会被重写。
  • 软件包仅安装服务器模块。应通过 GUC 或模块加载状态验证是否启用,而不是查看 pg_extension

最后修改:2026-08-09: update extension count (30409e7)