Yum安装

  1. [root@ztt-test-pg pg_pathman-1.5.3]# yum install pg_pathman11.x86_64

更改postgresql.conf的配置文件

  1. [root@ztt-test-pg pg_pathman-1.5.3]# vi /home/postgres/pgdata/postgresql.conf

将shared_preload_libraries注释取消,将下面变量赋值进去

shared_preload_libraries = ‘pg_pathman ‘

重启pg

  1. [root@ztt-test-pg pg_pathman-1.5.3]# su - postgres
  2. [postgres@ztt-test-pg ~]$ pg_ctl restart

配置扩展

  1. [postgres@ztt-test-pg ~]$ psql
  2. psql (11.3)
  3. Type "help" for help.
  4. postgres=# create extension pg_pathman ;
  5. CREATE EXTENSION

查看已安装的扩展

  1. postgres=# \dx
  2. List of installed extensions
  3. Name | Version | Schema | Description
  4. ------------+---------+------------+----------------------------------
  5. pg_pathman | 1.5 | public | Partitioning tool for PostgreSQL
  6. plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
  7. (2 rows)

创建测试表

  1. postgres=# create table tbl_test (id int, info text, c_time timestamp);
  2. CREATE TABLE

插入测试数据

  1. postgres=# insert into tbl_test select generate_series(1,100000),md5(random()::text),clock_timestamp();
  2. INSERT 0 100000
  3. postgres=# select count(id) from tbl_test;
  4. count
  5. --------
  6. 100000

范围分区

create_range_partitions(relation REGCLASS, — 主表OID
attribute TEXT, — 分区列名
start_value ANYELEMENT, — 开始值
p_interval ANYELEMENT, — 间隔;任意类型,适合任意类型的分区表
p_count INTEGER DEFAULT NULL, — 分多少个区
partition_data BOOLEAN DEFAULT TRUE) — 是否立即将数据从主表迁移到分区

  1. postgres=# select
  2. create_range_partitions('tbl_test'::regclass,'c_time','2019-05-28 00:00:00'::timestamp,interval '1 month',50,false) ;
  3. create_range_partitions
  4. -------------------------
  5. 50
  6. (1 row)

注意:
分区列必须有not null约束
分区个数必须能覆盖已有的所有记录

使用非堵塞式的迁移接口
partition_table_concurrently(relation REGCLASS, — 主表OID
batch_size INTEGER DEFAULT 1000, — 一个事务批量迁移多少记录
sleep_time FLOAT8 DEFAULT 1.0) — 获得行锁失败时,休眠多久再次获取,重试60次退出任务

  1. postgres=# select partition_table_concurrently('tbl_test'::regclass,10000,1.0);

仅仅存在主表的数据

  1. postgres=# select count(*) from only tbl_test;
  2. count
  3. -------
  4. 0
  5. (1 row)

明显全部迁移走了

查看分区表

  1. postgres=# select * from pathman_partition_list ;
  2. parent | partition | parttype | expr | range_min | range_max
  3. ----------+-------------+----------+--------+---------------------+---------------------
  4. tbl_test | tbl_test_1 | 2 | c_time | 2019-05-28 00:00:00 | 2019-06-28 00:00:00
  5. tbl_test | tbl_test_2 | 2 | c_time | 2019-06-28 00:00:00 | 2019-07-28 00:00:00
  6. tbl_test | tbl_test_3 | 2 | c_time | 2019-07-28 00:00:00 | 2019-08-28 00:00:00
  7. tbl_test | tbl_test_4 | 2 | c_time | 2019-08-28 00:00:00 | 2019-09-28 00:00:00

Hash分区
创建分区并迁移

  1. postgres=# SELECT create_hash_partitions('tbl_test', 'id', 100);
  2. create_hash_partitions
  3. ------------------------
  4. 100
  5. (1 row)
0 评论  
添加一条新评论