当前位置:Gxlcms > mysql > postgresql建立删除分区表

postgresql建立删除分区表

时间:2021-07-01 10:21:17 帮助过:564人阅读

常经常添加分区表,和清理分区表,很简单的2个函数 --添加分区表 CREATE OR REPLACE FUNCTION create_table(table_name character varying,table_num integer) RETURNS void LANGUAGE plpgsql AS $function$ declare v_date char(8); v_tablename varchar(64)

常经常添加分区表,和清理分区表,很简单的2个函数 --添加分区表 CREATE OR REPLACE FUNCTION create_table(table_name character varying,table_num integer) RETURNS void LANGUAGE plpgsql AS $function$ declare
v_date char(8); v_tablename varchar(64);
begin for i in 0..table_num loop
v_date :=to_char(current_date + i,'YYYYMMDD'); v_tablename := table_name ||'_'|| v_date;
execute 'create table ' || v_tablename ||'(like '||table_name||' including all) inherits('||table_name||')'; execute 'grant select on ' || v_tablename || ' to dwetl';
end loop; end $function$;
--删除分区表 CREATE OR REPLACE FUNCTION drop_table(table_name character varying,table_num integer) RETURNS void LANGUAGE plpgsql AS $function$ declare
v_date char(8); v_tablename varchar(64);
begin for i in 0..table_num loop
v_date :=to_char(current_date + i,'YYYYMMDD'); v_tablename := table_name ||'_'|| v_date;
execute 'drop table ' || v_tablename;

end loop; end $function$;

--执行函数 select create_table('table_name',table_num); select drop_table('table_name',table_num); --删除函数 drop FUNCTION create_table(table_name character varying,table_num integer); #必须写上输入参数 drop FUNCTION drop_table(table_name character varying,table_num integer);

人气教程排行