Chapter 18. System Catalogs

Overview

The system catalogs are the place where a relational database management system stores schema metadata, such as information about tables and columns, and internal bookkeeping information. PostgreSQL's system catalogs are regular tables. You can drop and recreate these tables, add columns to them, and insert and update values. However, this can cause severe system damage and data loss. Normally one never has to change the system catalogs by hand; there are always SQL commands to do that. (For example, CREATE DATABASE both inserts a row into the pg_database catalog and creates the database on disk.) However, there are some exceptions for esoteric operations, such as adding index access methods.

Table 18-1. System Catalogs

Catalog NamePurpose
pg_aggregateaggregate functions
pg_amindex access methods
pg_amopaccess method operators
pg_amprocaccess method support procedures
pg_attrdefcolumn default values
pg_attributetable columns (attributes, fields)
pg_classtables, indexes, sequences ("relations")
pg_databasedatabases
pg_descriptiondescriptions or comments on database objects
pg_groupuser groups
pg_indexadditional index information
pg_inheritproc(not used)
pg_inheritstable inheritance hierarchy
pg_ipl(not used)
pg_languagelanguages for writing functions
pg_largeobjectlarge objects
pg_listenerasynchronous notification
pg_opclassindex access method operator classes
pg_operatoroperators
pg_procfunctions and procedures
pg_relcheckcheck constraints
pg_rewritequery rewriter rules
pg_shadowdatabase users
pg_statisticoptimizer statistics
pg_triggertriggers
pg_typedata types

More detailed documentation of most catalogs follow.

In the description of the catalog tables there are two fixed-length character types that are reserved for use only in the system catalog. These are the "char" and name types. The name type exists only for storage of internal catalog names and is not intended for use by the general user. Its length is currently defined as 32 bytes (31 characters plus terminator) but should be referenced using the macro NAMEDATALEN. The length is set at compile time (and is therefore adjustable for special uses); the default maximum length may change in a future release. The type "char" (note the quotes) is different from char(1) in that it uses only 1 byte of storage. It is internally used in the system catalogs as an enumeration type.

Table 18-2. Specialty Character Types

Type NameStorageDescription
"char"1 byteSingle-character internal type
name32 bytesThirty-one character internal type

pg_aggregate

pg_aggregate stores information about aggregate functions. An aggregate function is a function that operates on a set of values (typically one column from each the row that matches a query condition) and returns a single value computed from all these values. Typical aggregate functions are sum, count, and max.

Table 18-3. pg_aggregate Columns

NameTypeReferencesDescription
aggnamename Name of the aggregate function
aggownerint4pg_shadow.usesysidOwner (creator) of the aggregate function
aggtransfnregproc (function) Transition function
aggfinalfnregproc (function) Final function
aggbasetypeoidpg_type.oidThe type on which this function operates when invoked from SQL
aggtranstypeoidpg_type.oidThe type of the aggregate function's internal transition (state) data
aggfinaltypeoidpg_type.oidThe type of the result
agginitvaltext  The initial value of the transition state. This is a text field that will be cast to the type of aggtranstype.

New aggregate functions are registered with the CREATE AGGREGATE command.

An aggregate function is identified through name and argument type. Hence aggname and aggname are the composite primary key.

pg_attrdef

pg_attrdef stores column default values. The main information about columns is stored in pg_attribute (see below). Only columns that explicitly specify a default value (when the table is created or the column is added) have an entry here.

Table 18-4. pg_attrdef Columns

NameTypeReferencesDescription
adrelidoidpg_class.oidThe table to which this column belongs
adnumint2  The number of the column; see pg_attribute.pg_attnum
adbintext An internal representation of the column default value
adsrctext A human-readable representation of the default value

pg_attribute

pg_attribute stores information about table columns. There will be exactly one pg_attribute row for every column in every table in the database. (There will also be attribute entries for indexes and other objects. See pg_class.)

The term attribute is equivalent to column and is used for historical reasons.

Table 18-5. pg_attribute Columns

NameTypeReferencesDescription
attrelidoidpg_class.oidThe table to which this column belongs
attnamename Column name
atttypidoidpg_type.oidThe data type of this column
attdispersionfloat4  Statistical dispersion. See "Notes on pg_attribute columns", which follows this table.
attlenint2  This is a copy of the pg_type.typlen for this column's type.
attnumint2  The column number. See "Notes on pg_attribute columns", which follows this table.
attnelemsint4 Number of dimensions, if the column is an array
attcacheoffint4  Code offset. See "Notes on pg_attribute columns", which follows this table.
atttypmodint4  Type modifier. See "Notes on pg_attribute columns", which follows this table.
attbyvalbool  A copy of pg_type.typbyval of this column's type
attstoragechar  A copy of pg_type.typstorage of this column's type
attissetbool  This attribute is a set. See "Notes on pg_attribute columns", which follows this table.
attalignchar  A copy of pg_type.typalign of this column's type
attnotnullbool  This represents a NOT NULL constraint. It is possible to change this field to enable or disable the constraint.
atthasdefbool  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.

Notes on pg_attribute Columns

attdispersion

attdispersion is the dispersion statistic of the column (0.0 to 1.0), or zero if the statistic has not been calculated, or -1.0 if VACUUM found that the column contains no duplicate entries (in which case the dispersion should be taken as 1.0/numberOfRows for the current table size).

attnum

Ordinary columns are numbered from 1 up. System columns, such as oid, have (arbitrary) negative numbers.

attcacheoff

The code offset is always -1 in storage, but when loaded into a tuple descriptor in memory this may be updated cache the offset of the attribute within the tuple.

atttypmod

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 and output functions as the third argument. The value will generally be -1 for types that do not need typmod.

attiset

When true, attiset indicates that this attribute is a set. In that case, what is really stored in the attribute is the OID of a tuple in the pg_proc catalog. The pg_proc tuple contains the query string that defines this set (that is, the query to run to get the set). So the atttypid (see above) refers to the type returned by this query, but the actual length of this attribute is the length (size) of an oid.

pg_class

pg_class catalogs tables and mostly everything else that has columns or is otherwise similar to a table. This includes indexes, sequences, views, and some kinds of special relation kinds. Not all fields are meaningful for all relation types.

Table 18-6. pg_class Columns

NameTypeReferencesDescription
relnamename Name of the table, index, view, etc.
reltypeoidpg_type.oid The data type that corresponds to this table (not functional, only set for system tables)
relownerint4pg_shadow.usesysidOwner of the relation
relamoidpg_am.oidIf this is an index, the access method used (btree, hash, etc.)
relfilenodeoid Name of the on-disk file of this relation
relpagesint4  Size of the on-disk representation of this table in pages (size BLCKSZ). This is only an approximate value which is calculated during VACUUM.
reltuplesint4  Number of tuples in the table. This is only an estimate used by the planner, updated by VACUUM.
reltoastrelidoidpg_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.
reltoastidxidoidpg_class.oidOid of the index on the TOAST table for this table, 0 if none
relhasindexbool True if this is a table and it has at least one index
relissharedbool Reserved. (This is not what it may appear to be.)
relkindchar  Table type. Possible vales are: 'r' = ordinary table, 'i' = index, 'S' = sequence, 'v' = view, 's' = special, 't' = secondary TOAST table
relnattsint2  Number of columns in the relation, besides system columns. There must be this many corresponding entries in pg_attribute. See also pg_attribute.attnum.
relchecksint2  Number of check constraints on the table; see pg_relcheck catalog
reltriggersint2  Number of triggers on the table; see pg_trigger catalog
relukeysint2 Reserved. (This is not what it may appear to be.)
relfkeysint2 Number foreign keys on the table
relhaspkeybool  Reserved; currently unused.
relhasrulesbool Whether a table has rules.
relhassubclassbool True if at least one table inherits this one.
relaclaclitem[]  Access permissions. See the descriptions of GRANT and REVOKE for details.

pg_database

pg_database stores information about the available databases. The pg_database table is shared between all databases of a cluster.

Table 18-7. pg_database Columns

NameTypeReferencesDescription
datnamename Database name
datdbaint4pg_shadow.usesysidOwner of the database, initially set to whichever user created it.
encodingint4 Character/multibyte encoding for this database
datistemplatebool  If true, this database can be used in the "TEMPLATE" clause of CREATE DATABASE to create the new database as a clone of this one.
datallowconnbool  If false then no one can connect to this database. This is used to protect the template0 database from being altered.
datlastsysoidoid  Last oid in existence after the database was created; useful particularly to pg_dump
datpathtext  If the database is stored at an alternative location then this records the location. It is either an environment variable name or an absolute path, depending how it was entered.

pg_description

pg_description stores an optional description or comment for each database object. Descriptions can be manipulated with the COMMENT command. Client applications can view the descriptions by joining with this table. Many built-in system objects have comments associated with them that are shown by psql's \d commands.

Table 18-8. pg_description Columns

NameTypeReferencesDescription
objoidoidany oid attributeThe oid of the object this description pertains to
descriptiontext Arbitrary text that serves as the description of this object.

pg_group

pg_group defines groups and stores what users belong to what groups. Groups are created with the CREATE GROUP command.

Table 18-9. pg_group Columns

NameTypeReferencesDescription
gronamename Name of the group
grosysidint4 An arbitrary number to identify this group
grolistint4[]pg_shadow.usesysidAn array containing the ids of the users in this group

