Slow queries are the silent killers of application performance. A single unoptimized query can cascade into connection pool exhaustion, memory pressure, and eventually full database unavailability. Commercial database performance platforms charge per instance, per metric, or per query analyzed — costs that scale directly with the size of the infrastructure you are trying to monitor.
This guide covers the best self-hosted database query profiling and optimization tools available in 2026: Percona Monitoring and Management (PMM) for comprehensive query analytics, pg_activity for real-time PostgreSQL monitoring, and pgcenter for PostgreSQL command-line diagnostics. You will learn how to install, configure, and use each tool to identify and fix slow queries without sending any data to third-party services.
Why Self-Host Your Database Query Profiling
Running query profiling and analysis on your own infrastructure offers several advantages over cloud-based database performance platforms:
- Zero query data exposure: Cloud APM services receive your actual SQL statements, schema names, table structures, and sometimes parameter values. For databases containing PII, financial records, or healthcare data, this is a compliance risk. Self-hosted tools keep every query within your network.
- Unlimited retention: Cloud platforms limit query history to 7–30 days on standard plans. Self-hosted query logs and analytics data persist as long as your storage allows, enabling month-over-month trend analysis and seasonal pattern detection.
- No sampling bias: Cloud services often sample query data to reduce storage costs. When you self-host, you can capture every single query execution, including rare edge-case queries that only run during specific business processes.
- Cost predictability: Percona PMM, pg_activity, and pgcenter are all free and open-source. Your only cost is the compute and storage for the monitoring infrastructure itself.
- Custom alerting thresholds: Define alert rules based on your specific workload characteristics rather than generic industry benchmarks. A query that takes 500ms might be fine for an analytics workload but catastrophic for a real-time trading system.
For broader database monitoring beyond query profiling, see our pgwatch2 vs Percona PMM vs pgmonitor comparison and database benchmarking tools guide.
Tool Comparison at a Glance
| Feature | Percona PMM (QAN) | pg_activity | pgcenter |
|---|---|---|---|
| Type | Full monitoring platform with web UI | CLI real-time monitor | CLI diagnostics toolkit |
| Database Support | MySQL, PostgreSQL, MongoDB | PostgreSQL only | PostgreSQL only |
| Query Analytics | Full slow query history with execution plans | Active queries only | Active queries + statistics tables |
| Slow Query Log Parsing | Built-in | No | No |
| Historical Data | Yes (configurable retention) | No | No |
| Alerting | Yes (Grafana-based) | No | No |
| Resource Monitoring | CPU, memory, disk I/O, network | Limited | Detailed PostgreSQL stats |
| Installation | Docker container | pip install | Single binary |
| Stars | 1,008 | 3,012 | 1,600 |
| Last Updated | April 2026 | January 2026 | January 2026 |
| Best For | Production monitoring + QAN dashboards | Real-time DBA terminal monitoring | Deep-dive PostgreSQL diagnostics |
Percona Monitoring and Management (PMM): Full-Stack Query Analytics
Percona PMM is the most comprehensive open-source database monitoring platform. Its Query Analytics (QAN) component captures every slow query, aggregates execution patterns, and presents them in a web-based dashboard with execution plan visualizations.
How PMM Query Analytics Works
PMM deploys agents (pmm-agent) on your database servers that collect query metrics from slow query logs, Performance Schema (MySQL), or pg_stat_statements (PostgreSQL). These metrics are sent to a central PMM server running Grafana and ClickHouse for storage and visualization.
Installing PMM Server via Docker
| |
The PMM server is available at https://<your-server-ip>/ with default credentials admin / admin. You will be prompted to set a new password on first login.
Connecting a PostgreSQL Database
| |
The --query-source=pgstatstatements flag tells PMM to use the pg_stat_statements extension for query data. You need to enable this extension in PostgreSQL first:
| |
Connecting a MySQL Database
| |
For MySQL slow query log parsing, enable the slow query log:
| |
Using the Query Analytics Dashboard
Once connected, the QAN dashboard at /graph/d/pmm-qan/ shows:
- Top queries by load: Queries ranked by total execution time, sorted by frequency × average duration
- Query execution plans: Visual EXPLAIN output for each query pattern
- Latency distribution: Histogram showing P50, P95, P99 response times
- Query fingerprint aggregation: Similar queries grouped by normalized pattern (e.g.,
SELECT * FROM users WHERE id = ?)
The dashboard automatically detects new query patterns and can alert when query latency exceeds your defined thresholds. For production deployments, pair PMM with a self-hosted alerting solution for PagerDuty-style notifications.
pg_activity: Real-Time PostgreSQL Monitoring
pg_activity is a command-line tool that provides a top-like view of PostgreSQL activity. It shows running queries, lock contention, transaction age, and database-level statistics in real-time — ideal for terminal-based database administration.
Installation
| |
Basic Usage
| |
Understanding the Interface
The pg_activity interface displays:
- Header: PostgreSQL version, uptime, number of connections, transactions/sec, tuples read/sec
- Process list: Each running query with PID, database, user, duration, state, and the full query text
- I/O statistics: Read/write throughput per database
- Lock information: Active locks and blocked queries highlighted in red
Key keyboard shortcuts:
Space: Pause/resume display refreshR: Reverse sort orderq: Quitd: Filter by database nameu: Filter by user name
pg_activity is particularly effective during incident response. When a production database slows down, connecting with pg_activity --min-duration 2 immediately shows which queries are causing the bottleneck, how long they have been running, and whether they are blocked by locks.
pgcenter: PostgreSQL Command-Line Diagnostics Toolkit
pgcenter is a comprehensive command-line toolkit for PostgreSQL administration and troubleshooting. Unlike pg_activity which focuses on real-time query monitoring, pgcenter provides a suite of tools for viewing statistics tables, managing configuration, and performing deep diagnostic analysis.
Installation
| |
Key Commands
| |
Analyzing Query Performance with pg_stat_statements
The pg_stat_statements view is the foundation of PostgreSQL query profiling. pgcenter provides a formatted view of this data:
| |
Output columns include:
- Total time: Cumulative execution time across all calls
- Mean time: Average execution time per call
- Rows: Total number of rows returned or modified
- Blk read/write time: Time spent reading/writing shared buffers
- Temp file count/size: Number and size of temporary files created
The record and replay functionality is particularly valuable for capacity planning. Record statistics during peak hours, then replay the data to identify trends in query patterns, table growth, and connection usage over time.
Complementary Tool: HypoPG for Index Testing
While not a profiling tool itself, HypoPG is a PostgreSQL extension that lets you test hypothetical indexes without actually creating them. This is essential for the optimization workflow: identify slow queries with PMM or pg_activity, then use HypoPG to test whether a specific index would improve performance before committing to the schema change.
| |
| |
This workflow — profile with PMM, test with HypoPG, deploy the winning index — is the most efficient path to query optimization without production risk.
Step-by-Step: From Slow Query to Fixed Query
Here is a complete workflow combining all three tools to identify and resolve a slow query in production:
Step 1: Detect the Problem with PMM QAN
Open the PMM Query Analytics dashboard. Sort by “Load” to see the queries consuming the most total database time. Look for queries with high P99 latency or a growing execution count.
| |
Step 2: Inspect Active Queries with pg_activity
| |
Look for queries in the “active” state with durations above your SLA threshold. Note the PID and the full query text.
Step 3: Analyze the Query Plan
| |
Look for Sequential Scan nodes, high row estimates, or excessive buffer reads.
Step 4: Test a Hypothetical Fix
| |
Step 5: Monitor the Improvement
After deploying the index, use pgcenter to record baseline statistics, then compare with post-deployment data:
| |
FAQ
What is the difference between database monitoring and query profiling?
Database monitoring tracks overall server health metrics like CPU usage, memory consumption, connection counts, and replication lag. Query profiling specifically analyzes individual SQL statements to identify slow queries, understand execution plans, and find optimization opportunities. Tools like Percona PMM do both, while pg_activity focuses primarily on real-time query visibility.
Can I use Percona PMM for both MySQL and PostgreSQL?
Yes. PMM supports MySQL, MariaDB, PostgreSQL, and MongoDB. You can monitor a mixed database environment from a single PMM server instance. Each database type has its own set of dashboards and query analyzers within the same interface.
Is pg_activity suitable for production monitoring?
pg_activity is designed for interactive, real-time inspection rather than continuous monitoring. It does not store historical data or provide alerting. Use it during active troubleshooting sessions — for example, when you receive an alert from your monitoring system and need to immediately see what queries are running. For continuous monitoring, deploy PMM or a similar platform.
How does HypoPG differ from creating a real index?
HypoPG creates “hypothetical” indexes that exist only in the query planner’s memory. The actual table data is not indexed, so there is zero storage overhead and zero impact on write performance. The planner treats the hypothetical index as if it existed when generating execution plans, allowing you to test optimization strategies without any risk to the production database.
What is the resource overhead of running PMM agents?
PMM agents typically consume 1–3% CPU and 50–100MB of memory per monitored database instance. The agent collects metrics at 1-second intervals for system metrics and 10-second intervals for query metrics. For very high-throughput databases (10,000+ queries per second), you may want to increase the query collection interval to reduce overhead.
Can pgcenter work with PostgreSQL versions older than 12?
Yes. pgcenter supports PostgreSQL 9.6 and newer. The available statistics views vary by version — newer PostgreSQL versions expose more detailed statistics through additional pg_stat_* views. pgcenter automatically adapts its display based on the connected server version.