DEV Community

Pawan Kukreja
Pawan Kukreja

Posted on • Updated on

[Summary] Chapter#08 "The Internals of PostgreSQL" Buffer Manager (Part-01)

Bugger manager manage data transferred between shared memory and persistent storage and PostgreSQL buffer manager works very efficiently.

Buffer Manager Structure
It comprises a buffer table, buffer descriptors and buffer pool, while buffer pool layers stores data file pages such as table and indexes and freespace maps and visibility maps. It is an array and stores one page of data file and it referred to buffer_id.
It is Comprise of three layer Structure
Buffer Pool
It is an array, each slot stores a data files pages and the indices of the array slots are referred to as buffer_ids.
Buffer Descriptors
It is layer in array, every descriptor has one-to-one buffer pool slot and holds metadata of the stored page in corresponding slot.
Buffer Table
It stores relations between buffer_tags of stored pages and buffer_ids of the descriptors.

Buffer Tag
In PostgreSQL, each page of all data files assigned a unique tag.
Buffer tag comprise of three values:
RelFileNode and fork number to which its page belong, and the block number of its page.

How a Backend Process Reads Pages

  • Backend process send request that includes page's buffer_tag to buffer manager.
  • Buffer Manager return buffer_ID of the slot that stores the requested Page and if it is not present in buffer pool, buffer manager loads the page from persistent storage and return buffer_ID.
  • The backend process accesses the buffer_ID slot.

Page Replacement Algorithm
Page selection algorithm is called page replacement algorithm and selected page is victim page. Buffer manager select one page in buffer pool that will be replaced by requested page in case of page not stored.

Flushing Dirty pages
Buffer manager requires help to perform this task and In PostgreSQL two background processes checkpointer and background writer are responsible for task.

Buffer Table
It is divided into three parts:

  • Hash Function
  • Hash Bucket Slots
  • Data Entries

Buffer Descriptor
It holds metadata of stored page in corresponding buffer pool, and descriptor structure is defined by structure BufferDesc.
Some fields are defined following:
Tag holds buffer_tag of the stored page in the corresponding buffer pool slot.
Buffer_id identifies descriptor
Refcount holds number of postgreSQL process accesses the stored page.
Usage_count holds number of times associated stored pages accessed since load.
Content_lock and io_in_progress_lock that are used to control access.
Flags can hold several states of the associated stored page.
FreeNext is a pointer to the next descriptor to generate freelist.

Reference

Top comments (0)