標籤:
建了分區表的同學相信添加分區表很頭疼,如果有按月分區又有按日分區的,而且是通過"_yyyymmdd"或者"_yyyymm"尾碼進行分區的,那麼可以用這個函數進行添加分區
CREATE OR REPLACE FUNCTION f_add_partition() RETURNS void LANGUAGE plpgsqlAS $function$declarev_max_childname text; --最大子表名稱v_parentname text; --子表對應的父表名稱v_suffix text; --子表日期尾碼sql text; --要執行的SQL語句GRA_TO text; --賦予許可權的語句begin--取出分區表最大分區表以及父表名稱for v_max_childname, v_parentname in select max(inhrelid::regclass::text),inhparent::regclass from pg_inherits where inhparent::regclass::text not like ‘%.%‘ group by inhparent loop raise notice ‘最大子表:%,父表:%‘,v_max_childname,v_parentname; sql= ‘select split_part(‘‘‘|| v_max_childname || ‘‘‘,‘‘_‘‘,(length(‘‘‘ || v_max_childname || ‘‘‘)-length(replace(‘‘‘ || v_max_childname || ‘‘‘,‘‘_‘‘,‘‘‘‘))+1))‘; --取出日期是按月還是按日 execute sql into v_suffix; --將取出的日期存入v_suffix while(length(v_suffix)=6 and v_suffix<‘201512‘) --判斷如果是按月,那麼迴圈執行建表語句並且賦予許可權 loop v_suffix=to_char (to_timestamp(v_suffix,‘yyyymm‘)+interval ‘1 month‘,‘yyyymm‘) ; --在取出的分區表日期按月+1 sql= ‘create table ‘||v_parentname ||‘_‘||v_suffix || ‘(like ‘ || v_parentname ||‘ including all) inherits (‘|| v_parentname ||‘)‘; execute sql; for GRA_TO in execute ‘select ‘‘grant ‘‘||privilege_type||‘‘ on ‘||v_parentname||‘_‘||v_suffix ||‘ to ‘‘||grantee from information_schema.table_privileges where table_name=‘‘‘||v_max_childname||‘‘‘‘ loop execute GRA_TO; end loop; end loop; while(length(v_suffix)=8 and v_suffix<‘20151231‘) --判斷如果是按日分區,迴圈執行後面的建表語句並且賦予許可權 loop v_suffix=to_char (to_timestamp(v_suffix,‘yyyymmdd‘)+interval ‘1 day‘,‘yyyymmdd‘) ; sql= ‘create table ‘||v_parentname||‘_‘||v_suffix || ‘(like ‘ || v_parentname ||‘ including all) inherits (‘|| v_parentname ||‘)‘; execute sql; for GRA_TO in execute ‘select ‘‘grant ‘‘||privilege_type||‘‘ on ‘||v_parentname||‘_‘||v_suffix||‘ to ‘‘||grantee from information_schema.table_privileges where table_name=‘‘‘||v_max_childname||‘‘‘‘ loop execute GRA_TO; end loop; end loop; end loop; RAISE NOTICE ‘Partition table has be created successfully!‘;end;$function$
\dt hank | tbl | table | hank hank | tbl_20140322 | table | hank hank | test | table | hank hank | test_201405 | table | hankselect f_add_partition();\dt
hank | tbl | table | hank hank | tbl_20140322 | table | hank 按日分區的顯示太多,此處省略一萬行。。。。。 hank | tbl_20151230 | table | hank hank | tbl_20151231 | table | hank
按月的比較少,就全部貼出來了 hank | test | table | hank hank | test_201405 | table | hank hank | test_201406 | table | hank hank | test_201407 | table | hank hank | test_201408 | table | hank hank | test_201409 | table | hank hank | test_201410 | table | hank hank | test_201411 | table | hank hank | test_201412 | table | hank hank | test_201501 | table | hank hank | test_201502 | table | hank hank | test_201503 | table | hank hank | test_201504 | table | hank hank | test_201505 | table | hank hank | test_201506 | table | hank hank | test_201507 | table | hank hank | test_201508 | table | hank hank | test_201509 | table | hank hank | test_201510 | table | hank hank | test_201511 | table | hank hank | test_201512 | table | hank
Postgresql 添加分區表(按月和按日通用)