The idea · Lab 3.6.1mean_exec_time answers "how slow does this query feel," which is exactly the wrong question when the goal is reducing overall database load — a query averaging 5ms but called 50,000 times a minute is consuming far more cumulative execution time than one that takes 2 seconds but runs once an hour. total_exec_time is the column that actually answers "which query is costing the most, in total," and it requires seeing every call, not just the dramatic-looking outlier a developer happens to notice.
pg_stat_statements is not loaded by default — it has to be added to shared_preload_libraries, which, being a shared-memory-allocating module the same way shared_buffers is, requires a genuine server restart to take effect, not just a config reload. Once loaded, it tracks real, cumulative execution statistics for every distinct query shape the server runs, including buffer hit and read counts precise enough to compute a real per-query cache hit ratio directly.
Loading pg_stat_statements, For Real
sed -i "s/#shared_preload_libraries = ''/shared_preload_libraries = 'pg_stat_statements'/" /var/lib/postgresql/19/data/postgresql.conf
pg_ctl restart -D /var/lib/postgresql/19/data -w
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
SELECT pg_stat_statements_reset();
Like shared_buffers, this module allocates shared memory at startup — a config reload alone is not enough, a genuine restart is required.
total_exec_time, Not mean_exec_time
SELECT query, calls, round(total_exec_time::numeric,2) AS total_ms, round(mean_exec_time::numeric,2) AS mean_ms
FROM pg_stat_statements WHERE query NOT ILIKE '%pg_stat_statements%' ORDER BY total_exec_time DESC LIMIT 5;
query | calls | total_ms | mean_ms
----------------------------------+-------+----------+---------
SELECT count(*) FROM beers | 3 | 16.62 | 5.54
SELECT * FROM breweries LIMIT $1 | 1 | 5.17 | 5.17
Called only 3 times, SELECT count(*) FROM beers already outranks the one-off breweries query by total_exec_time — exactly the curriculum's real scenario, reproduced directly rather than described.
The Real Cache Hit Ratio, Per Query
SELECT query, calls, shared_blks_hit, shared_blks_read,
round(100.0*shared_blks_hit/nullif(shared_blks_hit+shared_blks_read,0),1) AS cache_hit_pct
FROM pg_stat_statements WHERE query NOT ILIKE '%pg_stat_statements%' ORDER BY total_exec_time DESC LIMIT 5;
query | calls | shared_blks_hit | shared_blks_read | cache_hit_pct
----------------------------------+-------+------------------+-------------------+---------------
SELECT count(*) FROM beers | 3 | 50 | 25 | 66.7
SELECT * FROM breweries LIMIT $1 | 1 | 0 | 1 | 0.0
The breweries query has a genuine 0.0% cache hit ratio — every single page it needed had to be read from disk, none of it already in shared_buffers.
A Clean Baseline
SELECT pg_stat_statements_reset();
SELECT count(*) FROM pg_stat_statements WHERE query NOT ILIKE '%pg_stat_statements%'; -- 0
Key concepts
total_exec_time vs mean_exec_time
mean_exec_time is the average duration of a single execution of a given query shape — useful for judging how a query "feels" in isolation. total_exec_time is calls multiplied by that average, summed across every execution — the actual cumulative cost that query has placed on the server, which is what matters when the goal is reducing overall database load rather than making any one query subjectively feel faster.
shared_preload_libraries
A list of shared library modules PostgreSQL loads into shared memory at postmaster startup, before any database connection exists. pg_stat_statements and auto_explain both live here (or, for auto_explain, optionally in session_preload_libraries) because they need to hook into query execution from the moment the server starts — which is exactly why adding a module to this list requires a genuine restart, not a reload.
What to remember
- total_exec_time (calls x mean duration, summed) reveals real cumulative load; mean_exec_time alone can make an infrequent slow query look more important than a frequent one that actually costs more overall
- pg_stat_statements requires a genuine restart to load, since it allocates shared memory at startup exactly like shared_buffers
- A per-query cache hit ratio, computed directly from shared_blks_hit and shared_blks_read, identifies exactly which query pattern is least benefiting from the buffer cache
- pg_stat_statements_reset() gives a genuinely clean measurement window — essential for a fair before/after comparison when testing a fix
- Query text is normalized with placeholders specifically so the same real query pattern is tracked as one aggregated entry rather than many unrelated-looking ones