Skip to content

[Bug]: Data Branch MERGE/PICK rejects non-key updates on referenced rows #27992

Open

Description

@Ariznawlll

Description

DATA BRANCH MERGE and DATA BRANCH PICK cannot apply a branch-side update that changes only a non-key column when another table has a foreign key to the destination row. The same ordinary UPDATE succeeds. Both Data Branch operations reject the change before modifying the destination.

Environment

  • Branch: main
  • Commit: 33af6ae3c0c23e4f277137460577ea64678f7ecf
  • Deployment: fresh local single-CN / single-TN / single-Log cluster
  • Date: 2026-09-02

Steps to reproduce

create database branch_fk_apply;
use branch_fk_apply;
create table child_t (id int primary key, note varchar(32));
create table parent_t (
  id int primary key,
  child_id int,
  constraint fk_parent_child foreign key (child_id) references child_t(id)
);
insert into child_t values (1,'one'),(2,'two');
insert into parent_t values (1,2);

data branch create table leaf_merge from child_t;
update leaf_merge set note = 'leaf-merge' where id = 2;
data branch merge leaf_merge into child_t when conflict accept;

data branch create table leaf_pick from child_t;
update leaf_pick set note = 'leaf-pick' where id = 2;
update child_t set note = 'direct-control' where id = 2;
data branch pick leaf_pick into child_t keys(2) when conflict accept;

Actual behavior

Both MERGE and PICK return:

internal error: Cannot delete or update a parent row: a foreign key constraint fails

After the failed MERGE, child_t.id=2 remains two; after the failed PICK, it remains direct-control. No partial destination row change was observed.

Expected behavior

Changing only child_t.note must be applicable through Data Branch just as it is through ordinary UPDATE; the foreign key references child_t.id, which is unchanged. MERGE should leave leaf-merge and PICK should apply leaf-pick under WHEN CONFLICT ACCEPT.

Stability and controls

  • Minimal reproducer: 3/3 for both MERGE and PICK.
  • Direct control: update child_t set note = 'direct-control' where id = 2 succeeds in all three runs.
  • Non-FK Data Branch control: the same branch-side non-key update on a table without a referencing parent merges successfully in all three runs.
  • Atomicity: each rejected operation leaves the destination value unchanged.

Code analysis

The update apply path materializes ordinary updates as a diffDelete followed by diffInsert: appendDataBranchApplyRowAsSQLValues writes the two kinds separately in pkg/frontend/data_branch_output.go, and tryFlushDeletesOrInserts flushes deletes before inserts. Deleting the referenced child_t row violates parent_t.child_id -> child_t.id even though the user-visible operation does not modify id. The exact-key UPDATE path is restricted to FLOAT/DOUBLE key handling, so an integer primary key uses delete/insert replay.

Regression coverage

Add a deterministic Data Branch regression in test/distributed/cases/git4data/branch/merge and the corresponding PICK suite: a table referenced by a foreign key, a branch-side non-key update, MERGE/PICK with WHEN CONFLICT ACCEPT, direct-UPDATE control, and destination-value assertions.

Related

Activity

  1. added this to the MO-202609 milestone on Sep 2, 2026
  2. added
    kind/bug
    on Sep 2, 2026
  3. added theissue type on Sep 2, 2026
  4. Ariznawlll commented on Sep 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充范围:当分支目标表仅有“指向父表”的外键时,DATA BRANCH MERGE 的同类非键列更新连续 3 次均写入目标值;触发条件是该目标表的主键被另一张表引用。也就是说,问题不依赖循环外键,且不等同于“任意带外键的表不能 MERGE”。

  5. Ariznawlll commented on Sep 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充约束范围(同一台单 CN 本地 main,33af6ae3c0c23e4f277137460577ea64678f7ecf):

    目标表的主键被子表以 FOREIGN KEY ... ON UPDATE CASCADE 引用时,直接执行 UPDATE parent_t SET id = 3 WHERE id = 2 会将 child_t.parent_id 同步更新为 3。将完全相同的主键变更写入分支表后,DATA BRANCH MERGE 和 DATA BRANCH PICK 都在应用时返回 Cannot delete or update a parent row: a foreign key constraint fails;父表仍为 id=2,子表仍引用 2。

    该组合连续执行 3 次,现象一致。它表明问题不仅影响被引用行的非键字段更新,也影响本应由普通 UPDATE 触发级联语义的主键更新;MERGE/PICK 没有保留目标表原生 UPDATE 的约束处理语义。

  6. Ariznawlll commented on Sep 2, 2026

    @Ariznawlll
    CollaboratorAuthor

    同一提交中的原子性也已复核:分支同时修改 child_t.id=1(未被引用)和 child_t.id=2(被 parent_t 引用)的非键字段时,MERGE 在 id=2 的外键错误处结束后,目标仍为 1:one|2:two,分支仍为 1:branch-one|2:branch-two,引用关系保持 1 条。连续 3 次一致;当前未观察到前序可应用行被半量写入。

  7. added
    severity/s0
    ai-easy
    and removed on Sep 2, 2026
  8. gouhongshen commented on Sep 2, 2026

    @gouhongshen
    Contributor

    PR #27996 is trying to fix this issue.

  9. gouhongshen commented on Sep 3, 2026

    @gouhongshen
    Contributor

    PR #27996 has merged and is awaiting manual validation. The issue remains open.

  10. removed their assignment
    on Sep 16, 2026
  11. 16 remaining items

  12. added
    ai-easy
    on Oct 22, 2026
  13. gouhongshen commented on Oct 23, 2026

    @gouhongshen
    Contributor

    Investigation update: defect does not reproduce on current main

    A fresh investigation on current main (commit e7ea085fff) shows this defect is already fixed there by #27996, and explains why the 2026-09-16 re-validation still failed.

    Why the 2026-09-16 validation failed

    That validation ran commit 9572bd3311abb16d1df939956408a727bcf83eba (tag v4.2.1-hotfix-20260916, 4.2 hotfix line). That tree contains none of the #27996 fix:

    • git grep stagedUpdateSQL / dataBranchStagesUpdate / dataBranchDirectUpdateBatch at 9572bd3311 has 0 hits - the in-place update apply does not exist in that tree;
    • the FK regression files merge_foreign_key_update.sql / pick_foreign_key_update.sql are absent at that commit;
    • fix commit 24d0d6b8ff is not an ancestor of 9572bd3311.

    So the re-validation result is branch-mismatch evidence: it re-tested the pre-fix code of a release hotfix line, not the merged fix. The multi-CN topology was not the differentiator.

    Executable proof (same driver, two binaries built from the two commits)

    Both clusters were local, freshly initialized, single-CN/TN/Log, running the exact SQL from this issue's description:

    • RED, at 9572bd3311 (v4.2.1-hotfix-20260916): both DATA BRANCH MERGE ... WHEN CONFLICT ACCEPT and DATA BRANCH PICK ... KEYS(2) WHEN CONFLICT ACCEPT fail with
      ERROR 20101 (HY000): internal error: Cannot delete or update a parent row: a foreign key constraint fails,
      destinations unchanged (two / direct-control). This reproduces the issue exactly, on the commit the failed validation used.
    • GREEN, at e7ea085fff (current main): the same statements succeed. After MERGE: child_t = (1,'one'),(2,'leaf-merge'); after PICK: (1,'one'),(2,'leaf-pick'); parent_t = (1,2) preserved in both cases.

    Additional checks on current main:

    • committed regression suites pass: merge_foreign_key_update.sql 16/16, pick_foreign_key_update.sql 20/20 (mo-tester, method run);
    • genuine FK-violating operations are still rejected atomically: a branch-side DELETE of the referenced row through MERGE, and the same through PICK keys(2), both return the FK error and leave all destination rows unchanged, while deleting an unreferenced row through MERGE applies normally - so the in-place apply did not weaken FK enforcement;
    • focused frontend unit tests for the apply boundary (direct update SQL, staged update flush, generated-PK staging, special ODKU updates, mixed-schema updates, flush ordering, no-PK delete modes, conflict-accept expand-update, native-update split) and the fix(frontend): apply Data Branch updates in place #27996 integration tests TestDataBranchPick / TestForcedMultiCNDeleteAndInsertIgnore in pkg/tests/dml all pass.

    Conclusion and remaining work

    • No further change is needed or proposed for main: fix(frontend): apply Data Branch updates in place #27996's in-place update apply is present at HEAD and covers this issue's exact reproduction.
    • The defect still reproduces on any build from the 4.2 hotfix line (proven above at 9572bd3311). Closing this issue for releases requires backporting fix(frontend): apply Data Branch updates in place #27996 into that line.
    • Request to the reporter: re-run the acceptance checklist against a build that contains the fix (current main, or a 4.2 build after backport). The Sep-16 run tested a fix-less hotfix binary, so its rejection does not reflect the merged fix.

    This issue is moved to deferred on the AI-fix loop: the assigned target (main) needs no patch, the remaining work is a backport/acceptance decision that belongs to the maintainers of the 4.2 line.

  14. added and removed
    severity/s0
    on Oct 23, 2026
  15. gouhongshen commented on Oct 23, 2026

    @gouhongshen
    Contributor

    Existing-fix audit

    Assessment: already fixed on current main

    Fixed evidence

    PR #27996 merged into main on 2026-09-03 as 24d0d6b: #27996. Dedicated proof artifacts: test/distributed/cases/git4data/branch/merge/merge_foreign_key_update.sql and .result; branch/pick/pick_foreign_key_update.sql and .result. Existing logs under /Users/ghs-mo/MOWorkSpace/tmp/issue-fix/issue-fix-loop/state/artifacts/issue-27992/logs/ show the exact reproduction passing on main e7ea085, with MERGE setting leaf-merge, PICK setting leaf-pick, and referencing rows preserved. bvt-merge.log:141 records 16/16 for the dedicated MERGE case; bvt-pick.log:841 records 20/20 for the dedicated PICK case. Supporting investigation: #27992 (comment).

  16. slyunnn commented on Nov 19, 2026

    @slyunnn

    Partially fixed: the original MERGE/PICK reproduction passes on main, but remains broken on 4.2-dev.

    • Environment: isolated 2 CN / 1 TN / 1 Log / Proxy; the original SQL was executed through the MySQL protocol (PyMySQL) on Proxy 6001, CN1 16001, and CN2 16002.
    • Fixed scope: main at ae9e665f3093094ba3286e72bf1af8e111463180 (perf(exec): reduce accounted work on short index range scans (#32895)) contains fix(frontend): apply Data Branch updates in place #27996 (24d0d6b8fff0108efd4f41a048762e1698298688). MERGE and PICK applied the branch-side non-key change on all three endpoints with the foreign-key reference intact. Product MERGE and PICK BVT passed 16/16 and 20/20 through Proxy.
    • Remaining scope: 4.2-dev at 71e22339c8cca62b2f29f9fd6e686e653a1ff2d2 ([4.2-dev] fix(frontend): preserve string literals in merged role rules (#32891)). On all three endpoints, both statements below returned 20101: Cannot delete or update a parent row: a foreign key constraint fails, despite changing only note:
    create table child_t (id int primary key, note varchar(32));
    create table parent_t (id int primary key, child_id int,
      foreign key (child_id) references child_t(id));
    insert into child_t values (1,'one'),(2,'two');
    insert into parent_t values (1,2);
    data branch create table leaf_merge from child_t;
    update leaf_merge set note='leaf-merge' where id=2;
    data branch create table leaf_pick from child_t;
    update leaf_pick set note='leaf-pick' where id=2;
    update child_t set note='direct-control' where id=2;
    data branch merge leaf_merge into child_t when conflict accept;
    data branch pick leaf_pick into child_t keys(2) when conflict accept;

    Expected: both operations apply the non-key update, preserving the reference. Actual: both fail; the ordinary UPDATE succeeds and destination remains (1,'one'),(2,'direct-control'), with the parent reference unchanged after failure. The test was serial; no production performance or parallel execution claim is made.

    Please investigate/backport the in-place Data Branch update apply to 4.2-dev, retaining real FK enforcement and failure atomicity. The issue depends on update semantics rather than data scale, so large-data testing is not needed for this reproduction.

  17. slyunnn commented on Nov 19, 2026

    @slyunnn

    QA correction: target branch

    The original report targets main, and PR #27996 (24d0d6b8fff0108efd4f41a048762e1698298688) was merged into main. My preceding comment incorrectly treated a separate 4.2-dev observation as an acceptance requirement for this issue. No backport requirement or fix on 4.2-dev was established by this issue; the 4.2-dev result is an observation only, not grounds to classify the main-targeted fix as partially fixed. The earlier suggestion to backport should not be read as an agreed release requirement.

    On main at ae9e665f3093094ba3286e72bf1af8e111463180, the original MERGE/PICK reproduction passed through Proxy and both CNs; the existing product MERGE and PICK BVT passed 16/16 and 20/20 through Proxy. These are the checks actually performed, not a claim that every outstanding main acceptance case is complete. QA will assess the issue against its main scope.

  18. slyunnn commented on Nov 19, 2026

    @slyunnn

    Partially fixed on the issue's target branch main: non-key updates now work, but the ON UPDATE CASCADE primary-key case recorded in the issue discussion still fails.

    • Environment: main at ae9e665f3093094ba3286e72bf1af8e111463180 (perf(exec): reduce accounted work on short index range scans (#32895)), isolated 2 CN / 1 TN / 1 Log / Proxy; MySQL protocol via PyMySQL on Proxy 6001, CN1 16001 and CN2 16002.
    • Fixed scope: fix(frontend): apply Data Branch updates in place #27996 (24d0d6b8fff0108efd4f41a048762e1698298688) allows the original referenced-row non-key MERGE/PICK on all three endpoints. Existing product MERGE and PICK BVT passed 16/16 and 20/20 through Proxy.
    • Remaining reproduction on every endpoint:
    create table t(id int primary key,note varchar(20));
    create table r(id int primary key,tid int,
      foreign key(tid) references t(id) on update cascade);
    insert into t values (1,'one'),(2,'two');
    insert into r values (1,2);
    data branch create table bm from t;
    data branch create table bp from t;
    update bm set id=3 where id=2;
    update bp set id=3 where id=2;
    data branch merge bm into t when conflict accept;
    data branch pick bp into t keys(2,3) when conflict accept;

    Expected: the same key change as ordinary UPDATE t SET id=3 WHERE id=2 succeeds and cascades r.tid to 3. Actual: both branch operations return 1451: Cannot delete or update a parent row: a foreign key constraint fails; target remains (1,'one'),(2,'two') and r remains (1,2). The ordinary UPDATE control did cascade successfully. Real deletion of the referenced row remained rejected and atomic through MERGE and PICK.

    Please investigate Data Branch apply of primary-key changes so native ON UPDATE CASCADE semantics are preserved without weakening foreign-key enforcement. This is a key-update semantics issue; large-data testing is unnecessary to reproduce it. Tests were serial; no production-scale or engine-parallel result is claimed. The earlier 4.2-dev observation is outside this issue's established target scope, as corrected separately.

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

Metadata

Metadata

Assignees

Labels

ai-easydeferredkind/bugneeds-triage

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions