Skip to content

fix(insert): exclude fake primary key from LAST_INSERT_ID (#29903) - #30004

Merged
XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
cpegeric:bug_29903
Oct 14, 2026
Merged

XuPeng-SH merged 5 commits into
matrixorigin:mainfrom
cpegeric:bug_29903

Conversation

@cpegeric

@cpegeric cpegeric commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #29903

What this PR does / why we need it:

A FULLTEXT index carries a hidden auto_increment fake primary key (__mo_fake_pk_col) on its index table. When an INSERT into the base table synchronously populates the index, the index table's PRE_INSERT generates that fake key and the preinsert operator published it into the session LAST_INSERT_ID, so LAST_INSERT_ID() returned a hidden index value instead of the base table's business key.

create table id_probe(id bigint unsigned auto_increment primary key, label varchar(40));
alter table id_probe add fulltext label_ft(label);
insert into id_probe(label) values('small');
select id, last_insert_id() from id_probe;   -- id=1 but LAST_INSERT_ID leaked the hidden index key

Fix. Only a user-visible auto_increment column may set LAST_INSERT_ID. The publish is gated on tableHasUserAutoIncrColumn(...): a primary-key-less table — the hidden fulltext/vector index tables among them — carries a generated fake primary key that is auto_increment but internal, and its value must never surface. This enforces the contract already documented in dml/insert/multi_insert ("the hidden fake key must not count as an auto_increment target"). The prior behavior was only masked for equal-magnitude cases by the smallest-non-zero coordinator and still returned the wrong value when the base key outranked the index fake key (e.g. a table created with AUTO_INCREMENT=1000).

Tests.

  • Unit: retargeted the two existing auto-increment coordination tests to a real user auto column; added TestGenAutoIncrColFakePKDoesNotPublishLastInsertID (regression) and TestTableHasUserAutoIncrColumn (table-driven). 100% of changed blocks covered.
  • BVT: test/distributed/cases/fulltext/fulltext_last_insert_id — single insert, sequential insert, AUTO_INCREMENT=1000 latent case, multi-row first-key. Validated -g -n, 21/21.

🤖 Generated with Claude Code

@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 →

@XuPeng-SH

XuPeng-SH commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 7e5f08ebd4a5e0cbcdc4d4adac06af351ad2640a

Assessment: request_changes

Reviewed the complete four-file delta and its allocation, planning, process and frontend consumers. The gate introduces a visibility regression and leaves a reachable hidden-ID leak unresolved.

Model: gpt-6-astra / xhigh

Session: 01a12d8f-7dea-77c2-a565-38d45b5564e4 · slot 0 · generation 0

  • P2 pkg/sql/colexec/preinsert/preinsert.go:700: Create t(pk int primary key, __mo_fake_pk_col bigint unsigned auto_increment) and execute insert into t(pk) values (10).
    Impact: The visible auto-increment column generates an ID, but the new name-only predicate suppresses it. LAST_INSERT_ID() retains its previous value and the statement reports no generated ID. This is a new compatibility regression.
    Evidence: build_util.go:2614 permits this column name, and build_ddl.go preserves Hidden=false for user columns. incrservice.GetUserAutoColumnFromDef uses visibility, while store_sql.go:530 explicitly accounts for visible columns with this spelling. The source-extracted probe returned eligibility=false for this valid visible column. Use the existing visibility ownership rule rather than treating the name alone as proof of an internal column.

  • P2 pkg/sql/colexec/preinsert/preinsert.go:673: For t(id bigint auto_increment, v int) without an explicit primary key, execute insert into t(v) values (1), then insert into t(id,v) values (100,2).
    Impact: The second insert generates only a hidden fake key, yet the gate publishes that key and changes LAST_INSERT_ID() from 1 to 2. This preexisting leak remains unresolved despite the stated contract that hidden IDs must never surface.
    Evidence: build_ddl.go:4137 appends the hidden auto-increment key. column_cache.go:697 returns zero when all user values are explicit; table_cache.go:160-165 then selects the hidden column's nonzero result. Because the table also contains a user auto-increment column, the new gate accepts that result. The source-extracted composition probe confirmed this path with allocator returns 0 and 2. Filter the returned ID by its generating column's ownership; table-level existence is insufficient. The new mixed-column helper test does not exercise this behavior.

Validation:

  • Verified the assigned HEAD and clean source checkout. Frozen patch matches the Git delta apart from a terminal newline; diff whitespace and gofmt checks passed.

  • Reviewed every changed hunk, supplied comments, allocator result selection, DDL visibility, local/remote PRE_INSERT metadata, statement coordination, frontend publication, retry/error paths and test oracles.

  • Reused frozen successful exact-head build, Ubuntu/x86 UT, Linux/arm64 SCA, coverage and PROXY/pessimistic multi-CN BVT statuses. Skipped checks were not counted as passes.

  • Ran ownership_probe.go with Go 1.25.5 and CGO_ENABLED=0. Extracted production decision functions reproduced both defects; hidden-only and generated-user controls behaved correctly. Diagnostics are saved in the assigned attempt directory.

Limitations:

  • The probe uses minimal types and stubbed column-allocator returns grounded in the source trace; it is not a MatrixOne SQL or native package test. Source-derived SQL reproductions were saved but not executed.

  • Frozen evidence lacks per-test CI logs and the standalone linked-issue body. The PR's stated contract was reviewed; no performance benchmark was run.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 7e5f08ebd4a5e0cbcdc4d4adac06af351ad2640a

Assessment: request_changes

Reviewed the complete four-file delta and its allocation, planning, process and frontend consumers. The gate introduces a visibility regression and leaves a reachable hidden-ID leak unresolved.

Model: gpt-6-astra / xhigh

Session: 01a12d8f-7dea-77c2-a565-38d45b5564e4 · slot 0 · generation 0

  • P2 pkg/sql/colexec/preinsert/preinsert.go:700: Create t(pk int primary key, __mo_fake_pk_col bigint unsigned auto_increment) and execute insert into t(pk) values (10).
    Impact: The visible auto-increment column generates an ID, but the new name-only predicate suppresses it. LAST_INSERT_ID() retains its previous value and the statement reports no generated ID. This is a new compatibility regression.
    Evidence: build_util.go:2614 permits this column name, and build_ddl.go preserves Hidden=false for user columns. incrservice.GetUserAutoColumnFromDef uses visibility, while store_sql.go:530 explicitly accounts for visible columns with this spelling. The source-extracted probe returned eligibility=false for this valid visible column. Use the existing visibility ownership rule rather than treating the name alone as proof of an internal column.

  • P2 pkg/sql/colexec/preinsert/preinsert.go:673: For t(id bigint auto_increment, v int) without an explicit primary key, execute insert into t(v) values (1), then insert into t(id,v) values (100,2).
    Impact: The second insert generates only a hidden fake key, yet the gate publishes that key and changes LAST_INSERT_ID() from 1 to 2. This preexisting leak remains unresolved despite the stated contract that hidden IDs must never surface.
    Evidence: build_ddl.go:4137 appends the hidden auto-increment key. column_cache.go:697 returns zero when all user values are explicit; table_cache.go:160-165 then selects the hidden column's nonzero result. Because the table also contains a user auto-increment column, the new gate accepts that result. The source-extracted composition probe confirmed this path with allocator returns 0 and 2. Filter the returned ID by its generating column's ownership; table-level existence is insufficient. The new mixed-column helper test does not exercise this behavior.

Validation:

  • Verified the assigned HEAD and clean source checkout. Frozen patch matches the Git delta apart from a terminal newline; diff whitespace and gofmt checks passed.

  • Reviewed every changed hunk, supplied comments, allocator result selection, DDL visibility, local/remote PRE_INSERT metadata, statement coordination, frontend publication, retry/error paths and test oracles.

  • Reused frozen successful exact-head build, Ubuntu/x86 UT, Linux/arm64 SCA, coverage and PROXY/pessimistic multi-CN BVT statuses. Skipped checks were not counted as passes.

  • Ran ownership_probe.go with Go 1.25.5 and CGO_ENABLED=0. Extracted production decision functions reproduced both defects; hidden-only and generated-user controls behaved correctly. Diagnostics are saved in the assigned attempt directory.

Limitations:

  • The probe uses minimal types and stubbed column-allocator returns grounded in the source trace; it is not a MatrixOne SQL or native package test. Source-derived SQL reproductions were saved but not executed.

  • Frozen evidence lacks per-test CI logs and the standalone linked-issue body. The PR's stated contract was reviewed; no performance benchmark was run.

…atrixorigin#29903)

A table with no user primary key -- the hidden fulltext/vector index
tables among them -- carries an allocator-owned hidden fake primary key
(__mo_fake_pk_col) that is auto_increment. insertAutoValues returned the
first generated value across ALL auto columns, so this internal key
could become the statement's LAST_INSERT_ID: a base-table INSERT that
synchronously populates a fulltext index surfaced the index's hidden key
instead of the base table's business key.

Return the first generated value among user-visible columns only, using
the authoritative isInternal ownership flag (hidden fake PK, from the
table definition) rather than a column name. This also fixes the two
edge cases a name-based check got wrong: a user column that happens to be
named __mo_fake_pk_col is visible and still surfaces, and a table whose
user auto column is given an explicit value while only the hidden key
generates publishes nothing. InsertValues' return is consumed solely by
the LAST_INSERT_ID publish, so scoping it to user-visible columns has no
other effect.

Tests:
- UT TestInsertValuesExcludesInternalFakePKFromLastInsertID (internal-only,
  user-only, visible __mo_fake_pk_col, mixed explicit-user+generated-hidden).
- BVT fulltext/fulltext_last_insert_id (base table + synchronous fulltext
  index build) and auto_increment/last_insert_id_hidden_pk (the two edge cases).

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

Copy link
Copy Markdown
Contributor Author

fixed

@XuPeng-SH

XuPeng-SH commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: def72a2fa0d13d332735f0e36f1d8366d0535954

Assessment: approve

Reviewed the complete six-file delta. Filtering LAST_INSERT_ID at the allocator’s existing column-ownership boundary resolves both prior findings: visible columns named __mo_fake_pk_col remain eligible, and hidden-only allocations cannot overwrite session state. No actionable defects remain.

Model: gpt-6-astra / xhigh

Session: 01a12d8f-7dea-77c2-a565-38d45b5564e4 · slot 0 · generation 0

Validation:

  • Traced ownership through creation, catalog reload, reset, allocation, preinsert, and frontend session updates; checked error handling, explicit values, mixed columns, and multirow behavior.

  • Reviewed new unit and SQL regression coverage for both prior findings and fulltext inserts, including first-generated-ID semantics.

  • Reused frozen exact-head successful shared build, Ubuntu unit tests, Linux static analysis, coverage, PROXY BVT, and pessimistic multi-CN BVT evidence; verified test discovery includes the changed tests.

  • Local checks passed: assigned HEAD and merge-base, clean source status, frozen patch correspondence, diff whitespace, Go formatting, and SQL/result statement correspondence.

Limitations:

@mergify

mergify Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-10-14 11:46 UTC · Rule: main · triggered by @cpegeric with the merge queue checkbox
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-10-14 12:11 UTC · at 7c90a020f9f8d5d23ed7b434bd417e9a02f2831f

This pull request spent 25 minutes 17 seconds in the queue, with no time running CI.

Reason

Pull request #30004 has been dequeued

Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.

The author needs to enable "Allow edits from maintainers" on this pull request.

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

This branch was previously deployed

1 inactive deployment
ci — 7c90a020 Deployed Oct 14, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dequeued kind/bug Something isn't working size/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants