Skip to content

[Bug]: DATA BRANCH from a snapshot links children to a recreated same-name table #26120

Closed

Description

@Ariznawlll

Describe the bug

When DATA BRANCH CREATE TABLE reads a named snapshot after the source table has been dropped and recreated with the same name, the new branch contains the correct historical data but its ancestry metadata points to the current same-name table rather than the table object stored in the snapshot.

The matching branch-protection snapshot also records the current table ID with a timestamp that predates that table's creation. Subsequent DATA BRANCH DIFF operations fail because the ancestry resolver looks for the current table ID at the historical timestamp.

Tested at commit 2eb7f523e5a10f352d3e258183ed8440510388de.

Steps to reproduce

drop snapshot if exists g4d_parent_identity_sp;
drop database if exists g4d_parent_identity;
create database g4d_parent_identity;
use g4d_parent_identity;

create table parent_t (
  id int primary key,
  val varchar(20)
);
insert into parent_t values (1, 'snapshot-row');
create snapshot g4d_parent_identity_sp
  for table g4d_parent_identity parent_t;

set @snapshot_parent_id = (
  select rel_id
  from mo_catalog.mo_tables {snapshot='g4d_parent_identity_sp'}
  where account_id = 0
    and reldatabase = 'g4d_parent_identity'
    and relname = 'parent_t'
);

drop table parent_t;
create table parent_t (
  id int primary key,
  val varchar(20)
);
insert into parent_t values (2, 'current-row');

set @current_parent_id = (
  select rel_id
  from mo_catalog.mo_tables
  where account_id = 0
    and reldatabase = 'g4d_parent_identity'
    and relname = 'parent_t'
);

data branch create table child_t
  from parent_t{snapshot='g4d_parent_identity_sp'};

select * from child_t;

select bm.table_id, bm.p_table_id, bm.clone_ts
from mo_catalog.mo_branch_metadata bm
join mo_catalog.mo_tables mt on mt.rel_id = bm.table_id
where mt.reldatabase = 'g4d_parent_identity'
  and mt.relname = 'child_t';

select sname, obj_id, ts
from mo_catalog.mo_snapshots
where kind = 'branch'
  and database_name = 'g4d_parent_identity'
  and table_name = 'parent_t';

data branch diff child_t against parent_t;

Actual behavior

The historical and current table IDs are different:

snapshot parent ID = 287580
current parent ID  = 287581

The branch data is correctly copied from the snapshot:

1  snapshot-row

However, both records use the current table ID:

mo_branch_metadata.p_table_id = 287581
mo_snapshots.obj_id            = 287581

DATA BRANCH DIFF then returns:

internal error: cannot find table 287581 commit ts at snapshot ...

The same result was reproduced with three independently created child tables.

The database-level form was also reproduced independently:

data branch create database g4d_parent_db_dst
  from g4d_parent_db_src{snapshot='g4d_parent_db_identity_sp'};
data branch diff g4d_parent_db_dst.parent_t
  against g4d_parent_db_src.parent_t;

For three destination databases, the snapshot table ID was 287586, the
current same-name table ID was 287587, and every destination branch recorded
p_table_id = 287587. Each database-level DATA BRANCH DIFF then failed
because table 287587 did not exist at the snapshot timestamp.

Expected behavior

The branch ancestry and protection metadata should refer to the source table object that actually exists in the named snapshot (287580 in the example). Operations on the historical branch should not resolve its parent by the current table name.

Code context

handleCloneTable correctly passes the snapshot to the clone operation, but its cloneReceipt stores only the source database and table names. updateBranchMetaTable later calls:

tcc.Resolve(receipt.srcDb, receipt.srcTbl, nil)

without the receipt snapshot. It therefore resolves the current same-name relation and stores its ID in both mo_branch_metadata and the branch-protection snapshot.

The database-level path uses the same receipt/update flow and was confirmed to
record the same incorrect parent identity after same-name table recreation.

Activity

  1. added this to the MO-202607 milestone on Jul 23, 2026
  2. added
    kind/bug
    on Jul 23, 2026
  3. added theissue type on Jul 23, 2026
  4. VioletQwQ-0 commented on Jul 29, 2026

    @VioletQwQ-0
    Collaborator

    PR #26310 merged as ccfcea46981aba349b4fa11445202939f1045c53 (reviewed head 3eb675911c59623dd98a77871bbd0118a98a7206). Required CI and snapshot-aware identity regressions passed.

    QA handoff on main f6dab28046d70412cec132f0068840896852101c:

    • For both table-level and database-level snapshot branches, drop and recreate a same-name source table after taking the snapshot.
    • Verify the branch uses the snapshot table ID rather than the recreated table ID.
    • Verify ancestry/protection metadata and a subsequent DIFF terminal.

    Pass: copied rows and parent identity come from the snapshot generation, metadata points to that generation, and DIFF succeeds. Fail: rebinding to the recreated table, wrong metadata, leaked protection, or DIFF failure.

  5. Ariznawlll commented on Jul 29, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 分支完成验证。

    测试版本:52ea9f874f492c368a20efef16b46ab8ced2de37
    测试拓扑:Proxy + 2 CN
    重复次数:3 轮

    测试过程:

    1. 创建父表并写入第一代数据,记录原始 object ID。
    2. 创建 snapshot。
    3. 删除父表,再用相同表名重新创建第二代对象并写入不同数据。
    4. 从 snapshot 创建表级和数据库级 Data Branch。
    5. 校验目标数据、父对象 ID、branch protection/ancestry 元数据,并继续执行 DIFF。

    测试结果:

    • Data Branch 使用的是 snapshot 中第一代表的历史 object ID,没有错误绑定到后来创建的同名表。
    • 目标表数据来自 snapshot 代际。
    • ancestry 和保护元数据均指向正确历史对象。
    • 后续 DIFF 正常完成。
    • 连续 3 轮结果一致。

    结论:该问题在上述最新 main commit 上验证通过。

  6. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在 main@0ebba1c0c96bbcf0ff4091236e10df7b97e24a0c 补充完成黑盒和白盒验证。

    黑盒验证:

    • MOTR 用例:issue_26120_data_branch_snapshot_identity.test
    • 表级路径:snapshot 后 drop/recreate 同名源表,再从 snapshot 创建 branch。
    • 数据库级路径:独立 snapshot、同名对象重建,再创建数据库 branch。
    • 检查 branch 数据来自历史 generation,不来自当前同名表。
    • 检查 mo_branch_metadata.p_table_id 和 protection snapshot obj_id 均等于 snapshot 内历史 relation ID,并与当前 relation ID 不同。
    • 执行 DIFF 证明保存的历史 parent identity 可继续使用,同时保留普通 snapshot branch 控制组。

    结果:该 case 42/42 条 SQL/断言通过;本轮 Data Branch 全集 13/13 case、280/280 条 SQL/断言通过。

    白盒验证:

    go test ./pkg/tests/issues \
      -run '^TestIssue26120SnapshotBranchKeepsHistoricalParentIdentity$' \
      -count=1
    ok github.com/matrixorigin/matrixone/pkg/tests/issues
    

    Unhappy path / 历史对象边界:

    • snapshot 后删除源表并创建同名新 generation,刻意制造“名称相同但 relation ID 不同”。
    • branch 数据必须来自旧 generation,不能错误读取当前同名表。
    • p_table_id 和 protection obj_id 必须等于历史 ID 且不等于当前 ID。
    • 创建完成后继续执行 DIFF,验证 ancestry 不会在后续解析时因错误对象 ID 返回 cannot find table ... at snapshot。
    • 表级和数据库级路径分别覆盖,避免只修复其中一个入口。

    结论:表级和数据库级 snapshot ancestry 均指向正确的历史对象,后续 DIFF 可用;黑盒回归已加入 motr#69。

  7. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    Unhappy Path / 历史对象专项验证

    测试版本:main@0ebba1c0c96bbcf0ff4091236e10df7b97e24a0c

    该专项刻意制造“对象名称相同、relation generation 不同”:

    1. 创建源表和 snapshot,记录 snapshot 内历史 relation ID。
    2. 删除源表,再创建同名新表并写入不同数据。
    3. 分别执行表级和数据库级 snapshot branch。
    4. 检查 branch 数据必须来自历史 generation。
    5. p_table_id 和 protection snapshot obj_id 必须等于历史 ID,且不等于当前 ID。
    6. 创建后继续执行 DIFF,检查不会返回 cannot find table ... at snapshot。

    结果:表级和数据库级 ancestry 均绑定历史对象;同名当前对象没有被误引用;后续 DIFF 正常。专项用例 42/42 条 SQL/断言通过,对应 Go 集成测试返回 ok。

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

Metadata

Metadata

Assignees

Labels

kind/bugphase/testing

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions