Repository navigation
Fix the stale check of repair plans and table-rerun for UUID and numeric keys - #190
Merged
Merged
Conversation
Up to standards ✅🟢 Issues
|
| Category | Results |
|---|---|
| Complexity | 2 medium |
🟢 Metrics 6 complexity · -9 duplication
Metric Results Complexity 6 Duplication -9
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.
rasifr
approved these changes
Nov 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: falsedid not protect rows with a UUID or numeric keyThe 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):
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(), so1.50and1.5are equal there. The row fetch did not, and the row key kept the scale. So the row1.50on n1 and the row1.5on 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: adeleterule 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-rerunlost rows with a UUID, numeric or time keyRerun 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-reruncounted rows it could not match to the reportRerun matched the rows it read to the report by a printed key. A key that prints in another form (for example
1.50in a report from an earlier ACE and1.5now) 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
numericA column was wrapped in
trim_scale()when the name of its type started withnumericordecimal. For an enum likenumeric_level, this function doesn't exist, so the query failed. Now onlynumericanddecimalthemselves are numeric types.Changes that users can see
7.5, not7.50). Repair writes this form: anumericcolumn without a fixed scale gets the value without the trailing zeros on the target. A column with a fixed scale, such asnumeric(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.format_versionfield (1).table-repairandtable-rerunrefuse a report with a version they do not know. A report without the field, from an earlier ACE, is version 0.table-repairandtable-rerunremove 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.ORDER BY. The order was not used, and withtrim_scale()in the select list, anORDER BYon the key would force a sort of the whole block.What does not change
numericordecimalis not wrapped intrim_scale()any more. Only a Merkle tree for such a table must be rebuilt.time.Time), so the stale check is still skipped for them. This PR does not change that.