在PostgreSQL中打开临时文件

huangapple go评论52阅读模式
英文:

Open temp files in PostgreSQL

问题

我们在PostgreSQL的temp文件夹中有大约200GB的文件。现在是清理它们的时候,但我不确定是否安全。这就是为什么我想先查看这些文件。文件名看起来像这样:pgsql_tmp644088.0。我如何打开并阅读它们?使用记事本,我只能看到一些符号,例如: ЎҐЖ˜№T ipadinstagram%instagram_stories j‹74。你有什么建议?

英文:

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?

答案1

得分: 1

只需重新启动PostgreSQL,它就会删除所有留下的临时文件。

通常,临时文件不会存在超过查询持续时间的时间。检查是否存在长时间运行的查询。

英文:

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.

答案2

得分: 0

不要手动删除临时文件。作为第一步,我会检查哪些操作正在使用临时文件。您有一些选项来启用记录写入临时文件的日志:

  1. 启用 log_tmp_files

或者

  1. 使用 pgbadger

  2. 查询表 Pg_stat_statements,如下所示:

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;
  1. 查询 pg_stat_database 以获取数据库级别的统计信息,如下所示:
select datname, temp_files, pg_size_pretty(temp_bytes) as temp_file_size FROM pg_stat_database order by temp_bytes desc;
  1. 运行 EXPLAIN ANALYZE 并将 work_mem 大小与 explain analyze 打印的值进行比较。

作为一般建议,请将 temp_file_limit 设置为与 -1(默认值)不同的值。确保使用正确的索引来调整查询。小心地调整 work_mem。将 statement_timeout 设置为最佳值。

请注意:
当内存不足时会创建临时文件。

英文:

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:

  1. Enable log_tmp_files

or

  1. Use pgbadger

  2. 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;

  3. 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;

  4. 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.

huangapple
  • 本文由 发表于 2023年6月15日 21:59:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定