pgquery

pgquery / pg_stat_statements viewer

Chapter 10 · Query performance

Find the queries that cost you the most

pg_stat_statements records every normalized query the server runs. pgquery turns it into a sortable table so the expensive ones rise to the top.

Turn it on

The extension ships with PostgreSQL but needs to be preloaded:

# postgresql.conf
shared_preload_libraries = 'pg_stat_statements'

Restart the server, then run CREATE EXTENSION pg_stat_statements;. Most managed services (RDS, Cloud SQL, Supabase, Neon) have it available already.

Read it

  • Total time finds what costs the server the most overall.
  • Mean time finds the individually slow queries users wait on.
  • Calls finds chatty code: N+1 loops and missing caching.

Copy a query into the SQL editor and press Explain to see why it is slow.

And the rest of the picture

The Performance Advisor adds cache hit ratio, unused indexes, table bloat, sequential scan hot spots and connection use.

Questions

Which role can read pg_stat_statements?

Superusers and members of pg_read_all_stats see all queries; other roles see only their own.

Does it slow the server down?

The overhead is small and it is widely enabled in production.

More chapters