Skip to content

[Bug]: Prepared DML hangs on second execute when foreign-key actions are triggered #25419

Closed

Description

@Ariznawlll

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Searched open issues for:

  • prepared foreign key cascade hang
  • "PREPARE" "foreign key"
  • "ON UPDATE CASCADE" "prepared"
  • "ON DELETE SET NULL" "prepared"

No matching open issue was found.

Branch Name

main

Commit ID

51656deb6003

Other Environment Information

  • MatrixOne: select version() returns 8.0.30-MatrixOne-v1.3.0
  • MySQL comparison: 8.0.45
  • Tested through the MySQL client with server-side prepared statements.

Actual Behavior

A prepared DML statement that triggers a foreign-key action hangs on the second EXECUTE.

Observed cases:

  1. ON UPDATE CASCADE with a composite foreign key:

    • First EXECUTE updates parent (1,'a1') -> (1,'a1x') and cascades to child rows.
    • Second EXECUTE for parent (2,'b1') -> (2,'b1x') does not return within 30 seconds.
    • After killing only the mysql client, the data remains at the state after the first execute: (2,'b1') is unchanged in both parent and child tables.
  2. ON DELETE SET NULL:

    • First EXECUTE deletes parent 100 and sets child parent_id to NULL.
    • Second EXECUTE for parent 200 does not return within 30 seconds.
    • After killing only the mysql client, parent 200 and its child reference remain unchanged.

Control checks on the same MatrixOne build:

  • The same ON UPDATE CASCADE statements executed as normal SQL literals finish successfully.
  • The same ON DELETE SET NULL statements executed as normal SQL literals finish successfully.
  • The prepared hang still reproduces when the second parameter uses the same integer type, so this is not only a string-to-int coercion issue.

Expected Behavior

Same as MySQL: repeated EXECUTE of the prepared statement should finish normally, and each foreign-key action should be applied.

For ON UPDATE CASCADE, MySQL returns:

tenant  acct  status
1       a1x   open
2       b1x   hold

tenant  acct  child_id  note
1       a1x   10        c-a1-10
2       b1x   20        c-b1-20

For ON DELETE SET NULL, MySQL returns:

child_id  parent_id  note
1         NULL       s100-a
2         NULL       s200-a

Steps to Reproduce

Case 1: prepared parent update with ON UPDATE CASCADE

drop database if exists mysql_compat_model14_prepare_fk_upd_same_type;
create database mysql_compat_model14_prepare_fk_upd_same_type;
use mysql_compat_model14_prepare_fk_upd_same_type;

create table parent_cascade (
  tenant int not null,
  acct varchar(20) not null,
  status varchar(10),
  primary key (tenant, acct)
);

create table child_cascade (
  tenant int not null,
  acct varchar(20) not null,
  child_id int not null,
  note varchar(30),
  primary key (tenant, acct, child_id),
  constraint fk_child_cascade foreign key (tenant, acct)
    references parent_cascade(tenant, acct)
    on update cascade
    on delete cascade
);

insert into parent_cascade values (1,'a1','open'),(2,'b1','hold');
insert into child_cascade values (1,'a1',10,'c-a1-10'),(2,'b1',20,'c-b1-20');

prepare s_parent_upd from 'update parent_cascade set acct = ? where tenant = ? and acct = ?';
set @new_acct='a1x', @tenant=1, @old_acct='a1';
execute s_parent_upd using @new_acct,@tenant,@old_acct;
set @new_acct='b1x', @tenant=2, @old_acct='b1';
execute s_parent_upd using @new_acct,@tenant,@old_acct;

The second execute hangs on MatrixOne.

Case 2: prepared parent delete with ON DELETE SET NULL

drop database if exists mysql_compat_model14_prepare_fk_same_type;
create database mysql_compat_model14_prepare_fk_same_type;
use mysql_compat_model14_prepare_fk_same_type;

create table parent_setnull (
  id int primary key,
  code varchar(20) unique
);

create table child_setnull (
  child_id int primary key,
  parent_id int,
  note varchar(30),
  key idx_parent(parent_id, note),
  constraint fk_child_setnull foreign key (parent_id)
    references parent_setnull(id)
    on delete set null
);

insert into parent_setnull values (100,'p100'),(200,'p200');
insert into child_setnull values (1,100,'s100-a'),(2,200,'s200-a');

prepare s_parent_del from 'delete from parent_setnull where id = ?';
set @pid=100;
execute s_parent_del using @pid;
set @pid=200;
execute s_parent_del using @pid;

The second execute hangs on MatrixOne.

Additional information

This was found during MySQL compatibility exploration for prepared statements with composite primary keys / foreign keys / secondary indexes. The behavior is important for BVT unhappy-path coverage because normal non-prepared SQL works while repeated prepared execution does not.

Activity

  1. added this to the MO-202607 milestone on Jul 3, 2026
  2. added
    kind/bug
    on Jul 3, 2026
  3. added theissue type on Jul 3, 2026
  4. added
    severity/s0
    ai-easy
    and removed on Jul 8, 2026
  5. LeftHandCold commented on Jul 13, 2026

    @LeftHandCold
    Collaborator

    fixed

  6. Ariznawlll commented on Jul 13, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 上完成验证,并补充 motr 回归测试。

    验证环境

    • MatrixOne:main@52afd25fa63a3cb6037b7bf8ff0bf1328c4793ec
    • motr 用例:issue_25419_prepared_fk_actions
    • 回归 PR:matrixorigin/motr#48

    测试过程

    分别验证两个外键动作路径:

    1. 复合外键 ON UPDATE CASCADE;
    2. 单列外键 ON DELETE SET NULL。

    每个 prepared statement 都执行三次:第一次命中并触发外键动作,中间一次不命中任何行,第三次再次命中并触发外键动作。同时将每个父行扩展为关联多个子行,最后查询父表和子表确认全部级联结果。

    验证结果

    • 三次 EXECUTE 均正常返回,没有再次出现第二次执行卡住。
    • 复合外键更新正确级联到同一父行的多个子行。
    • DELETE 后多个子行的外键均正确设置为 NULL。
    • 最终父子表数据与预期一致。
    • 连续执行 5 轮均通过。
    SUMMARY | total=50 success=50 failed=0 skipped=0 | stmts=625 ok=625 fail=0
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions