美文网首页PostgreSQL
向大神致敬(1)---pg_pathman 的安装与使用

向大神致敬(1)---pg_pathman 的安装与使用

作者: 遥想公瑾当年 | 来源:发表于2016-11-02 22:25 被阅读2138次

    本文是学习大神博客实践操作,什么是pg_pathman?有多么牛逼的地方?详情看Postgrsql的教主博客:https://yq.aliyun.com/articles/62314?spm=5176.8091938.0.0.y665kg
    项目github地址:https://github.com/postgrespro/pg_pathman
    注意:pg_pathman只支持postgresql9.5+版本。(我本人测试,发现,版本不能低于9.5.3)
    作者只是模仿练习,步骤如下:

    一 配置安装

    [root@bogon opt]# cd /opt
    [root@bogon opt]# git clone https://github.com/postgrespro/pg_pathman
    #配置下PG的home环境变量,不设置的话, 会报错如下:
    # make: pg_config: Command not found
    # 原因是root下的,而pg的path是配置在postgres用户下的
    [root@bogon opt]#  export PATH=/home/postgres:$PATH
    [root@bogon opt]# cd pg_pathman
    [root@bogon opt]# make USE_PGXS=1
    [root@bogon opt]# make USE_PGXS=1 install
    #更改pg的配置文件
    [root@bogon pg_pathman]# cd $PGDATA
    [root@bogon data]# vim postgresql.conf
    #将shared_preload_libraries注释取消,将下面变量赋值进去
    shared_preload_libraries = 'pg_pathman,pg_stat_statements' 
    # esc退出,wq!保存退出!
    
    #启动数据库
    [root@bogon data]# su - postgres
    [postgres@bogon ~]$ pg_ctl start -D $PGDATA
    #如果已经启动,更改配置后快速重启
    # [postgres@bogon ~]$ pg_ctl restart -m fast
    
    #进入数据库,配置扩展,比如我之前创建的一个 Test数据库
    [postgres@bogon ~]$ psql Test
    psql (9.6.0)
    Type "help" for help.
    
    Test=# create extension pg_pathman;
    CREATE EXTENSION
    # 查看已安装的扩展
    Test=# \dx
                                List of installed extensions
        Name    | Version |   Schema   |                   Description             
          
    ------------+---------+------------+-------------------------------------------
    ------
     pg_pathman | 1.1     | public     | Partitioning tool ver. 1.1
     plpgsql    | 1.0     | pg_catalog | PL/pgSQL procedural language
     rum        | 1.0     | public     | RUM index access method
     uuid-ossp  | 1.1     | public     | generate universally unique identifiers (U
    UIDs)
     zhparser   | 1.0     | public     | a parser for full-text search of Chinese
    (5 rows)
    
    

    二 分区管理

    目前支持两种分区,range分区个hash分区。
    笔者认为,range分区用于有规律的分区,如比如,根据日期分区等。hash分区应该是指定进行分区。

    2.1 range分区

    2.1.1 分区函数介绍

    指定起始值、间隔、分区个数来range分区

    create_range_partitions(relation       REGCLASS,  -- 主表OID
                            attribute      TEXT,      -- 分区列名
                            start_value    ANYELEMENT,  -- 开始值
                            p_interval     ANYELEMENT,  -- 间隔;任意类型,适合任意类型的分区表
                            p_count        INTEGER DEFAULT NULL,   --  分多少个区
                            partition_data BOOLEAN DEFAULT TRUE)   --  是否立即将数据从主表迁移到分区, 
    --不建议这么使用, 建议使用非堵塞式的迁移( 调用partition_table_concurrently() )
    

    指定起始值、终值、间隔来range分区

    create_partitions_from_range(relation       REGCLASS,  -- 主表OID
                                 attribute      TEXT,      -- 分区列名
                                 start_value    ANYELEMENT,  -- 开始值
                                 end_value      ANYELEMENT,  -- 结束值
                                 p_interval     INTERVAL,    -- 间隔;interval 类型,用于时间分区表
                                 partition_data BOOLEAN DEFAULT TRUE)   --  是否立即将数据从主表迁移到分区, 
    --不建议这么使用, 建议使用非堵塞式的迁移( 调用partition_table_concurrently() )
    

    2.1.2 分区实验

    #创建分区主表
    Test=# create table part_test(id int, info text, crt_time timestamp not null);
    CREATE TABLE
    Test=# insert into part_test select id,md5(random()::text),clock_timestamp() from generate_series(1,10000) t(id);
    INSERT 0 10000
    Test=# select * from part_test limit 10;
     id |               info               |          crt_time          
    ----+----------------------------------+----------------------------
      1 | ab8e5ee0448a2b4ad61d7e0e767ee1ea | 2016-11-02 16:04:18.294478
      2 | 28a626af632e0ccebbd111a3e1a240e1 | 2016-11-02 16:04:18.294745
      3 | faf2f529a9a83d27b6e13e68ee8e7407 | 2016-11-02 16:04:18.294761
      4 | f4afc8263ea625000305825efff52eaa | 2016-11-02 16:04:18.294768
      5 | 385d942fded21a969f5c0dda64cb1aad | 2016-11-02 16:04:18.294774
      6 | 703f3bee94e322e3234a50cfed01ad24 | 2016-11-02 16:04:18.29478
      7 | 6e28eac2c127263860929b4c24b46a09 | 2016-11-02 16:04:18.294787
      8 | 68b9e8133af0aa1b024aa323ffae0c9c | 2016-11-02 16:04:18.294793
      9 | 58428fa313fc49d13dc14dad18b10fad | 2016-11-02 16:04:18.294799
     10 | cd2cb16088f7304a1f08cd9c9b774fd9 | 2016-11-02 16:04:18.294805
    (10 rows)
    
    

    注意:

    1. 分区列必须有not null约束
    2. 分区个数必须能覆盖已有的所有记录
      创建10个分区,每个分区包含一个月的数据。
    Test=# select create_range_partitions(
    'part_test'::regclass, --主表oid
    'crt_time',  --分区字段,一定要not null约束
    '2016-11-02 00:00:00'::timestamp, --开始时间
    interval '1 month',   --分区间隔,一个月
    10,  --分区表数量
    false  --  不立即将数据从主表迁移到分区,
    );
    

    执行完会创建10个分区表,然后执行非堵塞迁移。
    使用非堵塞式的迁移接口

    partition_table_concurrently(relation   REGCLASS,              -- 主表OID
                                 batch_size INTEGER DEFAULT 1000,  -- 一个事务批量迁移多少记录
                                 sleep_time FLOAT8 DEFAULT 1.0)    -- 获得行锁失败时,休眠多久再次获取,重试60次退出任务。
    
    Test=#  select partition_table_concurrently('part_test'::regclass,10000,1.0);
    

    迁移后,查看仅仅存在主表的数据

    Test=# select count(*) from only part_test;
     count 
    -------
         0
    
    

    明显全部迁移走了。

    2.2 hash分区

    相关文章

      网友评论

        本文标题:向大神致敬(1)---pg_pathman 的安装与使用

        本文链接:https://www.haomeiwen.com/subject/dzbvuttx.html