clock sweep
usage counts, a hand that goes round, and why postgres doesn't do plain LRU.
- address
- everything.storage.clock_sweep
- part
- part 12, pages and the buffer pool
- chapter
- chapter 9 of 12
- reading time
- 9 min
when a page has to come in and every frame is taken, something has to go. postgres picks it with a clock sweep: every frame has a usage count from 0 to 5, and every hit adds one. a hand goes round the frames looking for a victim. each frame it passes loses one, and the first frame it finds at 0 is reused.
pages used often keep getting topped up and survive lap after lap. a page used once is gone after one lap. it's most of what LRU gives you, at almost none of the cost.
the picture
you know how a cache is supposed to throw out the least recently used item? that needs a list of every page in the order it was last touched, reshuffled on every single access. postgres doesn't keep one.
think of a cleaner walking round a circle of desks, over and over. every time someone uses a desk, they add a tally mark to its card, up to five. every time the cleaner walks past a desk, they rub one mark out. a desk with no marks left gets cleared for the next person who needs one. busy desks get marked faster than the cleaner can rub them out. a desk somebody used once and walked away from is cleared the second time round.
that's the whole algorithm. request pages and watch it, one tick at a time:
6 empty frames, the hand at frame 0. request pages and watch where they land.
three things to notice. in "one hot page", t·0 stays in its frame for the whole run while everything else churns past it: every time the hand gets near it, it's been topped up again. in "a scan, twice", nothing survives: eight pages through six frames, each used once, and the second pass misses on every single page, because each page was thrown out just before it was needed again. and the hand only moves on a miss. hits never touch it.
real usage counts
the counts are real, and pg_buffercache shows them. the lab's accounts table is one page. read it once, three times, then seven:
SELECT count(*) FROM accounts;
SELECT usagecount FROM pg_buffercache
WHERE relfilenode = pg_relation_filenode('accounts') AND relforknumber = 0;
SELECT count(*) FROM accounts;
SELECT count(*) FROM accounts;
SELECT usagecount FROM pg_buffercache
WHERE relfilenode = pg_relation_filenode('accounts') AND relforknumber = 0;
SELECT count(*) FROM accounts;
SELECT count(*) FROM accounts;
SELECT count(*) FROM accounts;
SELECT count(*) FROM accounts;
SELECT usagecount FROM pg_buffercache
WHERE relfilenode = pg_relation_filenode('accounts') AND relforknumber = 0;| countint8 |
|---|
| 3 |
| usagecountint2 |
|---|
| 1 |
| countint8 |
|---|
| 3 |
| countint8 |
|---|
| 3 |
| usagecountint2 |
|---|
| 3 |
| countint8 |
|---|
| 3 |
| countint8 |
|---|
| 3 |
| countint8 |
|---|
| 3 |
| countint8 |
|---|
| 3 |
| usagecountint2 |
|---|
| 5 |
the same seven scans, captured step by step on the same engine:
- empty
start cold: drop the lab's accounts page out of shared buffers.
1, then 3, then 5, and there it stops. every scan pins the page once and bumps its count by one, and 5 is the ceiling (BM_MAX_USAGE_COUNT in the source).
page A: 50 hits page B: 5 hits
the hand comes round, again and again...across the whole pool, pg_buffercache_usage_counts() gives the distribution. after a few queries on shop:
SELECT count(*) FROM orders;
SELECT count(*) FROM orders;
SELECT name, city FROM customers WHERE id = 42;
SELECT usage_count, buffers, dirty, pinned
FROM pg_buffercache_usage_counts()
ORDER BY usage_count;| countint8 |
|---|
| 10000 |
| countint8 |
|---|
| 10000 |
| nametext | citytext |
|---|---|
| Ada Lovelace | London |
| usage_countint4 | buffersint4 | dirtyint4 | pinnedint4 |
|---|---|---|---|
| 0 | 15998 | 0 | 0 |
| 1 | 129 | 9 | 0 |
| 2 | 116 | 4 | 0 |
| 3 | 17 | 3 | 0 |
| 4 | 13 | 5 | 0 |
| 5 | 111 | 32 | 0 |
most of the 16,384 frames are at 0 because they're empty: the browser's pool starts out empty for every block. on a busy server that's been up a while, the pool is full, and the shape of this table tells you how much pressure it's under. lots of frames at 5 means a hot working set that fits; most frames at 0 or 1 means pages are being cycled through faster than they're reused.
why not plain LRU
because of what a hit costs. with an LRU list, every page access has to move that page to the front of a shared list, and a shared list needs a lock. a busy server does millions of buffer accesses a second across all its backends, and they'd all queue on that one lock. postgres had exactly that problem, and postgres 8.1 replaced its buffer replacement with the clock sweep to get rid of it. now a hit is one atomic add on the frame's state word. no lock, no list.
and clock sweep isn't worse at choosing, either. plain LRU has a known weakness: one big scan touches every page once, which makes every scanned page the "most recent", and flushes out the hot pages that were actually useful. usage counts are a little memory of frequency, so a page used five times outlives one used once. here's the same request stream under three policies:
- empty
start9 empty frames. every page anyone reads or writes has to come through here first.
with 9 frames, clock sweep keeps all three hot pages through the scan: they had usage to spare, and every scanned page arrived with just 1, so the hand took scan pages first. lru and fifo lose every hot page, and finish with 6 hits to clock sweep's 9. but make the scan long enough, many times the size of the pool, and even usage 5 runs out after five laps. that's the job of ring buffers: a big scan recycles a few frames of its own instead of borrowing everyone's.
the hand's real job
in the model, the hand only moves when a backend needs a frame. in postgres, it's the same: there's one shared hand, nextVictimBuffer, and whichever backend misses advances it. two more things happen on a real server:
- if the frame the hand picks is dirty, the backend has to write it out before reusing it. that's a write in the middle of somebody's SELECT. the background writer exists to get there first: it looks ahead of the hand and writes out dirty pages that are about to be reached, so backends usually find clean ones. that's writing dirty pages back.
- a frame that's pinned is skipped, whatever its count. if every frame is pinned, the sweep gives up with an error. that takes a pool far too small for the number of connections.
keylines
- every frame has a usage count, 0 to 5. each hit adds one.
- on a miss, the hand goes round: pinned frames are skipped, others lose one, the first at 0 is the victim.
- pages used often survive many laps, pages used once survive one.
- the count caps at 5, so old popularity fades.
- postgres uses it since 8.1 because a hit is one atomic add, not a reordering of a locked list.