Sub topic 8
Speeding up large dumps and restores with parallel jobs
This chapter assumes you know basic SQL and psql usage, can work in a Linux shell, and understand PostgreSQL databases, roles, and transactions. It also assumes you can create test databases without affecting production.
Large logical backups can take a long time because PostgreSQL must read many tables and recreate their data. Parallel jobs let several workers process independent tables at the same time. This can reduce elapsed time, but it uses more CPU, memory, disk bandwidth, and database connections.
Parallel dump with directory format
The pg_dump option -j sets the number of parallel worker jobs. Parallel dumping requires directory format, selected with -Fd. Other formats do not support parallel dumping.
pg_dump -U postgres -Fd -j 4 -f mydb_dir mydbThis command creates the directory mydb_dir. Four workers read table data concurrently. The directory is a collection of files, not one portable archive file.
A directory dump cannot be treated as a single file during copying. Copy the complete directory, preserve its contents, and do not modify it while a dump or restore is running.
Parallel restore with pg_restore
The pg_restore command can restore custom-format and directory-format dumps in parallel. A custom-format dump is created with -Fc.
pg_dump -U postgres -Fc -f mydb.dump mydb
createdb -U postgres mydb_restored
pg_restore -U postgres -j 4 -d mydb_restored --no-owner mydb.dumpThe restore coordinator reads the archive and assigns independent work to four workers. PostgreSQL still performs some steps in order. For example, it must create tables before loading rows, and it must load data before creating some indexes and constraints.
The --no-owner option prevents the restore from trying to assign objects to the original owners. Use it when those roles do not exist on the destination. If ownership matters, create the required roles first or omit this option.
Choosing the job count
More workers do not always mean a faster backup. Test with realistic data and storage. A useful starting point is two to four jobs, then increase the value while watching system load.
| Resource | Effect of more jobs |
|---|---|
| CPU | More compression, parsing, and index work |
| Storage | More concurrent reads and writes |
| Memory | More buffers and worker processes |
| Connections | More PostgreSQL sessions |
The source database may slow down during a parallel dump because workers compete with application queries. A parallel restore can also overload the destination storage. Schedule the operation during a quiet period when possible.
What cannot run in parallel
Plain SQL dumps are restored by psql, which processes statements as one stream. The -j option does not apply to this restore path. Tar-format dumps also cannot be created or restored in parallel with the standard dump tools.
Practical checks
Before starting, confirm that the destination has enough free space for the restored data, indexes, temporary files, and logs. Check that the PostgreSQL user can connect repeatedly. After creating an archive, inspect it before transferring or restoring it.
pg_restore --list mydb.dump | head -n 20For a directory dump, keep the entire directory together. A partial copy is not a valid backup. Always perform a test restore on a separate database before relying on the backup.
Exercise
Which command creates a dump that can use four parallel dump workers?
Show answer
pgdump -U postgres -Fd -j 4 -f mydbdir mydb