PG-表分区管理工具
pg_Partman
pg_partman是PostgreSQL的一个扩展插件,用于创建和管理基于时间或者基于序列的表分区。也支持多级子分区。子表和触发器都由扩展插件自身管理。已经有数据的表也能很容易的添加细粒度的分区。可选的保留策略能够自动删除不再需要的分区。后台工作进程(BGW)能够自动运行分区维护定时执行任务,而不需要依赖于linux cron等程序从外部进行维护。
安装配置
依赖
- PostgreSQL >= 9.6
安装插件
#wget https://github.com/pgpartman/pg_partman/archive/refs/tags/v4.5.1.tar.gz
wget https://github.com/pgpartman/pg_partman/archive/v4.2.2.tar.gz
tar -xf v4.2.2.tar.gz
cd pg_partman-4.2.2
make && make install
# If you do not want the background worker compiled and just want the plain PL/PGSQL functions, you can run this instead:
make NO_BGW=1 install
数据库服务中配置
配置postgresql.conf文件
shared_preload_libraries = 'pg_partman_bgw' # (change requires restart)
安装插件
select * from pg_available_extensions where name like 'pg_part%';
CREATE SCHEMA partman;
CREATE EXTENSION pg_partman SCHEMA partman;
CREATE ROLE partman WITH LOGIN;
GRANT ALL ON SCHEMA partman TO partman;
GRANT ALL ON ALL TABLES IN SCHEMA partman TO partman;
GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA partman TO partman;
GRANT EXECUTE ON ALL PROCEDURES IN SCHEMA partman TO partman; -- PG11+ only
GRANT ALL ON SCHEMA my_partition_schema TO partman;
GRANT CREATE ON DATABASE mydb TO partman;
pg_Pathman
pg_pathman是PostgreSQL Pro公司开源的扩展插件,可以为大型分布式数据库提供优化的分区解决方案。使用pg_pathman可以给大型数据库不停机分区,加速分区表查询,动态管理和增加分区,为分区增加外部表,操作联合分区等。
安装
适用环境
- PostgreSQL 9.5, 9.6, 10, 11, 12, 13;
- Postgres Pro Standard 9.5, 9.6, 10, 11, 12;
- Postgres Pro Enterprise;
软件安装
wget https://github.com/postgrespro/pg_pathman/archive/refs/tags/1.5.12.tar.gz
# git clone https://github.com/postgrespro/pg_pathman
cd pg_pathman
make USE_PGXS=1
make USE_PGXS=1 install
# wget https://github.com/postgrespro/pg_pathman/archive/1.5.3.tar.gz
# tar -zxvf v1.5.3.tar.gz
参数配置
vi $PGDATA/postgresql.conf
shared_preload_libraries = 'pg_pathman,pg_stat_statements'
数据库中配置
create extension pg_pathman;
插件升级
- 正常安装新版本的pg_pathman插件
- 重启pg服务
- 执行sql版本更新命令
ALTER EXTENSION pg_pathman UPDATE;
SET pg_pathman.enable = t;
select pathman_version();