我们先创建一张父表。 记住,所有的分区表都得继承他。
t_girl=# create table num_master (ID int not null primary key);CREATE table
接下来我们创建一个简单的函数来动态创建分区表。
t_girl=# create or replace function create_partition_table () returns voID as $$t_girl$# declare i int;t_girl$# declare cnt int;t_girl$# declare stmt text;t_girl$# begint_girl$# -- Created by ytt at 2013/12/15. Dynamic creating partition tables.t_girl$# i:= 0;t_girl$# cnt:=4;t_girl$# <<lable1>> while i < cnt loopt_girl$# stmt := 'create table num_slave'||i+1||'(check(ID >='||i*100||' and ID <'||(i+1)*100||')) inherits(num_master)';t_girl$# execute stmt;t_girl$# i:=i + 1;t_girl$# end loop lable1;t_girl$# return;t_girl$# end;t_girl$# $$ language plpgsql;CREATE FUNCTIONt_girl=#
OK。 现在可以执行了。
t_girl=# select create_partition_table(); create_partition_table ------------------------ (1 row)
列出所有的表
t_girl=# \d List of relations Schema | name | Type | Owner --------+------------+-------+---------- ytt | num_master | table | postgres ytt | num_slave1 | table | postgres ytt | num_slave2 | table | postgres ytt | num_slave3 | table | postgres ytt | num_slave4 | table | postgres ytt | t1 | table | t_girl(6 rows)
我们针对父表建立一个触发器函数体,对应其分区表的数据分布。
t_girl=# create or replace function num_insert_trigger()t_girl-# returns trigger as $$t_girl$# begint_girl$# -- Created by ytt at 2013/12/15. Do how to distribute data.t_girl$# if (new.ID >=0 and new.ID <100) thent_girl$# insert into num_slave1 values (new.*);t_girl$# elsif (new.ID >=100 and new.ID <200) thent_girl$# insert into num_slave2 values(new.*);t_girl$# elsif (new.ID >=200 and new.ID <300) thent_girl$# insert into num_slave3 values (new.*);t_girl$# elsif (new.ID >=300 and new.ID <400) thent_girl$# insert into num_slave4 values (new.*);t_girl$# elset_girl$# raise exception 'Column ID out of range.';t_girl$# end if;t_girl$# return null;t_girl$# end;t_girl$# $$t_girl-# language plpgsql;CREATE FUNCTION
我们看看已经建好的触发器:
t_girl=# \d+ num_master table "ytt.num_master" Column | Type | ModifIErs | Storage | Stats target | Description --------+---------+-----------+---------+--------------+------------- ID | integer | not null | plain | | Indexes: "num_master_pkey" PRIMARY KEY,btree (ID)Triggers: insert_num_slave_trigger BEFORE INSERT ON num_master FOR EACH ROW EXECUTE PROCEDURE ytt.num_insert_trigger()Child tables: num_slave1,num_slave2,num_slave3,num_slave4Has OIDs: no
我们现在生成简单的测试数据。
t_girl=# select func_create_sample_data(); func_create_sample_data ------------------------- (1 row)
上面的函数生成了大概400行的数据。
为了查看优化器是如何处理查询的,我们来看看简单的查询
t_girl=# explain select * from num_master where ID > 30 and ID < 120; query PLAN ----------------------------------------------------------------- Append (cost=0.00..5.00 rows=91 wIDth=4) -> Seq Scan on num_master (cost=0.00..0.00 rows=1 wIDth=4) Filter: ((ID > 30) AND (ID < 120)) -> Seq Scan on num_slave1 (cost=0.00..2.50 rows=70 wIDth=4) Filter: ((ID > 30) AND (ID < 120)) -> Seq Scan on num_slave2 (cost=0.00..2.50 rows=20 wIDth=4) Filter: ((ID > 30) AND (ID < 120))(7 rows)t_girl=#我也是今天刚刚接触到POSTGREsql的分区表,有问题,还希望提出。 总结
以上是内存溢出为你收集整理的POSTGRESQL 分区表初次体验全部内容,希望文章能够帮你解决POSTGRESQL 分区表初次体验所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)