01,创建分区表
[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]
Tips
通过实验得到:向主表插入数据时,数据会被自动存放至相应的分区表。
也可以直接向分区子表插入符合条件的数据,
当向分区子表插入不符合条件的额数据时,会提示:
[Err] ERROR: Trying to insert row into wrong partition (seg1 hadoop3:40000 pid=6679) DETAIL: Expected partition: test_partition_range_1_prt_p20111231, provided partition: test_partition_range_1_prt_p20111230
02,按时间分区
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 p20111230 START ('2011-12-30'::date) END ('2011-12-31'::date), --... );
03,使用Every分区
create table test.test_partition_every( id numeric, name character varying(32), dw_end_date date ) distributed by(id) partition by range(dw_end_Date) ( partition p201112 start('2011-12-1'::date) end ('2011-12-31'::date) every ('1 days'::interval) );
04,使用list分区
create table test.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 );
05,修改分区表
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]
06,新增分区
alter table test.test_partition_every add partition p20120105_6 start ('2012-01-05'::date) END ('2012-01-07'::date);
07,drop/truncate分区
alter table test.test_partition_every drop partition p20120105_6; alter table test.test_partition_every truncate partition p20120105_6;
08,拆分分区
alter table test.test_partition_every split partition p20120105_6 at(('2012-01-06'::date)) into (partition p20120105,partition p20120106);
09,交换分区
alter table test.test_partition_every exchange partition p20120102 with table test.test_on_partition;