背景

已有分区表,修改分区的范围。

例如拆分分区,合并分区。

语法如下,PG支持非常灵活的分区布局,看本文提到的HASH分区拆分,支持任意层级的分区,支持每个分区的层级深度不一样。特别适合某些数据分布不均匀的情况。例如id=1落在同一个分区但是数据量非常庞大,可以对这个分区再进行二级分区(使用其他分区方法,其他字段都可以,非常灵活)。

ALTER TABLE [ IF EXISTS ] name  
    ATTACH PARTITION partition_name { FOR VALUES partition_bound_spec | DEFAULT }  
ALTER TABLE [ IF EXISTS ] name  
    DETACH PARTITION partition_name
​
and partition_bound_spec is:
​
IN ( partition_bound_expr [, ...] ) |  
FROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )  
  TO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |  
WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )

拆分已有分区例子(hash)

1、创建一个哈希分区表,4个分区

postgres=# create table t_hash (id int , info text) PARTITION BY hash (id);  
CREATE TABLE  
postgres=# create table t_hash0 partition of t_hash FOR VALUES with (MODULUS 4, REMAINDER 0);  
CREATE TABLE  
postgres=# create table t_hash1 partition of t_hash FOR VALUES with (MODULUS 4, REMAINDER 1);  
CREATE TABLE  
postgres=# create table t_hash2 partition of t_hash FOR VALUES with (MODULUS 4, REMAINDER 2);  
CREATE TABLE  
postgres=# create table t_hash3 partition of t_hash FOR VALUES with (MODULUS 4, REMAINDER 3);  
CREATE TABLE

2、查看分区表

postgres=# \d+ t_hash  
                             Partitioned table "public.t_hash"  
 Column |  Type   | Collation | Nullable | Default | Storage  | Stats target | Description   
--------+---------+-----------+----------+---------+----------+--------------+-------------  
 id     | integer |           |          |         | plain    |              |   
 info   | text    |           |          |         | extended |              |   
Partition key: HASH (id)  
Partitions: t_hash0 FOR VALUES WITH (modulus 4, remainder 0),  
            t_hash1 FOR VALUES WITH (modulus 4, remainder 1),  
            t_hash2 FOR VALUES WITH (modulus 4, remainder 2),  
            t_hash3 FOR VALUES WITH (modulus 4, remainder 3)

3、插入一些记录

postgres=# insert into t_hash select generate_series(1,10);  
INSERT 0 10

4、查看每一条记录属于哪个分区

postgres=# select tableoid::regclass,* from t_hash;  
 tableoid | id | info   
----------+----+------  
 t_hash0  |  1 |   
 t_hash1  |  3 |   
 t_hash1  |  5 |   
 t_hash1  |  8 |   
 t_hash1  |  9 |   
 t_hash2  |  2 |   
 t_hash3  |  4 |   
 t_hash3  |  6 |   
 t_hash3  |  7 |   
 t_hash3  | 10 |   
(10 rows)

5、将1号分区拆分为2个分区。按8取模,把1号分区拆分成一个分区表(即1号分区被一个耳机分区表代替。而其他分区是直接的分区表,所以看起来就像一颗非平衡树)

4(0) , 4(1) , 4(2) , 4(3)
​
拆分为
​
4(0) , 4(1){8(1) , 8(5)} , 4(2) , 4(3)

解绑分区

postgres=# alter table t_hash DETACH PARTITION t_hash1;  
ALTER TABLE

创建二级分区

postgres=# create table t_hash1_subp (id int, info text) PARTITION BY hash (id);;  
CREATE TABLE  
postgres=# create table t_hash1_subp1 partition of t_hash1_subp FOR VALUES with (MODULUS 8, REMAINDER 1);  
CREATE TABLE  
postgres=# create table t_hash1_subp5 partition of t_hash1_subp FOR VALUES with (MODULUS 8, REMAINDER 5);  
CREATE TABLE

绑定二级分区到一级分区。

postgres=# alter table t_hash attach partition t_hash1_subp FOR VALUES WITH ( MODULUS 4, REMAINDER 1 );  
ALTER TABLE

将原来子分区的数据写入新的二级分区表(数据迁移)

postgres=# insert into t_hash1_subp select * from t_hash1;  
INSERT 0 4

查看记录,并列出记录所在分区名

postgres=# select tableoid::regclass,* from t_hash;  
   tableoid    | id | info   
---------------+----+------  
 t_hash0       |  1 |   
 t_hash1_subp1 |  3 |   
 t_hash1_subp5 |  5 |   
 t_hash1_subp5 |  8 |   
 t_hash1_subp5 |  9 |   
 t_hash2       |  2 |   
 t_hash3       |  4 |   
 t_hash3       |  6 |   
 t_hash3       |  7 |   
 t_hash3       | 10 |   
(10 rows)

列出非平衡的分区表

postgres=# \d+ t_hash  
                             Partitioned table "public.t_hash"  
 Column |  Type   | Collation | Nullable | Default | Storage  | Stats target | Description   
--------+---------+-----------+----------+---------+----------+--------------+-------------  
 id     | integer |           |          |         | plain    |              |   
 info   | text    |           |          |         | extended |              |   
Partition key: HASH (id)  
Partitions: t_hash0 FOR VALUES WITH (modulus 4, remainder 0),  
            t_hash1_subp FOR VALUES WITH (modulus 4, remainder 1), PARTITIONED,  
            t_hash2 FOR VALUES WITH (modulus 4, remainder 2),  
            t_hash3 FOR VALUES WITH (modulus 4, remainder 3)

扩展阅读,甚至我们可以把其他分区改成别的分区方法,例如将t_hash2改成list分区

postgres=# alter table t_hash detach partition t_hash2;
ALTER TABLE
​
postgres=# create table t_hash2_subp (id int, info text) partition by list (info);
CREATE TABLE
postgres=# create table t_hash2_supb1 partition of t_hash2_subp FOR VALUES in ('hello');
CREATE TABLE
postgres=# create table t_hash2_supb2 partition of t_hash2_subp FOR VALUES in ('abc','cde');
CREATE TABLE
postgres=# create table t_hash2_supb3 partition of t_hash2_subp DEFAULT;
CREATE TABLE
​
postgres=# alter table t_hash attach partition t_hash2_subp for values with (modulus 4, remainder 2);
ALTER TABLE
​
postgres=# \d+ t_hash
                             Partitioned table "public.t_hash"
 Column |  Type   | Collation | Nullable | Default | Storage  | Stats target | Description 
--------+---------+-----------+----------+---------+----------+--------------+-------------
 id     | integer |           |          |         | plain    |              | 
 info   | text    |           |          |         | extended |              | 
Partition key: HASH (id)
Partitions: t_hash0 FOR VALUES WITH (modulus 4, remainder 0),
            t_hash1_subp FOR VALUES WITH (modulus 4, remainder 1), PARTITIONED,
            t_hash2_subp FOR VALUES WITH (modulus 4, remainder 2), PARTITIONED,
            t_hash3 FOR VALUES WITH (modulus 4, remainder 3)
​
postgres=# insert into t_hash select id,'abc' from t_hash2;
INSERT 0 1
postgres=# insert into t_hash select id,'def' from t_hash2;
INSERT 0 1
postgres=# insert into t_hash select id,'hello' from t_hash2;
INSERT 0 1
​
postgres=# select tableoid::regclass,* from t_hash;
   tableoid    | id | info  
---------------+----+-------
 t_hash0       |  1 | 
 t_hash1_subp1 |  3 | 
 t_hash1_subp5 |  5 | 
 t_hash1_subp5 |  8 | 
 t_hash1_subp5 |  9 | 
 t_hash2_supb2 |  2 | abc
 t_hash2_supb1 |  2 | hello
 t_hash2_supb3 |  2 | def
 t_hash3       |  4 | 
 t_hash3       |  6 | 
 t_hash3       |  7 | 
 t_hash3       | 10 | 
(12 rows)

拆分已有分区例子(range)

与之类似,无非就是partition_bound_spec的用法不一样,略。

and partition_bound_spec is:
​
IN ( partition_bound_expr [, ...] ) |  
FROM ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] )  
  TO ( { partition_bound_expr | MINVALUE | MAXVALUE } [, ...] ) |  
WITH ( MODULUS numeric_literal, REMAINDER numeric_literal )

合并已有分区例子(hash)

与之类似,略

合并已有分区例子(range)

与之类似,略

小结

通过attach, detach支持分区的拆分,合并。PG支持非平衡分区表,深度不一定一致。例如本文,

4(0) , 4(1) , 4(2) , 4(3)
​
拆分为
​
4(0) , 4(1){8(1) , 8(5)} , 4(2) , 4(3)

参考

https://www.postgresql.org/docs/12/sql-altertable.html

CENTER_PostgreSQL_Community

请在登录后发表评论,否则无法保存。
1楼 xcvxcvsdf
2024-11-25 19:17:15+08

http://shengshun.njtcbmw.cn/huzhou/ http://huaguang.jxtcbmw.cn/yongzhou/ http://bjtcxxw.cn/shongjiangqu/ http://bjtcxxw.cn/sjzpw/ https://szwujiang.tiancebbs.cn/ http://bjtcxxw.cn/ershou/ http://jinqiang.ahtcbmw.cn/qhhd/ http://js.sytcxxw.cn/fujian/ http://wogao.ahtcbmw.cn/chongzuo/ http://km.lstcxxw.cn/zaozhuang/ http://fs.shtcxxw.cn/bayinguoleng/ http://huaguang.jxtcbmw.cn/yangpuqu/ http://huaguang.jxtcbmw.cn/zhenjiang/ http://nalei.zjtcbmw.cn/sxyc/ http://fuyang.tjtcbmw.cn/hljdxal/ http://ly.shtcxxw.cn/kelamayi/ https://myqchengbei.tiancebbs.cn/

2楼 xcvxcvsdf
2024-10-24 13:13:26+08

http://taiying.njtcbmw.cn/qingpush/ https://changyang.tiancebbs.cn/ http://gx.lztcxxw.cn/zhq/ http://js.sytcxxw.cn/ywyzp/ http://huaguang.jxtcbmw.cn/huanggang/ http://fuyang.tjtcbmw.cn/xjcj/ http://nalei.zjtcbmw.cn/sdly/ http://ouyu.hftcbmw.cn/ahla/ http://nalei.zjtcbmw.cn/shhongkou/ http://fuyang.tjtcbmw.cn/luzhou/ http://tuiguang.hntcxxw.cn/zhaog/ http://yz.cqtcxxw.cn/nanjing/ http://fuyang.tjtcbmw.cn/xzx/ http://ruanwen.xztcxxw.cn/kashi/ http://cf.lstcxxw.cn/handan/ http://jinqiang.ahtcbmw.cn/gczp/ http://yuanbang.tjtcbmw.cn/xilinguolemeng/

3楼 xiaowu
2024-04-24 10:46:31+08

l是火线还是零线:https://www.nanss.com/wenti/20390.html 信息技术教学反思:https://www.nanss.com/xuexi/19166.html 议论文怎么写:https://www.nanss.com/xuexi/18186.html 商朝的帝王顺序:https://www.nanss.com/shenghuo/18177.html 宴会菜单:https://www.nanss.com/yinshi/20402.html 水浒传人物性格特点:https://www.nanss.com/shenghuo/20378.html 怼是什么意思:https://www.nanss.com/wenti/19966.html 佛珠材质:https://www.nanss.com/shenghuo/20420.html 提示语:https://www.nanss.com/yulu/20497.html 杨善洲精神:https://www.nanss.com/gongzuo/19069.html 有教育意义的电影:https://www.nanss.com/shenghuo/19124.html 自我调节情绪的方法:https://www.nanss.com/shenghuo/20380.html 元旦主持词:https://www.nanss.com/xuexi/20465.html 土家族风俗:https://www.nanss.com/shenghuo/19387.html 结婚一周年:https://www.nanss.com/shenghuo/20416.html 父母催婚:https://www.nanss.com/shenghuo/20407.html 梦见和丈夫离婚:https://www.nanss.com/xingzuo/19767.html 年终晚会方案:https://www.nanss.com/gongzuo/19094.html 鸡蛋壳有什么用:https://www.nanss.com/yinshi/18271.html 日记一天一篇100字:https://www.nanss.com/xuexi/20262.html 质押合同:https://www.nanss.com/gongzuo/20318.html 职业生涯规划前言:https://www.nanss.com/gongzuo/18662.html 劳动的意义与重要性:https://www.nanss.com/shenghuo/20377.html 最能打动女人心的情话:https://www.nanss.com/yulu/20188.html 项目可行性报告模板:https://www.nanss.com/gongzuo/20101.html 龙凤胎取名:https://www.nanss.com/mingcheng/20580.html 市场推广方案:https://www.nanss.com/gongzuo/18643.html 冰心原名什么:https://www.nanss.com/shenghuo/18502.html 花形容词:https://www.nanss.com/xuexi/20278.html 每日一签语录:https://www.nanss.com/yulu/20484.html

© 2010 PostgreSQL中文社区