Updated on 2025-08-25 GMT+08:00

PG_ATTRIBUTE

PG_ATTRIBUTE stores information about table columns.

Table 1 PG_ATTRIBUTE columns

Column

Type

Description

attrelid

oid

Table the column belongs to.

attname

name

Column name.

atttypid

oid

Column type.

attstattarget

integer

Level of statistical details set by ANALYZE for the column.

  • Zero means no statistics are collected.
  • Negative values indicate using the system's default statistical object.
  • Positive values' exact meanings depend on the data type.

For scalar data types, attstattarget is both the target number of most common values to collect and the target number of histograms to generate.

attlen

smallint

Copy of the pg_type.typlen of this column's type.

attnum

smallint

Column number.

attndims

integer

For arrays, this value denotes the dimensionality. Otherwise, it is 0.

attcacheoff

integer

Always -1 on disk, but might update to the cached column offset within a row when loaded into memory's row descriptor.

atttypmod

integer

Type-specific data supported during table creation (for example, maximum length of varchar columns). Passed as the third parameter to type-related input and length coercion functions. Typically -1 for types not requiring atttypmod.

attbyval

Boolean

Copy of the pg_type.typbyval of this column's type.

attstorage

"char"

Copy of pg_type.typstorage of this column's type.

attalign

"char"

Copy of pg_type.typalign of this column's type.

attnotnull

Boolean

A NOT NULL constraint. You can modify this column to enable or disable the constraint.

atthasdef

Boolean

Whether the column has a default value, which corresponds to the record in the PG_ATTRDEF catalog that actually defines the value.

attisdropped

Boolean

Whether the column has been dropped and is invalid. If it has been dropped, it still exists in the table but is ignored by the analyzer, making it inaccessible through SQL.

attislocal

Boolean

Whether the column is locally defined within the object. A column can be both locally defined and inherited.

attcmprmode

tinyint

Compression method for a column. The operations are:

  • ATT_CMPR_NOCOMPRESS
  • ATT_CMPR_DELTA
  • ATT_CMPR_DICTIONARY
  • ATT_CMPR_PREFIX
  • ATT_CMPR_NUMSTR

attinhcount

integer

Count of immediate parent tables owning the column. If the number of parent tables owning a column is not zero, the column cannot be deleted or renamed.

attcollation

oid

Collation sequence defined for this column.

attacl

aclitem[]

Column-level access permission control.

attoptions

text[]

Attribute-level options.

attfdwoptions

text[]

Foreign-data wrapper property-level options.

attinitdefval

bytea

Stores the default value expression for this column. Used by ADD COLUMN in row-store tables.

attkvtype

tinyint

KV type of the column. Options:

  • 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.

Examples

Query column names and IDs of a specified table. Replace t1 and public with actual table and schema names, 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)