ITPub博客

首页 > 数据库 > 数据库开发技术 > postgresql分区表的使用

postgresql分区表的使用

原创 数据库开发技术 作者:anwll 时间:2015-12-08 14:57:36 0 删除 编辑
在看《PostgreSQL修炼之道》时讲到分区表的地方做了些实验,测试表的数据来之另外一个ORACLE数据库的dba_objects。

基本概念,postgresql中的分区表其实是通过表继承来实现的(表继承的概念度娘之),即对外看到的即是父表,分区表继承父表的栏位,                  各分区通常情况下不会增加字段                                                             
定义一个规则或者触发器把在对父表插入的语句重定向至合适的分区表。    

其他的设置:1. 为分区表增加约束,定义每个分区允许的键值,保证分区数据的正确
                   2. 对分区添加合适的索引
                   3. postgresql.conf的constraint_exclusion参数打开后,在查询条件中如果查询和分区符合,将只扫描该分区。


实验:
1. 创建父表               
postgres=# create table p_table1 INHERITS (p_table);
ERROR:  syntax error at or near "INHERITS"
LINE 1: create table p_table1 INHERITS (p_table);
                              ^
Time: 0.230 ms
要加一个()       
postgres=# create table p_table1() INHERITS (p_table);
CREATE TABLE
Time: 6.038 ms

2.创建子表         
create table p_table1() INHERITS (p_table);
create table p_table2() INHERITS (p_table);
create table p_table3() INHERITS (p_table);
create table p_table4() INHERITS (p_table);
create table p_table5() INHERITS (p_table);
create table p_table6() INHERITS (p_table);
create table p_table7() INHERITS (p_table);
create table p_table8() INHERITS (p_table);
create table p_table9() INHERITS (p_table);
create table p_table10() INHERITS (p_table);

3.创建触发器            
if (new.namespace=1) then LANGUAGE plpgsql;


create or replace function p_table_trigger()
returns trigger as $$
begin
if (new.namespace=1) then
insert into p_table1 values (new.*);
elsif (new.namespace=2) then
insert into p_table2 values (new.*);
elsif (new.namespace=3) then
insert into p_table3 values (new.*);
elsif (new.namespace=4) then
insert into p_table4 values (new.*);
elsif (new.namespace=5) then
insert into p_table5 values (new.*);
elsif (new.namespace=6) then
insert into p_table6 values (new.*);
elsif (new.namespace=7) then
insert into p_table7 values (new.*);
elsif (new.namespace=8) then
insert into p_table8 values (new.*);
elsif (new.namespace=9) then
insert into p_table9 values (new.*);
elsif (new.namespace=10) then
insert into p_table10 values (new.*);
else raise exception 'Out of range!';
end if;
return null;
end;
$$
LANGUAGE plpgsql;

create trigger insert_p_table_trigger
before insert on p_table
for each row execute procedure p_table_trigger();

4.测试插入两条数据

postgres=# \d+
                       List of relations
 Schema |   Name    | Type  |  Owner   |  Size   | Description 
--------+-----------+-------+----------+---------+-------------
 public | foracle   | table | postgres | 23 MB   | 
 public | p_table   | table | postgres | 0 bytes | 
 public | p_table1  | table | postgres | 0 bytes | 
 public | p_table10 | table | postgres | 0 bytes | 
 public | p_table2  | table | postgres | 0 bytes | 
 public | p_table3  | table | postgres | 0 bytes | 
 public | p_table4  | table | postgres | 0 bytes | 
 public | p_table5  | table | postgres | 0 bytes | 
 public | p_table6  | table | postgres | 0 bytes | 
 public | p_table7  | table | postgres | 0 bytes | 
 public | p_table8  | table | postgres | 0 bytes | 
 public | p_table9  | table | postgres | 0 bytes | 
 public | tt        | table | postgres | 16 kB   | 
(13 rows)
 
postgres=# select * from foracle where owner='SYSTEM' and object_name='T1';
       created       | data_object_id | edition_name | generated |    last_ddl_time    | namespace | object_id | object_name | object_type | owner  | seconda
ry | status | subobject_name | temporary |      timestamp      
---------------------+----------------+--------------+-----------+---------------------+-----------+-----------+-------------+-------------+--------+--------
---+--------+----------------+-----------+---------------------
 2015-09-09 17:09:00 |          89467 |              | N         | 2015-09-09 17:09:00 |         1 |     89467 | T1          | TABLE       | SYSTEM | N      
   | VALID  |                | N         | 2015-09-09:17:09:00
 2015-09-09 17:09:00 |          89467 |              | N         | 2015-09-09 17:09:00 |         1 |     89467 | T1          | TABLE       | SYSTEM | N      
   | VALID  |                | N         | 2015-09-09:17:09:00
(2 rows)
 
Time: 97.429 ms
postgres=# create trigger insert_p_table_trigger
before insert on p_table
for each row execute procedure p_table_trigger();postgres-# postgres-# 
CREATE TRIGGER
Time: 82.790 ms
postgres=# 
postgres=# insert into p_table select * from foracle where owner='SYSTEM' and object_name='T1';
INSERT 0 0
Time: 71.656 ms
postgres=# \d+
                        List of relations
 Schema |   Name    | Type  |  Owner   |    Size    | Description 
--------+-----------+-------+----------+------------+-------------
 public | foracle   | table | postgres | 23 MB      | 
 public | p_table   | table | postgres | 0 bytes    | 
 public | p_table1  | table | postgres | 8192 bytes | 
 public | p_table10 | table | postgres | 0 bytes    | 
 public | p_table2  | table | postgres | 0 bytes    | 
 public | p_table3  | table | postgres | 0 bytes    | 
 public | p_table4  | table | postgres | 0 bytes    | 
 public | p_table5  | table | postgres | 0 bytes    | 
 public | p_table6  | table | postgres | 0 bytes    | 
 public | p_table7  | table | postgres | 0 bytes    | 
 public | p_table8  | table | postgres | 0 bytes    | 
 public | p_table9  | table | postgres | 0 bytes    | 
 public | tt        | table | postgres | 16 kB      | 
(13 rows)
 数据插入了p_table1,大小8k,以为postgresql的block是8k。                                                           
                                                                                                                                                                                                                       
                                                                                                                                               

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/582521/viewspace-1868291/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-03-26

  • 博文量
    7
  • 访问量
    28442