wal collector monitors Write-Ahead Log (WAL) directory statistics, tracking the number and size of WAL segment files.
Status
Default: EnabledMetrics
pg_wal_segments
Type: GaugeDescription: Number of WAL segment files in the WAL directory Labels: None
pg_wal_size_bytes
Type: GaugeDescription: Total size of all WAL segment files in bytes Labels: None
SQL Query
How It Works
The collector:- Lists all files in the WAL directory using
pg_ls_waldir() - Filters for valid WAL segment files using pattern
^[0-9A-F]{24}$ - Counts the segments and sums their sizes
WAL Segment Naming
WAL segment files follow a specific naming pattern:- Format:
000000010000000000000001(24 hexadecimal characters) - Structure:
timelineId(8 chars) +logFileId(8 chars) +segmentId(8 chars)
- Archive status files (
.ready,.done) - Temporary files
- Other metadata files
PostgreSQL Versions
Supported: PostgreSQL 10+ Thepg_ls_waldir() function was introduced in PostgreSQL 10.
Pre-PostgreSQL 10: WAL was called “xlog” - use the xlog_location collector instead.
Required Permissions
The monitoring user needs:- Execute permission on
pg_ls_waldir()function - Typically requires
pg_monitorrole or superuser:
Example Output
- 15 WAL segment files present
- Total size: ~240 MB (15 segments × 16 MB each)
Use Cases
Monitor WAL Growth
Detect WAL Buildup
Estimate Write Load
Since each segment is typically 16MB:Normal WAL Retention
Typical WAL retention is controlled by:min_wal_size(default: 80MB = ~5 segments)max_wal_size(default: 1GB = ~64 segments)- Replication slots (can retain WAL indefinitely)
- Archive mode settings
Alert Examples
Troubleshooting
Excessive WAL Segments
Ifpg_wal_segments is unusually high:
-
Check replication slots:
-
Check archive status:
-
Verify configuration:
-
Check for inactive replication slots:
WAL Not Being Removed
Common causes:- Inactive replication slot holding WAL
- Archive command failing
wal_keep_size/wal_keep_segmentsset too high- Long-running transaction
Permission Errors
If the collector fails with permission errors:WAL Segment Size
Default segment size is 16MB, but can be configured at compile time:Related Metrics
Combine with other collectors for complete WAL monitoring:Related
- pg_ls_waldir function](https://www.postgresql.org/docs/current/functions-admin.html#FUNCTIONS-ADMIN-GENFILE)
- WAL configuration
- replication-slot collector
- xlog-location collector (PostgreSQL less than 10)