pg_wait_sampling

基于采样的等待事件统计

概览

扩展包名版本分类许可证语言
pg_wait_sampling1.1.9STATPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
6280pg_wait_sampling-
相关扩展powa pg_stat_statements pg_background pg_stat_kcache system_stats pgnodemx pg_profile pgsentinel

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG1.1.91817161514pg_wait_sampling-
RPMPGDG1.1.91817161514pg_wait_sampling_$v-
DEBPGDG1.1.91817161514postgresql-$v-pg-wait-sampling-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
PGDG 1.1.9
d12.x86_64
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
d12.aarch64
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
d13.x86_64
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
d13.aarch64
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
u22.x86_64
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
u22.aarch64
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
u24.x86_64
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
u24.aarch64
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9
PGDG 1.1.9

安装

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

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

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

pig install pg_wait_sampling;          # 当前活跃 PG 版本安装
pig ext install -y pg_wait_sampling -v 18  # PG 18
pig ext install -y pg_wait_sampling -v 17  # PG 17
pig ext install -y pg_wait_sampling -v 16  # PG 16
pig ext install -y pg_wait_sampling -v 15  # PG 15
pig ext install -y pg_wait_sampling -v 14  # PG 14
dnf install -y pg_wait_sampling_18       # PG 18
dnf install -y pg_wait_sampling_17       # PG 17
dnf install -y pg_wait_sampling_16       # PG 16
dnf install -y pg_wait_sampling_15       # PG 15
dnf install -y pg_wait_sampling_14       # PG 14
apt install -y postgresql-18-pg-wait-sampling   # PG 18
apt install -y postgresql-17-pg-wait-sampling   # PG 17
apt install -y postgresql-16-pg-wait-sampling   # PG 16
apt install -y postgresql-15-pg-wait-sampling   # PG 15
apt install -y postgresql-14-pg-wait-sampling   # PG 14

预加载配置

shared_preload_libraries = 'pg_wait_sampling';

创建扩展

CREATE EXTENSION pg_wait_sampling;

用法

pg_wait_sampling: 基于采样的等待事件统计

pg_wait_sampling 通过定期采样每个后端正在等待的事件来收集等待事件统计。它提供近期历史环形缓冲区和累积统计两种视图。

视图

当前等待事件:

SELECT * FROM pg_wait_sampling_current;
列名类型描述
pidint4进程 ID
event_typetext等待事件类型
eventtext等待事件名称
queryidint8查询 ID

等待事件历史(近期采样的环形缓冲区):

SELECT * FROM pg_wait_sampling_history;
列名类型描述
pidint4进程 ID
tstimestamptz采样时间戳
event_typetext等待事件类型
eventtext等待事件名称
queryidint8查询 ID

等待事件剖析(累积计数):

SELECT * FROM pg_wait_sampling_profile;
列名类型描述
pidint4进程 ID
event_typetext等待事件类型
eventtext等待事件名称
queryidint8查询 ID
counttext采样计数

获取特定进程的当前等待事件:

SELECT * FROM pg_wait_sampling_get_current(12345);

重置剖析

SELECT pg_wait_sampling_reset_profile();

配置

参数默认值描述
pg_wait_sampling.history_size5000环形缓冲区大小
pg_wait_sampling.history_period10历史采样周期(毫秒)
pg_wait_sampling.profile_period10剖析采样周期(毫秒)
pg_wait_sampling.profile_pidtrue按进程收集剖析
pg_wait_sampling.profile_queriestop按查询剖析:nonetopall
pg_wait_sampling.sample_cputrue采样 CPU 上的后端(事件列为 NULL)

示例:热点等待事件

SELECT event_type, event, count
FROM pg_wait_sampling_profile
ORDER BY count DESC
LIMIT 10;

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