一、数据库集群信息

1、gp_segment_configration

2、pg_filespace_entry

这两个表是在pg_global表空间下面的,是全局表。

用来查看集群segment信息,比如segment个数。

二、常用数据字典表

1、pg_class

保存了所有表、视图、序列、索引元数据信息,每个DDL/DML操作都必须跟这个表发生关系。

2、pg_attribute

记录字段的内容

3、gp_distribution_policy

记录表的分布键

4、pg_statistic 和 pg_stats

数据库中表的统计信息保存在pg_statistic中

pg_stats可以方便帮我们查看pg_statistic的内容

5、pg_partition

记录分区表的信息

6、pg_partition_rule

分区表的分区规则

7、pg_partitions

三、字典表的应用

1、查看表的信息

1)从catalog中查询

2)从information_schema查询

select   *  from information_schema.tables where table_name ='';

1、获取字段信息

1)从catalog中查询

SELECT a.attname,pg_catalog.format_type(a.atttypid,( pg_catalog.pg_attribute a, a.atttypmod) AS data_type
FROM pg_catalog.pg_attribute a,
 (SELECT c. oid 
 FROM pg_catalog.pg_class c 
 LEFT JOIN pg_catalog.pg_namespace n 
 ON n.oid = c.relnamespace 
 WHERE c.relname = 'pg_class' 
 AND n.nspname = 'pg_catalog' 
 ) b  
WHERE a.attrelid = b.oid 
AND a.attnum > 0 
AND NOT a.attisdropped ORDER BY a.attnum; 
使用regclass就会简化很多:
SELECT a.attname,pg_catalog.format_type(a.atttypid, a.atttypmod) AS data_type
FROM pg_catalog.pg_attribute a
WHERE a.attrelid ='pg_catalog.pg_class'::regclass
AND a.attnum > 0
AND NOT a.attisdropped ORDER BY a.attnum;
2)从information_schema查询
select * from information_schema.columns where table_name = '';
2、获取表的分布键

select a.attrnums[i.i],b.attname,a.localoid::regclass

from gp_distribution_policy a,

(select generate_series(1,10))i (i),

pg_attribute b

where a.attrnums[i.i) is not null

and a.localoid=b.attrelid

and a.attrnums[i.i]=b.attnum

and a.localoid='public.cxfa2 '::regclass

order by i.i;

3、获取一个视图定义

testDB=# create table cxfa( a int) distributed by (a);
CREATE TABLE
testDB=# create view v_cxfa as select * from cxfa;
CREATE VIEW
testDB=# select pg_get_viewdef('v_cxfa', true);
SELECT cxfa.a FROM cxfa;
(1 row)

4、查询备注信息

1)获取表的备注

select COALESCE(description, * *) as comment from pg_description where objoid=‘cxfa*::regclass and objsubid=0;

2)获取字段的备注

select  b.attname as columnname, COALESCE(a.description,'') as comment

from      pg_catalog.pg_description a,pg_catalog.pg_attribute b

where  objoid='cxfa'::regclass

and a.objoid=b.attrelid

and a.objsubid=b.attnum;

5、获取数据据库建表语句

1)调用plpythonu

CREATE PROCEDURAL LANGUAGE plpythonu;

2)创建存储过程

create or replace function get_table_structure(tablename text)    # 表名格式为 tableschema.table_name
    returns text
