1

In my postgresql.conf file, I have the following settings

logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
log_rotation_size = 10

But my log file is not getting truncated after 10 KB. please help.

1
  • Did you reload Postgres after you modified the configuration file? Sep 26, 2013 at 13:51

1 Answer 1

6

The log truncation doesn't happen because postgres has a protection to avoid truncating a log file that appears to be the same as the one it's currently logging into. Otherwise that would mean loosing the latest log entries, which seems hardly ever desirable. In fact, the doc on log_truncate_on_rotation says:

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

To solve this, you should change log_filename from 'postgresql-%a.log' to a name with a time granularity that is compatible with the log_rotation_size. As an example:

log_filename = postgresql-%a-%H.log

With these settings, the purpose of log_truncate_on_rotation would be to overwrite the log of the previous day at the same hour for the same application name if it happens to exist.

I assume that you are just testing the log rotation feature, because in reality 10Kb is too small to be useful, and overriding previous logs is dubious in production use. If you have hard constraints on the size of logs, you should combine the logfile switching done by postgres with an external cron job that aggressively removes the older logs as opposed to relying only on log_truncate_on_rotation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.