Skip to content

[Bug]: Snapshot scope is ignored for cross-table reads and database-snapshot table restore #27116

Closed

Description

@Ariznawlll

Problem

The declared object scope of a snapshot is not enforced. A table snapshot can read a different table, and a database snapshot can restore a table from a different database. In both cases, the operation uses the snapshot timestamp and succeeds instead of being rejected.

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_scope_table;
DROP SNAPSHOT IF EXISTS repro_scope_database;
DROP DATABASE IF EXISTS repro_scope_a;
DROP DATABASE IF EXISTS repro_scope_b;

CREATE DATABASE repro_scope_a;
CREATE DATABASE repro_scope_b;
CREATE TABLE repro_scope_a.t (id INT PRIMARY KEY, v VARCHAR(16));
CREATE TABLE repro_scope_b.t (id INT PRIMARY KEY, v VARCHAR(16));
INSERT INTO repro_scope_a.t VALUES (1, 'a_before');
INSERT INTO repro_scope_b.t VALUES (1, 'b_before');

CREATE SNAPSHOT repro_scope_table FOR TABLE repro_scope_a t;
CREATE SNAPSHOT repro_scope_database FOR DATABASE repro_scope_a;
UPDATE repro_scope_a.t SET v = 'a_after';
UPDATE repro_scope_b.t SET v = 'b_after';

-- Control: reads the object that the table snapshot was created for.
SELECT 'control_table_snapshot' AS case_name, v
  FROM repro_scope_a.t{snapshot='repro_scope_table'};

-- Expected: reject because repro_scope_b.t is outside this table snapshot.
SELECT 'cross_table_read' AS case_name, v
  FROM repro_scope_b.t{snapshot='repro_scope_table'};

-- Expected: reject because repro_scope_b is outside this database snapshot.
RESTORE TABLE repro_scope_b.t{snapshot='repro_scope_database'};
SELECT 'cross_database_restore' AS case_name, v FROM repro_scope_b.t;

Expected behavior

Only the object represented by a table snapshot may be read through that snapshot. A database snapshot may restore only tables in its represented database. The two cross-scope operations should return an error and must not read or modify repro_scope_b.t.

Actual behavior

The complete repro ran 3/3 on a2f89230085a0f6650ec7bb7017ba6b8a97c9efc:

control_table_snapshot  a_before
cross_table_read        b_before
cross_database_restore  b_before

The control proves the snapshot timestamp is valid for its actual target. The other two rows show that an unrelated table is read and then restored to its historical value.

Root-cause hypothesis

In pkg/frontend/snapshot.go around lines 900-915, table-restore validation compares the target fields only when the snapshot itself is table-level; the database-snapshot/table-restore combination has no database-name equality check. In pkg/sql/compile/sql_executor_context.go around lines 420-453, relation resolution applies the snapshot timestamp but does not verify the table/database object identity.

Duplicate check

Searched open and closed issues, plus open pull requests, for snapshot scope and cross-table restore behavior. No matching issue or active fix was found.

Regression coverage

No test change is included with this report. After fixing, this should be covered as a deterministic snapshot BVT: both cross-scope statements must fail, while the same-object control remains successful.

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. added
    severity/s0
    ai-easy
    and removed on Aug 13, 2026
  5. gouhongshen commented on Aug 17, 2026

    @gouhongshen
    Contributor

    fixed by #27134

  6. added and removed
    severity/s0
    on Aug 17, 2026
  7. Ariznawlll commented on Aug 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:`main`
    • commit:`414fb8e0f854a4ab31519394542f93535e2c492f`
    • 部署方式:本地隔离 DISK-V2 单 CN + TN + LogService;普通 SQL 路径重复 3 次
    • 测试日期:2026-08-17

    原问题验证

    按 issue 原始步骤创建两个数据库、同名表、表级 snapshot 和数据库级 snapshot,更新 snapshot 后数据,再执行跨表读取和跨数据库 restore。当前 main 每轮均得到正确的对象范围校验:同表读取返回 `a_before`;跨表/跨库读取或 restore 返回 `ERROR 20101`;失败 restore 后目标表仍为 `b_after`。

    Happy Path 覆盖

    1. 同一表使用表级 snapshot 读取,3/3 返回 `a_before`。
    2. 同一数据库内使用数据库级 snapshot restore 同一表,3/3 恢复为 `a_before`。
    3. restore 后同库的非目标表仍为 `u_after`,未发生越界恢复。

    Unhappy Path 覆盖

    1. 将表级 snapshot 用于同库另一张表,3/3 返回 `ERROR 20101`,对象未被读取。
    2. 将表级 snapshot 用于另一数据库同名表,3/3 返回 `ERROR 20101`。
    3. 将数据库级 snapshot restore 到另一数据库同名表,3/3 返回 `ERROR 20101`;随后重读目标表仍为 `b_after`,验证失败原子性。

    白盒验证

    不适用:问题可由公开 snapshot SQL 完整触发;本次以服务级黑盒和失败后状态检查为准,未添加内部 fault injection。

    回归测试

    • 已加入 MOTR PR #113,用例:issue_27116_snapshot_scope.test。
    • 用例按 snapshot/PITR 共享元数据配置为独占执行;不需要 big-data、soak 或 chaos,问题是对象身份/数据库范围校验,不依赖规模或外部故障。

    测试证据

    • MOTR focused 命令:`./motr --config config-6002.yml --repeat 3 14_issue_regression.issue_27116_snapshot_scope --branch main`
    • 结果:`3/3 success`,`87/87 statements`;基线结果文件:issue_27116_snapshot_scope.result。
    • MatrixOne 修复提交:de40fe49。

    测试结论

    测试通过。

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