MariaDB’s online DDL (Data Definition Language) operations are a game-changer, but they don’t magically make schema changes risk-free; the real magic is in understanding how they work and when they’re appropriate.

Let’s see a typical scenario. Imagine you have a products table and you need to add a new last_updated_by column.

-- Initial table structure
CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    price DECIMAL(10, 2) NOT NULL
);

-- Data insertion
INSERT INTO products (name, price) VALUES ('Gadget', 19.99), ('Widget', 29.99);

Now, to add the column online:

-- Adding a new column online
ALTER TABLE products
ADD COLUMN last_updated_by VARCHAR(100) NULL;

When this command runs, MariaDB doesn’t immediately lock the entire table for the duration. Instead, it employs a sophisticated multi-step process. For simple ADD COLUMN operations like this, it often uses a "copy-and-swap" method in the background, but for more complex changes, it might use a "direct" method. The key is that it creates a new version of the table with the added column, populates it, and then atomically swaps the old table with the new one. During this process, reads and writes to the original table are generally allowed, though there might be brief metadata locks.

The problem MariaDB’s online DDL solves is the traditional downtime required for schema modifications. Before online DDL, an ALTER TABLE statement would often acquire an exclusive lock on the table, blocking all reads and writes until the operation completed. For large tables, this could mean hours of unavailability, which is unacceptable for most modern applications. Online DDL allows these changes to occur with minimal or zero impact on application availability.

Internally, MariaDB manages this by creating a temporary table with the new schema, copying data from the original table to the temporary one, and then replacing the original table with the temporary one. For certain operations, like adding a column with a default value or altering a column’s type, it might perform the change in-place or use more optimized methods. The specific mechanism depends on the ALTER TABLE command’s complexity and the innodb_online_alter_log_dir setting (though this is more for replication-related aspects of online DDL).

The levers you control are primarily the ALTER TABLE statements themselves and the configuration of your MariaDB instance. Key settings that influence online DDL include:

  • innodb_online_alter_log_policy: This can be set to O (optimize) or R (rebuild). O attempts to perform online operations where possible, while R forces a table rebuild for many operations.
  • alter_table_online_methods: This system variable specifies which online DDL methods are allowed. You can see available methods with SHOW VARIABLES LIKE 'alter_table_online_methods';. For example, alter_table_online_methods=INSTANT,COPY. INSTANT operations are the fastest, often just metadata changes. COPY involves creating a new table and copying data.
  • slave_parallel_workers and slave_parallel_type: If you’re using replication, these settings can impact how quickly the schema change is propagated to replicas, indirectly affecting perceived availability.

The one thing that trips many people up is assuming all ALTER TABLE statements are truly "zero-downtime." While many are, complex operations or specific configurations can still lead to brief periods of write unavailability or significant performance degradation. For instance, changing a column’s data type in a way that requires a full table rewrite, or operations involving foreign key constraints, can be resource-intensive and might still cause contention. Always test your ALTER TABLE statements on a staging environment that mirrors your production workload and data size.

The next concept to explore is how to monitor and troubleshoot these online DDL operations when they don’t go as planned.

Want structured learning?

Take the full Mariadb course →