r/mysql • • Oct 19 '26

troubleshooting A JSON column round-trip changed ~11% of my float values on MySQL 8.4 (and broke a hash-chained audit log)

I had a tamper-evident audit log (each row stores the SHA-256 of the previous hash + payload). It verified fine on SQLite, then failed on MySQL at event 34. Nobody had touched the data.

Cause: the payload was stored in a JSON column. MySQL parses JSON into its binary format and re-renders it on read, and for some doubles the last digit changes:

0.9333333333333333 -> 0.9333333333333332

0.42451918914251396 -> 0.4245191891425139

My verifier re-serialized what it read back, so the bytes differed and the hash didn't match.

I measured it on a clean MySQL 8.4.10 vs MariaDB 10.4.34, default settings. I inserted the same {"v": x} into a TEXT column and a JSON column and compared the values read back:

- 10,000 random.random() doubles: 1,075 changed (10.75%)

- 2,000 ratios like i/15: 274 changed (13.7%)

- 2,000 two-decimal amounts: 0 changed

- MariaDB: 0 changed (its JSON is LONGTEXT with a validity check)

Two other things that bite hash chains:

- Keys get reordered, and whitespace normalized (documented: MySQL sorts object keys).

- DATETIME '10:15:30.999999' came back as 10:15:31 on MySQL (it rounds unless TIME_TRUNCATE_FRACTIONAL is set) but 10:15:30 on MariaDB (it truncated).

Looks related to Bug #112904 (JSON parser not in full-precision mode).

What fixed it: serialize once, hash that exact string, store that exact string in a TEXT column, and have the verifier hash the stored string without ever parsing and re-dumping it. Timestamps in the payload are naive UTC with whole seconds. After that, the chain verified clean across 8,424 events.

Has anyone seen this with Postgres JSONB? Its docs say it doesn't keep key order or whitespace either, but I haven't measured numbers there.

10 Upvotes

20 comments sorted by

3

u/bogdanelcs Oct 20 '26

From what the docs say, Postgres JSONB stores numbers as numeric, not doubles, so 0.9333333333333333 should come back with the same digits (I haven’t measured it, though). Key order and whitespace are lost with JSONB just like in MySQL, but the plain json type keeps the original text exactly. For anything hashed I’d still go with your fix of hashing the stored string, or canonicalize with RFC 8785 (JCS) before hashing so the bytes are defined no matter which database sits underneath.

1

u/Neither_Opinion69 Oct 20 '26

Yeah this fix working fine though.

2

u/Neither_Opinion69 Oct 19 '26

These were model scores and ratios, though, and the point is wider than floats: anything you hash has to be stored as the exact bytes you hashed. Key reordering and DATETIME rounding break it the same way.

2

u/Significant_Tune9219 Oct 20 '26

Postgres jsonb will not give you the original bytes either. It stores a binary value, drops whitespace, and does not keep object key order, and a JSON number is kept as numeric, so the digits you read back are a rendering of that numeric rather than the characters you inserted. Two-decimal amounts often survive, the same way they did for you, and a long float often does not. Hash the exact string in a text column. If you also want jsonb so you can query a field, keep it as a second column that is never part of the hash.

1

u/Neither_Opinion69 Oct 20 '26

Hmm, means maintaining a reference column

4

u/chicago913 Oct 19 '26

That column was load-bearing — and that's what really matters

1

u/AnnaLeeCoop Oct 21 '26

been there. Postgres JSONB will bite you the exact same way. It parses the payload into a custom binary format, meaning it strips whitespace, reorders keys, and casts numbers internally, which instantly breaks raw string hashes.

I ran into this with strict audit trails and had to scrap binary JSON columns entirely. MariaDB's architectural choice to implement JSON as a LONGTEXT alias with a validity constraint is a best way to do. It preserves your exact input string byte-for-byte, preventing these silent data mutations under the hood without forcing you into clumsy application-side workarounds.

1

u/Neither_Opinion69 Oct 21 '26

Yes, conclusion is MariaDB is better at handling this.

1

u/Aggressive_Ad_5454 Oct 20 '26

Thanks for pointing this out. Somebody will probably stay out of this particular pitfall after reading this.

1

u/jonathaz Oct 20 '26

JSON numbers are all double precision floating point, except that for no particular reason NaN and the INF values have no representation. You can output these in language bindings whose default settings will let you output them and then fail to parse them back in. It sounds like yours may be valid decimal expansions of doubles that the JSON serde in the database don’t round trip properly. But note that you could produce a number as text starting from something that isn’t a double that could have the same problem. I don’t think that for example, putting more digits than a double can represent, or a whole number that’s larger than doubles can represent without error is invalid JSON, not completely sure, but those cases wouldn’t roundtrip either.

1

u/Neither_Opinion69 Oct 20 '26

Will have to try that to find out

1

u/NastyPastyLucas Oct 20 '26 edited Oct 20 '26

Are you sure you are encoding the floats correctly? The errors do look like rounding errors between your maths program and the databases storage. See if it still happens when you use float/decimal or just a different type altogether like hex to represent the bytes.

Edit yeah float 64bit only really stores 15ish decimals and the examples you gave have 16 and 17 decimals that definitely does not fit. They are just trimming the value to fit into their domain. All floating point implementations work this way - use strings or decimals if you can't fix this in your program.

1

u/Neither_Opinion69 Oct 20 '26

Yes, fixed it this way, though

1

u/NastyPastyLucas Oct 20 '26

Sorry which way?

1

u/Neither_Opinion69 Oct 20 '26

I serialised it once, hashed that exact string, stored it, and verified it against that string.

1

u/NastyPastyLucas Oct 20 '26

Fair enough, for what its worth Postgres jsonb would've serialized this correctly as it uses the numeric type for numbers. Numeric will hold just about any number as written so long as it has less than 16 thousand or so decimal places (which I think fits your use case).

```

# select '{"a":0.123456789012345678901234567890}'::jsonb;

jsonb

-----------------------------------------

{"a": 0.123456789012345678901234567890}

(1 row)

```

1

u/Neither_Opinion69 Oct 20 '26

Noted, will try that