oracle pgsql分区表,postgresql分区表的使用

本文介绍了在PostgreSQL中使用表继承实现分区表的方法,包括创建父表和多个子分区表,以及通过触发器根据条件将数据插入合适的分区。实验展示了如何设置触发器以根据namespace值将数据分配到不同的分区,并验证了数据插入的效果。
摘要由CSDN通过智能技术生成

在看《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博客 ” ,链接:http://blog.itpub.net/582521/viewspace-1868291/,如需转载,请注明出处,否则将追究法律责任。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值