Repository navigation
Description
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, CN16001, CN16002 - 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, andON 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
- [Subtask]: Apply parent-side foreign key actions during REPLACE #24951 tracks parent-side foreign-key semantics for REPLACE.
- fix(replace): apply parent-side foreign key actions during REPLACE #25089 implemented those actions, including prepared input and CASCADE/SET
NULL. This reproduction is a distinct indexed-plan panic in that path.
Activity
fix: maintain indexes for REPLACE foreign key actions (#27062)
Ariznawlll commentedon Aug 23, 2026
测试版本
main @ 11a1301a5TestIssue26875全量回归 2 次,均通过。验证内容
ON DELETE CASCADE和ON DELETE SET NULL。REPLACE/级联变更后索引一致性与子表数据结果。回归用例
结论:最新 main 下 Happy Path 和关联完整性异常场景均通过,问题已修复。
fix: maintain indexes for REPLACE foreign key actions (matrixorigin#2…
fix: maintain indexes for REPLACE foreign key actions (matrixorigin#2…
fix: maintain indexes for REPLACE foreign key actions (matrixorigin#2…