学习目标
学习openGauss分区表
分区表是把逻辑上的一张表根据某种方案分成几张物理块进行存储,这张逻辑上的表称之为分区表,物理块称之为分区。
分区表是一张逻辑表,不存储数据,数据实际是存储在分区上的。
课程学习
连接openGauss
#第一次进入等待15秒
#数据库启动中...
su - omm
gsql -r
1.创建分区表
–范围分区表
create table update_table
(
c1 int,
c2 CHAR(2)
)
partition by range (c1)
(
partition update_table_p0 values less than (50),
partition update_table_p1 values less than (100),
partition update_table_p2 values less than (150)
);
–查看分区表信息
\d+ update_table;
select * from pg_partition;
–插入数据
insert into update_table values (1, ‘a’), (50, ‘b’), (100, ‘c’);
–超出范围的数据会报错
insert into update_table values (150, ‘d’);

–查看各区上的数据
select * from update_table partition(update_table_p0);2.创建分区
alter table update_table add partition update_table_p3 values less than (200);
insert into update_table values (150, ‘d’);3.修改分区属性
alter table update_table rename partition update_table_p1 to update_table_p1_1;4.删除分区
alter table update_table drop partition update_table_p0;5.删除分区表
drop table update_table;

课程作业
1.创建一个含有5个分区的范围分区表store,在每个分区中插入记录
-- 1.
create table store
(
c1 int,
c2 char(2)
)
partition by range (c1)
(
partition p0 values less than (10),
partition p1 values less than (20),
partition p2 values less than (30),
partition p3 values less than (40),
partition p4 values less than (50)
)
;
insert into store values (1, 'a'), (11, 'b'), (21, 'c'), (31, 'd'), (41, 'e');
\d+ store
2.查看分区1上的数据
-- 2.
select * from store partition(p0);

3.重命名分区2
-- 3.
alter table store rename partition p1 to p1_1;
\d+ store
select * from pg_partition;

4.删除分区5
-- 4.
alter table store drop partition p4;
5.增加分区6
-- 5.
alter table store add partition p5 values less than (60);

6.在系统表pg_partition中查看分区信息
-- 6.
select * from pg_partition;
7.删除分区表
-- 7.
drop table store;

学习心得
从本节课开始,除了要熟悉各种元命令外,还需要开始学习系统表,比如本文涉及的PG_PARTITION。
参考文档
openGauss产品文档 – PG_PARTITION
openGauss每日一练 | by 少安 | 汇总
松鼠镇楼,按时打卡





