← Back to PostgreSQL, page 2

postgresql-0028 · Easy fresher

How do you document tables and columns with COMMENT ON and read the descriptions back?

COMMENT ON TABLE and COMMENT ON COLUMN store descriptions in the catalog, where psql's \d+ and most database tools show them. obj_description and col_description read them back in SQL. Keeping comments in migrations documents the schema where people look.

Full answer

Test data:

sql
CREATE TABLE subscriptions (
  id      int PRIMARY KEY,
  plan    text NOT NULL,
  mrr_inr numeric(10,2)
);
COMMENT ON TABLE subscriptions IS 'One row per active customer subscription';
COMMENT ON COLUMN subscriptions.mrr_inr IS 'Monthly recurring revenue in rupees, excluding GST';
sql
SELECT obj_description('subscriptions'::regclass, 'pg_class') AS table_comment;

Result:

text
              table_comment
------------------------------------------
 One row per active customer subscription
(1 row)
sql
SELECT column_name, data_type, is_nullable, col_description('subscriptions'::regclass, ordinal_position::int) AS description FROM information_schema.columns WHERE table_name = 'subscriptions' ORDER BY ordinal_position;

Output:

text
 column_name | data_type | is_nullable |                    description
-------------+-----------+-------------+----------------------------------------------------
 id          | integer   | NO          | NULL
 plan        | text      | NO          | NULL
 mrr_inr     | numeric   | YES         | Monthly recurring revenue in rupees, excluding GST
(3 rows)

The table comment comes back from obj_description. The second query is a portable 'describe table' built on information_schema, with col_description adding each column's comment; only mrr_inr has one, so the others show NULL. The unit and tax rule in that comment are exactly the details that otherwise live in someone's memory. In psql, \d+ subscriptions shows the same information interactively.

sql
CREATE TABLE subscriptions (
  id      int PRIMARY KEY,
  plan    text NOT NULL,
  mrr_inr numeric(10,2)
);
COMMENT ON TABLE subscriptions IS 'One row per active customer subscription';
COMMENT ON COLUMN subscriptions.mrr_inr IS 'Monthly recurring revenue in rupees, excluding GST';

SELECT obj_description('subscriptions'::regclass, 'pg_class') AS table_comment;

SELECT column_name, data_type, is_nullable, col_description('subscriptions'::regclass, ordinal_position::int) AS description FROM information_schema.columns WHERE table_name = 'subscriptions' ORDER BY ordinal_position;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • COMMENT ON stores descriptions
  • obj_description and col_description read them
  • information_schema describes columns
  • Tools and psql show comments
  • Document units and rules

Common mistakes

  • Keeping column meaning only in a wiki
  • Comments that drift from reality

Follow-ups

  • How would you check that every column has a comment?
  • What does regclass do?

Tips

  • Document units