pg_partman

用于按时间或 ID 管理分区表的扩展

概览

扩展包名版本分类许可证语言
pg_partman5.4.3OLAPPostgreSQLC
ID扩展名BinLibLoadCreateTrustReloc模式
2510pg_partman-
相关扩展plpgsql citus pg_fkpart timescaledb periods emaj pg_cron plproxy temporal_tables
下游依赖timeseries

版本

类型仓库版本PG 大版本包名依赖
EXTPGDG5.4.31817161514pg_partmanplpgsql
RPMPGDG5.4.31817161514pg_partman_$v-
DEBPGDG5.4.31817161514postgresql-$v-partman-
OS / PGPG18PG17PG16PG15PG14
el8.x86_64
PGDG 5.4.3
el8.x86_64.pg14 : pg_partman_14
pg_partman_14-5.4.3-1PGDG.rhel8.10.x86_64.rpm PGDG · 5.4.3 · 279.6KiB pg_partman_14-5.4.2-1PGDG.rhel8.10.x86_64.rpm PGDG · 5.4.2 · 279.0KiB pg_partman_14-5.4.1-1PGDG.rhel8.10.x86_64.rpm PGDG · 5.4.1 · 278.6KiB pg_partman_14-5.4.0-1PGDG.rhel8.10.x86_64.rpm PGDG · 5.4.0 · 275.3KiB pg_partman_14-5.3.1-1PGDG.rhel8.x86_64.rpm PGDG · 5.3.1 · 271.3KiB pg_partman_14-5.3.0-1PGDG.rhel8.x86_64.rpm PGDG · 5.3.0 · 270.4KiB pg_partman_14-5.2.4-1PGDG.rhel8.x86_64.rpm PGDG · 5.2.4 · 261.4KiB pg_partman_14-5.2.3-1PGDG.rhel8.x86_64.rpm PGDG · 5.2.3 · 260.9KiB pg_partman_14-5.2.2-1PGDG.rhel8.x86_64.rpm PGDG · 5.2.2 · 260.1KiB pg_partman_14-5.2.1-1PGDG.rhel8.x86_64.rpm PGDG · 5.2.1 · 259.6KiB pg_partman_14-5.2.0-1PGDG.rhel8.x86_64.rpm PGDG · 5.2.0 · 259.3KiB pg_partman_14-5.1.0-1PGDG.rhel8.x86_64.rpm PGDG · 5.1.0 · 254.7KiB pg_partman_14-5.0.1-1PGDG.rhel8.x86_64.rpm PGDG · 5.0.1 · 249.4KiB pg_partman_14-5.0.0-1PGDG.rhel8.x86_64.rpm PGDG · 5.0.0 · 248.4KiB pg_partman_14-4.7.4-1PGDG.rhel8.x86_64.rpm PGDG · 4.7.4 · 246.9KiB pg_partman_14-4.7.3-3.rhel8.x86_64.rpm PGDG · 4.7.3 · 246.5KiB pg_partman_14-4.7.3-1.rhel8.x86_64.rpm PGDG · 4.7.3 · 246.2KiB pg_partman_14-4.7.2-1.rhel8.x86_64.rpm PGDG · 4.7.2 · 245.7KiB pg_partman_14-4.7.1-1.rhel8.x86_64.rpm PGDG · 4.7.1 · 260.6KiB pg_partman_14-4.7.0-1.rhel8.x86_64.rpm PGDG · 4.7.0 · 259.9KiB pg_partman_14-4.6.2-1.rhel8.x86_64.rpm PGDG · 4.6.2 · 256.2KiB pg_partman_14-4.6.1-1.rhel8.x86_64.rpm PGDG · 4.6.1 · 255.7KiB pg_partman_14-4.6.0-1.rhel8.x86_64.rpm PGDG · 4.6.0 · 252.2KiB pg_partman_14-4.5.1-2.rhel8.x86_64.rpm PGDG · 4.5.1 · 246.5KiB
el8.aarch64
el9.x86_64
PGDG 5.4.3
el9.x86_64.pg14 : pg_partman_14
pg_partman_14-5.4.3-1PGDG.rhel9.7.x86_64.rpm PGDG · 5.4.3 · 218.5KiB pg_partman_14-5.4.2-1PGDG.rhel9.7.x86_64.rpm PGDG · 5.4.2 · 218.0KiB pg_partman_14-5.4.1-1PGDG.rhel9.7.x86_64.rpm PGDG · 5.4.1 · 217.8KiB pg_partman_14-5.4.0-1PGDG.rhel9.7.x86_64.rpm PGDG · 5.4.0 · 216.1KiB pg_partman_14-5.3.1-1PGDG.rhel9.x86_64.rpm PGDG · 5.3.1 · 213.7KiB pg_partman_14-5.3.0-1PGDG.rhel9.x86_64.rpm PGDG · 5.3.0 · 212.9KiB pg_partman_14-5.2.4-1PGDG.rhel9.x86_64.rpm PGDG · 5.2.4 · 207.3KiB pg_partman_14-5.2.3-1PGDG.rhel9.x86_64.rpm PGDG · 5.2.3 · 206.9KiB pg_partman_14-5.2.2-1PGDG.rhel9.x86_64.rpm PGDG · 5.2.2 · 206.2KiB pg_partman_14-5.2.1-1PGDG.rhel9.x86_64.rpm PGDG · 5.2.1 · 205.9KiB pg_partman_14-5.2.0-1PGDG.rhel9.x86_64.rpm PGDG · 5.2.0 · 205.6KiB pg_partman_14-5.1.0-1PGDG.rhel9.x86_64.rpm PGDG · 5.1.0 · 201.7KiB pg_partman_14-5.0.1-1PGDG.rhel9.x86_64.rpm PGDG · 5.0.1 · 197.9KiB pg_partman_14-5.0.0-1PGDG.rhel9.x86_64.rpm PGDG · 5.0.0 · 197.2KiB pg_partman_14-4.7.4-1PGDG.rhel9.x86_64.rpm PGDG · 4.7.4 · 198.7KiB pg_partman_14-4.7.3-3.rhel9.x86_64.rpm PGDG · 4.7.3 · 198.5KiB pg_partman_14-4.7.3-1.rhel9.x86_64.rpm PGDG · 4.7.3 · 198.4KiB pg_partman_14-4.7.2-1.rhel9.x86_64.rpm PGDG · 4.7.2 · 198.1KiB pg_partman_14-4.7.1-1.rhel9.x86_64.rpm PGDG · 4.7.1 · 213.6KiB pg_partman_14-4.7.0-1.rhel9.x86_64.rpm PGDG · 4.7.0 · 213.1KiB pg_partman_14-4.6.2-1.rhel9.x86_64.rpm PGDG · 4.6.2 · 211.1KiB pg_partman_14-4.6.1-1.rhel9.x86_64.rpm PGDG · 4.6.1 · 210.6KiB
el9.aarch64
el10.x86_64
el10.aarch64
d12.x86_64
d12.aarch64
d13.x86_64
d13.aarch64
u22.x86_64
u22.aarch64
u24.x86_64
u24.aarch64

