Skip to content

[Bug]: REPLACE panics with indexed CASCADE/SET NULL child foreign key #26875

Closed

Description

@Ariznawlll

Description

REPLACE panics during planning when a referencing child table has both an
explicit secondary index on its foreign-key column and an ON DELETE CASCADE
or ON DELETE SET NULL action. The tables may be empty; the failure does not
depend on finding a conflicting parent row.

Environment

  • Branch: main
  • Commit: 415724ff30cfb1dd96fa2eb75110402fbc4e7239
  • Deployment: local mo-service, 2 CN / 1 TN / 1 Log service / Proxy
  • Tested endpoints: Proxy 6001, CN 16001, CN 16002
  • Date: 2026-08-09

Steps to reproduce

drop database if exists replace_fk_index_repro;
create database replace_fk_index_repro;
use replace_fk_index_repro;

create table p(
  id int primary key,
  note varchar(20)
);

create table c(
  id int primary key,
  pid int not null,
  index idx_pid(pid),
  constraint fk foreign key(pid) references p(id) on delete cascade
);

-- Both fail even though p and c are empty.
prepare r from 'replace into p values(?,?)';
replace into p values(1,'new');

Actual behavior

Both SQL statements return a recovered planner panic:

Error 20101 (HY000): internal error: panic runtime error:
index out of range [0] with length 0

github.com/matrixorigin/matrixone/pkg/sql/plan.(*QueryBuilder).mergeFiltersOnCompositeKey
    pkg/sql/plan/expr_opt.go:45
github.com/matrixorigin/matrixone/pkg/sql/plan.bindAndOptimizeReplaceQuery
    pkg/sql/plan/build.go:218

SELECT COUNT(*) FROM p remains 0, so the failed statement does not leave
partial data.

Expected behavior

The prepared statement should be created successfully. The literal REPLACE
should insert the parent row; when it replaces an existing parent, the configured
child delete action should be applied atomically.

Stability and controls

  • Reproduced 3/3 through the Proxy and directly through both CNs.
  • Ordinary literal REPLACE, SQL PREPARE, and MySQL binary prepare all hit the
    same planner panic.
  • ON DELETE CASCADE, combined UPDATE/DELETE CASCADE, and ON DELETE SET NULL
    reproduce.
  • Empty parent/child tables reproduce; no data-dependent conflict is required.
  • The same schema without the explicit child secondary index passes.
  • RESTRICT/NO ACTION and an UPDATE-only CASCADE do not hit the planner panic.
  • With the explicit index present, prepared DELETE correctly cascades and ODKU
    correctly updates the parent in place; the failure is specific to REPLACE.
  • A failed REPLACE leaves parent and child rows unchanged.
  • Dropping the child index makes the same cached binary prepared REPLACE recover.
    Adding the index again makes it fail again. This lifecycle was repeated 3/3.

Code analysis

The FK delete-action branch creates a TABLE_SCAN node with an empty
BindingTags slice. optimizeFilters later calls
mergeFiltersOnCompositeKey, which unconditionally evaluates
node.BindingTags[0] for every TABLE_SCAN that has a primary key. The explicit
child index changes the REPLACE FK-action plan shape so this internal scan
reaches that optimizer path.

The immediate safety invariant is that optimizer scan rewrites must not index
an empty BindingTags slice. The REPLACE/FK planner should also preserve the
binding tag expected by downstream filter rewrites.

Regression coverage

Add public BVT coverage for literal and prepared REPLACE with:

  • an explicit child index on the FK column;
  • CASCADE and SET NULL delete actions;
  • empty tables and an actual conflicting parent row;
  • failure atomicity and child-index correctness;
  • add/drop/re-add index lifecycle for an already prepared REPLACE.

Add a focused planner test asserting that all TABLE_SCAN nodes produced for
parent-side FK action branches have safe binding metadata, or that
mergeFiltersOnCompositeKey skips scans without binding tags.

Related

Activity

  1. added
    kind/bug
    on Aug 9, 2026
  2. added this to the MO-202608 milestone on Aug 9, 2026
  3. added theissue type on Aug 9, 2026
  4. added
    severity/s0
    and removed on Aug 10, 2026
  5. Ariznawlll commented on Aug 23, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • MatrixOne main @ 11a1301a5
    • 执行 TestIssue26875 全量回归 2 次,均通过。

    验证内容

    • 带索引的外键表覆盖 ON DELETE CASCADE 和 ON DELETE SET NULL。
    • 覆盖普通 SQL 字面量、二进制协议预处理参数、唯一索引、父子表生命周期、自引用及多级级联关系。
    • 验证 REPLACE/级联变更后索引一致性与子表数据结果。

    回归用例

    结论:最新 main 下 Happy Path 和关联完整性异常场景均通过,问题已修复。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions