PostgreSQL分區表建立

來源:互聯網
上載者:User

標籤:

pgsql 分區表:--主表create table test(id integer, name varchar(32));create index idx_test_id on test using btree(id);--分表create table test_b (like test including constraints including defaults including indexes) inherits(test);create table test_c (like test including constraints including defaults including indexes) inherits(test);alter table test_b add constraint con_test_c check(id >=1001 and id <= 2000);alter table test_c add constraint con_test_b check(id >=2001 and id <= 3000);create table test_d( check(id >=3001 and id <= 4000))inherits (test);--插資料insert into test_b select generate_series(1001,2000),‘bbb‘;insert into test_c select generate_series(2001,3000),‘ccc‘;insert into test_d select generate_series(3001,4000),‘ddd‘;--分析表,加入計劃analyze test_a;analyze test_b;analyze test_c;analyze test_d;--測試查詢計劃 constraint_exclusionset  constraint_exclusion = off/partition;show constraint_exclusion;explain select * from test where id = 1;--測試Insertinsert into test values (11,‘aaaaa‘);select * from test where name = ‘aaaaa‘select * from test_a;insert into test values(1,‘a‘),(1111,‘v‘),(2222,‘vv‘);select * from test_c;--建立觸發器函數create function tb_partition_insert()returns trigger  as$$beginif (id >=1 and id <= 1000) then    insert into test_a values(NEW.*);elseif (id >=1001 and id <= 2000) then    insert into test_b values(NEW.*);  elseif (id >=2001 and id <= 3000) then   insert into test_c 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_test_parition    before insert on test    for each row execute procedure tb_partition_insert();--測試Insertinsert into test values(1,‘a‘),(1111,‘v‘),(2222,‘vv‘);select pg_size_pretty(pg_relation_size(‘test‘));insert into test select generate_series(1,1000),‘aa‘;select count(1) from test_a;select pg_size_pretty(pg_relation_size(‘test‘));select pg_table_size(‘test‘)

 

參考:

為主:http://www.cnblogs.com/mchina/archive/2013/04/09/2973427.html
為輔:http://my.oschina.net/Kenyon/blog/59455 ,

         http://www.postgres.cn/docs/9.4/ddl-partitioning.html#DDL-PARTITIONING-CONSTRAINT-EXCLUSION
例子參考:http://francs3.blog.163.com/blog/static/4057672720112422436937/
結合:http://www.cnblogs.com/stephen-liu74/archive/2012/04/27/2291814.html
查看錶大小等操作:http://www.cnblogs.com/liuyuanyuanGOGO/p/3224554.html
分區表用到了繼承,在對錶進行清除資料操作時,要注意是否會同時清除父表和字表 http://francs3.blog.163.com/blog/static/40576727201011203725668/















PostgreSQL分區表建立

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.