Low-Medium Impact

Enable pg_stat_statements for Query Analysis

Find slow and frequent queries across your entire database

Why This Matters

pg_stat_statements is a built-in Postgres extension that tracks execution statistics for all SQL statements. It's the foundation for identifying performance problems across your entire database, not just individual queries.

Without it, you can only analyze queries one at a time with EXPLAIN ANALYZE. With pg_stat_statements, you can:

Note: pg_stat_statements is enabled by default on Supabase. For self-hosted Postgres, you need to enable it in postgresql.conf.

The Problem

Without pg_stat_statements, you can't see which queries are causing problems:

Incorrect - No query-level statistics
-- Database is slow, but which queries?
-- You can only check pg_stat_activity for current queries
SELECT pid, query, state 
FROM pg_stat_activity 
WHERE state = 'active';

-- This only shows what's running NOW
-- Can't see:
-- - Which queries run most often?
-- - Which queries take the most total time?
-- - Historical performance trends?

The Solution

Enable pg_stat_statements and query it to find problem queries:

Correct - Enable extension and query statistics
-- Enable the extension (if not already enabled)
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

-- Find slowest queries by total execution time
SELECT 
  query,
  calls,
  total_exec_time,
  mean_exec_time,
  max_exec_time
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;

Useful Queries

Find queries executed most frequently (potential N+1 problems):

-- Most frequently executed queries
SELECT 
  query,
  calls,
  total_exec_time,
  mean_exec_time
FROM pg_stat_statements
ORDER BY calls DESC
LIMIT 10;

Find queries with highest average execution time:

-- Slowest queries by average time
SELECT 
  query,
  calls,
  mean_exec_time,
  total_exec_time
FROM pg_stat_statements
WHERE calls > 100  -- Filter out one-off queries
ORDER BY mean_exec_time DESC
LIMIT 10;

Result: You can now identify queries that run 10,000 times per hour but take 50ms each - that's 500 seconds of database time that could be optimized.

Key Columns

Monitor Query Performance Automatically

Connect your Supabase project to automatically track slow queries and identify optimization opportunities.

Start Supabase Audit

Common Mistakes

Learn More