Skip to content

feat(plan): support ALTER TABLE AUTO_INCREMENT = value - #24531

Closed
ULookup wants to merge 82 commits into
matrixorigin:mainfrom
ULookup:fix/issue-23143-alter-auto-increment
Closed

ULookup wants to merge 82 commits into
matrixorigin:mainfrom
ULookup:fix/issue-23143-alter-auto-increment

Conversation

@ULookup

@ULookup ULookup commented May 21, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #23143

What this PR does / why we need it:

Make ALTER TABLE t AUTO_INCREMENT = N work, updating the starting offset for auto-increment columns.

Changes across five layers:

  • Proto (api.proto, plan.proto): new AlterKind_UpdateAutoIncrement enum, AlterTableAutoIncrement message, and oneof fields
  • Plan (build_ddl.go, build_alter_table.go, deepcopy.go): handle TableOptionAutoIncrement in both inplace and copy paths; offset = Value - 1; explicit error when table has no AUTO_INCREMENT column
  • Compile (compile/ddl.go, api.go): build UpdateAutoIncrement request (engine no-ops), then post-processing calls incrservice.SetOffset to update the offset
  • Incrservice (service.go, types.go, store_mem.go, store_sql.go): new SetOffset interface and implementations with never-decrease guard
  • Engine (disttae/txn_table.go, tae/catalog/schema.go, tae/txn/txnimpl/table.go): whitelist AlterKind_UpdateAutoIncrement to avoid panic on unknown kind

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@CLAassistant

CLAassistant commented May 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ULookup ULookup changed the title Fix/issue 23143 alter auto increment feat(plan): support ALTER TABLE AUTO_INCREMENT = value May 21, 2026
@mergify mergify Bot added the kind/feature label May 21, 2026
@ULookup ULookup linked an issue May 21, 2026 that may be closed by this pull request
@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label May 21, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes — two merge blockers remain.

  1. High: COPY reconciliation treats the hidden fake primary key as a user AUTO_INCREMENT column and breaks unrelated ALTER TABLE statements.

constructTableClone iterates every column with Typ.AutoIncr and now runs SELECT MAX(column) against the source table. MatrixOne's __mo_fake_pk_col is Hidden + AutoIncr, but it is not SQL-visible. Therefore a COPY-based ALTER on a table without a user primary key attempts to query MAX(__mo_fake_pk_col) and fails with:

invalid input: column __mo_fake_pk_col does not exist

This is deterministic in the current CI, not flaky: the standalone PESSIMISTIC, standalone multi-CN PROXY, and compose PESSIMISTIC jobs all show the same error, including ordinary statements such as ALTER TABLE pri01 ADD CONSTRAINT PRIMARY KEY(col1). One BVT file alone then cascades into 72 failed assertions.

Please fix this as a classification/ownership issue rather than only suppressing this one query. The planner already distinguishes user-visible AUTO_INCREMENT columns (AutoIncr && !Hidden), but both source maximum collection and destination reconciliation need the same contract. updateDstAutoIncrColumns currently calls GetAutoColumnFromDef, which intentionally includes hidden allocator columns, so merely skipping the source query is insufficient: a table containing both a visible AUTO_INCREMENT column and an internal hidden allocator column could still apply the user's requested offset to the hidden column. Preserve/rebuild internal fake-PK allocator state separately, and apply ALTER ... AUTO_INCREMENT reconciliation only to the user-visible column(s).

Please add regressions for at least:

  • a hidden-fake-PK-only table undergoing an unrelated COPY ALTER (no internal MAX(__mo_fake_pk_col) query and ALTER succeeds);
  • a definition containing both a visible AUTO_INCREMENT column and a hidden allocator column (the request affects only the visible column);
  • the existing requested-offset-vs-copied-maximum cases after that separation.

The new focused tests currently pass because their synthetic table definitions contain only a visible AUTO_INCREMENT column, which is exactly why they miss this production table category.

  1. The PR introduces staticcheck failures and cannot pass SCA.

Current head reports:

  • pkg/incrservice/private_table_cache.go:132: SA4004, the for loop is unconditionally terminated;
  • pkg/vm/engine/tae/logtail/mgr.go:290: SA4011, ineffective break (it only exits the switch, and the following condition performs the actual loop exit).

Please remove the dead/redundant control flow and rerun SCA. These are straightforward, but they are introduced by this PR and the current SCA job fails on them.