as $$
    try:
        table_name = tablename.lower().split('.')[1]
        talbe_schema=tablename.lower().split('.')[0]
    except (IndexError):
        return 'Please in put "tableschema.table_name"'
    get_table_oid="select oid,reloptions,relkind from pg_class where oid='%s'::regclass"%(tablename)
    try:
        rv_oid=plpy.execute(get_table_oid,5)      #plpy.execute(query [, max-rows])
        if not rv_oid:
            return 'Did not find any relation named"'+tablename +'".'
    except (Error):
        return 'Did not find any relation named"'+tablename +'".'
    table_oid=rv_oid[0]['oid']
    rv_reloptions=rv_oid[0]['reloptions'] # 访问方法特定的选项,使用"keyword=value"格式的字符串
    rv_relkind=rv_oid[0]['relkind']
    create_sql="";
    table_kind='table';
    if rv_relkind !='r' and rv_relkind !='v':  # r:普通表, v:视图, c:复合类型, t:表, o:内部 AO 节点文件,
        plpy.error('%s is not table or view'%(tablename));
    elif rv_relkind=='v':
        get_view_def="select pg_get_viewdef(%s,'t') as viewdef;" % (table_oid)
        rv_viewdef=plpy.execute(get_view_def);
        create_sql='create view %s as \n' % (tablename)
        create_sql += rv_viewdef[0]['viewdef']+'\n';
        table_kind='view'
    else:
       #获取行
      get_columns="select a.attname,pg_catalog.format_type(a.atttypid,a.atttypmod),\
       (select substring(pg_catalog.pg_get_expr(d.adbin,d.adrelid) for 128) \
        from pg_catalog.pg_attrdef d where d.adrelid=a.attrelid and d.adnum=a.attnum and a.atthasdef) \
        as default,a.attnotnull as isnull from pg_catalog.pg_attribute \
        a where a.attrelid= %s and a.attnum >0 and not a.attisdropped order by a.attnum;" % (table_oid);
        rv_columns=plpy.execute(get_columns)
       
        #获取分布键
        get_table_distribution1="select attrnums from pg_catalog.gp_distribution_policy t where localoid = '" + table_oid + "' "
        rv_distribution1=plpy.execute(get_table_distribution1,500)
        rv_distribution2=''
        if rv_distribution1 and rv_distribution1[0]['attrnums']:
            get_table_distribution2="select attname from pg_attribute where attrelid='"+table_oid+"' and attnum in (" + str(rv_distribution1[0]['attrnums']).strip('{').strip('}').strip('[').strip(']')+")"
            rv_distribution2=plpy.execute(get_table_distribution2,500)
 
        create_sql='create table %s (\n' % (tablename)
        #获取索引
        get_index="select pg_get_indexdef(indexrelid) as indexdef from pg_index where indrelid=%s" % (table_oid);
        rv_index=plpy.execute(get_index);
 
        get_parinfo1="select attname as columnname from pg_attribute where attnum =(select paratts[0] from pg_partition where parrelid=%s) and attrelid=%s;"%(table_oid,table_oid);
        get_parinfo2=""" select pp.parrelid,prl.parchildrelid,case when pp.parkind='h'::"char" then 'hash'::text when pp.parkind='r'::"char" then 'range'::text when pp.parkind='l'::"char" then 'list'::text else null::text end as partitiontype,pg_get_partition_rule_def(prl.oid,true) as partitionboundary from pg_partition pp,pg_partition_rule prl where pp.paristemplate=false and pp.parrelid = %s and prl.paroid = pp.oid order by prl.parname; """ % (table_oid)
        v_par_parent=plpy.execute(get_parinfo1);
        v_par_info=plpy.execute(get_parinfo2);
        max_column_len=10
        max_type_len=4
        max_modifiers_len=4
        max_default_len=4
        for i in rv_columns:
            if i['attname']:
                if max_column_len < i['attname'].__len__():
                    max_column_len=i['attname'].__len__()
            if i['format_type']:
                if max_type_len < i['format_type'].__len__():
                    max_type_len=i['format_type'].__len__()
            if i['default']:
                if max_type_len < i['default'].__len__():
                    max_default_len=i['default'].__len__()
        first=True
        for i in rv_columns:
            if first==True:
                split_char=' ';
                first=False
            else:
                split_char=',';
            if i['attname']:
                create_sql += " " + split_char + i['attname'].ljust(max_column_len+6)+''
            else:
                create_sql += "" + split_char + ' '.ljust(max_column_len+6)
            if i['format_type']:
                create_sql += ' ' + i['format_type'].ljust(max_type_len +2)
            else:
                create_sql += ' ' + ' '.ljust(max_type_len+2)
            if i['isnull'] and i['isnull']:
                create_sql += ' ' + ' not null '.ljust(8)
            if i['default']:
                create_sql += ' default ' + i['default'].ljust(max_default_len+6)
            create_sql += "\n"
        create_sql += ")"
 
        if rv_reloptions:
            create_sql +=" with ("+str(rv_reloptions).strip('{').strip('}').strip('[').strip(']') +")\n"
            create_sql = create_sql.replace("'",'')
        if rv_distribution2:
            create_sql += 'Distributed by ('
            for i in rv_distribution2:
                create_sql += i['attname'] + ','
            create_sql =create_sql.strip(',')+')'
        elif rv_distribution1:
            create_sql += 'Distributed randomly\n'
        if v_par_parent:
            partitiontype=v_par_info[0]['partitiontype'];
            create_sql +='\nPARTITION BY '+ partitiontype + "("+v_par_parent[0]['columnname']+")\n(\n";
            for i in v_par_info:
                create_sql +=" " +i['partitionboundary']+',\n';
            create_sql=create_sql.strip(',\n');
            create_sql+="\n)"
        create_sql+=";\n\n"
        for i in rv_index:
            create_sql += i['indexdef']+';\n'
 
        get_table_comment="select 'comment on %s %s is '''|| COALESCE (description,'')|| '''' as comment from pg_description where objoid=%s and objsubid=0;" % (table_kind,tablename,table_oid)
        get_column_comment="select 'comment on column %s.'||b.attname ||' is ''' || COALESCE(a.description,'')|| ''' ' as comment from pg_catalog.pg_description a,pg_catalog.pg_attribute b where objoid=%s and a.objoid=b.attrelid and a.objsubid=b.attnum;" % (tablename,table_oid)
        rv_table_comment=plpy.execute(get_table_comment);
        rv_column_comment=plpy.execute(get_column_comment);
 
        for i in rv_table_comment:
            create_sql += i['comment']+';\n'
        for i in rv_column_comment:
            create_sql +=i['comment']+';\n'
        return create_sql;
 
$$ LANGUAGE plpythonu;
3)使用例子
testdb=# SELECT get_table_structure('public.tb1_partition_range_yyyymmdd');
                                                get_table_structure                                               
-------------------------------------------------------------------------------------------------------------------
 create table public.tb1_partition_range_yyyymmdd (                                                               
   id               numeric,
   yyyymmdd         date                                                                                          
 ) with (appendonly=true, compresslevel=5)                                                                        
 Distributed by (id)                                                                                              
 PARTITION BY range(yyyymmdd)                                                                                     
 (                                                                                                                
  PARTITION p20120811 START ('2012-08-11'::date) END ('2012-08-12'::date) WITH (appendonly=true, compresslevel=5),
  PARTITION p20120812 START ('2012-08-12'::date) END ('2012-08-13'::date) WITH (appendonly=true, compresslevel=5) 
 );                                                                                                               
                                                                                                                  
 CREATE INDEX idx_yyyymmdd ON tb1_partition_range_yyyymmdd USING btree (yyyymmdd);    
 
5、查看表的依赖关系
pg_depend

Greenplum 常用数据字典的更多相关文章

  1. 常用数据字典---bai

    --常用数据字典 -- system: normal; sysdba --查询所有的逻辑对象.所有. select count(1) from dba_objects; select * from d ...

  2. Oracle中的数据字典技术及常用数据字典总结

    一.Oracle数据字典 数据字典是Oracle存放有关数据库信息的地方,其用途是用来描述数据的.比如一个表的创建者信息,创建时间信息,所属表空间信息,用户访问权限信息等.当用户在对数据库中的数据进行 ...

  3. Greenplum常用的gp_toolkit & pg_catalog监控语句

    gp_toolkit 说明 Greenplum数据库提供了一个名为gp_tooikit的管理schema,该schema下有关于查询系统目录,日志文件, 用户创建(databases,schema,t ...

  4. Greenplum 常用数据库管理语句,sql工具

    转载自:https://blog.csdn.net/you_xian/article/details/78549756作者:lianghc      在greenplum 使用过程中积累的一些常用查询 ...

  5. greenplum(postgresql) 数据字典

    greenplum是基于postgresql开发的分布式数据库,里面大部分的数据字典是一样的.我们在维护gp的时候对gp的数据字典比较熟悉,特此分享给大家.在这里不会详细介绍每个字典的内容,只会介绍常 ...

  6. 转://工作中 Oracle 常用数据字典集锦

    DBA工作中数据字典就等同于我们本和笔,时时刻刻也分不开的,不管是看状态,还是监控,都需要数据字典的支持,本文整理出来常用的数据字典系列,帮助大家来记住和汇总以便查询利用 ALL_CATALOG Al ...

  7. Oracle11g常用数据字典

    转:https://blog.csdn.net/fulq1234/article/details/79760698 Oracle数据字典的名称由前缀和后缀组成,使用_连接,含义说明如下: dba_:包 ...

  8. Oracle 中常用数据字典大总结

    原文出处:小宝马的爸爸 - 梦想的家园 前面呢,也断断续续的介绍了一些诸如 Sql*Plus 等等关于 Oracle 的基本的内容, 对于 Oracle 这样的大型数据库呢,自身的运行和维护也是个不得 ...

  9. GreenPlum 常用命令

    gpstate 命令 参数 作用 gpstate -b => 显示简要状态 gpstate -c => 显示主镜像映射 gpstart -d => 指定数据目录(默认值:$MASTE ...

随机推荐

  1. Python--对list、tuple、dict的操作

    一.List(列表) 首先,创建一个简单的list: animal = ['cat','dog','lion','tiger'] (1) 用索引的方式访问list中的元素:animal[0] 当索引从 ...

  2. golang之 iota 常量生成器

    常量声明可以使用iota常量生成器初始化,它用于生成一组以相似规则初始化的常量,但是不用每行都写一遍初始化表达式.在一个const声明语句中,在第一个声明的常量所在的行,iota将会被置为0,然后在每 ...

  3. PAT甲级题分类汇编——树

    本文为PAT甲级分类汇编系列文章. AVL树好难!(其实还好啦~) 我本来想着今天应该做不完树了,没想到电脑里有一份讲义,PPT和源代码都有,就一遍复习一遍抄码了一遍,更没想到的是编译一遍通过,再没想 ...

  4. 用mkdocs在gitee码云上建立一个简单的文档博客

    利用mkdocs建立简单的文档博客 一.概述 MkDocs 是一个用于创建项目文档的 快速, 简单 , 完美华丽 的静态站点生成器. 文档源码使用 Markdown 来撰写, 用一个 YAML 文件作 ...

  5. C++ 数组和vector的基本操作

    1.静态数组的基本操作 int a[5] = {0, 3, 4, 6, 2}; 1.1 数组的遍历 1.1.1 传统的for循环遍历 int size = sizeof(a) / sizeof(*a) ...

  6. Hibernate定义

    Hibernate:(Object Relational Mapping)对象关系映射   对象关系映射就是在操作数据库之前,先将数据库的表和实体类关联起来,通过实体类的对象,就可以操作数据库,开发人 ...

  7. Harbor 批量清理历史镜像

    公司 Harbor 仓库一年多没清理,硬盘被堆满了,为此写了个批量清除的 Python 脚本. 源代码见 github-harbor_clean.py # coding: utf-8 from ope ...

  8. 详解Ubuntu16.04安装Python3.7及其pip3并切换为默认版本(转)

    原文:https://www.jb51.net/article/156927.htm

  9. Linux mount/unmount 挂载和卸载指令

    对于Linux用户来讲,不论有几个分区,分别分给哪一个目录使用,它总归就是一个根目录.一个独立且唯一的文件结构 Linux中每个分区都是用来组成整个文件系统的一部分,她在用一种叫做“挂载”的处理方法, ...

  10. windows上安装python虚拟环境

    一.windows上安装python虚拟环境 1.安装pip install virtualenvvirtualenv --version 2.新建一个python虚拟环境virtual_env_01 ...