Skip to content

[Bug]: Prepared FULLTEXT and vector statements keep stale hidden-index plans after index DDL #25625

Description

@Ariznawlll

Is there an existing issue for this?

Branch / Commit

  • Branch: test/issues-24122-21685-4.0-dev
  • Commit: 7f7de555c49acc3a64995b078025f5526afc9c34

Environment

  • Local launch-with-proxy deployment with two CNs
  • Reproduced through Proxy 6001 and directly across CN 16001 / CN 16002
  • macOS arm64

Summary

Prepared statements that use special indexes retain stale hidden-index state after index DDL instead of being invalidated and rebuilt.

Index / prepared operation DDL Old prepared result Fresh result
FULLTEXT SELECT DROP + CREATE, same session empty set correct rows
FULLTEXT SELECT prepared on CN1, DROP + CREATE on CN2 empty set correct rows, including a row inserted on CN2
FULLTEXT UPDATE prepared on CN1, DROP + CREATE on CN2 success, 0 rows affected fresh prepared UPDATE affects 2 rows
FULLTEXT DELETE same cross-CN setup correct in the tested case correct
IVF_FLAT SELECT DROP + CREATE error referencing deleted old hidden table correct nearest rows
IVF_FLAT SELECT ALTER REINDEX, lists=2 to lists=3 index-out-of-range panic plan must be rebuilt
HNSW SELECT DROP + CREATE, then converged DML reads the pre-DDL HNSW snapshot fresh statement sees converged UPDATE/DELETE/INSERT

Controls:

  • Ordinary secondary-index prepared SELECT/UPDATE/DELETE remain correct across same-session and cross-CN DROP + CREATE.
  • A HNSW statement prepared after the index DDL observes later UPDATE/INSERT after the async index converges. This isolates the HNSW failure to the pre-DDL prepared plan, not normal async CDC delay.

FULLTEXT SELECT Reproduction

DROP DATABASE IF EXISTS prepared_special_index_ddl;
CREATE DATABASE prepared_special_index_ddl;
USE prepared_special_index_ddl;

CREATE TABLE ft(id INT PRIMARY KEY, title VARCHAR(200));
INSERT INTO ft VALUES (1,'matrixone alpha'),(2,'other');
CREATE FULLTEXT INDEX idx_ft ON ft(title);

PREPARE pf FROM
  "SELECT id FROM ft
   WHERE MATCH(title) AGAINST('matrixone' IN NATURAL LANGUAGE MODE)
   ORDER BY id";

EXECUTE pf; -- 1

DROP INDEX idx_ft ON ft;
CREATE FULLTEXT INDEX idx_ft ON ft(title);

EXECUTE pf; -- actual: empty; expected: 1

SELECT id FROM ft
WHERE MATCH(title) AGAINST('matrixone' IN NATURAL LANGUAGE MODE)
ORDER BY id; -- correct: 1

The same failure occurs when pf is prepared and executed on CN1 while CN2 performs DROP/CREATE and inserts a new matching row. The old CN1 statement returns empty; fresh CN1 SQL returns all old and new matches.

FULLTEXT Prepared UPDATE Variant

CREATE TABLE ftdml(
  id INT PRIMARY KEY,
  title VARCHAR(200),
  mark INT DEFAULT 0
);
INSERT INTO ftdml VALUES
  (1,'update_token alpha',0),
  (2,'other',0),
  (4,'update_token beta',0);
CREATE FULLTEXT INDEX idx_ftdml ON ftdml(title);

PREPARE pupd FROM
  "UPDATE ftdml SET mark=mark+1
   WHERE MATCH(title) AGAINST('update_token' IN NATURAL LANGUAGE MODE)";

EXECUTE pupd; -- 2 rows; reset mark to 0 after this control execution
UPDATE ftdml SET mark=0;

-- Run on another CN:
DROP INDEX idx_ftdml ON ftdml;
CREATE FULLTEXT INDEX idx_ftdml ON ftdml(title);

-- Back on the original CN:
EXECUTE pupd; -- actual: success, 0 rows affected

SELECT id FROM ftdml
WHERE MATCH(title) AGAINST('update_token' IN NATURAL LANGUAGE MODE);
-- fresh query correctly returns 1,4

PREPARE pupd2 FROM
  "UPDATE ftdml SET mark=mark+1
   WHERE MATCH(title) AGAINST('update_token' IN NATURAL LANGUAGE MODE)";
EXECUTE pupd2; -- correct: 2 rows affected

IVF_FLAT DROP / CREATE Reproduction

CREATE TABLE vec(id INT PRIMARY KEY, emb vecf32(3));
INSERT INTO vec VALUES
  (1,'[0,0,0]'),(2,'[1,0,0]'),(3,'[2,0,0]'),
  (4,'[3,0,0]'),(5,'[4,0,0]'),(6,'[5,0,0]');

CREATE INDEX idx_vec USING ivfflat ON vec(emb)
  lists=2 op_type 'vector_l2_ops';

PREPARE pv FROM
  "SELECT id FROM vec
   ORDER BY l2_distance(emb,'[0,0,0]')
   LIMIT 2 by rank with option 'mode=pre'";

EXECUTE pv; -- 1,2

DROP INDEX idx_vec ON vec;
CREATE INDEX idx_vec USING ivfflat ON vec(emb)
  lists=2 op_type 'vector_l2_ops';

EXECUTE pv;
-- actual: table "__mo_index_secondary_<old UUID>" does not exist

SELECT id FROM vec
ORDER BY l2_distance(emb,'[0,0,0]')
LIMIT 2 by rank with option 'mode=pre'; -- correct: 1,2

IVF_FLAT ALTER REINDEX Panic

-- Starting from vec + idx_vec above
PREPARE pvr FROM
  "SELECT id FROM vec
   ORDER BY l2_distance(emb,'[0,0,0]')
   LIMIT 2 by rank with option 'mode=pre'";

EXECUTE pvr; -- 1,2
ALTER TABLE vec ALTER REINDEX idx_vec IVFFLAT lists=3 force_sync;
EXECUTE pvr;

Observed stack:

internal error: panic runtime error: index out of range [2] with length 2
github.com/matrixorigin/matrixone/pkg/vectorindex/ivfflat.(*IvfflatSearchIndex[...]).LoadCentroids
    pkg/vectorindex/ivfflat/search.go:228
github.com/matrixorigin/matrixone/pkg/vectorindex/ivfflat.(*IvfflatSearchIndex[...]).Load

HNSW Isolation

  1. Prepare and execute an HNSW nearest-neighbor SELECT: 1,2.
  2. DROP and recreate the HNSW index with the same name.
  3. UPDATE id=1 from [0,0,0] to [9,0,0], DELETE id=2, and INSERT id=7 as [0.1,0,0].
  4. Wait 8 seconds for async HNSW CDC convergence.

The pre-DDL prepared statement continues to return stale pre-DDL keys (1, or 1,2 at earlier stages). Fresh SQL and a newly prepared statement return the converged nearest keys (7,3).

As a control, a statement prepared after the DDL changed correctly from 7,3 to 7,8 after a later pure UPDATE+INSERT and the same 8-second convergence wait.

White-box Analysis

initExecuteStmtParam in pkg/frontend/computation_wrapper.go is designed to rebuild a prepared plan when any object in preparePlan.Schemas has a newer catalog version.

However, GetParamRule.MatchNode in pkg/sql/plan/visit_plan_rule.go only records schemas for TABLE_SCAN, INSERT, and MULTI_UPDATE. FULLTEXT and vector-index access is rewritten through function/index-specific plan nodes. Their hidden/meta/index tables and DDL-dependent configuration are not reliably represented in the prepared statement's schema-version set.

Consequences observed here match that gap:

  • FULLTEXT keeps a stale hidden-table binding and returns no matches.
  • IVF_FLAT keeps an old hidden table UUID or combines old lists=2 state with the new lists=3 layout.
  • HNSW keeps searching the cached pre-DDL hidden index even after the recreated index has converged.

Expected Behavior

After CREATE/DROP/ALTER REINDEX changes an index used by a prepared statement, execution must either:

  1. detect the catalog/index version change and rebuild/recompile the statement; or
  2. return a normal stale-plan error that allows the client to reprepare.

It must not silently return an empty set, silently skip UPDATE rows, access a deleted hidden table, retain a pre-DDL HNSW snapshot, or panic because old IVF metadata is combined with a new layout.

Acceptance Criteria

  1. Track all FULLTEXT, IVF_FLAT, and HNSW hidden/meta/index tables referenced by prepared plans.
  2. Invalidate and safely rebuild SELECT and DML plans after DROP INDEX, CREATE INDEX, and ALTER REINDEX.
  3. Cover same-name and different-name index recreation.
  4. Cover changed IVF lists values without stale slice sizing or centroid state.
  5. Verify old prepared, fresh prepared, and non-prepared results agree after DDL.
  6. Cover Proxy, same-CN, and cross-CN DDL/execution.
  7. Add SELECT, UPDATE, and DELETE regressions, including ordinary secondary-index controls.

Activity

  1. added this to the MO-202607 milestone on Jul 11, 2026
  2. added theissue type on Jul 11, 2026
  3. changed the title [-][Bug]: Prepared FULLTEXT and vector queries keep stale hidden-index plans after index DDL[/-] [+][Bug]: Prepared FULLTEXT and vector statements keep stale hidden-index plans after index DDL[/+] on Jul 11, 2026
  4. added
    kind/bug
    severity/s0
    and removed on Jul 12, 2026
  5. added a commit that references this issue on Jul 13, 2026
    58a1522
  6. added
    ai-easy
    on Jul 14, 2026
  7. assigned and unassigned on Jul 14, 2026
  8. Ariznawlll commented on Jul 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    已在最新 main 服务上执行实际 SQL 验证。

    验证版本

    • Commit:0977fc07c51067ce76b769c7853d7374cb2fccec
    • 服务返回的 git_version():0977fc07c
    • 验证时间:2026-07-14

    验证过程

    1. FULLTEXT:prepare 查询后执行 DROP INDEX + CREATE INDEX,再次执行旧 prepared statement,并与 fresh SQL 对比。
    2. IVF_FLAT:prepare 向量查询后执行 DROP/CREATE,再执行旧 statement。
    3. IVF_FLAT REINDEX:将 lists=2 改为 lists=3 force_sync 后再次执行旧 statement。

    实际结果

    • FULLTEXT:重建前、旧 statement 重建后、fresh SQL 均返回 id=1。
    • IVF_FLAT:重建前、DROP/CREATE 后、REINDEX 后均返回 id=1,2。
    • 未出现旧 hidden table 不存在错误,也未出现 index out of range panic。

    结论与边界

    FULLTEXT 和 IVF_FLAT 的主要复现路径已在当前 main 上通过。计划层的 hidden-index schema 收集回归也已 PASS。此次没有等待并验证 HNSW 异步 CDC 收敛后的 UPDATE/DELETE/INSERT 完整变体,因此 HNSW 长链路仍属于未单独重跑的边界。

    #25625 verification

  9. Ariznawlll commented on Jul 14, 2026

    @Ariznawlll
    CollaboratorAuthor

    已将本 issue 的 special-index prepared plan 黑盒回归加入 matrixorigin/motr#48。

    MOTR 用例

    黑盒覆盖

    • FULLTEXT:PREPARE → DROP/CREATE 同名索引 → 旧 statement 与 fresh SQL 都必须返回 id=1,不能静默返回空集;
    • IVF_FLAT:PREPARE → DROP/CREATE 同名索引 → 旧 statement 与 fresh SQL 都必须返回最近的 1,2,不能引用已删除的旧 hidden table;
    • IVF_FLAT REINDEX:lists=2 改成 lists=3 force_sync 后再次执行旧 statement,必须继续返回 1,2,不能触发 index-out-of-range panic。

    HNSW 的异步收敛部分没有用固定 sleep 冒充稳定回归,后续需要单独的异步场景测试。

    运行结果

    在 MO main@0977fc07c51067ce76b769c7853d7374cb2fccec 上连续 3 轮均通过;两 CN + Proxy 下整套并发回归也通过。

    MOTR PR #48 black-box regression output

  10. 1 remaining item

  11. Ariznawlll commented on Jul 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    Reopening after reproducing the stale IVF hidden-table plan on the latest main.

    Verified version

    • Branch: main
    • Commit: d17b5a1f8e83cee4999181b9509af6126517985c
    • Date: 2026-07-28

    Current behavior

    The existing same-session regression now passes:

    1. Session A prepares and executes the IVF query.
    2. Session A drops and recreates the IVF index.
    3. Session A executes the old prepared statement again.

    This passed five consecutive runs, including ALTER REINDEX. The behavior is covered by the session-level DDL generation added in 00d7bf28f / #26021: DDL executed by the same session invalidates its prepared plans.

    The cross-session path is still broken:

    -- Session A
    create table vec(id int primary key, emb vecf32(3));
    insert into vec values
      (1,'[0,0,0]'),(2,'[1,0,0]'),(3,'[2,0,0]'),
      (4,'[3,0,0]'),(5,'[4,0,0]'),(6,'[5,0,0]');
    create index idx_vec using ivfflat on vec(emb)
      lists=2 op_type 'vector_l2_ops';
    prepare vector_ps from
      "select id from vec order by l2_distance(emb,'[0,0,0]')
       limit 2 by rank with option 'mode=pre'";
    execute vector_ps; -- 1,2
    -- Session B
    drop index idx_vec on vec;
    create index idx_vec using ivfflat on vec(emb)
      lists=2 op_type 'vector_l2_ops';
    -- Session A
    execute vector_ps;

    Actual result, reproduced twice with fresh databases:

    ERROR 1064 (HY000): SQL parser error:
    table "__mo_index_secondary_019fa899-9898-7480-97d3-e2686e48a780" does not exist
    

    The UUID in the error belongs to the deleted pre-DDL IVF entries table; the recreated index has a different hidden-table UUID.

    Root-cause evidence

    EXPLAIN for the prepared query contains only:

    Project
      -> Sort
           -> Table Function on ivf_search
    

    The IVF index-only rewrite removes the base TABLE_SCAN. Prepared schema collection still does not register the base/hidden-index dependencies from this FUNCTION_SCAN. A same-session DDL changes ddlVersion and masks the missing dependency, while DDL from Session B does not change Session A's generation. Session A therefore reuses the old plan and accesses the deleted hidden table.

    Required regression

    The permanent regression must use two live sessions:

    1. Prepare in Session A.
    2. Drop/recreate the IVF index in Session B.
    3. Execute the existing statement in Session A.

    The current MOTR case is same-session only and will be temporarily removed until this cross-session path is fixed and can be covered correctly.

  12. Ariznawlll commented on Jul 28, 2026

    @Ariznawlll
    CollaboratorAuthor

    The misleading same-session MOTR regression is being temporarily removed in matrixorigin/motr#68.

    After the cross-session prepared-plan dependency path is fixed, the case should be restored with two live sessions: prepare in Session A, rebuild the IVF index in Session B, then execute the existing statement in Session A.

  13. added a commit that references this issue on Jul 30, 2026
    0b466b7
  14. assigned and unassigned on Jul 31, 2026
  15. Ariznawlll commented on Aug 1, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:15a2d07c687a62a0984cf62846b092912a28c0f1
    • 部署方式:本地双 CN + Proxy;两个独立会话分别执行 PREPARE 和索引 DDL/DML。
    • 测试日期:2026-08-01

    原问题验证

    会话 A 对 FULLTEXT、IVF 和 HNSW 查询分别 PREPARE;会话 B DROP INDEX、修改向量/文本、以同名索引重新 CREATE。连续 3 轮中,会话 A 复用旧 prepared statement 后均读取到重建索引后的新结果,未返回旧 hidden-index 数据。

    Happy Path 覆盖

    1. FULLTEXT:alpha 初始命中 id=1,重建后更新为 gamma 时旧词为 0 行,再改为 alpha recreated 后重新命中 id=1。
    2. IVF:重建后最近邻从 id=1 切换为 id=2。
    3. HNSW:使用 bigint 主键建立索引,重建后最近邻同样切换为 id=2。

    Unhappy Path 覆盖

    1. 跨会话 DDL 是本 issue 的失效边界:prepared statement 不应保留已 DROP 的索引计划;三种索引均未读取陈旧结果。
    2. 向量/HNSW 初始化约束单独验证:非 bigint 主键会被正常拒绝,未把夹具配置错误误判为产品回归。

    白盒验证

    • 不适用:计划缓存失效可通过两个独立 SQL 会话的实际查询结果验证。

    回归测试

    • 已加入 motr 黑盒回归:PR #74,测试文件:issue_25625_prepared_special_index_ddl.test。
    • 回归稳定覆盖两会话下 FULLTEXT 和 IVF 的 prepared query、DROP + CREATE 同名索引、旧语句复用和新鲜 SQL 对照;HNSW 为异步 CDC 路径,仍保留本次专项审计验证,不将时序敏感断言纳入稳定 motr 基线。

    测试证据

    • 执行:FULLTEXT、IVF、HNSW 跨会话 prepared/DDL 黑盒审计连续 3 轮。
    • 每轮均记录:PASS 25625_cross_session_prepared_fulltext_ivf_hnsw_after_drop_recreate_index。
    • 对应回归提交:8f0f759。

    测试结论

    测试通过。

  16. added 6 commits that reference this issue on Sep 17, 2026
    2740f5f
    5eea4c0
    015c90d
    108a56d
    d9f396e
    35c226b
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