Skip to content

[Snapshots] Precision loss on json/jsonb large integers #686

@eminano

Description

@eminano

Describe the bug
When having a json/jsonb structure that contains large integers on the source database, the snapshot replicates the data to the target with precision loss.

Example:
Source: '{"number": 1234567890123456789}'
Target: '{"number": 1234567890123456800}'

This happens because the snapshot retrieves the row values via the pgx library, which uses json to unmarshal json/jsonb types under the hood. The encoding/json library unmarshals integers into float64 types, which is what's causing the loss of precision in very large integers (numbers > 2^53-1 (9,007,199,254,740,991) lose precision).

To Reproduce
Steps to reproduce the behavior:

  1. CREATE TABLE test_json (id int, data jsonb);
  2. INSERT INTO test_json VALUES (1, '{"num": 1234567890123456789}');
  3. Run pgstream snapshot
  4. Compare the value on source and target.
    SELECT data->>'num' FROM test_json WHERE id = 1;

Expected behavior
Precision should not be lost between source and target.

Setup (please complete the following information):

  • pgstream version: v0.9.5
  • Postgres version: 18.1
  • Postgres environment: self-hosted

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingsnapshotSnapshot related work

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions