Greenplum多级分区表添加分区报错ERROR: no partitions specified at depth 2

一般来说,我们二级分区表都会使用模版,如果没有使用模版特性,那么就会报ERROR: no partitions specified at depth 2类似的错误。因为没有模版,必须要显式指定分区。

当然我们在建表的时候,如果没有指定,那么后面也可以通过alter table 语句进行添加。下面我们通过一个例子看一下。

sql 复制代码
CREATE TABLE sales (
    trans_id integer,
    ctime timestamp without time zone,
    region text
)
 DISTRIBUTED BY (trans_id) PARTITION BY RANGE(ctime)
          SUBPARTITION BY LIST(region)
          (
          START ('2023-08-18 00:00:00'::timestamp without time zone) END ('2023-08-19 00:00:00'::timestamp without time zone) WITH (tablename='sales_1_prt_1', appendonly='false')
                  (
                  SUBPARTITION usa VALUES('usa') WITH (tablename='sales_1_prt_1_2_prt_usa', appendonly='false'),
                  SUBPARTITION asia VALUES('asia') WITH (tablename='sales_1_prt_1_2_prt_asia', appendonly='false'),
                  SUBPARTITION europe VALUES('europe') WITH (tablename='sales_1_prt_1_2_prt_europe', appendonly='false')
                  )
          );

#如果只指定一级分区,添加分区会报错
alter table sales add partition sale_3  start (date '2023-08-20') inclusive end (date '2023-08-21') exclusive ;
NOTICE:  CREATE TABLE will create partition "sales_1_prt_sale_3" for table "sales"
ERROR:  no partitions specified at depth 2
#必须显式指定才可以添加成功
alter table sales add partition sale_2 
START ('2023-08-19 00:00:00'::timestamp without time zone) END ('2023-08-20 00:00:00'::timestamp without time zone) 
             (
                  SUBPARTITION usa VALUES('usa'),
                  SUBPARTITION asia VALUES('asia') ,
                  SUBPARTITION europe VALUES('europe') 
                );
#插入一些测试数据
insert into sales select generate_series(1,10000000) ,current_date ,'usa';
insert into sales select generate_series(1,10000000) ,current_date ,'asia';
insert into sales select generate_series(1,10000000) ,current_date ,'europe';

#添加模版,就算之前有历史数据,也是瞬间完成
ALTER TABLE sales
SET SUBPARTITION TEMPLATE
          (
          SUBPARTITION usa VALUES('usa') WITH (tablename='sales'),
          SUBPARTITION asia VALUES('asia') WITH (tablename='sales'),
          SUBPARTITION europe VALUES('europe') WITH (tablename='sales')
          )
;
NOTICE:  adding level 1 subpartition template specification for relation "sales"
NOTICE:  CREATE TABLE will create partition "sales_1_prt_subpartition_template" for table "sales"NOTICE:  CREATE TABLE will create partition "sales" for table "sales_1_prt_subpartition_template"
NOTICE:  CREATE TABLE will create partition "sales" for table "sales_1_prt_subpartition_template"
NOTICE:  CREATE TABLE will create partition "sales" for table "sales_1_prt_subpartition_template"
ALTER TABLE

#这次只指定一级分区就可以添加成功
alter table sales  add partition sale_3  start (date '2023-08-20') inclusive end (date '2023-08-21') exclusive ;
NOTICE:  CREATE TABLE will create partition "sales_1_prt_sale_2" for table "sales"
NOTICE:  CREATE TABLE will create partition "sales_1_prt_sale_2_2_prt_usa" for table "sales_1_prt_sale_2"
NOTICE:  CREATE TABLE will create partition "sales_1_prt_sale_2_2_prt_asia" for table "sales_1_prt_sale_2"
NOTICE:  CREATE TABLE will create partition "sales_1_prt_sale_2_2_prt_europe" for table "sales_1_prt_sale_2"
ALTER TABLE

总结:

建表的时候,最好添加二级分区以后的模版,模版也可以后面变更,如果不加模版,添加分区的时候,必须指定子分区,所以分区级别越多,越复杂。

相关推荐
小北方城市网1 分钟前
数据库性能优化实战指南:从索引到架构,根治性能瓶颈
数据结构·数据库·人工智能·性能优化·架构·哈希算法·散列表
TDengine (老段)3 分钟前
TDengine Go 语言连接器进阶指南
大数据·数据库·物联网·golang·时序数据库·tdengine·涛思数据
~央千澈~3 分钟前
从阅文招聘JD看网文平台算法化-网文平台拥抱科技·卓伊凡
大数据·人工智能
房产中介行业研习社4 分钟前
2026年1月房产中介管理系统哪家好用
大数据·人工智能
deepdata_cn5 分钟前
零售门店:浅数据看客流,大数据看区域,深数据挖消费动机
大数据·零售·深数据·浅数据
何中应6 分钟前
使用Spring自带的缓存注解维护数据一致性
java·数据库·spring boot·后端·spring·缓存
ZeroToOneDev6 分钟前
Mybatis
java·数据库·mybatis
野犬寒鸦12 分钟前
从零起步学习RabbitMQ || 第一章:认识消息队列及项目实战中的技术选型
java·数据库·后端
xiatianxy13 分钟前
登高作业安全难题如何破?
大数据·人工智能·科技·物联网·安全·智能安全带
枫叶丹415 分钟前
【Qt开发】Qt系统(六)-> Qt 线程安全
c语言·开发语言·数据库·c++·qt·安全