plprofiler

剖析 PL/pgSQL 函数

概览

扩展包名版本分类许可证语言
plprofiler4.2.5LANGArtisticC
ID扩展名BinLibLoadCreateTrustReloc模式
3070plprofiler-
相关扩展pldbgapi plpgsql_check plpgsql pgtap pg_profile pg_stat_statements pg_store_plans auto_explain

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG4.2.51817161514plprofiler-
RPMPGDG4.2.51817161514plprofiler_$v-
DEBPGDG4.2.51817161514postgresql-$v-plprofiler-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
d12.aarch64
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
d13.x86_64
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
d13.aarch64
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
u22.x86_64
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
u22.aarch64
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
u24.x86_64
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
u24.aarch64
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5
PGDG 4.2.5

安装

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

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

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

pig install plprofiler;          # 当前活跃 PG 版本安装
pig ext install -y plprofiler -v 18  # PG 18
pig ext install -y plprofiler -v 17  # PG 17
pig ext install -y plprofiler -v 16  # PG 16
pig ext install -y plprofiler -v 15  # PG 15
pig ext install -y plprofiler -v 14  # PG 14
dnf install -y plprofiler_18       # PG 18
dnf install -y plprofiler_17       # PG 17
dnf install -y plprofiler_16       # PG 16
dnf install -y plprofiler_15       # PG 15
dnf install -y plprofiler_14       # PG 14
apt install -y postgresql-18-plprofiler   # PG 18
apt install -y postgresql-17-plprofiler   # PG 17
apt install -y postgresql-16-plprofiler   # PG 16
apt install -y postgresql-15-plprofiler   # PG 15
apt install -y postgresql-14-plprofiler   # PG 14

预加载配置

shared_preload_libraries = 'plprofiler';

创建扩展

CREATE EXTENSION plprofiler;

用法

plprofiler: 用于 PL/pgSQL 函数性能分析的服务端支持

plprofiler 是一个 PL/pgSQL 函数性能分析工具,能够识别性能瓶颈并生成交互式火焰图报告。

CREATE EXTENSION plprofiler;

配置

postgresql.conf 中添加:

shared_preload_libraries = 'plprofiler'

命令行工具

plprofiler 命令行工具用于控制性能分析并生成报告:

# 分析特定 SQL 命令
plprofiler run -d mydb --command "SELECT my_function()" --output report.html

# 实时监控性能分析
plprofiler monitor -d mydb

# 保存性能分析数据以便后续分析
plprofiler save -d mydb --name "my_profile"

# 生成包含火焰图的 HTML 报告
plprofiler report -d mydb --from-data "my_profile" --output report.html

# 列出已保存的性能分析数据集
plprofiler list -d mydb

# 重置性能分析数据
plprofiler reset-data -d mydb

# 导出/导入性能分析数据
plprofiler export -d mydb --from-data "my_profile" > profile.json
plprofiler import -d mydb --into-data "imported" < profile.json

SQL 接口

-- 为当前会话启用性能分析
SELECT pl_profiler_set_enabled_local(true);

-- 执行待分析的函数
SELECT my_function();

-- 将性能分析数据收集到共享哈希表中
SELECT pl_profiler_collect_data();

-- 禁用性能分析
SELECT pl_profiler_set_enabled_local(false);

-- 全局启用性能分析(适用于所有会话)
SELECT pl_profiler_set_enabled_global(true);

-- 重置本地/共享性能分析数据
SELECT pl_profiler_reset_local();
SELECT pl_profiler_reset_shared();

报告输出

生成的 HTML 报告包括:

  • 交互式火焰图:展示 PL/pgSQL 代码中花费的时钟时间
  • 每函数统计信息:包含自身时间(总时间减去子函数时间)
  • 排名靠前的函数:按耗时排序(默认:前 10 个)
  • 自包含的 HTML,无需外部依赖

分析方法

  • 直接分析:在收集数据时运行特定 SQL
  • 定时采集:基于时间间隔的统计收集
  • 按用户分析:通过 ALTER USER 为特定数据库用户启用性能分析
  • 生产监控:在生产系统上进行低开销的性能分析

最后修改 2026-03-14: update extension metadata (953cbd0)