Repository navigation
[4.2-dev] fix(clone): preserve foreign key metadata across alter copy - #26422
Merged
mergify[bot] merged 1 commit intoJul 30, 2026
Merged
Conversation
…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
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
XuPeng-SH
approved these changes
Jul 30, 2026
gouhongshen
marked this pull request as draft
July 30, 2026 03:12
ghost
approved these changes
Jul 30, 2026
Contributor
Merge Queue Status
This pull request spent 26 seconds in the queue, including 3 seconds running CI. Required conditions to merge
|
This branch was previously deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What type of PR is this?
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 TABLEreplaces 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:
mo_foreign_keysrows across the COPY replacement and restores the logical source table name;0;Backport notes:
eeaa44641d8984a248e6447b93e4e80d9f6a2f71;pkg/sql/planSQL string literal quoting helper required by fix(clone): preserve foreign key metadata across alter copy #26330. The helper originated in fix: escape SQL string literals to prevent injection in Data Branch operations #26218, but the rest of that unrelated change is not included.Validation
go test ./pkg/sql/plan ./pkg/sql/compile ./pkg/frontendgit diff --check origin/4.2-dev...HEADIssue #26328 was validated with a fresh single-node deployment using the exact reported schema and
ALTER TABLEsequence:4.2-devbase (84e8ed9d2)e933c4e90)CREATE DATABASE ... CLONE ...can't find fkTableThis 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.