create index tbl_weather_partition_20180709_created_time_index on tbl_weather_partition_20180709 (created_time);
create index tbl_weather_partition_20180709_city_code_index on tbl_weather_partition_20180709 (city_code);
创建触发器函数
对于开发人员来说,希望数据库是透明的,只管 insert into tbl_partition。对于数据插向哪个分区,则希望由DB决定。这点,ORACLE实现了,但是PG不行,需要前期人工处理下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
CREATEOR REPLACE FUNCTION tbl_weather_partition_insert_trigger() RETURNSTRIGGERAS $$ BEGIN IF ( NEW.created_time >=DATE'2018-07-09'AND NEW.created_time <DATE'2018-07-10' ) THEN INSERTINTO tbl_weather_partition_20180709 VALUES (NEW.*); ELSIF ( NEW.created_time >=DATE'2018-07-10'AND NEW.created_time <DATE'2018-07-11' ) THEN INSERTINTO tbl_weather_partition_20180710 VALUES (NEW.*); ELSE RAISE EXCEPTION 'Date out of range. Fix the tbl_weather_partition_insert_trigger() function!'; END IF; RETURNNULL; END; $$ LANGUAGE plpgsql;