Scrivio Interview library
InternalsIntermediate7 min read · updated 12 November 2026

A reporting query on PostgreSQL takes 40 seconds the first time and 3 seconds when repeated. EXPLAIN (ANALYZE, BUFFERS) shows "shared hit=120 read=480000" on the first run. Explain the numbers, the role of the OS page cache, and how PostgreSQL and InnoDB protect the cache from large scans.

shared hit=120 means 120 pages were found in shared_buffers; read=480000 means 480,000 pages were not there and were requested from the operating system, about 3.7 GB at 8 KB each. On the first run most of those reads went to disk; on the second run they came from the OS page cache, which PostgreSQL relies on as a second layer of caching, so the query fell from 40 to 3 seconds even though shared_buffers did not keep them. Both PostgreSQL, with a small ring buffer for large scans, and InnoDB, with midpoint insertion into its LRU list, stop one big scan from flushing the hot pages out of the cache.

Why this question is asked

A query that is slow once and fast afterwards confuses people, because the plan is identical both times. The difference is where the pages came from, and EXPLAIN (ANALYZE, BUFFERS) is the tool that shows it. Interviewers ask this to check that we can read buffer counts, understand PostgreSQL's two caching layers, and know how databases protect their caches from reporting queries. In this answer, we will decode the numbers, follow a page read through shared_buffers and the OS page cache, see the scan protections in PostgreSQL and InnoDB, and then list what we would actually do about the report.

1Reading the Buffer Numbers

With the BUFFERS option, every plan node reports how many pages it touched and where it got them. shared hit counts pages found in shared_buffers, PostgreSQL's own cache. shared read counts pages that were not there, so PostgreSQL asked the operating system for them. There are also dirtied and written counts, and local and temp counts for temporary tables and sorts that spill to disk.

Java
EXPLAIN (ANALYZE, BUFFERS) SELECT region, sum(amount) FROM sales WHERE sold_at >= '2026-01-01' GROUP BY region;
-- HashAggregate (actual time=39812..39813 rows=12 loops=1)
-- Buffers: shared hit=120 read=480000
-- -> Seq Scan on sales (actual time=0.4..36540 rows=61000000 loops=1)
-- Filter: (sold_at >= '2026-01-01')
-- 480000 pages x 8 KB = 3,840,000 KB, about 3.7 GiB read from outside shared_buffers
-- 120 pages, about 1 MB, were already in shared_buffers

Notice that read does not mean read from disk. It means read from outside shared_buffers. Whether the operating system served the page from its own memory or from storage is invisible in these counts, and that is exactly the detail that explains our two timings.

2PostgreSQL's Two Layers of Cache

PostgreSQL reads data files through the normal file system, so every page it reads also lands in the operating system's page cache, the memory Linux uses to cache recently read file blocks. This is called double buffering: the same page can be in shared_buffers and in the OS cache. PostgreSQL is designed to work with it, which is why the usual starting point for shared_buffers is around 25 percent of RAM and not 80 percent; the rest of memory works as a second, larger cache.

On the first run, the 480,000 pages were neither in shared_buffers nor in the OS cache, so they came from storage at roughly 100 MB per second, and the query took 40 seconds. On the second run, shared_buffers still did not hold them, for a reason we will see next, but the OS page cache did. Reading 3.7 GB from memory takes a couple of seconds, so the query took 3. The plan output would again say read=480000, and only the timing differs.

Step throughWhere a page comes from on each run
Queryshared_buffersOS page cacheDisk
run 1: need page 512
miss, read() from file
miss, read block
slow: counted as read
run 2: need page 512
miss again (ring buffer reused)
hit in OS memory: counted as read, but fast

7 messages in order. Play them, or step through them one at a time.

The second run is fast because the OS cache kept the pages, not shared_buffers.

3How PostgreSQL Protects shared_buffers From Big Scans

If a sequential scan of 3.7 GB pushed every page through shared_buffers normally, it would evict the hot pages of the OLTP workload, the customer and order indexes that every request needs, and the whole application would slow down after each report. PostgreSQL prevents this with a buffer access strategy. When a sequential scan reads a table larger than a quarter of shared_buffers, it uses a small ring of buffers, 256 KB, and reuses that ring over and over instead of taking new buffers. Similar rings exist for VACUUM and for bulk writes such as COPY.

So the big scan only ever occupies a tiny corner of shared_buffers. That is why shared hit stays small and why the second run still shows read=480000: by design, those pages were never allowed to stay. The OS page cache has no such protection, which is the price of the double buffering design.

4How InnoDB Protects Its Buffer Pool

