postgresql 建立删除分区表

常经常添加分区表,和清理分区表,很简单的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);

postgresql 建立删除分区表,布布扣,bubuko.com

postgresql 建立删除分区表

上一篇:Win10电脑设置 wifi热点


下一篇:AcWing252 树(点分治)