pg_stat_statements Query Builder
pg_stat_statements is the fastest way to find out what a PostgreSQL server actually spends its time on, but the column names have moved twice: PostgreSQL 13 renamed total_time to total_exec_time when it split planning from execution, and PostgreSQL 17 renamed blk_read_time to shared_blk_read_time. That is why the query you copied from a blog post errors with "column does not exist". Pick your major version and what you are hunting for, and this builder writes the Top-N query with the right column names, the percentage-of-total column, a cache hit ratio, the filters that keep BEGIN and COMMIT out of the results, plus the extension setup and the reset commands. It all runs in your browser.
Do more than pg_stat_statements query builder — meet Chat2DB
Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace.
How to use
- Select your PostgreSQL major version so the generated query uses the column names your server actually has.
- Choose what you are looking for - total time for load, mean time for a slow statement, temp blocks for queries spilling to disk - and set the row limit and minimum call count.
- Run the setup block once if the extension is not installed yet, then run the generated query and EXPLAIN the statements it puts at the top.
Frequently asked questions
Why does my pg_stat_statements query fail with "column total_time does not exist"?
Because you are on PostgreSQL 13 or newer and the query was written for 12 or older. Version 13 separated planning time from execution time, so total_time became total_exec_time, mean_time became mean_exec_time, min_time and max_time and stddev_time gained the same _exec_ infix, and new total_plan_time and mean_plan_time columns appeared. Version 17 made a second rename in the IO columns: blk_read_time and blk_write_time became shared_blk_read_time and shared_blk_write_time, with separate local_blk_ and temp_blk_ variants alongside them. Neither change is backward compatible, which is why monitoring scripts and blog snippets break silently across upgrades. Select your major version above and the generated SQL uses the correct spelling.
How do I enable pg_stat_statements?
It ships with PostgreSQL as a contrib module but is not active by default, because it needs shared memory allocated at startup. Add pg_stat_statements to shared_preload_libraries in postgresql.conf and restart the server - a reload is not enough for this parameter. Then run CREATE EXTENSION pg_stat_statements once in each database where you want to query the view. Set track_io_timing = on as well if you want the read and write time columns to hold anything other than zero. On managed services the steps differ slightly: on Amazon RDS and Aurora you add it to shared_preload_libraries in the parameter group and reboot, on Azure Database for PostgreSQL and Cloud SQL it is a server flag, and on several platforms the extension is already enabled. Non-superusers need membership in pg_read_all_stats to see statements from other users; without it they only see their own.
Should I sort by total_exec_time or mean_exec_time?
Sort by total_exec_time when you want to reduce load, and by mean_exec_time when you are chasing a specific slow statement a user complained about. The distinction matters more than it sounds: a 4 ms query executed two million times an hour consumes far more of the server than a 3-second report that runs twice a day, but a mean-time sort puts the report on top and buries the query that is actually saturating your CPU. A good habit is to read the total-time list first, then look at the max and stddev columns on those rows. A statement whose mean is 5 ms but whose max is 9 seconds is not a slow query - it is a query with an unstable plan or a locking problem, and the fix is different. Once you have the statement text, EXPLAIN (ANALYZE, BUFFERS) tells you why. Chat2DB can run these monitoring queries and the EXPLAIN side by side against the same connection: download it at https://chat2db.ai/download or use the web version at https://app.chat2db.ai.
