Repository navigation
fix: avoid redundant foreign key rewrites during alter table - #26790
Conversation
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
left a comment
There was a problem hiding this comment.
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, androaring.hare unavailable;CGO_ENABLED=0also cannot build the transitive dependencies. - Reviewed the new COPY FK reconciliation and
ConstraintDef.MarshalBinarywire-compatibility changes; no additional actionable issue was found.
06dd7c5 to
9e575f2
Compare
gouhongshen
left a comment
There was a problem hiding this comment.
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_26465changes a child FK columnv -> V, then its parent referenced columnv -> V, and verifiesinformation_schema.KEY_COLUMN_USAGEchanges fromV/vtoV/V.
Validation: go test ./pkg/sql/plan -count=1; rebased-head BVT 48/48; git diff --check.
XuPeng-SH
left a comment
There was a problem hiding this comment.
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.
Merge Queue Status
This pull request spent 12 seconds in the queue, including 2 seconds running CI. Required conditions to merge
|
…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
…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
…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
What type of PR is this?
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 ... CHANGEpreviously 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:
CHANGE COLUMNoperations through the existing in-placeReplaceDefpath;CHANGE COLUMNrenames on COPY, so both foreign-key catalog columns retain their original spelling;ConstraintDef.MarshalBinaryintermediate allocations while preserving the encoded wire format;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_USAGEtransitions fromV/vtoV/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.departments: 768 indexes, 349 ref-child IDsusers: 732 indexes, 349 FKs to one parentcandidates: 702 FKs across three parentsRun with:
Validation:
go test ./pkg/sql/plan -count=1go test ./pkg/sql/compile -run "^(TestAddChildTableIDToDistinctParents|TestReconcileAlterCopyForeignKeyReferencesOncePerRelation|TestRewriteForeignKeyReferencesForAlterCopy|TestReconcileRefChildTableIDForAlterCopy)$" -count=1go test ./pkg/vm/engine -run "^(TestConstraintDefMarshalBinaryWireCompatibility|TestIssue26465QAConstraintFixture)$" -count=1go test ./pkg/embed -run "^TestIssue26465AlterCopyPreservesParentReference$" -count=1mo-testerrun offoreign_key/issue_26465: 48/48 passed on the rebased headgit diff --check