Skip to content

[Bug]: RESTORE TABLE silently skips a referenced table and leaves post-snapshot data #27117

Open

Description

@Ariznawlll

Problem

RESTORE TABLE can report success while leaving its target unchanged when another table has a foreign-key reference to that target. This is a silent data-restore failure: a historical read sees the snapshot value, but the successful restore leaves the post-snapshot value in place.

Environment

  • Branch: main
  • Commit: a2f89230085a0f6650ec7bb7017ba6b8a97c9efc
  • Build: local official-main binary at that commit
  • Topology: isolated standalone LOG/TN/CN, with a shared temporary data path

Steps to reproduce

DROP SNAPSHOT IF EXISTS repro_fk_control_snapshot;
DROP SNAPSHOT IF EXISTS repro_fk_parent_snapshot;
DROP DATABASE IF EXISTS repro_fk_control;
DROP DATABASE IF EXISTS repro_fk_case;

-- Normal table-restore control.
CREATE DATABASE repro_fk_control;
CREATE TABLE repro_fk_control.t (id INT PRIMARY KEY, v VARCHAR(16));
INSERT INTO repro_fk_control.t VALUES (1, 'before');
CREATE SNAPSHOT repro_fk_control_snapshot FOR TABLE repro_fk_control t;
UPDATE repro_fk_control.t SET v = 'changed';
RESTORE TABLE repro_fk_control.t{snapshot='repro_fk_control_snapshot'};
SELECT 'control_table_restore' AS case_name, v FROM repro_fk_control.t;

CREATE DATABASE repro_fk_case;
CREATE TABLE repro_fk_case.parent_t (id INT PRIMARY KEY, v VARCHAR(16));
CREATE TABLE repro_fk_case.child_t (
  cid INT PRIMARY KEY,
  pid INT,
  CONSTRAINT fk_parent FOREIGN KEY (pid) REFERENCES repro_fk_case.parent_t(id)
);
INSERT INTO repro_fk_case.parent_t VALUES (1, 'before');
INSERT INTO repro_fk_case.child_t VALUES (1, 1);
CREATE SNAPSHOT repro_fk_parent_snapshot FOR TABLE repro_fk_case parent_t;
UPDATE repro_fk_case.parent_t SET v = 'changed';

-- Historical read control.
SELECT 'fk_snapshot_read' AS case_name, v
  FROM repro_fk_case.parent_t{snapshot='repro_fk_parent_snapshot'};
RESTORE TABLE repro_fk_case.parent_t{snapshot='repro_fk_parent_snapshot'};
SELECT 'fk_table_restore' AS case_name, v FROM repro_fk_case.parent_t;

Expected behavior

The restore should either restore parent_t.v to before, or return an explicit unsupported-operation error. It must not report success while leaving changed in the target table.

Actual behavior

The complete repro ran 3/3 on a2f89230085a0f6650ec7bb7017ba6b8a97c9efc:

control_table_restore  before
fk_snapshot_read       before
fk_table_restore       changed

The ordinary restore and the historical read controls both return before. Only the restore of the referenced table returns successfully but retains changed.

Root-cause hypothesis

pkg/frontend/snapshot.go lines 1632-1636 call checkTableIsMaster; for a referenced table the function logs that it will skip the restore and returns without an error. The caller therefore treats the statement as successful although the table was not recreated from the snapshot.

Duplicate check

Searched open and closed issues, plus open pull requests, for table restore with foreign keys. Existing historical work covers other restore failures and FK refactoring, but no report matches a successful table restore that leaves the referenced target unchanged.

Regression coverage

No test change is included with this report. After fixing, a deterministic snapshot BVT should assert that the restore either produces before or fails explicitly; returning success with changed must be prohibited.

