Skip to content

[Refactoring]: Eliminate legacy INSERT ... ON DUPLICATE KEY UPDATE path, unify to operator-based execution #24939

Closed

Description

@ck89119

Why do you want to refactor this code?

INSERT ... ON DUPLICATE KEY UPDATE (ODKU) currently has two execution paths, mirroring the situation REPLACE INTO had before #23946 / #24044:

  • Modern path (default): bindInsert → appendDedupAndMultiUpdateNodesForBindInsert, built on DEDUP JOIN + MULTI_UPDATE.
  • Legacy path (fallback): build.go catches ErrUnsupportedDML and falls back to buildInsert → buildInsertOnDuplicateUpdatePlans, producing a Node_ON_DUPLICATE_KEY node executed by the colexec onduplicatekey operator.

Maintaining two paths duplicates semantics, complicates correctness/perf work, and keeps the legacy onduplicatekey operator alive. We should eliminate the legacy ODKU path and unify everything onto the operator-based path, the same way REPLACE was unified.

The legacy path is still reached today for three scenarios the modern path rejects with ErrUnsupportedDML:

Scenario Trigger Modern Legacy
No explicit PK + has unique key (fake PK + UK) bind_insert.go update on duplicate without primary key ✗ ✓
Vector / irregular index table bind_insert.go insert into vector/text index table / have vector index table ✗ ✓
Foreign-key table (or referenced) dml_context.go foreign key constraint in ResolveTables ✗ ✓

Two other fallback cases — updating a PK column and updating a UK column — are rejected by both paths, so removing the legacy path causes no functional loss for them (only error-message unification).

Describe the solution you'd like

Unify all ODKU scenarios onto the modern DEDUP JOIN + MULTI_UPDATE path, then delete the legacy Node_ON_DUPLICATE_KEY path. Reuse the techniques already proven in #24044:

  1. Fake PK + UK: introduce an isFakePK branch in appendDedupAndMultiUpdateNodesForBindInsert. With a unique key, dedup/conflict-detect on the first unique key instead of the PK; with no unique key, degrade to a plain INSERT.
  2. Vector / irregular index tables: drop the hard rejections and reuse getValidIndexes to skip irregular indexes (maintained asynchronously by cron), as REPLACE does.
  3. FK tables: lift the ResolveTables rejection. Unlike REPLACE (which deletes the parent row and needs RESTRICT pre-checks), ODKU does not delete rows — its FK semantics are closer to INSERT + partial UPDATE, so the FK validation must be designed specifically (only when FK/referenced columns are actually updated). This is the largest unknown and may be split into a sub-task.

Then remove the legacy code:

  • Drop the ODKU fallback branch in build.go.
  • Remove the Node_ON_DUPLICATE_KEY construction branch and buildInsertOnDuplicateUpdatePlans in build_insert.go.
  • Remove the colexec onduplicatekey operator and its compile/remote-run/explain references.
  • Mark Node_ON_DUPLICATE_KEY / OnDuplicateKeyCtx as reserved in the proto.
  • Keep ondup_update_binder.go (still used by the modern path).

Add BVT/UT coverage for fake PK + UK, vector index tables, FK tables, NULL on unique key, multi-column UK, and composite PK under ODKU.

Describe alternatives you've considered

  • Keep both paths: rejected — it is the status quo and the source of the maintenance burden.
  • Delete the legacy path without filling the gaps: rejected — it would regress fake-PK-with-UK, vector-index, and FK tables from working to erroring.

Additional information

Activity

  1. self-assigned this
    on Jun 11, 2026
  2. added theissue type on Jun 11, 2026
  3. removed theissue type on Jun 11, 2026
  4. ck89119 commented on Jun 18, 2026

    @ck89119
    ContributorAuthor

    Scope: this issue/PR also absorbs #25000 — REPLACE does not maintain vector/fulltext indexes

    Folding the scope of #25000 into this work, since the same modern-DML index-maintenance rework is the right place to fix it.

    Problem (from #25000): REPLACE into a table carrying an irregular index (ivfflat vector / fulltext) succeeds but does not maintain the index synchronously, so a row inserted — or whose vector/text is changed — via REPLACE is not reflected in a subsequent index-backed ANN/KNN or fulltext search. Root cause is #24044, which removed the legacy REPLACE fallback and made bind_replace strip the irregular index (validIndexes, _ := getValidIndexes(tableDef)) instead of maintaining it.

    Repro (from #25000):

    create table t(id int primary key, b vecf32(3));
    insert into t values(1,'[1,1,1]'),(2,'[2,2,2]'),(3,'[3,3,3]'),(4,'[4,4,4]'),(5,'[5,5,5]');
    create index idx using ivfflat on t(b) lists=2 op_type 'vector_l2_ops';
    replace into t values(7,'[5,5,6]');
    select id from t order by l2_distance(b,'[5,5,5]') limit 3;   -- id=7 missing (index not maintained)

    Fix (this PR #24973): REPLACE now maintains IVF/fulltext indexes on the modern path — it deletes the conflicting old row's entries and inserts the new row's entries, keyed by the matched old row's PK (which can differ from the new PK when the conflict is on a non-PK unique key). Added BVT replace_statement/replace_irregular_index covering PK conflict, unique-key conflict (old PK != new PK) and no-conflict insert, for both fulltext and IVF.

    The standalone fix PR #25016 is closed in favor of this one.

  5. added
    severity/s2
    on Jun 30, 2026
  6. added this to the MO-202607 milestone on Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/refactorseverity/s2

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions