create or replace function func_trigger_stock_move_insert()
returns trigger
as
$$
begin
end
$$ language plpgsql volatile;
create trigger trigger_insert_stock_move
before insert
on stock_move_father
for each row
execute procedure func_trigger_stock_move_insert();
create or replace function func_trigger_stock_move_insert()
returns trigger
as
$$
declare
var_sub_table_name character varying;
var_is_exist boolean;
var_sql_create character varying;
var_sql_insert character varying;
var_date_start date;
begin
-- 设置本次要插入的表格var_sub_table_name
var_sub_table_name := ‘stock_move‘ || date_part(‘y‘, new.create_date)::char(4);
-- 设置时间约束
var_date_start := date_part(‘y‘, new.create_date)::char(4) || ‘-01‘ || ‘-01‘;
select count(1) into var_is_exist from pg_class where relname = var_sub_table_name;
if (var_is_exist = false) then
-- 创建新的表格.
var_sql_create := format(‘ create table if not exists %s
(
check ( create_date >= %L and create_date <= %L )
) inherits (stock_move_father);‘, var_sub_table_name, var_date_start, (var_date_start + interval ‘1 year‘));
raise notice ‘var_id:%‘,var_date_start;
execute var_sql_create;
end if;
var_sql_insert = format(‘insert into %I select $1.*;‘, var_sub_table_name);
execute var_sql_insert using new;
-- 表格创建完成后插入数据:
return null;
end
$$ language plpgsql volatile;
到这里,一个按照时间进行水平切分表格的触发器就完成啦.
原文:https://www.cnblogs.com/qianxunman/p/13731538.html