online_advisor

在线建议缺失索引、扩展统计信息与预备语句

概览

扩展包名版本分类许可证语言
online_advisor1.0ADMINPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
5270online_advisor-
相关扩展index_advisor hypopg pg_qualstats

Requires shared_preload_libraries=online_advisor on PostgreSQL 14-16; PGSTY backports upstream PG18 hook compatibility.

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.01817161514online_advisor-
RPMPIGSTY1.01817161514online_advisor_$v-
DEBPIGSTY1.01817161514postgresql-$v-online-advisor-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el8.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el9.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el9.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el10.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
el10.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d12.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d12.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d13.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
d13.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u22.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u24.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u26.x86_64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
u26.aarch64
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0
PIGSTY 1.0

构建

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

pig build pkg online_advisor         # 构建 RPM / DEB 包

安装

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

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

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

pig install online_advisor;          # 当前活跃 PG 版本安装
pig ext install -y online_advisor -v 18  # PG 18
pig ext install -y online_advisor -v 17  # PG 17
pig ext install -y online_advisor -v 16  # PG 16
pig ext install -y online_advisor -v 15  # PG 15
pig ext install -y online_advisor -v 14  # PG 14
dnf install -y online_advisor_18       # PG 18
dnf install -y online_advisor_17       # PG 17
dnf install -y online_advisor_16       # PG 16
dnf install -y online_advisor_15       # PG 15
dnf install -y online_advisor_14       # PG 14
apt install -y postgresql-18-online-advisor   # PG 18
apt install -y postgresql-17-online-advisor   # PG 17
apt install -y postgresql-16-online-advisor   # PG 16
apt install -y postgresql-15-online-advisor   # PG 15
apt install -y postgresql-14-online-advisor   # PG 14

预加载配置

shared_preload_libraries = 'online_advisor';

创建扩展

CREATE EXTENSION online_advisor;

用法

来源:

online_advisor 观察PostgreSQL执行计划和工作负载时间,然后推荐索引、扩展统计信息或准备语句。它仅报告候选方案;从不自动创建索引或统计对象。

核心流程

预加载库并重启PostgreSQL:

shared_preload_libraries = 'online_advisor'

在每个需要观察工作负载的数据库中创建并激活扩展:

CREATE EXTENSION online_advisor;

-- Calling an extension function activates collection in this database.
SELECT get_executor_stats();

运行代表性的工作负载后,检查推荐方案:

SELECT * FROM proposed_indexes;
SELECT * FROM proposed_statistics;
SELECT * FROM get_executor_stats();

-- Keep separate index candidates instead of combining compatible clauses.
SELECT * FROM propose_indexes(combine => false);

在应用生成的 create_indexcreate_statistics 语句之前,请审查每个建议。在创建索引或统计对象之后运行 ANALYZE,以便规划器可以使用当前统计数据。

对象和设置

  • proposed_indexes: propose_indexes(combine, reset) 的视图,并带有过滤体积、调用次数、耗时以及候选的 CREATE INDEX 语句。
  • proposed_statistics: propose_statistics(combine, reset) 的视图,并带有误估数、调用次数、耗时以及候选的 CREATE STATISTICS 语句。
  • get_executor_stats(reset): 返回聚合规划和执行时间、查询计数及规划开销比率。
  • online_advisor.filtered_threshold: 考虑为索引建议的过滤行数最小值,默认值为 1000
  • online_advisor.misestimation_threshold: 实际到估计行数比值,用于统计;默认值为 10
  • online_advisor.min_rows: 误估分析中返回的最小行数,默认值为 1000
  • online_advisor.max_index_proposalsonline_advisor.max_stat_proposals: 建议容量;在激活扩展之前设置它们。
  • online_advisor.do_instrumentation, online_advisor.log_duration, 和 online_advisor.prepare_threshold: 控制收集和准备语句建议。

注意事项

  • 仪器化会增加工作负载开销,请在目标系统上进行测量,并在不需要时禁用数据收集。
  • 索引启发式算法不考虑复合索引、连接索引或仅用于避免排序的索引的操作符顺序问题。
  • 扩展不会估计建议索引的好处。在构建昂贵的索引之前,使用计划审查或假设性索引工具进行评估。
  • 建议是数据库本地的,并依赖于激活或重置以来观察到的工作负载。