2021/4/28 PostgreSQL Buffer Manager
PostgreSQL Buffer Manager
PostgreSQL Buffer Manager Clock-sweep Replacement Strategy
>>
COMP9315 21T1 ♢ PG Buffers ♢ [0/8]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-buffers/slides.html 1/10
2021/4/28 PostgreSQL Buffer Manager
❖ PostgreSQL Buffer Manager PostgreSQL buffer manager:
provides a shared pool of memory buffers for all backends
all access methods get data from disk via buffer manager
Buffers are located in a large region of shared memory. De nitions: src/include/storage/buf*.h Functions: src/backend/storage/buffer/*.c
Buffer code is also used by backends who want a private buffer pool
COMP9315 21T1 ♢ PG Buffers ♢ [1/8]
∧ >>
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-buffers/slides.html 2/10
2021/4/28 PostgreSQL Buffer Manager
<< ∧ >> ❖ PostgreSQL Buffer Manager (cont)
Buffer pool consists of:
BufferDescriptors
shared xed array (size NBuffers) of BufferDesc
BufferBlocks
shared xed array (size NBuffers) of 8KB frames Buffer = index values in above arrays
indexes: global buffers 1..NBuffers; local buffers negative
Size of buffer pool is set in postgresql.conf, e.g. shared_buffers = 16MB # min 128KB, 16*8KB buffers
COMP9315 21T1 ♢ PG Buffers ♢ [2/8]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-buffers/slides.html 3/10
2021/4/28 PostgreSQL Buffer Manager
<< ∧ >> ❖ PostgreSQL Buffer Manager (cont)
COMP9315 21T1 ♢ PG Buffers ♢ [3/8]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-buffers/slides.html 4/10
2021/4/28 PostgreSQL Buffer Manager
<< ∧ >> ❖ PostgreSQL Buffer Manager (cont)
include/storage/buf.h
basic buffer manager data types (e.g. Buffer)
include/storage/bufmgr.h
de nitions for buffer manager function interface (i.e. functions that other parts of the system call to use buffer manager)
include/storage/buf_internals.h
de nitions for buffer manager internals (e.g. BufferDesc)
Code: backend/storage/buffer/*.c Commentary: backend/storage/buffer/README
COMP9315 21T1 ♢ PG Buffers ♢ [4/8]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-buffers/slides.html 5/10
2021/4/28 PostgreSQL Buffer Manager
<< ∧ >> ❖ PostgreSQL Buffer Manager (cont)
De nition of buffer descriptors simpli ed:
typedef struct BufferDesc
{
BufferTag tag; // ID of page contained in buffer int buf_id; // buffer’s index number (from 0)
// state, containing flags, refcount and usagecount
pg_atomic_uint32 state;
int freeNext; // link in freelist chain
…
} BufferDesc;
COMP9315 21T1 ♢ PG Buffers ♢ [5/8]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-buffers/slides.html 6/10
2021/4/28 PostgreSQL Buffer Manager
<< ∧ >> ❖ Clock-sweep Replacement Strategy
PostgreSQL page replacement strategy: clock-sweep treat buffer pool as circular list of buffer slots
NextVictimBuffer (NVB) holds index of next possible evictee
if Buf[NVB] page is pinned or “popular”, leave it usage_count implements “popularity/recency”
measure
incremented on each access to buffer (up to small limit)
decremented each time considered for eviction
else if pin_count = 0 and usage_count = 0 then grab this buffer
increment NextVictimBuffer and try again (wrap at end)
COMP9315 21T1 ♢ PG Buffers ♢ [6/8]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-buffers/slides.html 7/10
2021/4/28 PostgreSQL Buffer Manager
<< ∧ >> ❖ Clock-sweep Replacement Strategy
(cont)
Action of clock-sweep:
COMP9315 21T1 ♢ PG Buffers ♢ [7/8]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-buffers/slides.html 8/10
2021/4/28 PostgreSQL Buffer Manager
<< ∧ ❖ Clock-sweep Replacement Strategy
(cont)
For specialised kinds of access (e.g. sequential scan),
clock-sweep is not the best replacement strategy can allocate a private "buffer ring"
use this buffer ring with alternative replacement strategy
COMP9315 21T1 ♢ PG Buffers ♢ [8/8]
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-buffers/slides.html 9/10
2021/4/28 PostgreSQL Buffer Manager
Produced: 22 Feb 2021
https://cgi.cse.unsw.edu.au/~cs9315/21T1/lectures/pg-buffers/slides.html 10/10