Below are some PostgreSQL tuning recommendations that significantly improve bulk-data load performance by sacrificing durability and crash recovery guarantees. Use these only in isolated bulk-loading scenarios and be sure to revert to safe settings immediately after loading. Here are some of the common adjustments:
1. Disable Synchronous and Forced Disk Writes
fsync = off
Disables forced flushing of data to disk at each commit. This may lead to corruption in the event of a crash or power loss, but it greatly speeds up inserts.
synchronous_commit = off
Instructs PostgreSQL not to wait for WAL records to be flushed to disk for commit acknowledgement. This reduces commit latency at the cost of potentially losing the last transactions if a crash occurs.
full_page_writes = off
When disabled, PostgreSQL does not write full pages to WAL on the first modification. This can reduce write overhead but increases risk in the event of a hardware failure.
wal_level = minimal
If you are not planning to use replication or point-in-time recovery, setting the WAL level to minimal reduces WAL overhead.
2. Adjust Checkpoint Settings
Bulk loads can be slowed by too frequent checkpoints. Consider:
Increase checkpoint interval and size
checkpoint_timeout: Increase to a higher value (e.g., several hours) so that checkpoints occur less frequently.
max_wal_size (or in older versions, checkpoint_segments): Increase this value to allow more WAL data to accumulate between checkpoints.
checkpoint_completion_target
Set this closer to 1 to spread the checkpoint write IO over a longer period. This can smooth IO during heavy load operations.
3. Memory and Buffers Adjustments
maintenance_work_mem
Increase this setting to allocate more memory for maintenance tasks (such as index creation). For a heavy bulk load, setting this to a larger value (e.g., several GB on a capable system) can speed up index creation and vacuum operations.
shared_buffers
While this often depends on your overall workload and system memory, increasing shared_buffers can help in caching more data in memory during bulk operations.
work_mem
Depending on your query patterns during the bulk load, increasing work_mem may help with sorting and joining operations.
4. Consider Using Unlogged Tables
For an initial data load where durability isn’t required (during the loading phase only):
Use UNLOGGED tables. They do not write data to the WAL, which makes bulk inserts much faster. Example:
sql
CREATE UNLOGGED TABLE my_data (...);
Remember: Once the load is complete, if persistence is needed, you can dump the data and recreate it in a logged table.
5. Other Recommendations
Disable auto vacuum or tune it down temporarily.
Auto vacuum may run concurrently and affect bulk load speed. You can disable it temporarily (but make sure to run vacuum or re-enable it afterward).
Batch your transactions.
If possible, commit in larger transactions rather than many small ones to reduce overhead, especially when commit flush delays are disabled.
Important Caveat
These settings should only be applied temporarily in environments where data loss is acceptable (e.g., initial data load or testing environments).
After completing the load, you must revert these settings to ensure data durability and consistency. Failure to do so may result in significant data loss or corruption in the event of a crash.
By applying these settings, you are effectively instructing PostgreSQL to maximize throughput for bulk data loading at the expense of reliability. Always ensure that you have a proper backup or that the data can be reloaded if needed.