pg_stat_statements

track planning and execution statistics of all SQL statements executed

Overview

PackageVersionCategoryLicenseLanguage
pg_stat_statements1.11STATPostgreSQLC
IDExtensionBinLibLoadCreateTrustRelocSchema
6990pg_stat_statementsNoYesYesYesNoNo-
Relatedpg_qualstats pg_store_plans pg_track_settings pg_stat_monitor auto_explain pg_profile pg_show_plans pg_hint_plan
Depended Bypg_stat_kcache powa

Version

PG18PG17PG16PG15PG14
1.111.111.111.111.11

Install

Note: This is a built-in contrib extension of PostgreSQL

CREATE EXTENSION pg_stat_statements;

Usage

pg_stat_statements: track cumulative query execution statistics

pg_stat_statements tracks planning and execution statistics of all SQL statements executed by a server.

Querying Statistics

-- Top queries by total execution time
SELECT query, calls, total_exec_time, mean_exec_time, rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;

-- Top queries by shared buffer reads (I/O intensive)
SELECT query, calls, shared_blks_read, shared_blks_hit,
       shared_blk_read_time
FROM pg_stat_statements
ORDER BY shared_blks_read DESC
LIMIT 10;

-- Extension status (deallocations, last reset)
SELECT * FROM pg_stat_statements_info;

Key View Columns

ColumnTypeDescription
queryidbigintHash identifying normalized queries
querytextRepresentative query text
callsbigintExecution count
total_exec_timedouble precisionTotal execution time (ms)
mean_exec_timedouble precisionMean execution time (ms)
rowsbigintTotal rows retrieved/affected
shared_blks_hitbigintShared buffer cache hits
shared_blks_readbigintShared blocks read from disk
shared_blk_read_timedouble precisionTime reading shared blocks (ms)
wal_recordsbigintWAL records generated
wal_bytesnumericTotal WAL generated (bytes)
plansbigintTimes planned
total_plan_timedouble precisionTotal planning time (ms)

Functions

-- Reset all statistics
SELECT pg_stat_statements_reset();

-- Reset for a specific query
SELECT pg_stat_statements_reset(0, 0, queryid)
FROM pg_stat_statements
WHERE query LIKE '%my_table%';

-- Reset only min/max values
SELECT pg_stat_statements_reset(0, 0, 0, true);

-- Query without text (less I/O)
SELECT * FROM pg_stat_statements(showtext := false);

Configuration

ParameterDefaultDescription
pg_stat_statements.max5000Maximum tracked statements (server start only)
pg_stat_statements.tracktoptop, all (nested), or none
pg_stat_statements.track_utilityonTrack utility commands
pg_stat_statements.track_planningoffTrack planning statistics
pg_stat_statements.saveonPersist across server restarts

Requires shared_preload_libraries = 'pg_stat_statements' and compute_query_id = on.


Last Modified 2026-03-12: add pg extension catalog (95749bf)