Repository navigation
fix(clone): preserve foreign key metadata across alter copy - #26330
Conversation
|
I did a destructive review of the FK metadata paths. I do not think this is merge-ready yet; there are two compatibility/data-integrity blockers and one invariant bug. 1. Existing corrupted deployments and historical snapshots are not repairedIssue #26328 confirms that already-deployed v4.1.4 databases can have missing Historical snapshots/PITR are a harder form of the same problem: the restore paths query This needs an explicit source-of-truth/reconciliation design, plus a release-to-target compatibility test that creates the corruption with the old release and clones/restores it with the new binary. Requiring users to ALTER every affected table again would not be an acceptable repair. 2. COPY ALTER still silently loses catalog-only forward referencesThe engine explicitly supports forward references with COPY ALTER reconstructs the temporary table from A minimal regression case is: set foreign_key_checks = 0;
create database d;
create table d.child (
id int,
parent_id int,
constraint fk_forward foreign key (parent_id) references d.parent(id)
);
alter table d.child add column payload int;
create table d.parent (id int primary key);
set foreign_key_checks = 1;
-- the FK must still be present and enforceableThe same case should be tested after dropping an existing parent under 3.
|
|
Addressed all three blockers in
The root-cause gap was also corrected: the implementation no longer assumes the temporary CREATE omitted the replacement reverse ID. It preserves source-owned catalog metadata explicitly and canonicalizes whatever reverse-reference state CREATE produced. Validation: affected Go packages pass; |
|
第二轮破坏性 review(head
本地验证: |
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep-reviewed exact head 127bd41. The COPY ALTER FK catalog-transfer/canonical reverse-reference changes and historical restore/clone dependency reconstruction are internally consistent, including self references and unresolved forward references. Focused CGo tests passed for ./pkg/frontend ./pkg/sql/compile ./pkg/sql/plan.
Merge Queue Status
This pull request spent 35 minutes 32 seconds in the queue, including 35 minutes 16 seconds running CI. Required conditions to merge
|
What type of PR is this?
Which issue(s) this PR fixes:
Closes #26328
What this PR does / why we need it:
COPY-mode
ALTER TABLEreplaces a relation with a new physical table. The old implementation could lose or split foreign-key metadata while doing that:mo_catalog.mo_foreign_keysrows owned by the source child were deleted before the replacement inherited them;FOREIGN_KEY_CHECKS=0were absent fromTableDef, so reconstructing the temporary table could not recreate them;RefChildTableDefvalues whose add/remove/replace paths disagreed;SHOW CREATE TABLEbut lacked the catalog dependency used by clone and restore ordering.This PR:
RefChildTableDef, deduplicates IDs, preserves the self-reference sentinel0, and is also used byTRUNCATE TABLE;mo_foreign_keysfor 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;MO_TSunder 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 reportedmo_snapshotsmissing;The generated
clone_fk_after_alterresult was manually audited: all 17 recorded errors are intentional FK enforcement checks; there are no unexpected errors.Validation
./pkg/frontend ./pkg/sql/compile ./pkg/sql/plan) pass with the repository CGo test environment;clone_fk_after_alter: 240/240;snapshot_restore_to_table_fk_2: 484/484;restore_dropped_account_to_new: 20/20;restore_dropped_account_with_account_sp: 34/34;restore_database_using_cluster_sp: 557/557;1ca928aefdreproduced the old corruption (mo_foreign_keyscount changed from 1 to 0 after COPY ALTER whileSHOW CREATE TABLEretained the FK);pkg/frontendtests pass.