← Back to PureKit

How logrotate copytruncate Deadlocked a 349 GB PostgreSQL Log for 386 Days

#PostgreSQL#Linux#logrotate#Forensics
How logrotate copytruncate Deadlocked a 349 GB PostgreSQL Log for 386 Days

The Symptom

A production PostgreSQL node triggered critical disk-usage alerts. Inspecting the log directory revealed a single active PostgreSQL log file weighing 349 GB—unrotated for 386 consecutive days, right next to an unexplained 145 GB partial file.

At first glance, logrotate was installed, enabled via cron/systemd timer, and its configuration file looked completely normal.

Forensic Investigation

Instead of blindly deleting files (which on Linux does not even free disk blocks while PostgreSQL still holds the open file descriptor), I reconstructed the exact timeline using stat metadata and the logrotate status file:

stat /var/log/postgresql/postgresql.log
stat /var/log/postgresql/postgresql.log.1

Comparing atime, mtime, and ctime across the active log and the 145 GB remnant exposed a classic self-locking failure loop:

  1. The logrotate rule relied on copytruncate, which copies the entire active log file to a rotated copy before truncating the original in place.
  2. That means copytruncate requires free disk space ≥ the current log file size.
  3. Once free space dipped below the log size, the copy step aborted halfway—leaving the 145 GB partial artifact behind and consuming even more free space.
  4. Worse, logrotate still updated its state file timestamp as if the rotation cycle had been processed, guaranteeing that every subsequent run would fail the same way.

Zero-Downtime Remediation

  • Immediate relief without restarting PostgreSQL: safely truncated the active log descriptor after verifying no external ingestion job was tailing it, and removed the orphaned 145 GB partial artifact—reclaiming 349 GB with zero dropped connections.
  • Eliminated copytruncate: switched to PostgreSQL’s native logging collector (logging_collector = on with log_rotation_age and log_rotation_size) so PostgreSQL manages its own file descriptors cleanly without needing 2× disk space.
  • Guardrails: added disk-watermark and stale-rotation alerting so silent rotation failures surface within hours, not a year later.

Takeaways

  • Never rely on copytruncate for high-volume database logs; it creates a race window for lost lines and fails catastrophically when disk usage crosses 50%.
  • Always inspect inode timestamps (stat) and open descriptors (lsof) before touching massive production log files.

Enjoyed this note or seen a similar bug?

Feel free to drop a line—always glad to exchange ideas with fellow engineers.

Say hi · [email protected]