Sub topic 2
Enabling pg_stat_statements
This chapter assumes you know basic SQL, can connect with psql or another SQL client, and understand that a query plan describes how PostgreSQL runs a query. You also need access to PostgreSQL configuration, or a database administrator who can change it.
pg_stat_statements is a PostgreSQL extension that records cumulative statistics for executed SQL statements. The extension must be loaded when the PostgreSQL server starts. Creating the extension in a database is a separate step.
How enabling works
Enabling the extension has two stages:
- Add
pg_stat_statementstoshared_preload_libraries. - Restart PostgreSQL, then create the extension in each database where you want to use it.
The first stage loads the monitoring code into the server. The second stage creates the SQL view and helper functions that let you read the collected data.
Configure the server
Edit postgresql.conf, or use the configuration interface provided by your hosting service.
# postgresql.conf
# Changing this setting requires a server restart.
shared_preload_libraries = 'pg_stat_statements'If other libraries are already listed, keep them and add this extension as a comma-separated value:
shared_preload_libraries = 'pg_stat_statements,auto_explain'Do not replace an existing list without checking it first. Removing another preloaded library can change server behavior after the restart.
The setting applies to the PostgreSQL server, not just one database. A server restart is required because PostgreSQL loads shared libraries during startup. A reload is not enough.
On a self-managed server, the restart command depends on the installation method. For example, a systemd-managed PostgreSQL service might use:
sudo systemctl restart postgresqlUse the restart procedure documented for your operating system and PostgreSQL installation. Managed database services often provide a parameter group or server-parameter page instead of direct access to postgresql.conf.
Create the extension in a database
After the server restarts, connect to the database whose queries you want to monitor:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;The extension is installed per database. Loading the library makes the feature available to the server, but it does not automatically create the view in every database.
You can verify the server setting and extension separately:
SHOW shared_preload_libraries;
SELECT extname, extversion
FROM pg_extension
WHERE extname = 'pg_stat_statements';The first query confirms that the library was loaded in the current server process. The second confirms that the extension exists in the current database.
| Check | What it confirms |
|---|---|
| `SHOW shared_preload_libraries` | The server loaded the library |
| `pg_extension` query | The current database has the extension |
| `SELECT * FROM pg_stat_statements LIMIT 1` | The statistics view is accessible |
Creating an extension usually requires database-owner or elevated privileges. If you cannot change server settings or create extensions, ask an administrator to perform those steps. Query visibility can also depend on your role and PostgreSQL's statistics permissions.
Exercise
You changed sharedpreloadlibraries and ran CREATE EXTENSION, but PostgreSQL reports that pgstatstatements is not available. What should you check first?
Show answer
Check that the server was restarted after changing sharedpreloadlibraries, then run SHOW sharedpreloadlibraries and confirm that pgstatstatements appears in the result.