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.

Kindextension
Example LanguageSQL

📝 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

More case studies coming soon.

📚 Related Reference

❓ FAQ

QWhy does querying pg_stat_statements complain about shared_preload_libraries?
AThe extension collects data in shared memory, so the shared library has to be loaded at server startup through shared_preload_libraries — CREATE EXTENSION only creates the view. After editing postgresql.conf (or running ALTER SYSTEM SET) you must restart the instance for it to take effect.
Qpg_stat_statements or auto_explain?
Aauto_explain logs execution plans for slow statements and answers "why is this statement slow". pg_stat_statements ranks every statement by calls and time and answers "which statement is the suspect". The usual workflow is to narrow the field with pg_stat_statements first, then drill into individual statements with auto_explain or EXPLAIN.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