设为首页 加入收藏

TOP

PostgreSQL强大的多层表继承--及其在海量数据分类按月分区存储中的应用(二)
2014-11-24 07:50:42 来源: 作者: 【 】 浏览:11
Tags:PostgreSQL 强大 多层 继承 及其 海量 数据分类 分区 存储 应用
The triggle func will be exectued only when BEFORE INSERT
  • IF TG_OP <> 'INSERT' OR TG_TABLE_NAME <>'base_table_machine1' OR TG_WHEN <>
    'BEFORE' THEN
  • RETURN NULL;
  • END IF;
  • --Generate Table Name
  • str_sub_sample_time = date_part('year',NEW.sample_time)::varchar || '_' ||
  • CASE WHEN date_part('month',NEW.sample_time) <10 THEN '0' ELSE '' END
  • ||date_part('month',NEW.sample_time)::varchar;
  • str_sub_tablename = 'machine1_' || str_sub_sample_time;
  • --Check if table not created
  • select * from pg_tables where schemaname = 'public' and tablename=str_sub_
    tablename
  • into str_sql_cmd;
  • IF NOT FOUND THEN
  • --Create table Cmd
  • str_sql_cmd = '
  • CREATE TABLE '||str_sub_tablename||'
  • (
  • CONSTRAINT pk_'|| str_sub_tablename||' PRIMARY KEY (id ),
  • CONSTRAINT chk_'|| str_sub_tablename||'
  • CHECK(date_part(''year''::text, sample_time) = '||
  • date_part('year',NEW.sample_time)::varchar||
  • '::double precision AND
  • date_part(''month''::text, sample_time) = '||
  • date_part('month',NEW.sample_time)::varchar||'
  • )
  • )
  • INHERITS (base_table_machine1)
  • WITH ( OIDS=FALSE );
  • ALTER TABLE '||str_sub_tablename||' OWNER TO postgres;
  • CREATE INDEX idx_'|| str_sub_tablename||'_sample_time
  • ON '|| str_sub_tablename||'
  • USING btree (sample_time );
  • ';
  • EXECUTE str_sql_cmd;
  • END IF;
  • --insert Data
  • str_sql_cmd = 'INSERT INTO '||str_sub_tablename||'
  • ( id,dvalue,sample_time,machine_code,max_res,curr_res) VALUES (
  • nextval(''serial_id_seq''),$1,$2,$3,$4,$5);
  • ';
  • EXECUTE str_sql_cmd USING
  • NEW.dvalue,
  • NEW.sample_time,
  • NEW.machine_code,
  • NEW.max_res,
  • NEW.curr_res;
  • --return null because main table does not really contain data
  • return NULL;
  • END;
  • $BODY$
  • LANGUAGE plpgsql VOLATILE
  • COST 100;
  • ALTER FUNCTION on_insert_base_table_machine1()
  • OWNER TO postgres;
  • [sql]
    1. -- Function: on_insert_base_table_machine2()
    2. -- DROP FUNCTION on_insert_base_table_machine2();
    3. CREATE OR REPLACE FUNCTION on_insert_base_table_machine2()
    4. RETURNS trigger AS
    5. $BODY$
    6. DECLARE
    7. --Variable Hold subtable name
    8. str_sub_tablename varchar;
    9. --Variable Hold year\month info with timestamle
    10. str_sub_sample_time varchar;
    11. str_sql_cmd varchar;
    12. str_sub_checkval varchar;
    13. BEGIN
    14. --The triggle func will be exectued only when BEFORE INSERT
    15. IF TG_OP <> 'INSERT' OR TG_TABLE_NAME <>'base_table_machine2' OR TG_WHEN <>
      'BEFO
    首页 上一页 1 2 3 4 下一页 尾页 2/4/4
    】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
    分享到: 
    上一篇大型数据库的设计原则与开发技巧 下一篇大型数据库设计需要注意的一些原则

    评论

    帐  号: 密码: (新用户注册)
    验 证 码:
    表  情:
    内  容:

    ·求navicat for mysql (2025-12-26 13:21:33)
    ·有哪位大哥推荐一下m (2025-12-26 13:21:30)
    ·MySQL下载与安装教程 (2025-12-26 13:21:26)
    ·Linux_百度百科 (2025-12-26 12:51:52)
    ·Shell 流程控制 | 菜 (2025-12-26 12:51:49)