Sub topic 12
Tuning pg_stat_statements settings
This chapter assumes you know basic SQL and can connect to PostgreSQL with psql or another SQL client. You should also understand that PostgreSQL configuration changes may require a reload or a server restart.
pg_stat_statements collects cumulative statistics for normalized SQL statements. Its settings control which statements are tracked, how many statement entries can exist, whether planning time is measured, and whether statistics survive a server restart. These choices affect both the usefulness and the overhead of monitoring.
Settings that control collection
The settings are PostgreSQL configuration parameters. Set them in postgresql.conf, with an administration command, or through the service that manages your PostgreSQL instance.
| Setting | Purpose | Typical consideration |
|---|---|---|
| `pg_stat_statements.max` | Maximum number of statements stored | Increase it when entries are frequently evicted |
| `pg_stat_statements.track` | Tracks statements from top-level sessions, all nested statements, or none | `top` is usually a good starting point |
| `pg_stat_statements.track_utility` | Includes utility commands such as `CREATE TABLE` | Enable when those commands matter |
| `pg_stat_statements.track_planning` | Records planning time as well as execution time | Useful for planning-heavy workloads, with extra overhead |
| `pg_stat_statements.save` | Saves statistics across a clean server shutdown | Disable when each server start should begin with empty statistics |
`pg_stat_statements.max`
pg_stat_statements.max sets the maximum number of distinct statement entries held in shared memory. A normalized statement usually occupies one entry, but different users, databases, or query identifiers can create separate entries.
A value that is too small causes old entries to be evicted as new statements appear. Eviction makes rankings less complete and can hide infrequent queries. A larger value consumes more shared memory, so increase it only when the workload needs it.
# postgresql.conf
pg_stat_statements.max = 10000This setting requires a server restart. The exact default and allowed range depend on the PostgreSQL version.
Tracking scope
pg_stat_statements.track accepts top, all, or none.
toprecords statements issued directly by client sessions.allalso records nested statements executed inside functions or procedures.nonedisables statement tracking.
Use top when you want a simple view of application traffic. Use all when database functions contain important SQL that must be optimized separately. With all, a single client request may contribute statistics for both the outer call and its nested statements.
pg_stat_statements.track_utility controls utility commands that are not ordinary DML statements. Examples include CREATE, ALTER, and VACUUM. Tracking them can help diagnose administrative work, but it can add noise when your goal is application query performance.
Planning statistics and persistence
By default, the extension focuses on execution statistics. Set pg_stat_statements.track_planning to on when planning time is important, such as when queries are prepared frequently or use complex plans.
pg_stat_statements.track_planning = on
pg_stat_statements.track_utility = on
pg_stat_statements.save = onPlanning tracking adds work to statement processing. Compare the benefit with the monitoring cost, especially on high-throughput systems.
With pg_stat_statements.save = on, PostgreSQL saves statistics during a clean shutdown and restores them at startup. This preserves long-term totals, but it can make a new measurement window harder to interpret. You can reset the statistics before a controlled test instead.
Choosing settings deliberately
Use this decision path:
Do not increase every setting automatically. First identify the question you need to answer. Track nested statements or planning only when that information changes your investigation.
Exercise
A database has many statements and older entries disappear before you can review them. Which setting should you examine first, and what trade-off follows from increasing it?
Show answer
Examine pgstatstatements.max. Increasing it can reduce entry eviction, but it uses more shared memory and requires a server restart.