System Catalogs
pg_attribute
Overview
The pg_attribute
stores information about table columns. It mimics the PostgreSQL system catalog pg_attribute.
Columns
This table is designed for compatibility with tools that require PostgreSQL system tables, so it mostly has dummy data. Please note that not all columns in
pg_attribute
are applicable to every type of relation.The following columns are available for querying in pg_attribute
:
Column | Type | Description |
---|---|---|
attrelid | int | This column represents the OID of the table (See pg_class ) |
attname | string | This column represents the column name as specified in CREATE TABLE |
atttypid | int | This column represents the OID of the column type (See pg_type ) |
attnum | int | This column represents the column index (1-based) |
attlen | int | This column represents the byte size of the value (-1 for varying length types) |
attnotnull | bool | This column represents the not-null constraint. true if the column was declared as NOT NULL |
attcacheoff | int | Unused. |
atttypmod | int | Unused. |
attndims | int | Unused. |
attbyval | bool | Unused. |
attalign | string | Unused. |
attstorage | string | Unused. |
attcompression | string | Unused. |
atthasdef | bool | Unused. |
atthasmissing | bool | Unused. |
attidentity | string | Unused. |
attgenerated | string | Unused. |
attisdropped | bool | Unused. |
attislocal | bool | Unused. |
attinhcount | int | Unused. |
attstattarget | int | Unused. |
attcollation | int | Unused. |
attacl | string | Unused. |
attoptions | string | Unused. |
attfdwoptions | string | Unused. |
attmissingval | string | Unused. |
Example
Case 1: Retrieving Column Information for All Tables
- This example queries the
pg_attribute
to retrieve information about all columns across all tables in the database.
- You will get the response below.
If your database has many tables, the result could be quite long. You can manage the output by filtering with specific criteria using
WHERE
clauses or by limiting the number of rows with LIMIT
clauses.Case 2: Filtering Columns
- Create a new table.
- To get the OID (Object Identifier) of the books table, run the
pg_class.oid
query.
Please refer to the pg_class page.
- It will return the books table with its OID.
- To filter columns, we utilize the
pg_attribute
withWHERE
clause.
- The output will show the columns of the books table that you’ve just created.
Case 3: Joining pg_attribute with pg_class for Table and Column Names
- In this example, a join operation with
pg_class
is performed to include the name of the table (relname
).
- You will receive both table and column details in a single query result.