Repository navigation
Description
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 cacheConnection B:
USE view_plan_cache;
DROP VIEW vv;Connection A:
SELECT v FROM vv; -- byte-identical SQL
SELECT v FROM vv; -- one extra spaceReplaced View keeps the old result shape
Connection A first executes and caches:
SELECT * FROM vv; -- initial View has one column: idConnection 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 valueold. - 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
idcolumn; the whitespace variant, a new connection, the DDL connection, and an already-created binary prepared statement reportid,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 VIEWon 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:
- caches an exact COM_QUERY on session A;
- drops/replaces the View on session B;
- requires the same exact SQL on A to reject the missing View or return the replacement shape;
- covers same-CN and multi-CN placement, drop/recreate, incompatible result-column count, repeated execution, and recovery.
Related
- [Bug]: Prepared SELECT keeps stale view definition after CREATE OR REPLACE VIEW #25979 covered stale prepared View plans and is fixed; the prepared control passes here, while ordinary COM_QUERY remains stale.
- [Bug]: View metadata stays stale after source column type changes #26227 covers persisted View column metadata after dependency ALTER and explicitly treats plan invalidation as a separate lifecycle.
- PR fix: refresh persisted View metadata after dependency DDL #26876 targets persisted View metadata refresh, not session COM_QUERY cache invalidation.
Activity
Ariznawlll commentedon Aug 12, 2026
测试版本
main1c292cf20e113b296a3db2cb6a65e00af2ea2327etc/launch-with-proxy/launch.toml,2 CN / 1 TN / 1 Log + Proxy;本用例直连 CN16001原问题验证
使用两个固定到独立物理连接的普通
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 覆盖
id以及行1、2。2,120,beta-v2;仅改变空白的查询和新建连接也得到相同结果。1,210,alpha-v3;底表仍为 2 行,sum(id)=3、sum(amount)=30、标签为alpha,beta。Unhappy Path 覆盖
1146,没有返回旧列或旧数据。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 次均通过。测试证据
pkg/tests/issues/issue_26882_test.go27 success / 0 failed、306/306 statements。测试结论
测试通过。
Ariznawlll commentedon Aug 12, 2026
测试版本
main1c292cf20e113b296a3db2cb6a65e00af2ea2327etc/launch-with-proxy/launch.toml,2 CN / 1 TN / 1 Log + Proxy;本用例直连 CN16001原问题验证
使用两个固定到独立物理连接的普通
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 覆盖
id以及行1、2。2,120,beta-v2;仅改变空白的查询和新建连接也得到相同结果。1,210,alpha-v3;底表仍为 2 行,sum(id)=3、sum(amount)=30、标签为alpha,beta。Unhappy Path 覆盖
1146,没有返回旧列或旧数据。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 次均通过。测试证据
pkg/tests/issues/issue_26882_test.go27 success / 0 failed、306/306 statements。测试结论
测试通过。