Reading pg_stat_statements: A Practical SMB Workflow
For a two-to-five person engineering team, PostgreSQL performance tuning often gets deprioritized until a slow query surfaces in production. pg_stat_statements is a built-in extension that tracks execution statistics for every SQL statement, making it possible to find the real bottlenecks without guessing. The challenge for small teams is turning that raw data into a short, actionable list.
Enable pg_stat_statements
The extension is not active by default on most managed PostgreSQL offerings. A team member with sufficient privileges needs to add it to shared_preload_libraries in postgresql.conf—or the equivalent database flag—restart PostgreSQL, and run CREATE EXTENSION IF NOT EXISTS pg_stat_statements;. Once enabled, PostgreSQL records total execution time, call count, rows returned, and block I/O for each normalized query. Teams should verify the view is populated by selecting from pg_stat_statements and confirming that rows exist. Without this baseline, any optimization effort is anecdotal.
Identify the queries worth fixing
The view contains dozens of columns, but small teams should focus on a few: queryid, calls, total_exec_time, mean_exec_time, rows, shared_blks_read, and local_blks_read. A query with high total_exec_time but a low call count might be a one-off report; a query with moderate mean_exec_time and thousands of calls is usually the better target because it affects everyday throughput. Ordering by total_exec_time descending reveals the heaviest consumers of database time. Filtering out utility statements and focusing on SELECT, INSERT, UPDATE, and DELETE keeps the list relevant to application code.
Prioritize by impact, not just duration
Execution time alone can mislead. A query that runs once per night for ten minutes matters less than a query that runs fifty times per second for fifty milliseconds each. Multiplying mean_exec_time by calls estimates the daily load each statement places on the database. Teams should also inspect shared_blks_read: high buffer reads often signal missing indexes or inefficient scans. Querk can surface these patterns automatically, but the underlying logic remains the same—rank by cumulative impact on user-facing requests, then examine the query plan with EXPLAIN (ANALYZE, BUFFERS) for the top candidates.
Turn findings into a fix queue
For each prioritized query, capture the exact SQL, current mean execution time, and call frequency. Assign one engineer to reproduce the plan in staging, test an index or rewrite, and measure the delta before deploying. Small teams benefit from a lightweight backlog item per query rather than a broad performance sprint. Re-check pg_stat_statements after deployment to confirm the change reduced total_exec_time without increasing call count or block reads elsewhere.
Repeat the review weekly or after major releases. pg_stat_statements accumulates data until reset, so teams should reset statistics with pg_stat_statements_reset() after implementing fixes to isolate new regressions. Over time, this workflow turns database observability into a routine part of shipping code, rather than an emergency response.
