Overview
The PostgreSQL stats example collects metrics from variouspg_stat_* system views to monitor:
- Database activity (connections, transactions, queries)
- Table statistics (scans, tuples, vacuum operations)
- Index usage and performance
- Cache hit ratios
- I/O statistics
- Process states
Configuration
Database Connection
PG_DATABASE_DSN environment variable with your PostgreSQL connection string:
Complete Example
View complete config.yaml
View complete config.yaml
Key Queries Explained
Process Statistics
Monitors the state of PostgreSQL backend processes:Database Statistics
Collects key database-level metrics frompg_stat_database:
- numbackends: Number of active connections per database
- xact_commit/rollback: Transaction counts (counters)
- blks_read/hit: Disk I/O vs cache hits
Cache Hit Ratio
Calculates the percentage of queries served from cache:Metrics Types
- Counters: Monotonically increasing values (transactions, blocks read). Use
rate()in Prometheus to calculate per-second rates. - Gauges: Point-in-time values (connection counts, cache hit ratio)
Running the Example
- Save the configuration as
config.yaml - Set your database connection:
- Start Query Exporter:
- Access metrics at
http://localhost:9560/metrics
Full Example Source
The complete PostgreSQL monitoring example with all metrics and queries is available in the Query Exporter repository. This example includes additional queries for:- Table statistics (vacuum, analyze operations)
- Index usage and efficiency
- I/O statistics per table
- Sequence statistics