Featured image of post Comment Your Schema

Comment Your Schema

pg_dump, mysqldump, DataGrip, DBeaver, SchemaSpy and tbls all read the same field in the database catalog: a free-text description of each table and column. In many production schemas that field is empty, so the tools print a name and a type, and the meaning of a status value stays in application code or in somebody's memory. What changes for everyone who reads the schema when the field is filled in, and what does filling it in cost on a table that is already large?

(updated )
TL;DR
Every major database engine lets you attach comments to tables and columns: descriptions that live in the schema itself and show up in every tool that reads it. They cost nothing to add, require no downtime, and make every schema dump, ER diagram, and generated doc more useful, and almost nobody uses them.

Here is a column as most tools will show it:

1
status TINYINT NOT NULL

The type says how the value is stored and the name says roughly what it is about. Neither says whether 1 is pending or active, or whether 0 is a real state at all. The mapping exists somewhere, usually as a constant in application code, and once more than one service reads the table it can exist in more than one version. Anyone who has not seen the table before finds it by reading that code or by asking, and pays the same cost again for every other ambiguous column. The database has a place for the mapping, though: the catalog, the set of system tables where it keeps its own description of the schema, has a text field for exactly this.

What schema comments are

Schema comments are metadata strings attached directly to database objects: tables, columns, indexes, views. They’re stored in the database catalog and exposed through standard metadata queries.

In PostgreSQL:

1
2
3
COMMENT ON TABLE orders IS 'Customer purchase orders. One row per checkout.';
COMMENT ON COLUMN orders.status IS '1=pending, 2=processing, 3=shipped, 4=delivered, 5=cancelled';
COMMENT ON COLUMN orders.end_date IS 'NULL means order is still in progress';

In MySQL:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
ALTER TABLE orders MODIFY COLUMN status TINYINT NOT NULL
  COMMENT '1=pending, 2=processing, 3=shipped, 4=delivered, 5=cancelled';

-- Or at table creation:
CREATE TABLE orders (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    user_id BIGINT NOT NULL COMMENT 'References users.id',
    status TINYINT NOT NULL COMMENT '1=pending, 2=processing, 3=shipped, 4=delivered, 5=cancelled',
    total_cents BIGINT NOT NULL COMMENT 'Order total in cents, not dollars',
    end_date DATE DEFAULT NULL COMMENT 'NULL = order still in progress'
) COMMENT='Customer purchase orders. One row per checkout.';

SQL Server uses extended properties, Oracle uses COMMENT ON. The syntax varies. The concept is universal.

Where comments show up

This is the part that makes comments more useful than a wiki page or a README. Because they live in the catalog, they reach every tool that reads schema metadata, with nothing to configure.

pg_dump and mysqldump include comments in the output, so anyone restoring a backup or reviewing a migration gets the context without looking elsewhere. DBeaver, DataGrip, pgAdmin and MySQL Workbench all render column comments in their schema viewers and diagrams: hover over a column and the description is right there. And any script or automation that queries metadata picks them up for free.

In PostgreSQL:

1
2
3
4
5
6
7
8
-- Get a table comment
SELECT obj_description('orders'::regclass, 'pg_class');

-- Get a column comment
SELECT col_description('orders'::regclass, 1);

-- Or just use psql
\d+ orders

In MySQL:

1
2
3
4
SELECT COLUMN_NAME, COLUMN_COMMENT
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = 'your_database'
  AND TABLE_NAME = 'orders';

Documentation generators work from the same queries. SchemaSpy is Java-based and produces an interactive HTML site with ER diagrams, relationships and the comments it finds in the catalog. tbls is a single Go binary that outputs Markdown, PlantUML or SVG and is designed to run in pipelines, which makes it the lowest-friction option for generated docs in CI: point it at the database, commit the Markdown, and every migration that adds or changes a comment updates the docs.

External documentation, by contrast, drifts the moment someone adds a column or changes a status code without updating the doc. Comments move with ALTER TABLE, show up in every dump, and can’t be in a different repo than the data they describe. You write them once, in one place.

Schema-reading assistants (text-to-SQL tools, coding agents with database access) start from the same catalog, so a status TINYINT with no comment is as ambiguous to them as it is to a new engineer, except that the model answers with a plausible query instead of a question. Tiger Data’s internal test of its semantic catalog reported “up to 27%” better SQL generation from adding descriptions, with results that varied a lot by schema. A comment is the one catalog field that can carry business meaning, and it travels with the column it describes.

What to comment

Not everything needs a comment. A column called created_at TIMESTAMP NOT NULL is self-documenting. The columns worth the effort are the ones where the schema doesn’t tell the whole story:

  • Status and type columns: what do the values mean? 1=active, 2=suspended, 3=closed.
  • Nullable columns where NULL has meaning: “not set,” “not applicable,” or “ongoing”?
  • ID columns that reference other tables without foreign keys: owner_id BIGINT COMMENT 'References users.id'.
  • Columns with non-obvious units: total_cents vs total (dollars? cents? units?), duration (seconds? milliseconds? minutes?).
  • Columns with business logic encoded in values: plan_type TINYINT COMMENT '1=free, 2=starter, 3=pro, 4=enterprise'.
  • Tables themselves: what does this table represent? One row per what?

A good table comment answers: “What is one row in this table?” A good column comment answers: “What does this value mean when I see it in a query result?”

Adding comments to an existing schema

This is the part that makes the cost-benefit ratio hard to argue against.

In PostgreSQL, COMMENT ON is a catalog-only operation. It takes a lock that does not block reads or writes, and it doesn’t rewrite data. On a table with 500 million rows, it completes in milliseconds.

1
2
-- Catalog-only: no rewrite, reads and writes carry on.
COMMENT ON COLUMN orders.status IS '1=pending, 2=processing, 3=shipped, 4=delivered, 5=cancelled';

In MySQL, ALTER TABLE ... MODIFY COLUMN with a comment is a metadata-only change in most cases with InnoDB online DDL (schema changes that run while the table stays readable and writable), but behavior depends on the version and what else is in the MODIFY. For comment-only changes on MySQL 8.0+, the ALGORITHM=INSTANT path applies, which changes the table’s metadata and leaves the rows alone. On older versions or when combined with type changes, it may trigger a table rebuild.

1
2
3
-- MySQL 8.0+: instant for comment-only changes
ALTER TABLE orders MODIFY COLUMN status TINYINT NOT NULL
  COMMENT '1=pending, 2=processing, 3=shipped, 4=delivered, 5=cancelled';
Zero-downtime operation
In PostgreSQL, COMMENT ON is a catalog-only update: no rewrite, a lock that does not block reads or writes, and it completes in milliseconds even on tables with hundreds of millions of rows. In MySQL 8.0+, comment-only changes go through the ALGORITHM=INSTANT path. This is about as safe as database changes get.

So there is little reason to treat this as a project. It can be done incrementally, a few columns every time a table is touched, and over time the schema becomes self-documenting.

Keeping comments true

The challenge isn’t the mechanism, it’s the habit. Comments rot like any other documentation if they aren’t maintained, and a few things help.

The first is commenting at creation time. If the comment is part of the CREATE TABLE or the migration, it happens naturally, and it changes the review as well. A migration that adds is_processed TINYINT(1) DEFAULT 0 looks fine syntactically; processed by what, when, and is it idempotent? With a comment in the same diff, the review stops being “does this look right?” and becomes “does this match what we agreed on?” If the team uses a migration tool, comment fields in the migration template make the absence visible. A simple CI check can flag tables or columns without comments, and it doesn’t have to block merges, because even a warning changes behavior over time.

Watch out
Stale comments are worse than no comments; they actively mislead. A column that says '1=active, 2=inactive' when the code now also uses 3=suspended will send someone down the wrong path. Treat comment updates as part of the migration, not a follow-up task.

That is the maintenance cost, and the mitigation is treating comments as schema: when a column’s semantics change, the comment changes in the same migration, same PR, same review. A check in CI is what keeps that true after the people who cared have moved on (see Schema Conventions Don’t Survive Without Automation).

Comments also aren’t a substitute for all documentation. They’re good at describing what a column or table is, not how a multi-table workflow operates, and data flow diagrams, service interaction maps and runbooks still belong somewhere else. For teams with hundreds of tables and thousands of columns, retrofitting is slow, an incremental habit that pays off over months.

The columns worth starting with are the ones that make people ask questions. The status column where 0/1/2 means something nobody can quote from memory. The nullable date that means “ongoing” in one place and “missing” in another. The foreign key with no foreign key.

SELECT insights FROM experience WHERE downtime = 0; -- Ruslan Tolkachev