zoukankan      html  css  js  c++  java
  • PostgreSQL 如何实现分区表的一个小例子

    开始

    PostgreSQL 到目前为止,是不支持原生的分区表的,看看它如何实现:

    http://www.postgresql.org/docs/current/static/ddl-partitioning.html

    要实现分区,需要借助继承与规则。

    postgres=# create table ptest(id integer, name varchar(20));
    CREATE TABLE
    postgres=# create table ctest01(CHECK(id<5000000)) inherits (ptest);
    CREATE TABLE
    postgres=# create table ctest02(CHECK(id>=5000000)) inherits (ptest);
    CREATE TABLE
    postgres=# 
    postgres=# create index on ctest01(id);
    CREATE INDEX
    postgres=# create index on ctest02(id);
    CREATE INDEX
    postgres=# 
    postgres=# 
    
    postgres=# CREATE OR REPLACE FUNCTION ptest_insert_trigger() RETURNS TRIGGER AS $$ 
    postgres$# 
    postgres$# BEGIN 
    postgres$# 
    postgres$#    IF ( NEW.id <5000000 ) THEN 
    postgres$#        INSERT INTO ctest01 VALUES (NEW.*);
    postgres$#    ELSIF ( NEW.id >= 5000000 ) THEN 
    postgres$#        INSERT INTO ctest02 VALUES (NEW.*); 
    postgres$#    ELSE 
    postgres$#        RAISE EXCEPTION 'Error while inserting data';
    postgres$#    END IF; 
    postgres$#   
    postgres$#   RETURN NULL;
    postgres$# END; $$ LANGUAGE plpgsql;
    CREATE FUNCTION
    postgres=# 
    postgres=# CREATE TRIGGER insert_ptest_trigger BEFORE INSERT ON ptest FOR EACH ROW 
    postgres-#   EXECUTE PROCEDURE ptest_insert_trigger();
    CREATE TRIGGER
    postgres=# 

    其实如果从其他的表里一次性地向分区表里导入数据,那么最好先把 index 和 constraint 无效化。

    [作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

    结束

  • 相关阅读:
    Bye sent_keys
    快速获取Jenkins上build
    快速搞定selenium grid分布式
    python 图形界面开发
    [分享] 自动化测试与持续集成方案-- UI 检查
    hua ge ju hao
    暴力 C++
    简单排序(java)
    记codeforces两题
    hdu 1874, dijkstra
  • 原文地址:https://www.cnblogs.com/gaojian/p/2765744.html
Copyright © 2011-2022 走看看