Postgres 9.3 x64位,我正在使用历史表,每秒发生一次用户log.huge数据插入到目前为止,数据大约是9千万且还在计算 我每个月使用日期列对表进行分区,并且我使用了一个触发器,对于每个插件,它将检查特定月份表的天气是否存在,如果它将直接插入,则将尝试创建和插入数据。但触发器花了太多时间执行是否有任何方法来即兴表现
CREATE OR REPLACE FUNCTION history_master_part_trigger()
RETURNS TRIGGER AS $$
DECLARE
yr int;
mnth int;
tbname character varying;
sdate character varying;
edate character varying;
cnt int;
Begin
raise notice '%','something';
EXECUTE 'select EXTRACT(YEAR FROM DATE '''||NEW.hm_date||''')' INTO yr;
EXECUTE 'select EXTRACT(month FROM DATE '''||NEW.hm_date||''')' INTO mnth;
tbname := 'history_master_part_y'||yr||'m'||mnth;
sdate :=yr||'-'||mnth||'-01';
IF mnth = 12 THEN
edate :=yr+1||'-'||'01-01';
ELSE
edate :=yr||'-'||mnth+1||'-01';
END IF;
--raise notice 'table-----', tbname;
raise notice '%', sdate;
raise notice '%', edate;
raise notice '%',yr;
If(SELECT EXISTS (
SELECT 1
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public'
AND c.relname = tbname
AND c.relkind = 'r' -- only tables(?)
)) THEN
--INSERT INTO ||tbname|| VALUES ||NEW'.*)';
raise notice '%','inserting into'||tbname;
EXECUTE format('INSERT INTO ' || tbname || ' SELECT ($1).*')
USING NEW;
ELSE
EXECUTE 'CREATE TABLE '|| tbname||'(
CHECK ( hm_date >= DATE '''||sdate||''' AND hm_date < DATE '''||edate||''' )
) INHERITS (history_master_part)';
EXECUTE format('INSERT INTO ' || tbname || ' SELECT ($1).*')
USING NEW;
--RAISE EXCEPTION 'Caution Caution Caution Amit is getting angry';
END IF;
Return NULL;
End;
$$
LANGUAGE 'plpgsql'