表分区简介1

目录

文档用途

详细信息

文档用途

了解Postgresql表分区

详细信息

Postgreslq内核中支持表分区(Table partitioning)包括:范围(range)、列表(list)

PostgreSQL offers built-in support for the following forms of partitioning:

Range Partitioning

The table is partitioned into “ranges” defined by a key column or set of columns, with no overlap between the ranges of values assigned to different partitions. For example, one might partition by date ranges, or by ranges of identifiers for particular business objects.

List Partitioning

The table is partitioned by explicitly listing which key values appear in each partition.

If your application needs to use other forms of partitioning not listed above, alternative methods such as inheritance and UNION ALL views can be used instead. Such methods offer flexibility but do not have some of the performance benefits of built-in declarative partitioning.

事例:

1、范围分区

create table t_range(id int,name varchar(100),i_time timestamp not null) partition by range(id);
postgres=# \d+ t_range

create table t_range_1 partition of t_range for values from (1) to (1000);

create table t_range_2 partition of t_range for values from (1000) to (3000);

create table t_range_3 partition of t_range for values from (3000) to (5000);

create table t_range_4 partition of t_range for values from (5000) to (8000);

create table t_range_5 partition of t_range for values from (8000) to (10000);

postgres=# \d+ t_range

insert into t_range select id,md5(random()::text),current_date - id from generate_series(1,10001) t(id);

postgres=> insert into t_range select id,md5(random()::text),current_date - id from generate_series(1,10001) t(id);

ERROR:  no partition of relation "t_range" found for row

DETAIL:  Partition key of the failing row contains (id) = (10000).

postgres=>

INSERT INTO t_range SELECT generate_series(1,9999),md5(random()::text),clock_timestamp();

select count(*) from t_range_1;

select count(*) from t_range_5;

select count(*) from only t_range;

不允许分区字段修改后跨越分区

postgres=# update t_range set id=2000 where id=500;

ERROR:  new row for relation "t_range_1" violates partition constraint

DETAIL:  Failing row contains (2000, 7ed9bf07d00e369c74997bee774cea69, 2017-03-24 00:00:00).

postgres=# update t_range set id=200 where id=500;

UPDATE 1

非分区字段的字段修改操作会下发到子表上。

truncate主表

postgres=# truncate table t_range;

TRUNCATE TABLE

postgres=# select count(*) from t_range_5;

 count

-------

     0

(1 row)

会清除所有子表的数据。

2、list 分区

创建主表

create table t_list (id int,name varchar(100),region varchar(50) ) partition by list (region);

create table t_list_1 partition of t_list for values in ('beijing');

create table t_list_2 partition of t_list for values in ('nanjing');

create table t_list_3 partition of t_list for values in ('shanghai');

create table t_list_4 partition of t_list for values in ('chongqing');

create table t_list_5 partition of t_list for values in ('hangzhou');

postgres=# \d+ t_list

insert into t_list values (1,'a','beijing');

insert into t_list values (2,'b','nanjing');

insert into t_list values (3,'c','chongqing');

insert into t_list values (4,'d','hangzhou');

insert into t_list values (5,'e','shanghai');

insert into t_list values (6,'f','chongqing');

 insert into t_list values (7,'g','nanjing');

insert into t_list values (8,'h','beijing');

更多详细信息请登录【瀚高技术支持平台】 查看

发布了399 篇原创文章 · 获赞 108 · 访问量 65万+

猜你喜欢

转载自blog.csdn.net/pg_hgdb/article/details/103730465
今日推荐