You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Prepared SELECT keeps stale view definition after CREATE OR REPLACE VIEW #25979
A prepared query over a view keeps using the old view definition after CREATE OR REPLACE VIEW changes that definition.
Tested version
main at 3046f14d94b3f1a25839eeeb65729b64f85ac647.
Reproduction
CREATEDATABASEprep_view;
USE prep_view;
CREATETABLEt(a INT, b INT);
INSERT INTO t VALUES (1,10),(2,20);
CREATEVIEWvASSELECT a FROM t;
PREPARE s FROM'SELECT * FROM v ORDER BY 1';
EXECUTE s;
CREATE OR REPLACEVIEWvASSELECT b FROM t;
EXECUTE s;
Actual result
Both executions return the old view output:
a
1
2
The stale result reproduces 3/3 times.
Expected result
The second execution should use the replacement view definition and return:
b
10
20
MySQL 8.0.45 returns the expected result.
Additional evidence
Prepared queries over ordinary tables correctly rebuild after adding, renaming, or modifying columns, truncating the table, and dropping/recreating the table. The failure is specific to a changed view definition whose underlying base table did not change.
Root cause direction
The prepared plan expands the view into its base-table plan. Schema-change validation checks the recorded objects against the catalog cache before reusing the cached compile, but the prepared plan does not retain a dependency on the view definition itself. Replacing the view therefore leaves all recorded base-table versions unchanged and the old expanded plan is reused.
补充一个同根因场景:在会话 A 通过二进制协议构建基于视图的查询后,会话 B 执行 DROP VIEW active_v。随后会话 A 再次执行原语句,仍成功返回删除前的旧结果 [1],而不是报告视图不存在。MySQL 8.0.45 在相同步骤返回 ERROR 1146: Table prepare_view_probe.active_v does not exist。该现象连续出现 3/3 次;重新创建视图后原语句仍可执行。
Verified against the latest main at c726a6199094184a2e6ebc7504c3e011982b8976 (2026-08-04).
Results:
The original SQL reproduction no longer reproduces. After CREATE OR REPLACE VIEW v AS SELECT b FROM t, the same prepared statement returned column b with values 10, 20 in 3/3 runs.
A broader case with base-table UPDATE/DELETE/INSERT followed by replacement of both the view filter and projection also refreshed correctly; the prepared result matched a direct query of the new view.
Cross-session binary-protocol testing with a persistent COM_STMT_PREPARE handle also passed in 3/3 runs:
replacing the view from another session changed the prepared result from [1, 2] to [10, 20];
dropping the view from another session made the next execute fail with table "v" does not exist, rather than returning stale rows.
The built server reported git_version() = c726a61990. Merge commit 00d7bf28fcf240c34607943d491e65f2fb37455c from #26021 is an ancestor of this main and introduced prepared-plan catalog dependency tracking for views, which appears to have fixed this issue indirectly.
Conclusion: the reported stale-view behavior is no longer reproducible on the latest main.
Activity
Ariznawlll commentedon Jul 22, 2026
最新 main 回归结果(2026-07-22)
c0486a2446c84c6dd6bf142494fe85243877ddc5a=1;CREATE OR REPLACE VIEW改为列b=10后再次执行。1,没有读取新视图定义。Ariznawlll commentedon Jul 22, 2026
补充一个同根因场景:在会话 A 通过二进制协议构建基于视图的查询后,会话 B 执行
DROP VIEW active_v。随后会话 A 再次执行原语句,仍成功返回删除前的旧结果[1],而不是报告视图不存在。MySQL 8.0.45 在相同步骤返回ERROR 1146: Table prepare_view_probe.active_v does not exist。该现象连续出现 3/3 次;重新创建视图后原语句仍可执行。Ariznawlll commentedon Jul 27, 2026
补充一组带底表变更的复现范围:视图本身可正确反映底表的
UPDATE、DELETE与INSERT,但在同一会话中执行CREATE OR REPLACE VIEW后,先前准备的查询仍使用替换前的视图定义。三轮中均得到相同结果:
amount >= 10、标签为原值;amount >= 30、标签改为追加-v2后,已准备查询仍返回原来的 3 行和原标签;-v2标签。这说明现象不限于简单列投影;底表数据刷新正常,问题集中在视图定义替换后的已准备查询更新。
ck89119 commentedon Aug 4, 2026
Verified against the latest
mainatc726a6199094184a2e6ebc7504c3e011982b8976(2026-08-04).Results:
CREATE OR REPLACE VIEW v AS SELECT b FROM t, the same prepared statement returned columnbwith values10, 20in 3/3 runs.COM_STMT_PREPAREhandle also passed in 3/3 runs:[1, 2]to[10, 20];table "v" does not exist, rather than returning stale rows.The built server reported
git_version() = c726a61990. Merge commit00d7bf28fcf240c34607943d491e65f2fb37455cfrom #26021 is an ancestor of thismainand introduced prepared-plan catalog dependency tracking for views, which appears to have fixed this issue indirectly.Conclusion: the reported stale-view behavior is no longer reproducible on the latest
main.Ariznawlll commentedon Aug 4, 2026
测试版本
main6091b418fd529badca4e1855843bd38804de396fetc/launch/launch.toml);分别建立 prepared-reader 和 DDL/DML writer 两个物理 MySQL 连接。binary 路径通过 Go MySQL driverv1.8.1的PrepareContext(interpolateParams=false)发送COM_STMT_PREPARE/COM_STMT_EXECUTE。原问题验证
构造
base(id, label, amount)和v,在 reader 连接分别创建:PREPARE codex_25979_text FROM 'SELECT id,label FROM v WHERE amount>=? ORDER BY id';PrepareContext创建的*sql.Stmt。初始
v返回1:alpha,2:beta。随后由独立 writer 连接执行CREATE OR REPLACE VIEW,并复用 原来的 text prepared statement 和 binary*sql.Stmt查询;两条路径均返回替换后的定义结果,不返回旧 view 的行或投影。Happy Path 覆盖
1:alpha,2:beta。UPDATE id=1 amount=5、UPDATE id=3 amount=35、INSERT id=4后,两个旧 handle 都返回2:beta,3:gamma,4:delta,证明不会把普通基表 DML 误判为过期 view 计划。amount>=30且label投影改为CONCAT(label,'-v2')后,两个旧 handle 都返回3:gamma-v2,4:delta-v2。2:beta-v3。1:alpha-v4。Unhappy Path 覆盖
DROP VIEW v后,text 和 binary 两个旧 handle 都返回错误;没有返回任何 stale row。v替换为仅含id的不兼容定义时,两个旧 handle 都返回列解析/执行错误;没有复用旧的两列 shape。SELECT 1均成功,未观察到 session 不可用、panic 或 hang。白盒验证
go test ./pkg/sql/parsers/dialect/mysql -run 'Test.*Prepare' -count=1通过(0.442s)。test/distributed/cases/prepare/prepare_ddl_schema_change.sql已覆盖部分 text prepared view DDL/跨 session 场景。pkg/sql/plan的定向 dependency UT 时,本地临时 macOS 构建产物因@rpath/libmo.dylib缺失而无法启动;这是测试环境的动态库路径问题,不作为产品失败结论。黑盒三轮服务端结果不受该问题影响。回归测试
SET与部分 prepared view DDL 覆盖,但没有精确覆盖本 issue 的“两个物理连接 + text/binary 双协议 + replace/drop/recreate/incompatible/recover”的组合。motr/suites/scenarios:该路径需要多连接和 binary protocol,不能由单连接 SQL.test完整表达。本次仅完成验证,未新增回归文件或 PR。测试证据
1:alpha,2:beta→ 基表 DML 后2:beta,3:gamma,4:delta→ replace 后3:gamma-v2,4:delta-v2→ drop/incompatible 均报错且无 stale row → 重建/修复后分别为2:beta-v3、1:alpha-v4。测试结论
测试通过。
Ariznawlll commentedon Aug 4, 2026
已补充 motr 回归,Draft PR:
https://github.com/matrixorigin/motr/pull/75
新增
issue_25979_prepared_view_recompile,使用两个独立物理连接:reader 在同一连接上同时保留 SQL-levelPREPARE/EXECUTE和 binaryCOM_STMT_PREPARE/COM_STMT_EXECUTE,writer 从另一连接执行 DML/DDL。覆盖内容:
CREATE OR REPLACE VIEW后旧 handle 重编译并返回新定义结果;DROP VIEW、列不兼容替换必须报错,不能返回旧数据或旧投影;已执行:
--repeat 3新用例、脚本模块go test ./...,以及排除已知#25992 invalid allocation account后的14_issue_regression全套(110 success / 0 failed / 2 opt-in skipped)。注:文本协议设置 user variable 使用字面量
SET @var=<int>;若对 SET 本身再做 binary 参数化,会混入独立的 #25996 场景,故不在本回归中耦合。