Skip to content

[Bug]: master index applies LIMIT/OFFSET twice and returns wrong pages #25586

Closed

Description

@XuPeng-SH

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Branch Name

optimize_limit

Commit ID

899e3a236e

Other Environment Information

  • Hardware parameters: local Linux/x86 development environment
  • OS type: Linux
  • Others: planner-level reproduction using the MatrixOne mock optimizer; the failure is independent of data distribution once at least LIMIT + OFFSET rows match.

Actual Behavior

A query using a master index with LIMIT ... OFFSET ... receives the same pagination clause twice:

  1. applyIndicesForFiltersUsingMasterIndex puts the original Limit and Offset on the final master-index scan/intersection node.
  2. It then copies the same Limit and Offset to the outer INDEX join used for the base-table lookup.

Both nodes are compiled through compileSort, so the inner operation first skips OFFSET rows and limits the candidate set; the outer lookup then skips OFFSET rows again.

For a query with at least 15 matching rows, LIMIT 10 OFFSET 5 can therefore return only 5 rows and starts from the wrong logical page.

The isolated plan reproduction produced both of these nodes:

JOIN (master-index intersection)  limit=10 offset=5
JOIN (INDEX/base-table lookup)    limit=10 offset=5

Relevant code:

  • pkg/sql/plan/apply_indices_master.go:82-85
  • pkg/sql/plan/apply_indices_master.go:113-120
  • pkg/sql/compile/compile.go:1118-1131

Expected Behavior

LIMIT 10 OFFSET 5 must be applied exactly once to the final result. When at least 15 rows match, the query must return 10 rows after skipping the first 5 rows.

The index path may fetch LIMIT + OFFSET candidates internally, but only the final layer may apply the user-visible OFFSET.

Steps to Reproduce

CREATE TABLE t_master_limit (
  id INT PRIMARY KEY,
  a VARCHAR(20),
  b VARCHAR(20),
  INDEX idx_master USING master (a, b)
);

-- Insert at least 15 rows with the same indexed values.
INSERT INTO t_master_limit
SELECT result, 'same-a', 'same-b'
FROM generate_series(1, 30);

SELECT id
FROM t_master_limit
WHERE a = 'same-a' AND b = 'same-b'
LIMIT 10 OFFSET 5;

Inspect the logical/physical plan. The current master-index rewrite attaches LIMIT 10 OFFSET 5 to both the internal master-index node and the outer INDEX join. The result can contain only 5 rows rather than 10.

Additional information

Severity rationale: this is a stable wrong-result bug in an active secondary-index query path. It has a confirmed owner and a small deterministic regression-test shape.

Suggested fix direction:

  • keep the original LIMIT/OFFSET only on the outer INDEX join; or
  • fetch LIMIT + OFFSET from the inner index path and leave the user-visible OFFSET only on the outer layer, using overflow-safe arithmetic.

Add regression coverage for one and multiple master-index predicates, with and without OFFSET.

Activity

  1. added this to the MO-202607 milestone on Jul 10, 2026
  2. self-assigned this
    on Jul 10, 2026
  3. added
    kind/bug
    severity/s0
    on Jul 10, 2026
  4. added theissue type on Jul 10, 2026
  5. added a commit that references this issue on Jul 11, 2026
    df6a341
  6. XuPeng-SH commented on Jul 12, 2026

    @XuPeng-SH
    ContributorAuthor
  7. Ariznawlll commented on Jul 13, 2026

    @Ariznawlll
    Collaborator

    已在最新 main@2865ed6cc8a39727b8b274bb52b88fe441d0b30c 验证通过。

    • 对应修复:df6a34119 / fix(plan): preserve pagination across LIMIT optimizations #25587。
    • planner TestMasterIndexPaginationIsAppliedOnce 使用 -race -count=5 通过。
    • 真实服务端扩展覆盖 7 种 master-index 分页:双谓词、单谓词、IN、offset=0、中页、尾页、offset=结果集长度,以及 ORDER BY id LIMIT 10 OFFSET 5 的精确内容;全部结果正确。
    • 新增 MOTR 端到端回归,连续 5 轮通过:matrixorigin/motr#48。

    未再出现 OFFSET 被消费两次,修复有效。

  8. Ariznawlll commented on Jul 13, 2026

    @Ariznawlll
    Collaborator

    已在最新 main 上完成验证,并在 matrixorigin/motr#48 中补充回归测试。

    验证环境

    • MatrixOne:main@52afd25fa63a3cb6037b7bf8ff0bf1328c4793ec
    • motr 用例:issue_25586_master_index_limit_offset

    测试过程

    围绕 master index scan 中 LIMIT/OFFSET 被重复应用的问题,覆盖:

    1. 同一查询包含两个 master-index predicate;
    2. 单个 master-index predicate;
    3. IN predicate;
    4. offset=0 的首页;
    5. 中间页 offset;
    6. 尾页和刚好到末尾的 offset;
    7. 超出结果集范围的 offset;
    8. 使用稳定 ORDER BY 检查每一页的具体行内容,而不只检查行数。

    验证结果

    • LIMIT/OFFSET 只应用一次。
    • 首页、中间页、尾页和越界页内容均符合预期。
    • 单 predicate、多 predicate 和 IN 路径结果一致且稳定。
    • 连同本批测试连续执行 5 轮均通过。
    SUMMARY | total=50 success=50 failed=0 skipped=0 | stmts=625 ok=625 fail=0
    
  9. added 3 commits that reference this issue on Sep 17, 2026
    a39592d
    b6e6261
    026cf7a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions