greenplum 分区按照类型可以分为
#列表分表
create table gh_par_list(
id1 integer,
id2 varchar(10))
distributed by (id1)
partition by list(id2)
(
partition p1 values ('1','2') tablespace ts_gh,
partition p2 values ('3','0')
tablespace ts_gh,
default partition pd
tablespace ts_gh
);
#范围分区
create table gh_par_range
(id1 integer,
id2 varchar(10),
id3 date)
distributed by (id1)
partition by range(id3)
(partition p1 start ('2011-01-01'::date) end ('2012-01-01'::date) tablespace ts_gh,
partition p2 start('2020-01-01'::date) end ('2021-01-01'::date) tablespace ts_gh);
##简便分区
create table gh_par_range_every
(id1 integer,
id2 varchar(10),
id3 date)
distributed by (id1)
partition by range(id3)
(partition p2011 start ('2011-01-01'::date) end ('2030-01-01'::date) every ('1 year'::interval) tablespace ts_gh);
#表和分区的联系
tutorial=> \d pg_partition;
Table "pg_catalog.pg_partition"
Column | Type | Modifiers
---------------+------------+-----------
parrelid | oid | not null
parkind | "char" | not null
parlevel | smallint | not null
paristemplate | boolean | not null
parnatts | smallint | not null
paratts | int2vector | not null
parclass | oidvector | not null
Indexes:
"pg_partition_oid_index" UNIQUE, btree (oid)
"pg_partition_parrelid_index" btree (parrelid)
"pg_partition_parrelid_parlevel_istemplate_index" btree (parrelid, parlevel, paristemplate)
tutorial=>
**pg_partition中的parrelid关联pg_class的oid**
tutorial=> \d pg_partition_rule;
Table "pg_catalog.pg_partition_rule"
Column | Type | Modifiers
-------------------+----------+-----------
paroid | oid | not null
parchildrelid | oid | not null
parparentrule | oid | not null
parname | name | not null
parisdefault | boolean | not null
parruleord | smallint | not null
parrangestartincl | boolean | not null
parrangeendincl | boolean | not null
parrangestart | text |
parrangeend | text |
parrangeevery | text |
parlistvalues | text |
parreloptions | text[] |
partemplatespace | oid |
Indexes:
"pg_partition_rule_oid_index" UNIQUE, btree (oid)
"pg_partition_rule_parchildrelid_index" btree (parchildrelid)
"pg_partition_rule_parchildrelid_parparentrule_parruleord_index" btree (parchildrelid, parp
arentrule, parruleord)
"pg_partition_rule_paroid_parentrule_ruleord_index" btree (paroid, parparentrule, parruleor
d)
tutorial=>
**pg_partition_rule中的paroid关联pg_partition的oid**
##创建视图直接查询
create view vw_partition as
select pp.parrelid tableoid ,prl.parchildrelid,prl.parname as partitionname
from pg_partition pp,pg_partition_rule prl
where pp.paristemplate=false and prl.paroid=pp.oid;
select * from vw_partition t where tableoid='public.gh_par_range'::regclass;
#分区操作
##增加
alter table gh_par_range
add partition p3 start ('1991-01-01'::date) end ('1992-01-01'::date);
##删除
alter table gh_par_range drop partition p3;
##清空
alter table gh_par_range truncate partition p3;
##分离
alter table gh_par_range
split partition p3
at ('1991-07-01'::date)
into (partition p4,partition p5);
##交换
2016-12-01
greenplum 支持对表进行分区
tutorial=> create table gh_partition_range(id int,name varchar(32),dw_end_date date)
tutorial-> distributed by (id)
tutorial-> partition by range (dw_end_date)
tutorial(> partition p2015 start ('2015-01-01'::date) end ('2016-01-01'::date),
tutorial(> partition p2016 start ('2016-01-01'::date) end ('2017-01-01'::date)
NOTICE: CREATE TABLE will create partition "gh_partition_range_1_prt_p2015" for table "gh_partitio
n_range"NOTICE: CREATE TABLE will create partition "gh_partition_range_1_prt_p2016" for table "gh_partitio
tutorial=> insert into gh_partition_range values(1,23,'2015-01-01'::date);
tutorial=> select * from gh_partition_range;
----+------+-------------
tutorial=> insert into gh_partition_range values(1,23,'2016-01-01'::date);
tutorial=> insert into gh_partition_range values(1,23,'2018-01-01'::date);
ERROR: no partition for partitioning key (seg0 slave1:40000 pid=1875)
tutorial=> select * from gh_partition_range;
----+------+-------------
tutorial=> select * from gh_partition_range_1_prt_p2015;
----+------+-------------
tutorial=> select * from gh_partition_range_1_prt_p2016;
----+------+-------------
tutorial=> alter table gh_partition_range add partition p2017 start ('2017-01-01'::date) end ('2018-01-01'::date);
NOTICE: CREATE TABLE will create partition "gh_partition_range_1_prt_p2017" for table "gh_partitio
tutorial=> insert into gh_partition_range values(1,23,'2017-01-01'::date);
tutorial=> create table gh_one_partition (id int,name varchar(35),dw_date date)
partition by range (dw_date) (partition p2015 start ('2015-01-01'::date) end ('2016-01-01'::date), partition p2016 start ('2016-01-01'::date) end ('2017-01-01'::date));NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'id' as the Greenplum Dat
abase data distribution key for this table.HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen
are the optimal data distribution key to minimize skew.NOTICE: CREATE TABLE will create partition "gh_one_partition_1_prt_p2015" for table "gh_one_partit
ion"NOTICE: CREATE TABLE will create partition "gh_one_partition_1_prt_p2016" for table "gh_one_partit
tutorial=> insert into gh_one_partition values(1,26,'2015-01-01'::date);
tutorial=> insert into gh_one_partition values(1,26,'2015-02-01'::date);
tutorial=> select * from gh_one_partition;
tutorial=> select * from gh_partition_range_1_prt_p2015;
----+------+-------------
tutorial=> select * from gh_partition_range;
----+------+-------------
tutorial=> insert into gh_partition_range_1_prt_p2015 values(1,21,'2015-01-01'::date);
tutorial=> select * from gh_partition_range_1_prt_p2015;
----+------+-------------
tutorial=> select * from gh_one_partition_1_prt_p2015;
tutorial=> alter table gh_partition_range exchange partition p2015 with table gh_one_partition;
ERROR: relation "gh_one_partition" must have the same column names and column order as "gh_partition_range"
tutorial=> ALTER TABLE public.gh_one_partition RENAME dw_date TO dw_end_date;
tutorial=> alter table gh_partition_range exchange partition p2015 with table gh_one_partition;
ERROR: cannot EXCHANGE table "gh_one_partition" as it has child table(s)
tutorial=> alter table gh_one_partition drop partition p2016;
tutorial=> alter table gh_partition_range exchange partition p2015 with table gh_one_partition;
ERROR: cannot EXCHANGE table "gh_one_partition" as it has child table(s)
tutorial=> create table gh_one_partition2 as select * from gh_one_partition;
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'id' as the Greenplum
Database data distribution key for this table.HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen
are the optimal data distribution key to minimize skew.SELECT 2
tutorial=> alter table gh_partition_range exchange partition p2015 with table gh_one_partition;
ERROR: cannot EXCHANGE table "gh_one_partition" as it has child table(s)
tutorial=> alter table gh_partition_range exchange partition p2015 with table gh_one_partition2;
ERROR: child table "gh_one_partition2" has different type for column "name"
tutorial=> alter table gh_partition_range exchange partition p2015 with table gh_one_partition2;
tutorial=> select * from gh_one_partition2;
----+------+-------------
tutorial=> select * from gh_partition_range_1_prt_p2015;
----+------+-------------
tutorial=> truncate tabel gh_one_partition2;
ERROR: syntax error at or near "gh_one_partition2"
LINE 1: truncate tabel gh_one_partition2;
tutorial=> truncate table gh_one_partition2;
tutorial=> alter table gh_partition_range exchange partition p2015 with table gh_one_partition2;
tutorial=> select * from gh_one_partition2;
----+------+-------------
tutorial=> select * from gh_partition_range;
----+------+-------------