4.1 oid无处不在
oid是递增的,每一个表空间、表、索引、数据文件名、函数、约束等都对应有一个唯一标识的oid。全局递增。
select attrelid::regclass,attname
from pg_attribute a, pg_class b
where a.attrelid = b.oid
and b.relnamespace=11
and atttypid=26
and b.relstorage='h'
and attname='oid'
and b.relname not like '%index';
4.2 数据库集群信息
Greenplum的集群配置信息在master上面,这些信息可以了解整个集群的状况,可以得知是否有节点失败,可以修改这些配置实现集群的扩容等操作。
4.2.1 Gp_configuration和gp_segment_configuration
4.2.2 Gp_id
4.3 常用数据字典
4.3.1 pg_class
保存着所有表、视图、序列、索引的原数据信息,每一个DDL/DML操作都必须跟这个表发生联系。
4.4 分区表信息
4.4.1 如何实现分区表
分区的意思是把逻辑上的一个大表分隔成物理上的几块。GP中的分区表的实现基本上与PostgreSQL中的实现的原理一样都是通过表继承、规则、约束来实现的。
继承关系放在pg_inherts这个数据字典中。
4.4.2 pg_partition
一个表是否是分区表保存在pg_partition中,如果一个表是分区表(不包括子分区),则对应有一条记录在这个数据字典中。
查询一个表是否是分区表,只要将pg_partition与pg_class关联,然后执行count即可,如果这个表中有数据,则为分区表
select count(*) from pg_partition where parrelid = 'public.cxfa3'::regclass;
4.4.3 pg_partition_rule
保存分区表的分区规则。可以找到一个分区表对应的子表有哪些以及分区规则等信息。
4.4.4 pg_partitions视图及其优化
下面定义一个函数代替pg_partitions视图,充分利用索引,查询是通过regclass减少表的关联大大提高查询的效率。
create view public.v_pg_partitions
as
select pp.parrelid tableoid,prl.parchilddrelid,prl.parname as partitionname,
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,
case when pg_get_expr(prl.parrangeend,prl.parchildrelid) = '' then
pg_get_expr(prl.parlistvalues,prl.parchildrelid) else pg_get_expr(prl.parrangeend,prl.parchildrelid) end as HIGH_VALUE,
pg_get_partition_rule_def(prl.oid,true) as partitionboundary,
prl.parruleord as partitionposition
FROM pg_partition pp, pg_partition_rule prl
where pp.paristemplate = false and prl.paroid=pp.oid;
4.4 自定义类型以及类型转换
哪两种类型之间是可以转换的,哪两种不能,转换的规则是什么,这些都定义在了pg_cast中。
自定义类型转换
先定义一个类型转换函数:
create or replace function regclass2text(a regclass)
returns text
as $$
return a;
$$ language plpythonu;
在定义一个cast类型转换规则:
create cast(regclass as text) with function regclass2text(a regclass);
这样就定义好了一个cast类型转换规则。
4.7 数据字典应用示例
4.7.1 获取表的字段信息
表名放在pg_class中,schema名放在pg_namespace中,字段信息放在pg_attribute中。
select a.attname,pg_catalog.format_type(a.atttypid,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;