pg_stat_statements: SQL Statistics View Extension
Last updated: 2026-11-04
pg_stat_statements: SQL Statistics View Extension
pg_stat_statements accumulates, per normalized statement, the number of calls, total and mean execution time, rows returned, and shared block hits and dirty writes. It is the first place to look when hunting slow queries or wondering how often a statement actually runs. It only works when loaded through shared_preload_libraries — CREATE EXTENSION alone is not enough. The statistics live in shared memory and are wiped when the instance restarts.
| Kind | extension |
|---|---|
| Example Language | SQL |
📝 Syntax
-- postgresql.conf: shared_preload_libraries = 'pg_stat_statements', then restart the instance
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
SELECT queryid, calls, total_exec_time, rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;
Returns:CREATE EXTENSION returns success on completion. The extension provides the system view pg_stat_statements with columns such as queryid, user, dbname, calls, total_exec_time, mean_exec_time, rows and shared_blks_hit.
▶ Example
Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.
Output:
SHOW shared_preload_libraries
shared_preload_libraries
-------------------------
pg_stat_statements
(1 row)
SELECT calls,
round(total_exec_time::numeric, 3) AS total_ms,
rows,
rtrim(left(replace(query, chr(10), ' '), 46)) AS query_head
FROM pg_stat_statements
WHERE query LIKE '%t_pgss_demo%'
ORDER BY query
LIMIT 5
calls | total_ms | rows | query_head
-------+----------+------+--------------------------------------------------
1 | 0.443 | 0 | ANALYZE t_pgss_demo
1 | 0.853 | 0 | CREATE TABLE t_pgss_demo ( id int,
1 | 0.214 | 0 | DROP TABLE IF EXISTS t_pgss_demo
1 | 0.839 | 1000 | INSERT INTO t_pgss_demo SELECT g, repeat($1, $
(4 rows)
Note: total_ms changes on every run because it depends on current machine load; the $1 in query_head is pg_stat_statements' constant normalization — different parameter values of the same template are folded into one statistics entry.
📌 Key Points
- In browser PGlite it must be loaded on demand: import { pg_stat_statements } from '@electric-sql/pglite/contrib/pg_stat_statements', then new PGlite({ extensions: { pg_stat_statements } }). PGlite's contrib bundle already places it in shared_preload_libraries, which is why SHOW reports it here.
- Size: pg_stat_statements is about 11 KB, a lightweight extension in the same league as pg_trgm (~15 KB), btree_gist (~22 KB) and uuid-ossp (~17 KB); pgcrypto at ~1.1 MB is markedly larger.
- It must be preloaded via shared_preload_libraries and the instance restarted; otherwise querying pg_stat_statements fails with pg_stat_statements must be loaded via shared_preload_libraries.
- Statistics are aggregated by normalized statement text, with constants replaced by $1, $2 placeholders, so different parameter values of the same template collapse into a single entry — do not read such a row as several separate calls.
- The data lives in shared memory and is cleared on restart; snapshot it into a regular table if you want long-term trends.
- Timing columns such as total_exec_time depend heavily on machine load and differ on every run — ranking and relative multipliers matter more than absolute numbers.