DEV Community

dbSlice by Thinwork
dbSlice by Thinwork

Posted on Originally published at dbslice.net

Catch up a PostgreSQL replica after a network outage, from the command line

A PostgreSQL replica keeps a copy of a SQL Server table. A job copies only what changed, every few minutes. Then the network goes down for about 6 hours. When it comes back, how do you catch up without copying the whole table, and how do you prove the replica is in sync?

This is a lab example with a generic payments table (40,000 rows before the outage, no real data) and dbSlice by Thinwork. During the outage the source got 2,160 new payments, and 400 pending payments settled. Two small job files cover it. Neither holds a password: they only name saved connection profiles.

The two jobs

  • replica-payments-sync.json: "tipo": "sync", "mode": "Upsert", "watermark": "updated_at". Before it reads, it saves the highest updated_at as the mark. The next run reads only rows with updated_at >= mark.
  • replica-payments-check.json: "tipo": "status", read only, with "where" on the source limited to the last 8 hours.

Windows:

dbSlice.exe replica-payments-check.json check.log
dbSlice.exe replica-payments-sync.json sync.log
Enter fullscreen mode Exit fullscreen mode

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 check found

OK  Comparison completed in 0.5s: 108 matched, 2,160 missing in destination, 39,492 missing in source. 400 with different column values.
Enter fullscreen mode Exit fullscreen mode

2,160 new payments and 400 settled ones. The 39,492 "missing in source" are not a problem: the where filters only the source, so the replica's history outside the window shows up there.

Catch up and prove it

The same sync job that runs every few minutes:

OK  Done: 2,561 read, 2,160 inserted, 401 upsert/update in 0.6s
Enter fullscreen mode Exit fullscreen mode

2,561 = 2,160 + 400 + 1. The extra one is the row exactly at the mark (>= rereads it, so a row written in the same second is not lost). Not 42,160 rows: only what changed since the mark.

The second check:

OK  Comparison completed in 0.5s: 2,668 matched, 0 missing in destination, 39,492 missing in source. 0 with different column values.
Enter fullscreen mode Exit fullscreen mode

Running the sync again reads 1 row (the one at the mark), inserts 0. Linux returned the same counts.

Limits

The watermark does not see deletions: a row deleted on the source stays in the replica. A check of the whole table, without where, finds them. watermarkLookbackMinutes rereads a margin before the mark, for clocks or long transactions, at the cost of rereading it on every run.

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. Previous in the series: reconcile a SQL Server to PostgreSQL migration.

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


Originally published at dbslice.net.

Top comments (0)