Greenplum 元數據信息

Greenplum 元數據信息

Greenplum 元數據信息
1、 獲取集羣中數據庫信息
  1.1 集羣中的創建的數據庫信息
  1.2 查看每個數據庫的儲存大小
  1.3 查看集羣中hostname
  1.4 查看集羣數據庫的版本信息
  1.5 查看集羣master與segment安裝的信息
2、 查看數據庫下schema信息
  2.1 查看數據庫下創建的schema信息
  2.2 查看數據庫下每個schema的大小
3、 查看schema下表的信息
  3.1 查看schema下的表的清單
  3.2 查看錶的字段的信息
  3.3 查看schema下的每個表的大小
  3.4 獲取表的生命週期
  3.5 獲取表的膨脹率
  3.6 查看錶的傾斜率
  3.7 查看需要Analyze的表
  3.8 查看錶的字段個類型信息
  3.9 查看錶字段的註釋信息
  3.10 查看數據庫中的 AO 表
  3.11 查看數據庫中的堆表
  3.12 查看external外部表信息
  3.13 查看超過1GB傾斜率的表
4 查看集羣中用戶相關的信息
  4.1 集羣中創建的用戶信息
  4.2 用戶創建的表信息
5 集羣中Function的信息
  5.1 查看創建的所有Function的信息
  5.2 查看制定schema下的Function信息
6 集羣中資源隊列的信息
  6.1 查看創建的資源隊列
  6.2 查看資源隊列的參數配置
7 查看索引相關的信息
  7.1 查看索引大小超過表總大小 1/2  的系統表大小和索引大小
  7.2 查看制定schem上表的索引
  7.3 索引活躍度IO活躍度查看
  7.4 索引活躍度IO訪問活躍度
  7.5 查看 Master與Segment上索引不一致的問題
  7.6 查看索引的使用次數
8 集羣中正在運行的SQL信息
  8.1 查看正在運行的SQL信息
  8.2 查看SQL的鎖
9 查詢數據庫與表的年齡
  9.1 查詢數據庫的年齡
  9.2 查詢表的年齡

1、獲取集羣中數據庫信息

1.1 集羣中的創建的數據庫信息

select datname from pg_database where datname not in ('template1','template0','postgres');

1.2 查看每個數據庫的儲存大小

select pg_size_pretty(pg_database_size('databases')) as databasesize, 'databases' as databasename


databases : 數據庫信息

1.3 查看集羣中hostname

SELECT hostname FROM gp_segment_configuration group by hostname order by hostname;

1.4 查看集羣數據庫的版本信息

select version()

1.5 查看集羣master與segment安裝的信息

SELECT * FROM gp_segment_configuration;content爲-1的爲master節點,

2、查看數據庫下schema信息

2.1 查看數據庫下創建的schema信息

select nspname as schemaname from pg_namespace where nspname!~'pg_*' ORDER BY nspname

2.2 查看數據庫下每個schema的大小

select pg_size_pretty(cast(sum(pg_relation_size( schemaname || '.' || tablename)) as bigint)), schemaname
from pg_tables t inner join pg_namespace d on t.schemaname=d.nspname group by schemaname;

3、查看schema下表的信息

3.1 查看schema下的表的清單

select 'schemaname '||'.'||c.relname as tablenamefrom pg_catalog.pg_class c, pg_catalog.pg_namespace nwheren.oid = c.relnamespaceand n.nspname='schemaname 'and pc.relstorage IN ('type')



schemaname : schema的名字type:a和c是AO表,h是heap表,x是外表

3.2 查看錶的字段的信息

select table_schema||'.'||table_name as tablename,column_name,case character_maximum_length is null  when 't' then data_typeelse data_type||'('||character_maximum_length||')' end as character_maximum_lengthfrom information_schema.columns where table_schema='schema'and table_name='tablename';schema :  

​​​​​​​schema的信息Tablename : 表的名字

3.3 查看schema下的每個表的大小

select schemaname||'.'||tablename,pg_relation_size(schemaname || '.' || tablename)/1024/1024/1024 as tablesizefrom pg_tables t inner join pg_namespace d on t.schemaname=d.nspnameand nspname='schema 'ORDER BY tablesize desclimit 100;

​​​​​​​schema :  schema的信息

3.4 獲取表的生命週期

select staactionname,stausename,stasubtype,to_char(statime,'yyyy-mm-dd hh24:mm:ss')||'' as statimefrom pg_stat_last_operation where objid = 'tablename'::regclass  order by statime desctablename : 表的名字

3.5 獲取表的膨脹率

select percent_hiddenfrom gp_toolkit.__gp_aovisimap_compaction_info('main.t_ent_baseinfo'::regclass)ORDER BY percent_hidden desc;

3.6 查看錶的傾斜率

SELECT max(c) AS MaxSegRows, min(c) AS MinSegRows,
substr((max(c)-min(c))*100.0/max(c)||'',0,8) AS PercentageDifferenceBetween
FROM (SELECT count(*) c, gp_segment_id FROM tablename
GROUP BY 2) AS a
​​​​​​​
atablename : 表的名字

3.7 查看需要Analyze的表

select smischema||'.'||smitable as tablename,smisize,smicols,smirecs from gp_toolkit.gp_stats_missing where smisize='f' limit 10;

3.8 查看錶的字段個類型信息

select table_schema||'.'||table_name as tablename,column_name,case character_maximum_length is null  when 't' then data_typeelse data_type||'('||character_maximum_length||')' end as character_maximum_lengthfrom information_schema.columns where table_schema='schema'and table_name='tablename';schema :schem信息tablename : 表的名字

3.9 查看錶字段的註釋信息

SELECT 'tablename' as table_name      ,a.attname                            AS column_name      ,format_type(a.atttypid, a.atttypmod) AS data_type      ,d.description                        AS description         ,a.attnum      ,a.attnotnull                         AS notnull      ,coalesce(p.indisprimary, FALSE)      AS primary_key      ,f.adsrc                              AS default_valFROM   pg_attribute    aLEFT   JOIN pg_index   p ON p.indrelid = a.attrelid AND a.attnum = ANY(p.indkey)LEFT   JOIN pg_description d ON d.objoid  = a.attrelid AND d.objsubid = a.attnumLEFT   JOIN pg_attrdef f ON f.adrelid = a.attrelid  AND f.adnum = a.attnumWHERE  a.attnum > 0AND    NOT a.attisdroppedAND    a.attrelid = 'tablename'::regclassORDER  BY a.attnum;tablename : 表的名字

3.10 查看數據庫中的 AO 表

select t2.nspname, t1.relname from pg_class t1, pg_namespace t2where t1.relnamespace=t2.oid and relstorage in ('c', 'a');

3.11 查看數據庫中的堆表

select t2.nspname, t1.relname from pg_class t1, pg_namespace t2where t1.relnamespace=t2.oid and relstorage in ('h') and relkind='r';

3.12 查看external外部表信息

select t2.nspname, t1.relname from pg_class t1, pg_namespace t2where t1.relnamespace=t2.oid and relstorage in ('x') and relkind='r';

3.13 查看超過1GB傾斜率的表

SELECT'select ''' || schemaname || '.' || tablename || ''' tabname' ||E' ,pg_size_pretty(sum(pg_relation_size(\'' || schemaname || '.' || tablename || E'\'))::bigint) assum_relation_size' ||E' ,pg_size_pretty(max(pg_relation_size(\'' || schemaname || '.' || tablename || E'\'))) as max_relation_size'||E' ,pg_size_pretty(min(pg_relation_size(\'' || schemaname || '.' || tablename || E'\'))) as min_relation_size'||E' ,max(pg_relation_size(\'' || schemaname || '.' || tablename || E'\'))::numeric(100,1)/min(pg_relation_size(\'' || schemaname || '.' || tablename || E'\')) skew_info ' || E' from gp_dist_random(\'gp_id\') ' || E'wherepg_relation_size(\'' || schemaname || '.' || tablename || E'\' )<>0;' as question_table_sqlFROMpg_tablesWHEREpg_relation_size(tablename) > 1.0 * 1024 * 1024 * 1024and schemaname not in ('information_schema','pg_catalog')ORDER BYpg_relation_size(tablename)DESC;

4 查看集羣中用戶相關的信息

4.1 集羣中創建的用戶信息

select rolname,case rolsuper when 't' then '是管理員' when 'f' then '不是管理員'end as rolsuper, case rolcreaterole when 't' then '可以創建角色' when 'f' then '不可以創建角色' end as rolcreaterole,case rolcreatedb  when 't' then '可以創建DB' when 'f' then '不可以創建DB' end as rolcreatedb,case rolcanlogin  when 't' then '可以登錄' when 'f' then '不可以登錄' end as rolcanlogin,case rolconnlimit when  '-1' then '沒有限制' else '有限制' end as rolconnlimit,case  when rolvaliduntil is null  then '永不失效' else '有失效時間' end as rolvaliduntil,rsqnamefrom pg_roles,gp_toolkit.gp_resqueue_status where rolname not like 'gpcc%' and pg_roles.rolresqueue=gp_toolkit.gp_resqueue_status.queueidorder by rolname

4.2 用戶創建的表信息

select grantee,table_schema||'.'||table_name as tablename,
privilege_type,is_grantablefrom information_schema.table_privileges 
where grantee= 'gpadmin'  limit 100

5 集羣中Function的信息

5.1 查看創建的所有Function的信息

SELECT pg_proc.proname AS proname,pg_type.typname AS typename,pg_proc.pronargs
 AS argscount FROM pg_proc JOIN pg_type ON(pg_proc.prorettype = pg_type.oid) 
​​​​​​​WHERE pg_type.typname != 'void'and pg_proc.proname like 'sp_%' ORDER BY pg_proc.proname ;

void : 返回的類型

sp_% : 函數的前綴

5.2 查看制定schema下的Function信息

SELECT pg_proc.proname AS proname,pg_type.typname AS typename,pg_proc.pronargs AS argscount FROM pg_proc JOIN pg_type ON(pg_proc.prorettype = pg_type.oid) WHERE pg_type.typname != 'void'and pg_proc.proname like 'sp_%' and pronamespace = (SELECT pg_namespace.oid FROM pg_namespace WHERE nspname = 'schema' )ORDER BY pg_proc.proname ;

void : 返回的類型
sp_% : 函數的前綴
​​​​​​​schema : 制定的schema的信息

6 集羣中資源隊列的信息

6.1 查看創建的資源隊列

select  * from pg_resqueue

6.2 查看資源隊列的參數配置

select rsqname,resname,ressetting from pg_resqueue_attributes

7 查看索引相關的信息

7.1 查看索引大小超過表總大小 1/2 的系統表大小和索引大小

select indrelid::regclass tab,pg_size_pretty(avg(pg_total_relation_size(indrelid))::bigint) all_size,case when avg(pg_relation_size(indrelid))+ sum(pg_total_relation_size(indexrelid)) = avg(pg_total_relation_size(indrelid)) then 't' else 'f' end,pg_size_pretty(avg(pg_relation_size(indrelid))::bigint) tab_size,pg_size_pretty(sum(pg_total_relation_size(indexrelid))::bigint) all_idx_sizefrom pg_indexwhere indrelid in (select oid from pg_class where relnamespace in (select oid frompg_namespace wherenspname='pg_catalog') )group by indrelid::regclasshaving sum(pg_total_relation_size(indexrelid))::bigint >=avg(pg_total_relation_size(indrelid))::bigint * 0.5order by sum(pg_total_relation_size(indexrelid)) desclimit  50;獲取的前50條信息

7.2 查看制定schem上表的索引

select * from pg_stat_all_indexes where schemaname in ('schema1',[schema2,schema3]);

7.3 索引活躍度IO活躍度查看

select * from pg_catalog.pg_statio_all_indexes where schemaname in ('schema1',[schema2,schema3]);

7.4 索引活躍度IO訪問活躍度

select * from pg_catalog.pg_stat_all_indexes where schemaname in ('schema1',[schema2,schema3]);

7.5 查看 Master與Segment上索引不一致的問題

以下語句只要有數據輸出表示有的表索引不一致,請進一步查看
select distinct n.nspname,c.relname from gp_dist_random('pg_class') r ,pg_classc,pg_namespace nwhere r.oid=c.oid and r.relhasindex<>c.relhasindexand c.relnamespace = n.oid limit 10

7.6 查看索引的使用次數

selectrelname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetchfrompg_stat_user_indexesorder byidx_scan asc, idx_tup_read asc, idx_tup_fetch asc;

8 集羣中正在運行的SQL信息

8.1 查看正在運行的SQL信息

select datname,procpid,usename,current_query,waiting, 
to_char(query_start,'yyyy-mm-dd hh24:mm:ss') as query_start,
to_char(backend_start,'yyyy-mm-dd hh24:mm:ss') as backend_start,((substr(now()||'',0,20)::timestamp) - (substr(query_start||'',0,20)::timestamp))||'' 
as takingTime,client_addr,application_name,waiting_reason  from pg_stat_activity where current_query <> ''order by takingTime desc

8.2 查看SQL的鎖

select b.query_start,a.* fromgp_toolkit.gp_locks_on_relation a, 
pg_stat_activity b where a.lorpid=b.procpidand a.lorrelname
 not like 'pg_%' and a.lorrelname not like 'gp_%' order by 1desc;

9 查詢數據庫與表的年齡

9.1 查詢數據庫的年齡

select datname,age(datfrozenxid) from pg_database where age(datfrozenxid) > 1500000000
1500000000: 15億的年齡
如果超過15億,建議用戶在業務空閒時間段,執行:
 set vacuum_freeze_min_age = 0; 
 vacuum freeze;

9.2 查詢表的年齡

select * from (select pt.schemaname||'.'||ts.relname as tablename,pg_relation_size(pt.schemaname||'.'||ts.relname)/1024/1024/1024 as tablesizegb,ts.relfrozenxid,ts.stausename,ts.stasubtype,ts.statime from (SELECT ps.stausename,ps.staactionname,ps.stasubtype,pc.relname,ps.statime,age(relfrozenxid) as relfrozenxidFROM pg_stat_last_operation ps,pg_class pc WHERE ps.objid = pc.oidand age(pc.relfrozenxid) > 1500000000) ts,pg_tables ptwhere ts.relname = pt.tablenameorder by tablesizegb desc) txdwhere txd.tablesizegb > 0

1500000000 : 15 億的年齡
tablename : 表的名字
如果超過 15 億,建議用戶在業務空閒時間段,執行:
set vacuum_freeze_min_age = 0;
vacuum freeze tablename;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章