ALTER TABLE with MariaDB Xpand
This page is part of MariaDB's Documentation.
The parent of this page is: Table DDL for MariaDB Xpand
Topics on this page:
Overview
MariaDB Xpand supports the ALTER TABLE statement to alter, change, or modify tables:
Xpand accepts table options in
ALTER TABLEstatements to alter table characteristicsXpand avoids locking for
ALTER TABLEstatementsXpand allows concurrent read and write queries during
ALTER TABLEstatements
Compatibility
MariaDB Xpand 5.3
MariaDB Xpand 6.0
MariaDB Xpand 6.1
Index-Related Operations
ALTER TABLE .. ADD COLUMNAR INDEX
With MariaDB Xpand, the ALTER TABLE statement can be used to create a new Columnar index on an existing table using the ADD COLUMNAR INDEX clause:
ALTER TABLE hq_sales.invoices
ADD COLUMNAR INDEX idx_invoices_date_total (invoice_date, invoice_total);
For best performance, MariaDB recommends loading data into the table prior to adding a Columnar index.
ALTER TABLE .. ADD INDEX
With MariaDB Xpand, the ALTER TABLE statement can be used to create a new secondary index on an existing table using the ADD INDEX clause:
ALTER TABLE hq_sales.invoices
ADD INDEX idx_invoices_date_total (invoice_date, invoice_total);
ALTER TABLE .. ADD UNIQUE INDEX
With MariaDB Xpand, the ALTER TABLE statement can be used to create a new unique index on an existing table using the ADD UNIQUE INDEX clause:
ALTER TABLE hq_sales.invoices
ADD UNIQUE INDEX idx_invoices_date_total (invoice_date, invoice_total);
ALTER TABLE .. ALTER INDEX
With MariaDB Xpand, the ALTER TABLE statement can be used to alter an existing index using the ALTER INDEX clause:
ALTER TABLE hq_sales.invoices
ALTER COLUMNAR INDEX idx_invoices_date_total (invoice_date, invoice_total)
INVISIBLE;
ALTER TABLE .. DROP INDEX
With MariaDB Xpand, the ALTER TABLE statement can be used to drop an index from an existing table using the DROP INDEX clause:
ALTER TABLE hq_sales.invoices
DROP INDEX idx_invoices_date_total;
ALTER TABLE .. RENAME INDEX
With MariaDB Xpand, the ALTER TABLE statement can be used to rename an index using the RENAME INDEX clause:
ALTER TABLE hq_sales.invoices
RENAME INDEX idx_invoices_date_total
TO idx_invoices_date_total_old;
Primary Key-Related Operations
ALTER TABLE .. ADD PRIMARY KEY
With MariaDB Xpand, the ALTER TABLE statement can be used to create a primary key on an existing table using the ADD PRIMARY KEY clause:
ALTER TABLE hq_sales.invoices
ADD PRIMARY KEY (invoice_id);
ALTER TABLE .. DROP PRIMARY KEY
With MariaDB Xpand, the ALTER TABLE statement can be used to drop a primary key from an existing table using the DROP PRIMARY KEY clause:
ALTER TABLE hq_sales.invoices
DROP PRIMARY KEY, LOCK=SHARED;
Locking for the operation can be configured using the LOCK clause.
ALTER TABLE .. PRIMARY KEY
With MariaDB Xpand, the ALTER TABLE statement can be used to alter a primary key on an existing table using the PRIMARY KEY clause:
ALTER TABLE hq_sales.invoices
PRIMARY KEY DISTRIBUTE=3;
Replace a Primary Key
With MariaDB Xpand, the ALTER TABLE statement can be used to replace a primary key in an existing table by combining the DROP PRIMARY KEY and ADD PRIMARY KEY clauses:
ALTER TABLE hq_sales.invoices
DROP PRIMARY KEY,
ADD PRIMARY KEY (invoice_id, branch_id);
Foreign Key-Related Operations
ALTER TABLE .. ADD CONSTRAINT .. FOREIGN KEY
With MariaDB Xpand, the ALTER TABLE statement can be used to add a foreign key using the ADD FOREIGN KEY clause with an optional constraint name:
ALTER TABLE hq_sales.invoices
ADD CONSTRAINT fk_invoices_customers
FOREIGN KEY (customer_id)
REFERENCES hq_sales.customers (customer_id)
ON DELETE RESTRICT
ON UPDATE RESTRICT;
ALTER TABLE .. DROP FOREIGN KEY
With MariaDB Xpand, the ALTER TABLE statement can be used to drop a foreign key using the DROP FOREIGN KEY clause:
ALTER TABLE hq_sales.invoices
DROP FOREIGN KEY fk_invoices_customers;
Table Options
With MariaDB Xpand, the ALTER TABLE statement can be used to set table options:
ALTER TABLE hq_sales.invoices
REPLICAS=3;
Locking
MariaDB Xpand uses Multi-Version Concurrency Control (MVCC) to avoid locking for ALTER TABLE statements.
For additional information, see "MVCC (Multi-Version Concurrency Control) with MariaDB Xpand" and "Locking with MariaDB Xpand".
Online Schema Changes
MariaDB Xpand supports online schema changes to allow concurrent read and write queries to access the table while the ALTER TABLE statement is executed:
Xpand maintains read consistency during the execution of
ALTER TABLEstatementsWhen read and write queries access the table before the
ALTER TABLEcompletes, the queries see the original table schemaWhen reads and write queries access the table after the
ALTER TABLEstatement completes, the queries see the new table schema
For additional information, see "Online Schema Changes with MariaDB Xpand".
Best Practices
Although MariaDB Xpand is designed to perform online schema changes, MariaDB recommends following some best practices to minimize impact to your application:
Since
ALTER TABLEstatements can have long execution times, MariaDB recommends using a client in ascreensession or other detachable terminalTest the new schema with your application's queries in a non-production environment before making the change in production
Compare the
EXPLAINoutput before and after the schema changeEnsure there is sufficient storage available before starting the
ALTER TABLEoperationEnsure there are no replication concerns before starting the
ALTER TABLEoperation
Estimate ALTER TABLE Storage Requirements
When MariaDB Xpand executes an ALTER TABLE statement, it requires storage space that depends on many factors, which are described in the sections below.
If your service requires additional storage prior to performing an ALTER TABLE statement, see "Allocate Disk Space for Fault Tolerance and Availability with MariaDB Xpand".
Storage for Full Table Copy
When MariaDB Xpand executes an ALTER TABLE statement, it should have sufficient storage to create a full copy of the table, including replicas.
The size of a table can be estimated from system.table_sizes:
SELECT * FROM system.table_sizes;
Undo Log Growth
When MariaDB Xpand executes an ALTER TABLE statement, it should have sufficient storage to allow the Undo Log to grow due to concurrent queries:
Xpand pauses Undo Log trim until the
ALTER TABLEstatement completesThe required space depends on the current Undo Log size and expected growth from concurrent
INSERT,UPDATE, andDELETEqueries
Binary Log Growth
When MariaDB Xpand executes an ALTER TABLE statement, it should have sufficient storage to allow the binary logs to grow from concurrent queries:
Xpand does not pause binlog trim during the
ALTER TABLEstatement, but Xpand does not free the corresponding storage until Undo Log trim resumes after theALTER TABLEoperation completes
Minimum Available Space
When MariaDB Xpand executes an ALTER TABLE statement, it should generally have a minimum of 10% free storage after accounting for all of the above factors:
Xpand requires at least 5% of free storage, because Xpand will kill long-running transactions (including the
ALTER TABLEoperation) if free storage falls below that
Interactions with Replication
ALTER TABLE statements with MariaDB Xpand can interact with replication.
Sequential Execution with Inbound Replication
When MariaDB Xpand is configured for inbound replication, replicated ALTER TABLE statements are executed sequentially within the replication stream:
Replicated writes are paused until the replicated replicated
ALTER TABLEstatement completesIf the
ALTER TABLEstatement takes a long time, slave lag can occurBefore performing large
ALTER TABLEoperations over inbound replication, check if your cluster's binary log retention is sufficient to save the binary logs until replication has caught up
Row-Based Replication with Inbound Replication
When MariaDB Xpand is configured for inbound replication with row-based replication (RBR), the table columns in the binary log must match the table columns in the table's schema in Xpand:
When a table's schema changes, but Xpand tries to apply binary log events that reference the old schema, replication errors can occur
If a schema change operation will modify a column when inbound row-based replication is configured, the schema change operation can be executed in the replication stream to force sequential execution with replicated writes
The following schema change operations do not impact row-based replication:
Adding indexes
Changing the storage type
Changing the number of slices
Monitor ALTER TABLE Progress
When MariaDB Xpand executes an ALTER TABLE statement, the progress of the operation can be monitored by querying the system.alter_progress system table:
SELECT * FROM system.alter_progress;
Special Cases
Xpand does not accept the
CHECKconstraint syntax inALTER TABLEXpand does not support
ALTER TABLE .. CONVERTXpand does not support dropping primary keys using
ALTER TABLE .. DROP INDEX PRIMARY, but Xpand does support the operation usingALTER TABLE .. DROP PRIMARY KEY, LOCK=SHAREDXpand does not support the
%wildcard in database names
