Skip to content

Fix the stale check of repair plans and table-rerun for UUID and numeric keys - #190

Merged
mason-sharp merged 7 commits into
mainfrom
ace-228
Nov 2, 2026
Merged

mason-sharp merged 7 commits into
mainfrom
ace-228

Conversation

@danolivo

Copy link
Copy Markdown
Contributor

Fix the stale check of repair plans and table-rerun for UUID and numeric keys

What this fixes

1. A repair plan with allow_stale_repairs: false did not protect rows with a UUID or numeric key

The stale check reads the current commit timestamp of each target row and matches it to the diff report by row key. It built the key from raw pgx v5 values: a UUID printed as [0 0 0 ...], a numeric printed as a Go struct. Such a key never matched the key in the report, so the check was skipped with no message, and repair changed the row anyway.

What happens on main (checked with the new tests):

  • UUID key: a row that changed after the diff is overwritten by repair.
  • numeric key, and a composite (numeric, uuid) key: a row that changed after the diff is deleted by repair (see 2).

2. A numeric key with another scale on the other node was reported as two different rows

The block hash wraps numeric columns in trim_scale(), so 1.50 and 1.5 are equal there. The row fetch did not, and the row key kept the scale. So the row 1.50 on n1 and the row 1.5 on n2 were reported as "missing on n2" and "missing on n1", not as one changed row. A repair plan then applied the rules for missing rows to them: a delete rule deleted the row.

Now the row fetch uses the same column expression as the row hash, and the two rows are one row.

3. table-rerun lost rows with a UUID, numeric or time key

Rerun read rows with its own type switch, which knew only pgtype v4 types. The pool returns pgx v5 values, so these keys are printed in a different form than in the report. The rerun report gave a count of differences, but no rows.

4. table-rerun counted rows it could not match to the report

Rerun matched the rows it read to the report by a printed key. A key that prints in another form (for example 1.50 in a report from an earlier ACE and 1.5 now) did not match, and a row deleted on both nodes was counted too. Now the rerun key table includes the position of each report key, and rows are matched by that position. A row deleted on both nodes is not a difference.

5. table-diff and the mtree commands failed on an enum whose name starts with numeric

A column was wrapped in trim_scale() when the name of its type started with numeric or decimal. For an enum like numeric_level, this function doesn't exist, so the query failed. Now only numeric and decimal themselves are numeric types.

Changes that users can see

  • A numeric value in the diff report has no trailing zeros (7.5, not 7.50). Repair writes this form: a numeric column without a fixed scale gets the value without the trailing zeros on the target. A column with a fixed scale, such as numeric(10,2), is not affected. ACE already compared numeric values without the scale; now the report shows the value in the form it was compared.
  • The diff report has a format_version field (1). table-repair and table-rerun refuse a report with a version they do not know. A report without the field, from an earlier ACE, is version 0. table-repair and table-rerun remove the trailing zeros of its numeric values when they read it, so that the stale check finds the keys and the rerun counts a row that the report holds under two keys once.
  • The row fetch of a mismatched block and the row-hash query of the mtree diff have no ORDER BY. The order was not used, and with trim_scale() in the select list, an ORDER BY on the key would force a sort of the whole block.

What does not change

  • The row hash is the same, with one exception: a column of a domain over numeric whose name starts with numeric or decimal is not wrapped in trim_scale() any more. Only a Merkle tree for such a table must be rebuilt.
  • Timestamp and date keys are still printed in another form in the report (JSON string) than in a fresh read (time.Time), so the stale check is still skipped for them. This PR does not change that.

@danolivo danolivo self-assigned this Oct 26, 2026
@danolivo danolivo added bug Something isn't working enhancement New feature or request labels Oct 26, 2026
@danolivo
danolivo requested a review from mason-sharp October 26, 2026 06:57
@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6d163168-7607-405a-a689-da549d759039
Commits

Reviewing files that changed from the base of the PR and between 25bbdb5 and 063caeb.

Files selected for processing: 1
  • docs/CHANGELOG.md

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

The changes standardize numeric column expressions for hashing and fetching, add versioning and upgrade handling for diff reports, and update diff, rerun, Merkle, and repair paths to match rows using keys or report positions rather than query order.

Changes: Diff report and row matching

Layer / File(s) Summary
Numeric expressions and row hashing
db/queries/queries.go, db/queries/queries_test.go, tests/integration/col_expr_test.go, tests/integration/numeric_scale_test.go, tests/integration/native_pg_test.go, docs/CHANGELOG.md
Numeric detection now matches scalar numeric and decimal types exactly, excluding arrays and similarly named types. Shared expressions apply trim_scale() to those columns and cast selected types to text. Tests cover hash/fetch expression consistency and numeric scale cases.
Versioned diff reports
pkg/types/types.go, pkg/types/diff_format_test.go, pkg/common/utils.go, pkg/common/utils_test.go, internal/consistency/diff/table_diff.go, internal/consistency/mtree/merkle.go, internal/consistency/diff/table_rerun.go, internal/consistency/repair/table_repair.go, tests/integration/report_format_test.go, docs/CHANGELOG.md
Diff reports now include format_version. Version 0 reports can be upgraded using numeric-column metadata, while unsupported versions are rejected. Diff creation and rerun results set the current version.
Order-independent fetching and reruns
internal/consistency/diff/table_diff.go, internal/consistency/diff/table_rerun.go, internal/consistency/mtree/merkle.go, pkg/common/utils.go, pkg/common/utils_test.go, internal/consistency/mtree/merkle_test.go, tests/integration/scan_rows_test.go, docs/CHANGELOG.md
Diff and Merkle fetch queries no longer sort by primary key. Reruns associate fetched rows with report keys by stored position and skip keys missing from both nodes. Shared row-scanning helpers and tests cover row-order-independent comparisons.
Typed keys in stale repair
internal/consistency/repair/stale_repair.go, internal/consistency/repair/table_repair.go, tests/integration/scan_rows_test.go, docs/CHANGELOG.md
Stale repair now requires primary-key column types and uses typed key expressions when fetching commit timestamps. Delete execution receives target-node column types. Integration tests cover stale checks for numeric, UUID, and composite keys.

Priority: High

Merge Risk: Minimal · up to 063ca

No actionable merge-blocking risk remains.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title names the two main fixes: the stale check of repair plans and table-rerun for UUID and numeric keys. The PR also changes numeric detection, report format versioning, and row matching, but th…
Description check Passed The description is detailed and matches the changeset. It covers the stale check for UUID and numeric keys, numeric scale matching via the shared column expression, table-rerun row matching, numeric a…
Docstring Coverage Passed Docstring coverage is inconclusive: files from earlier reviews still need to be rechecked within the check's analysis limits. Not blocking.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the two main fixes: the stale check of repair plans and table-rerun for UUID and numeric keys. The PR also changes numeric detection, report format versioning, and row matching, but these support the same goal. The title is specific and matches the changeset.

Full details: Description check

Explanation

The description is detailed and matches the changeset. It covers the stale check for UUID and numeric keys, numeric scale matching via the shared column expression, table-rerun row matching, numeric and decimal type detection for enum names, the format_version field, removal of ORDER BY, and known limitations for timestamp and date keys.

Finishing Touches
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit checks the rows at dawn,
And trims the zeros one by one.
Keys find their place when orders change,
Old reports gain a version range.
The hashes match; the work is done,
The rabbit hops beneath the sun.

Comment @coderabbitai help to get the list of available commands.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 2 medium

Results:
2 new issues

Category Results
Complexity 2 medium

View in Codacy

🟢 Metrics 6 complexity · -9 duplication

Metric Results
Complexity 6
Duplication -9

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

The row hash and the row fetch of the diff engines each decided on their
own how a column is read. The hash wrapped a scalar numeric column in
trim_scale(); the fetch did not, and the Go code compensated for this
when it compared rows. If one side changes and the other does not, two
rows can hash the same and still be reported as different when their
block is fetched because of another row, or the reverse.

Now one function decides the SQL form of a column, and the row hash and
every row fetch start from it: table-diff, mtree diff and table-rerun.
The fetch adds the ::TEXT cast where the driver needs one, and names the
result after the column. table-rerun had its own copy of the cast rule
before.

A numeric primary key stored with another scale on the other node (1.50
and 1.5) is now one row. Before, the diff reported it as a row missing on
each node, and a repair plan applied the rules for missing rows to it.

The row fetches of a mismatched block have no ORDER BY now. In ORDER BY,
a plain name refers to the select-list entry of that name first, and
with this change that entry is trim_scale(pk) for a numeric key. The
database then cannot take the order from the primary key index and must
sort the whole block. The order was never used: the rows of the two
nodes are matched by key, and the report is sorted when it is written.
For the same reason the row-hash query of the mtree diff has no ORDER BY
either.

One change is visible: a numeric value in the diff report has no
trailing zeros now ("7.5", not "7.50"), as the hash compares it. Repair
writes this form, so a numeric column without a fixed scale on the
target gets the value without the trailing zeros. A column with a fixed
scale is not affected.

New tests:

- TestColExpr, TestRowHashExprUsesColExpr and
  TestFetchColExprStartsFromColExpr check the rule in unit tests.
- TestBuildFetchRowsSQLHasNoOrderBy and TestBuildRowHashQuery: the row
  fetch and the row-hash query of the mtree diff do not sort.
- TestCompareRowSetsIgnoresRowOrder: the result of the row comparison
  must not depend on the order of the rows.
- TestRowFetchMatchesRowHash checks on a server, for 32 column types,
  that the fetched text is the hashed text, per column and for the whole
  row. inet is a known gap: inet::text adds the netmask.
- TestNumericScaleInMismatchedBlock: a block differs because of one row,
  and other rows of it differ only in numeric scale. table-diff and mtree
  must report only the one row, with its numeric value in the new form.
- TestNumericKeyRepair: a numeric primary key stored as 1.50 on one node
  and as 1.5 on the other is one row for the diff, and repair finds it on
  the target by the key from the report. Simple and composite keys.

The new integration tests also run in TestNativePG.
A diff report had no version. If the form of the stored values changes,
repair and rerun cannot tell an old report from a new one, and they read
the values of one of them in a wrong way.

The report now has format_version, and the current version is 1.
table-diff, mtree diff and rerun set it when they create the report, so it
is also set on a report that is not written to a file. Repair and rerun
refuse a report with a version this build does not know, with an error
that names the version.

A report without the field was written before the field existed. It is
version 0, and repair and rerun accept it.

Tests:

- TestDiffOutputCheckFormatVersion and TestDiffOutputFormatVersionJSON
  (unit).
- TestDiffReportFormatVersionChecked (integration): table-diff writes
  version 1, repair and rerun refuse version 2, and repair accepts a
  report without the field.
Four places read table rows into Go values, each in its own way:
table-diff and mtree diff normalized the values to the form of the diff
report, table-rerun had its own type switch, and the stale check of
repair plans used the raw driver values. A row key is built from the
printed values, so the same row got different keys in different places.
Two of them were wrong:

- table-rerun's type switch matched only pgtype v4 types, but the pool
  returns pgx v5 values. A uuid, numeric or time key printed as a Go
  struct and did not match the key in the report, so the rerun dropped
  every row with such a key and still counted it as a difference.
- The stale check read the current commit timestamp of the target rows
  and matched them to the report by key. A numeric or uuid key did not
  match, so the check was skipped with no message, and repair overwrote
  rows that had changed after the diff.

Now one function reads every row: one entry per result column, each value
in the form of the diff report. table-diff, mtree diff, table-rerun and
the stale check use it, and the stale check selects the key columns in the
same form as the diff engines.

The form of a key column depends on its type, so the stale check needs
the column types. Without them a numeric key is read with its trailing
zeros and does not match the report. The check used to read the types
again when the caller gave none, and continued without them if that
failed. Now a missing type is an error, and the deletes of a
unidirectional repair, which gave no types, get the column types of the
target node, as the upserts do.

Timestamp and date keys still do not match between a report read from
JSON (a string) and a fresh read (time.Time). This commit does not change
that.

Tests:

- TestTableDiffRerunKeepsRows: a rerun with nothing changed must report
  the same rows as table-diff, on a table with a uuid key and numeric,
  time, interval and array columns. Before: no rows.
- TestAdvancedRepairPlan_StaleCheckFindsKey: rows with a numeric key, a
  uuid key and a composite (numeric, uuid) key change after the diff; a
  plan with allow_stale_repairs: false must leave them, both the update
  and the delete, and log the skip. Before: repair overwrote them.
table-rerun reads the rows of the report keys through a temporary key
table. It then matched each row read to the report by a key printed from
the values read. When a value prints in another form than the same value
in the report, the row looked missing on both nodes. An example is a
numeric key "1.50" in a report from an earlier ACE, read now as "1.5".
Such a row was counted as a persistent difference, but no row was
written for it.

The key table now has one more column: the position of the key in the
list of report keys. The rerun selects it with the row and matches the
row to its report key by this position. The database has already
matched the key values, so the printed form does not matter.

Now a row that is missing on both nodes was deleted on both nodes after
the diff. The nodes agree on it, so the rerun does not count it.

Tests:

- TestTableDiffRerunMatchesRowsByPosition: a report key "1.50" for a row
  read as "1.5". The rerun must report the row.
- TestTableDiffRerunRowGoneOnBothNodes deletes one of two different rows
  on both nodes after table-diff. The rerun must report one row.
A report of format version 0 was written before the row fetch wrapped
numeric columns in trim_scale(). It differs from version 1 in one way: a
scalar numeric value can keep its trailing zeros ("7.50"). This breaks two
things:

- The stale check of a repair plan matches the keys of the report with
  keys read from the target now ("7.5"). For such a key the check was
  skipped with no message, and repair could overwrite a row that had
  changed after the diff.
- A numeric key stored with another scale on each node is in such a
  report twice: "1.50" in the rows of one node and "1.5" in the rows of
  the other. table-rerun finds the same row for both keys and counted the
  row twice.

Repair and rerun now bring a version 0 report to version 1 after they
read it: the trailing zeros of the numeric values are removed, by the
column types of the table. Without the column types this is an error, so
that a report is never marked as upgraded while nothing was converted.

Tests:

- TestUpgradeDiffOutput and TestNumericColumns (unit): numeric values as
  strings and as json.Number lose their trailing zeros; other columns do
  not change; a version 1 report does not change; without column types
  the upgrade fails and the report keeps version 0.
- TestAdvancedRepairPlan_StaleCheckFindsKey/numeric_v0 (integration):
  the stale check finds the keys of a version 0 report.
- TestTableDiffRerunVersion0SplitKey (integration): a version 0 report
  with one row under the keys "1.50" and "1.5". The rerun must report
  the row once.
The column expression of the row hash is in db/queries, but the rules of
the row fetch that start from it were in pkg/common. So the rule "the
fetched value is the hashed value" was split between two packages, and
pkg/common had to import db/queries.

The row fetch rules are in db/queries now, next to the column expression
of the row hash. Their callers and their unit tests move with them, and
pkg/common does not import db/queries any more. No change in behavior.
A column was wrapped in trim_scale() when the name of its type started
with "numeric" or "decimal". A type whose name only starts with these
words, such as an enum numeric_level, has no trim_scale(), so the row hash
and the row fetch of such a table failed with an error.

Now the type must be numeric or decimal itself, with or without a typmod.

The row hash changes only for a column of a domain over numeric whose name
starts with "numeric" or "decimal": such a column is not wrapped in
trim_scale() any more. A Merkle tree of such a table must be built again.

Tests:

- TestIsNumericType (unit): enum-like names are not numeric.
- TestNumericNamedEnum (integration): table-diff on a table with a column
  of an enum named numeric_level. Before: the query failed.
@mason-sharp
mason-sharp requested review from rasifr and removed request for mason-sharp November 2, 2026 03:27
@mason-sharp
mason-sharp merged commit 85bcd38 into main Nov 2, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants