1
0
mirror of https://github.com/MariaDB/server.git synced 2026-10-23 18:49:29 +03:00

MDEV-36436 Assertion "unexpected references" == 0 after ALTER IGNORE TABLE

Problem:
=========
An assertion failure occurs in InnoDB during consecutive
ALTER TABLE operations using the COPY algorithm. The crash
happens during the table rename phase because the
source table (dict_table_t::n_ref_count) is non-zero, despite
the thread holding an exclusive Metadata Lock (MDL).

Reason:
========
When ALTER IGNORE TABLE is executed via the COPY algorithm,
it generates undo logs for every row inserted into the
intermediate table (e.g., #sql-alter-...). The background Purge
Thread, responsible for cleaning up these undo logs, attempts
to take an MDL on the table to prevent the table from
being dropped while in use.

Race condition:
==================
First ALTER: Creates #sql-alter-, copies data, and renames it to t1.

Purge Activation: The Purge thread picks up the undo logs from step 1.
It takes an MDL on the temporary name (#sql-alter-) and increments
the table's n_ref_count.

Identity Shift: InnoDB renames the physical table object to t1, but
the Purge thread still holds a reference to this object.

Second ALTER: Starts a new copy process. When it attempts to rename
the "new" t1 to a backup name, it checks if n_ref_count == 0.
Because the Purge thread is still "pinning" the object to
clean up logs from the first ALTER, the count is > 0,
triggering the assertion failure.

Solution:
========
ALTER IGNORE TABLE needs row-level undo logging to easily
roll back the last inserted row in case of duplicate key errors.
By discarding the last undo log record after inserting each row,
purge will not process any log records generated by
ALTER IGNORE TABLE, preventing unexpected access from the purge
subsystem during subsequent DDL operations.

Make skip_alter_undo (1-bit) to (2-bit enum)
to support different ALTER operation modes:

- NO_UNDO (0): Normal mode with standard undo logging
- SKIP_UNDO (1): ALTER mode that skips undo logging
- IGNORE_UNDO (2): ALTER IGNORE mode that rewrites undo blocks

trx_undo_report_row_operation(): Add ALTER IGNORE undo
rewriting logic. Store old undo record info before writing
new records for IGNORE_UNDO mode. Reset undo top_offset
to maintain only latest insert undo

row_ins_clust_index_entry_low(): Prevent ALTER IGNORE from
using bulk insert optimization. since it requires to write
undo log for each row operation that's incompatible with
existing bulk operation.
This commit is contained in:
Thirunarayanan Balathandayuthapani
2026-02-26 12:07:23 +05:30
committed by Thirunarayanan B
parent ad70702859
commit e5fed9eaf4
18 changed files with 151 additions and 58 deletions

View File

@@ -4542,7 +4542,7 @@ bool select_insert::prepare_eof()
/* Don't convert the warning to error in case
statistics updation fails */
Abort_on_warning_instant_set save_abort_on_warning(thd, false);
error= table->file->extra(HA_EXTRA_END_ALTER_COPY);
error= table->file->extra(HA_EXTRA_END_COPY);
if (error == HA_ERR_FOUND_DUPP_KEY)
{
uint key_nr= table->file->get_dup_key(error);
@@ -4688,7 +4688,7 @@ void select_insert::abort_result_set()
finalize_replace(table, info.handle_duplicates, info.ignore);
table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
if (table->file->extra(HA_EXTRA_ABORT_ALTER_COPY) == HA_ERR_ROLLBACK)
if (table->file->extra(HA_EXTRA_ABORT_COPY) == HA_ERR_ROLLBACK)
thd->transaction_rollback_request= true;
/*
If at least one row has been inserted/modified and will stay in
@@ -5165,8 +5165,13 @@ select_create::prepare(List<Item> &_values, SELECT_LEX_UNIT *u)
!table->s->long_unique_table)
{
table->file->ha_start_bulk_insert((ha_rows) 0);
if (thd->lex->duplicates == DUP_ERROR && !thd->lex->ignore)
table->file->extra(HA_EXTRA_BEGIN_ALTER_COPY);
if (thd->lex->duplicates == DUP_ERROR)
{
static_assert(int{HA_EXTRA_BEGIN_ALTER_IGNORE_COPY} ==
int{HA_EXTRA_BEGIN_COPY} + 1, "");
table->file->extra(ha_extra_function(int{HA_EXTRA_BEGIN_COPY} +
thd->lex->ignore));
}
table->file->extra(HA_EXTRA_WRITE_CACHE);
}
thd->abort_on_warning= !info.ignore && thd->is_strict_mode();