P50 28s Analyze →
↩ All field notes
Querk · Field notes

Reading pg_stat_statements: A Practical SMB Workflow

Published April 24, 2026 · Querk — Postgres review pipeline

Reading pg_stat_statements: A Practical SMB Workflow

Small to medium-sized businesses (SMBs) in the trucking and logistics industry often run lean engineering teams. With limited resources, optimizing database performance is critical. Slow database queries can cripple real-time tracking, dispatch, and billing systems, directly impacting your bottom line. PostgreSQL's `pg_stat_statements` extension provides a powerful, built-in tool for identifying these bottlenecks. Here's a practical workflow for your 2–5 person engineering team to extract the real slow queries worth fixing first.

Installation and Setup

Before you start, ensure `pg_stat_statements` is installed and configured on your PostgreSQL server. Most modern PostgreSQL installations include it, but you might need to enable it. Connect to your database as a superuser and run:

```sql

CREATE EXTENSION pg_stat_statements;

```

Next, configure the extension. Edit your `postgresql.conf` file (typically found in `/etc/postgresql/<version>/main/`) and add or modify these settings:

```

pg_stat_statements.max = 10000 # Adjust based on your needs, but don't go too crazy.

pg_stat_statements.track = all

```

Restart your PostgreSQL server for the changes to take effect. Now, `pg_stat_statements` will begin collecting statistics on all executed SQL statements.

Querying pg_stat_statements

The core of this workflow involves querying the `pg_stat_statements` view. Here’s a basic query to identify the slowest queries:

```sql

SELECT

query,

calls,

total_time,

mean_time,

stddev_time

FROM

pg_stat_statements

ORDER BY

total_time DESC

LIMIT 10;

```

This query retrieves the top 10 queries by `total_time` (the total execution time for all calls). `calls` shows how many times the query was executed, `mean_time` is the average execution time, and `stddev_time` indicates the variability in execution time.

Focus on queries with high `total_time` and a significant number of `calls`. These are your primary targets. If a query has a high `total_time` but few `calls`, it might be less critical than a query executed frequently, even if its individual execution time is lower.

Analyzing and Optimizing

Once you've identified slow queries, the next step is analysis. Examine the `query` column to understand the SQL statement. Consider using `EXPLAIN ANALYZE` on the query to get detailed execution plans. This will show you where the query is spending its time (e.g., table scans, index usage).

Common optimization strategies include:

* **Indexing:** Ensure appropriate indexes exist on columns used in `WHERE` clauses, `JOIN` conditions, and `ORDER BY` clauses.

* **Query Rewriting:** Sometimes, a poorly written query can be rewritten for better performance.

* **Data Modeling:** Review your database schema for potential inefficiencies.

* **Connection Pooling:** Implement connection pooling to reduce connection overhead.

Remember to test your changes thoroughly. After making optimizations, re-run the `pg_stat_statements` query to measure the impact of your changes. Iterate until performance improves.

Beyond the Basics

This workflow provides a solid foundation. For more advanced analysis, consider using tools that can help you visualize and monitor your database performance over time. QueryDoctor provides real-time query analysis and performance monitoring specifically designed for PostgreSQL, helping you quickly identify and resolve performance bottlenecks in your trucking and logistics applications.

Querk reviews a slow Postgres query in ~30 seconds — index DDL, rewrites, write-path impact, and a verification command. Paste a query →