DEV Community


Posted on

Memory behind Postgres

Without diving a lot into the intricacies of the underlying architecture of Postgres, we'll be looking at it from the top.

Memory architecture in Postgres is classified into two broad categories:

  1. Local Memory Area
  2. Shared Memory Area

As the name suggests, the local memory area is used by backend processes for their use. While the shared memory area is shared between all the processes.

Now, let's talk about the local memory area.

Local Memory Area

Each backend process allocates a memory area for query processing.

These areas are further divided where their sizes are either fixed or variable. Some areas are discussed below -

Sub-Area Description
work_mem The executor uses this area for sorting tuples by ORDER BY and DISTINCT operations. It also uses it for joining tables by merge-join and hash-join operations.
maintenance_work_mem This parameter is used for some kinds of maintenance operations (VACUUM, REINDEX).
temp_buffers The executor uses this area for storing temporary tables.

Shared Memory Area

A shared memory area is allocated by a PostgreSQL server when it starts up.

These areas are also further divided into various sub-sections that are further discussed below -

Sub-Area Description
Shared buffer pool PostgreSQL loads pages within tables and indexes from persistent storage to a shared buffer pool, and then operates on them directly. In itself, this is a huge topic and can become very complicated if we delve into all the types and sizes of the buffers.
WAL buffer PostgreSQL supports the WAL (Write ahead log) mechanism to ensure that no data is lost after a server failure. WAL data is a transaction log in PostgreSQL and the WAL buffer is a buffering area of the WAL data before writing it to a persistent storage.
Commit log The commit log (CLOG) keeps the states of all transactions, and is part of the concurrency control mechanism. The commit log is allocated to the shared memory and used throughout transaction processing.

These are not the only areas where the memory is allocated, memory is allocated for the following as well -

  • Sub-areas for the various access control mechanisms
  • Sub-areas for the various background processes
  • Sub-areas for transaction processing, and others

This post was just a beginner-friendly way to introduce to the readers about the memory architecture of the Postgres server. Also this, in no way is complete and the following could be referred to for further reading -

Until then, thank you for your time and see ya!

Top comments (0)