[MDEV-11424] Instant ALTER TABLE of failure-free record format changes Created: 2016-11-30 Updated: 2023-12-19 Resolved: 2019-02-20 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Data Definition - Alter Table, Storage Engine - InnoDB |
| Fix Version/s: | 10.4.3 |
| Type: | Task | Priority: | Critical |
| Reporter: | Marko Mäkelä | Assignee: | Marko Mäkelä |
| Resolution: | Fixed | Votes: | 7 |
| Labels: | None | ||
| Issue Links: |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sprint: | 10.4.0-1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Description |
|
This is an umbrella task for allowing ALTER TABLE to be instantaneous in cases that cannot fail due to existing records being incompatible with the altered table definition. Later, MDEV-16356 and MDEV-16291 could extend this to support ALGORITHM=NOCOPY for operations that can avoid rebuilding tables, but need to validate the data. IntroductionTraditionally, ALTER TABLE would be roughly equivalent to the following SQL statements:
This mode of operation is still available by specifying ALGORITHM=COPY or SET old_alter_table=1 (or starting with Copying a table and rebuilding all its indexes can be a very expensive operation. While InnoDB mostly allows tables to be rebuilt online since MariaDB Server version 10.0, the temporary files can occupy a significant amount of space and I/O capacity. There are cases where the data format is not affected by the ALTER TABLE operation; only metadata needs to be updated. Examples include renaming columns, changing the default values of columns, and changing the maximum length of a VARCHAR column such that the storage format does not change. A goal for MariaDB Server is to allow instantaneous execution of any ALTER TABLE operation where data conversions cannot fail, and indexes do not need to be rebuilt. Even in cases where some affected indexes have to be rebuilt, it will be more efficient to only rebuild some indexes than to copy the whole table. The goal can be reformulated as: Avoid rebuilding the table. How to avoid rebuilding the table, if the underlying storage format would be affected by the ALTER TABLE operation? By extending the storage format in a way that allows the data to be in ‘non-canonical’ format. The main examples of this are The original InnoDB storage format (retroactively named ROW_FORMAT=REDUNDANT) is very generic, basically allowing NULL values and arbitrary length for every column. For it, Note: Whenever the PRIMARY KEY is changed, all indexes will have to be rebuilt. Likewise, some operations on indexed columns may require the indexes to be rebuilt. The space-optimized row formats COMPACT and DYNAMIC omit ‘is null’ flags for NOT NULL columns and length information for fixed-length columns. MDEV-17520 could extend Operations that involve adding or dropping indexes (also DROP COLUMN can imply this) will not be supported for ALGORITHM=INSTANT; they will be supported with ALGORITHM=NOCOPY. ALTER TABLE…ADD [UNIQUE] INDEX supports concurrent modifications to the table since MariaDB 10.0. Operations that will continue to be refused by ALGORITHM=INSTANT (and ALGORITHM=NOCOPY even after MDEV-16291) include:
Any ALTER TABLE that would be refused with ALGORITHM=NOCOPY (anything that rebuilds the clustered index) will drop any ‘instant ALTER TABLE’ metadata. The metadata would also be deleted if a rebuild is explicitly requested by the use of the FORCE keyword. Metadata format changesIn InnoDB, instant ALTER TABLE affects clustered index page leaf records only. The data dictionary will reflect the most recent table definition. Additional metadata for interpreting records that correspond to an earlier version of the table definition will be stored in the clustered index tree as follows.
Data format changesUser records in the clustered index leaf pages will have to indicate which format they correspond to.
A note on MVCCBecause ha_innobase::commit_inplace_alter_table() will be invoked while holding MDL_EXCLUSIVE, any transactions that read or modified the table must finish before the ALTER TABLE can commit. But it is possible that some old transaction tries to do its first access to the table after the ALTER TABLE committed. Such transactions may receive an error message 'table definition changed', as noted in MySQL Bug#28432. It would be too much effort to support MVCC if a transaction after ALTER modified a record (converting it to newer dictionary version) that would otherwise be visible to the old transaction.
For simplicity and consistency, we could always return an error to the SELECT statements (after any ALTER TABLE). ALTER TABLE operations that potentially affect the format of a rowIn MariaDB Server 10.2, the following alter_table_operations might require a table to be rebuilt:
Legend:
|
| Comments |
| Comment by Marko Mäkelä [ 2017-10-15 ] |
|
I filed the following prerequisite tasks: This task would address the remaining ALTER TABLE operations that currently require the table to be rebuilt. The solution would involve storing a format identifier in each record and storing multiple versions of the table definition by somehow extending the hidden ‘default row’ record that was introduced in Secondary indexes of full columns (not column prefixes) in ROW_FORMAT=COMPACT or ROW_FORMAT=DYNAMIC will have to be rebuilt when the indexed columns are modified as follows:
Lifting these restrictions on secondary indexes would require a change to the secondary index format and a rewrite of the InnoDB change buffer ( |
| Comment by Marko Mäkelä [ 2018-10-22 ] |
|
The following were filed as follow-up work for |
| Comment by Marko Mäkelä [ 2018-12-12 ] |
|
Fixing |
| Comment by Marko Mäkelä [ 2019-02-20 ] |
|
MDEV-17520 will not be in MariaDB Server 10.4 due to the size overhead of the current implementation (of using a format like ROW_FORMAT=REDUNDANT on clustered index leaf pages). We could implement it later in a different form, using a per-page or per-record format identifier. |
| Comment by Marko Mäkelä [ 2019-02-20 ] |
|
Some related bugs will be fixed later. The MariaDB 10.4 tasks related to instant ALTER TABLE (mainly |