[Bug]: COPY ALTER ADD COLUMN FIRST makes MASTER-indexed rows invisible

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

Maintainers usually reply within 1 day

@XuPeng-SH is already working on this.

Since Nov 26, 2026.

Assessment

This issue has not been assessed yet.

Description

kind/bug severity/s0

Description

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

After COPY ALTER inserts a column before existing columns, an unaffected MASTER index makes existing rows disappear from filtered queries. The base rows and hidden MASTER entries still exist, but the MASTER index join returns no matching rows.

Minimal reproduction

CREATE TABLE m(
  a VARCHAR(30),
  b VARCHAR(30),
  INDEX mi USING MASTER(a)
);
INSERT INTO m VALUES('x','payload');

SELECT a,b FROM m WHERE a='x';
-- Before ALTER: ('x','payload')

ALTER TABLE m ADD COLUMN lead INT DEFAULT 7 FIRST,ALGORITHM=COPY;

SELECT a,b FROM m WHERE a='x';
-- Actual: empty set
-- Expected: ('x','payload')

SELECT COUNT(*) FROM m WHERE a='x';
-- Actual: 0
-- Expected: 1

SELECT a,b FROM m IGNORE INDEX(mi) WHERE a='x';
-- Positive control: ('x','payload')

SELECT COUNT(*) FROM m IGNORE INDEX(mi) WHERE a='x';
-- Positive control: 1

The failure persists on a fresh connection. Two independent fresh-database reproductions produced identical results.

Positive controls and scope

  • An ordinary KEY ri(a) remains correct after the same ADD COLUMN ... FIRST.
  • A MASTER index remains correct when the new column is appended at the end instead of inserted before existing columns.
  • IGNORE INDEX(mi) returns the original row, proving that the base row was not lost.
  • Rebuilding the affected index with ALTER TABLE m ALGORITHM=COPY,MODIFY COLUMN a VARCHAR(40) makes the query return the row again.
  • Single-column and composite MASTER indexes, with and without an explicit primary key, reproduce the problem. Duplicate keys and duplicate complete rows were also covered.
  • The hidden MASTER entries remain present after ALTER; this is not physical loss of hidden-index rows.

Root cause

MASTER lookup keys include the source column's physical Seqnum:

  • pkg/sql/plan/apply_indices_master.go constructs lookup prefixes using serial_full(getColSeqFromColDef(...), value).
  • pkg/sql/plan/apply_indices.go:getColSeqFromColDef returns ColDef.Seqnum.
  • pkg/sql/compile/alter.go:cloneUnaffectedIndexes clones an unaffected index's old hidden entries without rewriting those keys.

ADD COLUMN ... FIRST changes the existing columns' physical sequence numbers in the replacement table. New queries therefore generate prefixes using the new sequence numbers, while the cloned MASTER entries still contain the old sequence numbers. Appending a column does not renumber existing columns, and MODIFY on an indexed column rebuilds the entries, explaining both positive controls.

Expected behavior

COPY ALTER must preserve MASTER-index query results for every existing row. If column renumbering requires MASTER entries to be rewritten, the unaffected-index clone path must rewrite them or rebuild the index; it must not publish an index that silently returns false-empty results.

Duplicate check

Open and closed searches covered USING MASTER with ALTER TABLE, MASTER with ADD COLUMN or ALGORITHM=COPY, serial_full, Seqnum, COPY ALTER FIRST, and MASTER wrong results. No matching issue was found.

This differs from #32245, which tracks missing ordinary/unique hidden-index entries: here the MASTER entries remain complete but their Seqnum key labels no longer match the replacement schema. It also differs from #32056 (stale composite-PK references in ordinary indexes), #33108 (FK-action index maintenance), and #25586 (MASTER LIMIT/OFFSET paging).

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.