How to speed up pg_dump when dumping large databases
After a series of rather hefty posts, let's take a breather with some lighter material.
Today we'll discuss how to speed up pg_dump when dealing with large data volumes.
Speeding up options discussed here:
- Compression
- Dump/restore without saving dumps on disk
- Parallelized
pg_dump - Advanced custom parallelization
Monitoring dump progress
- Verbose Output: Use
pg_dump --verboseto get detailed information during the dump process. - Progress Estimation: While
pg_dumpdoesn't provide a progress bar, you can estimate progress by monitoring the size of the output file or, in the case of plain-format dumps, using tools likepv. - System Monitoring: Monitor system resources (CPU, I/O) to infer activity levels during the dump.
Option 1: Consider applying compression
In the cases of weak disks or network, it makes sense to apply compression. Note that this is not going to help to speed the process up if disk IO and network (if it's used) are not saturated (inspect resource utilization with standard tools like iostat, top, and so on).
- Custom (
-Fc) and Directory (-Fd) Formats: These formats apply compression by default. The default method is gzip at level 6, unless specified otherwise. - Plain Format: No compression is applied by default. To compress, pipe the output through a compression utility, e.g.,
pg_dump ... | gzip.
Choosing compression methods and levels
PostgreSQL supports multiple compression methods:
zstd: Offers the best balance between speed and compression ratio. Levels 1–5 are optimal for most datasets.lz4: Fastest compression, but results in larger dump sizes.gzip: Widely supported but slower and less efficient compared tozstd.
For detailed benchmarks and recommendations, refer to Best pg_dump compression settings for Postgres in 2024