PostgreSQL for SQL Server DBAs – What is an alternative to sys.dm_exec_query_stats in the PostgreSQL world?

PostgreSQL for SQL Server DBAs

The sys.dm_exec_query_stats Dynamic Management View (DMV) in Microsoft SQL Server provides performance statistics for cached query plans. It allows database administrators and developers to monitor and analyze SQL query performance, helping them identify inefficiencies and optimize queries. By tracking execution frequency and resource consumption, it becomes easier to prioritize tuning efforts and improve overall system performance.

This DMV plays a crucial role in diagnosing performance issues and optimizing query execution strategies. With detailed insights into execution times, resource usage, and execution counts, database teams can pinpoint high-impact queries. Leveraging this information empowers them to make data-driven decisions for performance tuning and better resource allocation.

Using sys.dm_exec_query_stats

Here is a basic example of how you might use sys.dm_exec_query_stats to get information about query execution times, CPU time, logical reads, and so on:

This query returns detailed statistics on executed SQL statements, including execution count, CPU time, logical reads, and writes. It also provides total elapsed time, average elapsed time per execution, and the actual text of each SQL statement. By analyzing these metrics, you can identify performance bottlenecks and optimize query efficiency. As a result, this data enhances database performance and improves overall system responsiveness.

In PostgreSQL, although there isn’t a direct equivalent to SQL Server’s sys.dm_exec_query_stats, similar insights are achievable. By leveraging PostgreSQL’s system catalogs and views, especially the pg_stat_statements extension, you can monitor query execution statistics. This extension provides crucial data, including execution frequency, resource usage, and performance metrics for SQL statements. Consequently, PostgreSQL DBAs can effectively track query performance and optimize overall server efficiency.

First, enable the pg_stat_statements module in your PostgreSQL instance by adding it to shared_preload_libraries in the configuration file. Then, restart the PostgreSQL server to apply the changes. Additionally, you may need to create the extension in your database using the following command.

Once pg_stat_statements is enabled, you can query its view to get query performance statistics. Here’s an example query similar in spirit to the SQL Server example:

This will give you:

  • query: Text of a representative query, with some values anonymized.
  • calls: Number of times the statement was executed.
  • total_time: Total time spent in the statement, in milliseconds.
  • rows: Total number of rows retrieved or affected by the statement.
  • min_time, max_time, mean_time, stddev_time: Minimum, maximum, mean, and standard deviation of the execution times for the statement, respectively.
  • blocks_hit: Number of times disk blocks were found already in the buffer cache, avoiding disk reads.
  • blocks_read: Number of disk blocks read.

This view is extremely useful for identifying slow queries, frequently executed queries, and queries that are reading a lot of data from disk.

Keep in mind that pg_stat_statements tracks queries across all databases by default and retains data until explicitly reset. Use functions like pg_stat_reset() or pg_stat_statements_reset() to clear the data. PostgreSQL allows you to manage access permissions for pg_stat_statements at the role level.

About Shiv Iyer 497 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.