构建

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

pig build pkg pg_partman         # 构建 RPM / DEB 包

安装

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

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

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

pig install pg_partman;          # 当前活跃 PG 版本安装
pig ext install -y pg_partman -v 18  # PG 18
pig ext install -y pg_partman -v 17  # PG 17
pig ext install -y pg_partman -v 16  # PG 16
pig ext install -y pg_partman -v 15  # PG 15
pig ext install -y pg_partman -v 14  # PG 14
dnf install -y pg_partman_18       # PG 18
dnf install -y pg_partman_17       # PG 17
dnf install -y pg_partman_16       # PG 16
dnf install -y pg_partman_15       # PG 15
dnf install -y pg_partman_14       # PG 14
apt install -y postgresql-18-partman   # PG 18
apt install -y postgresql-17-partman   # PG 17
apt install -y postgresql-16-partman   # PG 16
apt install -y postgresql-15-partman   # PG 15
apt install -y postgresql-14-partman   # PG 14

创建扩展

CREATE EXTENSION pg_partman CASCADE;  -- 依赖: plpgsql

用法

pg_partman: 按时间或 ID 管理分区表的扩展

pg_partman 使用 PostgreSQL 原生声明式分区(v5.0+)自动创建和管理基于时间和基于数字的分区集。它处理新分区的添加和旧分区的按保留策略删除,并提供可选的后台工作进程用于自动维护。

创建扩展

CREATE SCHEMA partman;
CREATE EXTENSION pg_partman SCHEMA partman;

创建基于时间的分区集

CREATE TABLE public.measurements (
    id          bigserial,
    created_at  timestamptz NOT NULL DEFAULT now(),
    value       numeric
) PARTITION BY RANGE (created_at);

SELECT partman.create_parent(
    p_parent_table  := 'public.measurements',
    p_control       := 'created_at',
    p_interval      := '1 day'
);

创建基于序列/ID 的分区集

CREATE TABLE public.events (
    id      bigserial,
    data    text
) PARTITION BY RANGE (id);

SELECT partman.create_parent(
    p_parent_table  := 'public.events',
    p_control       := 'id',
    p_interval      := '100000'
);

运行维护

手动触发分区维护(创建新分区、删除过期分区):

SELECT partman.run_maintenance();

或针对特定表:

SELECT partman.run_maintenance(p_parent_table := 'public.measurements');

配置保留策略

更新配置以设置保留策略:

UPDATE partman.part_config
SET    retention = '30 days',
       retention_keep_table = false
WHERE  parent_table = 'public.measurements';

后台工作进程

postgresql.conf 中启用自动维护:

shared_preload_libraries = 'pg_partman_bgw'
pg_partman_bgw.interval = 3600          -- 每小时运行一次(秒)
pg_partman_bgw.dbname = 'mydb'

将现有数据迁移到分区

CALL partman.partition_data_proc('public.measurements');

显示分区

SELECT * FROM partman.show_partitions('public.measurements');

撤销分区

CALL partman.undo_partition_proc('public.measurements');

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