postgresql 分区表

系统 3051 0

 

1.普通方式建立主表

      
        create
      
      
        table
      
      
         tbl_partition(

    id 
      
      
        integer
      
      
        ,

    name 
      
      
        varchar
      
      (
      
        20
      
      
        ),

    gender boolean,

    join_date date,

    dept 
      
      
        char
      
      (
      
        4
      
      
        )

)
      
    

2.创建分区表.(注意加上约束和继承)

      
        create
      
      
        table
      
      
         tbl_partition_201211(

    
      
      
        check
      
      ( join_date
      
        >=
      
      DATE 
      
        '
      
      
        2012-11-01
      
      
        '
      
      
        and
      
       join_date
      
        <
      
       DATE 
      
        '
      
      
        2012-12-01
      
      
        '
      
      
         )

)inherits(tbl_partition);




      
      
        create
      
      
        table
      
      
         tbl_partition_201212 (


      
      
        check
      
       ( join_date 
      
        >=
      
       DATE 
      
        '
      
      
        2012-12-01
      
      
        '
      
      
        AND
      
       join_date 
      
        <
      
       DATE 
      
        '
      
      
        2013-01-01
      
      
        '
      
      
         )      

) INHERITS (tbl_partition);




      
      
        create
      
      
        table
      
      
         tbl_partition_201301 (


      
      
        check
      
       ( join_date 
      
        >=
      
       DATE 
      
        '
      
      
        2013-01-01
      
      
        '
      
      
        AND
      
       join_date 
      
        <
      
       DATE 
      
        '
      
      
        2013-02-01
      
      
        '
      
      
         )      

) INHERITS (tbl_partition);




      
      
        create
      
      
        table
      
      
         tbl_partition_201302 (


      
      
        check
      
       ( join_date 
      
        >=
      
       DATE 
      
        '
      
      
        2013-02-01
      
      
        '
      
      
        AND
      
       join_date 
      
        <
      
       DATE 
      
        '
      
      
        2013-03-01
      
      
        '
      
      
         )  

) INHERITS (tbl_partition);




      
      
        create
      
      
        table
      
      
         tbl_partition_201303 (


      
      
        check
      
       ( join_date 
      
        >=
      
       DATE 
      
        '
      
      
        2013-03-01
      
      
        '
      
      
        AND
      
       join_date 
      
        <
      
       DATE 
      
        '
      
      
        2013-04-01
      
      
        '
      
      
         ) 

) INHERITS (tbl_partition);




      
      
        create
      
      
        table
      
      
         tbl_partition_201304 (


      
      
        check
      
       ( join_date 
      
        >=
      
       DATE 
      
        '
      
      
        2013-04-01
      
      
        '
      
      
        AND
      
       join_date 
      
        <
      
       DATE 
      
        '
      
      
        2013-05-01
      
      
        '
      
      
         )  

) INHERITS (tbl_partition);




      
      
        create
      
      
        table
      
      
         tbl_partition_201305 (


      
      
        check
      
       ( join_date 
      
        >=
      
       DATE 
      
        '
      
      
        2013-05-01
      
      
        '
      
      
        AND
      
       join_date 
      
        <
      
       DATE 
      
        '
      
      
        2013-06-01
      
      
        '
      
      
         )  

) INHERITS (tbl_partition);
      
    

 

3.分区表上建立索引.

      
        create
      
      
        index
      
       tbl_partition_201211_joindate 
      
        on
      
      
         tbl_partition_201211 (join_date);




      
      
        create
      
      
        index
      
       tbl_partition_201212_joindate 
      
        on
      
      
         tbl_partition_201212 (join_date); 




      
      
        create
      
      
        index
      
       tbl_partition_201301_joindate 
      
        on
      
      
         tbl_partition_201301 (join_date);    




      
      
        create
      
      
        index
      
       tbl_partition_201302_joindate 
      
        on
      
      
         tbl_partition_201302 (join_date); 




      
      
        create
      
      
        index
      
       tbl_partition_201303_joindate 
      
        on
      
      
         tbl_partition_201303 (join_date); 




      
      
        create
      
      
        index
      
       tbl_partition_201304_joindate 
      
        on
      
      
         tbl_partition_201304 (join_date); 




      
      
        create
      
      
        index
      
       tbl_partition_201305_joindate 
      
        on
      
       tbl_partition_201305 (join_date); 
    

 

