brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: pg_restore -j restores a large PostgreSQL dump with several parallel jobs

Postgresql
~/tips/tip-database-pg-restore-jobs-parallel

pg_restore -j 4 -d appdb appdb.dump

Restores a PostgreSQL archive dump using several concurrent jobs so large restores finish sooner.

Dumping with pg_dump -j is the half of parallel backups people usually hear about. The restore side is where the waiting often hurts more, and pg_restore has its own switch for it.

pg_restore -j 4 -d appdb appdb.dump

With -j (--jobs), pg_restore runs the slowest steps concurrently: loading data, creating indexes and creating constraints. Each job uses its own connection to the server. The docs suggest starting with the number of CPU cores on the server, and say that values higher than that can still help, while a value that is too high slows things down again.

The rules from the docs are what trip people up:

  • It only works with the custom and directory archive formats. A plain SQL file made with pg_dump has to go through psql.
  • The input must be a regular file or directory, not a pipe or standard input. So cat appdb.dump | pg_restore -j 4 will not do.
  • It cannot be combined with --single-transaction.
  • It is ignored when pg_restore only writes a script instead of connecting to a database.

A parallel restore is not all-or-nothing the way -1 is, so a failure halfway leaves a partly restored database. I would restore into an empty scratch database first, check it, and only then think about the real one. And do not go overboard with the job count on a small home server: a few jobs and a disk that cannot keep up is just thrashing, which the docs also warn about.

Further reading: PostgreSQL documentation: pg_restore

More Postgresql tips FROM pg_settings FROM pg_hba_file_rules LOAD 'auto_explain'; CREATE EXTENSION pgstattuple; pg_dump -O -x -f mydb.sql mydb All Postgresql tips (24) All topics

Link