Repository navigation
Description
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
MERGEandPICK. - Direct control:
update child_t set note = 'direct-control' where id = 2succeeds 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
- [Bug]: DATA BRANCH CREATE DATABASE rejects valid cyclic foreign-key schemas #26111 covers database-branch creation for cyclic foreign keys; it does not cover replay of branch updates into a referenced row.
Activity
Ariznawlll commentedon Sep 2, 2026
补充范围:当分支目标表仅有“指向父表”的外键时,
DATA BRANCH MERGE的同类非键列更新连续 3 次均写入目标值;触发条件是该目标表的主键被另一张表引用。也就是说,问题不依赖循环外键,且不等同于“任意带外键的表不能 MERGE”。Ariznawlll commentedon Sep 2, 2026
补充约束范围(同一台单 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 的约束处理语义。
Ariznawlll commentedon Sep 2, 2026
同一提交中的原子性也已复核:分支同时修改
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 次一致;当前未观察到前序可应用行被半量写入。gouhongshen commentedon Sep 2, 2026
PR #27996 is trying to fix this issue.
fix(frontend): apply Data Branch updates in place (#27996)
gouhongshen commentedon Sep 3, 2026
PR #27996 has merged and is awaiting manual validation. The issue remains open.
16 remaining items
gouhongshen commentedon Oct 23, 2026
Investigation update: defect does not reproduce on current main
A fresh investigation on current
main(commite7ea085fff) 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(tagv4.2.1-hotfix-20260916, 4.2 hotfix line). That tree contains none of the #27996 fix:git grep stagedUpdateSQL / dataBranchStagesUpdate / dataBranchDirectUpdateBatchat9572bd3311has 0 hits - the in-place update apply does not exist in that tree;merge_foreign_key_update.sql/pick_foreign_key_update.sqlare absent at that commit;24d0d6b8ffis not an ancestor of9572bd3311.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:
9572bd3311(v4.2.1-hotfix-20260916): bothDATA BRANCH MERGE ... WHEN CONFLICT ACCEPTandDATA BRANCH PICK ... KEYS(2) WHEN CONFLICT ACCEPTfail withERROR 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.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:
merge_foreign_key_update.sql16/16,pick_foreign_key_update.sql20/20 (mo-tester, methodrun);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;TestDataBranchPick/TestForcedMultiCNDeleteAndInsertIgnoreinpkg/tests/dmlall pass.Conclusion and remaining work
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.9572bd3311). Closing this issue for releases requires backporting fix(frontend): apply Data Branch updates in place #27996 into that line.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
deferredon 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.gouhongshen commentedon Oct 23, 2026
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).
slyunnn commentedon Nov 19, 2026
Partially fixed: the original MERGE/PICK reproduction passes on
main, but remains broken on4.2-dev.2 CN / 1 TN / 1 Log / Proxy; the original SQL was executed through the MySQL protocol (PyMySQL) on Proxy6001, CN116001, and CN216002.mainatae9e665f3093094ba3286e72bf1af8e111463180(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 passed16/16and20/20through Proxy.4.2-devat71e22339c8cca62b2f29f9fd6e686e653a1ff2d2([4.2-dev] fix(frontend): preserve string literals in merged role rules (#32891)). On all three endpoints, both statements below returned20101: Cannot delete or update a parent row: a foreign key constraint fails, despite changing onlynote: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.slyunnn commentedon Nov 19, 2026
QA correction: target branch
The original report targets
main, and PR #27996 (24d0d6b8fff0108efd4f41a048762e1698298688) was merged intomain. My preceding comment incorrectly treated a separate4.2-devobservation as an acceptance requirement for this issue. No backport requirement or fix on4.2-devwas established by this issue; the4.2-devresult 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
mainatae9e665f3093094ba3286e72bf1af8e111463180, the original MERGE/PICK reproduction passed through Proxy and both CNs; the existing product MERGE and PICK BVT passed16/16and20/20through Proxy. These are the checks actually performed, not a claim that every outstanding main acceptance case is complete. QA will assess the issue against itsmainscope.slyunnn commentedon Nov 19, 2026
Partially fixed on the issue's target branch
main: non-key updates now work, but theON UPDATE CASCADEprimary-key case recorded in the issue discussion still fails.mainatae9e665f3093094ba3286e72bf1af8e111463180(perf(exec): reduce accounted work on short index range scans (#32895)), isolated2 CN / 1 TN / 1 Log / Proxy; MySQL protocol via PyMySQL on Proxy6001, CN116001and CN216002.24d0d6b8fff0108efd4f41a048762e1698298688) allows the original referenced-row non-key MERGE/PICK on all three endpoints. Existing product MERGE and PICK BVT passed16/16and20/20through Proxy.Expected: the same key change as ordinary
UPDATE t SET id=3 WHERE id=2succeeds and cascadesr.tidto3. Actual: both branch operations return1451: Cannot delete or update a parent row: a foreign key constraint fails; target remains(1,'one'),(2,'two')andrremains(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 CASCADEsemantics 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 earlier4.2-devobservation is outside this issue's established target scope, as corrected separately.