Skip to content

[Bug]: CREATE TEMPORARY TABLE ... CLONE leaves an unqueryable session ghost after failing #27042

Closed

Description

@Ariznawlll

Description

CREATE TEMPORARY TABLE dst CLONE src is accepted by the SQL grammar, but it cannot create a usable temporary destination. The first execution returns ERROR 1146 for the destination itself and leaves a stale session alias. Within that session, the table is simultaneously:

  • absent to SHOW, SELECT, INSERT, and DROP;
  • present to all later CREATE TEMPORARY TABLE statements.

Even DROP TEMPORARY TABLE IF EXISTS returns success without clearing the ghost alias. The session cannot reuse the name until it disconnects.

Environment

  • Branch: main
  • Commit: 2d9ee9c75398b5492f14db7d9bd4d9f777251bd3
  • Test date: 2026-08-12
  • Deployment: local single process, 1 Log Service + 1 TN + 2 CN; reproduced through CN1

Steps to reproduce

Run all statements in one session:

DROP DATABASE IF EXISTS clone_temp_target;
CREATE DATABASE clone_temp_target;
USE clone_temp_target;

CREATE TABLE src(id INT PRIMARY KEY, v VARCHAR(20));
INSERT INTO src VALUES (1,'one'),(2,'two');

CREATE TEMPORARY TABLE temp_dst CLONE src;
SHOW CREATE TABLE temp_dst;
SELECT * FROM temp_dst;

CREATE TEMPORARY TABLE temp_dst CLONE src;
CREATE TEMPORARY TABLE temp_dst(id INT PRIMARY KEY, v VARCHAR(20));

DROP TEMPORARY TABLE IF EXISTS temp_dst;
CREATE TEMPORARY TABLE temp_dst(id INT PRIMARY KEY, v VARCHAR(20));

Actual behavior

CREATE TEMPORARY TABLE temp_dst CLONE src;
ERROR 1146 (HY000): no such table clone_temp_target.temp_dst

SHOW CREATE TABLE temp_dst;
ERROR 1146 (HY000): no such table clone_temp_target.temp_dst

SELECT * FROM temp_dst;
ERROR 1146 (HY000): table "temp_dst" does not exist

CREATE TEMPORARY TABLE temp_dst CLONE src;
ERROR 1050 (HY000): table temp_dst already exists

CREATE TEMPORARY TABLE temp_dst(id INT PRIMARY KEY, v VARCHAR(20));
ERROR 1050 (HY000): table temp_dst already exists

DROP TEMPORARY TABLE IF EXISTS temp_dst;
Query OK

CREATE TEMPORARY TABLE temp_dst(id INT PRIMARY KEY, v VARCHAR(20));
ERROR 1050 (HY000): table temp_dst already exists

There is no permanent catalog row for the target.

Expected behavior

Because the grammar explicitly supports the temporary option for CLONE, the statement should create a queryable session-local table containing the source rows.

If temporary CLONE is intentionally unsupported, it must be rejected before creating physical or session state, with a clear unsupported-operation error. A failed statement must never reserve an unusable name in the session.

Stability and controls

  • Reproducer: 3/3 with independent names temp_dst_r1, temp_dst_r2, and temp_dst_r3 in one session.
  • Every first attempt returned 1146; every second attempt returned 1050.
  • DROP TEMPORARY TABLE IF EXISTS returned success but did not clear the stale name.
  • Permanent catalog count for all three target names: 0.
  • Fresh-session control: ordinary temporary creation, insert, select, and drop using the same name all succeeded.
  • CREATE TEMPORARY TABLE ... LIKE src plus INSERT ... SELECT succeeded and returned both source rows.
  • Temporary-source control: cloning a temporary source into a permanent destination succeeded and returned the exact row.
  • Source rows remained unchanged.

Evidence

The complete reproducer is preserved at:

/private/tmp/mo-clone-2d9-validation/temp_target.sql

Three-round signature:

r1: first=1146, second=1050
r2: first=1146, second=1050
r3: first=1146, second=1050
permanent-catalog-count=0

Code analysis

