PostgreSQL Buffer Manager

COMP9315 21T1 ♢ PG Buffers ♢ [0/8]
❖ PostgreSQL Buffer Manager

PostgreSQL buffer manager:

Buffers are located in a large region of shared memory.

Definitions:  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]
❖ PostgreSQL Buffer Manager (cont)

Buffer pool consists of:

BufferDescriptors

BufferBlocks Buffer = index values in above arrays 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]
❖ PostgreSQL Buffer Manager (cont)

[Diagram:Pics/storage/buffer-memory.png]

COMP9315 21T1 ♢ PG Buffers ♢ [3/8]
❖ PostgreSQL Buffer Manager (cont)

include/storage/buf.h

include/storage/bufmgr.h include/storage/buf_internals.h Code: backend/storage/buffer/*.c

Commentary: backend/storage/buffer/README

COMP9315 21T1 ♢ PG Buffers ♢ [4/8]
❖ PostgreSQL Buffer Manager (cont)

Definition of buffer descriptors simplified:

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]
❖ Clock-sweep Replacement Strategy

PostgreSQL page replacement strategy: clock-sweep

COMP9315 21T1 ♢ PG Buffers ♢ [6/8]
❖ Clock-sweep Replacement Strategy (cont)

Action of clock-sweep:

[Diagram:Pics/storage/clock-sweep.png]

COMP9315 21T1 ♢ PG Buffers ♢ [7/8]
❖ Clock-sweep Replacement Strategy (cont)

For specialised kinds of access (e.g. sequential scan),

COMP9315 21T1 ♢ PG Buffers ♢ [8/8]


Produced: 22 Feb 2021