4.postgresql不能自动插入到字表.所以

要创建触发器 函数 和创建触发器.这样对外插入就不会有字表的感觉.

触发器函数:

      
        CREATE
      
      
        OR
      
      
        REPLACE
      
      
        FUNCTION
      
      
         tbl_partition_insert_trigger()                      


      
      
        RETURNS
      
      
        TRIGGER
      
      
        AS
      
      
         $$  


      
      
        BEGIN
      
      
        IF
      
       ( NEW.join_date 
      
        >=
      
       DATE 
      
        '
      
      
        2012-11-01
      
      
        '
      
      
        AND
      
      
            

         NEW.join_date 
      
      
        <
      
       DATE 
      
        '
      
      
        2012-12-01
      
      
        '
      
       ) 
      
        THEN
      
      
        INSERT
      
      
        INTO
      
       tbl_partition_201211 
      
        VALUES
      
       (NEW.
      
        *
      
      
        );  

    ELSIF ( NEW.join_date 
      
      
        >=
      
       DATE 
      
        '
      
      
        2012-12-01
      
      
        '
      
      
        AND
      
      
          

            NEW.join_date 
      
      
        <
      
       DATE 
      
        '
      
      
        2013-01-01
      
      
        '
      
       ) 
      
        THEN
      
      
        INSERT
      
      
        INTO
      
       tbl_partition_201212 
      
        VALUES
      
       (NEW.
      
        *
      
      
        );  

    ELSIF ( NEW.join_date 
      
      
        >=
      
       DATE 
      
        '
      
      
        2013-01-01
      
      
        '
      
      
        AND
      
      
          

            NEW.join_date 
      
      
        <
      
       DATE 
      
        '
      
      
        2013-02-01
      
      
        '
      
       ) 
      
        THEN
      
      
        INSERT
      
      
        INTO
      
       tbl_partition_201301 
      
        VALUES
      
       (NEW.
      
        *
      
      
        );  

    ELSIF ( NEW.join_date 
      
      
        >=
      
       DATE 
      
        '
      
      
        2013-02-01
      
      
        '
      
      
        AND
      
      
          

            NEW.join_date 
      
      
        <
      
       DATE 
      
        '
      
      
        2013-03-01
      
      
        '
      
       ) 
      
        THEN
      
      
        INSERT
      
      
        INTO
      
       tbl_partition_201302 
      
        VALUES
      
       (NEW.
      
        *
      
      
        );  

    ELSIF ( NEW.join_date 
      
      
        >=
      
       DATE 
      
        '
      
      
        2013-03-01
      
      
        '
      
      
        AND
      
      
          

            NEW.join_date 
      
      
        <
      
       DATE 
      
        '
      
      
        2013-04-01
      
      
        '
      
       ) 
      
        THEN
      
      
        INSERT
      
      
        INTO
      
       tbl_partition_201303 
      
        VALUES
      
       (NEW.
      
        *
      
      
        );  

    ELSIF ( NEW.join_date 
      
      
        >=
      
       DATE 
      
        '
      
      
        2013-04-01
      
      
        '
      
      
        AND
      
      
          

            NEW.join_date 
      
      
        <
      
       DATE 
      
        '
      
      
        2013-05-01
      
      
        '
      
       ) 
      
        THEN
      
      
        INSERT
      
      
        INTO
      
       tbl_partition_201304 
      
        VALUES
      
       (NEW.
      
        *
      
      
        );

    ELSIF ( NEW.join_date 
      
      
        >=
      
       DATE 
      
        '
      
      
        2013-05-01
      
      
        '
      
      
        AND
      
      
        

            NEW.join_date 
      
      
        <
      
       DATE 
      
        '
      
      
        2013-06-01
      
      
        '
      
       ) 
      
        THEN
      
      
        INSERT
      
      
        INTO
      
       tbl_partition_201305 
      
        VALUES
      
       (NEW.
      
        *
      
      
        ); 

    
      
      
        ELSE
      
      
          

        RAISE EXCEPTION 
      
      
        '
      
      
        Date out of range. Fix the tbl_partition_insert_trigger() function!
      
      
        '
      
      
        ;  

    
      
      
        END
      
      
        IF
      
      
        ;  

    
      
      
        RETURN
      
      
        NULL
      
      
        ;  


      
      
        END
      
      
        ;  

$$  

LANGUAGE plpgsql;
      
    

触发器:

      
        CREATE
      
      
        TRIGGER
      
      
         insert_tbl_partition_traigger

    BEFORE 
      
      
        INSERT
      
      
        ON
      
      
         tbl_partition

    
      
      
        For
      
      
         EACH ROW 

    
      
      
        EXECUTE
      
      
        PROCEDURE
      
       tbl_partition_insert_trigger();
    

 

 

 

5.插入数据进行测试.

 

      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        1
      
      , 
      
        '
      
      
        David
      
      
        '
      
      , 
      
        '
      
      
        1
      
      
        '
      
      , 
      
        '
      
      
        2013-01-10
      
      
        '
      
      , 
      
        '
      
      
        TS
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        2
      
      , 
      
        '
      
      
        Sandy
      
      
        '
      
      , 
      
        '
      
      
        0
      
      
        '
      
      , 
      
        '
      
      
        2013-02-10
      
      
        '
      
      , 
      
        '
      
      
        TS
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        3
      
      , 
      
        '
      
      
        Eagle
      
      
        '
      
      , 
      
        '
      
      
        1
      
      
        '
      
      , 
      
        '
      
      
        2012-11-01
      
      
        '
      
      , 
      
        '
      
      
        TS
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        4
      
      , 
      
        '
      
      
        Miles
      
      
        '
      
      , 
      
        '
      
      
        1
      
      
        '
      
      , 
      
        '
      
      
        2012-12-15
      
      
        '
      
      , 
      
        '
      
      
        SD
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        5
      
      , 
      
        '
      
      
        Simon
      
      
        '
      
      , 
      
        '
      
      
        1
      
      
        '
      
      , 
      
        '
      
      
        2012-12-10
      
      
        '
      
      , 
      
        '
      
      
        SD
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        6
      
      , 
      
        '
      
      
        Rock
      
      
        '
      
      , 
      
        '
      
      
        1
      
      
        '
      
      , 
      
        '
      
      
        2012-11-10
      
      
        '
      
      , 
      
        '
      
      
        SD
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        7
      
      , 
      
        '
      
      
        Peter
      
      
        '
      
      , 
      
        '
      
      
        1
      
      
        '
      
      , 
      
        '
      
      
        2013-01-11
      
      
        '
      
      , 
      
        '
      
      
        SD
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        8
      
      , 
      
        '
      
      
        Sally
      
      
        '
      
      , 
      
        '
      
      
        0
      
      
        '
      
      , 
      
        '
      
      
        2013-03-10
      
      
        '
      
      , 
      
        '
      
      
        BCSC
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        9
      
      , 
      
        '
      
      
        Carrie
      
      
        '
      
      , 
      
        '
      
      
        0
      
      
        '
      
      , 
      
        '
      
      
        2013-04-02
      
      
        '
      
      , 
      
        '
      
      
        BCSC
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        10
      
      , 
      
        '
      
      
        Lee
      
      
        '
      
      , 
      
        '
      
      
        1
      
      
        '
      
      , 
      
        '
      
      
        2013-01-05
      
      
        '
      
      , 
      
        '
      
      
        BMC
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        11
      
      , 
      
        '
      
      
        Nicole
      
      
        '
      
      , 
      
        '
      
      
        0
      
      
        '
      
      , 
      
        '
      
      
        2012-11-10
      
      
        '
      
      , 
      
        '
      
      
        PROJ
      
      
        '
      
      
        );




      
      
        insert
      
      
        into
      
       tbl_partition 
      
        values
      
       (
      
        12
      
      , 
      
        '
      
      
        Renee
      
      
        '
      
      , 
      
        '
      
      
        0
      
      
        '
      
      , 
      
        '
      
      
        2013-01-10
      
      
        '
      
      , 
      
        '
      
      
        TS
      
      
        '
      
      );
    

 

 

另: 解除分区方法:

      
        alter
      
      
        table
      
       tbl_partition_201304 no inherit tbl_partition;
    

  增加分区:

 

 

参考:

PostgreSQL官方说明: http://www.postgresql.org/docs/9.2/static/ddl-partitioning.html

ITEYE: http://diegoball.iteye.com/blog/713826

kenyon(君羊): http://my.oschina.net/Kenyon/blog/59455

博客园: http://www.cnblogs.com/mchina/archive/2013/04/09/2973427.html

postgresql 分区表


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论