Skip to main content
View rawEdit

pg_stat_statements metrics

Query-level performance metrics from the pg_stat_statements extension.

Prerequisites​

The pg_stat_statements extension must be installed and configured:

-- Check if installed
select * from pg_extension where extname = 'pg_stat_statements';

-- Install if missing
create extension if not exists pg_stat_statements;

Required postgresql.conf settings:

shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.track = 'all'
pg_stat_statements.max = 10000

Core metrics​

Execution metrics​

MetricTypeDescription
pg_stat_statements_calls_totalCounterTotal number of query executions
pg_stat_statements_total_exec_time_secondsCounterTotal execution time
pg_stat_statements_mean_exec_time_secondsGaugeAverage execution time per call
pg_stat_statements_rows_totalCounterTotal rows returned or affected

Planning metrics​

MetricTypeDescription
pg_stat_statements_total_plan_time_secondsCounterTotal planning time
pg_stat_statements_mean_plan_time_secondsGaugeAverage planning time per call

Buffer metrics​

MetricTypeDescription
pg_stat_statements_shared_blks_hit_totalCounterShared buffer hits
pg_stat_statements_shared_blks_read_totalCounterShared blocks read from disk
pg_stat_statements_shared_blks_written_totalCounterShared blocks written
pg_stat_statements_shared_blks_dirtied_totalCounterShared blocks dirtied

I/O timing metrics​

Available when track_io_timing = on:

MetricTypeDescription
pg_stat_statements_blk_read_time_secondsCounterTime spent reading blocks
pg_stat_statements_blk_write_time_secondsCounterTime spent writing blocks

WAL metrics​

PostgreSQL 13+:

MetricTypeDescription
pg_stat_statements_wal_records_totalCounterWAL records generated
pg_stat_statements_wal_bytes_totalCounterWAL bytes generated

Labels​

All pg_stat_statements metrics include these labels:

LabelDescriptionExample
queryidUnique query identifier-4021163671685...
datnameDatabase namemyapp
usenameUser nameapp_user
cluster_nameCluster identifierproduction
node_nameNode identifierprimary

Common queries​

Top queries by total time​

topk(10,
sum by (queryid, datname) (
rate(pg_stat_statements_total_exec_time_seconds[5m])
)
)

Query call rate​

sum by (queryid) (
rate(pg_stat_statements_calls_total[5m])
)

Average query latency​

pg_stat_statements_mean_exec_time_seconds

Buffer hit ratio per query​

sum by (queryid) (rate(pg_stat_statements_shared_blks_hit_total[5m]))
/
(
sum by (queryid) (rate(pg_stat_statements_shared_blks_hit_total[5m]))
+
sum by (queryid) (rate(pg_stat_statements_shared_blks_read_total[5m]))
)

Queries with high planning time ratio​

pg_stat_statements_mean_plan_time_seconds
/
(pg_stat_statements_mean_plan_time_seconds + pg_stat_statements_mean_exec_time_seconds)
> 0.1

Dashboard usage​

These metrics are used in:

Troubleshooting​

Metrics show zero or missing​

  1. Verify extension is loaded:

    select * from pg_stat_statements limit 1;
  2. Check pg_stat_statements.track setting:

    show pg_stat_statements.track;
  3. Ensure monitoring user has access:

    grant pg_read_all_stats to postgres_ai_mon;

queryid changes after PostgreSQL upgrade​

The queryid hash algorithm may change between major PostgreSQL versions. This is expected behavior — historical data with old queryids will appear as different queries.

High memory usage from pg_stat_statements​

Reduce pg_stat_statements.max if memory is constrained:

-- Check current usage
select count(*) from pg_stat_statements;

-- Reduce max entries (requires restart)
-- postgresql.conf: pg_stat_statements.max = 5000