分區表意思是將一個大表在物理上分割成幾塊,GPDB中的分區表和PostgreSQL中實現原理同樣,都是用過表繼承、約束來實現。可是與PostgreSQL也有所不一樣,在PostgreSQL中,一個父表,多個子表來實現分區表,須要手動向子表插入數據,若是向父表插入數據,則直接會被插入到父表中,在GPDB中,能夠直接想父表插入數據,即可以根據約束直接自動向對應的子表插入數據,當分區子表不存在時,插入失敗html
[PARTITION BY partition_type (column) [SUBPARTITION BY partition_type (column)] [SUBPARTITION TEMPLATE (template_spec)] [...] (partition_spec) |[SUBPARTITION BY partition_type(column)] [...] (partition_spec) [(subpartition_spec [(...)] )] ] and partition_element is: DEFAULT PARTITION name | [PARTITION name] VALUES (list_value[,...]) | [PARTITION name] START ([datatype] 'start_value') [INCLUSIVE|EXCLUSIVE] [ END ([datatype] 'end_value') [INCLUSIVE|EXCLUSIVE] [ EVERY ([datatype] [number|INTERVAL] 'interval_value')] | [PARTITION name] END ([DATATYPE] 'end_value') [INCLUSIVE|EXCLUSIVE] [ EVERY ([datatype] [number|INTERVAL] 'interval_value')] [ with (partition_storage_parameter=value [,...])] [ TABLESPACE tablespace]
create table public.test_partition_range( id numeric, name character varying(32), dw_end_date date ) distributed by (id) PARTITION BY range(dw_end_date) ( PARTITION p20201230 START ('2020-12-30'::date) END ('2020-12-31'::date), PARTITION p20201231 START ('2020-12-31'::date) END ('2021-01-01'::date), PARTITION p20210101 START ('2021-01-01'::date) END ('2021-01-02'::date), PARTITION p20210102 START ('2021-01-02'::date) END ('2021-01-03'::date), PARTITION p20210103 START ('2021-01-03'::date) END ('2021-01-04'::date), PARTITION p20210104 START ('2021-01-04'::date) END ('2021-01-05'::date), PARTITION p20210105 START ('2021-01-05'::date) END ('2021-01-06'::date) ); create table test_partition_range1 ( id int, name varchar(64), fdate varchar(64) ) distributed by (id) partition by range(fdate) ( partition p1 start ('2017-01-01') inclusive end ('2017-01-31') exclusive, partition p2 start ('2017-02-01') inclusive end ('2017-02-29') exclusive, default partition default_p ); create table test_partition_range ( id int, name varchar(64), fdate varchar(64) ) distributed by (id) partition by range(fdate) ( partition p1 start ('2017-01-01') inclusive end ('2017-01-31') exclusive, partition p2 start ('2017-02-01') inclusive end ('2017-02-29') exclusive, default partition default_p ); inclusive :指定包含,例如上面的 start ('2017-01-01') inclusive 則是包含'2017-01-01' exclusive : 指定不包含, 例如上面的 end ('2017-01-31') exclusive 則是不包含'2017-01-31'
執行結果顯示git
testDB=# create table public.test_partition_range( testDB(# id numeric, testDB(# name character varying(32), testDB(# dw_end_date date testDB(# ) distributed by (id) testDB-# PARTITION BY range(dw_end_date) testDB-# ( testDB(# PARTITION p20201230 START ('2020-12-30'::date) END ('2020-12-31'::date), testDB(# PARTITION p20201231 START ('2020-12-31'::date) END ('2021-01-01'::date), testDB(# PARTITION p20210101 START ('2021-01-01'::date) END ('2021-01-02'::date), testDB(# PARTITION p20210102 START ('2021-01-02'::date) END ('2021-01-03'::date), testDB(# PARTITION p20210103 START ('2021-01-03'::date) END ('2021-01-04'::date), testDB(# PARTITION p20210104 START ('2021-01-04'::date) END ('2021-01-05'::date), testDB(# PARTITION p20210105 START ('2021-01-05'::date) END ('2021-01-06'::date) testDB(# ); NOTICE: CREATE TABLE will create partition "test_partition_range_1_prt_p20201230" for table "test_partition_range" NOTICE: CREATE TABLE will create partition "test_partition_range_1_prt_p20201231" for table "test_partition_range" NOTICE: CREATE TABLE will create partition "test_partition_range_1_prt_p20210101" for table "test_partition_range" NOTICE: CREATE TABLE will create partition "test_partition_range_1_prt_p20210102" for table "test_partition_range" NOTICE: CREATE TABLE will create partition "test_partition_range_1_prt_p20210103" for table "test_partition_range" NOTICE: CREATE TABLE will create partition "test_partition_range_1_prt_p20210104" for table "test_partition_range" NOTICE: CREATE TABLE will create partition "test_partition_range_1_prt_p20210105" for table "test_partition_range" CREATE TABLE testDB=# create table test_partition_range1 testDB-# ( testDB(# id int, testDB(# name varchar(64), testDB(# fdate varchar(64) testDB(# ) distributed by (id) testDB-# partition by range(fdate) testDB-# ( testDB(# partition p1 start ('2017-01-01') inclusive end ('2017-01-31') exclusive, testDB(# partition p2 start ('2017-02-01') inclusive end ('2017-02-29') exclusive, testDB(# default partition default_p testDB(# ); NOTICE: CREATE TABLE will create partition "test_partition_range1_1_prt_default_p" for table "test_partition_range1" NOTICE: CREATE TABLE will create partition "test_partition_range1_1_prt_p1" for table "test_partition_range1" NOTICE: CREATE TABLE will create partition "test_partition_range1_1_prt_p2" for table "test_partition_range1" CREATE TABLE
create table public.test_partition_every( id numeric, name character varying(32), dw_end_date date ) distributed by(id) partition by range(dw_end_Date) ( partition p202012 start('2020-12-1'::date) end ('2021-12-31'::date) every ('1 days'::interval) ); eg:根據選定的範圍,跨越基數,快速分區每個子表 create table test_partition_every_1 ( id int, name varchar(64), fdate date ) distributed by (id) partition by range (fdate) ( partition pn_ start ('2017-01-01'::date) end ('2017-12-31'::date) every ('1 day'::interval), default partition default_p ); every:指定跨越基數
執行結果顯示github
testDB=# create table public.test_partition_every( testDB(# id numeric, testDB(# name character varying(32), testDB(# dw_end_date date testDB(# ) distributed by(id) testDB-# partition by range(dw_end_Date) testDB-# ( testDB(# partition p202012 start('2020-12-1'::date) end ('2020-12-31'::date) testDB(# every ('1 days'::interval) testDB(# ); NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_1" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_2" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_3" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_4" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_5" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_6" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_7" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_8" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_9" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_10" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_11" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_12" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_13" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_14" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_15" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_16" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_17" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_18" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_19" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_20" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_21" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_22" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_23" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_24" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_25" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_26" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_27" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_28" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_29" for table "test_partition_every" NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p202012_30" for table "test_partition_every" CREATE TABLE
根據值的分組,相同的數據歸類到一組,也就一個分區中 create table public.test_partition_list( member_id numeric, city character varying(32) )distributed by (member_id) partition by list(city) ( partition guangzhou values('guangzhou'), partition hangzhou values('hangzhou'), default partition other_city ); create table test_partition_list ( id int, name varchar(64), fdate varchar(10) ) distributed by (id) partition by list (fdate) ( partition p1 values ('2017-01-01', '2017-01-02'), partition p2 values ('2017-01-03'), default partition pd );
執行結果顯示sql
testDB=# create table test_partition_range testDB-# ( testDB(# id int, testDB(# name varchar(64), testDB(# fdate varchar(64) testDB(# ) distributed by (id) testDB-# partition by range(fdate) testDB-# ( testDB(# partition p1 start ('2017-01-01') inclusive end ('2017-01-31') exclusive, testDB(# partition p2 start ('2017-02-01') inclusive end ('2017-02-29') exclusive, testDB(# default partition default_p testDB(# ); NOTICE: CREATE TABLE will create partition "test_partition_range_1_prt_default_p" for table "test_partition_range" NOTICE: CREATE TABLE will create partition "test_partition_range_1_prt_p1" for table "test_partition_range" NOTICE: CREATE TABLE will create partition "test_partition_range_1_prt_p2" for table "test_partition_range" CREATE TABLE
ALTER DEFAULT PARTITION DROP DEFAULT PARTITION [IF EXISTS] DROP PARTITION [IF EXISTS] { partition_name | FOR (RANK(number)) | FOR (value) } [CASCADE] TRUNCATE DEFAULT PARTITION TRUNCATE PARTITION { partition_name | FOR (RANK(number)) | FOR (value) } RENAME DEFAULT PARTITION TO new_partition_name RENAME PARTITION { partition_name | FOR (RANK(number)) | FOR (value) } TO new_partition_name ADD DEFAULT PARTITION NAME [(subpartition_spec)] ADD PARTITION [name] partition_element [(subpartition_spec)] EXCHANGE PARTITION { partition_name | FOR (RANK(number)) | FOR (value) } WITH TABLE TABLE_NAME [WITH|WITHOUT VALIDATION] EXCHANGE EFAULT PARTITION WITH TABLE TABLE_NAME [WITH|WITHOUT VALIDATION] SET SUBPARTITION TEMPLATE (subpartition_spec) SPLIT DEFAULT PARTITION { AT (list_value) |START([datatype] range_value) [INCLUSIVE|EXCLUSIVE] END ([datatype]) range_value) [INCLUSIVE|EXCLUSIVE] } [INTO (PARTITION new_partition_name, PARTITION default_partition_name)] SPLIT DPARTITION { partition_name | FOR (RANK(number)) | FOR (value) } AT(value) [INTO (PARTITION partition_name, PARTITION partition_name]
alter table public.test_partition_every add partition p20210205_6 start ('2012-02-05'::date) END ('2012-01-07'::date); alter table public.test_partition_every drop partition p20120105_6; alter table public.test_partition_every truncate partition p20120205_6;
添加分區操做步驟ide
testDB=# alter table public.test_partition_every add partition p20210105_6 start ('2012-01-05'::date) END ('2012-01-07'::date); NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p20210105_6" for table "test_partition_every" ALTER TABLE testDB=# select gp_segment_id,tableoid::regclass,count(*) from test_partition_every group by 1,2 order by 1,2; gp_segment_id | tableoid | count ---------------+----------+------- (0 rows) testDB=# \d+ public.test_partition_every Table "public.test_partition_every" Column | Type | Modifiers | Storage | Stats target | Description -------------+-----------------------+-----------+----------+--------------+------------- id | numeric | | main | | name | character varying(32) | | extended | | dw_end_date | date | | plain | | Child tables: test_partition_every_1_prt_p202012_1, test_partition_every_1_prt_p202012_10, test_partition_every_1_prt_p202012_11, test_partition_every_1_prt_p202012_12, test_partition_every_1_prt_p202012_13, test_partition_every_1_prt_p202012_14, test_partition_every_1_prt_p202012_15, test_partition_every_1_prt_p202012_16, test_partition_every_1_prt_p202012_17, test_partition_every_1_prt_p202012_18, test_partition_every_1_prt_p202012_19, test_partition_every_1_prt_p202012_2, test_partition_every_1_prt_p202012_20, test_partition_every_1_prt_p202012_21, test_partition_every_1_prt_p202012_22, test_partition_every_1_prt_p202012_23, test_partition_every_1_prt_p202012_24, test_partition_every_1_prt_p202012_25, test_partition_every_1_prt_p202012_26, test_partition_every_1_prt_p202012_27, test_partition_every_1_prt_p202012_28, test_partition_every_1_prt_p202012_29, test_partition_every_1_prt_p202012_3, test_partition_every_1_prt_p202012_30, test_partition_every_1_prt_p202012_31, test_partition_every_1_prt_p202012_32, test_partition_every_1_prt_p202012_33, test_partition_every_1_prt_p202012_34, test_partition_every_1_prt_p202012_35, test_partition_every_1_prt_p202012_36, test_partition_every_1_prt_p202012_37, test_partition_every_1_prt_p202012_38, test_partition_every_1_prt_p202012_39, test_partition_every_1_prt_p202012_4, test_partition_every_1_prt_p202012_40, test_partition_every_1_prt_p202012_41, test_partition_every_1_prt_p202012_42, test_partition_every_1_prt_p202012_43, test_partition_every_1_prt_p202012_44, test_partition_every_1_prt_p202012_45, test_partition_every_1_prt_p202012_46, test_partition_every_1_prt_p202012_47, test_partition_every_1_prt_p202012_48, test_partition_every_1_prt_p202012_49, test_partition_every_1_prt_p202012_5, test_partition_every_1_prt_p202012_50, test_partition_every_1_prt_p202012_51, test_partition_every_1_prt_p202012_52, test_partition_every_1_prt_p202012_53, test_partition_every_1_prt_p202012_54, test_partition_every_1_prt_p202012_55, test_partition_every_1_prt_p202012_56, test_partition_every_1_prt_p202012_57, test_partition_every_1_prt_p202012_58, test_partition_every_1_prt_p202012_59, test_partition_every_1_prt_p202012_6, test_partition_every_1_prt_p202012_60, test_partition_every_1_prt_p202012_61, test_partition_every_1_prt_p202012_7, test_partition_every_1_prt_p202012_8, test_partition_every_1_prt_p202012_9, test_partition_every_1_prt_p20210105_6 Distributed by: (id) Partition by: (dw_end_date) testDB=# alter table public.test_partition_every drop partition p20210105_6; ALTER TABLE testDB=# alter table public.test_partition_every add partition p20210205_6 start ('2012-02-05'::date) END ('2012-01-07'::date); ERROR: START greater than END for partition "p20210205_6" testDB=# alter table public.test_partition_every add partition p20210205_6 start ('2012-02-05'::date) END ('2012-02-07'::date); NOTICE: CREATE TABLE will create partition "test_partition_every_1_prt_p20210205_6" for table "test_partition_every" ALTER TABLE testDB=# \d+ public.test_partition_every Table "public.test_partition_every" Column | Type | Modifiers | Storage | Stats target | Description -------------+-----------------------+-----------+----------+--------------+------------- id | numeric | | main | | name | character varying(32) | | extended | | dw_end_date | date | | plain | | Child tables: test_partition_every_1_prt_p202012_1, test_partition_every_1_prt_p202012_10, test_partition_every_1_prt_p202012_11, test_partition_every_1_prt_p202012_12, test_partition_every_1_prt_p202012_13, test_partition_every_1_prt_p202012_14, test_partition_every_1_prt_p202012_15, test_partition_every_1_prt_p202012_16, test_partition_every_1_prt_p202012_17, test_partition_every_1_prt_p202012_18, test_partition_every_1_prt_p202012_19, test_partition_every_1_prt_p202012_2, test_partition_every_1_prt_p202012_20, test_partition_every_1_prt_p202012_21, test_partition_every_1_prt_p202012_22, test_partition_every_1_prt_p202012_23, test_partition_every_1_prt_p202012_24, test_partition_every_1_prt_p202012_25, test_partition_every_1_prt_p202012_26, test_partition_every_1_prt_p202012_27, test_partition_every_1_prt_p202012_28, test_partition_every_1_prt_p202012_29, test_partition_every_1_prt_p202012_3, test_partition_every_1_prt_p202012_30, test_partition_every_1_prt_p202012_31, test_partition_every_1_prt_p202012_32, test_partition_every_1_prt_p202012_33, test_partition_every_1_prt_p202012_34, test_partition_every_1_prt_p202012_35, test_partition_every_1_prt_p202012_36, test_partition_every_1_prt_p202012_37, test_partition_every_1_prt_p202012_38, test_partition_every_1_prt_p202012_39, test_partition_every_1_prt_p202012_4, test_partition_every_1_prt_p202012_40, test_partition_every_1_prt_p202012_41, test_partition_every_1_prt_p202012_42, test_partition_every_1_prt_p202012_43, test_partition_every_1_prt_p202012_44, test_partition_every_1_prt_p202012_45, test_partition_every_1_prt_p202012_46, test_partition_every_1_prt_p202012_47, test_partition_every_1_prt_p202012_48, test_partition_every_1_prt_p202012_49, test_partition_every_1_prt_p202012_5, test_partition_every_1_prt_p202012_50, test_partition_every_1_prt_p202012_51, test_partition_every_1_prt_p202012_52, test_partition_every_1_prt_p202012_53, test_partition_every_1_prt_p202012_54, test_partition_every_1_prt_p202012_55, test_partition_every_1_prt_p202012_56, test_partition_every_1_prt_p202012_57, test_partition_every_1_prt_p202012_58, test_partition_every_1_prt_p202012_59, test_partition_every_1_prt_p202012_6, test_partition_every_1_prt_p202012_60, test_partition_every_1_prt_p202012_61, test_partition_every_1_prt_p202012_7, test_partition_every_1_prt_p202012_8, test_partition_every_1_prt_p202012_9, test_partition_every_1_prt_p20210205_6 Distributed by: (id) Partition by: (dw_end_date) truncate 只會清理,不會刪除 testDB=# alter table public.test_partition_every truncate partition p20210205_6; ALTER TABLE testDB=# testDB=# \d+ public.test_partition_every Table "public.test_partition_every" Column | Type | Modifiers | Storage | Stats target | Description -------------+-----------------------+-----------+----------+--------------+------------- id | numeric | | main | | name | character varying(32) | | extended | | dw_end_date | date | | plain | | Child tables: test_partition_every_1_prt_p202012_1, test_partition_every_1_prt_p202012_10, test_partition_every_1_prt_p202012_11, test_partition_every_1_prt_p202012_12, test_partition_every_1_prt_p202012_13, test_partition_every_1_prt_p202012_14, test_partition_every_1_prt_p202012_15, test_partition_every_1_prt_p202012_16, test_partition_every_1_prt_p202012_17, test_partition_every_1_prt_p202012_18, test_partition_every_1_prt_p202012_19, test_partition_every_1_prt_p202012_2, test_partition_every_1_prt_p202012_20, test_partition_every_1_prt_p202012_21, test_partition_every_1_prt_p202012_22, test_partition_every_1_prt_p202012_23, test_partition_every_1_prt_p202012_24, test_partition_every_1_prt_p202012_25, test_partition_every_1_prt_p202012_26, test_partition_every_1_prt_p202012_27, test_partition_every_1_prt_p202012_28, test_partition_every_1_prt_p202012_29, test_partition_every_1_prt_p202012_3, test_partition_every_1_prt_p202012_30, test_partition_every_1_prt_p202012_31, test_partition_every_1_prt_p202012_32, test_partition_every_1_prt_p202012_33, test_partition_every_1_prt_p202012_34, test_partition_every_1_prt_p202012_35, test_partition_every_1_prt_p202012_36, test_partition_every_1_prt_p202012_37, test_partition_every_1_prt_p202012_38, test_partition_every_1_prt_p202012_39, test_partition_every_1_prt_p202012_4, test_partition_every_1_prt_p202012_40, test_partition_every_1_prt_p202012_41, test_partition_every_1_prt_p202012_42, test_partition_every_1_prt_p202012_43, test_partition_every_1_prt_p202012_44, test_partition_every_1_prt_p202012_45, test_partition_every_1_prt_p202012_46, test_partition_every_1_prt_p202012_47, test_partition_every_1_prt_p202012_48, test_partition_every_1_prt_p202012_49, test_partition_every_1_prt_p202012_5, test_partition_every_1_prt_p202012_50, test_partition_every_1_prt_p202012_51, test_partition_every_1_prt_p202012_52, test_partition_every_1_prt_p202012_53, test_partition_every_1_prt_p202012_54, test_partition_every_1_prt_p202012_55, test_partition_every_1_prt_p202012_56, test_partition_every_1_prt_p202012_57, test_partition_every_1_prt_p202012_58, test_partition_every_1_prt_p202012_59, test_partition_every_1_prt_p202012_6, test_partition_every_1_prt_p202012_60, test_partition_every_1_prt_p202012_61, test_partition_every_1_prt_p202012_7, test_partition_every_1_prt_p202012_8, test_partition_every_1_prt_p202012_9, test_partition_every_1_prt_p20210205_6 Distributed by: (id) Partition by: (dw_end_date) testDB=#
不太熟悉ui
切割普通分區: 將分區p2 在 '2017-02-20' 左右切分紅兩塊 alter table test_partition_range split partition p2 at ('2017-02-20') into (partition p2, partition p3); 切割默認分區: alter table test_partition_range split default partition start ('2017-03-01') end ('2017-03-31') into (partition p4, default partition);
在GPDB中,分區是能夠嵌套增長的,分區下面能夠有子分區spa
create table test_partition_range_2 ( id int, name varchar(64), fdate varchar(10) ) distributed by (id) partition by range(fdate) subpartition by list(name) subpartition template ( subpartition c1 values ('xiaoxiao'), subpartition c2 values ('xiaohua') ) ( partition p1 start ('2017-01-01') end ('2017-01-31') ) 上面的分區中,p1會再分兩個c1/c2子分區
alter table test_partition_range_2 alter partition p1 truncate partition c2;
alter table test_partition_range_2 alter partition p1 drop partition c2;
https://blog.51cto.com/u_13126942/2053712
https://www.cnblogs.com/kingle-study/p/10550987.htmlcode
http://greenplum.org/docs/510/ref_guide/sql_commands/ALTER_TABLE.htmlhtm
http://www.dbaref.com/choosing-the-table-storage-model-in-greenplumblog
https://github.com/digoal/blog/blob/master/201708/20170817_01.md
https://github.com/digoal/blog/blob/master/201708/20170817_03.md
https://github.com/digoal/blog/blob/master/201708/20170818_02.md
https://github.com/digoal/blog/blob/master/201607/20160719_02.md
https://github.com/digoal/blog/blob/master/201711/20171123_01.md