SELECT nspname , relname , COUNT(*) AS partition_num FROM pg_class c , pg_namespace n , pg_inherits i WHERE c.oid = i.inhparent AND c.relnamespace = n.oid AND c.relhassubclass AND c.relkind = 'r' GROUPBY1,2ORDERBY partition_num DESC;
--创建分区表 --以下列出可能是月分区表,也有可能是日分区表,筛选条件 < 100,比较粗暴,建议根据自己的环境调整此 SQL FOR table_rec IN (SELECT nspname , relname , COUNT(*) AS partition_num FROM pg_class c , pg_namespace n , pg_inherits i WHERE c.oid = i.inhparent AND c.relnamespace = n.oid AND c.relhassubclass AND c.relkind = 'r' GROUP BY 1,2 having count(*) < 100 ORDER BY partition_num DESC ) LOOP v_partbl_name := table_rec.relname; FOR date_rec inselect date_key from tbl_month_suffix order by date_key LOOP v_date_key := date_rec.date_key; v_childtbl_name := v_partbl_name || '_' || v_date_key; execute 'create table ifnot exists ' || v_childtbl_name || ' ( like ' || v_partbl_name || ' including all ) inherits (' || v_partbl_name || ')' ; execute 'grantselecton ' || v_childtbl_name || ' to dwetl'; ENDLOOP; ENDLOOP; RAISE NOTICE '分区表创建完成!'; return1; end; $$ language 'plpgsql';