Skip to content

[Bug]: Renaming a prefix-indexed column leaves stale prefix metadata #26822

Closed

Description

@LeftHandCold

Is there an existing issue for the same bug?

I searched open and closed MatrixOne issues and PRs for RENAME COLUMN with prefix indexes, prefix_lengths metadata, SHOW CREATE after a rename, and unique-index metadata migration.

This is distinct from:

Branch Name

main

Commit ID

08770e1beeb23239c91165492f2fe85243a3c66a

Other Environment Information

  • Local single-node MatrixOne on Linux
  • MySQL client against port 6001
  • Reproduced again in a current local main build
  • The failure persists after an explicit TN flush

Actual Behavior

RENAME COLUMN updates the visible index part and the mo_indexes.column_name row, but leaves the old column name in mo_indexes.algo_params.prefix_lengths.

For UNIQUE KEY uq_a(a(4)), after renaming a to renamed_a:

  • SHOW CREATE TABLE displays UNIQUE KEY uq_a (renamed_a) and loses (4);
  • catalog metadata says column_name=renamed_a but algo_params={"prefix_lengths":"a:4"};
  • a value with the already-used four-character prefix can be inserted, violating the original unique-prefix constraint;
  • after flush, a forced hidden-index lookup for the original row returns no row while IGNORE INDEX returns it.

Expected Behavior

Renaming a column must preserve the prefix-index definition and unique-prefix semantics. All name-bearing index metadata must move atomically from a:4 to renamed_a:4, and forced/base-table reads must agree before and after flush.

Steps to Reproduce

Run the expected-error insert separately so the client can continue.

DROP DATABASE IF EXISTS rename_prefix_repro;
CREATE DATABASE rename_prefix_repro;
USE rename_prefix_repro;

CREATE TABLE t (
  id INT PRIMARY KEY,
  a VARCHAR(32),
  UNIQUE KEY uq_a(a(4))
);
INSERT INTO t VALUES (1, 'abcd-one');

-- Correct before RENAME: ERROR 1062 Duplicate entry 'abcd'.
INSERT INTO t VALUES (9, 'abcd-control');

ALTER TABLE t RENAME COLUMN a TO renamed_a;

SHOW CREATE TABLE t;
-- Wrong: UNIQUE KEY uq_a (renamed_a), with the (4) prefix omitted.

SELECT name, column_name, ordinal_position, algo_params
FROM mo_catalog.mo_indexes
WHERE database_id = (
  SELECT dat_id FROM mo_catalog.mo_database
  WHERE datname = 'rename_prefix_repro'
)
ORDER BY name, ordinal_position;
-- uq_a | renamed_a | 1 | {"prefix_lengths":"a:4"}

-- Wrong: should fail the same unique prefix, but succeeds.
INSERT INTO t VALUES (2, 'abcd-two');

SELECT id, renamed_a FROM t ORDER BY id;
-- 1 | abcd-one
-- 2 | abcd-two

SELECT id, renamed_a
FROM t FORCE INDEX(uq_a)
WHERE renamed_a = 'abcd-one';
-- before flush: 1 | abcd-one

SELECT mo_ctl('dn', 'flush', 'rename_prefix_repro.t');

SELECT id, renamed_a
FROM t FORCE INDEX(uq_a)
WHERE renamed_a = 'abcd-one';
-- wrong: empty result

SELECT id, renamed_a
FROM t IGNORE INDEX(uq_a)
WHERE renamed_a = 'abcd-one';
-- correct: 1 | abcd-one

Additional Information

build_alter_rename_column.go updates IndexDef.Parts and the mo_indexes.column_name catalog row, but does not rewrite ordinary-index IndexAlgoParams. Prefix lengths are encoded as a column:length mapping, so later DML and planner code cannot match the renamed part to its prefix length.

The rename operation needs to parse the prefix-length metadata, replace only the renamed key while preserving other index parameters and multi-part entries, then persist the updated params together with the part and catalog-column rewrite.

Please retain black-box coverage for:

  • unique prefix duplicate rejection before and after rename;
  • SHOW CREATE and mo_indexes metadata before/after;
  • same-prefix insert after rename;
  • FORCE versus IGNORE reads across a flush;
  • multi-part indexes with only one prefixed part, ensuring unrelated params remain unchanged.

Activity

  1. self-assigned this
    on Aug 9, 2026
  2. LeftHandCold commented on Aug 9, 2026

    @LeftHandCold
    CollaboratorAuthor

    Fixed locally in commit 9b7629e901e13abed8b64670aba50238e487f70f.

    The rename path now rewrites the matching key in ordinary-index prefix_lengths metadata and persists the updated algo_params together with the index-part/catalog column rename. Other prefix entries and flat params are retained; vector/plugin index metadata remains on its existing hook path.

    Validated on a locally built MO service:

    • focused and full pkg/sql/plan tests passed;
    • controlled CGo go vet and make build passed;
    • new distributed regression passed 17/17, covering SHOW CREATE, catalog rows, unique-prefix rejection, a multi-part prefix index, FORCE/IGNORE reads, and flush;
    • existing alter_table_rename_column regression passed 269/269.
  3. LeftHandCold commented on Aug 9, 2026

    @LeftHandCold
    CollaboratorAuthor

    Follow-up fix: ALTER TABLE ... CHANGE COLUMN has an independent planner path. It now renames prefix-length metadata together with the indexed column name.

    Commit: 8e1c18823ad39356838fd24d2813b2832ad8417e

    Validation:

    • Added a CHANGE COLUMN white-box regression test.
    • Extended rename_prefix_index distributed coverage for SHOW CREATE, mo_indexes metadata, unique-prefix enforcement, and FORCE/IGNORE INDEX agreement after flush: 28/28.
    • Existing alter_table_change_column distributed suite: 923/923.
    • Full ./pkg/sql/plan tests and controlled go vet passed.
  4. added a commit that references this issue on Aug 9, 2026
    ac33944
  5. added
    kind/bug
    severity/s0
    on Aug 11, 2026
  6. LeftHandCold commented on Aug 11, 2026

    @LeftHandCold
    CollaboratorAuthor

    Validation result — fixed by #26829.

    Verified on the exact merge commit ac339444b558daf2c3d7fe63f755743449db7696, with an 08770e1 baseline comparison.

    Baseline: RENAME left algo_params.prefix_lengths keyed by the old column, SHOW CREATE lost the prefix length, duplicate-prefix INSERT was accepted, and FORCE could miss the original row after flush.
    Fix verification: RENAME and CHANGE preserve (N) in SHOW CREATE and rewrite the persisted metadata to the new name. A duplicate prefix is rejected, valid DML remains correct, and FORCE equals IGNORE both before and after flush. The regression coverage also validates INSERT/UPDATE/DELETE after rename. Malformed/stale historical metadata now fails closed for DML rather than silently writing inconsistent index data.

    Conclusion: the reported DDL/catalog correctness defect is fixed. Closing.

  7. added 3 commits that reference this issue on Sep 17, 2026
    b5b2b81
    31e4bad
    0c59fbb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions