Timescale Logo
Database Performance

PostgreSQL Extensions: pg_stat_statements

pg_stat_statements is a powerful PostgreSQL extension that provides a means for tracking execution statistics of all SQL statements executed by a server. This extension is incredibly useful for developers as it helps identify performance issues in a PostgreSQL database. It tracks the frequency, execution time, and other valuable statistics of each query, thereby enabling developers to optimize their database performance.

Installing the pg_stat_statements Extension

Before you can use the pg_stat_statements extension, you need to install it. Here's how:

1. First, ensure that the extension is available in your PostgreSQL version. You can check this by running the following command:


SELECT * FROM pg_available_extensions WHERE name = 'pg_stat_statements';

If the extension is not available in your PostgreSQL version, you should really upgrade—it’s been bundled since before PostgreSQL 9.0. If you need help figuring out the best practices on when to upgrade PostgreSQL, check out this blog post.

2. If the extension is available, you need to first load it into your shared_preloaded_libraries in postgresql.conf, as it will require additional shared memory. If you’re using Timescale, you can simply skip this step, as the extension is already preloaded. If you’re not using Timescale, you can install it by running the following command:


CREATE EXTENSION pg_stat_statements;

Please note that you need superuser privileges to install extensions in PostgreSQL.

Using the pg_stat_statements Extension

Once installed, pg_stat_statements starts tracking the queries automatically. You can view the statistics by querying the pg_stat_statements view. Here's a simple example:

SELECT
    query,
 calls,
    total_exec_time,
rows,
    100.0 * shared_blks_hit / nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
FROM pg_stat_statements
ORDER BY total_time DESC
LIMIT 5;

This query returns the top five queries with the highest total execution time.

Time-Series Use Cases for the pg_stat_statements Extension

pg_stat_statements is particularly useful in time-series databases where query performance is critical. By tracking query performance over time, developers can identify slow queries and optimize them for better performance. This is crucial in time-series databases, where data is continuously ingested and queried.

Using pg_stat_statements With Timescale and Time-Series Data

If you're using Timescale, the high-performance, time-series PostgreSQL++ cloud database, you can use pg_stat_statements to monitor query performance. The PostgreSQL extension is enabled by default in all Timescale services, tracking the queries automatically. You can then query the pg_stat_statements view to analyze the performance of your time-series queries.

✨ Editor's Note: If you’re looking for further insights displayed in a convenient, easy-to-access manner, it’s worth trying out Timescale— it’s 100 percent free for 30 days)— where we offer visual information about your query performance directly in the UI console.

Remember, optimizing your database performance is an iterative and ongoing process. Regularly monitor your query performance using pg_stat_statements and make necessary optimizations to ensure your time-series database runs smoothly.

Visit our blog to learn how to identify slow queries and other performance issues and how to do point-in-time database and query monitoring using pg_stat_staments.

Timescale Logo

Subscribe to the Timescale Newsletter

By submitting, I acknowledge Timescale’s Privacy Policy
2024 © Timescale Inc. All rights reserved.