Chapter 2: pg_stat_statements
Unlocking performance insights that drive better decisions
When applications slow down, your team usually feels it before they can explain it. Dashboards start lagging. Reports take longer to generate. Restarting the database becomes a default fix, although not because it solves the issue; rather, because tracking down the cause takes too long.
This kind of performance drag is frustrating and expensive. It delays teams, hurts user experience, and adds pressure to scale infrastructure before exhausting existing resources. The real challenge? Visibility. PostgreSQL doesn’t offer detailed performance insights out of the box, leaving your team guessing at what’s wrong.
The pg_stat_statements extension gives you a clear, structured view of how queries behave in your PostgreSQL database. It tracks which queries are running, how long they take, how often they’re called, and how much I/O they generate. It even groups similar queries together so you don’t have to chase down every variation just to spot a pattern.
The result: faster troubleshooting, better decisions, and less time wasted on guesswork.

Why pg_stat_statements is critical for enterprise use
1. It helps you pinpoint expensive queries
You no longer need to rely on complaints or dig through logs. pg_stat_statements shows you which queries are consuming the most time, generating the most I/O, or running most frequently. You can sort by total execution time, average runtime, or resource usage and immediately find performance bottlenecks.
2. It surfaces patterns, not just outliers
Query normalization means you're not chasing down dozens of similar statements with different values. You get a consolidated view of query behavior across your application, making it easier to detect repeat issues and tune accordingly.
3. It gives you before-and-after clarity
Thinking about a schema change or releasing a new feature? pg_stat_statements gives you the ability to track performance impact over time. Instead of relying on gut feeling, you have real data to show what improved and what didn’t.
4. It helps you prioritize tuning efforts
Not all slow queries are equally important. This extension lets your team focus on the queries that matter most, backed by real metrics, so time and resources go where they have the biggest impact.
5. It shortens the debugging loop
Instead of jumping between logs, code, and config files, engineers can go straight to the database for answers. That saves hours of time and keeps projects on track.
Your options for using pg_stat_statements
Adding pg_stat_statements to your PostgreSQL environment isn’t difficult. But, like any extension, how you implement and manage it can make a big difference.
Option 1: Manual setup
You can enable pg_stat_statements by modifying your postgresql.conf, restarting the database, and creating the extension in each schema where it's needed. But it’s not set-and-forget. On high-traffic systems, you’ll need to manage the data it collects and monitor for compatibility with other extensions. As your environment grows, so does the overhead.
Option 2: Through a commercial PostgreSQL vendor
Some proprietary PostgreSQL vendors include pg_stat_statements in their packages. That might simplify installation, but you may be locked into specific pricing tiers, cloud providers, or database forks. That can limit flexibility long-term and introduce unexpected costs for something that’s supposed to be open source.
Option 3: Integrated into a tested, open source PostgreSQL solution
Percona includes pg_stat_statements in our enterprise-ready PostgreSQL solution. It’s tested to work alongside critical components like Patroni, pgBackRest, and PgBouncer, and integrates directly with Percona Monitoring and Management (PMM) dashboards. You get full visibility into your queries from day one, backed by 24/7 expert support, with no license fees and no proprietary constraints.
Seeing the full picture, faster
pg_stat_statements gives your team the insight to stop guessing and start fixing. Whether you're diagnosing slow reports, scaling a growing application, or trying to improve database performance across the board, this extension helps you do it with confidence.