The failure crosses the temporary-table create path and the generic block-clone path:

  1. pkg/sql/parsers/dialect/mysql/mysql_sql.y:9843-9854 explicitly accepts CREATE temporary_opt TABLE ... CLONE ... and stores the temporary flag.
  2. pkg/sql/compile/ddl.go:1397-1406 rewrites the destination to a generated session-local physical name. After creation, pkg/sql/compile/ddl.go:1505-1513 registers the user alias; pkg/sql/compile/ddl.go:2091-2095 commits that alias before CreateTable returns success.
  3. pkg/sql/compile/ddl.go:4033-4047 then unconditionally enters RestoreTable.
  4. The clone operator still resolves Ctx.DstTblName, which is the user-facing alias. pkg/sql/colexec/table_clone/table_clone.go:321-325 calls dstDB.Relation with that alias instead of the generated physical temporary-table name, producing 1146.
  5. The statement rollback removes the physical relation, but the already-committed session alias is outside transaction rollback. Subsequent create checks session.GetTempTable at pkg/sql/compile/ddl.go:1355-1359 and returns 1050.

This also explains why disconnecting clears the problem while DROP ... IF EXISTS does not.

Regression coverage

Add a same-session distributed CLONE BVT under test/distributed/cases/git4data/clone/ that asserts:

  1. temporary destination CLONE returns the exact source schema and rows;
  2. the target is invisible from a second session;
  3. disconnect removes it;
  4. any injected or unsupported failure leaves no physical relation and no session alias;
  5. DROP TEMPORARY TABLE IF EXISTS truly releases the name.

Add a focused planner/operator test that verifies the clone destination is the generated temporary physical name, not the user alias.

Related

No matching open or closed issue or fix PR was found using exact searches for CREATE TEMPORARY TABLE ... CLONE, temporary CLONE no such table, and stale temporary-table aliases. #26087 concerns concurrent Data Branch quota accounting and is not a duplicate.

Activity

  1. added this to the MO-202608 milestone on Aug 12, 2026
  2. added
    kind/bug
    on Aug 12, 2026
  3. added theissue type on Aug 12, 2026
  4. added
    severity/s0
    ai-easy
    and removed on Aug 12, 2026
  5. Ariznawlll commented on Aug 13, 2026

    @Ariznawlll
    CollaboratorAuthor

    在最新官方 main 上复测,问题仍稳定存在。

    • commit:1d7b6311ce91df0968da435bb405f3d68137c595
    • 环境:本地单进程,1 Log Service + 1 TN + 1 CN
    • 结果:3/3

    同一会话内的对照结果:

    1. 普通临时表创建、写入、查询正常;
    2. CREATE TEMPORARY TABLE like_temp LIKE src 正常,结构和唯一索引均保留;
    3. CREATE TEMPORARY TABLE clone_temp CLONE src 每次均返回 ERROR 1146: no such table clone_temp_repro.clone_temp,目标不可查询;
    4. SHOW FULL TABLES 中没有目标永久表。

    最新代码路径仍与 issue 描述一致:

    • grammar 继续明确接受 CREATE temporary_opt TABLE ... CLONE ...:pkg/sql/parsers/dialect/mysql/mysql_sql.y:9911;
    • Scope.TableClone 创建临时目标后无条件进入 RestoreTable:pkg/sql/compile/ddl.go:4040-4054;
    • TableClone.Prepare 使用逻辑目标名执行 dstDB.Relation(...):pkg/sql/colexec/table_clone/table_clone.go:296-298,无法解析已编码的会话临时物理关系。

    服务端日志也明确记录失败发生在 [RestoreTable] clone (s.Run) 之后,statement status 为 fail,错误即目标逻辑名不存在。

  6. added a commit that references this issue on Aug 14, 2026
    37fcd20
  7. added and removed
    severity/s0
    on Aug 25, 2026
  8. gouhongshen commented on Sep 8, 2026

    @gouhongshen
    Contributor

    Automated AI triage

    This issue requires human involvement and cannot currently be completed independently by the
    automated fix loop.

    Reason

    The frontend/release owner must validate the repaired supported revision and disposition the open issue, or provide a new post-fix failing sequence. No additional independent implementation is established.

    Evidence

    The reported inconsistent alias/physical-table state is already addressed by merged PR #27070, commit 37fcd20. Inspected origin/main contains physical destination resolution, alias transaction ownership, and temporary_table_clone.sql; local c838d31 predates the repair.

    Please use this analysis to provide the missing decision, environment, evidence, or ownership
    needed before the issue is returned to the AI candidate pool.

  9. added 3 commits that reference this issue on Sep 17, 2026
    3a628b8
    f92b6db
    506c90e
  10. gouhongshen commented on Oct 15, 2026

    @gouhongshen
    Contributor

    已修复

  11. Ariznawlll commented on Oct 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:83222fa8371007c7969d3aeca3ab8ced90695f4c
    • 部署方式:从该 SHA 本地编译、独立启动 1 CN / 1 TN / 1 LogService;SQL 监听 127.0.0.1:16001,数据目录与其他测试隔离。
    • 测试日期:2026-10-15。
    • 修复:MatrixOne PR #27070 已合入 main。

    原问题验证

    使用原 issue 的 src(id INT PRIMARY KEY, v VARCHAR(20)) 与两行 (1,'one'), (2,'two'),在同一会话执行 CREATE TEMPORARY TABLE dst CLONE src。修复后建表成功,SELECT id,v ORDER BY id 精确返回上述两行,SHOW CREATE TABLE 显示临时表与主键;不再出现原先首次 1146、之后名称被幽灵别名占用的现象。使用独立临时表名重复 3/3 轮,均成功;每轮 DROP 后同名普通临时表可创建、写入、查询。

    Happy Path 覆盖

    1. 三轮临时 CLONE 均返回 2 行,MIN(id)=1、MAX(id)=2,源表始终为 2 行、SUM(id)=3。
    2. 每轮 DROP TEMPORARY TABLE 后立即复用同名,分别写入 7/8/9 并查询,均为 1 行且值准确。
    3. 会话 A 中 CLONE 后,会话 B 读取同名目标返回 1146;B 可独立创建同名临时表并读到自己的 99,A 仍读到原来的 (1,'one'), (2,'two')。A 断开后,其他会话仍看不到其临时表,同名可重新创建。

    Unhappy Path 覆盖

    1. 同一会话重复 CLONE 已有临时目标返回预期 1050;原临时表仍是两行,未重复追加或损坏。
    2. CLONE 不存在的源表返回预期 1064;随后同会话立即创建同名普通临时表、写入 10 并读到 1 行,确认失败没有留下不可用的会话别名。
    3. 会话 B 对会话 A 的临时表读取返回 1146,不会越过会话可见性边界;两会话同名表互不覆盖。
    4. 清理后源表仍为 (1,'one'), (2,'two');测试库已删除。

    白盒验证

    本轮未做内部注入;原缺陷和失败原子性可由公开 SQL 与双会话直接观察。未把本地单 CN 测试扩大解释为多 CN 故障注入结果。

    回归测试

    • 已追加到当天唯一的 MOTR 汇总 PR #237,文件:测试脚本 与 精确结果。覆盖原始复现、双会话隔离、重复创建、DROP 后名称复用、缺失源失败后原子性。
    • 在上述 MatrixOne main SHA 上定向 compare 3/3 通过,合计 81/81 语句通过。
    • 12_git4data suite 运行 81 例:79 通过、1 跳过、1 失败。失败是另一用例 issue_26072_special_float_materialization 的冲突错误文案 golden 差异(预期 DELETE/DELETE,实际 UPDATE/UPDATE);该用例仍返回冲突并保持目标数据不变,与 [Bug]: CREATE TEMPORARY TABLE ... CLONE leaves an unqueryable session ghost after failing #27042 的 3/3 结果无关。未修改其 golden。
    • PR (AOE) incompatible bug fix #237 当前尚未合入;共享集成 CI 尚未验证,后续继续跟踪。此确定性会话别名问题不依赖数据规模或外部故障,无需额外 big-data/Chaos 用例。

    测试证据

    • 修复 PR #27070、MOTR PR #237 的定向脚本和预期输出。
    • 本地定向日志:/private/tmp/motr-27042-20261015.sa72nP/motr/var/log/run-1792064439020229000,摘要 total=3 success=3 failed=0、stmts=81 ok=81 fail=0。
    • 本地 suite 日志:/private/tmp/motr-27042-20261015.sa72nP/motr/var/log/run-1792064301301602000;失败项差异已在上文说明。

    测试结论

    测试通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions