Postgresql 添加分區表(按月和按日通用)

來源:互聯網
上載者:User

標籤:

建了分區表的同學相信添加分區表很頭疼,如果有按月分區又有按日分區的,而且是通過"_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 添加分區表(按月和按日通用)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.