stat_statements collector exposes query performance statistics from the pg_stat_statements extension.
Status
Default: Disabled Enable with:--collector.stat-statements
Prerequisites
Thepg_stat_statements extension must be installed and configured:
postgresql.conf:
Metrics
pg_stat_statements_calls_total
Type: CounterDescription: Number of times the statement has been executed Labels:
user- Database userdatname- Database namequeryid- Query identifier hash
pg_stat_statements_seconds_total
Type: CounterDescription: Total time spent executing the statement, in seconds Labels:
user, datname, queryid
pg_stat_statements_rows_total
Type: CounterDescription: Total number of rows retrieved or affected by the statement Labels:
user, datname, queryid
pg_stat_statements_block_read_seconds_total
Type: CounterDescription: Total time spent reading blocks, in seconds Labels:
user, datname, queryid
Note: Requires track_io_timing = on
pg_stat_statements_block_write_seconds_total
Type: CounterDescription: Total time spent writing blocks, in seconds Labels:
user, datname, queryid
Note: Requires track_io_timing = on
pg_stat_statements_query_id
Type: CounterDescription: Mapping of queryid to query text (when
--collector.stat-statements.include_query is enabled)
Labels:
queryid- Query identifierquery- Query text (truncated)
Configuration Flags
—collector.stat-statements.include_query
Default:falseDescription: Include actual query text in metrics When enabled, exports the
pg_stat_statements_query_id metric with query text.
—collector.stat-statements.query_length
Default:120Description: Maximum length of query text to include Query text is truncated to this length to limit label cardinality.
—collector.stat-statements.limit
Default:100Description: Maximum number of statements to return Limits the number of queries exported to control cardinality.
—collector.stat-statements.exclude_databases
Default: “ (none)Description: Comma-separated list of databases to exclude Example:
--collector.stat-statements.exclude_databases="postgres,template0"
—collector.stat-statements.exclude_users
Default: “ (none)Description: Comma-separated list of users to exclude Example:
--collector.stat-statements.exclude_users="postgres,monitoring"
SQL Queries
The collector uses different queries based on PostgreSQL version. PostgreSQL 17+:Filtering Strategy
The collector only exports queries in the top 90th percentile by execution time:PostgreSQL Versions
Supported: PostgreSQL 9.4+ (withpg_stat_statements extension)
Version-specific changes:
- PostgreSQL 13+: Uses
total_exec_timeinstead oftotal_time - PostgreSQL 17+: Uses
shared_blk_read_timeandshared_blk_write_time
Required Permissions
The monitoring user needs:pg_stat_statementsextension installed- Execute permission on
pg_stat_statements()function - Access to
pg_databasecatalog
Example Output
Use Cases
Identify Slow Queries
Query Call Frequency
I/O-Heavy Queries
Rows per Query
Performance Considerations
Cardinality Impact
Each unique query creates separate time series for each metric. On a busy server with many unique queries, this can create millions of time series. Mitigation strategies:- Use
--collector.stat-statements.limitto cap exported queries - Use
--collector.stat-statements.exclude_databasesfor system databases - Only enable on staging/development initially
- The collector already filters to top 90th percentile
Query Text Storage
When using--collector.stat-statements.include_query:
- Query text is stored as a label
- Increases metric size significantly
- Use
--collector.stat-statements.query_lengthto limit size - Consider if you really need query text (queryid may be sufficient)