Activity

  1. added this to the MO-202608 milestone on Aug 13, 2026
  2. added theissue type on Aug 13, 2026
  3. added
    kind/bug
    on Aug 13, 2026
  4. gouhongshen commented on Aug 13, 2026

    @gouhongshen
    Contributor

    Automated AI triage

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

    Reason

    A maintainer must choose RESTORE TABLE semantics when the target is an FK parent: cascade dependent tables or reject with a documented error. Next action: define the contract and acceptance BVT, then requeue.

    Evidence

    snapshot.go filters FK dependencies by the requested table, while recreateTable returns success after detecting a master table, matching the reported silent no-op.

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

  5. added
    severity/s0
    ai-easy
    and removed on Sep 4, 2026
  6. gouhongshen commented on Sep 4, 2026

    @gouhongshen
    Contributor

    PR #28126 is trying to fix this issue.

  7. gouhongshen commented on Sep 17, 2026

    @gouhongshen
    Contributor

    Fixed by PR #28126, merged to main as db39156.

    The explicit RESTORE TABLE path now rejects a target referenced by a foreign key with a shared not-supported error before destructive restore work begins. It therefore cannot report success while silently retaining post-snapshot data. Database and account bulk restore keep their existing referenced-master skip behavior.

    Verification recorded in the merged PR:

    • focused frontend unit coverage for target validation, explicit versus bulk behavior, metadata lookup errors, and rollback-before-mutation;
    • full pkg/frontend test and coverage runs;
    • isolated standalone mo-tester: restore_table_referenced_by_fk 26/26, restore_fk_table 289/289, restore_fk_restore_master_table 100/100;
    • direct issue reproduction now returns the explicit unsupported-operation error and preserves the changed parent and child rows.

    Closing: the reported silent-success behavior is eliminated. Coordinated restoration of dependent tables remains intentionally unsupported and out of scope.

  8. gouhongshen commented on Sep 24, 2026

    @gouhongshen
    Contributor

    PR #28126 has merged and is awaiting manual validation. The issue remains open.

  9. Ariznawlll commented on Oct 16, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:48dbe277b6d20216c1c513d03c5cbd681380a8a2
    • 部署方式:本地隔离的单 CN/TN/Log 集群,SQL 端口 16042,运行时 git_version()=48dbe277b6
    • 测试日期:2026-10-16

    原问题验证

    PR #28126 修复的 snapshot 显式表恢复分支已按预期拒绝 FK 父表:

    -- parent_t(id PK,v)=(1,'before'); child_t(1,1) 通过 FK 引用 parent_t(1)。
    -- 创建数据库 snapshot/PITR,历史时间点 T 后把 parent_t.v 改成 'changed'。
    RESTORE TABLE db.parent_t{snapshot='snap'};
    -- ERROR 20105: can not restore table 'db.parent_t' referenced by some foreign key constraint

    但同一条件下,PITR 显式表恢复仍然是原问题的静默成功:

    RESTORE DATABASE db TABLE parent_t FROM PITR policy_name 'T';
    SELECT v FROM db.parent_t;
    -- RESTORE 退出码 0;SELECT 返回 changed,而历史值为 before。

    在 hb_restore_pitr_fk_1618、hb_pitr_fk_r2_1622、hb_pitr_fk_r3_1622 三个全新数据库中均复现(3/3)。第一组 PITR 时间点为 2026-10-16 15:19:51。父子数据和 FK 元数据保持原样,未发生半恢复;但调用者会错误地认为父表已恢复。

    Happy Path 覆盖

    在后两组中,同一有效 PITR 时间点恢复没有 FK 引用的 plain_t,退出码均为 0,v 从 changed 恢复为 before。因此不是时间点无效,也不是 PITR 整体失效。另一个独立数据库级 PITR/snapshot 恢复,包含生成列、唯一索引和视图,结果与历史状态一致。

    Unhappy Path 覆盖

    1. snapshot 显式恢复被引用父表:3/3 返回 20105,父/子当前数据保留,后续查询正常。
    2. PITR 显式恢复被引用父表:3/3 错误地返回成功,父表仍为 changed,子表仍为 (1,1),后续查询正常。
    3. 未来 PITR 时间点:返回 20101,原表和视图不变。

    白盒验证

    本轮做了源码路径对照,没有新增或运行专门 UT。pkg/frontend/snapshot.go:908,2221-2244 在显式表恢复前调用 validateRestoreTableTarget 并返回明确错误;pkg/frontend/pitr.go:1677-1683 检出 FK 父表后直接 return 成功,显式 PITR 入口没有对应预检。这解释了两个命令的不同结果;checkTableIsMaster 的错误处理另有静态疑点,本评论不把它当作已复现问题。

    回归测试

    本轮未新增仓库测试或 PR。后续回归应在同一 FK 父表场景中分别断言 snapshot 与 PITR 显式表恢复:要么按定义协调恢复依赖,要么在修改前明确报不支持;不能成功返回却保留修改后的数据。数据库/账号批量恢复的现有跳过语义应单独测试,不与显式表恢复混同。

    测试证据

    固定 commit、3 组独立库、同时间点普通表对照及精确输出保存在隔离本机记录 /private/tmp/mo-explore-run-20261016/snapshot_pitr_whitebox_20261016.md。关键读数:parent_t.v=changed,plain_t.v=before,snapshot 显式父表恢复报 20105。此评论中的 SQL 和输出也可直接独立复核。该 PITR 分支与已关闭的 #19377(数据库级恢复 FK 父表时报错)不同。

    snapshot 显式表恢复修复有效;PITR 同语义入口仍保留原问题,原 issue 的跨入口验收尚未满足。

    测试结论

    测试不通过。

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

Metadata

Metadata

Assignees

Labels

ai-easykind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions