Skip to content

[Bug]: v4.2.1 multi-CN IVF post search returns nondeterministic candidates due to unordered LIMIT merge #28943

Closed

Description

@iamlinjunhong

Is there an existing issue for the same bug?

  • I searched existing issues and did not find the same bug.

Branch Name

Official tag v4.2.1 (unmodified source).

Commit ID

d2393868a7aaa6343518d80849fe4696aa3577e9

Other Environment Information

  • macOS ARM64, Go 1.26.4, native CPU build.
  • Local cluster with two CNs and local disk storage.
  • 1,000,000 rows; vecf32(1024); approximately 2 KB question text and 8 KB answer text per row.
  • One IVF index, lists=1024, vector_l2_ops; session probe_limit=5.
  • 894,354 rows satisfy the business filters.
  • All data was loaded before creating the index. The data directory and existing index were created with 4.0-dev and reused with v4.2.1, without rebuilding the index or changing the query vector.
  • Control: the same fixture/index/query was stable over 30 executions on 4.0-dev commit da7ad6be78eb48ee81b8b6c33c5a7651c9364587.

Actual Behavior

Repeated identical IVF mode=post queries return different ID multisets, not merely a different row order. The query contains two identical LIMIT 200 branches joined with UNION ALL.

Over 30 executions on v4.2.1, alternating between the two CN endpoints (15 executions per endpoint), every execution returned 400 rows. Results consisted of two disjoint groups A and B, each containing 200 IDs:

Result multiset Executions
A + A 25
B + B 1
A + B 4

Across the 29 consecutive comparisons:

  • 22 unchanged;
  • 5 replaced 200 rows;
  • 2 replaced all 400 rows.

For every repeated ID, its distance and vector-string hash remained identical. Independent float64 L2 recomputation differed by at most 4.935991660542927e-09 from the returned distance. The synthetic input vectors are finite; this reproduction does not require NaN.

Expected Behavior

With unchanged data/index/query, candidate selection across CN partitions must preserve the global distance ranking before applying the candidate budget. Arbitrary arrival order of CN results must not discard an entire partition's better candidates.

This report concerns whole candidate groups being replaced, not the unspecified output order of UNION ALL or an expectation that approximate search always matches exhaustive search.

Steps to Reproduce

This is a reproduced large-fixture case; a standalone minimized reproducer has not yet been prepared. The setup and query shape are below. The query-vector placeholder must be replaced with one fixed 1024-dimensional vector for the fixture, and held unchanged across runs.

  1. Start a two-CN cluster. Load the million-row fixture described below, then create its IVF index. In the executed experiment these steps ran on 4.0-dev, followed by restarting the same data directory with official v4.2.1.
  2. Set probe_limit=5 in each session.
  3. Execute two copies of the following branch as (<branch>) UNION ALL (<branch>), 30 times. Compare ID multisets, ignoring output order.
  4. Inspect EXPLAIN PHYPLAN and confirm that each branch dispatches ivf_search to both CNs and merges their output through an ordinary limit before the join.
CREATE TABLE ca_comprehensive_dataset (
    md5_id varchar(32) PRIMARY KEY,
    question text,
    answer text,
    content_type varchar(32),
    source_type varchar(16),
    question_vector vecf32(1024),
    allow_access varchar(16),
    allow_identities varchar(16),
    delete_flag int
);

-- Load all 1,000,000 rows first.
SET ivf_threads_build=4;
CREATE INDEX idx_question_vector USING ivfflat
ON ca_comprehensive_dataset(question_vector)
lists=1024 op_type 'vector_l2_ops';

SET probe_limit=5;

Each identical branch:

