A large table was loaded from SQL Server into PostgreSQL. The load is over, but you do not trust it yet. Did it stop early? And the source kept taking writes while the load ran: were those rows copied with their old values?

This is a lab example with a generic orders table (50,000 rows, no real data) and dbSlice by Thinwork. Two small job files cover it. Neither holds a password: they only name saved connection profiles.

The two jobs

  • migration-orders-check.json: "tipo": "status". Compares and only reads.
  • migration-orders-fix.json: "tipo": "sync", "mode": "Upsert". Inserts missing rows and updates existing ones. Never deletes.

Windows:

dbSlice.exe migration-orders-check.json check.log dbSlice.exe migration-orders-fix.json fix.log 

Linux (Docker edition) runs the same files through the jobs API with curl and jq. The token goes only in the Authorization: Bearer header (dbs_xxxxxxxx_...).

What the first compare found

OK Comparison completed in 0.9s: 48,495 matched, 1,200 missing in destination, 0 missing in source. 305 with different column values. 

The load stopped at row 48,800, and 305 orders changed on the source after they were copied.

The Upsert and the proof

OK Done: 50,000 read, 1,200 inserted, 48,800 upsert/update in 2.7s 

The second compare:

OK Comparison completed in 0.9s: 50,000 matched, 0 missing in destination, 0 missing in source. 0 with different column values. 

Linux returned the same counts.

Running the fix again

OK Done: 50,000 read, 0 inserted, 50,000 upsert/update in 2.6s 

Upsert rewrites the matched rows. The result is idempotent, so nothing changes, but the work is not zero. The proof that it is finished is the second compare with 0 / 0 / 0. For a very large table, add a where to the source of the fix job to limit it to the period that was in use during the migration. The where filters only the source, so a compare with the same where shows "missing on source" for rows outside the window.

On failure, the CLI exit code is non-zero, which is what cron and CI need.

The full article has the job files, the Linux steps and scheduling: dbslice.net.

dbSlice is our own product, so take this with that in mind.

Originally published at dbslice.net.