9.1第45章

From PostgreSQL wiki
Jump to navigationJump to search

系统表是关系型数据库存放结构元数据的地方, 比如表和字段,以及内部登记信息等。 PostgreSQL 的系统表就是普通表。 你可以删除然后重建这些表,增加列,插入和更新数值, 然后彻底把你的系统搞垮。 我们不应该手工修改系统表,通常总有 SQL 命令可以做这些事情。 (比如,CREATE DATABASE 向 pg_database 表插入一行 — 并且实际上在磁盘上创建该数据库。) 有几种特别深奥的操作例外,比如增加索引访问方法。

45.1. 概述

Table 45-1 列出了系统表。 每个表更详细的文档在后面。

大多数系统表都是在数据库创建的过程中从模版数据库中拷贝过来的, 因此都是数据库相关的。少数表是在整个安装中物理上所有数据库共享的; 这些表在独立的表的描述中用指明了。

Table 42-1. 系统表
表名字	表用途
pg_aggregate	聚集函数
pg_am	索引访问方法
pg_amop	访问方法操作符
pg_amproc	访问方法支持过程
pg_attrdef	字段缺省值
pg_attribute	表的列("属性","字段")
pg_authid	认证标识符(角色)
pg_auth_members	认证标识符成员关系
pg_cast	转换(数据类型转换)
pg_class	表,索引,序列,视图("关系")
pg_constraint	检查约束,唯一约束,主键约束,外键约束
pg_collation	collations (locale information)
pg_conversion	编码转换信息
pg_database	本集群内的数据库
pg_db_role_setting	每个角色及每个数据库的设定
pg_default_acl	对象类型的默认权限设置
pg_depend	数据库对象之间的依赖性
pg_description	数据库对象的描述或注释
pg_enum	enum label and value definitions
pg_extension	已安装的扩展模块
pg_foreign_data_wrapper	外部数据转换器定义
pg_foreign_server	外部服务器的定义
pg_foreign_table	额外的外部表
pg_index	附加的索引信息
pg_inherits	表继承层次
pg_language	用于写函数的语言
pg_largeobject	大对象
pg_namespace	模式
pg_opclass	索引访问方法操作符表
pg_operator	操作符
pg_opfamily	访问方法操作符集
pg_pltemplate	过程语言使用的模板数据
pg_proc	函数和过程
pg_rewrite	查询重写规则
pg_seclabel	数据库对象的安全标签
pg_shdepend	在共享对象上的依赖性
pg_shdescription	共享对象的描述
pg_statistic	优化器统计
pg_tablespace	这个数据库集群里面的表空间
pg_trigger	触发器
pg_ts_config	文本检索配置
pg_ts_config_map	文本检索配置token mappings
pg_ts_dict	文本检索字典
pg_ts_parser	文本检索分析器
pg_ts_template	文本检索配置模板
pg_type	数据类型
pg_user_mapping	外部服务器用户的映射

45.2. pg_aggregate

pg_aggregate 存储与聚集函数有关的信息。 聚集函数是对一个数值集(通常每个匹配查询条件的行中的一个字段) 进行操作的函数,它返回从这些值中计算出的一个数值。 典型的聚集函数是 sum,count,和 max。 pg_aggregate 里的每条记录都是一条 pg_proc 里面的记录的扩展。 pg_proc 记录运载该聚集的名字,输入和输出 数据类型,以及其它一些和普通函数类似的信息。

Table 45-2. pg_aggregate 字段
名字	类型	引用	描述
aggfnoid	regproc	pg_proc.oid	此聚集函数的pg_procOID
aggtransfn	regproc	pg_proc.oid	转换函数
aggfinalfn	regproc	pg_proc.oid	终处理函数(如果没有则为零)
aggsortop	oid	pg_operator.oid	关联排序操作符(零或者无)
aggtranstype	oid	pg_type.oid	此聚集函数的内部转换(状态)数据的数据类型
agginitval	text	 	转换状态的初始值。这是一个文本数据域, 它包含初始值的外部字串表现形式。如果数据域是  NULL, 那么转换状态值从 NULL 开始。

新聚集函数是用 CREATE AGGREGATE 命令注册的。 参阅 Section 32.10 获取关于写聚集函数 以及转换函数的含义等的更多信息。

45.3. pg_am

pg_am 存储有关索引访问方法的信息。 系统支持的每种索引访问方法都有一行。这个表的内容在 Chapter 48 详细讨论。

Table 45-3. pg_am 字段
名字	类型	引用	描述
Name	Type	References	Description
amname	name	 	Name of the access method
amstrategies	int2	 	Number of operator strategies for this access method, or zero if access method does not have a fixed set of operator strategies
amsupport	int2	 	Number of support routines for this access method
amcanorder	bool	 	Does the access method support ordered scans sorted by the indexed  column's value?
amcanorderbyop	bool	 	Does the access method support ordered scans sorted by the result of an operator on the indexed column?
amcanbackward	bool	 	Does the access method support backward scanning?
amcanunique	bool	 	Does the access method support unique indexes?
amcanmulticol	bool	 	Does the access method support multicolumn indexes?
amoptionalkey	bool	 	Does the access method support a scan without any constraint for the  first index column?
amsearchnulls	bool	 	Does the access method support IS NULL/NOT NULL searches?
amstorage	bool	 	Can index storage data type differ from column data type?
amclusterable	bool	 	Can an index of this type be clustered on?
ampredlocks	bool	 	Does an index of this type manage fine-grained predicate locks?
amkeytype	oid	pg_type.oid	Type of data stored in index, or zero if not a fixed type
aminsert	regproc	pg_proc.oid	"Insert this tuple" function
ambeginscan	regproc	pg_proc.oid	"Prepare for index scan" function
amgettuple	regproc	pg_proc.oid	"Next valid tuple" function, or zero if none
amgetbitmap	regproc	pg_proc.oid	"Fetch all valid tuples" function, or zero if none
amrescan	regproc	pg_proc.oid	"(Re)start index scan" function
amendscan	regproc	pg_proc.oid	"Clean up after index scan" function
ammarkpos	regproc	pg_proc.oid	"Mark current scan position" function
amrestrpos	regproc	pg_proc.oid	"Restore marked scan position" function
ambuild	regproc	pg_proc.oid	"Build new index" function
ambuildempty	regproc	pg_proc.oid	"Build empty index" function
ambulkdelete	regproc	pg_proc.oid	Bulk-delete function
amvacuumcleanup	regproc	pg_proc.oid	Post-VACUUM cleanup function
amcostestimate	regproc	pg_proc.oid	Function to estimate cost of an index scan
amoptions	regproc	pg_proc.oid	Function to parse and validate reloptions for an index

45.4. pg_amop

pg_amop 表存储有关和索引访问方法操作符表 关联的信息。如果一个操作符是一个操作符表中的成员,那么在这个表 中会占据一行。

A family member can be either a search operator or an ordering operator. An operator can appear in more than one family, but cannot appear in more than one search position nor more than one ordering position within a family. (It is allowed, though unlikely, for an operator to be used for both search and ordering purposes.)

Table 45-4. pg_amop 字段

Name	Type	References	Description
amopfamily	oid	pg_opfamily.oid	The operator family this entry is for
amoplefttype	oid	pg_type.oid	Left-hand input data type of operator
amoprighttype	oid	pg_type.oid	Right-hand input data type of operator
amopstrategy	int2	 	Operator strategy number
amoppurpose	char	 	Operator purpose, either s for search or o for ordering
amopopr	oid	pg_operator.oid	OID of the operator
amopmethod	oid	pg_am.oid	Index access method operator family is for
amopsortfamily	oid	pg_opfamily.oid	The btree operator family this entry sorts according to, if an  ordering operator; zero if a search operator

A "search" operator entry indicates that an index of this operator family can be searched to find all rows satisfying WHERE indexed_column operator constant. Obviously, such an operator must return boolean, and its left-hand input type must match the index's column data type.

An "ordering" operator entry indicates that an index of this operator family can be scanned to return rows in the order represented by ORDER BY indexed_column operator constant. Such an operator could return any sortable data type, though again its left-hand input type must match the index's column data type. The exact semantics of the ORDER BY are specified by the amopsortfamily column, which must reference a btree operator family for the operator's result type.

   Note: At present, it's assumed that the sort order for an ordering operator is the default for the referenced opfamily, i.e., ASC NULLS LAST. This might someday be relaxed by adding additional columns to specify sort options explicitly. 

An entry's amopmethod must match the opfmethod of its containing operator family (including amopmethod here is an intentional denormalization of the catalog structure for performance reasons). Also, amoplefttype and amoprighttype must match the oprleft and oprright fields of the referenced pg_operator entry.

45.5. pg_amproc

pg_amproc 存储有关与索引访问方法 操作符表相关联的支持过程的信息。每个属于某个操作符表的支持过程都占有 一行。

Table 45-5. pg_amproc 字段

Name	Type	References	Description
amprocfamily	oid	pg_opfamily.oid	The operator family this entry is for
amproclefttype	oid	pg_type.oid	Left-hand input data type of associated operator
amprocrighttype	oid	pg_type.oid	Right-hand input data type of associated operator
amprocnum	int2	 	Support procedure number
amproc	regproc	pg_proc.oid	OID of the procedure

The usual interpretation of the amproclefttype and amprocrighttype fields is that they identify the left and right input types of the operator(s) that a particular support procedure supports. For some access methods these match the input data type(s) of the support procedure itself, for others not. There is a notion of "default" support procedures for an index, which are those with amproclefttype and amprocrighttype both equal to the index opclass's opcintype.

45.6. pg_attrdef

pg_attrdef 表存储字段缺省值。字段的主要信息存放在 pg_attribute (见下文)。只有明确声明一个缺省值(该表何时创建或字段何时增加) 的字段在这里有元组。

Table 45-6. pg_attrdef 字段
名字	类型	引用	描述
adrelid	oid	pg_class.oid	这个字段所属的表
adnum	int2	pg_attribute.attnum	字段数目
adbin	text	 	字段缺省值的内部表现形式
adsrc	text	 	人类可读的缺省值的内部表现形式

因为它并未跟踪可能影响缺省之表现形式的外部变化。 反编译 adbin 字段(比如,用 pg_get_expr) 是更好的显示缺省值的方法。

45.7. pg_attribute

pg_attribute 表 存储关于表的字段的信息。数据库里每个表的每个字段都在 pg_attribute 里有一行。 (还有用于索引,以及所有在 pg_class 里有记录的对象。)

术语 属性 等效于 列/字段,使用它是历史原因。

Table 45-7. pg_attribute 字段
Name	Type	References	Description
attrelid	oid	pg_class.oid	The table this column belongs to
attname	name	 	The column name
atttypid	oid	pg_type.oid	The data type of this column
attstattarget	int4	 	attstattarget controls the level of detail of statistics accumulated for this column by ANALYZE. A zero value indicates that no statistics should be collected. A negative value says to use the system default statistics target. The exact meaning of positive values is data type-dependent. For scalar data types, attstattarget is both the target number of "most common values" to collect, and the target number of histogram bins to create.
attlen	int2	 	A copy of pg_type.typlen of this column's type
attnum	int2	 	The number of the column. Ordinary columns are numbered from 1 up. System columns, such as oid, have (arbitrary) negative numbers.
attndims	int4	 	Number of dimensions, if the column is an array type; otherwise 0.(Presently, the number of dimensions of an array is not enforced, so any nonzero value effectively means "it's an array".)
attcacheoff	int4	 	Always -1 in storage, but when loaded into a row descriptor in memory this might be updated to cache the offset of the attribute within the row
atttypmod	int4	 	atttypmod records type-specific data supplied at table creation time (for example, the maximum length of a varchar column). It is passed to type-specific input functions and length coercion functions. The value will generally be -1 for types that do not need atttypmod.
attbyval	bool	 	A copy of pg_type.typbyval of this column's type
attstorage	char	 	Normally a copy of pg_type.typstorage of this column's type. For TOAST-able data types, this can be altered after column creation to control storage policy.
attalign	char	 	A copy of pg_type.typalign of this column's type
attnotnull	bool	 	This represents a not-null constraint. It is possible to change this column to enable or disable the constraint.
atthasdef	bool	 	This column has a default value, in which case there will be a corresponding entry in the pg_attrdef catalog that actually defines the value.
attisdropped	bool	 	This column has been dropped and is no longer valid. A dropped column is still physically present in the table, but is ignored by the parser and so cannot be accessed via SQL.
attislocal	bool	 	This column is defined locally in the relation. Note that a column can be locally defined and inherited simultaneously.
attinhcount	int4	 	The number of direct ancestors this column has. A column with a nonzero number of ancestors cannot be dropped nor renamed.
attcollation	oid	pg_collation.oid	The defined collation of the column, or zero if the column is not of a collatable datatype.
attacl	aclitem[]	 	Column-level access privileges, if any have been granted specifically on this column
attoptions	text[]	 	Attribute-level options, as "keyword=value" strings 

在一个删除了的字段的 pg_attribute 记录里, atttypid 重置为零,但是 attlen 和其它从 pg_type 拷贝的仍然有效。 这么安排是为了对付后来背删除的字段的数据类型也被删除的情况, 因为这个时候不再有 pg_type 行了。 attlen 和其它字段可以用于解析表中一行内容。

45.8. pg_authid

系统表 pg_authid 包含有关数据库认证标识符(角色)的信息。 一个角色体现"用户"和"组"的概念。 一个用户实际上只是一个设置了 rolcanlogin 标志的角色。 任何角色(不管设置了 rolcanlogin 标志)都可以有其它角色做为成员; 参阅 pg_auth_members。

因为这个系统表包含口令,所以它不是公共可读的。 pg_roles 是一个在 pg_authid 上的视图,只是把口令域填成了空白。

Chapter 18 包含用户和权限管理的详细信息。

因为用户标识是集群范围的,pg_authid 在一个集群里所有的数据库之间是共享的: 每个集群只有一个 pg_authid 拷贝,而不是每个数据库一个。

Table 45-8. pg_authid 字段
名字	类型	引用	描述
rolname	name	Role name
rolsuper	bool	Role has superuser privileges
rolinherit	bool	Role automatically inherits privileges of roles it is a member of
rolcreaterole	bool	Role can create more roles
rolcreatedb	bool	Role can create databases
rolcatupdate	bool	Role can update system catalogs directly. (Even a superuser cannot do this unless this column is true)
rolcanlogin	bool	Role can log in. That is, this role can be given as the initial session authorization identifier.
rolreplication	bool	Role is a replication role. That is, this role can initiate streaming replication (see Section 25.2.5) and set/unset the system backup mode using pg_start_backup and pg_stop_backup.
rolconnlimit	int4	For roles that can log in, this sets maximum number of concurrent connections this role can make. -1 means no limit.
rolpassword	text	Password (possibly encrypted); null if none. If the password is encrypted, this column will begin with the string md5 followed by a 32-character hexadecimal MD5 hash. The MD5 hash will be of the user's password concatenated to their username. For example, if user joe has password xyzzy, PostgreSQL will store the md5 hash of xyzzyjoe. A password that does not follow that format is assumed to be unencrypted.
rolvaliduntil	timestamptz	Password expiry time (only used for password authentication); null if no expiration

45.9. pg_auth_members

系统表 pg_auth_members 显示角色之间的成员关系。 任何非闭环的关系集合都是允许的。

因为用户标识是集群范围的,pg_auth_members 是在一个集群里的所有数据库之间共享的:每个集群里只有一个 pg_auth_members 拷贝,而不是每个数据库一个。

Table 45-9. 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	 	加入 member 可以把 roleid 角色的成员关系赋予其它角色,则为真。

45.10. pg_cast

pg_cast 表存储数据类型转换路径, 包括内置路径和那些通过 CREATE CAST 定义的。

Table 45-10. pg_cast 字段
名字	类型	引用	描述
castsource	oid	pg_type.oid	源数据类型的 OID
casttarget	oid	pg_type.oid	目标数据类型的OID
castfunc	oid	pg_proc.oid	用于执行这个转换的函数的 OID。如果该数据类型是 二进制兼容的,那么为零(也就是说,不需要运行时的操作来执行 转换)。
castcontext	char	 	标识这个转换可以在什么环境里调用。 e 表示只能进行明确的转换(使用 CAST 或者 ::)。 a 表示在赋值给目标字段的时候隐含调用, 也可以明确调用。 i 表示在表达式中隐含,当然也包括其他情况。
castmethod	char	 	Indicates how the cast is performed. f means that the function specified in the castfunc field is used. i means that the input/output functions are used. b means that the types are binary-coercible, thus no conversion is required. 

在 pg_cast 里列出的类型转换函数必须总是以类型转换的源类型作为它的第一个参数类型, 并且返回类型转换的目的类型作为它的结果类型。一个类型转换函数最多有三个参数。 如果出现了第二个参数,必须是 integer 类型;它接受与目标类型关联的修饰词, 如果没有,就是 -1。如果出现了第三个参数,那么必须是 boolean 类型; 如果该类型转换是一种明确的转换,那么它接受 true,否则接受 false。

