Repository navigation
Description
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 CN116001and CN216002 - 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 succeedsFor 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
Ariznawlll commentedon Aug 12, 2026
测试版本
maine8ecba8662ca4c4d9c0179978425fc08505967dc原问题验证
使用 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 覆盖
Unhappy Path 覆盖
20101/HY000及foreign key constraint failscause。白盒验证
本轮以公开 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,通过。测试证据
测试结论
测试通过。