skip to content

the buffer pool

shared_buffers, the buffer table, pins, and what Buffers: shared hit=3 really counted.

address
everything.storage.buffer_pool
part
part 12, pages and the buffer pool
chapter
chapter 8 of 12
reading time
9 min

postgres never reads a page straight into your query. every page anyone reads or writes goes through shared buffers: a fixed array of 8 KB frames in shared memory, 16,384 of them at the default 128 MB. a hash table, the buffer table, says which page is in which frame.

a query that finds its page there scores a hit. one that doesn't has to read it in first, into a frame some other page gets thrown out of. Buffers: shared hit=3 counts pages, not rows.

the picture

you know the Buffers: shared hit=3 read=2 line at the bottom of an EXPLAIN? it's the most useful number in the plan and the least explained. it's a count of trips to the buffer pool, and of the trips that missed.

think of a reading room with a fixed number of desks (the frames). books live in the archive downstairs (the files). at the door there's a card box saying which book is on which desk (the buffer table). you look up your book: if it's on a desk, you sit down and read (a hit). if not, someone fetches it from the archive and puts it on a desk, clearing off whatever book was there (a read, and an eviction). while you're reading, there's a tag on the desk so nobody clears it under you (a pin). and if you scribbled in the book, it has to be copied back to the archive before that desk can be given to anyone else (a dirty page).

here's ada lovelace, customer 42, looked up from an empty pool on a real server, and then looked up again:

shared buffers (pg_buffercache)
+ 89 frames holding other relations, not drawn
page table: page → frame
  • empty

empty the buffer pool. every page of shop is on disk (or in the OS page cache) and nowhere else.

step 1/3
real output, a postgres 18.6 server · cold-warm
step through. the frames that hold shop's pages, and the plan that put them there. the cold lookup reads its pages in (read), the second one finds them (hit). the pips under each frame are its usage count.

the cold lookup reads 3 pages: the root of customers_pkey, a leaf, and the heap page ada is on. two more frames filled up during planning, not execution: the planner reads each index's metapage to learn how tall the tree is, which is why there's a metapage of customers_email_key in there even though the query never uses that index. the second lookup is 3 hits and no reads, and each hit bumped its frame's usage count from 1 to 2.

this capture runs on a native postgres 18.6 with io_method = sync, so a read is a plain read and nothing reads ahead behind its back. (the default is worker; asynchronous I/O is what that changes.)

hit and read, in the browser

the browser engine has a buffer pool too, and every block on this page starts from a fresh copy of the database with nothing in it. so the first time a query runs, everything is a read:

sqldb=shop
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT name, city FROM customers WHERE id = 42;
EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT name, city FROM customers WHERE id = 42;
output, from postgres 18.6
#1 · EXPLAIN
Index Scan using customers_pkey on customers (actual rows=1.00 loops=1) Index Cond: (id = 42) Index Searches: 1 Buffers: shared read=3 Planning: Buffers: shared hit=42 read=4 dirtied=1
#2 · EXPLAIN
Index Scan using customers_pkey on customers (actual rows=1.00 loops=1) Index Cond: (id = 42) Index Searches: 1 Buffers: shared hit=3

same as the real server: read=3, then hit=3. the Planning: line is the planner's own page trips (the catalog, the metapages), and it's gone the second time because the planner caches what it learned.

predict the output
Buffers: shared hit=3. what are the 3?
text
Index Scan using customers_pkey on customers (actual rows=1.00 loops=1)
  Index Cond: (id = 42)
  Index Searches: 1
  Buffers: shared hit=3

a "read" here is not necessarily a disk read. it means "not in shared buffers, so postgres asked the operating system". on a real server the OS has its own page cache underneath, and a read that hits it costs a microsecond, not a trip to the SSD (why pages exist has the numbers measured on one machine). EXPLAIN can't tell the two apart. in the browser, "the OS" is the engine's in-memory file system, so every read is fast.

looking inside the pool

pg_buffercache shows every frame: which relation and block it holds, its usage count, whether it's dirty, and how many backends have it pinned. after the lookup above:

sqldb=shop · ext=pg_buffercache · session pool
SELECT name, city FROM customers WHERE id = 42;
SELECT c.relname, b.relblocknumber AS block, b.usagecount, b.isdirty, b.pinning_backends
FROM pg_buffercache b
JOIN pg_class c ON c.relfilenode = b.relfilenode
WHERE c.relname IN ('customers', 'customers_pkey', 'customers_email_key')
  AND b.relforknumber = 0
ORDER BY c.relname, block;
output, from postgres 18.6
#1 · SELECT 1
nametextcitytext
Ada LovelaceLondon
(1 row)
#2 · SELECT 5
relnamenameblockint8usagecountint2isdirtyboolpinning_backendsint4
customers01f0
customers_email_key01f0
customers_pkey01f0
customers_pkey11f0
customers_pkey31f0
(5 rows)

four pages of two indexes and one heap page, out of a pool of:

sqldb=shop · ext=pg_buffercache · session pool
continues session pool (1 earlier block run first)
SHOW shared_buffers;
SELECT buffers_used, buffers_unused, buffers_dirty, buffers_pinned
FROM pg_buffercache_summary();
output, from postgres 18.6
#1 · SHOW
shared_bufferstext
128MB
(1 row)
#2 · SELECT 1
buffers_usedint4buffers_unusedint4buffers_dirtyint4buffers_pinnedint4
29816086550
(1 row)

16,384 frames, and only a few hundred in use: the catalog pages every query needs, and what the lookup brought in. a real server's pool is usually full after a while, which is when the question of what to throw out starts to matter. that's the clock sweep.

the lookup, step by step

every page access in postgres goes through one function, ReadBuffer. it does the same few steps every time:

hover a stage.

hover a stage. the miss path is where the cost is: picking a victim, maybe writing it out, and the read.
  • ├── buffer table: (relation, fork, block) → buffer ida hash, in 128 partitions
  • ├── buffer descriptors: 16,384 of themtag, pins, usage, flags
  • └── buffer blocks: 16,384 frames of 8 KBthe pages
the three arrays in shared memory that make up the buffer pool, sized for the default 128 MB. a lookup goes table → descriptor → page.

the descriptor of each frame (its tag, pin count, usage count, dirty and valid flags) is a small struct packed into a single 32-bit state word that's updated with atomic operations, so the common case, pin and unpin, takes no lock at all.

pins

a pin is a promise: "I'm looking at this frame, don't reuse it". it's held for as long as a backend has a pointer into the page, usually microseconds. a cursor that stops halfway through a scan holds one for as long as it stays open:

sqldb=events · ext=pg_buffercache
BEGIN;
DECLARE c CURSOR FOR SELECT id FROM events;
FETCH 1 FROM c;
SELECT b.relblocknumber AS block, b.pinning_backends, b.usagecount
FROM pg_buffercache b
WHERE b.relfilenode = pg_relation_filenode('events') AND b.pinning_backends > 0;
COMMIT;
output, from postgres 18.6
#1 · BEGIN
#2 · DECLARE CURSOR
#3 · FETCH 1
idint8
1
(1 row)
#4 · SELECT 1
blockint8pinning_backendsint4usagecountint2
011
(1 row)
#5 · COMMIT

block 0 of events, pinned by one backend: this one, parked on the first row. a pinned frame can't be evicted, which is also why VACUUM sometimes skips a page and says so: someone had it pinned.

dirty pages

writing to a page changes the copy in shared buffers, and only that copy. the frame is now dirty: it differs from the file. nothing is written to the table's file at commit (the WAL is, which is what makes that safe):

sqldb=shop · ext=pg_buffercache · session dirty
UPDATE customers SET city = 'Paris' WHERE id = 42;
SELECT CASE b.relforknumber WHEN 0 THEN 'main' WHEN 1 THEN 'fsm' ELSE 'vm' END AS fork,
       b.relblocknumber AS block, b.isdirty
FROM pg_buffercache b
WHERE b.relfilenode = pg_relation_filenode('customers') AND b.isdirty
ORDER BY fork, block;
CHECKPOINT;
SELECT count(*) FILTER (WHERE b.isdirty) AS dirty, count(*) AS frames
FROM pg_buffercache b
WHERE b.relfilenode = pg_relation_filenode('customers');
output, from postgres 18.6
#1 · UPDATE 1
#2 · SELECT 3
forktextblockint8isdirtybool
main0t
main23t
vm0t
(3 rows)
#3 · CHECKPOINT
#4 · SELECT 1
dirtyint8framesint8
06
(1 row)

three dirty frames from one UPDATE: page 0, where ada's old version got its xmax; page 23, where the new version went (page 0 was full); and the visibility map page, because page 0 just lost its all-visible bit. the CHECKPOINT wrote all of them out, and the frames are clean again, still holding their pages. who writes dirty pages back, and when, is its own chapter.

keylines

  • every page access goes through shared buffers: 8 KB frames in shared memory, 16,384 at the default 128 MB.
  • the buffer table maps (relation, fork, block) to a frame.
  • hit = the page was already in a frame. read = it had to come from the OS, which may still be its page cache.
  • a pin keeps a frame from being evicted while someone uses it.
  • a write only dirties the frame. the file catches up later.