Skip to content

fix: maintain vector/fulltext index synchronously on REPLACE (#25000) - #25016

Closed
ck89119 wants to merge 3 commits into
matrixorigin:mainfrom
ck89119:issue-25000-main
Closed

ck89119 wants to merge 3 commits into
matrixorigin:mainfrom
ck89119:issue-25000-main

Conversation

@ck89119

@ck89119 ck89119 commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • BUG

Which issue(s) this PR fixes:

issue #25000

What this PR does / why we need it:

REPLACE into a table carrying an irregular index (ivfflat vector index or fulltext index) succeeded but did not maintain the index. As a result, a row inserted (or whose indexed value changed) via REPLACE was not reflected in a subsequent index-backed ANN/KNN or MATCH ... AGAINST query until the next cron reindex. INSERT was not affected.

Root cause: the modern operator-based REPLACE path (DEDUP JOIN + MULTI_UPDATE, introduced by #24044) stripped irregular indexes via getValidIndexes and relied on async maintenance. This PR restores synchronous maintenance for non-async ivfflat and fulltext indexes, leaving async ones to the cron exactly as the legacy insert/update paths do.

How

  • Collect the non-async ivf / fulltext index defs before they are stripped from tableDef.Indexes.
  • Sink the processed new rows once (a tag-preserving SINK_SCAN) so auto-increment / generated columns are evaluated a single time and the same rows feed both the main conflict-resolution branch and the index-maintenance sub-plans.
  • Build the index sub-plans after createQuery (the same sequencing the legacy insert path uses): they use positional column refs and hand-built nodes, so they must not go through the per-step optimize/remap pipeline. Only the newly appended steps are finalized via handleHashMapMessages.
  • ivf: insert entries for every incoming row, and delete the stale entries of conflicting rows. On a primary-key conflict the new PK equals the replaced row's PK, so the old entry keyed by org_pk is removed; a brand-new PK matches nothing and deletes nothing.
  • fulltext: a single POSTDML node per index deletes the stale inverted entries (by doc id = the incoming PK) and re-tokenizes the new rows.

Regular tables and regular secondary indexes are unaffected.

Known limitation: for the rare case of a table with both an irregular index and a unique secondary index, a unique-key-only conflict (where the replaced row's PK differs from the incoming PK) is not yet covered for the irregular index; tracked as a follow-up.

Tests

  • New BVT test/distributed/cases/vector/vector_ivf_replace.sql and test/distributed/cases/fulltext/fulltext_replace.sql covering new-row REPLACE, existing-row REPLACE (stale value cleared) and multi-row REPLACE.
  • New unit tests TestCollectSyncIvfMultiIndexes and TestCollectSyncFulltextIndexes.
  • go test ./pkg/sql/plan passes; existing vector_ivf_membership / fulltext / fulltext_membership BVTs pass with no regression.

🤖 Generated with Claude Code

…rigin#25000)

REPLACE into a table carrying an irregular index (ivfflat vector index or
fulltext index) succeeded but did not maintain the index, so a row inserted
or whose indexed value changed via REPLACE was not reflected in a subsequent
index-backed ANN/KNN or MATCH...AGAINST query until the next cron reindex.

The modern operator-based REPLACE path stripped irregular indexes via
getValidIndexes and relied on async maintenance. This restores synchronous
maintenance for non-async ivfflat and fulltext indexes:

- Collect the non-async ivf / fulltext index defs before they are stripped.
- Sink the processed new rows once (tag-preserving SINK_SCAN) so auto-increment
  / generated columns are evaluated a single time and shared by the main
  conflict-resolution branch and the index-maintenance sub-plans.
- Build the index sub-plans AFTER createQuery (like the legacy insert path):
  they use positional column refs and must not go through the per-step
  optimize/remap pipeline; finalize only the new steps via handleHashMapMessages.
- ivf: insert entries for every incoming row and delete the stale entries of
  conflicting rows (a PK conflict's new PK equals the replaced row's PK, so the
  old entry keyed by org_pk is removed; a brand-new PK matches nothing).
- fulltext: a single POSTDML node per index deletes the stale inverted entries
  (by doc id = incoming PK) and re-tokenizes the new rows.

Regular tables and regular secondary indexes are unaffected.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ck89119
ck89119 requested a review from a user June 16, 2026 16:22
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

The result files in the previous commit were regenerated against a stale
mo-service binary and captured the pre-fix (buggy) behavior. Regenerate them
against the fixed build so they reflect synchronous index maintenance:
ivf KNN now returns the newly REPLACE'd row and drops stale vectors, and
fulltext MATCH now sees the new tokens and drops the replaced row's stale ones.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@ck89119

ck89119 commented Jun 18, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #24973 (issue #24939), which fixes #25000 as part of the modern-DML index-maintenance rework: REPLACE now deletes the conflicting old row's IVF/fulltext entries and inserts the new row's entries on the modern path. Closing in favor of that PR.

@ck89119 ck89119 closed this Jun 18, 2026
@ck89119
ck89119 deleted the issue-25000-main branch June 18, 2026 08:47

This branch was previously deployed

1 inactive deployment
ci — 22ffa573 Deployed Jun 16, 2026 by ck89119 via Matrixone Utils CI / pr-size-label #38816
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants