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

总结:

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

相关推荐
[听得时光枕水眠]2 分钟前
MySQL基础(三)DQL(Data Query Language,数据查询语言)
数据库·mysql·oracle
我科绝伦(Huanhuan Zhou)3 分钟前
深入解析Oracle SQL调优健康检查工具(SQLHC):从原理到实战优化
数据库·sql·oracle
咸鱼求放生1 小时前
es在Linux安装
大数据·elasticsearch·搜索引擎
人大博士的交易之路3 小时前
今日行情明日机会——20250606
大数据·数学建模·数据挖掘·数据分析·涨停回马枪
神奇侠20244 小时前
Hive SQL常见操作
hive·hadoop·sql
一只叫煤球的猫4 小时前
MySQL 8.0 SQL优化黑科技,面试官都不一定知道!
后端·sql·mysql
寒山李白4 小时前
MySQL安装与配置详细讲解
数据库·mysql·配置安装
多多*4 小时前
微服务网关SpringCloudGateway+SaToken鉴权
linux·开发语言·redis·python·sql·log4j·bootstrap
文牧之4 小时前
PostgreSQL 的扩展pg_freespacemap
运维·数据库·postgresql
deriva5 小时前
某水表量每15分钟一报,然后某天示数清0了,重新报示值了 ,如何写sql 计算每日水量
数据库·sql