Sub topic 4
Reading the Core Columns
Assume you know basic SQL and can query PostgreSQL with psql or another SQL client. You also need access to a database where pg_stat_statements is enabled. This chapter uses PostgreSQL 13 or later, where execution-time columns are named total_exec_time and mean_exec_time.
pg_stat_statements stores one summary row for each normalized query. The row combines many executions instead of recording every individual execution. Its core columns help answer three questions:
- How often did this query run?
- How much database time did it consume?
- How much data did it return?
The most useful columns
| Column | Meaning | Unit or type |
|---|---|---|
| `queryid` | Identifier for the normalized query shape | Integer |
| `query` | Representative SQL text | Text |
| `calls` | Number of recorded executions | Count |
| `total_exec_time` | Combined execution time for all calls | Milliseconds |
| `mean_exec_time` | Average execution time per call | Milliseconds |
| `rows` | Total rows returned or affected | Count |
calls is the first number to check. A query that ran 100,000 times may deserve attention even if each call is fast. A query that ran once may have a high average time but little effect on normal workload.
total_exec_time is the sum of execution time across all calls. It is useful for finding workload heavy hitters. mean_exec_time is calculated from the total time and call count. It shows the typical cost of one execution, but an average can hide unusually slow calls.
rows is the total number of rows returned by SELECT statements or affected by commands such as UPDATE and DELETE. It is not the number of rows scanned internally. A query can return one row after scanning millions of rows.
A first inspection query
SELECT queryid,
calls,
round(total_exec_time::numeric, 2) AS total_ms,
round(mean_exec_time::numeric, 2) AS mean_ms,
rows,
left(query, 100) AS query
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 10;The round expressions make the output easier to read. The left expression limits the displayed query text to 100 characters. It does not change the stored statement.
Suppose a result contains these values:
| calls | total_ms | mean_ms | rows |
|---|---|---|---|
| 60,000 | 120,000 | 2.00 | 60,000 |
| 20 | 90,000 | 4,500.00 | 20 |
The first query consumed more total database time. It is a frequent workload cost. The second query is much slower per call, so it may affect requests that use it, but it contributed less cumulative time during this measurement period.
How query text and queryid work together
The queryid identifies a query shape. PostgreSQL normally groups statements that differ only in constant values. For example, these statements can share one statistics row:
SELECT * FROM orders WHERE customer_id = 42;
SELECT * FROM orders WHERE customer_id = 99;The stored query commonly displays a parameterized form such as customer_id = $1. This grouping lets you analyze the operation as a class instead of analyzing every literal separately.
Exercise
A query has 50,000 calls, a mean execution time of 3 ms, and another query has 5 calls with a mean of 2,000 ms. Which query has the larger workload cost if no other data is available?
Show answer
The first query has an estimated total of 150,000 ms. The second has an estimated total of 10,000 ms, so the first query has the larger aggregate cost.