col_description
PG 7.2+→ textReturns the comment stored for a table column. Column comments are set with COMMENT ON COLUMN. The column is identified by its attribute number (attnum), not its name.
Signature
col_description ( table oid, column integer ) → textParameters
| Parameter | Type | Description |
|---|---|---|
| table | oid | OID of the table |
| column | integer | Attribute number of the column (pg_attribute.attnum) |
Examples
Comment on first column of orders
SELECT col_description('orders'::regclass, 1);Primary key identifierAll column comments for customers table
SELECT attname, col_description('customers'::regclass, attnum) FROM pg_attribute WHERE attrelid='customers'::regclass AND attnum > 0 AND NOT attisdropped;(rows)Set then retrieve column comment
COMMENT ON COLUMN orders.amount IS 'Order total in EUR'; SELECT col_description('orders'::regclass, attnum) FROM pg_attribute WHERE attrelid='orders'::regclass AND attname='amount';Order total in EURColumn comments via information_schema join
SELECT column_name, col_description(table_oid::regclass, ordinal_position) FROM information_schema.columns WHERE table_name = 'products';(rows)Full column documentation for API reference
SELECT attname, col_description(attrelid, attnum) AS doc FROM pg_attribute WHERE attrelid = 'api_keys'::regclass AND attnum > 0 AND NOT attisdropped ORDER BY attnum;(rows)col_description() requires the attribute number (attnum), not the column name. Using the wrong number (e.g. 0 or a name string) always returns NULL.
✓ Instead: Join with pg_attribute to get attnum by name: SELECT col_description('t'::regclass, attnum) FROM pg_attribute WHERE attrelid='t'::regclass AND attname='mycol';
Store column descriptions as COMMENT ON COLUMN and use col_description() to generate OpenAPI schema descriptions directly from PostgreSQL — one source of truth for field documentation.
SELECT json_build_object(attname, json_build_object('description', col_description(attrelid, attnum), 'type', format_type(atttypid,atttypmod))) FROM pg_attribute WHERE attrelid='users'::regclass AND attnum>0 AND NOT attisdropped;JSON object suitable for OpenAPI schema generation