Skip to content

[Bug]: IVFFLAT PRE and AUTO exact membership returns empty after entries persistence or physical copy #29042

Closed

Description

@Ariznawlll

Problem

After ALTER TABLE ... ADD COLUMN rebuilds a table that has an IVFFLAT index,
an explicit mode=pre vector query returns an empty result even though matching
rows remain in the base table and in the vector index.

The wrong result is not tied to an old prepared plan. It also occurs with a
newly parsed static query and with a PreparedStatement created after the ALTER.
It affects both vector_l2_ops/l2_distance and
vector_l2sq_ops/l2_distance_sq, with and without IVFFLAT INCLUDE columns.

Environment

  • MatrixOne commit: 01d60e1c4ded1b0f3fc1a4ecd75ce54e95e23b90
  • Version: 8.0.30-MatrixOne-v1.3.0
  • Topology: 2 CN / 1 TN / 1 LogService
  • SQL endpoints: both CNs were checked independently

Minimal reproduction

SET experimental_ivf_index = 1;
SET probe_limit = 1;

CREATE DATABASE ivf_copy_alter_pre;
USE ivf_copy_alter_pre;

CREATE TABLE t (
  id INT PRIMARY KEY,
  grp INT,
  v VECF32(2)
);

INSERT INTO t VALUES
  (1, 1, '[0,0]'),
  (2, 1, '[1,0]'),
  (3, 2, '[0,1]'),
  (4, 2, '[2,0]');

CREATE INDEX ix USING IVFFLAT ON t(v)
  LISTS = 1 OP_TYPE 'vector_l2_ops' INCLUDE(grp);

SELECT id FROM t
WHERE grp = 1
ORDER BY l2_distance(v, '[0,0]')
LIMIT 2 BY RANK WITH OPTION 'mode=pre';
-- 1, 2

ALTER TABLE t ADD COLUMN note INT DEFAULT 7;

SELECT id FROM t
WHERE grp = 1
ORDER BY l2_distance(v, '[0,0]')
LIMIT 2 BY RANK WITH OPTION 'mode=pre';
-- empty result

Actual result

After the ALTER, mode=pre returns zero rows.

The post-ALTER plan still chooses the membership-filter topology:

Table Scan on t (grp = 1)
  -> Vector Index Scan on ix
       Runtime Filter Probe
  -> SEMI JOIN
       Runtime Filter Build
       Table Scan on t (grp = 1)

The base-table producer has matching primary keys, but none survive the runtime
membership filter at the IVFFLAT scan.

Expected result

mode=pre should continue to return IDs 1, 2 after an unrelated column is
added.

At the same post-ALTER state, the following controls all return IDs 1, 2:

  • mode=force
  • mode=post
  • mode=include when grp is an INCLUDE column
  • mode=auto in the four-row reproduction

Reproducibility and scope

  • 3 independent database recreations
  • 2 CN endpoints per recreation
  • 6/6 minimal observations returned the same empty mode=pre result
  • A broader matrix reproduced the same behavior for L2 and L2SQ, static SQL,
    pre-ALTER PreparedStatements, and post-ALTER PreparedStatements
  • The same queries return the expected rows before the ALTER

This is a correctness issue in the explicit pre-filter path after COPY ALTER,
not approximate-nearest-neighbor recall variance: LISTS=1 and
probe_limit=1 scan the only IVF list, and the control paths return both rows.

Activity

  1. added this to the MO-202609 milestone on Sep 17, 2026
  2. added
    kind/bug
    on Sep 17, 2026
  3. added theissue type on Sep 17, 2026
  4. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    Expanded the post-DDL scope on the same commit and 2-CN topology.

    The empty mode=pre result occurs after these unrelated schema changes:

    ALTER TABLE t ADD COLUMN note INT DEFAULT 7;
    ALTER TABLE t ADD COLUMN note INT DEFAULT 7 FIRST;
    ALTER TABLE t ADD COLUMN note INT DEFAULT 7 AFTER grp;
    ALTER TABLE t DROP COLUMN obsolete;
    ALTER TABLE t CHANGE COLUMN payload payload2 VARCHAR(40);
    ALTER TABLE t ALTER COLUMN payload SET DEFAULT 'x';

    Each statement leaves the base rows queryable and the IVFFLAT index usable by
    mode=post; when the filter column is covered, mode=include also remains
    correct. Only the membership-based mode=pre path returns no rows. The same
    behavior is visible immediately from both CN endpoints.

    These controls do not produce the empty result in the same fixture:

    ALTER TABLE t RENAME COLUMN payload TO payload2;
    ALTER TABLE t MODIFY COLUMN payload VARCHAR(40);
    ALTER TABLE t ADD INDEX ix_grp(grp);
    ALTER TABLE t RENAME COLUMN grp TO category;
    ALTER TABLE t MODIFY COLUMN grp BIGINT;
    ALTER TABLE t RENAME TO t_new;

    A synchronous IVFFLAT rebuild restores pre-filter results:

    ALTER TABLE t ALTER REINDEX ix IVFFLAT FORCE_SYNC;

    After that statement, the minimal query again returns IDs 1, 2 in
    mode=pre on both CNs. This narrows the problem to the IVFFLAT state produced
    or retained by the affected COPY ALTER operations rather than to the added,
    dropped, or defaulted column itself.

  5. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    进一步缩小了影响路径:异常有精确的 100/101 候选主键数边界。

    在 500 行表上创建 IVFFLAT lists=5,随后执行会触发 COPY ALTER 的 ADD COLUMN,使用非主键谓词形成不同大小的成员集合,并在两个 CN 上分别比较 mode=pre 与 mode=force:

    成员集合大小 mode=pre
    1、2、5、10、50、99、100 返回空结果
    101、102、150、250、500 与 mode=force 一致

    ALTER 前,以上全部规模都返回正确结果;ALTER 后,两套 CN 的结果完全一致。mode=auto 在这组简单查询中也保持正确。

    这个边界与 pkg/vectorindex/ivfflat/search.go 中的 exactPkFilterThreshold = 100 完全吻合:小成员集合进入 exactRelationMembershipScan,101 个及以上成员走另一条 membership-filter 路径。结合前一组 EXPLAIN ANALYZE,异常表的隐藏 entries 扫描能够读到索引行,但 exact storage membership filter 将其从 4 行过滤为 0 行;正常对照表则能输出匹配行。

    因此问题可以进一步限定为:COPY ALTER 生成的 IVFFLAT 隐藏 entries 对象在“小成员集合 exact storage membership”优化路径上无法匹配;通用的大成员集合过滤路径不受影响。ALTER REINDEX ... IVFFLAT FORCE_SYNC 会重写物理索引状态并恢复该路径。

  6. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    主键编码矩阵进一步限定了 #29042 的影响范围。相同的 4 行表、相同谓词和 COPY ALTER,在两套 CN 上得到以下结果:

    主键类型 ALTER 前 PRE ALTER 后 PRE
    INT 正确 空结果
    BIGINT 正确 空结果
    BIGINT UNSIGNED 正确 空结果
    VARCHAR 正确 正确
    BINARY 正确 正确
    UUID 正确 正确
    DECIMAL(20,0) 正确 正确
    复合主键 (INT,VARCHAR) 正确 正确

    因此 COPY ALTER 后 exact storage membership 的异常目前只落在整数族主键编码上,并不是所有主键类型或所有 PRE membership 都损坏。

    CHAR 主键暴露了另一个与 ALTER 无关的 PRE runtime-key 类型校验问题,已单独记录为 #29045,避免混入本 issue。

  7. changed the title [-][Bug]: IVFFLAT mode=pre returns an empty result after COPY ALTER[/-] [+][Bug]: IVFFLAT mode=pre returns empty after COPY ALTER, clone, snapshot clone, or data branch[/+] on Sep 17, 2026
  8. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    进一步确认问题不局限于 COPY ALTER,而是所有会复制 IVFFLAT 隐藏物理表的生命周期路径。

    从一个最终 schema 已经确定、且 PRE 查询完全正确的 500 行源表分别创建:

    • 普通 CREATE TABLE ... CLONE ...;
    • snapshot clone;
    • DATA BRANCH CREATE TABLE ... FROM ...。

    源表和三个派生表都保留同一 IVFFLAT 定义。两个 CN 上比较 mode=pre 与 mode=force 后得到完全相同的边界:

    表 成员数 1/2/50/99/100 成员数 101/102/250/500
    原始源表 正确 正确
    普通 clone 空结果 正确
    snapshot clone 空结果 正确
    data branch 空结果 正确

    三个派生表的执行计划都选择 Vector Index Scan,并非索引未被使用。分别执行 ALTER TABLE ... ALTER REINDEX ... IVFFLAT FORCE_SYNC 后,小成员集合 PRE 结果恢复。

    这与 COPY ALTER 的整数主键、100/101 阈值和 REINDEX 恢复特征完全一致,说明缺陷位于克隆 IVFFLAT entries 物理对象后的小整数主键 exact-storage-membership 匹配,不是 COPY ALTER 上层语法独有的问题。

  9. changed the title [-][Bug]: IVFFLAT mode=pre returns empty after COPY ALTER, clone, snapshot clone, or data branch[/-] [+][Bug]: IVFFLAT PRE and AUTO retry return empty after COPY ALTER, clone, snapshot clone, or data branch[/+] on Sep 17, 2026
  10. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    影响并不限于显式 L2 PRE:

    1. 普通 clone 后,vector_l2_ops、vector_l2sq_ops、vector_l1_ops、vector_ip_ops、vector_cosine_ops 全部复现小整数主键 PRE 空结果;VECF32 与 VECF64 都受影响。各源表 PRE 与 FORCE 一致,派生表 FORCE 正确、PRE 为空。
    2. 默认 mode=auto 在发生 AUTO -> PRE correctness retry 时也会返回空结果。

    AUTO 重试复现使用 1,000 行,只有距离较远的 ID 901..950 满足谓词,查询请求 10 行:

    SELECT id FROM t WHERE category=1
    ORDER BY l2_distance(v,'[0,0]')
    LIMIT 10 BY rank WITH OPTION 'mode=auto';

    两个 CN 的结果均为:

    表 FORCE POST PRE AUTO
    原始源表 901..910 空 901..910 901..910
    普通 clone 901..910 空 空 空

    源表的 AUTO 能在 POST underfill 后通过 PRE 重试补满;clone 表的重试进入损坏的 exact-membership 路径,因此默认 AUTO 也产生静默 false negative。该问题会影响依赖 AUTO correctness retry 的正常查询,并非只有主动选择 mode=pre 的用户可见。

  11. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    增量写入进一步证明这是克隆 entries 物理对象的问题,并且实际表现不一定始终“全空”,还可能是新旧数据混合的部分 false negative。

    在含 10 条匹配行的源表上建立 IVFFLAT 后执行普通 clone,随后在 clone 表依次执行写入;两个 CN 的 PRE 结果完全一致:

    阶段 FORCE 可见 ID PRE 可见 ID
    clone 完成 1..10 空
    INSERT id=0 0..10 仅 0
    UPDATE id=1 的向量列 0..10 0,1
    UPDATE id=2 的无关列 0..10 仍为 0,1
    ODKU 重写 id=3 的向量列 0..10 0,3,1
    REPLACE id=4 0..10 0,3,4,1
    IVFFLAT FORCE_SYNC REINDEX 0..10 与 FORCE 全部一致

    clone 后新写入的 entries 行可以被 exact membership 正常匹配;从源索引物理复制过来的旧 entries 行不能匹配。只有会重写向量索引项的 DML 才让对应旧行重新可见,修改无关列没有效果。

    因此线上表在 clone/data branch/COPY ALTER 后继续写入时,PRE/AUTO-retry 可能返回“部分新行 + 少量被重写旧行”,而不是稳定的空结果,这类结果更难通过简单的非空检查发现。

  12. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    持久化边界进一步排除了“只有从源表复制来的旧 object 损坏”的解释。

    复现步骤:

    1. 正常源表建立 IVFFLAT,普通 clone;
    2. clone 表初始 10 条旧行在 PRE 中全部不可见;
    3. 向 clone 表 INSERT 新的 id=0;PRE 暂时只返回 id=0;
    4. 分别对 clone 基表和它的 IVFFLAT entries 隐藏表执行 mo_ctl('dn','flush',...)。

    两个 CN 的结果:

    阶段 FORCE PRE
    INSERT 后、flush 前 0..10 仅 0
    基表与 entries flush 后 0..10 空

    新写入索引项在内存态能够通过 exact membership;一旦成为 clone 隐藏 entries 表的持久化 object,也无法通过该过滤。源/clone 隐藏 entries 的 catalog 列号和 attr_seqnum 均一致,因此问题更接近 clone 后 relation/object 身份或持久化 reader 对 membership hint 的映射,而不是用户列定义、索引行内容或单纯旧 object 复用。

    普通 flush 不是恢复手段;在有 clone 后写入的场景中,它会让暂时可见的新索引行也消失。当前验证中只有 IVFFLAT REINDEX 能稳定恢复持久化 PRE/AUTO-retry 结果。

  13. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    FULLTEXT2 + IVFFLAT 混合检索同样会触发本问题,并且已排除全文索引未就绪。

    构造 1,000 行表,其中全文词 rareterm 只命中 ID 901..950;源表同时建立 FULLTEXT2 与 IVFFLAT,普通 clone 后对 clone 的 FULLTEXT2 单独执行 FORCE_SYNC REINDEX,不重建 IVFFLAT。

    两个 CN 上:

    • 源表和 clone 的独立 MATCH 都稳定返回 50 行 901..950;
    • 混合查询计划同时包含 fulltext2_search 与 Vector Index Scan;
    • 源表:FORCE=901..910,POST=空,PRE=901..910,AUTO=901..910;
    • clone:FORCE=901..910,POST=空,PRE=空,AUTO=空。

    查询形状:

    SELECT id FROM docs
    WHERE MATCH(body) AGAINST('+rareterm' IN BOOLEAN MODE)
    ORDER BY l2_distance(v,'[0,0]')
    LIMIT 10 BY rank WITH OPTION 'mode=auto';

    全文召回的 50 个整数主键正好进入 IVFFLAT 的 small exact-membership 路径。源表 AUTO 能从空的 POST 结果重试 PRE;clone 表的 PRE membership 无法匹配持久化 entries,最终默认 AUTO 静默返回空。该影响覆盖常见的“全文召回 + 向量重排”组合,而不只是普通列谓词。

  14. Ariznawlll commented on Sep 17, 2026

    @Ariznawlll
    CollaboratorAuthor

    主键编码矩阵补充:clone 后的持久化 exact-membership 问题覆盖全部常见整数宽度,而非只有 INT/BIGINT。

    这与 docfilter 对整数 key 使用 exact integer set、其他类型使用另一种 membership 表示的代码分支相吻合;BOOL 的错误表现不同,但仍只发生在 clone 后持久化 entries + exact membership 组合中。

  15. 7 remaining items

  16. cpegeric commented on Sep 17, 2026

    @cpegeric
    Contributor

    @iamlinjunhong please take a look.

  17. LeftHandCold commented on Sep 18, 2026

    @LeftHandCold
    Collaborator

    补充一条来自 Main Nightly 的复现证据。结论是:这不是普通的 ANN recall 波动,也不像环境/OOM 导致的测试误报;它是一个高可信的产品正确性异常,并且当前证据更支持“#29042 的同一持久化边界/关联路径”,暂时不另建重复 Issue。

    Run / Job:

    本次运行实际观察到的顺序(UTC,2026-09-17)

    1. 21:36:38.012:IVF entries 写入完成,AffectedRows=5。
    2. 21:36:39.204:TN 对该 entries 表执行 flusher.force。
    3. 21:36:39.594:该表 FLUSH-START。
    4. 21:36:39.663:FLUSH-END,记录 aobj-merge-rows=5、tombstone-rows=0。
    5. 全文过滤在混合查询前连续三次返回 4 行,说明 FULLTEXT 候选已就绪。
    6. 21:36:42.462–42.533:IVF centroids 和同一 entries 表均完成查询侧加载/订阅。
    7. 21:36:42.599:mixed-fulltext-vector 返回空集;期望结果为 1,2,4。

    该用例是 5 行固定数据、mode=pre、lists=2、probe_limit=5,距离没有并列;失败发生在后续 DML/concurrency 步骤之前。因此这不是排序差异,也不是写入尚未完成导致的普通测试时序问题。

    与 #29042 的对应关系

    #29042 的后续验证已经表明:即使没有 Clone/COPY ALTER,普通源表的 IVFFLAT entries 在持久化后,非主键谓词生成的小成员集合也可能使 PRE 查询返回空集,而 FORCE 仍然正确:

    本次运行同时具备:整数主键、全文条件生成的小候选集合、mode=pre,以及查询前已明确完成的 entries 持久化。因此它与该 Issue 的触发条件高度吻合。

    仍未声称已经完成的证明

    原始 ANLI 脚本失败后会清理数据库,没有保存同一 fixture 的 FORCE/POST、精确扫描或 entries 持久化前后的对照结果;因此本条证据还不能单独证明具体错误代码行,也不能据此判断是近期提交引入的回归。它也不是 #28843 的首次访问超时问题。

    资源侧没有发现支持 OOM/重启解释的证据:故障窗口 TP CN working-set 峰值约 40.4 GiB(55 GiB limit),TN 约 22.9 GiB(32 GiB limit),相关 Pod restart counter 为 0。

    建议的最小确认

    请在同一被测 SHA 上复用这 5 行 fixture,显式保存:

    1. 单独 MATCH 结果;
    2. mode=pre、mode=post、mode=force 结果;
    3. 同一查询改为精确主键候选集合的对照;
    4. entries flush 前后的结果和实际成员集合/reader 证据。

    如果 flush 后稳定得到 PRE=空、POST/FORCE=1,2,4,即可把本次 Nightly 作为 #29042 的直接持续复现;否则再把问题收窄到 FULLTEXT 生成成员集合与 IVF PRE reader 的组合路径。

    本评论仅补充只读 Run/Loki/监控证据,没有修改集群或执行修复。

  18. added
    ai-easy
    on Oct 12, 2026
  19. Ariznawlll commented on Oct 12, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    MatrixOne 固定最新 main@02a2823c2007d5e4fb4ada148e93743df337551a,本地单 CN/TN/LogService。MOTR 用例在 matrixorigin/motr#229,脚本 issue_29042_ivf_persisted_membership.py。

    原问题验证

    原问题的关键不是 ANN 召回率,而是 IVFFLAT 隐藏 entries 持久化或物理复制后,非主键谓词的小整数主键 membership 在 mode=pre 被过滤为空;POST 不足时 AUTO 重试也可能因此漏结果。本次按原证据重新构造 entries flush、COPY ALTER、普通 clone,以及距离较远的 901–950 匹配行造成 POST underfill 的 AUTO 场景。在当前 main 上,PRE/AUTO 与 FORCE 均得到预期结果,未再出现空集。

    Happy Path 覆盖

    • 200 行 INT 主键、非主键 grp<=10,原始 entries flush 前后 FORCE/PRE/AUTO 均返回 1–10;ALTER TABLE ... ADD COLUMN 和 CREATE TABLE ... CLONE 后继续正确。
    • 原 issue 的 membership 100/101 分界各自验证,复制后的 PRE 仍返回预期 Top-10;直接主键 id<=10 作为独立控制。
    • 1000 行稀疏/远距离谓词,原表与 clone 的 FORCE/PRE/AUTO 都返回 901–910;POST 在相同 fixture 返回空,证明 AUTO 成功并非因为 POST 本来就足够。

    Unhappy Path 覆盖

    • grp<0 无匹配行时 PRE 返回空,不把任意索引行误报为匹配。
    • 同一个 case 独立重建三轮;随后 MOTR 对照 golden 又连续运行 3 次,结果全部一致。任一模式、阶段或边界值不符都会让脚本失败。
    • 清理范围仅是本次自动生成的测试数据库。

    白盒验证

    测试显式对照 mode=force(基准)与 mode=pre/auto,并用 mo_ctl('dn','flush', '<测试库>.<entries表>') 强制经过原问题的持久化边界。由于此次是 SQL 可观测结果回归,没有据此断言具体内部过滤器实现或多 CN 缓存协议。

    回归测试

    新增 MOTR case 的脚本、golden 与入口均位于上述 PR,记录后 --repeat 3 对照通过。该 case 锁定持久化、COPY ALTER、普通 clone、100/101 分界和 AUTO correctness retry;本次尚未复跑原报告中的双 CN、snapshot clone、data branch、全部量化类型、多索引及混合全文过滤。如这些属于此 issue 的正式完整验收,还需在相应环境追加验证,不能把单 CN 结果扩大为那些路径已通过。

    测试证据

    • MOTR PR:matrixorigin/motr#229,14_issue_regression.issue_29042_ivf_persisted_membership,3/3 success、0 fail;每次包含三轮独立建库,分别检查原始/flush/ALTER/clone/AUTO 分支。
    • 另有独立 Python SQL 复核脚本 validate_29042.py、validate_29042_flush.py、validate_29042_clone.py、validate_29042_auto.py,各场景连续三轮结果正确。

    当前 main 在本次可验证的核心路径上已恢复正确。我把现有通过证据和未覆盖边界一并转回研发确认;本轮没有关闭 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/bug

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions