Skip to content

[Bug]: Prepared SELECT keeps stale view definition after CREATE OR REPLACE VIEW #25979

Closed

Description

@Ariznawlll

Summary

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

CREATE DATABASE prep_view;
USE prep_view;

CREATE TABLE t(a INT, b INT);
INSERT INTO t VALUES (1,10),(2,20);
CREATE VIEW v AS SELECT a FROM t;

PREPARE s FROM 'SELECT * FROM v ORDER BY 1';
EXECUTE s;

CREATE OR REPLACE VIEW v AS SELECT 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.

Activity

  1. added this to the MO-202607 milestone on Jul 21, 2026
  2. added
    kind/bug
    on Jul 21, 2026
  3. added theissue type on Jul 21, 2026
  4. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 回归结果(2026-07-22)

    • 测试基线:c0486a2446c84c6dd6bf142494fe85243877ddc5
    • prepared 查询首次读取视图列 a=1;CREATE OR REPLACE VIEW 改为列 b=10 后再次执行。
    • 实际结果:仍返回旧列值 1,没有读取新视图定义。
    • 结论:问题仍可复现。
  5. Ariznawlll commented on Jul 22, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充一个同根因场景:在会话 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 次;重新创建视图后原语句仍可执行。

  6. Ariznawlll commented on Jul 27, 2026

    @Ariznawlll
    CollaboratorAuthor

    补充一组带底表变更的复现范围:视图本身可正确反映底表的 UPDATE、DELETE 与 INSERT,但在同一会话中执行 CREATE OR REPLACE VIEW 后,先前准备的查询仍使用替换前的视图定义。

    三轮中均得到相同结果:

    • 初始视图为 amount >= 10、标签为原值;
    • 修改底表后,已准备查询能正确返回更新后的数据;
    • 将视图替换为 amount >= 30、标签改为追加 -v2 后,已准备查询仍返回原来的 3 行和原标签;
    • 同一会话直接查询视图则正确返回 2 行及 -v2 标签。

    这说明现象不限于简单列投影;底表数据刷新正常,问题集中在视图定义替换后的已准备查询更新。

  7. added
    severity/s0
    and removed on Aug 3, 2026
  8. ck89119 commented on Aug 4, 2026

    @ck89119
    Contributor

    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.

  9. Ariznawlll commented on Aug 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:6091b418fd529badca4e1855843bd38804de396f
    • 部署方式:本地独立单 CN 实例(etc/launch/launch.toml);分别建立 prepared-reader 和 DDL/DML writer 两个物理 MySQL 连接。binary 路径通过 Go MySQL driver v1.8.1 的 PrepareContext(interpolateParams=false)发送 COM_STMT_PREPARE/COM_STMT_EXECUTE。
    • 测试日期:2026-08-04
    • 稳定性:完整用例连续执行 3 轮,三轮结果一致。

    原问题验证

    构造 base(id, label, amount) 和 v,在 reader 连接分别创建:

    1. text protocol:PREPARE codex_25979_text FROM 'SELECT id,label FROM v WHERE amount>=? ORDER BY id';
    2. binary protocol:同一 SQL 通过 PrepareContext 创建的 *sql.Stmt。

    初始 v 返回 1:alpha,2:beta。随后由独立 writer 连接执行 CREATE OR REPLACE VIEW,并复用 原来的 text prepared statement 和 binary *sql.Stmt 查询;两条路径均返回替换后的定义结果,不返回旧 view 的行或投影。

    Happy Path 覆盖

    1. text 与 binary prepared 查询初始结果均为 1:alpha,2:beta。
    2. writer 对基表执行 UPDATE id=1 amount=5、UPDATE id=3 amount=35、INSERT id=4 后,两个旧 handle 都返回 2:beta,3:gamma,4:delta,证明不会把普通基表 DML 误判为过期 view 计划。
    3. writer 将 view 替换为 amount>=30 且 label 投影改为 CONCAT(label,'-v2') 后,两个旧 handle 都返回 3:gamma-v2,4:delta-v2。
    4. 删除后按新定义重建 view,两个旧 handle 都返回 2:beta-v3。
    5. 将不兼容 view 定义修复为双列投影后,两个旧 handle 无需重新 prepare 即恢复,并返回 1:alpha-v4。

    Unhappy Path 覆盖

    1. DROP VIEW v 后,text 和 binary 两个旧 handle 都返回错误;没有返回任何 stale row。
    2. 将 v 替换为仅含 id 的不兼容定义时,两个旧 handle 都返回列解析/执行错误;没有复用旧的两列 shape。
    3. 两个失败场景之后均立即重建兼容 view 并复用原 handle 成功,排除错误后 prepared state 被永久损坏。
    4. 所有 DDL 失败/失效路径后,prepared-reader 连接上的 SELECT 1 均成功,未观察到 session 不可用、panic 或 hang。

    白盒验证

    • go test ./pkg/sql/parsers/dialect/mysql -run 'Test.*Prepare' -count=1 通过(0.442s)。
    • 代码侧确认本问题的修复依赖 prepared plan 的 catalog/view dependency 收集与 schema 变更后的重新编译;现有 test/distributed/cases/prepare/prepare_ddl_schema_change.sql 已覆盖部分 text prepared view DDL/跨 session 场景。
    • 额外尝试 pkg/sql/plan 的定向 dependency UT 时,本地临时 macOS 构建产物因 @rpath/libmo.dylib 缺失而无法启动;这是测试环境的动态库路径问题,不作为产品失败结论。黑盒三轮服务端结果不受该问题影响。

    回归测试

    • 现有 BVT 已有 text prepared SET 与部分 prepared view DDL 覆盖,但没有精确覆盖本 issue 的“两个物理连接 + text/binary 双协议 + replace/drop/recreate/incompatible/recover”的组合。
    • 建议补充到 motr/suites/scenarios:该路径需要多连接和 binary protocol,不能由单连接 SQL .test 完整表达。本次仅完成验证,未新增回归文件或 PR。
    • 大数据、spill、chaos、多 CN/Proxy migration 与本 issue 的 catalog dependency 失效条件无直接触发关系,本轮不适用。

    测试证据

    • 黑盒 validator 在上述 commit 和隔离实例上连续 3 轮执行;每轮均覆盖 text/binary 两个旧 handle,且所有断言通过。
    • 关键结果序列: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。

    测试结论

    测试通过。

  10. Ariznawlll commented on Aug 4, 2026

    @Ariznawlll
    CollaboratorAuthor

    已补充 motr 回归,Draft PR:

    https://github.com/matrixorigin/motr/pull/75

    新增 issue_25979_prepared_view_recompile,使用两个独立物理连接:reader 在同一连接上同时保留 SQL-level PREPARE/EXECUTE 和 binary COM_STMT_PREPARE/COM_STMT_EXECUTE,writer 从另一连接执行 DML/DDL。

    覆盖内容:

    • 初始查询与基表 UPDATE/INSERT 对旧 prepared handle 的可见性;
    • CREATE OR REPLACE VIEW 后旧 handle 重编译并返回新定义结果;
    • DROP VIEW、列不兼容替换必须报错,不能返回旧数据或旧投影;
    • 重新创建/修复为兼容 view 后,同一批原 handle 可恢复;
    • 全部失效/恢复后 reader session 仍可正常查询。

    已执行:--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 场景,故不在本回归中耦合。

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

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions