Q: How do you partition a 500GB active production table in MariaDB without service downtime?
MariaDB interview questions on partitioning a 500GB table: what ALTER TABLE does online, key rules, pt-online-schema-change, lock timeouts, and Galera TOI.
Want to master this scenario in a live sandbox? KodeKloud's PostgreSQL Database Administration & High Availability Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: This comes up in mariadb interview questions because the naive answer, run ALTER TABLE ... PARTITION BY, rewrites 500GB under pressure and can block writes. I answer in layers: what MariaDB can do online natively, what it cannot, how an online schema change tool fills the gap, and what extra danger Galera adds. The goal is a plan where the cutover takes seconds, the migration can be aborted at any point, and no long metadata lock queues up behind a stuck transaction.
Check the partition key rule before anything else
Every unique key, including the primary key, must contain all columns used in the partitioning expression. A table with PRIMARY KEY (id) cannot be partitioned by created_at until the key becomes (id, created_at). That is a real schema decision with consequences: foreign keys are not supported on partitioned InnoDB tables, and uniqueness of id alone is no longer enforced by the database. I confirm with the application owners first.
SHOW CREATE TABLE orders\G
-- PRIMARY KEY (`id`),
-- UNIQUE KEY `uq_order_ref` (`order_ref`)
-- Both keys must include created_at to partition by it:
-- PRIMARY KEY (`id`,`created_at`),
-- UNIQUE KEY `uq_order_ref` (`order_ref`,`created_at`)
SELECT TABLE_NAME, ROUND((DATA_LENGTH+INDEX_LENGTH)/1024/1024/1024,1) AS gb
FROM information_schema.TABLES WHERE TABLE_NAME='orders';
Know what native ALTER can and cannot do online
Converting a non-partitioned table to a partitioned one rebuilds the whole table. I do not rely on ALGORITHM=INPLACE, LOCK=NONE for that step, and I state the options explicitly so the server errors out instead of silently choosing a blocking copy. Maintenance on an already partitioned table is a different story: adding a partition to the end of a RANGE layout or reorganising an empty catch-all partition is cheap.
-- Ask for the non-blocking behaviour explicitly; the server errors if it cannot honour it
ALTER TABLE orders
DROP PRIMARY KEY, ADD PRIMARY KEY (id, created_at),
PARTITION BY RANGE (TO_DAYS(created_at)) (
PARTITION p202610 VALUES LESS THAN (TO_DAYS('2026-11-01')),
PARTITION p202611 VALUES LESS THAN (TO_DAYS('2026-12-01')),
PARTITION pmax VALUES LESS THAN MAXVALUE
), ALGORITHM=INPLACE, LOCK=NONE;
-- Expect an error that INPLACE is not supported for this operation,
-- so the 500GB table is not rebuilt under a write-blocking lock.
-- Later maintenance on the partitioned table
ALTER TABLE orders REORGANIZE PARTITION pmax INTO (
PARTITION p202612 VALUES LESS THAN (TO_DAYS('2027-01-01')),
PARTITION pmax VALUES LESS THAN MAXVALUE);
Use an online schema change tool for the conversion
pt-online-schema-change builds an empty shadow table with the new definition, adds triggers to mirror writes, copies rows in chunks, and swaps names at the end. It throttles on replica lag and server load. I always run --dry-run first. gh-ost is another option, but it is built and tested around MySQL binlogs, so I would test it on MariaDB in staging before relying on it. Both need free disk roughly equal to the table size.
pt-online-schema-change \
--alter "DROP PRIMARY KEY, ADD PRIMARY KEY (id, created_at), PARTITION BY RANGE (TO_DAYS(created_at)) (PARTITION p202610 VALUES LESS THAN (TO_DAYS('2026-11-01')), PARTITION p202611 VALUES LESS THAN (TO_DAYS('2026-12-01')), PARTITION pmax VALUES LESS THAN MAXVALUE)" \
D=shop,t=orders,h=10.0.1.11,u=osc --ask-pass \
--chunk-time 0.5 --max-lag 5 --critical-load Threads_running=100 \
--set-vars lock_wait_timeout=5 --dry-run
# Operation, tries, wait: ... Dry run complete. `shop`.`orders` was not altered.
# Re-run with --execute when the dry run is clean
Protect the cutover from metadata lock pile-ups
The final rename needs an exclusive metadata lock. If a long transaction holds a lock on the table, the rename waits and every new query queues behind it, which looks like an outage. I set a short lock_wait_timeout so the tool gives up and retries, and I check for long transactions before cutover. MariaDB ships a metadata_lock_info plugin that shows who holds and waits for locks.
SET SESSION lock_wait_timeout = 5;
INSTALL SONAME 'metadata_lock_info';
SELECT THREAD_ID, LOCK_MODE, LOCK_TYPE, TABLE_SCHEMA, TABLE_NAME
FROM information_schema.METADATA_LOCK_INFO WHERE TABLE_NAME='orders';
SELECT trx_id, trx_started, trx_mysql_thread_id
FROM information_schema.INNODB_TRX ORDER BY trx_started LIMIT 5;
Galera caveat: TOI versus RSU
On Galera, the default wsrep_OSC_method=TOI runs DDL in total order across all nodes at once, so a long ALTER can stall the whole cluster. RSU applies the change on one desynchronised node at a time, but it is not replicated and the schema must stay compatible with the incoming write sets, which a primary key change and partitioning usually are not. That is why I run the shadow-table tool, whose short DDL steps are fine under TOI, and I watch flow control.
SHOW VARIABLES LIKE 'wsrep_OSC_method';
-- wsrep_OSC_method TOI
-- During the migration, watch flow control and queue size
SHOW GLOBAL STATUS WHERE Variable_name IN ('wsrep_flow_control_paused','wsrep_local_recv_queue_avg');
-- Only for compatible, tested changes, one node at a time
SET SESSION wsrep_OSC_method = 'RSU';
- The partitioning column must be part of every unique key, so the primary key usually has to change first.
- Converting a big table to a partitioned one is a full rebuild, so I ask for ALGORITHM and LOCK options explicitly and use an online schema change tool.
- pt-online-schema-change copies in chunks, throttles on lag, and swaps tables with a short lock_wait_timeout so a stuck transaction cannot queue the application.
- On Galera, TOI blocks the cluster during DDL and RSU needs compatible schemas, so the tool-driven approach is safer.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Running ALTER TABLE PARTITION BY directly on the 500GB table The table is rebuilt with a lock level you did not choose, which can block writes for hours and fill the disk.
- Skipping the lock_wait_timeout setting A long-running transaction makes the cutover wait for a metadata lock and every later query queues behind it.
- Forgetting that foreign keys are unsupported on partitioned tables The migration fails late or forces dropping constraints that the application relied on.
- Using RSU on Galera for a primary key and partition change The node applies a different structure than the replicated write sets expect and can fail or diverge.