buffercache_summary collector exposes summary statistics from the pg_buffercache extension, providing insights into PostgreSQL’s shared buffer cache usage.
Status
Default: Disabled Enable with:--collector.buffercache-summary
Prerequisites
Thepg_buffercache extension must be installed:
The
pg_buffercache_summary() function does not take locks, making it safe for production use.Metrics
pg_buffercache_summary_buffers_used
Type: GaugeDescription: Number of used shared buffers Labels: None
pg_buffercache_summary_buffers_unused
Type: GaugeDescription: Number of unused shared buffers Labels: None
pg_buffercache_summary_buffers_dirty
Type: GaugeDescription: Number of dirty shared buffers Labels: None Dirty buffers contain modified data that has not yet been written to disk.
pg_buffercache_summary_buffers_pinned
Type: GaugeDescription: Number of pinned shared buffers Labels: None Pinned buffers are currently in use by active queries and cannot be evicted.
pg_buffercache_summary_usagecount_avg
Type: GaugeDescription: Average usage count of used shared buffers Labels: None The usage count indicates how frequently a buffer has been accessed. Higher values mean the buffer is “hotter” and less likely to be evicted.
SQL Query
PostgreSQL Versions
Supported: PostgreSQL 16+Required Permissions
The monitoring user needs:pg_buffercacheextension installed- Execute permission on
pg_buffercache_summary()function - Typically requires
pg_monitorrole or superuser privileges
Example Output
Use Cases
Monitor Buffer Cache Utilization
Track Dirty Buffer Ratio
- Checkpointer or background writer can’t keep up
- Heavy write workload
- Need to tune
bgwriter_*or checkpoint parameters
Monitor Pinned Buffers
- Long-running queries holding buffers
- Insufficient buffer cache size
- Connection pooling issues
Average Buffer Usage Count
Tuning Recommendations
High Unused Buffers
If you have many unused buffers:- Consider reducing
shared_buffersto free memory for other uses - Or accept this as normal if workload doesn’t require all allocated buffers
High Dirty Buffer Ratio
If dirty buffers are consistently high (>20% of used buffers):- Increase
bgwriter_lru_maxpagesto write more buffers per round - Decrease
bgwriter_delayto write more frequently - Adjust checkpoint parameters (
checkpoint_timeout,max_wal_size) - Consider faster storage for better write throughput
Low Usage Count Average
If average usage count is consistently low (less than 2):- Buffer cache may be too small for working set
- Consider increasing
shared_buffers - Query patterns may not benefit from caching (sequential scans)
High Pinned Buffers
If pinned buffers are consistently high:- Investigate long-running queries
- Check for connection leaks
- Review transaction management
Performance Considerations
Thepg_buffercache_summary() function:
- Does not take locks on the buffer pool
- Is much faster than
pg_buffercache(which returns per-buffer details) - Safe to call frequently in production
- Returns a single row with aggregate statistics
pg_buffercache view.
Comparison with pg_buffercache
| Feature | pg_buffercache_summary() | pg_buffercache view |
|---|---|---|
| PostgreSQL version | 16+ | 8.1+ |
| Locks | No locks | No locks |
| Output | Single summary row | One row per buffer |
| Performance | Very fast | Can be slow (100k+ rows) |
| Detail level | Aggregate statistics | Per-buffer details |
| Best for | Monitoring, alerting | Deep analysis, debugging |
Alert Examples
Troubleshooting
Extension Not Available
If the collector fails with extension errors:Function Not Found
If you get “function pg_buffercache_summary() does not exist”:- Verify PostgreSQL version is 16 or later
- Check extension version:
SELECT extversion FROM pg_extension WHERE extname = 'pg_buffercache' - Update extension if needed:
ALTER EXTENSION pg_buffercache UPDATE
Permission Denied
Related
- pg_buffercache extension
- Shared buffer configuration
- stat-bgwriter collector - Background writer statistics
- stat-checkpointer collector - Checkpointer statistics