在 pg_cast 里创建一条源类型和目标类型相同的记录是合理的, 只要相关联的函数接受多过一个参数。这样的记录代表"长度转换函数", 他们把该类型的数值转换为对特定的类型修饰词数值合法的值。 不过,请注意,现在还不支持将非缺省类型修饰词和用户创建数据类型关联起来, 因此这个设施只用于少量的内置类型,这些类型都有内置于语法分析器里的类型修饰词语法。

如果一条 pg_cast 的记录有着不同的原类型和目标类型, 并且有一个接收多于一个参数的函数,那么它就意味着用一个步骤从一种类型转换到另外一种类型, 同时还附加一个长度转换。如果没有这样的记录,那么转换成一个使用了类型修饰词的类型涉及两个步骤, 一个是在数据类型之间转换,另外一个是附加修饰词。

42.12. pg_class

pg_class 表记载表和几乎所有有 字段或者是那些类似表的东西。包括索引(不过还要参阅 pg_index),序列,视图,复合类型和一些特殊关系类型; 参阅 relkind。 在下面,当我们指所有这些对象的时候我们说 "relations"(关系)。 不是所有字段对所有关系类型都有意义。

Table 42-12. pg_class 字段
Name	Type	References	Description
relname	name	 	Name of the table, index, view, etc.
relnamespace	oid	pg_namespace.oid	The OID of the namespace that contains this relation
reltype	oid	pg_type.oid	The OID of the data type that corresponds to this table's row type, if any (zero for indexes, which have no pg_type entry)
reloftype	oid	pg_type.oid	For typed tables, the OID of the underlying composite type, zero for all other relations
relowner	oid	pg_authid.oid	Owner of the relation
relam	oid	pg_am.oid	If this is an index, the access method used (B-tree, hash, etc.)
relfilenode	oid	 	Name of the on-disk file of this relation; zero means this is a "mapped" relation whose disk file name is determined by low-level state
reltablespace	oid	pg_tablespace.oid	The tablespace in which this relation is stored. If zero, the database's default tablespace is implied. (Not meaningful if the relation has no on-disk file.)
relpages	int4	 	Size of the on-disk representation of this table in pages (of size BLCKSZ). This is only an estimate used by the planner. It is updated by VACUUM, ANALYZE, and a few DDL commands such as CREATE INDEX.
reltuples	float4	 	Number of rows in the table. This is only an estimate used by the planner. It is updated by VACUUM, ANALYZE, and a few DDL commands such as CREATE INDEX.
reltoastrelid	oid	pg_class.oid	OID of the TOAST table associated with this table, 0 if none. The TOAST table stores large attributes "out of line" in a secondary table.
reltoastidxid	oid	pg_class.oid	For a TOAST table, the OID of its index. 0 if not a TOAST table.
relhasindex	bool	 	True if this is a table and it has (or recently had) any indexes
relisshared	bool	 	True if this table is shared across all databases in the cluster. Only certain system catalogs (such as pg_database) are shared.
relpersistence	char	 	p = permanent table, u = unlogged table, t = temporary table
relkind	char	 	r = ordinary table, i = index, S = sequence, v = view, c = composite type, t = TOAST table, f = foreign table
relnatts	int2	 	Number of user columns in the relation (system columns not counted). There must be this many corresponding entries in pg_attribute. See also pg_attribute.attnum.
relchecks	int2	 	Number of CHECK constraints on the table; see pg_constraint catalog
relhasoids	bool	 	True if we generate an OID for each row of the relation
relhaspkey	bool	 	True if the table has (or once had) a primary key
relhasrules	bool	 	True if table has (or once had) rules; see pg_rewrite catalog
relhastriggers	bool	 	True if table has (or once had) triggers; see pg_trigger catalog
relhassubclass	bool	 	True if table has (or once had) any inheritance children
relfrozenxid	xid	 	All transaction IDs before this one have been replaced with a permanent ("frozen") transaction ID in this table. This is used to track whether the table needs to be vacuumed in order to prevent transaction ID wraparound or to allow pg_clog to be shrunk. Zero (InvalidTransactionId) if the relation is not a table.
relacl	aclitem[]	 	Access privileges; see GRANT and REVOKE for details
reloptions	text[]	 	Access-method-specific options, as "keyword=value" strings 

Several of the Boolean flags in pg_class are maintained lazily: they are guaranteed to be true if that's the correct state, but may not be reset to false immediately when the condition is no longer true. For example, relhasindex is set by CREATE INDEX, but it is never cleared by DROP INDEX. Instead, VACUUM clears relhasindex if it finds the table has no indexes. This arrangement avoids race conditions and improves concurrency.

42.13. pg_constraint

系统表 pg_constraint 存储表上的检查约束、主键、唯一约束和外键约束。 (字段约束不会得到特殊对待。每个字段约束都等效于某些表约束。) 参阅 CREATE TABLE 获取更多信息。

在域上面的检查约束存储在这里。

Table 42-13. pg_constraint 字段 名字 类型 引用 描述 conname name 约束名字(不一定是唯一的!) connamespace oid pg_namespace.oid 包含这个约束的名字空间的 OID contype char c = 检查约束, f = 外键约束, p = 主键约束, u = 唯一约束 condeferrable boolean 这个约束可以推迟吗? condeferred boolean 缺省时这个约束是否推迟的? 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 如果是检查约束,则是表达式的人类可读形式

   注意: consrc 在被引用的对象改变之后不会被更新, 它不会跟踪字段的名字修改。与其依赖这个字段,我们最好还是使用 pg_get_constraintdef() 来抽取一个检查约束的定义。 
   注意: pg_class.relchecks 需要和在此表上为给定关系找到的检查约束的数目一致。 

42.14. pg_conversion

系统表 pg_conversion 描述可用的编码转换信息。参阅 CREATE CONVERSION 获取更多信息。

Table 42-14. pg_conversion 字段 名字 类型 引用 描述 conname name 转换名字(在一个名字空间里是唯一的) connamespace oid pg_namespace.oid 包含这个转换的名字空间的 OID conowner oid pg_authid.oid 编码转换的所有者 conforencoding int4 源编码 ID contoencoding int4 目的编码 ID conproc regproc pg_proc.oid 转换过程 condefault boolean 如果这是缺省转换则为真


42.15. pg_database

pg_database 表存储关于可用数据库的信息。 数据库是用 CREATE DATABASE创建的。 参考 Chapter 19 获取一些参数的详细含义。

和大多数系统表不同,pg_database 是在一个集群里的所有数据库共享的∶每个集群只有一份 pg_database 拷贝,而不是每个数据库一份。

Table 42-15. pg_database 字段 名字 类型 引用 描述 datname name 数据库名字 datdba oid pg_authid.oid 数据库所有人,通常为其创建者 encoding int4 数据库的字符编码方式 datistemplate bool 如果为真则此数据库可以用于CREATE DATABASE TEMPLATE子句,把新数据库创建为此数据库的克隆。 datallowconn bool 如果为假则没有人可以联接到这个数据库。这个字段用于保护 template0 数据库不被更改。 datlastsysoid oid 数据库里最后一个系统 OID; 对 pg_dump 特别有用 datvacuumxid xid 在该数据库中这个标志标记为在"已知已提交"或者"已知已退出"之前, 所有元组都以事务 ID 插入或删除。这个标志用于判断何时提交日志空间可以循环使用。 datfrozenxid xid 在该数据库中这个标志标记为一个永久的("冻结(frozen)")的事务 ID 之前, 所有元组都以事务 ID 插入。 该标志可以用于检查一个数据库是否应该马上清理以避免事务 ID 重叠的问题。 dattablespace text pg_tablespace.oid 该数据库的缺省表空间。在这个数据库里,所有 pg_class.reltablespace 为零的表都将保存在这个表空间里;特别要指出的是,所有非共享的系统表也都存放在这里。 datconfig text[] 运行时配置变量的会话缺省值 datacl aclitem[] 访问权限,参阅 GRANT 和 REVOKE 获取详细描述。


42.16. pg_depend

pg_depend 表记录数据库对象之间的依赖关系。 这个信息允许 DROP 命令找出哪些其他的对象必须由 DROP CASCADE 删除, 或者是在 DROP RESTRICT 的情况下避免删除。

又见 pg_shdepend, 这个表的功能类似,用于记录那些在数据库集群之间共享的对象之间的依赖性关系。

Table 42-16. pg_depend 字段 名字 类型 引用 描述 classid oid pg_class.oid 有倚赖对象所在系统表的 OID objid oid 任何 OID 属性 指定的有依赖对象的 OID objsubid int4 对于表字段,这个是该属性的字段数(objid和classid引用表本身)。 对于所有其他对象类型,目前这个字段是零。 refclassid oid pg_class.oid 被引用对象所在的系统表的 oid refobjid oid 任意 OID 属性 指定的被引用对象的 OID refobjsubid int4 对于表字段,这个是该字段的字段号(objid和classid引用表本身)。 对于所有其他对象类型,目前这个字段是零。 deptype char 一个定义这个依赖关系特定语义的代码。见文本。

在所有情况下,一个 pg_depend 记录表示被引用的对象不能在有依赖的对象被删除前删除。 不过,这里还有几种由 deptype 定义的情况:

DEPENDENCY_NORMAL (n)

   独立创建的对象之间的一般关系。 有倚赖的对象可以在不影响被引用对象的情况下删除。 被引用对象只有在声明了CASCADE的情况下删除,这时有依赖的对象也被删除。 例子:一个表字段对其数据类型有一般依赖关系。 

DEPENDENCY_AUTO (a)

   有依赖对象可以和被引用对象分别删除, 并且如果删除了被引用对象则应该被自动删除(不管是RESTRICT还是CASCADE模式)。 例子:一个表上面的命名约束是在该表上的自动依赖关系,因此如果删除了表,它也会被删除。 

DEPENDENCY_INTERNAL (i)

   有依赖的对象是作为被引用对象的一部分创建的, 实际上只是它的内部实现的一部分。 DROP有依赖对象是不能直接允许的(我们将告诉用户发出一条删除被引用对象的DROP)。 一个对被引用对象的DROP 将传播到有依赖对象,不管是否声明了CASCADE。 例子:一个创建来强制外键约束的触发器在该约束的pg_constraint 记录上是标记为内部依赖的。 

DEPENDENCY_PIN (p)

   没有有依赖对象;这种类型的记录标志着系统本身依赖于被引用对象,因此这个对象决不能被删除。 这种类型的记录只有在initdb的时候创建。有依赖对象的字段里是零。 

将来可能还会有其他依赖的风格。

42.17. pg_description

pg_description 表可以给每个数据库对象存储一个可选的描述(注释)。 你可以用 COMMENT 命令操作这些描述, 并且可以用 psql 的 \d 命令查看。 许多内置的系统对象的描述提供了pg_description的初始内容。

Table 42-17. pg_description 字段 名字 类型 引用 描述 objoid oid 任意 oid 属性 这条描述所描述的对象的 OID classoid oid pg_class.oid 这个对象出现的系统表的 OID objsubid int4 对于一个表字段的注释,它是字段号(objoid 和 classoid 指向表自身)。对于其它对象类型,它是零。 description text 作为对该对象的描述的任意文本 Prev Home

42.18. pg_index

pg_index 包含关于索引的一部分信息。 其他的信息大多数在 pg_class。

Table 42-18. pg_index 字段 名字 类型 引用 描述 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 如果为真,该索引代表该表的主键。 (这个字段为真的时候indisunique应该总是为真。) indisclustered bool 如果为真,那么该表最后在这个索引上建了簇。 indkey int2vector pg_attribute.attnum 这是一个包含 indnatts 数值的数组,这些数组值表示这个索引所建立的表字段。 比如一个值为 1 3 的意思是第一个字段和第三个字段组成这个索引键字。 这个数组里的零表明对应的索引属性是在这个表字段上的一个表达式,而不是一个简单的字段引用。 indclass oidvector pg_opclass.oid 对于索引键字里面的每个字段,这个字段都包含一个指向所使用的操作符表的 OID,参阅 pg_opclass 获取细节。 indexprs text 表达式树(以 nodeToString() 形式表现) 用于那些非简单字段引用的索引属性。它是一个列表,在 indkey 里面的每个零条目一个元素。如果所有索引属性都是简单的引用,则为空。 indpred text 部分索引断言的表达式树(是以nodeToString()的形式表现的)。如果不是部分索引, 则是空字串。


42.19. pg_inherits

表pg_inherits记录关于表继承层次的信息。 数据库里每个直接的子系表都有一条记录。 (间接的继承可以通过追溯记录链来判断。)

Table 42-19. pg_inherits 字段 名字 类型 引用 描述 inhrelid oid pg_class.oid 子表的 OID。 inhparent oid pg_class.oid 父表的 OID。 inhseqno int4 如果一个子表存在多个直系父表(多重继承), 这个数字表明此继承字段的排列顺序。计数从 1 开始。


42.20. pg_language

pg_language 登记编程语言, 你可以用这些语言或接口写函数或者存储过程。 参阅 CREATE LANGUAGE 和 Chapter 35 获取更多关于语言句柄的信息。

Table 42-20. pg_language 字段 名字 类型 引用 描述 lanname name 语言的名字 lanispl bool 对于内部语言而言是假(比如SQL),对于用户定义的语言则是真。 目前,pg_dump 仍然使用这个东西判断 哪种语言需要转储,但是这些可能在将来被其他机制取代。 lanpltrusted bool 如果这是可信语言则为真,意味着系统相信它不会被授予任何正常 SQL 执行环境之外的权限。 只有超级用户可以创建不可信的语言。 lanplcallfoid oid pg_proc.oid 对于非内部语言,这是指向该语言句柄的引用,语言句柄是一个特殊函数, 负责执行以某种语言写的所有函数。 lanvalidator oid pg_proc.oid 这个字段引用一个语言校验器函数,它负责检查新创建的函数的语法和有效性。 如果没有提供校验器,则为零。 lanacl aclitem[] 访问权限,参阅 GRANT 和 REVOKE 获取细节。

42.21. pg_largeobject

pg_largeobject 表保存那些标记着"大对象"的数据。 一个大对象是使用其创建时分配的 OID 标识的。 每个大对象都分解成足够小的小段或者"页面"以便以行的形式存储在 pg_largeobject 里。 每页的数据定义为LOBLKSIZE(目前是BLCKSZ/4,或者通常是 2K 字节)。

Table 42-21. pg_largeobject 字段 名称 类型 引用 描述 loid oid 包含本页的大对象的标识符 pageno int4 本页在其大对象数据中的页码 从零开始计算 data bytea 存储在大对象中的实际数据。 这些数据绝不会超过LOBLKSIZE字节,而且可能更少。

pg_largeobject 的每一行保存一个大对象的一个页面, 从该对象内部的字节偏移(pageno * LOBLKSIZE)开始。 这种实现允许松散的存储:页面可以丢失,而且可以比LOBLKSIZE 字节少 — 即使它们不是对象的最后一页。大对象内丢失的部分读做零。

42.22. pg_listener

pg_listener 表支持 LISTEN 和 NOTIFY 命令。 一个监听器为它监听的每个通知名称在 pg_listener 里创建一条记录。 一个通知发起人扫描 pg_listener 并且更新每条匹配的记录以显示一个通知已经发生。 通知发起人还发送一个信号给监听器(使用记录在表中的 PID)以唤醒它做处理。

Table 42-22. pg_listener 字段 名称 类型 引用 描述 relname name 通知条件名。(该名字不需要匹配任何数据库中的实际关系; 项 "relname" 是历史原因形成的。) listenerpid int4 创建此条目的服务器进程的 PID。 notification int4 如果这个监听器上没有等待的事件,那么是零。 如果有等待的事件,那么是发送通知的服务器的 PID。

42.23. pg_namespace

表 pg_namespace 存储名字空间。 名字空间是 SQL92 模式下层的结构∶每个名字空间有独立的关系, 类型等集合但并不会相互冲突。

Table 42-23. pg_namespace 字段 名字 类型 参考 描述 nspname name 名字空间的名字 nspowner oid pg_authid.oid 名字空间的所有者 nspacl aclitem[] 访问权限; 参阅 GRANT 和 REVOKE 获取细节。


42.24. pg_opclass

pg_opclass 定义索引访问方法操作符表。 每个操作符表为一种特定数据类型和一种特定索引访问方法定义索引字段的语义。 请注意对于一种指定的数据类型/访问方法组合,可能会有多种操作符表,这样就可以支持多种行为。

操作符表在 Section 32.14 里有比较详细的描述。

Table 42-24. pg_opclass 字段 名字 类型 参考 描述 opcamid oid pg_am.oid 操作符表所服务的索引访问方法 opcname name 这个操作符表的名字 opcnamespace oid pg_namespace.oid 这个操作符表的名字空间 opcowner oid pg_authid.oid 操作符表所有人 opcintype oid pg_type.oid 操作符表的输入数据类型 opcdefault bool 如果操作符表是opcintype(见前一个字段)的缺省,则为真 opckeytype oid pg_type.oid 索引数据的类型,如果和opcintype相同则为零

定义一个操作符表的大部分信息实际上并不在它的 pg_opclass 行里,而是在相关的 pg_amop 和 pg_amproc 行里。我们认为这些行是定义操作符表的一部分 — 这和用一行 pg_class 加上在 pg_attribute 和其他表中的行定义一个关系(表)没啥不同。


42.25. pg_operator

表 pg_operator 存储有关操作符的信息。 参阅 CREATE OPERATOR 和 Section 32.12 获取这些操作符参数的细节。

Table 42-25. pg_operator 字段 名字 类型 引用 描述 oprname name 操作符名字 oprnamespace oid pg_namespace.oid 包含此操作符的名字空间的 OID。 oprowner oid pg_authid.oid 操作符所有者 oprkind char b = 中缀 ("两边"), l = 前缀 ("左边"), r = 后缀 ("右边") oprcanhash bool 这个操作符支持散列连接 oprleft oid pg_type.oid 左操作数的类型 oprright oid pg_type.oid 右操作数的类型 oprresult oid pg_type.oid 结果类型 oprcom oid pg_operator.oid 如果存在的话,此操作符的交换符 oprnegate oid pg_operator.oid 如果存在的话,此操作符的反转器 oprlsortop oid pg_operator.oid 如果此操作符支持融合连接,这里是对左手边操作数排序的操作符 (L<L) oprrsortop oid pg_operator.oid 如果此操作符支持融合连接,这里是对右手边操作数排序的操作符 (R<R) oprltcmpop oid pg_operator.oid 如果这个操作符支持融合连接,那么这是比较左右操作数类型(L<R)的小于操作符。 oprgtcmpop oid pg_operator.oid 如果这个操作符支持融合连接,那么这是比较左右操作数类型(L>R)的小于操作符。 oprcode regproc pg_proc.oid 实现这个操作符的函数 oprrest regproc pg_proc.oid 此操作符的约束选择性计算函数 oprjoin regproc pg_proc.oid 此操作符的连接选择性计算函数

未用的字段包含零,比如,oprleft对于前缀操作符而言是零。


42.26. pg_pltemplate

系统表 pg_pltemplate 为过程语言存储"模板"信息。 一个语言的模板允许该语言可以在某个数据库里使用简单的 CREATE LANGUAGE 命令创建, 而不许要声明实现细节。

和许多系统表不一样,pg_pltemplate 是在集群里的所有数据库之间共享的: 每个集群只有一个 pg_pltemplate 的拷贝,而不是每个数据库一个。 这样就允许这些信息在需要时每个数据库都可以访问。

Table 42-26. pg_pltemplate 字段 名字 类型 引用 描述 tmplname name 这个模板所服务的语言的名字 tmpltrusted boolean 如果语言被人为是可信的,则为真 tmplhandler text 调用句柄函数的名字 tmplvalidator text 校验函数的名字,如果没有则为 NULL tmpllibrary text 实现语言的共享库的路径 tmplacl aclitem[] 模板的访问权限(未用)

目前还没有任何命令可以用语操作过程语言模板;要修改内置的信息, 超户必须使用普通的 INSERT,DELETE,或者 UPDATE 命令修改该表。 将来版本的 PostgreSQL 有可能提供一些命令来修改这些内容, 以便更清晰些。

在实现的时候,tmplacl 字段将提供对模板自身的访问控制 (也就是使用它创建一个语言的权限),而不是对那些从模板创建的语言的访问控制。


42.27. pg_proc

pg_proc 表存储关于函数(或过程)的信息。 CREATE FUNCTION 和 Section 32.3 包含一些字段含义的更多信息。

该表包含聚集函数和普通函数的数据。 如果 proisagg 为真,那么在 pg_aggregate 里应该有一个匹配行。

Table 42-27. pg_proc 字段 名字 类型 引用 描述 proname name 函数名字 pronamespace oid pg_namespace.oid 包含该函数名字空间的 OID。 proowner oid pg_authid.oid 函数所有者 prolang oid pg_langauge.oid 这个函数的实现语言或调用接口 proisagg bool 函数是聚集函数 prosecdef bool 函数是一个安全定义器(也就是说,一个"setuid"函数) proisstrict bool 如果任何调用参数是空,那么函数返回空。这时函数实际上连调用都不调用。 不是"strict"的函数必须准备处理空输入。 proretset bool 函数返回一个集合(也就是说,指定数据类型的多个数值) provolatile char provolatile 告诉我们该函数的结果是否只倚赖于它的输入参数,或者还会被外接因素影响。 对于"不可变的(immutable)"函数它是i, 这样的函数对于相同的输入总是产生相同的结果。 对于"稳定(stable)"的函数它是 s, (对于固定输入)其结果在一次扫描里不变。 对于"易变(volatile)"函数它是 v, 其结果可能在任何时候变化。(v 也用于那些有副作用的函数,因此调用它们无法得到优化。) pronargs int2 参数数目 prorettype oid pg_type.oid 返回值的数据类型 proargtypes oidvector pg_type.oid 一个存放函数参数的数据类型的数组。数组里只包括输入参数(包括 INOUT 参数),因此代表 该函数的调用签名(接口)。 proallargtypes oid[] pg_type.oid 一个包含函数参数的数据类型的数组。数组里包括所有参数的类型(包括 OUT 和 INOUT 参数); 不过,如果所有参数都是 IN 参数,那么这个字段就会是空。 请注意数组下标是以 1 为起点的,而因为历史原因,proargtypes 的下标起点为 0。 proargmodes char[] 一个保存函数参数模式的数组,编码如下:i 表示 IN 参数, o 表示 OUT 参数, b 表示 INOUT 参数。 如果所有参数都是 IN 参数,那么这个字段为空。请注意,下标对应的是 proallargtypes 的位置,而不是 proargtypes。 proargnames text[] 一个保存函数参数的名字的数组。没有名字的参数在数组里设置为空字串。 如果没有一个参数有名字,这个字段将是空。 请注意,此数组的下标对应 proallargtypes,而不是 proargtypes。 prosrc text 这个字段告诉函数句柄如何调用该函数。它实际上对于解释语言来说就是函数的源程序, 或者一个链接符号,一个文件名,或者是任何其他的东西,具体取决于语言/调用习惯的实现。 probin bytea 关于如何调用该函数的附加信息。同样,其含义也是和语言相关的。 proacl aclitem[] 访问权限; 参阅 GRANT 和 REVOKE 获取细节。

对于内置和动态装载得编译函数,prosrc 包含函数的 C 语言名字(链接符号)。 对于所有其它语言类型,prosrc 包含该函数的源文本。 probin 除了用于动态装载的 C 函数之外没有其它用途, 这个时候它给出包含给函数的共享库的文件名。


42.28. pg_rewrite

系统表pg_rewrite存储为表和视图定义的重写规则。

Table 42-28. pg_rewrite 字段 名称 类型 引用 描述 rulename name 规则名称 ev_class oid pg_class.oid 使用这条规则的表名称 ev_attr int2 这条规则适用的字段(目前总是为零,表示整个表) ev_type char 规则适用的事件类型:1 = SELECT, 2 = UPDATE, 3 = INSERT, 4 = DELETE is_instead bool 如果该规则是INSTEAD规则,那么为真 ev_qual text 规则的资格条件的表达式树(以nodeToString形式存在) ev_action text 规则动作的查询树(以nodeToString形式存在)

   注意: 如果一个表在这个系统表里有任何规则存在,那么 pg_class.relhasrules 必须为真。 

42.29. pg_shdepend

系统表 pg_shdepend 记录数据库对象和共享对象,比如角色,之间的依赖性关系。 这些信息允许 PostgreSQL 保证在企图删除这些对象之前,这些对象是没有被引用的。

又见 pg_depend, 这个表作用类似,只是它是用于在一个数据库内部的对象的依赖性关系的。

和其它大多数系统表不同, pg_shdepend 是在集群里面所有的数据库之间共享的: 每个数据库集群只有一个 pg_shdepend,而不是每个数据库一个。

Table 42-29. pg_shdepend 字段 名字 类型 引用 描述 dbid oid pg_database.oid 依赖对象所在的数据库的 OID,如果是共享对象,则为零 classid oid pg_class.oid 依赖对象所在的系统表的 OID objid oid 任何 OID 字段 指定的依赖对象的 OID refclassid oid pg_class.oid 被引用对象所在的系统表的 OID(必须是一个共享表) refobjid oid 任意 OID 字段 指定的被引用对象的 OID deptype char 一段代码,定义了这个依赖性关系的特定语义;参阅其文本。

