pg_stat_statements Extension
Overview
A PostgreSQL extension that tracks execution statistics of all SQL statements executed by a server. It provides insights into query performance, allowing you to identify slow queries and optimize them.
Dashboard Configuration
This extension requires activation via the PlanetScale Dashboard before it can be used. It must be enabled through shared libraries and requires a database restart.
To enable pg_stat_statements:
- From the PlanetScale organization dashboard, select the desired database
- Navigate to the Cluster configuration page from the menu on the left
- Choose the branch whose extensions you'd like to configure in the "Branch" dropdown
- Select the Extensions tab
- Enable pg_stat_statements and configure its parameters
- Click Queue extension changes to apply the configuration
- Once you're ready to apply the changes, click "Apply changes"
Parameters
pg_stat_statements.max
- Type: Integer
- Default:
5000
- Description: Maximum number of statements tracked by the extension.
pg_stat_statements.track
- Type: Select
- Options: top, all
- Default:
top
- Description: Which statements to track. 'top' tracks top-level statements only, 'all' tracks all statements including nested ones.
pg_stat_statements.track_utility
- Type: Boolean
- Default:
true
- Description: Whether to track utility statements (such as PREPARE, EXPLAIN, etc.).
pg_stat_statements.track_planning
- Type: Boolean
- Default:
false
- Description: Whether to track planning statistics for statements.
pg_stat_statements.save
- Type: Boolean
- Default:
true
- Description: Whether to save statement statistics across server restarts.
Usage
After enabling the extension through the dashboard, you can install it in your database:
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;
Then you can query the statistics view:
-- View the most time-consuming queries SELECT query, calls, total_exec_time, mean_exec_time, rows FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;
External Documentation
For more detailed information about pg_stat_statements usage and the available statistics, see the official PostgreSQL documentation.
Need help?
Get help from the PlanetScale Support team, or join our GitHub discussion board to see how others are using PlanetScale.