Skip to content

fix: avoid redundant foreign key rewrites during alter table - #26790

Merged
mergify[bot] merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-26465-fk-reconcile
Aug 8, 2026
Merged

mergify[bot] merged 2 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-26465-fk-reconcile

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Aug 7, 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:

Related to #26465

What this PR does / why we need it:

QA has tables with hundreds of index definitions and duplicated foreign-key metadata. A logically storage-agnostic same-name ALTER TABLE ... CHANGE previously selected ALTER COPY. COPY replaced the table ID, then rewrote full foreign-key/constraint metadata repeatedly: once per duplicate FK or historical ref-child table ID. The resulting allocation is temporary heap allocation, not the on-disk constraint blob itself, but it can be tens of GB for one ALTER.

This PR:

  • routes exactly same-spelling, storage-agnostic CHANGE COLUMN operations through the existing in-place ReplaceDef path;
  • keeps case-only CHANGE COLUMN renames on COPY, so both foreign-key catalog columns retain their original spelling;
  • reconciles COPY foreign-key references once per unique parent/child relationship after the temporary table lifecycle, rather than once per duplicate metadata entry;
  • avoids parent ref-child mutations during temporary COPY-table creation and performs one final, exact reconciliation;
  • reduces ConstraintDef.MarshalBinary intermediate allocations while preserving the encoded wire format;
  • adds focused plan/compiler/engine/embed tests, a representative FK BVT, and QA-shape benchmarks.

The BVT intentionally uses a compact multi-FK, multi-parent topology. It now also changes the case of a child FK column and then a parent referenced column, verifying information_schema.KEY_COLUMN_USAGE transitions from V/v to V/V. The production cardinalities are retained in Go benchmarks so regular BVT/CI is not made expensive.

QA-shape benchmark

Local macOS/arm64 measurements, -benchmem -benchtime=1x; B/op is cumulative heap allocation for one ALTER, not retained metadata size.

Scenario Before This PR
departments: 768 indexes, 349 ref-child IDs 5.27s / 1.67GB 2.78s / 134MB
users: 732 indexes, 349 FKs to one parent 20.99s / 84.53GB 2.69s / 123MB
candidates: 702 FKs across three parents 2.69s / 6.22GB 20.7ms / 12.4MB

Run with:

go test -run "^$" -bench "^BenchmarkIssue26465AlterCopyQAShape$" -benchmem -benchtime=1x ./pkg/embed

Validation:

  • go test ./pkg/sql/plan -count=1
  • go test ./pkg/sql/compile -run "^(TestAddChildTableIDToDistinctParents|TestReconcileAlterCopyForeignKeyReferencesOncePerRelation|TestRewriteForeignKeyReferencesForAlterCopy|TestReconcileRefChildTableIDForAlterCopy)$" -count=1
  • go test ./pkg/vm/engine -run "^(TestConstraintDefMarshalBinaryWireCompatibility|TestIssue26465QAConstraintFixture)$" -count=1
  • go test ./pkg/embed -run "^TestIssue26465AlterCopyPreservesParentReference$" -count=1
  • mo-tester run of foreign_key/issue_26465: 48/48 passed on the rebased head
  • git diff --check

@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 →

@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.

Deep review — request changes

Reviewed the complete 13-file diff at head 06dd7c597da2b429482deac5237f15c7bc58bd4d against base c72224bea6a2628b20ca57581e0bff0de1357e91.

Found one correctness issue (P1), at pkg/sql/plan/build_alter_table.go:732:

With the default lower_case_table_names=1, a case-only ALTER TABLE child CHANGE COLUMN v V INT is classified as same-name and enters the new INPLACE path. That path only updates the relation definition and produces no FK catalog SQL. The existing COPY ChangeColumn path compares the origin spelling and emits getSqlForRenameColumn to update both column_name and refer_column_name in mo_catalog.mo_foreign_keys. After this INPLACE ALTER, the relation definition can contain V while FK catalog rows still contain v, leaving information_schema.KEY_COLUMN_USAGE stale. Please retain COPY for case-only spelling changes or enqueue the same catalog updates, and add a regression test covering both child-side and parent-side FK columns.

Verification:

  • git diff --check, gofmt -d, and parser/tree package compilation passed.
  • Targeted plan/compile/engine tests could not reach assertions because native headers usearch.h, xxhash.h, and roaring.h are unavailable; CGO_ENABLED=0 also cannot build the transitive dependencies.
  • Reviewed the new COPY FK reconciliation and ConstraintDef.MarshalBinary wire-compatibility changes; no additional actionable issue was found.

@gouhongshen gouhongshen left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 9e575f2.

Root cause confirmed: ColName() compares normalized identifiers, so CHANGE v V was classified as INPLACE under lower_case_table_names=1; that path does not emit FK catalog updates.

The algorithm now compares ColNameOrigin(): only identical spelling may be INPLACE. Any spelling-only rename stays on the existing COPY path, whose ChangeColumn emits both mo_foreign_keys.column_name and refer_column_name updates.

Regression coverage:

  • planner test asserts case-only CHANGE selects COPY and contains both FK catalog updates;
  • foreign_key/issue_26465 changes a child FK column v -> V, then its parent referenced column v -> V, and verifies information_schema.KEY_COLUMN_USAGE changes from V/v to V/V.

Validation: go test ./pkg/sql/plan -count=1; rebased-head BVT 48/48; git diff --check.

@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.

Reviewed base 629cbbe2fe51d6d457a31f450c7e5c1bc4628213 at head 9e575f20fc55c6bcd29478099541bbece45a135c.

No material correctness issue found. Checked ALTER COPY FK reconciliation, duplicate-parent/child deduplication, case-only CHANGE COLUMN COPY routing and catalog metadata updates, and ConstraintDef wire-compatible serialization.

git diff --check passed. Exact-head CI passed Linux UT, arm64 SCA, BVT, coverage, Iceberg, and MongoDB checks; Darwin/Upgrade jobs were skipped. Manual benchmarks were not run.

@mergify mergify Bot added the queued label Aug 8, 2026
@mergify

mergify Bot commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-08 09:09 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-08-08 09:09 UTC · at 9e575f20fc55c6bcd29478099541bbece45a135c · squash

This pull request spent 12 seconds in the queue, including 2 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86

@mergify
mergify Bot merged commit 6481927 into matrixorigin:main Aug 8, 2026
26 of 27 checks passed
@mergify mergify Bot removed the queued label Aug 8, 2026
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rigin#26790)

QA has tables with hundreds of index definitions and duplicated foreign-key metadata. A logically storage-agnostic same-name `ALTER TABLE ... CHANGE` previously selected ALTER COPY. COPY replaced the table ID, then rewrote full foreign-key/constraint metadata repeatedly: once per duplicate FK or historical ref-child table ID. The resulting allocation is temporary heap allocation, not the on-disk constraint blob itself, but it can be tens of GB for one ALTER.

This PR:

- routes exactly same-spelling, storage-agnostic `CHANGE COLUMN` operations through the existing in-place `ReplaceDef` path;
- keeps case-only `CHANGE COLUMN` renames on COPY, so both foreign-key catalog columns retain their original spelling;
- reconciles COPY foreign-key references once per unique parent/child relationship after the temporary table lifecycle, rather than once per duplicate metadata entry;
- avoids parent ref-child mutations during temporary COPY-table creation and performs one final, exact reconciliation;
- reduces `ConstraintDef.MarshalBinary` intermediate allocations while preserving the encoded wire format;
- adds focused plan/compiler/engine/embed tests, a representative FK BVT, and QA-shape benchmarks.

The BVT intentionally uses a compact multi-FK, multi-parent topology. It now also changes the case of a child FK column and then a parent referenced column, verifying `information_schema.KEY_COLUMN_USAGE` transitions from `V/v` to `V/V`. The production cardinalities are retained in Go benchmarks so regular BVT/CI is not made expensive.

Approved by: @XuPeng-SH, @heni02
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rigin#26790)

QA has tables with hundreds of index definitions and duplicated foreign-key metadata. A logically storage-agnostic same-name `ALTER TABLE ... CHANGE` previously selected ALTER COPY. COPY replaced the table ID, then rewrote full foreign-key/constraint metadata repeatedly: once per duplicate FK or historical ref-child table ID. The resulting allocation is temporary heap allocation, not the on-disk constraint blob itself, but it can be tens of GB for one ALTER.

This PR:

- routes exactly same-spelling, storage-agnostic `CHANGE COLUMN` operations through the existing in-place `ReplaceDef` path;
- keeps case-only `CHANGE COLUMN` renames on COPY, so both foreign-key catalog columns retain their original spelling;
- reconciles COPY foreign-key references once per unique parent/child relationship after the temporary table lifecycle, rather than once per duplicate metadata entry;
- avoids parent ref-child mutations during temporary COPY-table creation and performs one final, exact reconciliation;
- reduces `ConstraintDef.MarshalBinary` intermediate allocations while preserving the encoded wire format;
- adds focused plan/compiler/engine/embed tests, a representative FK BVT, and QA-shape benchmarks.

The BVT intentionally uses a compact multi-FK, multi-parent topology. It now also changes the case of a child FK column and then a parent referenced column, verifying `information_schema.KEY_COLUMN_USAGE` transitions from `V/v` to `V/V`. The production cardinalities are retained in Go benchmarks so regular BVT/CI is not made expensive.

Approved by: @XuPeng-SH, @heni02
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…rigin#26790)

QA has tables with hundreds of index definitions and duplicated foreign-key metadata. A logically storage-agnostic same-name `ALTER TABLE ... CHANGE` previously selected ALTER COPY. COPY replaced the table ID, then rewrote full foreign-key/constraint metadata repeatedly: once per duplicate FK or historical ref-child table ID. The resulting allocation is temporary heap allocation, not the on-disk constraint blob itself, but it can be tens of GB for one ALTER.

This PR:

- routes exactly same-spelling, storage-agnostic `CHANGE COLUMN` operations through the existing in-place `ReplaceDef` path;
- keeps case-only `CHANGE COLUMN` renames on COPY, so both foreign-key catalog columns retain their original spelling;
- reconciles COPY foreign-key references once per unique parent/child relationship after the temporary table lifecycle, rather than once per duplicate metadata entry;
- avoids parent ref-child mutations during temporary COPY-table creation and performs one final, exact reconciliation;
- reduces `ConstraintDef.MarshalBinary` intermediate allocations while preserving the encoded wire format;
- adds focused plan/compiler/engine/embed tests, a representative FK BVT, and QA-shape benchmarks.

The BVT intentionally uses a compact multi-FK, multi-parent topology. It now also changes the case of a child FK column and then a parent referenced column, verifying `information_schema.KEY_COLUMN_USAGE` transitions from `V/v` to `V/V`. The production cardinalities are retained in Go benchmarks so regular BVT/CI is not made expensive.

Approved by: @XuPeng-SH, @heni02

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working kind/enhancement kind/test-ci size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants