pgelog

通过伪自治事务实现扩展日志记录

概览

扩展包名版本分类许可证语言
pgelog1.0.2ADMINPostgreSQLSQL
ID扩展名BinLibLoadCreateTrustReloc模式
5870pgelog-
相关扩展dblink pg_variables table_log pgaudit logerrors dblink

Release tag 1.0.2 still ships extension SQL version 1.0; requires the dblink extension at runtime in addition to pg_variables.

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.0.21817161514pgelogdblink, pg_variables
RPMPIGSTY1.0.21817161514pgelog_$vpostgresql$v-contrib, pg_variables_$v
DEBPIGSTY1.0.21817161514postgresql-$v-pgelogpostgresql-$v-pg-variables
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
u22.x86_64
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
u22.aarch64
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
u24.x86_64
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
u24.aarch64
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
PIGSTY 1.0.2
u26.x86_64
u26.aarch64

构建

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

pig build pkg pgelog         # 构建 RPM / DEB 包

安装

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

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

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

pig install pgelog;          # 当前活跃 PG 版本安装
pig ext install -y pgelog -v 18  # PG 18
pig ext install -y pgelog -v 17  # PG 17
pig ext install -y pgelog -v 16  # PG 16
pig ext install -y pgelog -v 15  # PG 15
pig ext install -y pgelog -v 14  # PG 14
dnf install -y pgelog_18       # PG 18
dnf install -y pgelog_17       # PG 17
dnf install -y pgelog_16       # PG 16
dnf install -y pgelog_15       # PG 15
dnf install -y pgelog_14       # PG 14
apt install -y postgresql-18-pgelog   # PG 18
apt install -y postgresql-17-pgelog   # PG 17
apt install -y postgresql-16-pgelog   # PG 16
apt install -y postgresql-15-pgelog   # PG 15
apt install -y postgresql-14-pgelog   # PG 14

创建扩展

CREATE EXTENSION pgelog CASCADE;  -- 依赖: dblink, pg_variables

用法

来源:READMEcontrol fileextension SQL 1.0.2Tag v1.0.2

pgelog 通过使用 dblink 实现的 pseudo-autonomous transactions 写入可抵抗 rollback 的日志行。它会在 pg_variables 中缓存额外连接,因此同一 session 内重复写日志的成本更低。

依赖与安装

  • PostgreSQL 11+
  • dblink
  • pg_variables
  • 无密码本地 dblink 访问,通常通过 peer
CREATE EXTENSION IF NOT EXISTS dblink;
CREATE EXTENSION IF NOT EXISTS pg_variables;
CREATE EXTENSION pgelog;

主要对象与函数

SELECT pgelog_to_log('SQL', 'standalone', 'Test of logging by pgelog', '1');

SELECT pgelog_get_param('pgelog_ttl_minutes');
SELECT pgelog_set_param('pgelog_ttl_minutes', '2880');
  • pgelog_logs:基础日志表。
  • pgelog_vw_logs:带时间差的日志视图。
  • pgelog_params:配置表。
  • pgelog_to_log(...):写入一条不会随 rollback 消失的日志。
  • pgelog_get_param(...)pgelog_set_param(...)pgelog_delete_param(...):管理扩展设置。

典型用法

官方 README 展示了在 PL/pgSQL exception handler 中使用 pgelog_to_log(...):先用 GET STACKED DIAGNOSTICS 收集诊断信息,再写入一条 FAIL 日志,最后重新抛出错误。

注意事项

  • 每个 session 最多会额外打开一个 dblink 连接,因此 max_connections 需要把这一点算进去。
  • 上游 v1.0.2 仍然使用同一组用户可见对象;Pigsty 提到的运行时 dblinkpg_variables 依赖,已被 control file 与 README 共同确认。

最后修改 2026-05-01: update extension data (e399d22)