Skip to content

Instantly share code, notes, and snippets.

@kmoppel
Last active January 12, 2017 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmoppel/1f2b3417cd38bff1187033844c0c0cf5 to your computer and use it in GitHub Desktop.
Save kmoppel/1f2b3417cd38bff1187033844c0c0cf5 to your computer and use it in GitHub Desktop.
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