Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Postgres performance optimization and best practices guide maintained by Supabase
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/monitor-pg-stat-statements.md
1---2title: Enable pg_stat_statements for Query Analysis3impact: LOW-MEDIUM4impactDescription: Identify top resource-consuming queries5tags: pg-stat-statements, monitoring, statistics, performance6---78## Enable pg_stat_statements for Query Analysis910pg_stat_statements tracks execution statistics for all queries, helping identify slow and frequent queries.1112**Incorrect (no visibility into query patterns):**1314```sql15-- Database is slow, but which queries are the problem?16-- No way to know without pg_stat_statements17```1819**Correct (enable and query pg_stat_statements):**2021```sql22-- Enable the extension23create extension if not exists pg_stat_statements;2425-- Find slowest queries by total time26select27calls,28round(total_exec_time::numeric, 2) as total_time_ms,29round(mean_exec_time::numeric, 2) as mean_time_ms,30query31from pg_stat_statements32order by total_exec_time desc33limit 10;3435-- Find most frequent queries36select calls, query37from pg_stat_statements38order by calls desc39limit 10;4041-- Reset statistics after optimization42select pg_stat_statements_reset();43```4445Key metrics to monitor:4647```sql48-- Queries with high mean time (candidates for optimization)49select query, mean_exec_time, calls50from pg_stat_statements51where mean_exec_time > 100 -- > 100ms average52order by mean_exec_time desc;53```5455Reference: [pg_stat_statements](https://supabase.com/docs/guides/database/extensions/pg_stat_statements)56