InnoDB does not rely on the OS cache, so it needs the protection inside its own buffer pool. It uses an LRU list, least recently used, split into two parts: a young sublist of hot pages and an old sublist, by default the last 37 percent of the list (innodb_old_blocks_pct). A newly read page is inserted at the midpoint, the head of the old sublist, not at the head of the whole list.

A page moves into the young sublist only if it is accessed again after it has been in the pool for innodb_old_blocks_time, 1000 milliseconds by default. A table scan touches each page several times within a few milliseconds and then never again, so its pages stay in the old part and are the first to be evicted. Hot OLTP pages, touched again and again over minutes, stay young.

Cache protection against large scans
PostgreSQLInnoDB
main cacheshared_buffers (~25% of RAM to start)buffer pool (often most of RAM)
second cacheOS page cacheusually none (direct I/O)
scan protection256 KB ring buffer for big seq scansmidpoint insertion into old sublist
promotion ruleusage count in clock sweepre-access after innodb_old_blocks_time
visible inEXPLAIN (ANALYZE, BUFFERS)SHOW ENGINE INNODB STATUS, metrics

5What We Would Do About the Report

The real fix is to read fewer pages. A report that scans 61 million rows to aggregate one year can often use an index on sold_at, a partitioned table where only the 2026 partitions are scanned, or a pre-aggregated summary table refreshed every night. If the query legitimately needs all those pages, we give it memory: more RAM for the OS cache, or a dedicated reporting replica so the primary's caches serve only OLTP traffic.

After a restart or failover, caches are cold and the first runs are slow again. The pg_prewarm extension can load chosen tables or indexes into shared_buffers, and with its autoprewarm worker PostgreSQL can reload the pages that were cached before the restart. InnoDB has a similar feature that dumps and reloads the list of buffer pool pages at shutdown and startup.

6Follow-up questions

What does track_io_timing add to EXPLAIN output?

With track_io_timing on, EXPLAIN (ANALYZE, BUFFERS) also reports I/O Timings, the time spent waiting for reads and writes of data pages. That finally separates the two runs: on the first one most of the 40 seconds is read time, while on the second the read time is small even though the read count is the same, proving the pages came from the OS cache. It adds a small overhead from timing calls, which pg_test_timing can measure on the server.

Would raising shared_buffers to 75 percent of RAM fix it?

Probably not, and it may hurt. The ring buffer would still keep the big scan out of shared_buffers, and less memory would be left for the OS cache that made the second run fast. Changes to shared_buffers should be tested against the real workload.

How do we see cache usage per table in PostgreSQL?

The pg_buffercache extension shows which relations occupy shared_buffers, and pg_statio_user_tables shows heap and index blocks hit and read per table, which helps find tables that keep missing the cache.

7Conclusion

In this answer, we decoded shared hit=120 read=480000: 120 pages came from shared_buffers and 480,000 pages, about 3.7 GB, came from outside it. The first run read them from disk and the second from the OS page cache, PostgreSQL's second caching layer, which is why the time dropped from 40 to 3 seconds. PostgreSQL keeps large scans in a 256 KB ring buffer and InnoDB inserts new pages at the midpoint of its LRU list, so reports do not evict hot pages. To fix the report we read fewer pages with indexes, partitions or summaries, add memory, prewarm after restarts or move it to a replica.

What to say in the interviewAbout 90 seconds aloud

Open with

The buffers line says the query touched 480,120 pages. 120 were hits, already in shared_buffers. 480,000 were reads, meaning PostgreSQL had to ask the operating system for them; at 8 KB per page that is about 3.7 GB.

Then, if they want more

Read does not mean disk, though. PostgreSQL goes through the file system, so the OS page cache is a second cache layer, which is why shared_buffers is usually started around a quarter of RAM. On the first run those pages came from storage, which is the 40 seconds.

On the second run they were still not in shared_buffers, because a sequential scan of a table bigger than a quarter of shared_buffers uses a 256 KB ring buffer and reuses it, so the scan cannot evict the hot OLTP pages.

But the OS cache kept them, so the same 480,000 reads came from memory in 3 seconds. track_io_timing would show that directly as much lower read time.

InnoDB, which mostly bypasses the OS cache, protects its buffer pool differently: new pages enter the LRU list at the midpoint, in the old sublist, and only move to the young part if touched again after innodb_old_blocks_time, one second by default, so a scan's pages are evicted first.

To fix the report I would reduce pages read with an index, partition pruning or a summary table, consider more memory, use pg_prewarm after restarts, or move reporting to a replica.

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.
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 "A reporting query on PostgreSQL takes 40 seconds the first time and 3 seconds when...": https://scrivio-interview-library.netlify.app/databases/storage-internals/a04564c0cf/