skip to content

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:

policy =
frames =
workload =
ring =
a·0a·1a·2a·0*a·1a·2a·0a·1a·2e·0e·1e·2e·3e·4e·5e·6e·7e·8e·9e·10a·0a·1a·2
page table: page → frame
  • empty
hits
0
reads
0
evicted
0
written back
0

start9 empty frames. every page anyone reads or writes has to come through here first.

hitread from diskdirtypinnedempty frame
request 1/24
model of textbook lru replacement, not what postgres does
step to the end with the ring off, then on. with lru, the scan's pages push the hot ones out and the last three requests miss; with the ring, the scan recycles its own two frames and those requests are hits. switch to clock sweep: a scan this short can't get through the hot pages' usage counts, ring or not.

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:

after =
T1SELECT sum(length(kind)) FROM events
hot20 frames
events32 frames
anything else (catalog, indexes)120

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.

a frame holding something else, or nothing
real output, a postgres 18.6 server · ring
switch between the three moments. each square is one of the 256 frames, filled if it holds a page of 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.
predict the output
the seq scan read all 335 pages of events through the pool. how many of them are still in it afterwards?
sql
-- 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.

after the seq scan: events32
after the seq scan: hot20
after the index scan: events171
after the index scan: hot0
real output, a postgres 18.6 server · ring
frames left holding each table, on the 256-frame server. the same 335 pages of events, read two ways.

the rules

whoringwhen
sequential scan256 KB (32 frames)the table is bigger than a quarter of shared_buffers
VACUUM, ANALYZEvacuum_buffer_usage_limit, 2 MB by defaultalways
COPY FROM, CREATE TABLE AS16 MB, at most an eighth of shared_buffersalways
everything elsenone: 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:

sqldb=events · ext=pg_buffercache
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;
output, from postgres 18.6
#1 · SHOW
shared_bufferstext
128MB
(1 row)
#2 · SELECT 1
sumint8
132117
(1 row)
#3 · SELECT 1
framesint8
335
(1 row)

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+:

sqldb=lab
SHOW vacuum_buffer_usage_limit;
VACUUM (BUFFER_USAGE_LIMIT '256kB') notes;
VACUUM (BUFFER_USAGE_LIMIT 0) notes;
output, from postgres 18.6
#1 · SHOW
vacuum_buffer_usage_limittext
2MB
(1 row)
#2 · VACUUM
#3 · VACUUM

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.