函数建分区表

按周生成分区表

do                                                                                                               
$$
DECLARE base text; --生命sql类型

pgsqltest text; --字符串为文本类型,执行函数


i int; --i为整数
BEGIN
base = 'create table main_history_p_%s partition of main_history_p for values FROM (''%s'') to (''%s'')';


FOR i IN 0..11 loop --不是左闭右开
pgsqltest = format(base,
to_char('2024-01-01'::timestamp + (i || 'week')::INTERVAL, --第一个%s占位符
'YYYYMMDD'),
'2024-01-01'::timestamp + (i || 'week')::INTERVAL, --第二个%s占位符
'2024-01-01'::timestamp + (i + 1 || 'week')::INTERVAL); --第三个%s占位符
--raise notice '%', sqlstring;
EXECUTE pgsqltest; --执行sqlstring
END loop; --结束loop
END --结束begin
$$language plpgsql; --结束函数

按月生成分区表

do                                                                                                               
$$
DECLARE base text; --生命sql类型

pgsqltest text; --字符串为文本类型,执行函数

i int; --i为整数
BEGIN
base = 'create table main_history_p_%s partition of main_history_p for values FROM (''%s'') to (''%s'')';

FOR i IN 0..11 loop --不是左闭右开
pgsqltest = format(base,
to_char('2024-01-01'::timestamp + (i || 'month')::INTERVAL, --第一个%s占位符
'YYYYMMDD'),
'2024-01-01'::timestamp + (i || 'month')::INTERVAL, --第二个%s占位符
'2024-01-01'::timestamp + (i + 1 || 'month')::INTERVAL); --第三个%s占位符
--raise notice '%', sqlstring;
EXECUTE pgsqltest; --执行sqlstring
END loop; --结束loop
END --结束begin
$$language plpgsql; --结束函数

发表评论

邮箱地址不会被公开。 必填项已用*标注