PG中的系统表和系统视图(数据字典)
系统表
大多数系统表都是在数据库创建的过程中从模版数据库中拷贝过来的,因此都是数据库相关的。少数表是在整个安装中物理上所有数据库共享的;这些表在独立的表的描述中用指明了。
查看数据库系统表命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | lhrdb=# \dt pg_* List of relations Schema | Name | Type | Owner ------------+-------------------------+-------+---------- pg_catalog | pg_aggregate | table | postgres pg_catalog | pg_am | table | postgres pg_catalog | pg_amop | table | postgres pg_catalog | pg_amproc | table | postgres pg_catalog | pg_attrdef | table | postgres pg_catalog | pg_attribute | table | postgres pg_catalog | pg_auth_members | table | postgres pg_catalog | pg_authid | table | postgres pg_catalog | pg_cast | table | postgres pg_catalog | pg_class | table | postgres pg_catalog | pg_collation | table | postgres pg_catalog | pg_constraint | table | postgres pg_catalog | pg_conversion | table | postgres pg_catalog | pg_database | table | postgres pg_catalog | pg_db_role_setting | table | postgres pg_catalog | pg_default_acl | table | postgres pg_catalog | pg_depend | table | postgres pg_catalog | pg_description | table | postgres pg_catalog | pg_enum | table | postgres pg_catalog | pg_event_trigger | table | postgres pg_catalog | pg_extension | table | postgres pg_catalog | pg_foreign_data_wrapper | table | postgres pg_catalog | pg_foreign_server | table | postgres pg_catalog | pg_foreign_table | table | postgres pg_catalog | pg_index | table | postgres pg_catalog | pg_inherits | table | postgres pg_catalog | pg_init_privs | table | postgres pg_catalog | pg_language | table | postgres pg_catalog | pg_largeobject | table | postgres pg_catalog | pg_largeobject_metadata | table | postgres pg_catalog | pg_namespace | table | postgres pg_catalog | pg_opclass | table | postgres pg_catalog | pg_operator | table | postgres pg_catalog | pg_opfamily | table | postgres pg_catalog | pg_partitioned_table | table | postgres pg_catalog | pg_policy | table | postgres pg_catalog | pg_proc | table | postgres pg_catalog | pg_publication | table | postgres pg_catalog | pg_publication_rel | table | postgres pg_catalog | pg_range | table | postgres pg_catalog | pg_replication_origin | table | postgres pg_catalog | pg_rewrite | table | postgres pg_catalog | pg_seclabel | table | postgres pg_catalog | pg_sequence | table | postgres pg_catalog | pg_shdepend | table | postgres pg_catalog | pg_shdescription | table | postgres pg_catalog | pg_shseclabel | table | postgres pg_catalog | pg_statistic | table | postgres pg_catalog | pg_statistic_ext | table | postgres pg_catalog | pg_statistic_ext_data | table | postgres pg_catalog | pg_subscription | table | postgres pg_catalog | pg_subscription_rel | table | postgres pg_catalog | pg_tablespace | table | postgres pg_catalog | pg_transform | table | postgres pg_catalog | pg_trigger | table | postgres pg_catalog | pg_ts_config | table | postgres pg_catalog | pg_ts_config_map | table | postgres pg_catalog | pg_ts_dict | table | postgres pg_catalog | pg_ts_parser | table | postgres pg_catalog | pg_ts_template | table | postgres pg_catalog | pg_type | table | postgres pg_catalog | pg_user_mapping | table | postgres (62 rows) |
表名字 用途
- pg_aggregate 聚集函数
- pg_am 索引访问方法
- pg_amop 访问方法操作符
- pg_amproc 访问方法支持过程
- pg_attrdef 字段缺省值
- pg_attribute 表的列(也称为”属性”或”字段”)
- pg_authid 认证标识符(角色)
- pg_auth_members 认证标识符成员关系
- pg_autovacuum 每个关系一个的自动清理配置参数
- pg_cast 转换(数据类型转换)
- pg_class 表、索引、序列、视图(“关系”)
- pg_constraint 检查约束、唯一约束、主键约束、外键约束
- pg_conversion 编码转换信息
- pg_database 本集群内的数据库
- pg_depend 数据库对象之间的依赖性
- pg_description 数据库对象的描述或注释
- pg_index 附加的索引信息
- pg_inherits 表继承层次
- pg_language 用于写函数的语言
- pg_largeobject 大对象
- pg_listener 异步通知
- pg_namespace 模式
- pg_opclass 索引访问方法操作符类
- pg_operator 操作符
- pg_pltemplate 过程语言使用的模板数据
- pg_proc 函数和过程
- pg_rewrite 查询重写规则
- pg_shdepend 在共享对象上的依赖性
- pg_shdescription 共享对象上的注释
- pg_statistic 优化器统计
- pg_tablespace 这个数据库集群里面的表空间
- pg_trigger 触发器
- pg_type 数据类型
pg_class
查询所有业务表信息
1 | select * from pg_tables where tablename not like 'pg%' and tablename not like 'sql_%' order by tablename; |
查询所有业务表名称及表描述
1 2 | select tablename,obj_description(relfilenode,'pg_class') from pg_tables a, pg_class b where a.tablename = b.relname and a.tablename not like 'pg%' and a.tablename not like 'sql_%' order by a.tablename; |
查询某表的字段信息
1 2 3 | select a.attname as fieldname, col_description(a.attrelid,a.attnum) as comment,format_type(a.atttypid,a.atttypmod) as type, a.attnotnull as notnull from pg_class as c,pg_attribute as a where c.relname = '表名称' and a.attrelid = c.oid and a.attnum > 0; |
pg_class (系统表:对象)是一个对象表,表的每个字段都是‘rel’开头,分明就是 ‘relation’这个单词的缩写,意思就是‘关系’。表中relkind字段决定对象类型:r = 普通表,i = 索引,S = 序列,v = 视图, c = 复合类型,s = 特殊,t = TOAST表。对象所属的relnamespace(模式名称)和relowner(所有者)都是用其对应的oid显示,所以要直观看到实际本名要联合pg_namespace(系统表:模式)和pg_roles(系统视图:角色)一起查,这两个表和视图中都有oid字段。
该系统表记录了数据表、索引(仍然需要参阅pg_index)、序列、视图、复合类型和一些特殊关系类型的元数据。注意:不是所有字段对所有对象类型都有意义。
名称 | 类型 | 参考 | 描述 |
---|---|---|---|
oid | oid | 行标识符(隐藏属性;必须明确选择) | |
relname | name | 表格,索引,视图等的名称 | |
relnamespace | oid | pg_namespace.oid | 包含此relation的名称空间的oid |
reltype | oid | pg_type .oid | 与此表行类型对应的数据类型的oid(如果有的话)(对于没有pg_type条目的索引,为零 ) |
reloftype | oid | pg_type .oid | 对于类型表,基础复合类型的oid,对于所有其他relation为零 |
relowner | oid | pg_authid.oid | relation的所有者 |
relam | oid | pg_am.oid | 如果这是一个索引,则使用的访问方法(B-树,散列等) |
relfilenode | oid | 该relation的磁盘文件的名称; 零表示这是一个“映射”relation,其磁盘文件名由低级状态决定 | |
reltablespace | oid | pg_tablespace.oid | 存储该relation的表空间。如果为零,则隐含数据库的默认表空间。(如果relation没有磁盘上的文件,则无意义。) |
relpages | int4 | 该表的磁盘表示的大小(页面大小为BLCKSZ)。这只是计划者使用的估计值。它由 VACUUM,ANALYZE和一些DDL命令(如 CREATE INDEX)更新。 | |
reltuples | float4 | 表中的行数。这只是计划者使用的估计值。它由VACUUM,ANALYZE和一些DDL命令(如CREATE INDEX)更新。 | |
relallvisible | int4 | 在表格的可见性图中标记为全部可见的页面数。这只是计划者使用的估计值。它由VACUUM,ANALYZE和一些DDL命令(如CREATE INDEX)更新。 | |
reltoastrelid | oid | pg_class .oid | 与此表关联的TOAST表的oid,如果没有,则为0。TOAST表在“辅助表”中存储“超出行”的大型属性 。 |
relhasindex | bool | 如果这是一个表并且它有(或最近有)任何索引,则为真 | |
relisshared | bool | 如果此表在群集中的所有数据库之间共享,则为true。只有某些系统目录(如 pg_database)被共享。 | |
relpersistence | char | p =永久表, u =未记录表,t =临时表 | |
relkind | char | r = 普通表, i = 索引, S = 序列, t = TOAST表, v = 视图, m = 物化视图, c = 组合类型, f = 外部表, p = 分区表, I = 分区索引 | |
relnatts | int2 | relation中的用户列数(系统列未计数)。pg_attribute中必须有许多相应的条目。另见pg_attribute.attnum。 | |
relchecks | int2 | 表上CHECK约束的数量; 请参阅pg_constraint目录 | |
relhasoids | bool | 如果我们为relation的每一行生成oid,则为真 | |
relhaspkey | bool | 如果表具有(或曾经有)主键,则为真 | |
relhasrules | bool | 如果表具有(或曾经有)规则,则为真; 请参阅pg_rewrite目录 | |
relhastriggers | bool | 如果表具有(或曾经有)触发器,则为真; 请参阅 pg_trigger目录 | |
relhassubclass | bool | 如果表有(或曾经有过)任何继承孩子,则为真 | |
relrowsecurity | bool | 如果表已启用行级安全性,则为true; 请参阅 pg_policy目录 | |
relforcerowsecurity | bool | 如果行级别安全性(启用时)也为true,则也适用于表所有者; 请参阅pg_policy目录 | |
relispopulated | bool | 如果relation被填充,则为真(对于除某些实例化视图之外的所有relation都是如此) | |
relreplident | char | 用于为行构成“副本标识”的列:d = default(主键,如果有的话),n =无,f =所有列 i =具有indisreplident set的索引或default | |
relfrozenxid | xid | 在此表之前的所有交易ID已被替换为永久(“冻结”)交易ID。这用于跟踪是否需要将表抽真空以防止事务ID环绕或允许缩小pg_clog。零(InvalidTransactionId)如果relation不是一个表。 | |
relminmxid | xid | 在此表之前的所有多重作业ID已由该事务ID替换。这用于跟踪是否需要将表抽真空以防止多轴实现ID 绕回或允许缩小pg_multixact。零(InvalidMultiXactId)如果relation不是一个表。 | |
relacl | aclitem[] | 访问权限; 看到GRANT和REVOKE的细节 | |
reloptions | text[] | 特定于访问方法的选项,如“keyword = value”字符串 |
1 2 3 4 5 6 7 8 9 10 11 12 | #查看指定表对象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表中都有一行记录。
另外,查询列信息也可以使用information_schema.columns
视图。
名字 | 类型 | 引用 | 描述 |
---|---|---|---|
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 | 该字段所拥有的直接祖先的个数。如果一个字段的祖先个数非零,那么它就不能被删除或重命名。 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #查看指定表中包含的字段名和字段编号。 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=# \d information_schema.columns; View "information_schema.columns" Column | Type | Collation | Nullable | Default --------------------------+------------------------------------+-----------+----------+--------- table_catalog | information_schema.sql_identifier | | | table_schema | information_schema.sql_identifier | | | table_name | information_schema.sql_identifier | | | column_name | information_schema.sql_identifier | | | ordinal_position | information_schema.cardinal_number | | | column_default | information_schema.character_data | | | is_nullable | information_schema.yes_or_no | | | data_type | information_schema.character_data | | | character_maximum_length | information_schema.cardinal_number | | | character_octet_length | information_schema.cardinal_number | | | numeric_precision | information_schema.cardinal_number | | | numeric_precision_radix | information_schema.cardinal_number | | | numeric_scale | information_schema.cardinal_number | | | datetime_precision | information_schema.cardinal_number | | | interval_type | information_schema.character_data | | | interval_precision | information_schema.cardinal_number | | | character_set_catalog | information_schema.sql_identifier | | | character_set_schema | information_schema.sql_identifier | | | character_set_name | information_schema.sql_identifier | | | collation_catalog | information_schema.sql_identifier | | | collation_schema | information_schema.sql_identifier | | | collation_name | information_schema.sql_identifier | | | domain_catalog | information_schema.sql_identifier | | | domain_schema | information_schema.sql_identifier | | | domain_name | information_schema.sql_identifier | | | udt_catalog | information_schema.sql_identifier | | | udt_schema | information_schema.sql_identifier | | | udt_name | information_schema.sql_identifier | | | scope_catalog | information_schema.sql_identifier | | | scope_schema | information_schema.sql_identifier | | | scope_name | information_schema.sql_identifier | | | maximum_cardinality | information_schema.cardinal_number | | | dtd_identifier | information_schema.sql_identifier | | | is_self_referencing | information_schema.yes_or_no | | | is_identity | information_schema.yes_or_no | | | identity_generation | information_schema.character_data | | | identity_start | information_schema.character_data | | | identity_increment | information_schema.character_data | | | identity_maximum | information_schema.character_data | | | identity_minimum | information_schema.character_data | | | identity_cycle | information_schema.yes_or_no | | | is_generated | information_schema.character_data | | | generation_expression | information_schema.character_data | | | is_updatable | information_schema.yes_or_no | | | postgres=# select * from information_schema.columns where table_name='test'; table_catalog | table_schema | table_name | column_name | ordinal_position | column_default | is_nullable | data_type | character_maximum_length | character_octet_length | numeric_precision | numeric_precision_radix | numeric_scale | datetime_precision | interval_type | interval_precision | character_set_catalog | character_set_schema | character_set_name | collation_catalog | collation_schema | collation_name | domain_catalog | domain_schema | domain_name | udt_catalog | udt_schema | udt_name | scope_catalog | scope_schema | scope_name | maximum_cardinality | dtd_identifier | is_self_referencing | is_identity | identity_generation | identity_start | identity_increment | identity_maximum | identity_minimum | identity_cycle | is_generated | generation_expression | is_updatable ---------------+--------------+------------+--------------+------------------+----------------+-------------+--------------------------+--------------------------+------------------------+-------------------+-------------------------+---------------+--------------------+---------------+--------------------+-----------------------+----------------------+--------------------+-------------------+------------------+----------------+----------------+---------------+-------------+-------------+------------+-------------+---------------+--------------+------------+---------------------+----------------+---------------------+-------------+---------------------+----------------+--------------------+------------------+------------------+----------------+--------------+-----------------------+-------------- postgres | public | test | usename | 1 | | YES | name | | | | | | | | | | | | postgres | pg_catalog | C | | | | postgres | pg_catalog | name | | | | | 1 | NO | NO | | | | | | NO | NEVER | | YES postgres | public | test | usesysid | 2 | | YES | oid | | | | | | | | | | | | | | | | | | postgres | pg_catalog | oid | | | | | 2 | NO | NO | | | | | | NO | NEVER | | YES postgres | public | test | usecreatedb | 3 | | YES | boolean | | | | | | | | | | | | | | | | | | postgres | pg_catalog | bool | | | | | 3 | NO | NO | | | | | | NO | NEVER | | YES postgres | public | test | usesuper | 4 | | YES | boolean | | | | | | | | | | | | | | | | | | postgres | pg_catalog | bool | | | | | 4 | NO | NO | | | | | | NO | NEVER | | YES postgres | public | test | userepl | 5 | | YES | boolean | | | | | | | | | | | | | | | | | | postgres | pg_catalog | bool | | | | | 5 | NO | NO | | | | | | NO | NEVER | | YES postgres | public | test | usebypassrls | 6 | | YES | boolean | | | | | | | | | | | | | | | | | | postgres | pg_catalog | bool | | | | | 6 | NO | NO | | | | | | NO | NEVER | | YES postgres | public | test | passwd | 7 | | YES | text | | 1073741824 | | | | | | | | | | | | | | | | postgres | pg_catalog | text | | | | | 7 | NO | NO | | | | | | NO | NEVER | | YES postgres | public | test | valuntil | 8 | | YES | timestamp with time zone | | | | | | 6 | | | | | | | | | | | | postgres | pg_catalog | timestamptz | | | | | 8 | NO | NO | | | | | | NO | NEVER | | YES postgres | public | test | useconfig | 9 | | YES | ARRAY | | | | | | | | | | | | postgres | pg_catalog | C | | | | postgres | pg_catalog | _text | | | | | 9 | NO | NO | | | | | | NO | NEVER | | YES (9 rows) |
pg_attrdef
该系统表主要存储字段缺省值,字段中的主要信息存放在pg_attribute系统表中。注意:只有明确声明了缺省值的字段在该表中才会有记录。
名字 | 类型 | 引用 | 描述 |
---|---|---|---|
adrelid | oid | pg_class.oid | 这个字段所属的表 |
adnum | int2 | pg_attribute.attnum | 字段编号,其规则等同于pg_attribute.attnum |
adbin | text | 字段缺省值的内部表现形式。 | |
adsrc | text | 缺省值的人可读的表现形式。 |
1 2 3 4 5 6 7 8 | #查看指定表有哪些字段存在缺省值,同时显示出字段名和缺省值的定义方式 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[] | 运行时配置变量的会话缺省。 |
1 2 3 4 5 | #从输出结果可以看出口令字段已经被加密。 postgres=# SELECT rolname,rolpassword FROM pg_authid; rolname | rolpassword -----------+------------------------------------- postgres | md5a3556571e93b0d20722ba62be61e8c2d |
pg_auth_members
该系统表存储角色之间的成员关系。
名字 | 类型 | 引用 | 描述 |
---|---|---|---|
roleid | oid | pg_authid.oid | 组角色的ID。 |
member | oid | pg_authid.oid | 属于组角色roleid的成员角色的ID。 |
grantor | oid | pg_authid.oid | 赋予此成员关系的角色的ID。 |
admin_option | bool | 如果具有把其它成员角色加入组角色的权限,则为真。 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #1. 先查看角色成员表中有哪些角色之间的隶属关系,在当前结果集中只有一个成员角色隶属于一个组角色, # 如果有多个成员角色隶属于同一个组角色,这样将会有多条记录。 postgres=# SELECT * FROM pg_auth_members ; roleid | member | grantor | admin_option --------+--------+---------+-------------- 16446 | 16445 | 10 | f (1 row) #2. 查看组角色的名字。 postgres=# SELECT rolname FROM pg_authid a,pg_auth_members am WHERE a.oid = am.roleid; rolname --------- mygroup (1 row) #3. 查看成员角色的名字。 #4. 如果需要用一个结果集获取角色之间的隶属关系,可以将这两个结果集作为子查询后再进行关联。 postgres=# SELECT rolname FROM pg_authid a,pg_auth_members am WHERE a.oid = am.member; rolname --------- myuser (1 row) |
pg_constraint
该系统表存储PostgreSQL中表对象的检查约束、主键、唯一约束和外键约束。
名字 | 类型 | 引用 | 描述 |
---|---|---|---|
conname | name | 约束名字(不一定是唯一的)。 | |
connamespace | oid | pg_namespace.oid | 包含这个约束的名字空间(模式)的OID。 |
contype | char | c = 检查约束, f = 外键约束, p = 主键约束, u = 唯一约束 | |
condeferrable | bool | 该约束是否可以推迟。 | |
condeferred | bool | 缺省时这个约束是否是推迟的? | |
conrelid | oid | pg_class.oid | 该约束所在的表,如果不是表约束则为0。 |
contypid | oid | pg_type.oid | 该约束所在的域,如果不是域约束则为0。 |
confrelid | oid | pg_class.oid | 如果为外键,则指向参照的表,否则为0。 |
confupdtype | char | 外键更新动作代码。 | |
confdeltype | char | 外键删除动作代码。 | |
confmatchtype | char | 外键匹配类型。 | |
conkey | int2[] | pg_attribute.attnum | 如果是表约束,则是约束控制的字段列表。 |
confkey | int2[] | pg_attribute.attnum | 如果是外键,则是参照字段的列表。 |
conbin | text | 如果是检查约束,则表示表达式的内部形式。 | |
consrc | text | 如果是检查约束,则是表达式的人可读的形式。 |
pg_tablespace
该系统表存储表空间的信息。注意:表可以放在特定的表空间里,以帮助管理磁盘布局和解决IO瓶颈。
名字 | 类型 | 引用 | 描述 |
---|---|---|---|
spcname | name | 表空间名称。 | |
spcowner | oid | pg_authid.oid | 表空间的所有者,通常是创建它的角色。 |
spclocation | text | 表空间的位置(目录路径)。 | |
spcacl | aclitem[] | 访问权限。 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #1. 创建表空间。 postgres=# CREATE TABLESPACE my_tablespace LOCATION '/opt/PostgreSQL/9.1/mydata'; CREATE TABLESPACE #2. 将新建表空间的CREATE权限赋予public。 postgres=# GRANT CREATE ON TABLESPACE my_tablespace TO public; GRANT #3. 查看系统内用户自定义表空间的名字、文件位置和创建它的角色名称。 #4. 系统创建时自动创建的两个表空间(pg_default和pg_global)的文件位置为空(不是NULL)。 postgres=# SELECT spcname,rolname,spclocation FROM pg_tablespace ts,pg_authid a WHERE ts.spcowner = a.oid AND spclocation <> ''; spcname | rolname | spclocation ---------------+----------+---------------------------- my_tablespace | postgres | /opt/PostgreSQL/9.1/mydata (1 row) |
pg_namespace
该系统表存储名字空间(模式)。
名字 | 类型 | 引用 | 描述 |
---|---|---|---|
nspname | name | 名字空间(模式)的名称。 | |
nspowner | oid | pg_authid.oid | 名字空间(模式)的所有者 |
nspacl | aclitem[] | 访问权限。 |
1 2 3 4 5 6 | #查看当前数据库public模式的创建者的名称。 postgres=# SELECT nspname,rolname FROM pg_namespace n, pg_authid a WHERE nspname = 'public' AND nspowner = a.oid; nspname | rolname ---------+---------- public | postgres (1 row) |
pg_database
该系统表存储数据库的信息。和大多数系统表不同的是,在一个集群里该表是所有数据库共享的,即每个集群只有一份pg_database拷贝,而不是每个数据库一份。
名字 | 类型 | 引用 | 描述 |
---|---|---|---|
datname | name | 数据库名称。 | |
datdba | oid | pg_authid.oid | 数据库所有者,通常为创建该数据库的角色。 |
encoding | int4 | 数据库的字符编码方式。 | |
datistemplate | bool | 如果为真,此数据库可以用于CREATE DATABASE TEMPLATE子句,把新数据库创建为此数据库的克隆。 | |
datallowconn | bool | 如果为假,则没有人可以联接到这个数据库。 | |
datlastsysoid | oid | 数据库里最后一个系统OID,此值对pg_dump特别有用。 | |
datvacuumxid | xid | ||
datfrozenxid | xid | ||
dattablespace | text | pg_tablespace.oid | 该数据库的缺省表空间。在这个数据库里,所有pg_class.reltablespace为零的表都将保存在这个表空间里,特别要指出的是,所有非共享的系统表也都存放在这里。 |
datconfig | text[] | 运行时配置变量的会话缺省值。 | |
datacl | aclitem[] | 访问权限。 |
pg_index
该系统表存储关于索引的一部分信息。其它的信息大多数存储在pg_class。
名字 | 类型 | 引用 | 描述 |
---|---|---|---|
indexrelid | oid | pg_class.oid | 该索引在pg_class里的记录的OID。 |
indrelid | oid | pg_class.oid | 索引所在表在pg_class里的记录的OID。 |
indnatts | int2 | 索引中的字段数量(拷贝的pg_class.relnatts)。 | |
indisunique | bool | 如果为真,该索引是唯一索引。 | |
indisprimary | bool | 如果为真,该索引为该表的主键。 | |
indisclustered | bool | 如果为真,那么该表在这个索引上建了簇。 | |
indkey | int2vector | pg_attribute.attnum | 该数组的元素数量为indnatts,数组元素值表示建立这个索引时所依赖的字段编号,如1 3,表示第一个字段和第三个字段构成这个索引的键值。如果为0,则表示是表达式索引,而不是基于简单字段的索引。 |
indclass | oidvector | pg_opclass.oid | 对于构成索引键值的每个字段,这个字段都包含一个指向所使用的操作符表的OID。 |
indexprs | text | 表达式树用于那些非简单字段引用的索引属性。它是一个列表,在indkey里面的每个零条目一个元素。如果所有索引属性都是简单的引用,则为空。 | |
indpred | text | 部分索引断言的表达式树。如果不是部分索引, 则是空字串。 |
见如下应用示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #查看该索引所在表的名称,以及构成该索引的键值数量和具体键值的字段编号。 postgres=# SELECT indnatts,indkey,relname FROM pg_index i, pg_class c WHERE c.relname = 'testtable2' AND indrelid = c.oid; indnatts | indkey | relname ----------+--------+------------ 2 | 13 | testtable2 (1 row) #查看指定表包含的索引,同时列出索引的名称。 postgres=# SELECT t.relname AS table_name, c.relname AS index_name FROM (SELECT relname,indexrelid FROM pg_index i, pg_class c WHERE c.relname = 'testtable2' AND indrelid = c.oid) t, pg_index i,pg_class c WHERE t.indexrelid = i.indexrelid AND i.indexrelid = c.oid; table_name | index_name ------------+---------------- testtable2 | testtable2_idx (1 row) |
pg_statistic
目录pg_statistic
存储有关数据库内容的统计数据。其中的项由ANALYZE创建,查询规划器会使用这些数据来进行查询规划。注意所有的统计数据天然就是近似的,即使它刚刚被更新。
通常对于数据表中一个已经被 ANALYZE 过的列,在本目录中会存在一个stainherit
= false
的项。如果该列所在的表具有后代(即有其他表继承该表),对于该列还会创建第二个stainherit
= true
的项。stainherit
= true
的项表示列在整个继承树上的统计数据,即通过SELECT *
column* FROM *
table**
看到的数据的统计,而stainherit
= false
的项表示对SELECT *
column* FROM ONLY *
table*
的结果的统计。
pg_statistic
也存储关于索引表达式值的统计数据,就好像它们是真正的数据列,但在这种情况中starelid
指索引。对一个普通非表达式索引列不会创建项,因为它将是底层表列的项的冗余。当前,索引表达式的项都具有stainherit
= false
。
因为不同类型的统计信息适用于不同类型的数据, pg_statistic
被设计成不太在意自己存储的是什么类型的统计。 只有极为常用的统计信息(比如NULL的含量)才在pg_statistic
里给予专用的字段。 其它所有东西都存储在“槽位”中,而槽位是一组相关的列, 它们的内容用槽位中的一个列里的代码表示。 更详细的信息请参阅 src/include/catalog/pg_statistic.h
。
pg_statistic
不应该是公共可读的,因为即使是一个表内容的统计性信息也可能被认为是敏感的(例子:一个薪水列的最大和最小值可能是非常有趣的)。pg_stats
是pg_statistic
上的一个公共可读的视图,它只会显示出当前用户可读的表的信息。
pg_statistic
Columns
列类型描述 |
---|
starelid oid (references pg_class .oid )被描述列所属的表或索引 |
staattnum int2 (references pg_attribute .attnum )被描述列的编号 |
stainherit bool 如果为真,统计包含了继承后代的列而不仅仅是指定关系的列 |
stanullfrac float4 列的项为空的比例 |
stawidth int4 非空项的平均存储宽度,以字节计 |
stadistinct float4 列中非空唯一值的数目。一个大于零的值是唯一值的真正数目。 一个小于零的值是表中行数的乘数的负值;例如,对于一个 80% 的值为非空且每个非空值平均出现两次的列,可以表示为stadistinct = -0.4。一个0值表示唯一值的数目未知。 |
stakind N *个“槽位”的统计类型。 |
staop* N
|
stacoll N *个“槽”中的统计信息。 例如,可排序列的直方图槽将显示定义数据排序顺序的排序规则。对于不可整理数据,为零。 |
stanumbersN float4[]第* N`个“槽位”的类型的数值类型统计, 如果该槽位不涉及数值类型则为NULL |
stavalues N anyarray第* N*个“槽位”的类型的列值,如果该槽位类型不存储任何数据值则为 NULL。 每个数组的元素值实际上都是指定列的数据类型或者是一个相关类型(如数组元素类型), 因此,除了把这些列的类型定义成 anyarray`之外别无他法。 |
系统视图
除了系统表之外,PostgreSQL 还提供了一系列内置的视图。 系统视图提供了查询系统表的一些便利的访问方法。 其它一些视图提供了访问内部服务器状态的方法。
信息模式提供了另外一套视图,它的功能覆盖了系统视图的功能。因为信息模式是 SQL 标准,而这里描述的视图是 PostgreSQL 特有的,所以最好用信息模式来获取自己需要的所有信息。
下面列出了这里描述的所有系统视图。下面是每个视图更详细的信息。有些视图提供了对统计收集器的结果的访问;
除了特别声明的,这里描述的所有视图都是只读的。
pg_user是在视图pg_shadow上面建的一个视图,其中隐藏了密码。
在pg数据库中有很多的系统视图,学习pg需要了解这些视图,pg中大部分视图都是以pg_开头的,查看所有的系统视图的命令如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | lhrdb=# \dv pg_* List of relations Schema | Name | Type | Owner ------------+---------------------------------+------+---------- pg_catalog | pg_available_extension_versions | view | postgres pg_catalog | pg_available_extensions | view | postgres pg_catalog | pg_config | view | postgres pg_catalog | pg_cursors | view | postgres pg_catalog | pg_file_settings | view | postgres pg_catalog | pg_group | view | postgres pg_catalog | pg_hba_file_rules | view | postgres pg_catalog | pg_indexes | view | postgres pg_catalog | pg_locks | view | postgres pg_catalog | pg_matviews | view | postgres pg_catalog | pg_policies | view | postgres pg_catalog | pg_prepared_statements | view | postgres pg_catalog | pg_prepared_xacts | view | postgres pg_catalog | pg_publication_tables | view | postgres pg_catalog | pg_replication_origin_status | view | postgres pg_catalog | pg_replication_slots | view | postgres pg_catalog | pg_roles | view | postgres pg_catalog | pg_rules | view | postgres pg_catalog | pg_seclabels | view | postgres pg_catalog | pg_sequences | view | postgres pg_catalog | pg_settings | view | postgres pg_catalog | pg_shadow | view | postgres pg_catalog | pg_shmem_allocations | view | postgres pg_catalog | pg_stat_activity | view | postgres pg_catalog | pg_stat_all_indexes | view | postgres pg_catalog | pg_stat_all_tables | view | postgres pg_catalog | pg_stat_archiver | view | postgres pg_catalog | pg_stat_bgwriter | view | postgres pg_catalog | pg_stat_database | view | postgres pg_catalog | pg_stat_database_conflicts | view | postgres pg_catalog | pg_stat_gssapi | view | postgres pg_catalog | pg_stat_progress_analyze | view | postgres pg_catalog | pg_stat_progress_basebackup | view | postgres pg_catalog | pg_stat_progress_cluster | view | postgres pg_catalog | pg_stat_progress_create_index | view | postgres pg_catalog | pg_stat_progress_vacuum | view | postgres pg_catalog | pg_stat_replication | view | postgres pg_catalog | pg_stat_slru | view | postgres pg_catalog | pg_stat_ssl | view | postgres pg_catalog | pg_stat_subscription | view | postgres pg_catalog | pg_stat_sys_indexes | view | postgres pg_catalog | pg_stat_sys_tables | view | postgres pg_catalog | pg_stat_user_functions | view | postgres pg_catalog | pg_stat_user_indexes | view | postgres pg_catalog | pg_stat_user_tables | view | postgres pg_catalog | pg_stat_wal_receiver | view | postgres pg_catalog | pg_stat_xact_all_tables | view | postgres pg_catalog | pg_stat_xact_sys_tables | view | postgres pg_catalog | pg_stat_xact_user_functions | view | postgres pg_catalog | pg_stat_xact_user_tables | view | postgres pg_catalog | pg_statio_all_indexes | view | postgres pg_catalog | pg_statio_all_sequences | view | postgres pg_catalog | pg_statio_all_tables | view | postgres pg_catalog | pg_statio_sys_indexes | view | postgres pg_catalog | pg_statio_sys_sequences | view | postgres pg_catalog | pg_statio_sys_tables | view | postgres pg_catalog | pg_statio_user_indexes | view | postgres pg_catalog | pg_statio_user_sequences | view | postgres pg_catalog | pg_statio_user_tables | view | postgres pg_catalog | pg_stats | view | postgres pg_catalog | pg_stats_ext | view | postgres pg_catalog | pg_tables | view | postgres pg_catalog | pg_timezone_abbrevs | view | postgres pg_catalog | pg_timezone_names | view | postgres pg_catalog | pg_user | view | postgres pg_catalog | pg_user_mappings | view | postgres pg_catalog | pg_views | view | postgres (67 rows) |
(1)系统视图
视图名称 | 描述 |
---|---|
pg_available_extensions | 可用的扩展 |
pg_available_extension_versions | 所有版本的扩展 |
pg_config | 编译时配置参数 |
pg_cursors | 打开的游标 |
pg_file_settings | 配置文件内容摘要 |
pg_group | 数据库用户组 |
pg_hba_file_rules | 客户端认证配置文件内容摘要 |
pg_indexes | 索引 |
pg_locks | 当前保持或者等待的锁 |
pg_matviews | 物化视图 |
pg_policies | 策略 |
pg_prepared_statements | 预备好的语句 |
pg_prepared_xacts | 预备好的事务 |
pg_publication_tables | 发布及其相关表格 |
pg_replication_origin_status | 有关复制源的信息,包括复制进度 |
pg_replication_slots | 复制槽信息 |
pg_roles | 数据库角色 |
pg_rules | 规则 |
pg_seclabels | 安全标签 |
pg_sequences | 序列 |
pg_settings | 参数设置 |
pg_shadow | 数据库用户 |
pg_stats | 规划器统计信息 |
pg_tables | 表 |
pg_timezone_abbrevs | 时区简写 |
pg_timezone_names | 时区名字 |
pg_user | 数据库用户 |
pg_user_mappings | 用户映射 |
pg_views | 视图 |
(2)动态统计视图
视图名称 | 描述 |
---|---|
pg_stat_activity | 每个服务器进程一行,显示与那个进程的当前活动相关的信息,例如状态和当前查询。 |
pg_stat_replication | 每一个 WAL 发送进程一行,显示有关到该发送进程 连接的后备服务器的复制的统计信息。 |
pg_stat_wal_receiver | 只有一行,显示来自 WAL 接收器所连接服务器的有关该接收器的统计信息。 |
pg_stat_subscription | 每个订阅至少一行,显示订阅工作者的相关信息。详细信息, |
pg_stat_ssl | 每个连接(常规连接和复制连接)一行, 显示有关在此连接上使用的 SSL 的信息。 |
pg_stat_progress_vacuum | 每个运行VACUUM的后端(包括 autovacuum工作者进程)一行, 显示当前的进度。 |
(3)已收集统计信息的视图
视图名称 | 描述 |
---|---|
pg_stat_archiver | 只有一行,显示有关 WAL 归档进程活动的统计信息。 |
pg_stat_bgwriter | 只有一行,显示有关后台写进程的活动的统计信息。 |
pg_stat_database | 每个数据库一行,显示数据库范围的统计信息。 |
pg_stat_database_conflicts | 每个数据库一行,显示数据库范围的统计信息, 这些信息的内容是关于由于与后备服务器的恢复过程 发生冲突而被取消的查询。 |
pg_stat_all_tables | 当前数据库中每个表一行,显示有关访问指定表的统计信息。详见pg_stat_all_tables。 |
pg_stat_sys_tables | 和pg_stat_all_tables一样,但只显示系统表。 |
pg_stat_user_tables | 和pg_stat_all_tables一样,但只显示用户表。 |
pg_stat_xact_all_tables | 和pg_stat_all_tables相似,但计数动作只在当前事务内发生(还没有被包括在pg_stat_all_tables和相关视图中)用于生存和死亡行数量的列以及清理和分析动作在此视图中不出现。 |
pg_stat_xact_sys_tables | 和pg_stat_xact_all_tables一样,但只显示系统表。 |
pg_stat_xact_user_tables | 和pg_stat_xact_all_tables一样,但只显示用户表。 |
pg_stat_all_indexes | 当前数据库中的每个索引一行,显示:表OID、索引OID、模式名、表名、索引名、 使用了该索引的索引扫描总数、索引扫描返回的索引记录数、使用该索引的简 单索引扫描抓取的活表(livetable)中数据行数。当前数据库中的每个索引一行,显示与访问指定索引有关的统计信息。 |
pg_stat_sys_indexes | 和pg_stat_all_indexes一样,但只显示系统表上的索引。 |
pg_stat_user_indexes | 和pg_stat_all_indexes一样,但只显示用户表上的索引。 |
pg_statio_all_tables | 当前数据库中每个表一行(包括TOAST表),显示:表OID、模式名、表名、 从该表中读取的磁盘块总数、缓冲区命中次数、该表上所有索引的磁盘块读取总数、 该表上所有索引的缓冲区命中总数、在该表的辅助TOAST表(如果存在)上的磁盘块读取总数、 在该表的辅助TOAST表(如果存在)上的缓冲区命中总数、TOAST表的索引的磁盘块读 取总数、TOAST表的索引的缓冲区命中总数。 当前数据库中的每个表一行,显示有关在指定表上 I/O的统计信息。 |
pg_statio_sys_tables | 和pg_statio_all_tables一样,但只显示系统表。 |
pg_statio_user_tables | 和pg_statio_all_tables一样,但只显示用户表。 |
pg_statio_all_indexes | 当前数据库中每个索引一行,显示:表OID、索引OID、模式名、 表名、索引名、该索引的磁盘块读取总数、该索引的缓冲区命中总数。 当前数据库中的每个索引一行,显示与指定索引上的 I/O 有关的统计信息。详见pg_statio_all_indexes。 |
pg_statio_sys_indexes | 和pg_statio_all_indexes一样,但只显示系统表上的索引。 |
pg_statio_user_indexes | 和pg_statio_all_indexes一样,但只显示用户表上的索引。 |
pg_statio_all_sequences | 当前数据库中每个序列对象一行,显示:序列OID、模式名、序列名、序列的磁盘读取总数、序列的缓冲区命中总数。 当前数据库中的每个序列一行,显示与指定序列上的 I/O 有关的统计信息。 |
pg_statio_sys_sequences | 和pg_statio_all_sequences一样,但只显示系统序列(目前没有定义系统序列,因此这个视图总是为空)。 |
pg_statio_user_sequences | 和pg_statio_all_sequences一样,但只显示用户序列。 |
pg_stat_user_functions | 对于所有跟踪功能,函数的OID,模式,名称,数量 通话总时间,和自我的时间。自我时间是 在函数本身所花费的时间量,总时间包括 它调用函数所花费的时间。时间值以毫秒为单位。每一个被跟踪的函数一行,显示与执行该函数有关的统计信息。 |
pg_stat_xact_user_functions | 和pg_stat_user_functions相似,但是只统计在当前事务期间的调用(还没有被包括在pg_stat_user_functions中) |
(4)PG中常用的系统视图
1、pg_tables:
该视图提供了对有关数据库中每个表的有用信息地访问。
字段名 | 类型 | 引用 | 描述 |
---|---|---|---|
schemaname | name | pg_namespace.nspname | 包含表的模式名字。 |
tablename | name | pg_class.relname | 表的名字。 |
tableowner | name | pg_authid.rolname | 表的所有者的名字。 |
tablespace | name | pg_tablespace.spcname | 包含表的表空间名字(如果是数据库缺省,则为 NULL)。 |
hasindexes | bool | pg_class.relhasindex | 如果表拥有(或者最近拥有)任何索引,则为真。 |
hasrules | bool | pg_class.relhasrules | 如果表存在规则,则为真。 |
hastriggers | bool | pg_class.reltriggers | 如果表有触发器,则为真。 |
2、pg_indexes:
该视图提供对数据库中每个索引的有用信息的访问。
字段名 | 类型 | 引用 | 描述 |
---|---|---|---|
schemaname | name | pg_namespace.nspname | 包含表和索引的模式的名字。 |
tablename | name | pg_class.relname | 索引所在表的名字。 |
indexname | name | pg_class.relname | 索引的名字。 |
tablespace | name | pg_tablespace.spcname | 包含索引的表空间名字(如果是数据库缺省,则为NULL)。 |
indexdef | text | 索引定义(一个重建的创建命令)。 |
3、pg_views:
该视图提供了对数据库里每个视图的有用信息的访问途径。
字段名 | 类型 | 引用 | 描述 |
---|---|---|---|
schemaname | name | pg_namespace.nspname | 包含此视图的模式名字。 |
viewname | name | pg_class.relname | 视图的名字。 |
viewowner | name | pg_authid.rolname | 视图的所有者的名字。 |
definition | text | 视图定义(一个重建的SELECT查询)。 |
4、pg_user:
该视图提供了对数据库用户的相关信息的访问。 这个视图只是pg_shadow表的公众可读的部分的视图化,但是不包含口令字段。
字段名 | 类型 | 引用 | 描述 |
---|---|---|---|
usename | name | 用户名。 | |
usesysid | int4 | 用户ID(用于引用这个用户的任意数字)。 | |
usecreatedb | bool | 用户是否可以创建数据库。 | |
usesuper | bool | 用户是否是一个超级用户。 | |
usecatupd | bool | 用户是否可以更新系统表。(即使超级用户也不能这么干,除非这个字段为真。) | |
passwd | text | 口令(可能加密了)。 | |
valuntil | abstime | 口令失效的时间(只用于口令认证)。 | |
useconfig | text[] | 运行时配置参数的会话缺省。 |
5、pg_roles:
该视图提供访问数据库角色有关信息的接口。这个视图只是pg_authid表的公开可读部分的视图化,同时把口令字段用空白填充。
字段名 | 类型 | 引用 | 描述 |
---|---|---|---|
rolname | name | 角色名。 | |
rolsuper | bool | 是否有超级用户权限的角色。 | |
rolcreaterole | bool | 是否可以创建更多角色的角色。 | |
rolcreatedb | bool | 是否可以创建数据库的角色。 | |
rolcatupdate | bool | 是否可以直接更新系统表的角色。 | |
rolcanlogin | bool | 如果为真,表示是可以登录的角色。 | |
rolpassword | text | 不是口令(总是 ****)。 | |
rolvaliduntil | timestamptz | 口令失效日期(只用于口令认证);如果没有失效期,为NULL。 | |
rolconfig | text[] | 运行时配置变量的会话缺省。 |
6、pg_rules:
该视图提供对查询重写规则的有用信息访问的接口。
字段名 | 类型 | 引用 | 描述 |
---|---|---|---|
schemaname | name | pg_namespace.nspname | 包含表的模式的名字。 |
tablename | name | pg_class.relname | 规则施加影响的表的名字。 |
rulename | name | pg_rewrite.rulename | 规则的名字。 |
definition | text | 规则定义(一个重新构造的创建命令)。 |
7、pg_settings:
该视图提供了对服务器运行时参数的访问。它实际上是SHOW和SET命令的另外一种方式。它还提供一些用SHOW不能直接获取的参数的访问,比如最大和最小值。
字段名 | 类型 | 引用 | 描述 |
---|---|---|---|
name | text | 运行时配置参数名。 | |
setting | text | 参数的当前值。 | |
category | text | 参数的逻辑组。 | |
short_desc | text | 参数的一个简短的描述。 | |
extra_desc | text | 有关参数的额外的、更详细的信息。 | |
context | text | 设置这个参数的值要求的环境。 | |
vartype | text | 参数类型(bool、integer、real和string)。 | |
source | text | 当前参数值的来源。 | |
min_val | text | 该参数允许的最小值(非数字值为NULL)。 | |
max_val | text | 该参数允许的最大值(非数字值为NULL)。 |
我们不能对pg_settings视图进行插入或者删除, 只能更新。
对pg_settings中的一行进行UPDATE等效于在该命名参数上执行SET命令。这个修改值影响当前会话使用的数值。
如果在一个最后退出的事务中发出了UPDATE命令,那么UPDATE命令的效果将在事务回滚之后消失。一旦包围它的事务提交,这个效果将固化,直到会话结束。
PostgreSQL 12新视图
PostgreSQL 12新增的系统表:
pg_stat_progress_cluster:显示cluster语句或者vacuum full语句的执行状态
pg_stat_progress_create_index:显示create index语句的执行状态
pg_stat_gssapi:显示connections是否使用GSSAPI authentication与encryption信息
pg_statistic_ext_data:保存了create statistics语句创建的statistics object,该视图不应该被the public读取,因为该视图的内容可能是敏感的。该系统表只能用superuser查询,否则会有如下的错误:
1 2 3 | postgres=> select * from pg_statistic_ext_data; ERROR: permission denied for table pg_statistic_ext_data postgres=> |
pg_stats_ext是一个公有的可读的view(这是PG12新建立的view),该view是基于pg_statistic_ext_data和pg_statistic_ext的关联的,该视图包括被current user看到的table和columns信息。
information_schema
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | postgres=# select oid,* from pg_catalog.pg_namespace; oid | oid | nspname | nspowner | nspacl -------+-------+--------------------+----------+------------------------------------- 99 | 99 | pg_toast | 10 | 11 | 11 | pg_catalog | 10 | {postgres=UC/postgres,=U/postgres} 2200 | 2200 | public | 10 | {postgres=UC/postgres,=UC/postgres} 12971 | 12971 | information_schema | 10 | {postgres=UC/postgres,=U/postgres} (4 rows) postgres=# \d information_schema. information_schema.administrable_role_authorizations information_schema.check_constraint_routine_usage information_schema.column_column_usage information_schema.columns information_schema.data_type_privileges information_schema.element_types information_schema.foreign_server_options information_schema.information_schema_catalog_name information_schema._pg_foreign_servers information_schema.referential_constraints information_schema.role_udt_grants information_schema.schemata information_schema.sql_parts information_schema.tables information_schema.udt_privileges information_schema.user_mappings information_schema.view_table_usage information_schema.applicable_roles information_schema.check_constraints information_schema.column_domain_usage information_schema.column_udt_usage information_schema.domain_constraints information_schema.enabled_roles information_schema.foreign_servers information_schema.key_column_usage information_schema._pg_foreign_table_columns information_schema.role_column_grants information_schema.role_usage_grants information_schema.sequences information_schema.sql_sizing information_schema.transforms information_schema.usage_privileges information_schema.view_column_usage information_schema.attributes information_schema.collation_character_set_applicability information_schema.column_options information_schema.constraint_column_usage information_schema.domains information_schema.foreign_data_wrapper_options information_schema.foreign_table_options information_schema.parameters information_schema._pg_foreign_tables information_schema.role_routine_grants information_schema.routine_privileges information_schema.sql_features information_schema.table_constraints information_schema.triggered_update_columns information_schema.user_defined_types information_schema.view_routine_usage information_schema.character_sets information_schema.collations information_schema.column_privileges information_schema.constraint_table_usage information_schema.domain_udt_usage information_schema.foreign_data_wrappers information_schema.foreign_tables information_schema._pg_foreign_data_wrappers information_schema._pg_user_mappings information_schema.role_table_grants information_schema.routines information_schema.sql_implementation_info information_schema.table_privileges information_schema.triggers information_schema.user_mapping_options information_schema.views select relname, relkind from pg_catalog.pg_class where relnamespace=12971 order by 1; relname | relkind ---------------------------------------+--------- _pg_foreign_data_wrappers | v _pg_foreign_servers | v _pg_foreign_table_columns | v _pg_foreign_tables | v _pg_user_mappings | v administrable_role_authorizations | v applicable_roles | v attributes | v character_sets | v check_constraint_routine_usage | v check_constraints | v collation_character_set_applicability | v collations | v column_column_usage | v column_domain_usage | v column_options | v column_privileges | v column_udt_usage | v columns | v constraint_column_usage | v constraint_table_usage | v data_type_privileges | v domain_constraints | v domain_udt_usage | v domains | v element_types | v enabled_roles | v foreign_data_wrapper_options | v foreign_data_wrappers | v foreign_server_options | v foreign_servers | v foreign_table_options | v foreign_tables | v information_schema_catalog_name | v key_column_usage | v parameters | v referential_constraints | v role_column_grants | v role_routine_grants | v role_table_grants | v role_udt_grants | v role_usage_grants | v routine_privileges | v routines | v schemata | v sequences | v sql_features | r sql_implementation_info | r sql_parts | r sql_sizing | r table_constraints | v table_privileges | v tables | v transforms | v triggered_update_columns | v triggers | v udt_privileges | v usage_privileges | v user_defined_types | v user_mapping_options | v user_mappings | v view_column_usage | v view_routine_usage | v view_table_usage | v views | v (65 rows) postgres=# |
其中information_schema是方便用户查看表/视图/函数信息提供的,它大多是视图,MySQL,SQL Server同样有information_schema这个schema。 通过查看information_chema.tables, information_schema.columns可以方便的获取表/字段信息。
pg_catalog是系统Schema,包含了系统的自带函数/数据类型定义等,pg_catalog是保障postgres正常运转的重要基石。
Information_schema自动的存在于每个database中,里面包含了数据库中所有对象的定义信息。
Information_schema默认不存在于任何用户的search_path中,所以对所有用户都是隐藏的。\dn看不到,通过pgAdmin等客户端工具也不会自动显示。因此访问这个schema的任何视图都需要加上schema名。当然也可以通过修改search_path参数来访问。但PG不推荐这样做,因为里面的视图名称可能会跟用户应用程序中的对象名冲突。
Information_schema中的视图使用了几种特别的数据类型,如:cardinal_number 非负整数,yes_or_no相当于boolean
Informationschema中的视图基本都有权限校验,特定的用户只能看到特定的信息,也就是相当于Oracle中all*开头的视图。其中几个重要的视图如下:
表信息:information_schema.tables ,相当于Oracle中的all_tables
字段信息:information_schema.columns,相当于Oracle中的all_tab_cloumns
procedure/function:routines, 不包括package,因为pg不支持package
约束信息:
information_schema.table_constraints。另外在constraint_column_usage视图中有约束相关的字段信息;在referential_constraints中有关于外键约束的进一步的信息;在check_constraints中有关于check约束的进一步信息。key_column_usage记录了除check约束之外的其他约束相关的字段信息(check约束在check_constraints中已有记录)。
注意这个table_constraints不要跟constraint_table_usage搞混了。前者是记录了所有约束;而后者是记录了所有有相关约束的表(当然也有约束名显示),但不包括check约束。所以后者在我看来是没什么用的
权限信息:
table_privileges中记录了表权限,column_privileges中记录了列上的权限,routine_privileges上记录了function/procedure的权限,role_usage_grants记录了sequence/domain等类型的对象的usage权限,跟usage_privileges类似
在命令号里,\dp或\z也可以看到相关对象的授权信息
视图信息:
Views中记录视图基础信息,view_table_usage记录视图所依赖的表,view_routine_usage记录所依赖的function, view_column_usage记录所涉及的字段
另外sequece/trigger/schema等都有相关视图,要用到时直接查就好了。所以现在整个逻辑结构都差不多有答案了
注释
表的注释可以通过 obj_description 函数获取, 列的注释通过col_description函数获取。
参考:https://www.xmmup.com/pgzhongtianjiahechaxunzhushicomment.html