Log rotation¶
Version added: 3.1.0
logrotate controls the growth of log files on PostgreSQL instance Pods. Without rotation, PostgreSQL and pgBackRest logs can grow until they consume the Persistent Volume and affect database stability. logrotate keeps retention and disk usage predictable.
In Percona Operator for PostgreSQL, logrotate runs in a dedicated logrotate sidecar container on every instance Pod when persistent logging is enabled. It reads the log files at the specified path and rotates them according to the set of rules.
By default, logrotate rotates logs as follows:
-
PostgreSQL server
*.logand*.csvlog files under/pgdata/pgNN/log/:- Rotates log files when they reach 100 megabytes in size (
size 100M). This acts as a safety measure to prevent the logs from using too much disk space. The rotation by size does not replace or interfere with PostgreSQL’s built-in log rotation, which is based on the age or time of the log files using thelogging_collectorsetting. This ensures both rotation methods work together without causing conflicts. - Keeps up to 7 rotated files
- Uses
copytruncate, which meanslogrotatecopies the log file contents to a rotated file and empties the original file in place. This allows processes like PostgreSQL and pgBackRest to keep writing to the same log file without needing to close and reopen it, ensuring uninterrupted logging during rotation.
- Rotates log files when they reach 100 megabytes in size (
-
pgBackRest client logs on instance Pods under
/pgdata/pgbackrest/log/*.log:- Rotates daily, or earlier when a file reaches 100 megabytes in size (
maxsize 100M) - Keeps up to 7 rotated files
- Uses
copytruncate - Skips missing or empty log files and leaves rotated logs uncompressed
- Runs on schedule. By default, this is once per day at midnight.
- Rotates daily, or earlier when a file reaches 100 megabytes in size (
Configure log rotation¶
You can customize log rotation if you need different retention, size limits, or want to rotate additional files. Configure it in these ways:
- Override the default logrotate configuration via the Custom Resource
- Add additional configuration via a ConfigMap
- Set a custom log rotation schedule
When you change logrotate configuration, the Operator updates the configuration hash and makes a rolling restart of the instance Pods.
Override the default logrotate configuration¶
Use the spec.logcollector.logRotate.configuration section in the Custom Resource to completely override the default logrotate settings for the Operator-managed postgres.conf snippet.
Important
You must provide the full logrotate configuration because the Operator replaces the default configuration with the one you provide. Refer to the default configuration to see the built-in rules and use it as a guide for your custom settings.
Here’s an example configuration that rotates PostgreSQL logs hourly and keeps three copies:
spec:
logcollector:
enabled: true
image: docker.io/percona/fluentbit:{{ logcollector }}
logRotate:
schedule: "0 * * * *"
configuration: |
/pgdata/pg*/log/*.log
/pgdata/pg*/log/*.csv {
hourly
rotate 3
missingok
notifempty
copytruncate
sharedscripts
}
Apply the configuration:
kubectl apply -f deploy/cr.yaml -n <namespace>
Extend the default configuration via ConfigMap¶
You can pass additional logrotate rules via a ConfigMap. Use spec.logcollector.logRotate.extraConfig.name to load additional .conf files from that ConfigMap. The file name must end with .conf.
Important
The postgres.conf key name is reserved for the Operator-managed main configuration. Do not use it in your custom ConfigMap.
For example, you want to record that a log rotation ran. To do that, add a postrotate script to your logrotate configuration. After logrotate finishes rotating the matched files, it appends a timestamped line to a file on the data volume
-
Create a ConfigMap. For example,
custom-logrotate.yaml:custom-logrotate.yamlapiVersion: v1 kind: ConfigMap metadata: name: my-logrotate-extra namespace: <namespace> data: extra.conf: | /pgdata/pg18/log/*.log { daily rotate 7 missingok notifempty copytruncate sharedscripts postrotate echo "$(date -u +%FT%TZ) rotated on $POD_NAME" >> /pgdata/logrotate-postrotate.log endscript } -
Create the ConfigMap:
kubectl apply -f custom-logrotate.yaml -n <namespace> -
Reference it in the Custom Resource:
spec: logcollector: enabled: true image: docker.io/percona/fluentbit:{{ logcollector }} logRotate: extraConfig: name: my-logrotate-config -
Apply the Custom Resource:
kubectl apply -f deploy/cr.yaml -n <namespace>
Set a custom schedule¶
Use spec.logcollector.logRotate.schedule to set the rotation schedule as a cron expression. The default is once per day at midnight (0 0 * * *).
This example runs log rotation every 6 hours:
spec:
logcollector:
enabled: true
image: docker.io/percona/fluentbit:{{ logcollector }}
logRotate:
schedule: "0 */6 * * *"
Apply the Custom Resource:
kubectl apply -f deploy/cr.yaml -n <namespace>
Created: September 9, 2026