Skip to content

[Bug] Synchronous FULLTEXT/IVF indexes become stale after UPDATE changes primary key #25617

Closed

Description

@Ariznawlll

Summary

When an ordinary UPDATE changes a primary-key value, the base table is updated but synchronous irregular indexes keep the old primary-key lookup value. The row then becomes invisible to FULLTEXT/IVF_FLAT index queries, and deleting/reinserting the old key can expose stale index entries.

This is reproducible on the current tree (7821d21c723372e45a8f65da3bbb896a157b1036) with the default synchronous IVF_FLAT path and FULLTEXT. HNSW and explicit ASYNC IVF use CDC and eventually reconcile, so this report focuses on synchronous FULLTEXT/IVF maintenance.

FULLTEXT reproduction

SET experimental_fulltext_index = 1;
CREATE DATABASE repro_pk_irregular;
USE repro_pk_irregular;

CREATE TABLE ft (
  id BIGINT PRIMARY KEY,
  body TEXT,
  FULLTEXT fti(body)
);
INSERT INTO ft VALUES (1, 'alpha one'), (2, 'beta two');

SELECT id FROM ft WHERE MATCH(body) AGAINST('alpha');
-- expected: 1

UPDATE ft SET id = 5 WHERE id = 1;

SELECT * FROM ft ORDER BY id;
-- returns the base row (5, 'alpha one')

SELECT id FROM ft WHERE MATCH(body) AGAINST('alpha');
-- actual: empty; expected: 5

The hidden FULLTEXT table still contains doc_id = 1, so the index lookup cannot join the row after the base primary key becomes 5. The stale entry survives CN changes and service restart. If id=5 is deleted and id=1 is inserted again with different text, the old alpha tokens can be returned for the new row.

Synchronous IVF_FLAT reproduction

SET experimental_ivf_index = 1;

CREATE TABLE iv (
  id BIGINT PRIMARY KEY,
  v VECF32(3)
);
INSERT INTO iv VALUES (1, '[1,1,1]'), (2, '[9,9,9]');

CREATE INDEX ix USING IVFFLAT
  ON iv(v) LISTS = 2 OP_TYPE 'vector_l2_ops';

UPDATE iv SET id = 5 WHERE id = 1;

SELECT * FROM iv ORDER BY id;
-- base table contains id=5

SELECT id
FROM iv
ORDER BY l2_distance(v, '[1,1,1]')
LIMIT 2;
-- actual: id=2 only; expected to include id=5

The hidden IVF entries retain the old origin_pk = 1. After deleting id=5 and inserting id=1 with a different vector, stale and new entries can both map to id=1, producing stale/duplicate KNN results.

Code path

  • Ordinary UPDATE accepts primary-key changes: pkg/sql/plan/bind_update.go.
  • FULLTEXT delete maintenance joins hidden doc_id with the source primary key: pkg/sql/plan/build_dml_util.go.
  • IVF delete/reinsert maintenance similarly uses the source/origin primary key.

The primary-key documentation says primary-key values are immutable. If that contract is intended, ordinary UPDATE should reject this statement consistently. If primary-key UPDATE remains accepted, synchronous FULLTEXT/IVF maintenance must carry and remove the old key before inserting the new key.

Validation

  • Reproduced through proxy and both CN ports.
  • Reproduced after service restart.
  • Regular/unique secondary indexes do not show this stale-key behavior.
  • HNSW and explicit ASYNC IVF eventually reconcile through CDC.

Environment: macOS arm64, MatrixOne standalone multi-CN launch-with-proxy.

Activity

  1. added this to the MO-202607 milestone on Jul 11, 2026
  2. added
    kind/bug
    on Jul 11, 2026
  3. added theissue type on Jul 11, 2026
  4. Ariznawlll commented on Jul 11, 2026

    @Ariznawlll
    CollaboratorAuthor

    Additional validation on the same build:

    • Multi-row primary-key update reproduces the same result:
      UPDATE ftfail SET id = id + 1;
      The base rows become ids 2 and 3, but FULLTEXT MATCH queries return no rows. The equivalent synchronous IVF_FLAT update leaves the base rows at ids 2 and 3 while KNN returns only one old-key match.
    • Composite primary keys reproduce it as well: updating one component makes the base row visible under the new composite key, but FULLTEXT/IVF queries omit it.
    • Temporary tables are affected too. A temporary FULLTEXT table updated from id=1 to id=5 loses the row from MATCH results; a temporary synchronous IVF_FLAT table similarly omits id=5 from KNN.
    • ROLLBACK after the primary-key UPDATE leaves the index consistent; COMMIT produces the stale hidden-key state.

    This broadens the scope to ordinary and temporary tables, single-row/multi-row updates, and composite primary keys.

  5. Ariznawlll commented on Jul 11, 2026

    @Ariznawlll
    CollaboratorAuthor

    Prepared-statement coverage on the same build:

    • PREPARE/EXECUTE UPDATE and DELETE correctly maintain regular UNIQUE/secondary indexes.
    • PREPARE/EXECUTE FULLTEXT body UPDATE and DELETE correctly update/remove MATCH() results.
    • PREPARE/EXECUTE IVF vector UPDATE and DELETE correctly update/remove KNN results.
    • PREPARE/EXECUTE ODKU on a FULLTEXT table correctly updates the indexed document.
    • A prepared primary-key UPDATE reproduces the same stale-key behavior already described: the base row moves to the new id, but synchronous FULLTEXT/IVF queries omit it.

    Therefore prepared execution does not introduce a separate corruption path; it reaches the same primary-key maintenance gap.

  6. removed
    kind/bug
    on Jul 11, 2026
  7. added
    kind/bug
    severity/s0
    and removed on Jul 12, 2026
  8. Ariznawlll commented on Jul 15, 2026

    @Ariznawlll
    CollaboratorAuthor

    最新 main 可复核验证(MOTR)

    实际执行命令

    # MatrixOne:全新隔离 worktree,构建并启动最新 main
    git fetch origin main
    git worktree add --detach /tmp/matrixone-motr-pr48-main-20260715 origin/main
    make thirdparties
    CGO_CFLAGS='-I$(pwd)/thirdparties/install/include' make build
    ./mo-service -launch ./etc/launch/launch.toml
    
    # MOTR:先录制并人工检查 result,再做非 record 稳定性回归
    ./motr --record --do-test '14_issue_regression.issue_(25151|25347|25384|25502|25546|25617|25626)' -v
    ./motr --repeat 5 --do-test '14_issue_regression.issue_(25151|25347|25384|25502|25546|25617|25626)' -v
    • 源码 HEAD:f1c0a92d882dbdad469ffd2c96f607796207c797
    • 服务端核对:git_version()=f1c0a92d8
    • 总结果:35/35 success,380/380 条语句符合预期,0 failed。

    本 issue 实际用例

    1. 创建同步 FULLTEXT 表和同步 IVF_FLAT 表,各插入 id=1/2。
    2. 对两张表分别尝试 UPDATE ... SET id=5 WHERE id=1。
    3. 两个 UPDATE 都必须得到预期错误 20313: unsupported DML: update primary key on a table with a synchronous full-text/vector index。
    4. 错误后检查基础表 id 仍为 1/2。
    5. FULLTEXT 查询仍返回 id=1;IVF 距离查询仍返回 id=1/2,证明失败路径没有造成基础表和隐藏索引分叉。
    6. 该 case 连续执行 5 次,5/5 成功。
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