gp 日常使用脚本

--查询某schema下所有函数的属主
SELECT n.nspname AS "Schema",p.proname AS "Name",pg_catalog.pg_get_userbyid(p.proowner) AS "Owner" 
FROM pg_catalog.pg_proc p 
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace 
WHERE n.nspname='dwbi02' ORDER BY 2;

--查询某schema下所有函数的详细信息
SELECT n.nspname, p.proname, pg_catalog.pg_get_userbyid(p.proowner) AS "Owner",
 CASE WHEN proallargtypes IS NOT NULL THEN
    pg_catalog.array_to_string(ARRAY(
      SELECT
        CASE
          WHEN p.proargmodes[s.i] = 'i' THEN ''
          WHEN p.proargmodes[s.i] = 'o' THEN 'OUT '
          WHEN p.proargmodes[s.i] = 'b' THEN 'INOUT '
          WHEN p.proargmodes[s.i] = 'v' THEN 'VARIADIC '
        END ||
        CASE
          WHEN COALESCE(p.proargnames[s.i], '') = '' THEN ''
          ELSE p.proargnames[s.i] || ' ' 
        END ||
        pg_catalog.format_type(p.proallargtypes[s.i], NULL)
      FROM
        pg_catalog.generate_series(1, pg_catalog.array_upper(p.proallargtypes, 1)) AS s(i)
    ), ', ')
  ELSE
    pg_catalog.array_to_string(ARRAY(
      SELECT
        CASE
          WHEN COALESCE(p.proargnames[s.i+1], '') = '' THEN ''
          ELSE p.proargnames[s.i+1] || ' '
          END ||
        pg_catalog.format_type(p.proargtypes[s.i], NULL)
      FROM
        pg_catalog.generate_series(0, pg_catalog.array_upper(p.proargtypes, 1)) AS s(i)
    ), ', ')
  END AS "data_types" 
  FROM pg_catalog.pg_proc p 
  LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
  WHERE 1=1
      AND n.nspname <> 'pg_catalog'
      AND n.nspname <> 'information_schema'
      AND n.nspname = 'dmc' order by 2,3;
      
      
--某schema下,表数量统计(正则过滤分区表子分区)
SELECT count(*) FROM pg_tables WHERE tablename !~ '.*_1+_prt_.*$'AND schemaname = 'ctl';

--查询一个库下各schema占用的空间
SELECT pg_size_pretty(cast( sum(pg_total_relation_size( schemaname  || '.' || tablename)) AS bigint)), schemaname
FROM pg_tables t INNER JOIN pg_namespace d ON t.schemaname=d.nspname  GROUP BY schemaname;

SELECT pg_size_pretty(cast( sum(pg_total_relation_size( schemaname  || '.' || tablename)) AS bigint)), schemaname
FROM pg_tables t INNER JOIN pg_namespace d ON t.schemaname=d.nspname  WHERE schemaname in ('mad','mad_ods','dwbi_ods','coupon_ods') GROUP BY schemaname;

SELECT pg_size_pretty(cast( sum(pg_total_relation_size( schemaname  || '.' || tablename)) AS bigint)), schemaname
FROM pg_tables t INNER JOIN pg_namespace d ON t.schemaname=d.nspname  WHERE schemaname = 'mad' GROUP BY schemaname;

--统计某张表的分区数量
SELECT p.schemaname, p.tablename, count(*) prt_count
FROM pg_partitions p
WHERE p.tablename='表名(不含schema名称)' 
GROUP BY 1,2
ORDER BY 2;

--统计AO分区表数量(不含子表)
SELECT count(*) FROM pg_class c 
LEFT JOIN pg_namespace n 
ON c.relnamespace = n.oid 
WHERE relstorage IN ('c','a') 
AND c.oid NOT IN(SELECT parchildrelid FROM pg_partition_rule);

--统计AO表分区数量
SELECT count(*) FROM pg_class c 
LEFT JOIN pg_namespace n 
ON c.relnamespace = n.oid 
WHERE relstorage IN ('c','a') 
AND c.oid IN(SELECT parchildrelid FROM pg_partition_rule);

--查询分区数量大于100的AO表
SELECT * FROM
(
SELECT n.nspname, c.relname, count(*) part_count FROM pg_class c 
LEFT JOIN pg_namespace n 
ON c.relnamespace = n.oid 
INNER JOIN pg_partitions p
ON p.tablename=c.relname
AND p.schemaname=n.nspname 
WHERE c.relstorage IN ('c','a') 
AND c.oid NOT IN(SELECT parchildrelid FROM pg_partition_rule) 
GROUP BY n.nspname, c.relname 
) a
WHERE part_count >= 100
ORDER BY 3 DESC;

--统计分区表大小(不含索引)
SELECT p.schemaname,p.tablename,sum(sotdsize) 
FROM pg_partitions p LEFT JOIN  (SELECT autnspname, autrelname, sotdsize  FROM gp_toolkit.__gp_user_tables a, gp_toolkit.gp_size_of_table_disk b  
WHERE a.autoid = b.sotdoid) b on p.partitionschemaname = b.autnspname AND p.partitiontablename = b.autrelname 
GROUP BY 1,2 
ORDER BY 1,2;

--统计某分区表大小(含索引)
SELECT p.schemaname, p.tablename, round(sum(pg_total_relation_size(p.schemaname || '.' || p.partitiontablename))/1024/1024) "MB"
FROM pg_partitions p
WHERE p.tablename='employee_daily' 
GROUP BY 1,2
ORDER BY 2;

--统计某schema下各分区表大小(含索引)
SELECT p.schemaname, p.tablename, round(sum(pg_total_relation_size(p.schemaname || '.' || p.partitiontablename))/1024/1024) "MB"
FROM pg_partitions p
WHERE p.schemaname ='mad' 
GROUP BY 1,2 
ORDER BY 2;

--查询所有非分区表名
SELECT t.schemaname, t.tablename 
FROM pg_tables t, pg_partitions p 
WHERE t.tablename <> p.tablename AND t.tablename <> p.partitiontablename AND t.schemaname='mad' 
GROUP BY 1,2
ORDER BY 2;

--查询所有表名(不含分区表子表)
SELECT n.nspname,c.relname 
FROM pg_class c, pg_namespace n 
WHERE c.relnamespace = n.oid AND n.nspname NOT LIKE 'pg_%' AND n.nspname NOT LIKE 'gp_%' 
AND n.nspname <> 'information_schema' AND relkind IN('r') AND relstorage <> 'x' 
AND c.oid NOT IN(SELECT parchildrelid FROM pg_partition_rule);

--Greenplum查询争用(表锁)
SELECT l.locktype, l.database, c.relname, l.relation, l.transactionid, l.pid, l.mode, l.granted,a.current_query 
FROM pg_locks l, pg_class c, pg_stat_activity a WHERE l.relation=c.oid AND l.pid=a.procpid ORDER BY c.relname;

--用函数杀SQL
-------------------------------------
--一般查询SQL
SELECT pg_cancel_backend(procpid);

--其他SQL
SELECT pg_terminate_backend(procpid);
-------------------------------------

--查询某张表的数据分布均衡性
SELECT gp_segment_id,count(*) FROM schemaname.tablename GROUP BY gp_segment_id ORDER BY count(*) DESC;

--查询数据分布均匀性
SELECT * FROM gp_toolkit.gp_skew_idle_fractions; --siffraction字段值大于0.1的需要重新选择分布键

--保证全库统计信息是最新的情况下,查询表膨胀情况
SELECT * FROM gp_toolkit.gp_bloat_diag; --bdidiag字段内容显示膨胀程度

--查询用户情况
SELECT rolname,rolsuper,rolinherit,rolcreaterole,rolcanlogin,rolconnlimit,rolresqueue,oid FROM pg_roles ORDER BY 8;

--查询所有外部表
SELECT n.nspname,c.relname 
FROM pg_class c, pg_namespace n 
WHERE c.relnamespace = n.oid AND n.nspname NOT LIKE 'pg_%' AND n.nspname NOT LIKE 'gp_%' 
AND n.nspname <> 'information_schema' AND relkind IN('r') AND relstorage = 'x' 
AND c.oid NOT IN(SELECT parchildrelid FROM pg_partition_rule);

--重组表 消除膨胀
\d+ dwbi01_ods.boh__bohrscsales
ALTER TABLE dwbi01_ods.boh__bohrscsales SET WITH (REORGANIZE=TRUE) DISTRIBUTED RANDOMLY;
ALTER TABLE dwbi01_ods.boh__bohrscsales SET WITH (REORGANIZE=TRUE) DISTRIBUTED BY (transaction_code);
ANALYZE dwbi01_ods.boh__bohrscsales;

--检测字段是否适合用作分布键
select COUNT(bizdate), gp_segment_id from dwbi01_ods.boh__bohrscsales group by 2;

--查询角色配置情况(使用资源组时)
SELECT rolname, rolsuper, rolinherit, rolcreaterole, rolcanlogin, rsgname FROM pg_roles, pg_resgroup WHERE pg_roles.rolresgroup=pg_resgroup.oid;


--查询膨胀率大于20的AO表
SELECT nspname, relname, AVG(percent_hidden)
FROM
(
   SELECT n.nspname, c.relname, (gp_toolkit.__gp_aovisimap_compaction_info(c.oid)).percent_hidden percent_hidden
   FROM pg_class c, pg_namespace n
   WHERE c.relnamespace = n.oid
     AND relstorage IN ('c','a')
     AND c.oid NOT IN(SELECT parchildrelid FROM pg_partition_rule)
) t
WHERE t.percent_hidden > 20
GROUP BY nspname, relname
ORDER BY 3 DESC;

--查询溢出文件大小(详细)
select * from gp_toolkit.gp_workfile_entries;

--查询溢出文件大小
select * from gp_toolkit.gp_workfile_usage_per_query;

--查询每个Segment上的溢出文件大小
select * from gp_toolkit.gp_workfile_usage_per_segment;

--查询表的分布键
SELECT att.nspname,att.relname,string_agg (a.attname, ',') attby 
FROM 
(
 SELECT c.oid,n.nspname,c.relname,regexp_split_to_table (array_to_string (d.attrnums, ','),',')::int as attnu
 FROM gp_distribution_policy d 
 LEFT JOIN pg_class c ON c.oid = d.localoid 
 LEFT JOIN pg_namespace n ON n.oid = c.relnamespace  
 WHERE c.oid = 'ods.oracletest'::regclass
) att
LEFT JOIN pg_attribute a ON a.attrelid = att.oid
WHERE att.attnu = a.attnum
GROUP BY 1,2;

--查询前20张大表
SELECT tabs.nspname AS schema_name,
       COALESCE(parts.tablename, tabs.relname) AS table_name,
       ROUND(SUM(sotaidtablesize) / 1024 / 1024, 3) AS table_MB,
       ROUND(SUM(sotaidtablesize) / 1024 / 1024 / 1024, 3) AS table_GB,
       ROUND(SUM(sotaididxsize) / 1024 / 1024 / 1024, 3) AS index_GB,
       ROUND(SUM(sotaididxsize) / 1024 / 1024, 3) AS index_MB
FROM gp_toolkit.gp_size_of_table_and_indexes_disk sotd,
    (
    SELECT c.oid, c.relname, n.nspname
    FROM pg_class c, pg_namespace n
    WHERE n.oid = c.relnamespace
    AND c.relname NOT LIKE '%_err'
    ) tabs
LEFT JOIN pg_partitions parts 
ON tabs.nspname = parts.schemaname
AND tabs.relname = parts.partitiontablename
WHERE sotd .sotaidoid = tabs.oid
GROUP BY tabs.nspname, COALESCE(parts.tablename, tabs.relname)
ORDER BY 4 DESC
LIMIT 20;

--资源队列属性视图
SELECT * FROM pg_catalog.pg_resqueue_attributes;



--查询Filespace和Locating
SELECT a.dbid,a.content,a.role,a.port,a.hostname,b.fsname,c.fselocation FROM gp_segment_configuration a,pg_filespace b,pg_filespace_entry c WHERE a.dbid=c.fsedbid AND b.oid=c.fsefsoid ORDER BY 2,6;

--日期计算:
riqi::date + interval '1 month' --"riqi"增加1月
riqi::date - interval '1 day'   --"riqi"减去1天

 

posted on 2020-03-20 15:45  kingle-l  阅读(1031)  评论(0编辑  收藏  举报

levels of contents