We have about 200 GB files inside temp
folder in PostgreSQL. And it’s time to clear it but I’m not sure is it safe or not. Thats why I want to look at these files first. File names look like: pgsql_tmp644088.0
. How can I open and read them? With Notepad I can only see some symbols, for example: ЎҐЖ№T ipadinstagram%instagram_stories j‹74
. What is your reccomendations?
Question posted in PostgreSQL
The official documentation can be found here.
The official documentation can be found here.
2
Answers
Do not manually delete temp file. As a first step i would check what operations are using temp files. You have some options to enable logs on queries writing to temp files:
or
Use pgbadger
Query table Pg_stat_statements, as follows:
SELECT interval ‘1 millisecond’ * total_time AS total_exec_time,
total_time / calls AS avg_exec_time_ms,
temp_blks_written,
query AS query
FROM pg_stat_statements
WHERE temp_blks_written > 0
ORDER BY temp_blks_written DESC
LIMIT 20;
Query pg_stat_database to give db level stats, as follows:
select datname, temp_files , pg_size_pretty(temp_bytes) as temp_file_size FROM pg_stat_database order by temp_bytes desc;
Run EXPLAIN ANALYZE and compare work_mem size with values printed by explain analyze.
As a general recommendation set temp_file_limit to a value different than -1 (default).
Be sure to use correct indices to tune queries.
Tune work_mem (carefully).
Set statement_timeout to an optimal value.
Please note:
Temp files are created when memory is not sufficient.
Simply restart PostgreSQL, and it should delete all the temporary files that were left behind.
Normally, temporary files don’t exist longer than the duration of a query. Check for long running queries.