Skip to content

[Bug]: indexed ON DELETE CASCADE panics when target has no children but unrelated child rows exist #29954

Closed

Description

@dengn

Existing issue check

Searched for CASCADE + empty/matching + ColumnExpressionExecutor/panic. Related closed issues #27539 (indexed ON DELETE SET NULL with matching child rows) and #26875 (REPLACE planner panic) do not describe this current-head DELETE/CASCADE empty-match case.

Environment

  • Branch: main
  • Commit: dd38098
  • Freshly compiled binary; SELECT version(), git_version(): 8.0.30-MatrixOne-v; dd38098.
  • Local isolated LOG/TN/CN instance, PyMySQL 1.2.3.
  • MySQL control: official mysql:8.4 image.
  • Observed through OpenAI Agents SDK 0.23.1 SQLAlchemySession.clear_session after making its String session_id declarations MySQL-compatible (VARCHAR(255)).

Minimal reproduction

Use a new database/table namespace:

CREATE TABLE p(id VARCHAR(255) PRIMARY KEY);
CREATE TABLE c(
  id INT PRIMARY KEY,
  pid VARCHAR(255),
  KEY idx_pid(pid),
  FOREIGN KEY(pid) REFERENCES p(id) ON DELETE CASCADE
);
INSERT INTO p VALUES('s'),('other');
INSERT INTO c VALUES(2,'other');

-- Target 's' has no child rows, while the child table is nonempty.
DELETE FROM p WHERE id='s';

Actual behavior

The DELETE returns a recovered internal executor panic:

ERROR 20101: internal error: panic runtime error:
index out of range [0] with length 0
github.com/matrixorigin/matrixone/pkg/sql/colexec.(*ColumnExpressionExecutor).Eval
    pkg/sql/colexec/evalExpression.go:1955
github.com/matrixorigin/matrixone/pkg/sql/colexec/projection.(*Projection).Call
    pkg/sql/colexec/projection/projection.go:108
...
github.com/matrixorigin/matrixone/pkg/sql/colexec/deletion.(*Deletion).normalDelete

The server continues responding to SELECT 1; this report claims a recovered statement panic, not a process crash.

Expected behavior

Delete parent 's' with no error. Parent 'other' and its child should remain unchanged. MySQL 8.4 succeeds with the identical fixture.

Repeated controls and scope

Each combination used fresh tables and was repeated three times:

  • Explicit single-column child index + no matching child rows: panic 3/3.
  • Explicit composite child index on (pid,note) + no matching child rows: panic 3/3.
  • Insert target child, DELETE it first in the same transaction, then DELETE parent while an unrelated child remains: panic 3/3 for each index shape.
  • No explicit child secondary index: succeeds for empty-match, delete-child-first and matching-child CASCADE.
  • Matching target children with either index shape: CASCADE succeeds.
  • Indexed RESTRICT: succeeds when there are no target children, correctly rejects when matching children exist.
  • Indexed child table without FK: ordinary DELETE succeeds.
  • MySQL 8.4 controls: all CASCADE combinations succeed; RESTRICT correctly rejects matching children.
    A 90-case matrix covers two backends, five FK/index variants, three child-match modes and three repeats.

Failure atomicity checks

After rollback:

  • Initially empty target: both parent rows and the unrelated child remain (parent count 2, child count 1).
  • Delete-child-first transaction: rollback restores target child too (parent count 2, child count 2).
    SELECT 1 succeeds after every attempt.

Application impact and verified workaround

OpenAI Agents SQLAlchemySession clears the target's messages before deleting its parent session row, leaving other sessions' message rows in the child table. Its explicit (session_id,created_at) index exposes this path.

A scoped schema adapter omitting only the explicit child secondary index, while retaining the foreign key and ON DELETE CASCADE, passed the SDK add/read, reconstructed-session readback, cross-session isolation and clear tests on MatrixOne and MySQL. This workaround can slow history lookup and is not a database fix.

Suspected area and regression request

The observed differentiator is an indexed FK delete-action projection with zero matching child rows but a nonempty child scan. Root cause before ColumnExpressionExecutor.Eval is not established.

Add literal and bound-parameter DELETE regressions for simple/composite child indexes, empty target matches with unrelated child rows, child-delete-first transactions, matching CASCADE, and rollback atomicity. Keep no-index and RESTRICT controls.

Activity

  1. added
    kind/bug
    on Oct 12, 2026
  2. added a commit that references this issue on Oct 14, 2026
    5d971d0
  3. Ariznawlll commented on Oct 15, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:官方 main
    • commit:8795f3699b40504376343fa1acda8b4619f0c76e(已核实修复 PR fix(plan): handle empty indexed FK cascades and cover self-FK ALTER #29994 的 merge commit 5d971d07a7c5a52a71f2c10390b109cebf5c0825 是其祖先)
    • 部署方式:本地隔离的嵌入式 MatrixOne,1 CN / 1 TN / 1 LogService;MySQL 协议连接;每轮独立数据库,结束后删除
    • 测试日期:2026-10-15

    原问题验证

    对照 issue 原始 SQL:父表有 s、other 两行,子表仅有引用 other 的行;对子表分别建立单列 KEY(pid) 与复合 KEY(pid,note)。在目标 s 没有匹配子行但子表非空时执行 DELETE FROM p WHERE id='s'。两种索引均 3/3 成功,无原来的执行器 panic;s 被删除,other 及其子行各保留 1 行。无显式子索引的对照路径也 3/3 成功。每轮结束服务仍可执行 SELECT 1。

    Happy Path 覆盖

    1. 单列索引、复合索引和无显式子索引三种形态下,目标父行有匹配子行时执行 CASCADE,3/3 均删除目标父/子行,同时保留无关父/子行各 1 行。
    2. 上述空匹配路径下,原问题所需的“无关子行不被误删”已按行键和引用值核对,3/3。

    Unhappy Path 覆盖

    1. 三种索引形态分别先在事务内删除目标子行,再删除目标父行,随后 ROLLBACK;3/3 均恢复父表 2 行、子表 2 行,包括目标子行,无部分提交。
    2. 带索引的 ON DELETE RESTRICT:无匹配子行的父行可删除;有匹配子行的父行删除被外键约束拒绝(本版本返回 20101),拒绝后该父行和子行仍各 1 行,3/3。

    白盒验证

    本轮为公开 SQL 的黑盒验收,没有新增或运行 Go UT / -race;PR #29994 已包含 planner 与分布式 SQL 回归,但其 PR 内测试结果不冒充本轮执行结果。

    回归测试

    • 新增 MOTR 用例:suites/14_issue_regression/t/issue_29954_indexed_empty_cascade.test 及对应 .result,已并入今天唯一的汇总 motr PR #237(head 9cd26d702865a3ccd29ed6c85f89ca39961c195e)。原独立 PR insert empty values results in index out of range error #245 已注明迁移并关闭。用例精确断言原始空匹配、单列/复合索引、匹配级联、事务回滚、RESTRICT 拒绝后的行状态。
    • 在同一官方 main 的隔离环境中,MOTR 非录制比较模式 --repeat 3:本用例 3/3 通过;与当天另外两个已通过用例合并复核时共 9/9 次、228/228 条语句通过。
    • PR (AOE) incompatible bug fix #237 尚未合入;完整 14_issue_regression suite 和集成 CI 尚未验证,后续继续跟踪。该缺口不是本次产品原问题复测的通过证明。
    • 本问题是小数据确定性 SQL/事务路径,不依赖数据规模、spill、长稳或外部故障注入;本次不补 big-data、stability、chaos。

    测试证据

    • 修复 PR #29994,merge commit 已核实进入上述官方 main。
    • MOTR PR #237 的用例与预期结果;最新 main 上的合并定向比较摘要:total=9 success=9 failed=0 skipped=0 | stmts=228 ok=228 fail=0。
    • 本地独立探针:/private/tmp/mo-28041-main-WBgvoq/matrixone/.local-validation/issue_29954.go,逐轮执行两种显式索引及无索引对照、匹配级联、回滚与 RESTRICT 状态断言;3 轮无断言失败。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions