Skip to content

fix(vdiff): preserve PK definition order in getSourcePKCols - #905

Open
pedroalb wants to merge 1 commit into
slack-22.0from
fix/vdiff-source-pk-cols-ordering
Open

fix(vdiff): preserve PK definition order in getSourcePKCols#905
pedroalb wants to merge 1 commit into
slack-22.0from
fix/vdiff-source-pk-cols-ordering

Conversation

@pedroalb

Copy link
Copy Markdown

Summary

  • Fix getSourcePKCols to preserve PK definition order (SEQ_IN_INDEX) instead of column ordinal position
  • The bug causes false VDiff mismatches (ExtraRowsSource/ExtraRowsTarget) on resume when PK column order differs from ordinal position
  • Add regression test covering 4 PK ordering scenarios

Root Cause

getSourcePKCols put PrimaryKeyColumns into a map[string]struct{} (losing order), then iterated td.table.Columns in ORDINAL_POSITION order. On VDiff resume, VStreamRows receives the wrongly-ordered lastpk values and the row streamer pairs them positionally with its own pkColumns (which ARE in correct SEQ_IN_INDEX order from BaseShowPrimary). This corrupts the WHERE clause.

Fix

Replace the map+ordinal-scan with a column index lookup that preserves PK definition order.

Impact

Only affects tables where PK column order differs from column ordinal position AND VDiff resumes (via max-diff-duration timeout, auto-retry, or manual resume). Single uninterrupted VDiff passes are unaffected.

Test Plan

  • TestSourcePKColsOrdering regression test with 4 cases
  • CI passes

References

The getSourcePKCols function put PrimaryKeyColumns into a map (losing
order) then iterated td.table.Columns in ORDINAL_POSITION order. This
produced sourcePkCols in column ordinal order rather than PK definition
order (SEQ_IN_INDEX).

On VDiff resume, VStreamRows receives the wrongly-ordered lastpk values
and the row streamer pairs them positionally with its own pkColumns
(which ARE in correct SEQ_IN_INDEX order). This corrupts the WHERE
clause and generates false ExtraRowsSource/ExtraRowsTarget reports.

Fix: build a column-name-to-index map and iterate PrimaryKeyColumns in
definition order, preserving SEQ_IN_INDEX ordering.

Upstream PR: vitessio#20603
Related issue: vitessio#20601

Signed-off-by: Pedro Albuquerque <pedroalb@gmail.com>
Signed-off-by: Pedro Albuquerque <pedro.albuquerque@slack-corp.com>
@pedroalb
pedroalb requested a review from a team as a code owner July 17, 2026 15:23
@github-actions github-actions Bot added this to the v22.0.4 milestone Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant