$linuxjunkies
>

pg_stat_statements

also: pg_stat_statements extension

A PostgreSQL extension that tracks execution statistics for all SQL statements executed on a database server, including query count, execution time, and resource usage.

pg_stat_statements is a PostgreSQL contrib module that must be explicitly loaded into a database to enable query performance monitoring. It records metrics such as the number of times a query was executed, total and average execution time, rows returned, and I/O statistics for every unique SQL statement.

To use it, you add pg_stat_statements to the shared_preload_libraries parameter in postgresql.conf, restart PostgreSQL, and then create the extension with CREATE EXTENSION pg_stat_statements;

Example query to find slow statements:

SELECT query, calls, mean_exec_time FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 10;
This helps database administrators identify performance bottlenecks and optimize slow queries.

Related terms