pg_index

pg_index contains part of the information about indexes. Other index information is in pg_class.

Table 18-10. pg_index Columns

NameTypeReferencesDescription
indexrelidoidpg_class.oidThe oid of the pg_class entry for this index
indrelidoidpg_class.oidThe oid of the pg_class entry of the table this index is for
indprocoidpg_proc.oidThe registered procedure, if this is a functional index.
indkeyint2vectorpg_attribute.attnum This is an vector (array) of up to INDEX_MAX_KEYS values that indicate which table columns this index pertains to. For example a value of 1 3 would mean that the first and the third column make up the index key.
indclassoidvectorpg_opclass.oid For each column in the index key this contains a reference to the "operator class" to use. See pg_opclass for details.
indisclusteredbool unused
indislossybool Reserved.
indhaskeytypebool The type of key is different from type of column. (obsolete)
indisuniquebool If true, this is a unique index.
indisprimarybool If true, this index is a unique index that represents the primary key of the table.
indreferenceoid unused
indpredtext Query plan for partial index predicate (not functional)

pg_inherits

pg_inherits records information about table inheritance hierarchies.

Table 18-11. pg_inherits Columns

NameTypeReferencesDescription
inhrelidoidpg_class.oid This is the reference to the subtable. Therefore the identified table is inherited from another table.
inhparentoidpg_class.oid This is the reference to the parent table, from which the table referenced by inhrelid is inherited.
inhseqnoint4  If there is more than one subtable/parent pair (multiple inheritance), this number tells the order in which the inherited columns are to be arranged. The count starts at 1.

pg_language

pg_language stores call interfaces or languages in which you can write functions or stored procedures. See under CREATE LANGUAGE for more information about language handlers.

Table 18-12. pg_language Columns

NameTypeReferencesDescription
lannamename Name of the language
lanisplbool  This is false for internal languages (such as SQL) and true for dynamically loaded language handler modules. It essentially means that, if it is true, the language may be dropped.
lanpltrustedbool  This is a trusted language. See under CREATE LANGUAGE what this means. If this is an internal language (lanispl is false) then this field is meaningless.
lanplcallfoidoidpg_proc.oid For non-internal languages this references the language handler, which is a special function that is responsible for executing all functions that are written in the particular language.
lancompilertext not useful

pg_operator

See CREATE OPERATOR for details on these operator parameters.

Table 18-13. pg_operator Columns

NameTypeReferencesDescription
oprnamename Name of the operator
oprownerint4pg_shadow.usesysidOwner (creator) of the operator
oprprecint2 unused
oprkindchar  'b' = infix ("both"), 'l' = prefix ("left"), 'r' = postfix ("right")
oprisleftbool unused
oprcanhashbool This operator supports hash joins.
oprleftoidpg_type.oidType of the left operand
oprrightoidpg_type.oidType of the right operand
oprresultoidpg_type.oidType of the result
oprcomoidpg_operator.oidCommutator of this operator, if any
oprnegateoidpg_operator.oidNegator of this operator, if any.
oprlsortopoidpg_operator.oid If this operator supports merge joins, it is set to the operator that sorts the type of the left-hand operand.
oprrsortopoidpg_operator.oid If this operator supports merge joins, the operator that sorts the type of the right-hand operand
oprcoderegproc Function that implements this operator
oprrestregproc Restriction selectivity estimation function for this operator
oprjoinregproc Join selectivity estimation function for this operator

pg_proc

pg_proc stores information about functions (or procedures). See CREATE FUNCTION for more information about the meaning of some fields.

Table 18-14. pg_proc Columns

NameTypeReferencesDescription
pronamename Name of the function
proownerint4pg_shadow.usesysidOwner (creator) of the function
prolangoidpg_language.oidImplementation language or call interface of this function
proisinhbool unused
proistrustedbool not functional
proiscachablebool Function returns same result for same input values
proisstrictbool  Function is strict. See "Notes on pg_proc columns", which follows this table.
pronargsint2 Number of arguments
proretsetbool Function returns a set (probably not functional)
prorettypeoidpg_type.oidData type of the return value (0 if the function does not return a value)
proargtypesoidvectorpg_type.oidA vector with the data types of the function arguments
probyte_pctint4 Reserved; unused.
properbyte_pct_cpuint4 Reserved; unused.
propercall_pct_cpuint4 Reserved; unused.
prooutin_ratioint4 Reserved; unused.
prosrctext  Source. See "Notes on pg_proc columns", which follows this table.
probinbytea Reserved; unused.

Notes on pg_proc Columns

proisstrict

If proisstrict is set, the function returns null if any call argument is null. In that case the function will not actually be called at all. Functions that are not "strict" must be prepared to handle null inputs.

