When a query create temporary files, it writes two entries for each one in the log file. The first one contains the information of the temporary file itself, the second one has the full SQL query. Something like this:
2020-05-08 11:02:32 EDT 2020-05-08 10:17:04 EDT 19297 60/837 0 LOG: temporary file: path "base/pgsql_tmp/pgsql_tmp19297.3079949", size 297
2020-05-08 11:02:32 EDT 2020-05-08 10:17:04 EDT 19297 60/837 0 STATEMENT: SELECT ...
Sometimes, a query will write millions of small temporary files (for whatever reason it doesn't create a big one). But then the SQL query will be logged all those millions of times in my postgresql-10-main.log file and fill the logs file system.
Is there a way to tell Postgres to log the statement only upon the creation of the first temporary file? I don't want to completely disable this logging as it is quite helpful in identifying and addressing queries that use too much temp space.
My current log configuration is as follows:
log_timezone = 'localtime'
log_statement = 'none'
log_duration = off
log_min_duration_statement = 5000
log_line_prefix = '%t %s %p %v %x '
log_checkpoints = on
log_connections = on
log_disconnections = on
log_lock_waits = on
track_functions = all
log_temp_files = 0
log_autovacuum_min_duration = 0
Thank you!