Skip to content

[Bug]: prepared INSERT ignores foreign_key_checks changes between executions #26892

Closed

Description

@Ariznawlll

Description

An INSERT prepared plan captures the session value of foreign_key_checks at prepare time and ignores later ON/OFF changes. Fresh text INSERTs honor the current session value, but the prepared statement can either keep rejecting after checks are disabled or keep accepting invalid foreign keys after checks are enabled.

Environment

  • Branch: main
  • Commit: 8be242b25bf9a44d73a3b1cc1db75fd0264a293c
  • Deployment: local mo-service, 1 Log / 1 TN / 2 CN, MySQL protocol on CN1 16001 and CN2 16002
  • Test date: 2026-08-10 (Asia/Shanghai)

Steps to reproduce

CREATE DATABASE fk_db;
USE fk_db;
CREATE TABLE p(id INT PRIMARY KEY);
INSERT INTO p VALUES (1);
CREATE TABLE c(id INT, CONSTRAINT fk_p FOREIGN KEY(id) REFERENCES p(id));

SET foreign_key_checks = 0;
-- binary COM_STMT_PREPARE: INSERT INTO c VALUES (?)
-- execute parameter 4: succeeds (expected while OFF)
SET foreign_key_checks = 1;
-- execute parameter 5: unexpectedly succeeds

For the reverse direction, prepare while foreign_key_checks=1, then set it to 0; the prepared INSERT continues to reject an invalid parent, while a fresh text INSERT succeeds.

Actual behavior

The 3-round binary matrix produced:

freshOff=<nil> rows=1
freshOn=Error 20101 ... rows=1
prepOff=<nil> rows=2
prepOn=<nil> rows=3
prepOffAgain=<nil> rows=4

Thus, after preparing with checks OFF, switching to ON does not enforce the FK. The opposite transition was also reproduced: a plan prepared with checks ON kept enforcing after switching to OFF.

Expected behavior

Prepared DML must observe the current session foreign_key_checks value at each execution, just like fresh text DML:

  • OFF: invalid child rows are allowed;
  • ON: invalid child rows are rejected;
  • changing the variable between executions must take effect without closing the prepared statement.

Stability and controls

  • Binary prepared reproducer: 3/3.
  • Fresh text control: OFF accepted the invalid row; ON rejected it, 3/3.
  • Cross-CN topology: DDL/catalog setup on CN2, prepared execution on CN1.
  • Failure is deterministic and data-visible: invalid child rows were inserted while ON.
  • No restart, timeout, or infrastructure error occurred.

Code analysis

pkg/sql/plan/build.go (bindAndOptimizeInsert) reads IsForeignKeyChecksEnabled(ctx) while building the INSERT and only emits FK checks when that value is enabled. Unlike the REPLACE path, it does not set query.HasForeignKeyAction for FK-sensitive INSERT plans. pkg/frontend/computation_wrapper.go uses that marker in shouldRebuildPreparePlan; without it, EXECUTE reuses the old plan across foreign_key_checks transitions.

Regression coverage

Add a distributed prepared-statement scenario for plain INSERT (then INSERT IGNORE and ODKU) covering both OFF→ON and ON→OFF transitions, fresh text controls, row counts, and rejection atomicity.

Related

Duplicate searches for prepared foreign_key_checks, foreign key checks prepared statement, and related open/closed issues and PRs found no matching report or fix.

Activity

  1. added
    kind/bug
    on Aug 10, 2026
  2. added this to the MO-202608 milestone on Aug 10, 2026
  3. added theissue type on Aug 10, 2026
  4. added
    ai-light
    on Aug 12, 2026
  5. Ariznawlll commented on Aug 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:e8ecba8662ca4c4d9c0179978425fc08505967dc
    • 部署方式:本地 1 CN / 1 TN / 1 Log,MySQL 协议端口 6001
    • 测试日期:2026-08-12

    原问题验证

    使用 SQL PREPARE 和 go-sql-driver/mysql 真实 binary prepare(interpolateParams=false,同一连接/同一 statement)重复切换 foreign_key_checks。OFF→ON→OFF 与 ON→OFF→ON 两个方向连续 3 轮均按当前 session 值执行:OFF 允许孤儿行,ON 拒绝孤儿行,未再复用 prepare 时的旧状态。

    Happy Path 覆盖

    1. fresh text INSERT 与 prepared INSERT 对照一致;切换后无需重新 prepare。
    2. 覆盖 plain INSERT、INSERT IGNORE、真实键 ODKU、无真实键 ODKU fallback;两种切换方向均正确。
    3. ON 状态下合法 parent 仍可成功插入;每次成功均核对 RowsAffected、完整行集、count 和 sum。

    Unhappy Path 覆盖

    1. ON 状态下所有预期 FK 拒绝均命中 20101/HY000 及 foreign key constraint fails cause。
    2. 每次拒绝后立即核对完整行集,确认无部分写入;同一 prepared handle 随后仍可执行合法值或在 OFF 状态执行孤儿行。
    3. 覆盖反向切换,避免只验证“开启检查”;连续 3 轮无 statement 污染、panic、hang、服务重启或残留 schema。

    白盒验证

    本轮以公开 SQL 和 MySQL binary protocol 黑盒验证为主;该问题可由公开 session 变量与 prepared DML 完整触发,无需额外故障注入。

    回归测试

    已在 motr PR #107 增加 14_issue_regression.issue_26892_prepared_fk_checks,组合 binary protocol 与 SQL PREPARE 场景。定向 --repeat 3 为 3/3,通过;与 #26797、#26867 并行复跑总计 9/9,通过。

    测试证据

    • 回归代码与稳定输出:motr PR #107
    • 本地定向回归:3/3;两方向切换、10 类预期拒绝/轮的原子性和错误后恢复断言全部通过。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-lightkind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions