Chinaunix首页 | 论坛 | 博客
  • 博客访问: 68602
  • 博文数量: 11
  • 博客积分: 286
  • 博客等级: 二等列兵
  • 技术积分: 136
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-03 15:31
文章分类

全部博文(11)

文章存档

2015年(5)

2014年(3)

2011年(3)

我的朋友

分类: Mysql/postgreSQL

2011-05-07 16:29:43

需要设置constraint_exclusion = on。
  1. CREATE TABLE t20110101
  2. (
  3.     CONSTRAINT timecheck CHECK (
  4.     time >= '2011-01-01'::timestamp with time zone
  5.     AND
  6.     time < '2011-01-02'::timestamp with time zone)
  7. )
  8. INHERITS (t);

  9. CREATE TRIGGER trig_t_insert
  10.   BEFORE INSERT
  11.   ON t
  12.   FOR EACH ROW
  13.   EXECUTE PROCEDURE t_insert();

  14. CREATE OR REPLACE FUNCTION t_insert()
  15.   RETURNS trigger AS
  16. $BODY$
  17. DECLARE
  18.         t timestamp with time zone;
  19. BEGIN
  20.         select into t TIMESTAMP WITH TIME ZONE 'epoch' + (NEW.time_of_t::bigint/1000000) * INTERVAL '1 second';
  21.         NEW.time := t;

  22.         if t >= '2011-03-19 00:00:00+00' and t < '2011-03-20 00:00:00+00' then
  23.                 insert into t20110319 select NEW.*;
  24.         elsif t >= '2011-03-20 00:00:00+00' and t < '2011-03-21 00:00:00+00' then
  25.                 insert into t20110320 select NEW.*;
  26.         elsif t >= '2011-03-18 00:00:00+00' and t < '2011-03-19 00:00:00+00' then
  27.                 insert into t20110318 select NEW.*;
  28.         else
  29.                 raise warning 'The time (%) is not accepted by subtable, storing in supertable',NEW.time_of_t;
  30.                 return NEW;
  31.     end if;

  32.     return NULL;
  33. END
  34. $BODY$
  35.   LANGUAGE 'plpgsql';
阅读(802) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~