[Bug]: CHANGE primary-key column exposes implicit PK part in secondary index DDL

Open
#33,318 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@matrix-meow is already working on this.

Since Nov 26, 2026.

Assessment

This issue has not been assessed yet.

Description

kind/bug needs-triage

Description

Tested on MatrixOne main c0cef5c9cce2164fedd201c188bfffe4d2d09a96 (version() = 8.0.30-MatrixOne-v1.3.0, git_version() = c0cef5c), single CN.

COPY ALTER CHANGE COLUMN on a single-column primary key destroys the implicit-PK alias marker stored in secondary-index parts. SHOW CREATE TABLE consequently exposes the PK as an extra user-declared index column, silently changing KEY ix_v(v) into KEY ix_v(v,id2).

Minimal reproduction

CREATE TABLE t(
  id INT PRIMARY KEY,
  v INT,
  KEY ix_v(v)
);
INSERT INTO t VALUES(1,10),(2,20);

SHOW CREATE TABLE t;
-- KEY `ix_v` (`v`)

ALTER TABLE t ALGORITHM=COPY,CHANGE COLUMN id id2 INT;

SHOW CREATE TABLE t;

Actual final DDL:

CREATE TABLE `t` (
  `id2` int NOT NULL,
  `v` int DEFAULT NULL,
  PRIMARY KEY (`id2`),
  KEY `ix_v` (`v`,`id2`)
)

Expected final DDL:

CREATE TABLE `t` (
  `id2` int NOT NULL,
  `v` int DEFAULT NULL,
  PRIMARY KEY (`id2`),
  KEY `ix_v` (`v`)
)

The PK may remain an internal payload of the secondary index, but it must not become a new user-declared key part merely because the PK column was renamed with CHANGE.

Positive control

Pure RENAME preserves the declared index definition:

CREATE TABLE control(
  id INT PRIMARY KEY,
  v INT,
  KEY ix_v(v)
);
INSERT INTO control VALUES(1,10),(2,20);

ALTER TABLE control ALGORITHM=COPY,RENAME COLUMN id TO id2;
SHOW CREATE TABLE control;
-- KEY `ix_v` (`v`)

Forced-index reads return both rows in both tables, so the reproduced defect is the persisted index metadata/regenerated DDL, not a claim of lost base rows.

Root cause

Ordinary secondary-index definitions append the table PK as an internal alias part (catalog.CreateAlias(pk)). During a renaming CHANGE, pkg/sql/plan/build_alter_change_column.go:359-368 resolves every alias and writes the plain new column name back into indexInfo.Parts:

partCol = catalog.ResolveAlias(partCol)
...
indexInfo.Parts[j] = newColName

The pure-RENAME implementation in build_alter_rename_column.go:132-160 explicitly preserves the alias marker. DDL regeneration in pkg/sql/plan/build_show_util.go suppresses only alias-marked implicit PK parts, so CHANGE makes the PK visible as an extra declared column.

Validation

The widened DDL and the pure-RENAME control were reproduced in two independent fresh-database runs, in addition to the initial discovery run. The results were identical in every run.

Duplicate check

Open and closed issue searches covered CHANGE COLUMN, primary-key rename, secondary-index alias parts, and unexpected PK columns in SHOW CREATE. No matching issue was found.

Closed #27658 concerns a stale public mo_indexes.PRIMARY column name after pure RENAME; this report instead concerns an existing secondary index's implicit alias part after CHANGE, while the pure-RENAME control is correct. Closed #26824 concerns user-created reserved alias names, and #32056/#32245 concern hidden-index data rather than SHOW CREATE alias suppression. Open #30256 covers renamed/added dependency binding but does not cover this existing-index metadata path.

Dominant language
Go
Stars
1.9k
Forks
312
Avg merge
22h 59m
Merged PRs (30d)
736

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from matrixorigin/matrixone

All issues in matrixorigin/matrixone

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.