本文整理自网络,侵删。
一、pg_class:
该系统表记录了数据表、索引(仍然需要参阅pg_index)、序列、视图、复合类型和一些特殊关系类型的元数据。注意:不是所有字段对所有对象类型都有意义。
名字 | 类型 | 引用 | 描述 |
relname | name | 数据类型名字。 | |
relnamespace | oid | pg_namespace.oid | 包含这个对象的名字空间(模式)的OI。 |
reltype | oid | pg_type.oid | 对应这个表的行类型的数据类型。 |
relowner | oid | pg_authid.oid | 对象的所有者。 |
relam | oid | pg_am.oid | 对于索引对象,表示该索引的类型(B-tree,hash)。 |
relfilenode | oid | 对象存储在磁盘上的文件名,如果没有则为0。 | |
reltablespace | oid | pg_tablespace.oid | 对象所在的表空间。如果为零,则表示使用该数据库的缺省表空间。(如果对象在磁盘上没有文件,这个字段就没有什么意义) |
relpages | int4 | 该数据表或索引所占用的磁盘页面数量,查询规划器会借助该值选择最优路径。 | |
reltuples | float4 | 表中行的数量,该值只是被规划器使用的一个估计值。 | |
reltoastrelid | oid | pg_class.oid | 与此表关联的TOAST表的OID,如果没有为0。TOAST表在一个从属表里"离线"存储大字段。 |
reltoastidxid | oid | pg_class.oid | 如果是TOAST表,该字段为它索引的OID,如果不是TOAST表则为0。 |
relhasindex | bool | 如果这是一个数据表而且至少有(或者最近有过)一个索引,则为真。它是由CREATE INDEX设置的,但DROP INDEX不会立即将它清除。如果VACUUM发现一个表没有索引,那么它清理 relhasindex。 | |
relisshared | bool | 如果该表在整个集群中由所有数据库共享,则为真。 | |
relkind | char | r = 普通表,i = 索引,S = 序列,v = 视图, c = 复合类型,s = 特殊,t = TOAST表 | |
relnatts | int2 | 数据表中用户字段的数量(除了系统字段以外,如oid)。在pg_attribute里肯定有相同数目的数据行。见pg_attribute.attnum. | |
relchecks | int2 | 表中检查约束的数量,参阅pg_constraint表。 | |
reltriggers | int2 | 表中触发器的数量;参阅pg_trigger表。 | |
relhasoids | bool | 如果我们为对象中的每行都生成一个OID,则为真。 | |
relhaspkey | bool | 如果该表存在主键,则为真。 | |
relhasrules | bool | 如表有规则就为真;参阅pg_rewrite表。 | |
relhassubclass | bool | 如果该表有子表,则为真。 | |
relacl | aclitem[] | 访问权限。 |
见如下应用示例:
代码如下:
#查看指定表对象testtable的模式
postgres=# SELECT relname,relnamespace,nspname FROM pg_class c,pg_namespace n WHERE relname = 'testtable' AND relnamespace = n.oid;
relname | relnamespace | nspname
-------------+--------------+---------
testtable | 2200 | public
(1 row)
#查看指定表对象testtable的owner(即role)。
postgres=# select relname,rolname from pg_class c,pg_authid au where relname = 'testtable' and relowner = au.oid;
relname | rolname
-------------+----------
testtable | postgres
(1 row)
#查看指定表对象testtable的模式
postgres=# SELECT relname,relnamespace,nspname FROM pg_class c,pg_namespace n WHERE relname = 'testtable' AND relnamespace = n.oid;
relname | relnamespace | nspname
-------------+--------------+---------
testtable | 2200 | public
(1 row)
#查看指定表对象testtable的owner(即role)。
postgres=# select relname,rolname from pg_class c,pg_authid au where relname = 'testtable' and relowner = au.oid;
relname | rolname
-------------+----------
testtable | postgres
(1 row)
二、pg_attribute:
该系统表存储所有表(包括系统表,如pg_class)的字段信息。数据库中的每个表的每个字段在pg_attribute表中都有一行记录。
名字 | 类型 | 引用 | 描述 |
attrelid | oid | pg_class.oid | 此字段所属的表。 |
attname | name | 字段名。 | |
atttypid | oid | pg_type.oid | 字段的数据类型。 |
attstattarget | int4 | attstattarget控制ANALYZE为这个字段设置的统计细节的级别。零值表示不收集统计信息,负数表示使用系统缺省的统计对象。正数值的确切信息是和数据类型相关的。 | |
attlen | int2 | 该字段所属类型的长度。(pg_type.typlen的拷贝) | |
attnum | int2 | 字段的编号,普通字段是从1开始计数的。系统字段,如oid,是任意的负数。 | |
attndims | int4 | 如果该字段是数组,该值表示数组的维数,否则是0。 | |
attcacheoff | int4 | 在磁盘上总是-1,但是如果装载入内存中的行描述器中, 它可能会被更新为缓冲在行中字段的偏移量。 | |
atttypmod | int4 | 表示数据表在创建时提供的类型相关的数据(比如,varchar字段的最大长度)。其值对那些不需要atttypmod的类型而言通常为-1。 | |
attbyval | bool | pg_type.typbyval字段值的拷贝。 | |
attstorage | char | pg_type.typstorage字段值的拷贝。 | |
attalign | char | pg_type.typalign字段值的拷贝。 | |
attnotnull | bool | 如果该字段带有非空约束,则为真,否则为假。 | |
atthasdef | bool | 该字段是否存在缺省值,此时它对应pg_attrdef表里实际定义此值的记录。 | |
attisdropped | bool | 该字段是否已经被删除。如果被删除,该字段在物理上仍然存在表中,但会被分析器忽略,因此不能再通过SQL访问。 | |
attislocal | bool | 该字段是否局部定义在对象中的。 | |
attinhcount | int4 | 该字段所拥有的直接祖先的个数。如果一个字段的祖先个数非零,那么它就不能被删除或重命名。 |
见如下应用示例:
代码如下:
#查看指定表中包含的字段名和字段编号。
postgres=# SELECT relname, attname,attnum FROM pg_class c,pg_attribute attr WHERE relname = 'testtable' AND c.oid = attr.attrelid;
relname | attname | attnum
-------------+----------+--------
testtable | tableoid | -7
testtable | cmax | -6
testtable | xmax | -5
testtable | cmin | -4
testtable | xmin | -3
testtable | ctid | -1
testtable | i | 1
(7 rows)
#只查看用户自定义字段的类型
postgres=# SELECT relname,attname,typname FROM pg_class c,pg_attribute a,pg_type t WHERE c.relname = 'testtable' AND c.oid = attrelid AND atttypid = t.oid AND attnum > 0;
relname | attname | typname
-------------+----------+---------
testtable | i | int4
(7 rows)
#查看指定表中包含的字段名和字段编号。
postgres=# SELECT relname, attname,attnum FROM pg_class c,pg_attribute attr WHERE relname = 'testtable' AND c.oid = attr.attrelid;
relname | attname | attnum
-------------+----------+--------
testtable | tableoid | -7
testtable | cmax | -6
testtable | xmax | -5
testtable | cmin | -4
testtable | xmin | -3
testtable | ctid | -1
testtable | i | 1
(7 rows)
#只查看用户自定义字段的类型
postgres=# SELECT relname,attname,typname FROM pg_class c,pg_attribute a,pg_type t WHERE c.relname = 'testtable' AND c.oid = attrelid AND atttypid = t.oid AND attnum > 0;
relname | attname | typname
-------------+----------+---------
testtable | i | int4
(7 rows)
三、pg_attrdef:
该系统表主要存储字段缺省值,字段中的主要信息存放在pg_attribute系统表中。注意:只有明确声明了缺省值的字段在该表中才会有记录。
名字 | 类型 | 引用 | 描述 |
adrelid | oid | pg_class.oid | 这个字段所属的表 |
adnum | int2 | pg_attribute.attnum | 字段编号,其规则等同于pg_attribute.attnum |
adbin | text | 字段缺省值的内部表现形式。 | |
adsrc | text | 缺省值的人可读的表现形式。 |
见如下应用示例:
代码如下:
#查看指定表有哪些字段存在缺省值,同时显示出字段名和缺省值的定义方式
postgres=# CREATE TABLE testtable2 (i integer DEFAULT 100);
CREATE TABLE
postgres=# SELECT c.relname, a.attname, ad.adnum, ad.adsrc FROM pg_class c, pg_attribute a, pg_attrdef ad WHERE relname = 'testtable2' AND ad.adrelid = c.oid AND adnum = a.attnum AND attrelid = c.oid;
relname | attname | adnum | adsrc
-------------+----------+---------+-------
testtable2 | i | 1 | 100
(1 row)
书籍
#查看指定表有哪些字段存在缺省值,同时显示出字段名和缺省值的定义方式
postgres=# CREATE TABLE testtable2 (i integer DEFAULT 100);
CREATE TABLE
postgres=# SELECT c.relname, a.attname, ad.adnum, ad.adsrc FROM pg_class c, pg_attribute a, pg_attrdef ad WHERE relname = 'testtable2' AND ad.adrelid = c.oid AND adnum = a.attnum AND attrelid = c.oid;
relname | attname | adnum | adsrc
-------------+----------+---------+-------
testtable2 | i | 1 | 100
(1 row)
四、pg_authid:
该系统表存储有关数据库认证的角色信息,在PostgreSQL中角色可以表现为用户和组两种形式。对于用户而言只是设置了rolcanlogin标志的角色。由于该表包含口令数据,所以它不是公共可读的。PostgreSQL中提供了另外一个建立在该表之上的系统视图pg_roles,该视图将口令字段填成空白。
名字 | 类型 | 引用 | 描述 |
rolname | name | 角色名称。 | |
rolsuper | bool | 角色是否拥有超级用户权限。 | |
rolcreaterole | bool | 角色是否可以创建其它角色。 | |
rolcreatedb | bool | 角色是否可以创建数据库。 | |
rolcatupdate | bool | 角色是否可以直接更新系统表(如果该设置为假,即使超级用户也不能更新系统表)。 | |
rolcanlogin | bool | 角色是否可以登录,换句话说,这个角色是否可以给予会话认证标识符。 | |
rolpassword | text | 口令(可能是加密的);如果没有则为NULL。 | |
rolvaliduntil | timestamptz | 口令失效时间(只用于口令认证);如果没有失效期,则为NULL。 | |
rolconfig | text[] | 运行时配置变量的会话缺省。 |
相关阅读 >>
更多相关阅读请进入《sql》频道 >>

数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
相关推荐
评论
管理员已关闭评论功能...
- 欢迎访问木庄网络博客
- 可复制:代码框内的文字。
- 方法:Ctrl+C。