<@U0675Q5MTL5> High value of temporary files why is it bad? How much does this affect performance? Does this mean that you need to be passionate about work_mem?
Temp Files: total size 8572 GB (~75GB/day) - Primary, 85 TB (~745GB/day) - Replica (Stats Age 114 days)

The creation of a large volume of temporary files in Postgres can indicate potential performance issues for several reasons:
-
Disk I/O: Temporary files are written to disk, which is several orders of magnitude slower than memory. Accessing these files frequently can severely degrade database performance.
-
Resource Contention: Utilizing disk space for temporary files means less space is available for other critical operations, such as writing WALs or storing permanent table data. This can lead to contention and further slow down the system.
-
Missed Optimization: If queries generate large temporary files, it often suggests that they may not be optimized, for instance, needing better indexes or join conditions. This means they are using more resources than necessary.
-
work_mem Setting: A high value of temporary files could point to a
work_mem
setting that is too low. However, increasingwork_mem
indiscriminately can also lead to issues if not enough memory is available for all concurrent operations, possibly leading to swapping or out-of-memory conditions.
It's worth investigating queries that generate large temporary files and assessing whether:
-
The queries can be optimized (e.g., by changing joins, adding indexes) to reduce sorting or hashing operations that spill to disk.
-
The
work_mem
setting is appropriate for the workload. Be cautious when adjusting this parameter as it is allocated per operation, not per connection, and can quickly lead to high memory consumption.
Regularly reviewing the work_mem
setting and the performance of heavy operations is a good practice. This attention should balance the risk of consuming too much memory with the performance gains from higher work_mem
values.
Remember, any change to work_mem
or other server configuration should be tested thoroughly before being applied to production to ensure that it doesn't have unintended negative impacts.
Since you're dealing with a substantial size of temporary files both on the primary and replica, here's how to further investigate the problem:
-
Identify the queries generating large temporary files using
pg_stat_statements
or log files withlog_temp_files
setting. -
Evaluate current
work_mem
setting and compare it against the typical size of sort/hash operations in your workload. Adjust cautiously. -
Review your queries and indexes to ensure that they are efficient.
-
If temporary files are unavoidable due to the nature of your workload, consider using faster storage (SSDs) for the temp file directory.
-
Monitor the memory usage to make sure that the server has enough RAM to handle the increased
work_mem
, if you choose to increase it.
Remember to profile changes in a testing environment before applying them to production to measure their impact.
Would you like to perform specific checks or adjustments in your configuration or queries?