I rechecked the core AUTO_INCREMENT paths: lower-than-existing-max reconciliation, transaction-private rollback cleanup, copied-row maximum reconciliation, quoted identifiers, and table-version fencing are materially improved from the previous revision. The blocker above is a missed system category at the COPY boundary, not a rejection of the overall design.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Request changes on two common-path data-correctness issues. These are independent of 2PC/recovery. The previous hidden-column classification and SCA blockers appear fixed, and the epoch plumbing is a meaningful improvement, but the following invariants are still not closed.

[P1] COPY ALTER mixes source and destination column-index coordinate systems

buildTableCloneMeta records both the source data maxima and allocator offsets by source col_index (pkg/sql/compile/operator.go:2285-2296,2303-2337). updateDstAutoIncrColumns later looks up those maps using the destination AutoColumn.ColIndex (pkg/sql/colexec/table_clone/table_clone.go:542-553). A COPY ALTER that drops, adds, or reorders a column before the auto-increment column changes that index.

Minimal counterexample:

  1. Source schema is (x INT, id BIGINT AUTO_INCREMENT PRIMARY KEY), so id has source index 1; source max is 200 and allocator offset is 250.
  2. ALTER TABLE ... DROP COLUMN x, AUTO_INCREMENT = 100 takes the COPY path; id now has destination index 0 and the requested offset is 99.
  3. The source maps contain key 1, but the destination lookup reads key 0, so both source safeguards become zero and the code calls SetOffset(..., "id", 99, ...) instead of 250.
  4. A later generated value can collide with copied data. The hidden __mo_fake_pk_col allocator has the same identity problem when its index moves.

I reproduced this deterministically at the exact PR head: a focused unit test expected offset 250 and gomock observed 99. Please carry a stable source-to-destination identity/mapping through TableCloneCtx (including rename semantics) rather than using positional indices across schemas. Cover drop/add/reorder/rename before a visible AUTO_INCREMENT column and before the hidden fake-PK column.

[P1] Legacy CN writes silently bypass the epoch fence after ALTER

Both setAutoIncrEpochDependency and registerWriteAutoIncrEpoch return success when AutoIncrEpochKnown == false (pkg/vm/engine/tae/rpc/handle.go:419-442). The test in pkg/vm/engine/tae/rpc/rpc_test.go:84-93 explicitly codifies that the same legacy V0 write is accepted after the table epoch advances.

That is not a safe rolling-upgrade boundary. An old CN can retain a preallocated AUTO_INCREMENT range from before the ALTER; the DDL lock prevents overlapping statements but does not invalidate that private range on every legacy CN. After the ALTER commits, the old CN can generate from the stale range, send Known=false, and TN accepts the write without checking the advanced epoch. This defeats the fence on ordinary 1PC writes and can commit values below the reset point or duplicate existing values.

Please make mixed-version behavior fail-safe: for example, capability-gate ALTER TABLE ... AUTO_INCREMENT until all writers support epochs, reject unknown-epoch user writes once the table has entered the fenced epoch regime, or provide a synchronous cluster-wide invalidation protocol with an equivalent correctness proof. Add a mixed-version test with a stale legacy CN allocation before ALTER and a write after ALTER; accepting Known=false must not be the success criterion.

Validation performed on bdcaba31648a7452aceec6e37e41ad20223be991

  • Existing tests passed for pkg/incrservice, pkg/sql/colexec/table_clone, pkg/sql/compile, pkg/sql/plan, pkg/vm/engine/disttae, and pkg/vm/engine/tae/rpc.
  • Race tests passed for pkg/incrservice and pkg/sql/colexec/table_clone.
  • go vet passed for the directly affected SQL/increment packages; make err-check and git diff --check passed.
  • Required CI is green, but its matrix does not exercise schema-index remapping, and the legacy-CN test currently asserts the unsafe outcome.

I did not find a new blocking leak, deadlock, or unbounded-growth issue in the normal paths. The request-changes decision is specifically about the two reproducible correctness gaps above.

@gouhongshen gouhongshen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex automated review

No blocking correctness, compatibility, concurrency, resource-handling, performance, or test-coverage regressions found in the PR diff. The reviewed CI checks are successful.

This branch was previously deployed

1 inactive deployment
ci — bdcaba31 Deployed Jul 20, 2026 by VioletQwQ-0 via Matrixone Utils CI / Coverage #41443
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ALTER TABLE AUTO_INCREMENT = value not supported

8 participants