Last active
January 12, 2017 11:13
-
-
Save kmoppel/1f2b3417cd38bff1187033844c0c0cf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table t_order(id int, created_on timestamptz, value int, check (false) no inherit); | |
create index ON t_order(created_on); | |
create index ON t_order(id); | |
create table t_order_201701 (LIKE t_order INCLUDING indexes) INHERITS (t_order); | |
create table t_order_201612 (LIKE t_order INCLUDING indexes) INHERITS (t_order); | |
alter table t_order_201612 add constraint part check (created_on >= '2016-12-01' and created_on < '2017-01-01'); | |
CREATE OR REPLACE FUNCTION public.ins_route() | |
RETURNS trigger | |
LANGUAGE plpgsql | |
AS $function$ | |
begin | |
if NEW.created_on >= '2016-12-01' and NEW.created_on < '2017-01-01' then | |
insert into t_order_201612 values(NEW.*); | |
end if; | |
if NEW.created_on >= '2017-01-01' and NEW.created_on < '2017-02-01' then | |
insert into t_order_201701 values(NEW.*); | |
end if; | |
return null; | |
end; | |
$function$; | |
create trigger trg_rerout before insert on t_order for each row execute procedure ins_route(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment