Sobes.tech
Senior

What can be seen in pg_stat_statements? What is stored there regarding queries?

sobes.tech AI

Answer from AI

pg_stat_statements is an extension for PostgreSQL that collects statistics on executed SQL queries.

This system table stores aggregated data for each unique query (normalized, with parameters replaced), including:

  • Number of calls (calls)
  • Total execution time (total_time)
  • Average execution time (mean_time)
  • Minimum and maximum execution times
  • Number of locks
  • Number of returned rows
  • Number of errors
  • Execution plan (in some versions)

This allows analyzing query performance, identifying "heavy" or frequently called queries, and optimizing them.

Example query to view the slowest queries:

SELECT query, calls, total_time, mean_time
FROM pg_stat_statements
ORDER BY total_time DESC
LIMIT 10;

Thus, pg_stat_statements helps DBAs and developers monitor and optimize database load.

What can be seen in pg_stat_statements? What is… - sobes.tech