Repeated PostgreSQL statements can consume most of a server's execution time while ordinary database metrics still look acceptable. Enabling pg_stat_statements records normalized query patterns inside the server, so slow or high-frequency SQL can be ranked from the database instead of reconstructed from application logs.
The pg_stat_statements module loads at server start and keeps statement counters in shared memory across the whole instance. The database-level CREATE EXTENSION command exposes those counters through the pg_stat_statements and pg_stat_statements_info views in each database that needs to query them.
Changing shared_preload_libraries requires a full restart, which drops active sessions. Current PostgreSQL releases also need compute_query_id set to auto or on, and query text can expose application details, so restrict access to superusers or controlled monitoring roles such as pg_read_all_stats.
Related: How to analyze slow queries in PostgreSQL \\
Related: How to monitor PostgreSQL performance metrics
$ sudo -u postgres psql -X -At -c "SHOW config_file;" /etc/postgresql/18/main/postgresql.conf
$ sudo cp /etc/postgresql/18/main/postgresql.conf /etc/postgresql/18/main/postgresql.conf.before-pg-stat-statements
Replace the path with the value returned by SHOW config_file.
$ sudoedit /etc/postgresql/18/main/postgresql.conf
shared_preload_libraries = 'pg_stat_statements' compute_query_id = auto
Preserve existing preload entries by appending pg_stat_statements to the existing comma-separated list inside the same quoted value.
pg_stat_statements.max = 10000 pg_stat_statements.track = top
pg_stat_statements.max defaults to 5000 in current PostgreSQL releases, and raising it increases shared memory use. pg_stat_statements.track defaults to top; use all only when nested statements inside functions need to be counted.
$ sudo systemctl restart postgresql
A server restart disconnects existing sessions and can abort in-flight transactions.
When postgresql is not the unit name, identify the correct service with systemctl list-units 'postgresql*'.
$ sudo -u postgres psql -X -At -c "SHOW shared_preload_libraries;" pg_stat_statements
$ sudo -u postgres psql -X -At -c "SHOW compute_query_id;" auto
pg_stat_statements needs query IDs to group matching statements. Use on instead of auto only when local policy requires explicit query ID calculation.
$ sudo -u postgres psql -X -d postgres -c "CREATE EXTENSION IF NOT EXISTS pg_stat_statements;" CREATE EXTENSION
Run CREATE EXTENSION in each database that needs to query the pg_stat_statements view.
An error about a missing extension control file typically indicates the PostgreSQL contrib/extension files are not installed for the server version.
$ sudo -u postgres psql -X -d postgres -c "SELECT pg_stat_statements_reset();" pg_stat_statements_reset ------------------------------- 2026-06-07 05:37:01.795373+00 (1 row)
The no-argument reset clears collected pg_stat_statements counters for the whole PostgreSQL instance.
$ sudo -u postgres psql -X -d postgres -c "SELECT now();"
now
-------------------------------
2026-06-07 05:37:01.811228+00
(1 row)
$ sudo -u postgres psql -X -d postgres -c "SELECT calls, query FROM pg_stat_statements ORDER BY calls DESC LIMIT 5;"
calls | query
-------+-----------------------------------
2 | SELECT now()
1 | SELECT pg_stat_statements_reset()
(2 rows)
The recorded query text is normalized for grouping, so literals are commonly replaced with placeholders. The example shows SELECT now() after it was run twice during validation.