pg的分区表基于表继承
原orders表
CREATE TABLE public.orders
(
orderid integer NOT NULL DEFAULT nextval('orders_orderid_seq'::regclass),
orderdate date NOT NULL,
customerid integer,
netamount numeric(12,2) NOT NULL,
tax numeric(12,2) NOT NULL,
totalamount numeric(12,2) NOT NULL,
CONSTRAINT orders_pkey PRIMARY KEY (orderid),
CONSTRAINT fk_customerid FOREIGN KEY (customerid)
REFERENCES public.customers (customerid) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE SET NULL
)
create table orders_2004_01(check(orderdate>=date '2004-01-01' and orderdate<date '2004-02-01')) inherits(orders);
create table orders_2004_02(check(orderdate>=date '2004-02-01' and orderdate<date '2004-03-01')) inherits(orders);
create table orders_2004_03(check(orderdate>=date '2004-03-01' and orderdate<date '2004-04-01')) inherits(orders);
create table orders_2004_04(check(orderdate>=date '2004-04-01' and orderdate<date '2004-05-01')) inherits(orders);
create table orders_2004_05(check(orderdate>=date '2004-05-01' and orderdate<date '2004-06-01')) inherits(orders);
create table orders_2004_06(check(orderdate>=date '2004-06-01' and orderdate<date '2004-07-01')) inherits(orders);
create table orders_2004_07(check(orderdate>=date '2004-07-01' and orderdate<date '2004-08-01')) inherits(orders);
create table orders_2004_08(check(orderdate>=date '2004-08-01' and orderdate<date '2004-09-01')) inherits(orders);
create table orders_2004_09(check(orderdate>=date '2004-09-01' and orderdate<date '2004-10-01')) inherits(orders);
create table orders_2004_10(check(orderdate>=date '2004-10-01' and orderdate<date '2004-11-01')) inherits(orders);
create table orders_2004_11(check(orderdate>=date '2004-11-01' and orderdate<date '2004-12-01')) inherits(orders);
create table orders_2004_12(check(orderdate>=date '2004-12-01' and orderdate<date '2005-01-01')) inherits(orders);
分区表添加主键
alter table only orders_2004_01 add constraint orders_2004_01_pkey primary key(orderid);
alter table only orders_2004_02 add constraint orders_2004_02_pkey primary key(orderid);
alter table only orders_2004_03 add constraint orders_2004_03_pkey primary key(orderid);
alter table only orders_2004_04 add constraint orders_2004_04_pkey primary key(orderid);
alter table only orders_2004_05 add constraint orders_2004_05_pkey primary key(orderid);
alter table only orders_2004_06 add constraint orders_2004_06_pkey primary key(orderid);
alter table only orders_2004_07 add constraint orders_2004_07_pkey primary key(orderid);
alter table only orders_2004_08 add constraint orders_2004_08_pkey primary key(orderid);
alter table only orders_2004_09 add constraint orders_2004_09_pkey primary key(orderid);
alter table only orders_2004_10 add constraint orders_2004_10_pkey primary key(orderid);
alter table only orders_2004_11 add constraint orders_2004_11_pkey primary key(orderid);
alter table only orders_2004_12 add constraint orders_2004_12_pkey primary key(orderid);
创建本地索引
create index ix_orders_2004_01_custid on orders_2004_01 using btree(customerid);
create index ix_orders_2004_02_custid on orders_2004_02 using btree(customerid);
create index ix_orders_2004_03_custid on orders_2004_03 using btree(customerid);
create index ix_orders_2004_04_custid on orders_2004_04 using btree(customerid);
create index ix_orders_2004_05_custid on orders_2004_05 using btree(customerid);
create index ix_orders_2004_06_custid on orders_2004_06 using btree(customerid);
create index ix_orders_2004_07_custid on orders_2004_07 using btree(customerid);
create index ix_orders_2004_08_custid on orders_2004_08 using btree(customerid);
create index ix_orders_2004_09_custid on orders_2004_09 using btree(customerid);
create index ix_orders_2004_10_custid on orders_2004_10 using btree(customerid);
create index ix_orders_2004_11_custid on orders_2004_11 using btree(customerid);
create index ix_orders_2004_12_custid on orders_2004_12 using btree(customerid);
添加约束
alter table only orders_2004_01 add constraint fk_2004_01_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_02 add constraint fk_2004_02_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_03 add constraint fk_2004_03_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_04 add constraint fk_2004_04_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_05 add constraint fk_2004_05_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_06 add constraint fk_2004_06_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_07 add constraint fk_2004_07_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_08 add constraint fk_2004_08_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_09 add constraint fk_2004_09_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_10 add constraint fk_2004_10_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_11 add constraint fk_2004_11_customerid foreign key(customerid) references customers(customerid) on delete set null;
alter table only orders_2004_12 add constraint fk_2004_12_customerid foreign key(customerid) references customers(customerid) on delete set null;
迁移数据到分区表
create or replace function orders_update_trigger()
returns trigger as $$
begin
delete from orders where old.orderid=orderid;
insert into orders values(new.*);
return null;
end;
$$
language plpgsql;
create trigger update_orders before update on orders for each row execute procedure orders_update_trigger();
begin;
select * from orders;
select count(*) from orders_2004_01;
select count(*) from orders_2004_02;
update orders set orderid=orderid;
select count(*) from orders_2004_12;
select count(*) from orders_2004_01;
commit