Updated on 2024-09-30 GMT+08:00

PG_ATTRIBUTE

PG_ATTRIBUTE records information about table columns.

Table 1 PG_ATTRIBUTE columns

Name

Type

Description

attrelid

oid

Table to which the column belongs

attname

name

Column name

atttypid

oid

Column type

attstattarget

integer

Controls the level of details of statistics collected 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

smallint

Copy of pg_type.typlen of the column's type

attnum

smallint

Number of a column.

attndims

integer

Number of dimensions if the column is an array; otherwise, the value is 0.

attcacheoff

integer

This column is always -1 on disk. When it is loaded into a row descriptor in the memory, it may be updated to cache the offset of the columns in the row.

atttypmod

integer

Type-specific data supplied at table creation time (for example, the maximum length of a varchar column). This column is used as the third parameter when passing to type-specific input functions and length coercion functions. The value will generally be -1 for types that do not need ATTTYPMOD.

attbyval

boolean

Copy of pg_type.typbyval of the column's type

attstorage

"char"

Copy of pg_type.typstorage of this column's type

attalign

"char"

Copy of pg_type.typalign of the column's type

attnotnull

boolean

A not-null constraint. It is possible to change this column to enable or disable the constraint.

atthasdef

boolean

Indicates that this column has a default value, in which case there will be a corresponding entry in the pg_attrdef table that actually defines the value.

attisdropped

boolean

Whether the column has been dropped and is no longer valid. A dropped column is still physically present in the table but is ignored by the analyzer, so it cannot be accessed through SQL.

attislocal

boolean

Whether the column is defined locally in the relation. Note that a column can be locally defined and inherited simultaneously.

attcmprmode

tinyint

Compressed modes for a specific column The compressed mode includes:

  • ATT_CMPR_NOCOMPRESS
  • ATT_CMPR_DELTA
  • ATT_CMPR_DICTIONARY
  • ATT_CMPR_PREFIX
  • ATT_CMPR_NUMSTR

attinhcount

integer

Number of direct ancestors this column has. A column with an ancestor cannot be dropped nor renamed.

attcollation

oid

Defined collation of a column

attacl

aclitem[]

Permissions for column-level access

attoptions

text[]

Property-level options

attfdwoptions

text[]

Property-level external data options

attinitdefval

bytea

attinitdefval stores the default value expression. ADD COLUMN in a row-store table must use this column.

attkvtype

tinyint

kv_type attribute of a column. Values:

  • 0 indicates the default value, which is used for non-time series tables.
  • 1 indicates TSTAG, a dimension attribute, which is used only for time series tables.
  • 2 indicates TSFIELD, a metric attribute, which is used only for time series tables.
  • 3 indicates TSTIME, a time attribute, which is used only for time series tables.

Example

Query the field names and field IDs of a specified table. Replace t1 and public with the actual table name and schema name, respectively.

1
2
3
4
5
6
7
SELECT attname,attnum FROM pg_attribute WHERE attrelid=(SELECT pg_class.oid FROM pg_class JOIN pg_namespace ON relnamespace=pg_namespace.oid WHERE relname='t1' and nspname='public') and attnum>0; 
     attname      | attnum
------------------+--------
 product_id       |      1
 product_name     |      2
 product_quantity |      3
(3 rows)