[Bug]: COPY ALTER clauses after DROP PRIMARY KEY use a nil primary-key definition

Open
#33,317 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.

Within one COPY ALTER, DROP PRIMARY KEY leaves CopyTableDef.Pkey=nil for subsequent clauses. Later clauses do not handle that valid intermediate keyless state consistently:

  1. RENAME or CHANGE of any visible column causes a recovered nil-pointer panic.
  2. Adding an ordinary secondary index is rejected with primary key cannot be empty for secondary index.

These symptoms are reported together because they have the same trigger and invalid intermediate table state, although the two downstream consumers fail at different locations.

Problem 1: DROP PRIMARY KEY followed by RENAME/CHANGE panics

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

ALTER TABLE t ALGORITHM=COPY,
  DROP PRIMARY KEY,
  RENAME COLUMN v TO w;

Actual result:

Error 20101: internal error: panic runtime error: invalid memory address or nil pointer dereference
...
github.com/matrixorigin/matrixone/pkg/sql/plan.updateRenameColumnInTableDef
    pkg/sql/plan/build_alter_rename_column.go:184

The CHANGE variant on a fresh table also panics:

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

ALTER TABLE t ALGORITHM=COPY,
  DROP PRIMARY KEY,
  CHANGE COLUMN v w INT;

Its top planner frame is buildColumnAndConstraint at pkg/sql/plan/build_alter_change_column.go:372.

The renamed column need not be a former PK part. Both statements should succeed and leave a keyless table containing id and w with the original row.

Clause-order positive controls

Both reverse-order statements succeed and produce the expected keyless table with column w:

ALTER TABLE t ALGORITHM=COPY,
  RENAME COLUMN v TO w,
  DROP PRIMARY KEY;
ALTER TABLE t ALGORITHM=COPY,
  CHANGE COLUMN v w INT,
  DROP PRIMARY KEY;

Problem 2: DROP PRIMARY KEY followed by ADD INDEX is rejected

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

ALTER TABLE t ALGORITHM=COPY,
  DROP PRIMARY KEY,
  ADD INDEX ix_v(v);

Actual result:

Error 20101: internal error: primary key cannot be empty for secondary index

Expected result is a keyless table with a usable KEY ix_v(v) and both original rows.

Index positive controls

The equivalent transition succeeds when split into two statements:

ALTER TABLE t ALGORITHM=COPY,DROP PRIMARY KEY;
ALTER TABLE t ALGORITHM=COPY,ADD INDEX ix_v(v);

These controls also succeed:

  • DROP PRIMARY KEY, ADD UNIQUE KEY uk_v(v) in one statement;
  • creating an originally keyless table as CREATE TABLE t(v INT,KEY ix_v(v)) and reading through ix_v.

Thus an ordinary secondary index on the final keyless table is valid; the failure is specific to the same-statement intermediate state.

Root cause

pkg/sql/plan/build_alter_add_primarykey.go:102-129 implements DropPrimaryKey and ends with:

tableDef.Pkey = nil

Clauses are then applied sequentially. The RENAME and CHANGE implementations iterate primaryKeyDef.Names without a nil guard (build_alter_rename_column.go:180-194 and build_alter_change_column.go:371-377). Separately, getTablePriKeyName(nil) returns an empty string, and buildSecondaryIndexDef rejects that empty key instead of constructing the ordinary index using the final keyless-table representation.

Validation and expected behavior

Each failure was reproduced in two independent fresh-database runs, in addition to the initial discovery runs. All failed statements preserved the original schema and rows, and the connection remained usable (SELECT 42 succeeded).

COPY ALTER should maintain a consistent keyless intermediate representation after DROP PRIMARY KEY, or every later clause must handle it safely. Clause order must not choose between success, a recovered planner panic, and an internal error for equivalent valid schema transitions.

Duplicate check

Open and closed issue searches covered DROP PRIMARY KEY combined with RENAME, CHANGE, ADD INDEX, nil Pkey, the affected planner functions, and the exact secondary-index error. No matching issue was found for either symptom.

The nearest item, closed #30415, covers DROP PRIMARY KEY + DROP COLUMN in handleDropColumnWithPrimaryKey; it does not cover RENAME/CHANGE or ADD INDEX. Open #30256 tracks final-schema binding for renamed/added dependencies, but does not cover these post-DROP nil-Pkey and keyless ordinary-index paths. #32245 concerns missing hidden-index entries and is unrelated to these planning-time failures.

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.