Skip to content

[Bug]: COM_QUERY plan cache returns deleted or replaced View data after DDL in another session #26882

Closed

Description

@Ariznawlll

Description

A session-local COM_QUERY plan cache entry for a View is not invalidated when another session drops or replaces that View. Reissuing the byte-identical SQL can return rows from a deleted View or the old result shape indefinitely, while a whitespace variant, a new connection, and an existing prepared statement all observe current catalog state.

Environment

  • Branch: main
  • Commit: 1eee01fe427d90fb2bedfac1e14e9ae7fbced76f
  • Deployment: local mo-service, 1 Log / 1 TN / 2 CN / Proxy; reproduced both across CNs and between two sessions on the same CN

Steps to reproduce

Use two persistent physical connections. Both cases require connection A to execute the exact text once before connection B changes the View.

Deleted View still returns rows

Connection A:

CREATE DATABASE view_plan_cache;
USE view_plan_cache;
CREATE TABLE t(id INT PRIMARY KEY, v VARCHAR(10));
INSERT INTO t VALUES (1, 'old');
CREATE VIEW vv AS SELECT v FROM t;

SELECT v FROM vv; -- returns old and populates the COM_QUERY plan cache

Connection B:

USE view_plan_cache;
DROP VIEW vv;

Connection A:

SELECT v FROM vv;  -- byte-identical SQL
SELECT  v FROM vv; -- one extra space

Replaced View keeps the old result shape

Connection A first executes and caches:

SELECT * FROM vv; -- initial View has one column: id

Connection B replaces it:

DROP VIEW vv;
CREATE VIEW vv AS SELECT id, v, CONCAT(v, 'x') AS label FROM t;

Connection A repeats SELECT * FROM vv and then executes the whitespace variant SELECT * FROM vv.

Actual behavior

  • After DROP VIEW, the exact cached SQL succeeds and returns the deleted View's old value old.
  • The whitespace variant and a new connection correctly return table "vv" does not exist.
  • After drop/recreate with a new definition, the exact cached SQL reports only the old id column; the whitespace variant, a new connection, the DDL connection, and an already-created binary prepared statement report id,v,label.
  • The exact SQL remained stale through 20 retries over 5 seconds.

Representative latest-main output:

round=1 exact_value="old" exact_err=<nil> variant_missing=true newconn_missing=true
round=2 exact_value="old" exact_err=<nil> variant_missing=true newconn_missing=true
round=3 exact_value="old" exact_err=<nil> variant_missing=true newconn_missing=true

round=1 prepared=id,v,label exact=id variant=id,v,label cn2=id,v,label newconn=id,v,label recovered_tick=-1
round=2 prepared=id,v,label exact=id variant=id,v,label cn2=id,v,label newconn=id,v,label recovered_tick=-1
round=3 prepared=id,v,label exact=id variant=id,v,label cn2=id,v,label newconn=id,v,label recovered_tick=-1

Expected behavior

When a View is dropped or its identity/definition changes, cached COM_QUERY plans that depend on that View must be invalidated. The next byte-identical query must either report the missing View or bind and return the replacement View's current columns and rows.

Stability and controls

  • Cross-CN reproducer: 3/3 for DROP and 3/3 for replacement.
  • Same-CN, two-session reproducer: 3/3 for DROP and 3/3 for replacement.
  • Same-session DDL control: passed; executing the DDL on the reader session invalidated the exact SQL and returned the new shape.
  • Cache-key control: changing only whitespace bypassed the stale entry and observed the current catalog.
  • New-connection control: always observed the current catalog.
  • Prepared-statement control: observed the replacement definition, confirming the prepared View dependency fix remains effective.
  • No partial DDL or fixture error was observed; SHOW CREATE VIEW on both CNs reported the current definition.

Evidence

The two standalone Go reproducers use database/sql with go-sql-driver/mysql, pin both physical connections with db.Conn, compare result-column metadata, and assert all controls. They were run against the server-reported git_version() = 1eee01fe4.

Code analysis

pkg/frontend/plan_cache.go keys ordinary session plans by SQL text. cachedPlanForInput retrieves that entry before parsing. At dispatch, checkModify in pkg/frontend/mysql_cmd_executor.go only validates TableDef/ObjRef dependencies present in query-plan nodes. The observed signature indicates that a View's ordinary cached plan tracks the unchanged base table but not the View catalog object's identity/version, so remote View DDL does not set flag and the old plan survives. This is a code-path inference from the black-box controls and current implementation.

DDL in the same session calls ses.cleanCache(), explaining why the local-DDL control passes. Prepared statements use separate catalog/View dependency tracking introduced for #25979 and correctly rebuild in this reproducer.

Regression coverage

Add a multi-session protocol scenario that:

  1. caches an exact COM_QUERY on session A;
  2. drops/replaces the View on session B;
  3. requires the same exact SQL on A to reject the missing View or return the replacement shape;
  4. covers same-CN and multi-CN placement, drop/recreate, incompatible result-column count, repeated execution, and recovery.

Related

Activity

  1. added this to the MO-202608 milestone on Aug 9, 2026
  2. added
    kind/bug
    on Aug 9, 2026
  3. added theissue type on Aug 9, 2026
  4. assigned and unassigned on Aug 12, 2026
  5. Ariznawlll commented on Aug 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:1c292cf20e113b296a3db2cb6a65e00af2ea2327
    • 部署方式:本地 etc/launch-with-proxy/launch.toml,2 CN / 1 TN / 1 Log + Proxy;本用例直连 CN 16001
    • 测试日期:2026-08-13

    原问题验证

    使用两个固定到独立物理连接的普通 COM_QUERY 会话。reader 连续两次执行完全相同的 select * from v order by id,初始视图只有 id 列,返回 1、2;writer 随后 DROP VIEW 并将同名视图重建为 id、v、label 三列,只保留 id=2。reader 不换连接、SQL 文本不变再次查询,实际列为 id,v,label,数据为 2,120,beta-v2,未复用旧视图计划。上述黑盒用例连续执行 3/3 次,结果一致。

    Happy Path 覆盖

    1. 同一固定 reader 连接连续执行字节完全相同的查询,初始两次均返回一列 id 以及行 1、2。
    2. 另一连接替换同名视图后,原 reader 立即看到三列新定义及唯一结果 2,120,beta-v2;仅改变空白的查询和新建连接也得到相同结果。
    3. 再次重建视图为新定义后,原 reader 连接恢复并返回 1,210,alpha-v3;底表仍为 2 行,sum(id)=3、sum(amount)=30、标签为 alpha,beta。

    Unhappy Path 覆盖

    1. writer 删除视图后,原 reader 对缓存过的完全相同 SQL 返回 MySQL 错误码 1146,没有返回旧列或旧数据。
    2. DROP VIEW 失败路径后同一 reader 会话仍可执行 select 1,重建视图后也可继续查询;测试库删除后 information_schema.schemata 中残留数为 0。

    白盒验证

    执行 go test ./pkg/tests/issues -run 'TestIssue26882ComQueryCacheTracksViewIdentity' -count=3 -v,3/3 通过。该测试覆盖固定 reader/DDL 双连接下的视图身份变更、删除后的 1146,并保留订阅库无表扫描视图的普通查询控制。

    回归测试

    已加入 motr PR #106:issue_26882_com_query_view_cache.go。该多连接/协议场景使用独立物理连接,并执行列名、行值、错误码、会话恢复、底表不变量和清理断言;聚焦用例重复 3 次均通过。

    测试证据

    • MatrixOne 聚焦白盒:pkg/tests/issues/issue_26882_test.go
    • motr 回归实现及评审:PR #106
    • 最终聚焦回归矩阵包含本用例,共执行 27 个重复项,结果 27 success / 0 failed、306/306 statements。

    测试结论

    测试通过。

  6. Ariznawlll commented on Aug 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:1c292cf20e113b296a3db2cb6a65e00af2ea2327
    • 部署方式:本地 etc/launch-with-proxy/launch.toml,2 CN / 1 TN / 1 Log + Proxy;本用例直连 CN 16001
    • 测试日期:2026-08-13

    原问题验证

    使用两个固定到独立物理连接的普通 COM_QUERY 会话。reader 连续两次执行完全相同的 select * from v order by id,初始视图只有 id 列,返回 1、2;writer 随后 DROP VIEW 并将同名视图重建为 id、v、label 三列,只保留 id=2。reader 不换连接、SQL 文本不变再次查询,实际列为 id,v,label,数据为 2,120,beta-v2,未复用旧视图计划。上述黑盒用例连续执行 3/3 次,结果一致。

    Happy Path 覆盖

    1. 同一固定 reader 连接连续执行字节完全相同的查询,初始两次均返回一列 id 以及行 1、2。
    2. 另一连接替换同名视图后,原 reader 立即看到三列新定义及唯一结果 2,120,beta-v2;仅改变空白的查询和新建连接也得到相同结果。
    3. 再次重建视图为新定义后,原 reader 连接恢复并返回 1,210,alpha-v3;底表仍为 2 行,sum(id)=3、sum(amount)=30、标签为 alpha,beta。

    Unhappy Path 覆盖

    1. writer 删除视图后,原 reader 对缓存过的完全相同 SQL 返回 MySQL 错误码 1146,没有返回旧列或旧数据。
    2. DROP VIEW 失败路径后同一 reader 会话仍可执行 select 1,重建视图后也可继续查询;测试库删除后 information_schema.schemata 中残留数为 0。

    白盒验证

    执行 go test ./pkg/tests/issues -run 'TestIssue26882ComQueryCacheTracksViewIdentity' -count=3 -v,3/3 通过。该测试覆盖固定 reader/DDL 双连接下的视图身份变更、删除后的 1146,并保留订阅库无表扫描视图的普通查询控制。

    回归测试

    已加入 motr PR #106:issue_26882_com_query_view_cache.go。该多连接/协议场景使用独立物理连接,并执行列名、行值、错误码、会话恢复、底表不变量和清理断言;聚焦用例重复 3 次均通过。

    测试证据

    • MatrixOne 聚焦白盒:pkg/tests/issues/issue_26882_test.go
    • motr 回归实现及评审:PR #106
    • 最终聚焦回归矩阵包含本用例,共执行 27 个重复项,结果 27 success / 0 failed、306/306 statements。

    测试结论

    测试通过。

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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions