Postgresql 中文操作指南
53.11. pg_class #
目录 pg_class 描述了具有列或以其他方式类似于表的表和其他对象。这包括索引(但也参见 pg_index )、序列(但也参见 pg_sequence )、视图、物化视图、复合类型和 TOAST 表;请参阅 relkind 。在下文中,当我们指代所有这些类型的对象时,我们所说的就是“关系”。并非 pg_class 的所有列对所有关系类型都具有意义。
The catalog pg_class describes tables and other objects that have columns or are otherwise similar to a table. This includes indexes (but see also pg_index), sequences (but see also pg_sequence), views, materialized views, composite types, and TOAST tables; see relkind. Below, when we mean all of these kinds of objects we speak of “relations”. Not all of pg_class's columns are meaningful for all relation kinds.
Table 53.11. pg_class Columns
Table 53.11. pg_class Columns
Column Type Description |
oid oid Row identifier |
relname name Name of the table, index, view, etc. |
relnamespace oid (references pg_namespace.oid) The OID of the namespace that contains this relation |
reltype oid (references pg_type.oid) The OID of the data type that corresponds to this table’s row type, if any; zero for indexes, sequences, and toast tables, which have no pg_type entry |
reloftype oid (references pg_type.oid) For typed tables, the OID of the underlying composite type; zero for all other relations |
relowner oid (references pg_authid.oid) Owner of the relation |
relam oid (references pg_am.oid) If this is a table or an index, the access method used (heap, B-tree, hash, etc.); otherwise zero (zero occurs for sequences, as well as relations without storage, such as views) |
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 (references 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, except for partitioned tables, where this is the tablespace in which partitions will be created when one is not specified in the creation command. |
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 live 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. If the table has never yet been vacuumed or analyzed, reltuples contains -1 indicating that the row count is unknown. |
relallvisible int4 Number of pages that are marked all-visible in the table’s visibility map. 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 (references pg_class.oid) OID of the TOAST table associated with this table, zero if none. The TOAST table stores large attributes “out of line” in a secondary 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/sequence, u = unlogged table/sequence, t = temporary table/sequence |
relkind char r = ordinary table, i = index, S = sequence, t = TOAST table, v = view, m = materialized view, c = composite type, f = foreign table, p = partitioned table, I = partitioned index |
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 |
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 or index has (or once had) any inheritance children or partitions |
relrowsecurity bool True if table has row-level security enabled; see pg_policy catalog |
relforcerowsecurity bool True if row-level security (when enabled) will also apply to table owner; see pg_policy catalog |
relispopulated bool True if relation is populated (this is true for all relations other than some materialized views) |
relreplident char Columns used to form “replica identity” for rows: d = default (primary key, if any), n = nothing, f = all columns, i = index with indisreplident set (same as nothing if the index used has been dropped) |
relispartition bool True if table or index is a partition |
relrewrite oid (references pg_class.oid) For new relations being written during a DDL operation that requires a table rewrite, this contains the OID of the original relation; otherwise zero. That state is only visible internally; this field should never contain anything other than zero for a user-visible relation. |
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_xact to be shrunk. Zero (InvalidTransactionId) if the relation is not a table. |
relminmxid xid All multixact IDs before this one have been replaced by a transaction ID in this table. This is used to track whether the table needs to be vacuumed in order to prevent multixact ID wraparound or to allow pg_multixact to be shrunk. Zero (InvalidMultiXactId) if the relation is not a table. |
relacl aclitem[] Access privileges; see Section 5.7 for details |
reloptions text[] Access-method-specific options, as “keyword=value” strings |
relpartbound pg_node_tree If table is a partition (see relispartition), internal representation of the partition bound |
pg_class 中的几个 Boolean 标志被被动维护:如果那是正确状态,则保证它们为真,但在条件不再为真时可能不会立即重置为假。例如, relhasindex 由 CREATE INDEX 设置,但 DROP INDEX 永远不会清除它。相反,如果 VACUUM 发现表没有任何索引,则它会清除 relhasindex 。此排序可避免竞争条件并改进并发性。
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.