pg_accumulator

PostgreSQL 中用于余额与周转跟踪的累积寄存器

概览

扩展包名版本分类许可证语言
pg_accumulator1.1.3FUNCPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
4845pg_accumulatoraccum
相关扩展plpgsql financial topn quantile first_last_agg

版本

类型仓库版本PG 大版本包名依赖
EXTPIGSTY1.1.31817161514pg_accumulatorplpgsql
RPMPIGSTY1.1.31817161514pg_accumulator_$v-
DEBPIGSTY1.1.31817161514postgresql-$v-pg-accumulator-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
el8.aarch64
el9.x86_64
el9.aarch64
el10.x86_64
el10.aarch64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
d12.x86_64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
d12.aarch64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
d13.x86_64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
d13.aarch64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
u22.x86_64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
u22.aarch64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
u24.x86_64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
u24.aarch64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
u26.x86_64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
u26.aarch64
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3
PIGSTY 1.1.3

构建

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

pig build pkg pg_accumulator         # 构建 RPM / DEB 包

安装

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

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

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

pig install pg_accumulator;          # 当前活跃 PG 版本安装
pig ext install -y pg_accumulator -v 18  # PG 18
pig ext install -y pg_accumulator -v 17  # PG 17
pig ext install -y pg_accumulator -v 16  # PG 16
pig ext install -y pg_accumulator -v 15  # PG 15
pig ext install -y pg_accumulator -v 14  # PG 14
dnf install -y pg_accumulator_18       # PG 18
dnf install -y pg_accumulator_17       # PG 17
dnf install -y pg_accumulator_16       # PG 16
dnf install -y pg_accumulator_15       # PG 15
dnf install -y pg_accumulator_14       # PG 14
apt install -y postgresql-18-pg-accumulator   # PG 18
apt install -y postgresql-17-pg-accumulator   # PG 17
apt install -y postgresql-16-pg-accumulator   # PG 16
apt install -y postgresql-15-pg-accumulator   # PG 15
apt install -y postgresql-14-pg-accumulator   # PG 14

创建扩展

CREATE EXTENSION pg_accumulator CASCADE;  -- 依赖: plpgsql

用法

来源:READMEdocumentationcontrol file

pg_accumulator 为 PostgreSQL 提供声明式累积寄存器。寄存器按任意维度和资源记录业务流水,并生成余额、发生额、流水历史、校验和重建等查询与维护函数。

启用

CREATE EXTENSION pg_accumulator;

如果高写入场景需要 delta buffer 和后台 worker,需要预加载共享库并重启 PostgreSQL:

shared_preload_libraries = 'pg_accumulator'

创建寄存器

SELECT accum.register_create(
  name := 'inventory',
  dimensions := '{"warehouse": "int", "product": "int"}',
  resources := '{"quantity": "numeric", "amount": "numeric"}',
  kind := 'balance'
);

一次调用会创建寄存器元数据、流水表、汇总表、余额缓存、触发器、生成查询函数和配套索引。

记账与查询

SELECT accum.register_post('inventory', '{
  "period": "2026-04-01",
  "document": "receipt:1",
  "dimensions": {"warehouse": 1, "product": 42},
  "resources": {"quantity": 10, "amount": 250}
}');

SELECT * FROM accum.inventory_balance(
  dimensions := '{"warehouse": 1, "product": 42}'
);

SELECT * FROM accum.inventory_turnover(
  from_date := '2026-04-01',
  to_date := '2026-04-30',
  dimensions := '{"warehouse": 1}',
  group_by := '["product"]'
);

更正与维护

使用 register_unpostregister_repost 处理追溯更正。必要时使用 register_verifyregister_rebuild_totalsregister_rebuild_cache 校验和修复派生状态。

SELECT accum.register_unpost('inventory', 'receipt:1');
SELECT accum.register_verify('inventory');

配置

文档中的 GUC 包括 pg_accumulator.background_workerspg_accumulator.delta_merge_intervalpg_accumulator.delta_merge_delaypg_accumulator.delta_merge_batch_sizepg_accumulator.partitions_aheadpg_accumulator.maintenance_interval


最后修改 2026-07-02: extension update 2026-07-02 (d4da20c)