Quick revision
The short answer
EXPLAIN (ANALYZE, BUFFERS) reports, for each plan node, how many 8 kB pages were found in PostgreSQL's shared buffers (hit), had to be read from outside them (read), and were dirtied or written. On the first run the query read 48,000 pages, about 375 MB, from outside shared_buffers; on the second run all 48,120 pages were already cached. When tuning, we should treat the page count as the real measure of work, remember that read can still mean the operating system cache, and judge the query by its cold-cache behaviour if the data does not fit in memory.
What to remember
In this answer, we saw that BUFFERS reports the 8 kB pages each node found in shared_buffers, read from outside, dirtied and wrote. The first run read 48,000 pages, about 375 MB, from outside PostgreSQL's cache and the second found all 48,120 cached, so the work was identical and only the source changed. A read can still come from the operating system cache, which track_io_timing helps reveal. We should tune for cold-cache behaviour when the data does not fit in memory and treat fewer pages touched, from better or covering indexes and narrower rows, as the lasting improvement.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
EXPLAIN (ANALYZE, BUFFERS) reports, for each plan node, how many 8 kB pages were found in PostgreSQL's shared buffers (hit), had to be read from outside them (read), and were dirtied or written. On the first run the query read 48,000 pages, about 375 MB, from outside shared_buffers; on the second run all 48,120 pages were already cached. When tuning, we should treat the page count as the real measure of work, remember that read can still mean the operating system cache, and judge the query by its cold-cache behaviour if the data does not fit in memory.
Why this question is asked
Timings in a query plan are noisy: the same query can take 3 seconds once and 80 milliseconds a minute later, only because the data is now in memory. Senior engineers learn to look at how much data a query touches instead, and the BUFFERS option is the tool for that. Interviewers ask this to see whether we understand PostgreSQL's caching layers and avoid the classic trap of tuning a query on a warm cache and declaring victory. In this answer, we will recap how PostgreSQL caches pages, read the two runs in the question number by number, see why read does not always mean disk, and then turn this into a tuning method.
1A Quick Recap of Pages and shared_buffers
PostgreSQL stores tables and indexes in pages, also called blocks, of 8 kB each. It never reads a single row from disk; it reads the page that contains the row. To avoid reading the same pages again and again, it keeps recently used pages in its own memory area called shared_buffers, a cache shared by all connections, often sized at about a quarter of the server's memory.
Below that there is a second cache that PostgreSQL does not control: the operating system's page cache, the memory the operating system uses to keep recently read file data. When PostgreSQL asks for a page that is not in shared_buffers, the operating system may return it from its own cache in microseconds, or it may have to read it from the disk, which takes much longer.
- shared_buffersPostgreSQL's own cache; counted as shared hit
- Operating system page cachenot visible to PostgreSQL; counted as read
- Disk or network storagereal I/O; also counted as read
3 layers, from the top down. Step through them one at a time.
BUFFERS can tell hit from read, but not which of the two lower layers served a read.
2What BUFFERS Adds to the Plan
With BUFFERS, each node gets a Buffers line. shared hit is the number of pages found in shared_buffers. shared read is the number of pages PostgreSQL had to request from the operating system. dirtied counts pages this query modified, and written counts pages it had to write out itself to make room. There are similar local counters for temporary tables and temp counters for sorts and hashes that spilled to disk.
Notice that the total is the same both times: 120 + 48,000 = 48,120 pages. The query did exactly the same amount of logical work. What changed is where the pages came from. On the first run 48,000 pages were not in shared_buffers; 48,000 x 8 kB is 384,000 kB, about 375 MB, fetched from outside. On the second run every page was already cached, so the query was about twenty times faster without any change to the query or the plan.
3Why read Does Not Always Mean Disk
It is tempting to say that the first run did 375 MB of disk I/O. But PostgreSQL only knows that the pages were not in its own cache. If the operating system had them in its page cache, those 48,000 reads were memory copies, not disk reads. On a server with 64 GB of memory and 16 GB of shared_buffers, much of the rest is often the operating system cache, so read is frequently served from memory.
To see the real I/O cost, we turn on track_io_timing. PostgreSQL then measures how long it waited for each read, and EXPLAIN shows it next to the buffers.
Here 2.7 of the 2.95 seconds were spent waiting for reads, so the first run really was dominated by I/O. If the I/O timing had been 40 ms, the pages came from the operating system cache and the slow part was elsewhere, for example CPU spent filtering rows.
4Tuning for the Cold Cache
So which run is the truth? It depends on the production data set. If the table and its indexes fit comfortably in memory and are used all the time, the warm run is what users see. But if the data set is larger than memory, such as a year of events in a 500 GB table where each account's rows are touched only now and then, most real executions look like the first run. Benchmarking only the second run hides the I/O cost that users actually pay.
This is why page counts are a more reliable target than timings from one run. If we change the query or the index so that it touches 3,000 pages instead of 48,000, it will be faster both cold and warm, and the improvement is repeatable. A timing that improved from 2.9 s to 0.14 s may only mean the cache warmed up.
- Run EXPLAIN (ANALYZE, BUFFERS)note hit + read, the total pages touched
- Does the working set fit in memory?YesJudge by warm runs, still reduce pagesNoJudge by cold behaviour, read counts matter
- Turn on track_io_timingis read time real I/O or OS cache?
- Change index, columns or querycovering index, narrower rows, better filter
- Compare total pages before and afterfewer pages is a lasting improvement
5 steps in order. Play them, or step through them one at a time.
5Reducing the Pages a Query Touches
There are a few standard ways to reduce pages. A better index lets the query visit only the matching rows instead of filtering many. A covering index, one that contains every column the query needs (with INCLUDE for extra columns), allows an index-only scan that does not visit the table at all, provided the visibility map is up to date thanks to vacuum. Narrower rows mean more rows per page, so the same rows sit in fewer pages; selecting fewer columns does not change the pages read from the table, but moving large rarely used columns to another table does. And rows that are stored close together for the same account, for example in a table clustered or partitioned by time, need fewer pages for the same result.
One version note: in recent PostgreSQL versions EXPLAIN ANALYZE includes the buffer counts by default, so we may see them without asking; check your version. On older versions we must add BUFFERS explicitly.
6Follow-up questions
What does temp read and written mean in a Sort node?
It means the sort did not fit in work_mem, the memory each sort or hash operation may use, so PostgreSQL wrote parts of the data to temporary files and read them back. The plan usually also says Sort Method: external merge with a disk size. Temp pages are 8 kB too, so temp written=25000 is about 200 MB spilled. We fix it by sorting fewer rows, using an index that already returns rows in order, or raising work_mem for that session or query, keeping in mind that work_mem is per operation and per connection.
How do I test a cold cache honestly?
On a test machine we can restart PostgreSQL and drop the operating system cache before the run, so both caches are empty. On a shared server we cannot do that, so instead we look at the read counts and I/O timings of the first execution, or we run the query for an account whose data nobody has touched recently. auto_explain with buffers logging also captures real cold executions in production.
Is a high hit count a problem if everything is cached?
It can be. Each hit still costs CPU time to find, lock and read the page, so a query that touches 2 million cached pages may take a second even with no I/O at all. Many pages per returned row usually points to a missing or poorly chosen index, so a high hit count is worth fixing even when reads are zero.
7Conclusion
In this answer, we saw that BUFFERS reports the 8 kB pages each node found in shared_buffers, read from outside, dirtied and wrote. The first run read 48,000 pages, about 375 MB, from outside PostgreSQL's cache and the second found all 48,120 cached, so the work was identical and only the source changed. A read can still come from the operating system cache, which track_io_timing helps reveal. We should tune for cold-cache behaviour when the data does not fit in memory and treat fewer pages touched, from better or covering indexes and narrower rows, as the lasting improvement.
Open with
BUFFERS adds, for each plan node, how many 8 kB pages it found in shared_buffers, which is the hit count, how many it had to request from outside, the read count, and how many it dirtied or wrote.
Then, if they want more
In this example, the total is the same on both runs, 48,120 pages, so the query did the same work. The first time, 48,000 pages, roughly 375 megabytes, were not in PostgreSQL's cache; the second time everything was a hit, which is why it was so much faster.
I would be careful about calling those reads disk I/O, because PostgreSQL cannot see the operating system's page cache, and a read may have come from there. Turning on track_io_timing shows how long the reads actually waited, which tells me whether I/O was really the bottleneck. For tuning, I ask whether the production data fits in memory.
If it does not, for example a big events table where each account is read rarely, the cold first run is what users see, and benchmarking the warm second run hides the cost.
So I use the total pages touched as my main measure and try to reduce it with a better index, a covering index for an index-only scan, or narrower rows, because fewer pages is an improvement that holds both cold and warm. Also, newer PostgreSQL versions include buffers in EXPLAIN ANALYZE by default.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain what EXPLAIN (ANALYZE, BUFFERS) adds in PostgreSQL. A query shows shared hit=120...": https://scrivio-interview-library.netlify.app/databases/query-optimization/9fc7ba6ed2/