Buffers and loops
Overview
Read runtime counters at the node where they are reported. PostgreSQL reports actual rows and node times as per-loop averages; loops counts executions. Buffer statistics describe page access activity rather than unique rows or guaranteed physical disk operations.
First example
EXPLAIN QUERY PLAN SELECT c.id,(SELECT COUNT(*) FROM ep_order o WHERE o.customer_id=c.id) FROM ep_customer c ORDER BY c.id;Observed SQLite result (rows in JSON):
[ [ 3, 0, 0, "SCAN c" ], [ 7, 0, 0, "CORRELATED SCALAR SUBQUERY 1" ], [ 12, 7, 0, "SEARCH o USING COVERING INDEX ep_customer_id (customer_id=?)" ]]How it works
The first SQLite plan has a customer scan, a correlated scalar subquery and a child covering-index search. The second query counts orders for three customers: 60, 40, 0. This demonstrates repeated dependent work, but EQP does not measure actual loops or buffers. In an illustrative PostgreSQL node, actual rows=20 loops=5 means about 100 output rows across executions, allowing for rounding; actual time values are also averaged per execution. Do not multiply buffer counters by loops: they are already accumulated and can include child activity. Shared hit means found in PostgreSQL shared buffers; shared read means brought into them, potentially from OS cache rather than physical disk. Local buffers relate to temporary tables; temp blocks describe temporary working data. A high hit count may still indicate expensive repeated work. Distinguish planning time from execution time; avoid summing inclusive node times.
Practical example
SELECT c.id,(SELECT COUNT(*) FROM ep_order o WHERE o.customer_id=c.id) FROM ep_customer c ORDER BY c.id;Observed SQLite result (rows in JSON):
[ [ 1, 60 ], [ 2, 40 ], [ 3, 0 ]]Practice and mistakes
Practice: replace the correlated count with LEFT JOIN and GROUP BY. Hint 1: retain customer 3. Hint 2: count o.id rather than COUNT(*). Compare outputs, then inspect plans without claiming measured loop counts. For a hypothetical actual rows=4 loops=12, estimate 48 output rows.
PostgreSQL 17 example — NOT RUN
BEGIN;SET LOCAL statement_timeout = '10s';SET LOCAL lock_timeout = '2s';CREATE TEMP TABLE ep_customer(id INTEGER PRIMARY KEY, name TEXT NOT NULL);INSERT INTO ep_customer VALUES(1,'Ada'),(2,'Reza'),(3,'Sara');CREATE TEMP TABLE ep_order(id INTEGER PRIMARY KEY, customer_id INTEGER NOT NULL REFERENCES ep_customer(id), status TEXT NOT NULL CHECK(status IN ('common','rare')), amount INTEGER NOT NULL CHECK(amount>=0));WITH RECURSIVE seq(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM seq WHERE n<100)INSERT INTO ep_order SELECT n, CASE WHEN n<=60 THEN 1 ELSE 2 END, CASE WHEN n<=90 THEN 'common' ELSE 'rare' END,n*10 FROM seq;CREATE INDEX ep_status ON ep_order(status);CREATE INDEX ep_customer_id ON ep_order(customer_id);CREATE INDEX ep_amount ON ep_order(amount);ANALYZE ep_order;
EXPLAIN (ANALYZE, BUFFERS) SELECT c.id,(SELECT COUNT(*) FROM ep_order o WHERE o.customer_id=c.id) FROM ep_customer c ORDER BY c.id;ROLLBACK;Expected result, not executed: "Inspect actual loops and buffers; counts are not predetermined."