WITH t AS (
    SELECT md5_id, question, answer, content_type, source_type,
           l2_distance(question_vector, '<fixed 1024-dimensional vector>') AS vec_dist,
           allow_access, allow_identities, question_vector
    FROM ca_comprehensive_dataset
    WHERE delete_flag IS NULL
)
SELECT md5_id, question, answer, source_type, vec_dist, question_vector
FROM t
WHERE (
    locate('OPEN', allow_access) > 0
    OR (locate('ABO', allow_access) > 0
        AND locate('SA_O', allow_identities) > 0)
)
AND locate('日更', content_type) = 0
AND locate('直播', content_type) = 0
ORDER BY vec_dist
LIMIT 200 BY RANK WITH OPTION 'mode=post'
Synthetic fixture details
  • IDs are md5(cast(i as varchar)), for i=0..999999.
  • The first 740,000 vectors form a dense cluster around one normalized float32 vector. Every 370th row in that range uses a distinct normalized near vector, for 2,000 near vectors total. Near vectors are formed by adding Gaussian noise with standard deviation 1e-5 to the cluster vector, then normalizing.
  • Remaining rows use one of 64 normalized background vectors (i % 64). All vectors have dimension 1024 and finite components.
  • NumPy RNG seeds: base vectors 20260916, near-vector noise 20260917.
  • Per-row question/answer text is independently generated base64 text, approximately 2 KB/8 KB, to produce a substantial scan instead of a tiny highly compressible fixture.
  • content_type: 直播 when i % 97 == 0, otherwise 日更 when i % 89 == 0, otherwise 普通.
  • allow_access: CLOSED when i % 13 == 0, otherwise OPEN; allow_identities='SA_O'.
  • delete_flag=1 when i % 101 == 0, otherwise NULL.
  • Query selection after index creation: take the centroid of the largest bucket; generate a direction with RNG seed 20260918; remove its projection onto the span of nearby centroid offsets (distance < 0.01), normalize, and add 0.02 * direction to that centroid. Convert to float32 and freeze the result for all subsequent executions.
  • The executed fixture had 1,024 non-NULL centroids and approximately 740,000 entries in its largest selected bucket.

Additional information: execution plan and code analysis

The executed plan uses an IVF candidate budget of 260 for each outer LIMIT 200 branch. Each CN produces partition-local candidates. The physical plan merges them with an ordinary LIMIT, with no global distance TopN at that merge:

CN0: tablefunction -> projection -> limit --+
                                           merge -> limit -> hash build
CN1: tablefunction -> projection -> limit --+                    |
                                                      hash join -> top200

Actual physical-plan excerpt for one branch (the other branch has the same structure):

Scope 1 (Magic: Merge, mcpu: 1, Receiver: [0, 1])
  Pipeline: └── hash build
                └── limit
                    └── merge
  PreScopes: {
    Scope 1 (Magic: Remote, mcpu: 1, Receiver: [])
      DataSource: []
      Pipeline: └── connect to MergeReceiver [0]
                    └── limit
                        └── projection
                            └── tablefunction
    Scope 2 (Magic: Remote, mcpu: 1, Receiver: [])
      DataSource: []
      Pipeline: └── connect to MergeReceiver [1]
                    └── limit
                        └── projection
                            └── tablefunction
  }

Source chain pinned to the reproduced version:

  1. The IVF rewrite sets both IndexReaderParam.Limit and tableFuncNode.Limit: apply_indices_ivfflat.go.
  2. IVF search can be dispatched to multiple CNs, with partition-specific reader parameters: compile.go.
  3. The FUNCTION_SCAN compilation continues through compileSort: compile.go. A node with Limit and no OrderBy takes the compileLimit path: compileSort.
  4. compileLimit adds local limits, merges the scopes, and adds another ordinary limit: compile.go.
  5. That operator truncates by received row count, without comparing distances: limit.go.

Causal explanation: either CN can supply the entire 260-row candidate budget. Whichever partition arrives first can therefore displace all candidates from the other partition. The later business filters and outer Top200 cannot recover discarded candidates. The two UNION branches race independently, explaining the observed A+A, B+B, and A+B outcomes. UNION ALL exposes the two independent outcomes; the problematic candidate truncation is inside each branch.

