开始

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 ]

结束

相关文章:

  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-25
  • 2021-12-12
  • 2021-06-29
  • 2021-06-03
猜你喜欢
  • 2021-10-30
  • 2021-06-26
  • 2021-07-06
  • 2021-11-04
  • 2021-06-17
  • 2022-03-07
  • 2021-10-04
相关资源
相似解决方案