Skip to content

[4.2-dev] fix(clone): preserve foreign key metadata across alter copy - #26422

Merged
mergify[bot] merged 1 commit into
matrixorigin:4.2-devfrom
gouhongshen:agent/cherry-pick-26330-4.2
Jul 30, 2026
Merged

mergify[bot] merged 1 commit into
matrixorigin:4.2-devfrom
gouhongshen:agent/cherry-pick-26330-4.2

Conversation

@gouhongshen

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

Backport of #26330 to 4.2-dev.

Relates to #26328.

What this PR does / why we need it:

COPY-mode ALTER TABLE replaces a relation with a new physical table. Before #26330, this could lose or split foreign-key metadata, and clone/snapshot/PITR restore could miss dependency edges after an affected ALTER.

This backport:

  • transfers child-side mo_foreign_keys rows across the COPY replacement and restores the logical source table name;
  • keeps parent-side names stable so self references do not point at the temporary relation;
  • canonicalizes parent reverse-reference mutations and preserves self-reference sentinel 0;
  • derives clone and restore dependency edges from historical table DDL in addition to catalog rows;
  • enumerates ordinary snapshot source databases at the source timestamp;
  • includes the unit and BVT regression coverage from fix(clone): preserve foreign key metadata across alter copy #26330.

Backport notes:

Validation

  • go test ./pkg/sql/plan ./pkg/sql/compile ./pkg/frontend
  • git diff --check origin/4.2-dev...HEAD

Issue #26328 was validated with a fresh single-node deployment using the exact reported schema and ALTER TABLE sequence:

Check 4.2-dev base (84e8ed9d2) This PR (e933c4e90)
FK catalog rows before ALTER 1 1
FK catalog rows after ALTER 0 1
CREATE DATABASE ... CLONE ... Fails with can't find fkTable Succeeds
Cloned FK catalog rows N/A 1
Valid child insert N/A Succeeds
Missing-parent child insert N/A Rejected by FK
Clone the cloned database N/A Succeeds; FK catalog rows remain 1

This confirms both the original failure and the repair of the underlying catalog metadata, cloned schema, and FK enforcement.

The BVT SQL and audited result from #26330 are included unchanged; the complete BVT suite was not rerun locally for this backport.

…igin#26330)

COPY-mode `ALTER TABLE` replaces a relation with a new physical table. The old implementation could lose or split foreign-key metadata while doing that:

- `mo_catalog.mo_foreign_keys` rows owned by the source child were deleted before the replacement inherited them;
- catalog-only forward references created under `FOREIGN_KEY_CHECKS=0` were absent from `TableDef`, so reconstructing the temporary table could not recreate them;
- self references could leave multiple `RefChildTableDef` values whose add/remove/replace paths disagreed;
- databases and historical snapshots/PITR corrupted by an older binary retained the FK in `SHOW CREATE TABLE` but lacked the catalog dependency used by clone and restore ordering.

This PR:

- applies ALTER FK mutations to the source catalog rows, transfers those rows around the source drop, and restores the original logical table name after the physical replacement;
- keeps parent-side names stable during COPY so self references never point at the temporary relation;
- centralizes reverse-reference add/remove/restore/replace into one canonical mutation that emits exactly one `RefChildTableDef`, deduplicates IDs, preserves the self-reference sentinel `0`, and is also used by `TRUNCATE TABLE`;
- derives dependency edges from historical source table DDL in addition to `mo_foreign_keys` for database clone, snapshot restore, PITR restore, and dropped-account timestamp restore; table DDL repairs resolved historical constraints while the catalog remains necessary for unresolved forward references;
- enumerates ordinary snapshot source databases by `MO_TS` under the source account instead of resolving the snapshot name there. Snapshot metadata belongs to the system account, so name resolution under a dropped source account incorrectly reported `mo_snapshots` missing;
- adds unit and BVT coverage for repeated COPY ALTERs, altered parents/children, sibling and multi-level FKs, composite/multiple-parent FKs, self references plus external children, FK recreation, both forward-reference paths, snapshot clone, clone-after-clone, table/column renames, missing historical catalog rows, canonical TRUNCATE replacement, and timestamp-based source enumeration.

The generated `clone_fk_after_alter` result was manually audited: all 17 recorded errors are intentional FK enforcement checks; there are no unexpected errors.

Approved by: @heni02, @XuPeng-SH
@matrix-meow matrix-meow added the size/XL Denotes a PR that changes [1000, 1999] lines label Jul 30, 2026
@mergify
mergify Bot requested a review from XuPeng-SH July 30, 2026 03:07
@mergify mergify Bot added the kind/bug Something isn't working label Jul 30, 2026
@gouhongshen
gouhongshen marked this pull request as ready for review July 30, 2026 03:08
@gouhongshen
gouhongshen requested a review from aunjgr as a code owner July 30, 2026 03:08
@gouhongshen
gouhongshen requested a review from a user July 30, 2026 03:08
@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 →

@gouhongshen
gouhongshen marked this pull request as draft July 30, 2026 03:12
@mergify

mergify Bot commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-30 05:31 UTC · Rule: release-4.2 · triggered by rule Automatic queue on approval for release-4.2
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-07-30 05:31 UTC · at e933c4e9043a32ac9f32509da7d3d3b536c29844 · squash

This pull request spent 26 seconds in the queue, including 3 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 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 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 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 Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • 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)

@mergify
mergify Bot merged commit 890270f into matrixorigin:4.2-dev Jul 30, 2026
75 of 89 checks passed
@mergify mergify Bot removed the queued label Jul 30, 2026

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