pg_stat_database system view, providing detailed insights into database activity, transactions, block I/O, and more.
This collector is enabled by default.
What It Monitors
The database statistics collector tracks:- Active database connections
- Transaction commits and rollbacks
- Block reads and cache hits
- Tuple operations (rows returned, fetched, inserted, updated, deleted)
- Temporary file usage
- Deadlock counts
- Block I/O timing
- Query active time (PostgreSQL 14+)
Metrics Exposed
Connection Metrics
pg_stat_database_numbackends
Number of backends currently connected to the database. Type: GaugeLabels:
datid, datname
Transaction Metrics
pg_stat_database_xact_commit
Number of transactions committed in this database. Type: CounterLabels:
datid, datname
pg_stat_database_xact_rollback
Number of transactions rolled back in this database. Type: CounterLabels:
datid, datname
Block I/O Metrics
pg_stat_database_blks_read
Number of disk blocks read from disk. Type: CounterLabels:
datid, datname
pg_stat_database_blks_hit
Number of disk blocks found in the buffer cache (cache hits). Type: CounterLabels:
datid, datname
pg_stat_database_blk_read_time
Time spent reading data file blocks in milliseconds. Type: CounterLabels:
datid, datname
Requires
track_io_timing to be enabled in PostgreSQL configuration.pg_stat_database_blk_write_time
Time spent writing data file blocks in milliseconds. Type: CounterLabels:
datid, datname
Tuple (Row) Metrics
pg_stat_database_tup_returned
Number of rows returned by queries in this database. Type: CounterLabels:
datid, datname
pg_stat_database_tup_fetched
Number of rows fetched by queries in this database. Type: CounterLabels:
datid, datname
pg_stat_database_tup_inserted
Number of rows inserted by queries in this database. Type: CounterLabels:
datid, datname
pg_stat_database_tup_updated
Number of rows updated by queries in this database. Type: CounterLabels:
datid, datname
pg_stat_database_tup_deleted
Number of rows deleted by queries in this database. Type: CounterLabels:
datid, datname
Conflict and Error Metrics
pg_stat_database_conflicts
Number of queries canceled due to conflicts with recovery (standby servers only). Type: CounterLabels:
datid, datname
pg_stat_database_deadlocks
Number of deadlocks detected in this database. Type: CounterLabels:
datid, datname
Temporary File Metrics
pg_stat_database_temp_files
Number of temporary files created by queries in this database. Type: CounterLabels:
datid, datname
pg_stat_database_temp_bytes
Total amount of data written to temporary files in bytes. Type: CounterLabels:
datid, datname
Statistics Reset Metric
pg_stat_database_stats_reset
Timestamp when these statistics were last reset. Type: CounterLabels:
datid, datname
Active Time Metric (PostgreSQL 14+)
pg_stat_database_active_time_seconds_total
Time spent executing SQL statements in this database, in seconds. Type: CounterLabels:
datid, datname
This metric is only available on PostgreSQL 14 and later.
SQL Query Used
The collector dynamically builds a query based on PostgreSQL version:Configuration
Enable/Disable the Collector
Use Cases
Calculate Cache Hit Ratio
Measure how effectively PostgreSQL uses its buffer cache:Monitor Transaction Rate
Track database transaction throughput:Detect Deadlock Issues
Alert on deadlock occurrences:Monitor Temporary File Usage
Track queries that spill to disk:- Insufficient
work_memconfiguration - Complex queries requiring disk sorts
- Need for query optimization
Identify Read/Write Heavy Databases
Find databases with high I/O:Permissions Required
The database statistics collector requires:CONNECTprivilege on the database- Read access to the
pg_stat_databaseview - For PostgreSQL 10+:
pg_monitororpg_read_all_statsrole - For PostgreSQL <10: Read access to statistics views
Performance Considerations
The database statistics collector:- Queries a single system view (
pg_stat_database) - Has minimal performance impact
- Statistics are maintained in-memory by PostgreSQL
- Does not lock tables or interfere with operations
- Safe to query frequently
Related Collectors
- User Tables Statistics - Table-level statistics
- Database - Database size and connection limits
Important Notes
All counter metrics return accumulated values since the last statistics reset. To get rates, use Prometheus rate functions.