在任何情况下,一条 pg_shdepend 记录就表面这个被引用的 对象不能在未删除依赖对象的前提下删除。不过, deptype 同时还标出了几种不同的子风格:

SHARED_DEPENDENCY_OWNER (o)

   被引用的对象(必须是一个角色)是依赖对象的所有者。 

SHARED_DEPENDENCY_ACL (a)

   被引用的对象(必须是一个角色)在依赖对象的 ACL (访问控制列表,也就是权限列表)里提到。 (SHARED_DEPENDENCY_ACL 不会在对象的所有者头上添加的, 因为所有者会有一个 SHARED_DEPENDENCY_OWNER 记录。) 

SHARED_DEPENDENCY_PIN (p)

   没有依赖对象;这类记录标识系统自身依赖于该被依赖对象, 因此这样的对象绝对不能被删除。这种类型的记录只是由 initdb 创建。这样的依赖对象的字段都是零。 

其它依赖性的风格可能在将来会出现。请注意,目前的定义只是支持把角色当作被引用对象。

42.30. pg_statistic

pg_statistic表存储有关该数据库内容的统计数据。 记录是 ANALYZE 创建的,并且随后被查询规划器使用。 请注意所有统计信息天生都是近似的数值,即使我们假设它是最新的也如此。

pg_statistic 还存储有关索引表达式数值的统计数据。 这些是把他们当作实际的数据字段来描述的;特别是,starelid 引用索引。 不过,普通的非表达式字段没有记录,因为会和下层的表字段记录冗余。

因为不同类型的统计信息适用于不同类型的数据, pg_statistic 被设计成不太在意自己存储的是什么类型的统计。 只有极为常用的统计信息(比如NULL的含量)才在 pg_statistic里给予专用的字段。 其它所有东西都存储在"槽位"中,而槽位是一组相关的字段, 它们的内容用槽位中的一个字段的代码号码表示。 更详细的信息请参阅 src/include/catalog/pg_statistic.h。

pg_statistic 不应该是公众可读的, 因为即使是表内容的统计信息也应该认为是敏感的。 (例子:薪水字段的最大最小值肯定是相当让人感兴趣的。) pg_stats 是一个在 pg_statistic 上的全局可读的视图,它只显示那些表对于当前用户可读的信息。 pg_stats 也设计成具有比其所依靠的下层 pg_statistic 表更具可读性的信息 --- 代价是增加新的槽位类型的时候,它的模式必须扩展。

Table 42-30. pg_statistic 字段 名字 类型 参考 描述 starelid oid pg_class.oid 所描述的字段所属的表或者索引 staattnum int2 pg_attribute.attnum 所描述的字段的个数 stanullfrac float4 该字段中为 NULL 的记录的比率 stawidth int4 非 NULL 记录的平均存储宽度,以字节计 stadistinct float4 字段里唯一的非 NULL 数据值的数目。 一个大于零的数值是独立数值的实际数目。 一个小于零的数值是表中行数的分数的负数(比如,一个字段的数值平均出现概率为两次,那么可以表示为 stadistinct = -0.5)。 零值表示独立数值的数目未知。 stakindN int2 一个编码,表示这种类型的统计存储在 pg_statistic 行的 第N个"槽位"。 staopN oid pg_operator.oid 一个用于生成这些存储在第N个"槽位"的统计信息的操作符。 比如,一个柱面图槽位会显示<操作符,该操作符定义了 该数据的排序顺序。 stanumbersN float4[] 第N个"槽位"的相关类型的数值类型统计, 如果该槽位和数值类型没有关系,那么就是NULL。 stavaluesN anyarray 第N个"槽位"相关类型的字段数据值,如果该槽位类型不存储 任何数据值那么就是 NULL。 每个数组的元素值实际上都是指定字段的数据类型, 因此,除了把这些字段的类型定义成 "anyarray" 之外, 没有更好的办法。


42.31. pg_tablespace

系统表 pg_tablespace 存储有关可用的表空间的信息。 表可以放置在特定的表空间里,以帮助管理磁盘布局。

与大多数系统表不同,pg_tablespace 在一个集群中的所有数据库之间共享: 每个集群只有一份 pg_tablespace 的拷贝,而不是每个数据库一个。

Table 42-31. pg_tablespace 字段 名字 类型 参考 描述 spcname name 表空间名字 spcowner oid pg_authid.oid 表空间的所有者,通常是创建它的人 spclocation text 表空间的位置(目录路径) spcacl aclitem[] 访问权限; 参阅 GRANT 和 REVOKE 获取细节。


42.32. pg_trigger

系统表pg_trigger存储表上面的触发器。参阅 CREATE TRIGGER 获取更多信息。

Table 42-32. pg_trigger 字段 名称 类型 引用 描述 tgrelid oid pg_class.oid 这个触发器所处的表 tgname name 触发器名称(在同一表的所有触发器中必须唯一) tgfoid oid pg_proc.oid 要调用的函数 tgtype int2 标识触发器条件的位掩码 tgenabled bool 如果触发器打开则为真 tgisconstraint bool 如果触发器实现一个参考完整性约束则为真 tgconstrname name 参考完整性约束的名称 tgconstrrelid oid pg_class.oid 一个参考完整性约束引用的表 tgdeferrable bool 如果可推迟则为真 tginitdeferred bool 如果是初始可推迟则为真 tgnargs int2 传递给触发器函数的参数字串个数 tgattr int2vector 目前未用 tgargs bytea 传递给触发器的参数字串,每个都是用空零结尾

   注意: pg_class.reltriggers 需要和此表中指定关系上得触发器的个数相等。 

42.33. pg_type

系统表pg_type存储有关数据类型的信息。 基本类型(标量类型)是用 CREATE TYPE 创建的, 域是使用 CREATE DOMAIN 创建的。 同时还为数据库中每个表自动创建一个复合类型,以表示该表的行结构。 我们还可以用CREATE TYPE AS创建复合类型。

Table 42-33. pg_type 字段 名字 类型 引用 描述 typname name 数据类型名字 typnamespace oid pg_namespace.oid 包含这个类型的名字空间的 OID typowner oid pg_authid.oid 该类型的所有者 typlen int2 对于定长类型,typlen是该类型内部表现形式的字节数目。 对于变长类型,typlen 是负数。 -1 表示一种"变长"类型(有长度字属性的数据), -2 表示这是一个 NULL 结尾的 C 字串。 typbyval bool typbyval 判断内部过程传递这个类型的数值时是通过传值还是传引用。 只有 char,short,和 int 或相当的项目可以传值,所以如果该类型不是 1,2,4字节长, PostgreSQL 不会有传值的选择,因此 typbyval 最好是假。 变长类似总是传引用。请注意即使长度可以传值, typbyval 也可以为假; 比如,现在 float4就是如此。 typtype char typtype对于基础类型 是 b,对于复合类型是 c (比如,一个表的行类型)。 衍生类型时是 d (也就是说,一个域), 或者是p 表示伪类型。 又见 typrelid 和 typbasetype。 typisdefined bool 如果定义了类型则为真,如果是一种尚未定义的类型的占位符则为假。 如果typisdefined为假,那么除了该类型名称,名字空间,和 OID 之外没有可靠的信息。 typdelim char 当分析数组输入时,分隔两个此类型数值的字符请注意该分隔符是与数组元素数据类型相关联的, 而不是和数组数据类型关联。 typrelid oid pg_class.oid 如果是复合类型(见 typtype) 那么这个字段指向 pg_class 中定义该表的元组。 (对于自由存在的复合类型,pg_class 记录并不表示一个表,但是我们总需要它来查找该类型联接的 pg_attribute 记录。) 非复合类型为零。 typelem oid pg_type.oid 如果 typelem 不为 0, 那么它标识 pg_type 里面的另外一行。 当前类型可以当做一个产生类型为 typelem 的数组来描述。 一个"真正的" 数组类型是变长的 (typlen = -1), 但是一些定长的(typlen> 0)类型也拥有非零的 typelem,比如 name 和 point。 如果一个定长类型拥有一个typelem, 那么他的内部形式必须是 typelem 数据类型的某个数目的个数值,不能有其它数据。变长数组类型有一个该数组子过程定义的头(文件)。 typinput regproc pg_proc.oid 输入转换函数(文本格式) typoutput regproc pg_proc.oid 输出转换函数(文本格式) typreceive regproc pg_proc.oid 输入转换函数(二进制格式),如果没有则为 0 typsend regproc pg_proc.oid 输出转换函数(二进制格式),如果没有则为 0 typanalyze regproc pg_proc.oid 客户化的 ANALYZE 函数,如果使用标准函数,则为 0 typalign char

typalign 是当存储此类型的数值是要求的对齐性质。它应用于磁盘存储以及该值在 PostgreSQL 内部的大多数形式。 如果数值是连续存放的,比如在磁盘上以完全的裸数据的形式存放时, 那么先在此类型的数据前填充空白,这样它就可以按照要求的界限存储。 对齐引用是该序列中第一个数据的开头。

可能的值有:

   *
     c = char 对齐,也就是不需要对齐。
   *
     s = short 对齐 (在大多数机器上是 2字节)
   *
     i = int 对齐 (在大多数机器上是 4 字节)
   *
     d = double 对齐 (在大多数机器上是 8 字节,但不一定是全部)
   Note: 对于在系统表里使用的类型,在 pg_type 里定义的尺寸和对齐必须和编译器在一个表示表的一行的结构里的布局一样。 

typstorage char

typstorage 告诉一个变长类型(那些有 typlen = -1的) 说该类型是否准备 toast,以及对这种属性的类型的缺省策略是什么。 可能的值有

   *
     p: 数值总是以简单方式存储
   *
     e: 数值可以存储在一个"从属" 关系中(如果该关系有这么一个,参阅 pg_class.reltoastrelid).
   *
     m: 数值可以以内联的压缩方式存储
   *
     x: 数值可以以内联的压缩方式或者在"从属"表里存储。

请注意 m 域也可以移到从属表里存储,但只是最后的解决方法 (e 和 x 域先移走)。 typnotnull bool

typnotnull 代表在某类型上的一个 NOT NULL 约束。目前只用于域。 typbasetype oid pg_type.oid

如果这是一个衍生类型(参阅 typtype), 那么 typbasetype 标识作为这个类型 的基础的类型。如果不是衍生类型则为零。 typtypmod int4

域使用 typtypmod 记录要施用到它们的基础类型上的typmod (如果基础类型不使用typmod,那么为 -1)。如果这种类型不是域,那么为 -1。 typndims int4

如果一个域是数组,那么 typndims 是数组维数的数值 (也就是说,typbasetype是一个数组类型;域的typelem将匹配基本类型的 typelem)。非域非数组域为零。 typdefaultbin text

如果 typdefaultbin 为非 NULL,那么它是 该类型缺省表达式的nodeToString表现形式。目前这个字段只用于域。 typdefault text

如果某类型没有相关缺省值,那么typdefault是 NULL。如果 typdefaultbin 不是 NULL, 那么 typdefault 必须包含一个 typdefaultbin 代表的缺省表达式的人类可读的版本。 如果 typdefaultbin 为 NULL 但 typdefault 不是,那么 typdefault 是该类型缺省值的外部表现形式, 我们可以把它交给该类型的输入转换器生成一个常量。

42.34. 系统视图

除了系统表之外,PostgreSQL 还提供了一系列内置的视图。 系统视图提供了查询系统表的一些便利的访问方法。其它一些视图提供了访问内部服务器状态的方法。

信息模式(Chapter 31)提供了另外一套视图, 它的功能覆盖了系统视图的功能。因为信息模式是 SQL 标准,而这里描述的视图是 PostgreSQL 特有的, 所以我们最好用信息模式来获取自己需要的所有信息。

Table 42-34 列出了这里描述的所有系统视图。 下面是每个视图更详细的信息。有些视图提供了对统计收集器的结果的访问; 他们在 Table 24-1 里列出。

除了特别声明的,这里描述的所有视图都是只读的。

Table 42-34. 系统视图 视图名字 用途 pg_group 数据库用户的组 pg_indexes 索引 pg_locks 当前持有的锁 pg_prepared_xacts 目前准备好的事务 pg_roles 数据库角色 pg_rules 规则 pg_settings 参数设置 pg_shadow 数据库用户 pg_stats 规划器统计 pg_tables 表 pg_user 数据库用户 pg_views 视图

42.35. pg_group

视图 pg_group 的存在是为了向下兼容: 它模拟一个存在于 PostgreSQL 版本 8.1 之前的系统表。 它显示所有标记为不是 rolcanlogin 的角色的名字和成员, 这就是近似于用做组的那些角色的集合了。

Table 42-35. pg_group 字段 名字 类型 引用 描述 groname name pg_authid.rolname 组的名字 grosysid oid pg_authid.oid 组的 ID grolist oid[] pg_authid.oid 一个数组,包含这个组里面所有角色的 ID

42.36. pg_indexes

视图 pg_indexes 提供对数据库中每个索引的有用信息的访问。

Table 42-36. 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 索引定义(一个重建的创建命令)

42.37. pg_locks

视图 pg_locks 提供有关在数据库服务器中由打开的事务持有的锁的信息。 参阅 Chapter 12 获取有关锁的更多的讨论。

pg_locks 对每个活跃的可锁定对象,请求的锁模式, 以及相关的事务保存一行。因此,如果多个事务持有或者等待对同一个对象的锁, 那么同一个可锁定的对象可能出现多次。不过,一个目前没有锁在其上的对象将肯定不会出现。

有好几种不同的可锁定对象:一个关系(也就是说,一个表)、关系中独立页面、关系中独立的元组、一个事务 ID、 以及一般的数据库对象(用类别 OID 和对象 OID 标识,表示方法和 pg_description 或 pg_depend 一样。) 还有,扩展(extend)一个关系的权限也是用一种独立的可锁定对象表示的。

Table 42-37. pg_locks 字段 名字 类型 引用 描述 locktype text 可锁定对象的类型: relation, extend, page, tuple, transactionid, object, 或者 userlock database oid pg_database.oid 对象所在的数据库的 OID,如果对象是共享对象, 那么就是零,如果对象是一个事务 ID,就是 NULL。 relation oid pg_class.oid 关系的 OID,如果对象不是关系,也不是关系的一部分,则为 NULL page integer 关系内部的页面编号,如果对象不是元组页不是关系页,则为 NULL tuple smallint 页面里面的元组编号,如果对象不是元组,则为 NULL transactionid xid 事务的 ID,如果对象不是事务 ID,就是 NULL classid oid pg_class.oid 包含该对象的系统表的 OID,如果对象不是普通数据库对象,则为 NULL objid oid 任何 OID 字段 对象在其系统表内的 OID,如果对象不是普通数据库对象,则为 NULL objsubid smallint 对于表的一个字段,这是字段编号(classid 和 objid 指向表自身)。 对于其它对象类型,这个字段是零。如果这个对象不是普通数据库对象,则为 NULL transaction xid 持有此锁或者在等待此锁的事务的 ID。 pid integer  持有或者等待这个锁的服务器进程的进程 ID。  如果锁是被一个准备好的事务持有的,那么为空(null)。 mode text 这个进程持有的或者是期望的锁模式(参阅 Section 12.3.1) granted boolean 如果持有锁,为真,如果等待锁,为假

一行里的 granted 为真时表明指定事务持有一个锁。 假则表明该事务当前等待使用这个锁,这就暗示着某个其它的事务正在同样的可锁定对象上持有冲突的锁模式。 等待的会话将一直睡眠,直到另外一个锁释放(或者侦测到一个死锁条件)。一个事务一次最多等待一个锁。

每个事务都在它持续的时间里在他自己的事务 ID 上持有一个排他锁。 如果一个事务认为它必须等待另外一个事务,它会以企图在另外一个事务 ID 上获取共享锁的方式实现之。 这个锁只有在另外一个事务终止并且释放它的锁的前提下才能成功。

尽管元组是一种可以锁定的对象,但是有关行级别锁的信息是存储在磁盘上的,而不是在内存里, 因此,行级别的锁通常不会出现在这个视图里。如果一个事务在等待一个行级别的锁, 那么它通常会在这个视图里以等待当前持有该行锁的事务 ID 的方式出现。

如果使用了用户定义的锁,那么他们是使用那些给普通数据库对象的字段来显示的。 不过,这种情况下的锁字段的实际含义是取决于用户的。

在访问 pg_locks 视图的时候,内部的锁管理器数据结构会暂时被锁住, 然后制作一份这个视图的拷贝用于显示。这样就保证了视图生成一套连贯的结果,它不会不必要地过分阻塞普通的锁管理器。 但是如果这个视图访问得太频繁,肯定是会对数据库性能有些影响的。

pg_locks 提供了一个数据库集群里的所有的锁的全局视图, 而不仅仅那些和当前数据库相关的。尽管它的 relation 字段可以和 pg_class.oid 连接起来以标识被锁住的关系, 但是这个方法目前只能对在当前数据库里的关系有用(那些 database 字段是当前数据库的 OID 或者零的数据库)。

如果你打开了统计收集器,pid 字段可以可以和 pg_stat_activity 视图的 procpid 字段连接起来获取持有或者等待持有这个锁的会话的更多信息。 同样,如果你使用准备好的事务,可以把 transaction 字段和 pg_prepared_xacts 视图的 transaction 字段连接起来获取持有锁的那个已准备好事务的更多信息。 (一个已准备好的事务不能等待任何锁,但是在运行的时候,它继续持有它已经请求到的锁。)

42.38. pg_prepared_xacts

视图 pg_prepared_xacts 显示那些当前准备好进行两阶段提交的事务的信息 (参阅 PREPARE TRANSACTION 获取细节)。

pg_prepared_xacts 为每个准备好的事务包含一行。 如果事务提交或者回滚,则删除该条记录。

Table 42-38. pg_prepared_xacts 字段 名字 类型 引用 描述 transaction xid 准备好的事务的数字事务标识 gid text 赋予该事务的全局事务标识 prepared timestamp with time zone 事务准备好提交的时间 owner name pg_authid.rolname 执行该事务的用户的名字 database name pg_database.datname 该事务执行所在的数据库的名字

在访问 pg_prepared_xacts 视图的时候, 内部事务管理器数据结构被暂时锁住,并且为显示视图制作了一份拷贝。 这样就保证了视图生成一个一致的结果集,而不会阻塞正常的操作太长时间。 自然了,即便这么做,如果过于频繁地访问这个视图,肯定会对数据库性能造成一定的影响。

42.39. pg_roles

视图 pg_roles 提供访问数据库角色有关信息的接口。 它只是一个 pg_authid 表的公开可读部分的视图,把口令字段用空白填充了。

Table 42-39. pg_roles 字段 名字 类型 引用 描述 rolname name 角色名 rolsuper bool 有超级用户权限的角色 rolcreaterole bool 可以创建更多角色的角色 rolcreatedb bool 可以创建数据库的角色 rolcatupdate bool 可以直接更新系统表的角色。(除非这个字段为真,否则超级用户也不能干这个事情。) rolcanlogin bool 可以登录的角色,也就是说,这个角色可以给予初始化会话认证的标识符。 rolpassword text 不是口令(总是 ********) rolvaliduntil timestamptz 口令失效日期(只用于口令认证);如果没有失效期,为 NULL rolconfig text[] 运行时配置变量的会话缺省

42.40. pg_rules

视图 pg_rules 提供对查询重写规则的有用信息访问的接口。

Table 42-40. pg_rules 字段 名字 类型 引用 描述 schemaname name pg_namespace.nspname 包含表的模式的名字 tablename name pg_class.relname 规则施加影响的表的名字 rulename name pg_rewrite.rulename 规则的名字 definition text 规则定义(一个重新构造的创建命令)

pg_rules 视图排除了视图的 ON SELECT 规则; 就是那些可以在 pg_views 里看到的。

42.41. pg_settings

视图 pg_settings 提供了对服务器运行时参数的访问。 它实际上是 SHOW 和 SET 命令的另外一个接口。 它还提供一些用 SHOW 不能直接获取的参数的访问,比如最大和最小值。

Table 42-41. pg_settings 字段 名字 类型 引用 描述 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 命令的效果将在事务回滚之后消失。 一旦包围它的事务提交,这个效果将固化,直到会话结束,除非由其它的 UPDATE 或者 SET 命令覆盖。


42.42. pg_shadow

视图 pg_shadow 存在是为了向下兼容: 它模拟了一个 PostgreSQL 版本 8.1 之前的系统表。 它显示了所有标记了 rolcanlogin 的角色的属性。

这个系统表的名字来自于该表不能被公众可读,因为它包含口令。 pg_user 是一个在 pg_shadow 上公开可读的视图,只是把口令域填成了空白。

Table 42-42. pg_shadow 字段 名字 类型 引用 描述 usename name pg_authid.rolname 用户名 usesysid oid pg_authid.oid 用户的 ID usecreatedb bool 用户可以创建数据库 usesuper bool 用户是超级用户 usecatupd bool 用户可以更新系统表。(即使超级用户,如果这个字段不是真,也不能更新系统表。) passwd text 口令(可能是加密的) valuntil abstime 口令失效的时间(只用于口令认证) useconfig text[] 运行时配置变量的会话缺省

42.43. pg_stats

视图 pg_stats 提供对存储在 pg_statistic 表里面的信息的访问。这个视图允许我们只访问那些在 pg_statistic 里面对应用户有权限读取的表的数据行,因此我们可以安全地允许公众访问这个视图。

pg_stats 也设计成把信息以一种更易读的方式出现的形式, 它比下层的系统表更容易阅读 — 代价就是如果在 pg_statistic 里定义了新的数据槽位,那么我们必须扩展它的视图定义。

Table 42-43. pg_stats 字段 名字 类型 引用 描述 schemaname name pg_namespace.nspname 包含此表的模式名字 tablename name pg_class.relname 表的名字 attname name pg_attribute.attname 这一行描述的字段的名字 null_frac real 记录中字段为空的百分比 avg_width integer 字段记录以字节记的平均宽度 n_distinct real 如果大于零,就是在字段中独立数值的估计数目。如果小于零,就是独立数值的数目被行数除的负数。 (用负数形式是因为 ANALYZE 认为独立数值的数目是随着表增长而增长; 正数的形式用于在字段看上去好像有固定的可能值数目的情况下。)比如,-1 表示一个唯一字段,独立数值的个数和行数相同 most_common_vals anyarray 一个字段里最常用数值的列表。(如果看上去没有啥数值比其它更常见,则为 NULL。) most_common_freqs real[] 一个最常用数值的频率的列表,也就是说,每个出现的次数除以行数。 (如果 most_common_vals 是 NULL,则为 NULL) histogram_bounds anyarray 一个数值的列表,它把字段的数值分成几组大致相同热门的组。如果在 most_common_vals 里有数值,则在这个饼图的计算中省略。 (如果字段数据类型没有 < 操作符或者 most_common_vals 列表代表了整个分布性,则这个字段为 NULL。) correlation real 统计与字段值的物理行序和逻辑行序有关。它的范围从 -1 到 +1。 在数值接近 -1 或者 +1 的时候,在字段上的索引扫描将被认为比它接近零的时候开销更少,因为减少了对磁盘的随机访问。 (如果字段数据类型没有 < 操作符,那么这个字段为 NULL。)

在 most_common_vals 和 histogram_bounds 数组里的元素的最大数目可以用 ALTER TABLE SET STATISTICS 命令一个一个字段地设置, 或者通过设置运行时环境变量default_statistics_target 全局地设置。

42.44. pg_tables

视图 pg_tables 提供了对有关数据库中每个表的有用信息地访问。

Table 42-44. pg_tables 字段 名字 类型 引用 描述 schemaname name pg_namespace.nspname 包含表的模式名字 tablename name pg_class.relname 表的名字 tableowner name pg_authid.rolname 表的所有者的名字 tablespace name pg_tablespace.spcname 包含表的表空间名字(如果是数据库缺省,则为 NULL) hasindexes boolean pg_class.relhasindex 如果表拥有(或者最近拥有)任何索引,则为真 hasrules boolean pg_class.relhasrules 如果表有规则,则为真 hastriggers boolean pg_class.reltriggers 如果表有触发器,则为真

42.45. pg_user

视图 pg_user 提供了对数据库用户的相关信息的访问。 这个视图只是一个 pg_shadow 的公众可读的部分的视图化,它把口令域给刷掉了。

Table 42-45. pg_user 字段 名字 类型 引用 描述 usename name 用户名 usesysid int4 用户 ID (用于引用这个用户的任意数字) usecreatedb bool 用户可以创建数据库 usesuper bool 用户是一个超级用户 usecatupd bool 用户可以更新系统表。(即使超级用户也不能这么干,除非这个字段为真。) passwd text 口令(可能加密了) valuntil abstime 口令失效的时间(只用于口令认证) useconfig text[] 运行时配置参数的会话缺省

42.46. pg_views

视图 pg_views 提供了对数据库里每个视图的有用信息的访问途径。

Table 42-46. pg_views 字段 名字 类型 引用 描述 schemaname name pg_namespace.nspname 包含此视图的模式名字 viewname name pg_class.relname 视图的名字 viewowner name pg_authid.rolname 视图的所有者的名字 definition text 视图定义(一个重建的 SELECT 查询)