Validation scope / remaining work

  • No production-code modifications or fix verification have been performed.
  • The executed v4.2.1 test reuses an index built on 4.0-dev; fresh index creation on v4.2.1 has not been covered by this experiment.
  • No historical bisect has been performed; the stable 4.0 control is not a claim about the exact introducing commit.
  • The inner scan reported DOP 10. v4.2.1 background explain records a representative coordinator-partition query, so its reported input-block count must not be treated as the total across CN partitions; see ivf_search.go.

Activity

  1. added a commit that references this issue on Sep 15, 2026
    115c227
  2. Ariznawlll commented on Sep 16, 2026

    @Ariznawlll
    Collaborator

    在最新官方 main(d5c4e8b832ad0275766f6d8f8f0be7ec4c0fb544)补充了与本问题相关的两 CN 验证;以下结果只覆盖当前提交,不替代 v4.2.1 的历史复现记录。

    IVF 多 CN 候选合并

    • 用 fresh 50,000 行 vecf32(4) IVF fixture(lists=8、probe_limit=5)复现原始的两个完全相同 mode=post 分支 UNION ALL 形状。
    • 过滤后 LIMIT 200 BY RANK 的结果与 mode=force 精确基线均为 200 行,行集 MD5 一致。
    • 30 次串行(两 CN 交替)和 40 个并发请求中,两个分支组成的 400 行 ID multiset 恒为同一个 MD5;没有出现原报告中的 A+A/B+B 分组替换。
    • 当前物理计划使用全局 Top 路径,而非历史版本中 merge -> ordinary limit 的候选截断形状。

    FULLTEXT2 + IVFFLAT 混合检索

    同表建立 FULLTEXT2(body) WITH PARSER ngram 与 IVFFLAT(v),在三个独立数据库中执行:

    SELECT id
    FROM docs
    WHERE MATCH(body) AGAINST('alpha' IN BOOLEAN MODE)
    ORDER BY l2_distance(v, '[0,0,0]')
    LIMIT 3;
    • 每轮两 CN 都返回 4,8,12;mode=force 的精确基线也是 4,8,12。
    • EXPLAIN 同时包含 Table Function on fulltext2_search 和 Vector Index Scan on ivf,即该 IVFFLAT 组合没有退化为仅全文回表排序。
    • prepared statement(动态 MATCH 词、向量和 LIMIT)复用后,在三个独立库、两 CN 中保持同一结果。

    分页边界补充

    同一 50,000 行 IVF fixture 还覆盖了非首段分页:

    ... ORDER BY l2_distance(v, '[0,0,0,0]')
    LIMIT 200 OFFSET 100 BY RANK WITH OPTION 'mode=post';
    • 两 CN 都返回 200 行,ID-set MD5 为 0dc16a82d224efe8795236201c201cf0,与 mode=force 一致。
    • 40 个并发请求(两个 CN 均分)都得到该 MD5。
    • 两个完全相同的分页分支组成 UNION ALL 后,40 个并发请求均为 400 行,按 ID multiset 的 MD5 为 f0f2f03d24ae90a8ab3180befb38b7bd。
    • 随后将同一数据目录切换为 1 Log、1 TN、2 CN 四个独立 mo-service 进程,再执行相同的 40 并发双分支查询;40 次仍全部为 400 行及同一 MD5。因而该结论不依赖两个 CN 位于同一进程。

    DML 可见性边界

    FULLTEXT2 是 CDC 驱动的 always-async 索引。UPDATE 后立即查询仍可观察到旧命中;本地两 CN 在后续 CDC 消费完成后都收敛到基表的新命中集合。该延迟符合当前 async 索引模型,不作为本 issue 的错误结果。

    另外发现的独立问题

    带 FULLTEXT/FULLTEXT2 的直接 MATCH 过滤与 ROW_NUMBER() OVER (...) 组合时,窗口算子会阻断全文 rewrite;两类索引、双 CN、各 3 个新库都稳定得到 ERROR 20105,而直接 MATCH 与无 MATCH 的窗口查询均正常。该问题与 IVF 全局候选合并无关,已单独记录为 #28974。

  3. added
    ai-easy
    on Sep 16, 2026
  4. Ariznawlll commented on Sep 16, 2026

    @Ariznawlll
    Collaborator

    测试版本

    • 分支:v4.2.1-hotfix
    • commit:9572bd3311abb16d1df939956408a727bcf83eba
    • 版本:8.0.30-MatrixOne-v4.2.1-hotfix-20260916
    • 部署:TKE,1 TN / 3 Log / 3 CN / 2 Proxy
    • namespace:mo-search-commit-9572bd3311-20260916
    • 测试日期:2026-09-16

    数据与索引

    • 数据库表:anli_test.ca_comprehensive_dataset
    • 数据量:715,679 行
    • 向量列:question_vector vecf32(1024)
    • 索引:IVFFLAT(question_vector),lists=256,vector_l2_ops
    • 查询参数:probe_limit=5

    当前 ANLI fixture 与 Issue 原始 fixture 存在两项规模差异:原始场景为 100 万行、lists=1024、2 CN;本次为 715,679 行、lists=256、3 CN。本次结论仅针对当前 ANLI 数据集和上述 hotfix commit。

    原问题验证

    从表中固定选择同一个非 NULL 查询向量,保持 Issue 的核心 SQL 形状:两个完全相同的 IVF mode=post 分支,每个分支执行带业务过滤条件的 ORDER BY l2_distance(...) LIMIT 200 BY RANK,随后通过 UNION ALL 合并。

    验证时没有用返回顺序作为判断依据,而是分别对两个分支的 200 个 md5_id 排序后计算 SHA256,并对合并后的 400 个 ID multiset 再计算 SHA256。查询在三个 CN 入口轮转执行,共 15 次,每个 CN 5 次。

    结果:

    • 15/15 次查询成功,查询失败数为 0。
    • 每次总计返回 400 行,两个分支分别稳定返回 200 行。
    • 左分支 15 次 ID-set SHA256 均为:d10189ff61f6479054ed36685817bb7f503f6fc1d0160682a61da9e672959f5a。
    • 右分支 15 次 ID-set SHA256 均为:d10189ff61f6479054ed36685817bb7f503f6fc1d0160682a61da9e672959f5a。
    • 合并后的 400 行 ID multiset SHA256 在 15 次执行中均为:d13ba4c109b14c0ac2807854057dd33a324fdfd4eb2cbd7734c04d6dc29d8f67。
    • 未出现 Issue 中记录的 A+A、B+B、A+B 候选集合漂移。

    汇总输出:

    EXACT_LEFT_HASH_DISTRIBUTION
         15 d10189ff61f6479054ed36685817bb7f503f6fc1d0160682a61da9e672959f5a
    EXACT_RIGHT_HASH_DISTRIBUTION
         15 d10189ff61f6479054ed36685817bb7f503f6fc1d0160682a61da9e672959f5a
    EXACT_COMBINED_HASH_DISTRIBUTION
         15 d13ba4c109b14c0ac2807854057dd33a324fdfd4eb2cbd7734c04d6dc29d8f67
    EXACT_SUMMARY_VALID=15 INFRA_RETRIES=0 QUERY_FAILURES=0
    

    执行计划与稳定性

    • 物理计划中可以看到各 CN 的局部 top 以及汇总阶段的 merge top,候选集合在应用全局 LIMIT 前按距离进行合并,不再是历史问题中的无序普通 LIMIT 合并路径。
    • 测试完成后,TN、Log、Proxy 和 3 个 CN Pod 均为 Running。
    • 所有 Pod 的 restart count 均为 0。
    • 测试期间未观察到查询卡死、Pod 重启或集群不可用。

    回归范围

    本次是现有 ANLI 表和已建立 IVF 索引上的黑盒验证,没有新增或修改回归脚本。该 commit 的修复说明及原始 100 万行 fixture 验证见修复提交 9572bd3311abb16d1df939956408a727bcf83eba(#28945)。

    测试结论

    测试通过。

  5. added a commit that references this issue on Sep 16, 2026
    d2a3391
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

ai-easykind/bugphase/testing

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions