ring buffers
why one seq scan over a huge table doesn't flush everyone else's cache.
- address
- everything.storage.ring_buffers
- part
- part 12, pages and the buffer pool
- chapter
- chapter 10 of 12
- reading time
- 9 min
a big sequential scan doesn't get to use the whole buffer pool. when a table is bigger than a quarter of shared_buffers, its scan gets a ring buffer: 256 KB, 32 frames of its own, reused round and round. the scan reads the whole table through those 32 frames, and everybody else's pages stay where they were.
VACUUM gets a ring too (2 MB by default), and so do bulk writes like COPY (16 MB). they're all the same idea: work that touches every page once shouldn't get to evict pages that are used all the time.
the picture
you know how one big report query can make the whole app slower for a while, even after it's finished? that's what a cache flush feels like: the report read so much that it pushed everything useful out, and every other query now pays to read its pages back in.
think of the reading room again. a researcher wants to look at every book in the archive, once each. if they're allowed to take any desk, they'll eventually clear every desk in the room, and the regulars come back to find their books gone. so the librarian gives them a cart with 32 slots instead: every new book goes onto the cart, replacing the one they're done with. the researcher gets through the whole archive, and the desks never notice.
here's that as a model: a few hot pages used over and over, then one long scan, then the hot pages again. turn the ring on and off:
- empty
start9 empty frames. every page anyone reads or writes has to come through here first.
the model starts on lru on purpose, because a scan barely longer than the pool is the case clock sweep already handles: the hot pages' usage counts outlast it (clock sweep steps through why). a scan many times the size of the pool is a different story, and that needs a real server.
on a real server
the same thing on a real postgres 18.6, with the pool made tiny on purpose (shared_buffers = 2MB, 256 frames) so it fits on screen. a small table, hot, is read twice. then all of events (335 pages, more than the whole pool) is read two ways: a seq scan, then an index scan of every row:
a seq scan of events, 335 pages, more than a quarter of the pool. it gets a ring of 32 frames and goes round in it: hot is untouched.
hot or of events, by its real buffer id. the seq scan of events leaves exactly 32 frames behind it, in one ring. the index scan, which gets no ring, takes 171 frames and leaves nothing of hot.-- shared_buffers = 2MB: 256 frames
SELECT sum(length(kind)) FROM events; -- 335 pages
SELECT count(*) FROM pg_buffercache
WHERE relfilenode = pg_relation_filenode('events');the index scan reads the same table page by page in index order, and an index scan gets no ring. so its pages fight for frames through the clock sweep like everything else, and they win: hot's pages had usage 2, and after enough laps of the hand every one of them was evicted. 171 frames of events and 33 of its index are left when it's done.
the rules
| who | ring | when |
|---|---|---|
| sequential scan | 256 KB (32 frames) | the table is bigger than a quarter of shared_buffers |
| VACUUM, ANALYZE | vacuum_buffer_usage_limit, 2 MB by default | always |
| COPY FROM, CREATE TABLE AS | 16 MB, at most an eighth of shared_buffers | always |
| everything else | none: the clock sweep, the whole pool |
the 256 KB is picked to fit in the CPU's L2 cache, and the quarter is what the heap scan code checks before asking for a ring. the browser engine runs a 128 MB pool, so its quarter is 4,096 pages, and nothing in these datasets comes close. the same seq scan of events there keeps every page:
SHOW shared_buffers;
SELECT sum(length(kind)) FROM events;
SELECT count(*) AS frames FROM pg_buffercache
WHERE relfilenode = pg_relation_filenode('events') AND relforknumber = 0;| shared_bufferstext |
|---|
| 128MB |
| sumint8 |
|---|
| 132117 |
| framesint8 |
|---|
| 335 |
all 335, because 335 is nowhere near 4,096. VACUUM's ring is a setting you can see and change, per command or per session pg 16+:
SHOW vacuum_buffer_usage_limit;
VACUUM (BUFFER_USAGE_LIMIT '256kB') notes;
VACUUM (BUFFER_USAGE_LIMIT 0) notes;| vacuum_buffer_usage_limittext |
|---|
| 2MB |
0 turns the ring off and lets VACUUM use the whole pool: useful when you're vacuuming in a maintenance window and want it done fast, harmful on a busy server. postgres 17 raised the default to 2 MB.
dirty pages in a ring
a ring of 32 frames has a problem when the scan writes. a seq scan that sets hint bits, or an UPDATE driven by one, dirties pages, and a dirty frame can't be reused until its WAL is flushed and the page is written out. the two kinds of ring handle that differently:
- a scan's ring gives up the frame: the dirty page is dropped from the ring and left in the pool for the normal machinery to write, and the ring takes a fresh frame from the clock sweep. so a scan that dirties pages leaks out of its ring a bit.
- VACUUM's ring keeps its dirty frames: it flushes WAL as needed and writes the page itself, then reuses the frame. that's exactly why its ring is bigger, so it doesn't have to wait on WAL flushes all the time.
one level deepersynchronized scans
the same quarter-of-shared_buffers rule switches on one more trick. if a second seq scan of a big table starts while another is already halfway through, it starts reading where the first one is, rides along to the end, then wraps round and reads the part it missed. two scans, one pass of reads. that's synchronize_seqscans, on by default. it's why a seq scan of a big table without ORDER BY can return rows in a different order from run to run, one more reason the order of a result is never guaranteed without ORDER BY. (the captures here turn it off, so they come out the same every time.)
keylines
- a seq scan of a table bigger than a quarter of shared_buffers gets a 256 KB ring of its own.
- VACUUM gets a ring of vacuum_buffer_usage_limit (2 MB), bulk writes 16 MB.
- the ring keeps one-pass work from evicting the pages everyone else uses.
- index scans get no ring.
- the browser's pool is 128 MB, so no dataset here is big enough to trigger one.