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:
- empty
empty the buffer pool. every page of shop is on disk (or in the OS page cache) and nowhere else.
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:
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;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.
Index Scan using customers_pkey on customers (actual rows=1.00 loops=1)
Index Cond: (id = 42)
Index Searches: 1
Buffers: shared hit=3a "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:
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;| nametext | citytext |
|---|---|
| Ada Lovelace | London |
| relnamename | blockint8 | usagecountint2 | isdirtybool | pinning_backendsint4 |
|---|---|---|---|---|
| customers | 0 | 1 | f | 0 |
| customers_email_key | 0 | 1 | f | 0 |
| customers_pkey | 0 | 1 | f | 0 |
| customers_pkey | 1 | 1 | f | 0 |
| customers_pkey | 3 | 1 | f | 0 |
four pages of two indexes and one heap page, out of a pool of:
SHOW shared_buffers;
SELECT buffers_used, buffers_unused, buffers_dirty, buffers_pinned
FROM pg_buffercache_summary();| shared_bufferstext |
|---|
| 128MB |
| buffers_usedint4 | buffers_unusedint4 | buffers_dirtyint4 | buffers_pinnedint4 |
|---|---|---|---|
| 298 | 16086 | 55 | 0 |
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.
- ├── 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 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:
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;| idint8 |
|---|
| 1 |
| blockint8 | pinning_backendsint4 | usagecountint2 |
|---|---|---|
| 0 | 1 | 1 |
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):
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');| forktext | blockint8 | isdirtybool |
|---|---|---|
| main | 0 | t |
| main | 23 | t |
| vm | 0 | t |
| dirtyint8 | framesint8 |
|---|---|
| 0 | 6 |
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.