prosrc

prosrc tells the function handler how to invoke the function. It might be the actual source code of the function for interpreted languages, a link symbol, a file name, or just about anything else, depending the implementation language/call convention.

pg_relcheck

pg_relcheck stores CHECK constraints on tables. (Column constraints are not treated specially. Every column constraint is equivalent to some table constraint.) See under CREATE TABLE for more information.

Table 18-15. pg_relcheck Columns

NameTypeReferencesDescription
rcrelidoidpg_class.oidThe table to which this check constraint applies
rcnamename Constraint name
rcbintext An internal representation of the constraint expression
rcsrctext A human-readable representation of the constraint expression

Note

pg_class.relchecks needs to match up with the entries in this table.

pg_shadow

pg_shadow stores information about database users. The name stems from the fact that this table should not be readable by the public since it contains passwords. It is similar to a shadow password file in UNIX. pg_user is a view on pg_shadow that blanks out the password field.

Table 18-16. pg_shadow Columns

NameTypeReferencesDescription
usenamename User name
usesysidint4 User id (arbitrary, unique number used to reference this user)
usecreatedbbool User may create databases
usetracebool unused
usesuperbool User is a superuser
usecatupdbool  User may update system catalogs. (Even a superuser may not do this unless this attribute is true.)
passwdtext Password
valuntilabstime Account expiry time (only used for password authentication)

pg_type

Table 18-17. pg_type Columns

NameTypeReferencesDescription
typnamename Data type name
typownerint4pg_shadow.usesysidOwner (creator) of the type
typlenint2 Length of the storage representation of the type, -1 if variable length
typprtlenint2 unused
typbyvalbool  Pass by value. See "Notes on pg_type columns", which follows this table.
typtypechar  Type of type. See "Notes on pg_type columns", which follows this table.
typisdefinedbool Reserved; unused.
typdelimchar Character that separates two values of this type when parsing array input
typrelidoidpg_class.oid Table described by this type. See "Notes on pg_type columns", which follows this table.
typelemoidpg_type.oid Type element. See "Notes on pg_type columns", which follows this table.
typinputregproc Input function
typoutputregproc Output function
typreceiveregproc unused
typsendregproc unused
typalignchar Alignment. See "Notes on pg_type columns", which follows this table.
typstoragechar  Toast storage strategy. See "Notes on pg_type columns", which follows this table.
typdefaulttext Reserved; unused.

Notes on pg_type Columns

typbyval

typbyval determines whether internal routines pass a value of this type by value or by reference. Only char, short, and int equivalent items can be passed by value. Variable-length types are always passed by reference. Note that typbyval can be false even if the length would allow pass-by-value; this is currently true for type float4, for example.

typtype

typtype is b for a basic type and c for a catalog type (that is, a table). If typtype is c, typrelid is the OID of the type's entry in pg_class.

typrelid

If this is a catalog type (see typtype, above), then the typrelid field points to the pg_class entry that defines the corresponding table. A table could theoretically be used as a composite data type, but this is not fully functional.

typelem

If typelem is not 0 then it identifies another row in pg_type. The current type can then be subscripted like an array yielding values of type typelem. A non-zero typelem does not guarantee this type to be a "real" array type; some ordinary fixed-length types can also be subscripted (for example, oidvector). Variable-length types can not be turned into pseudo-arrays like that. Hence, the way to determine whether a type is a "true" array type is typelem != 0 and typlen < 0.

typalign

typalign is the alignment required when storing a value of this type. It applies to storage on disk as well as most representations of the value inside Postgres. When multiple values are stored consecutively, such as in the representation of a complete row on disk, padding is inserted before a datum of this type so that it begins on the specified boundary. The alignment reference is the beginning of the first datum in the sequence.

  • 'c' = CHAR alignment, that is, no alignment needed.

  • 's' = SHORT alignment (2 bytes on most machines).

  • 'i' = INT alignment (4 bytes on most machines).

  • 'd' = DOUBLE alignment (8 bytes on many machines, but by no means all).

For types used in system tables, it is critical that the size and alignment defined in pg_type agree with the way that the compiler will lay out the field in a struct representing a table row.

typstorage

typstorage tells for variable-length types (those with typlen = -1) if the type is prepared for toasting and what the default strategy for attributes of this type should be. Possible values are

  • 'p': Value must always be stored plain.

  • 'e': Value can be stored in a "secondary" relation (if the relation has one). Refer to pg_class.reltoastrelid.

  • 'm': Value can be stored compressed inline.

  • 'x': Value can be stored compressed inline or in "secondary".

Note that 'm' fields can also be moved out to secondary storage, but only as a last resort ('e' and 'x' fields are moved first).