PostgreSQL-限制总日志大小 [英] PostgreSQL - limit the total log size
问题描述
我有一个非常简单的要求-PostgreSQL日志目录应具有固定的最大大小,例如所有PostgreSQL日志最大10 GB.
I have a very simple requirement - PostgreSQL log directory should have fixed max size, e.g. max 10 GB for all PostgreSQL logs.
这可以通过创建固定大小的日志并使用日志轮转来实现(例如,轮流10个日志文件,每个日志文件1 GB).
It can be achieved by creating fixed-size logs and using log rotation (e.g. rotate 10 log files, 1 GB per log file).
我尝试使用PostgreSQL日志记录配置进行设置,但是我没有设法实现这一点.问题在于"log_truncate_on_rotation"不适用于基于大小的旋转,仅适用于基于时间的旋转.
I tried to set this up using PostgreSQL logging configuration, but I didn't manage to achieve that. The problem is that "log_truncate_on_rotation" does not works for size-based rotation, only for time-based rotation.
所以,我的问题是 - 是否可以仅使用 PostgreSQL 配置来实现,或者我必须使用其他配置?如果没有,为什么PostgreSQL没有该功能?
So, my question is - can it be achieved using only PostgreSQL configuration or I have to use something else? If not, why PostgreSQL does not have that feature?
我有几种选择:
- 使用"log_rotation_size"属性将最大文件大小设置为1 GB,然后设置CRON作业,如果文件数超过10,它将删除最旧的文件
- 使用logrotate实用程序,但是我每天执行一次的次数要多于一次
推荐答案
是的-您没有这种功能是正确的-您需要cron
Yes - you are right about no such feature - you need a cron
https://www.postgresql.org/docs/current/static/runtime-config-logging.html
log_truncate_on_rotation(布尔值)
log_truncate_on_rotation (boolean)
启用logging_collector时,此参数将导致PostgreSQL截断(覆盖)而不是附加到任何具有相同名称的现有日志文件.但是,截断会发生仅当由于基于时间的旋转而打开新文件时,不在服务器启动或基于大小的旋转期间.
When logging_collector is enabled, this parameter will cause PostgreSQL to truncate (overwrite), rather than append to, any existing log file of the same name. However, truncation will occur only when a new file is being opened due to time-based rotation, not during server startup or size-based rotation.
您甚至可以设置每分钟的工作来检查日志量:
you can set up even minutely job to check the amount of logs:
* * * * * rm $(ls -t /PGDATA/pg_log/* | tail -n 1)
这篇关于PostgreSQL-限制总日志大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!