PgPostgreSQL Lab
GitHub
Browser investigation · optional server extension

Find the expensive workload

A query returns few rows but performs substantial work. Inspect rows, buffers, and sort behavior in the browser before optionally ranking a server-wide workload.

You will learn to distinguish result size from work performed and identify memory-driven sort spilling, when a sort writes temporary data because it lacks memory. Prerequisites: plans and aggregation. These browser plans report individual runs.

The page loads a fresh order dataset automatically. Select Run SQL in each numbered editor in order. The editors share one database, so later experiments use changes from earlier ones.

Sections on this page

Browser: count the work, not only the answer

Predict how many output rows COUNT produces and how many table rows its scan must inspect.

Enable JavaScript to start the browser database.
SQL 01
Ctrl / ⌘ + Enter

The aggregate emits one row. For each tenant, inspect the scan's actual rows and Rows Removed by Filter. How much input did each scan examine to return its one count? Repeating a query may change cache activity and elapsed time without changing the amount of data its access path must inspect. A pair of samples does not establish a latency distribution.

Browser: expose memory pressure

work_mem is a memory budget used by individual sort/hash operations, not a cap on the whole database. Predict the sort method when a full sort has a small budget. Only EXPLAIN's plan rows are returned here, even though the underlying query processes all orders.

SQL 02
Ctrl / ⌘ + Enter

Look for the sort method and temporary-buffer activity. An external sort writes intermediate runs and merges them. In PGlite these temporary files use the embedded filesystem; their bytes are not measurements of a production disk. Depending on engine version and data shape, use the actual plan rather than requiring one printed memory or disk value.

Browser: change the memory budget

SQL 03
Ctrl / ⌘ + Enter

Compare Sort Method and temporary blocks read or written with SQL 02. Raising a budget can remove spilling for this fixture, but concurrent queries and multiple operators can multiply the total memory demand. A faster single sort does not establish a safe server-wide setting.

Explain the browser evidence

Which output measures the returned rows, which measures the scan, and which shows why the 64 kB sort used temporary storage?

Hint: keep four quantities separate

Count result rows, inspected rows, held memory, and temporary reads/writes separately. Then ask which of those quantities the changed setting directly affects.

Worked explanation

COUNT's small result hid the scan's larger input work. The low-budget sort can spill intermediate runs, while the larger budget can keep this fixture's sort in memory. Actual buffers and sort methods explain that difference more directly than a fixed timing threshold. These are per-query observations; the optional server extension adds call counts and cumulative workload statistics.

Go deeper: how the engine works

How does a memory budget turn a sort into temporary writes, reads, and merge passes? Read Memory limits and spilling (opens in a new tab) in Query Engine Book. Its DuckDB examples and teaching engine explain the general principle; PostgreSQL's operators and implementation details can differ. This reading is optional.

Transfer the idea

After the original experiments, return to SQL 02, which sets work_mem to 64kB. Change ORDER BY total_cents, id; to ORDER BY total_cents, id LIMIT 20; and select Run SQL. Compare Sort Method, temporary blocks, and the child scan's actual row count with the original 64 kB result. Can the sort keep fewer rows in memory while the scan still reads the same input?

Optional server extension

To run this extension, follow server setup. Copy each panel to the labelled terminal or psql session. These commands use the Docker database.

Measure cumulative work with pg_stat_statements

One request looks cheap in isolation, but thousands of calls dominate the service's database time. Rank the workload before tuning one example.

You will learn to distinguish cumulative cost from per-call latency, inspect waits, and connect a normalized statement to a concrete plan. Prerequisites: plans and lock diagnosis. Use a fresh server fixture, with the included pg_stat_statements preload configuration.

Start a controlled observation window

pg_stat_statements aggregates execution statistics for normalized statements. Similar statements with different literal constants can share an entry. Reset below affects statistics for the whole isolated server; do not use this reset casually on a shared system.

Observer session
SELECT pg_stat_statements_reset();
SELECT count(*) FROM lab.orders WHERE tenant_id = 42;
SELECT count(*) FROM lab.orders WHERE tenant_id = 43;
SELECT count(*) FROM lab.orders WHERE tenant_id = 44;

Predict how many normalized entries these three counts create. The constants differ, but the query shape is the same. Now add a different shape, executed once.

Session A
SELECT sum(total_cents) FROM lab.orders WHERE status = 'pending';

Rank cumulative work

Observer session
SELECT calls, round(total_exec_time::numeric, 3) AS total_ms,
       round(mean_exec_time::numeric, 3) AS mean_ms,
       rows, shared_blks_hit, shared_blks_read, query
FROM pg_stat_statements
WHERE query LIKE '%lab.orders%'
  AND query NOT LIKE '%pg_stat_statements%'
ORDER BY total_exec_time DESC;

Find the query entry for the three counts and compare its calls with the sum entry. The sum and count statements may exchange rank across machines. total_exec_time accumulates server execution time; mean_exec_time is an average, not a tail-latency percentile. The rows field reports rows emitted or affected, not every row inspected. A COUNT returning one result can still scan thousands of source rows.

These counters are cumulative since reset or entry creation. Real diagnosis needs a before/after window, workload context, and awareness that entries may be evicted. Cumulative execution time is not a direct measure of CPU time or elapsed wall time for the entire service.

Connect the aggregate to a plan

Run the supplied Observer query for tenant 42. In the plan, find the scan's actual rows and Rows Removed by Filter. Why would a different tenant population warrant a separate sample?

Observer session
EXPLAIN (ANALYZE, BUFFERS, TIMING OFF)
SELECT count(*) FROM lab.orders WHERE tenant_id = 42;
CREATE INDEX observability_tenant ON lab.orders (tenant_id);
EXPLAIN (ANALYZE, BUFFERS, TIMING OFF)
SELECT count(*) FROM lab.orders WHERE tenant_id = 42;

A normalized entry can combine parameters with different selectivity. One sampled plan does not explain every call. EXPLAIN ANALYZE adds its own execution and instrumentation; account for that when interpreting a short observation window.

Look for waiting, too

Observer session
SELECT application_name, state, wait_event_type, wait_event,
       clock_timestamp() - xact_start AS transaction_age,
       pg_blocking_pids(pid) AS blockers
FROM pg_stat_activity
WHERE datname = current_database() AND backend_type = 'client backend'
ORDER BY application_name;

An idle client waiting for its next command is different from an active statement waiting for a lock. Sampled activity and wait events help distinguish them. A query with modest execution cost can still participate in a latency incident through lock ownership and long transaction lifetime.

Explain the next measurement

A hint for explaining the server results

State the observation window, call count, work per call, and waiting separately. A single average cannot stand in for all four.

Worked explanation

The literal tenant predicates normalize to one statement shape with multiple calls. Rank total cost to find aggregate impact, then inspect representative plans and parameter distributions. Use activity and blockers to investigate waits. Verify an intervention with comparable workload deltas rather than comparing a warm one-off query with an earlier cumulative total.

Transfer the idea

Design a five-minute measurement for a high-volume endpoint. Name the counters collected at both boundaries, request counts from the application, and one tail-latency metric. Explain how you would notice that a faster query is being called twice as often after a release.

Next: prove you can recover.

Primary sources: pg_stat_statements and monitoring statistics.