2.4 DDL Modifies the Catalog

Structure-changing commands such as CREATE TABLE, ALTER TABLE, and DROP TABLE are called DDL (Data Definition Language). What does the database do when a DDL statement runs? The first thing it does is modify the catalog.

When CREATE TABLE customer ... runs, a table named customer is added to the catalog, along with records (rows) representing the columns and types the user defined. No data pages are created yet, but the catalog now holds information about the table. Pages are created one at a time when data is created.

But what about changing the structure of a table that already has records? Suppose we add a column called "grade" to a customer table holding 100 million rows. There are two approaches.

  • Modify only the catalog: Record in the catalog that "this table now has a grade column; rows stored earlier lack this column, so treat it as empty." No data page is read or modified, so it finishes instantly even on a 100-million-row table.
  • Rewrite every row in every page: Store all 100 million rows again in the new row format. All pages must be read and rewritten, so it takes a long time and the table may be locked in the meantime.

Which approach is used depends on the change. Adding a column that may be empty with no default is often a catalog-only change, and recent database versions optimize to change only the catalog wherever possible. In contrast, a change that requires rewriting the record values themselves, such as changing an existing column's type, requires reloading every row. This is why some ALTER TABLE statements take a long time and others complete immediately.