Process And Memory Architecture
Tags: apache, postgres, opensource, agedb
Published: 2023-06-09 23:38:54

## 2.1. Process Architecture
PostgreSQL is a relational database management system with a client/server architecture. It operates on a single host and utilizes a multi-process structure. 
A PostgreSQL server comprises several processes responsible for managing a single database cluster. These processes can be classified into the following types:

- Postgres server processes.
- Backend processes.
- Background processes.
- Replication associated processes.
- Background worker processes.

## 2.1.1. Postgres Server Process
In a PostgreSQL server, the postgres server process serves as the parent of all other processes. It is initiated by executing the pg_ctl utility with the start option. The postgres process allocates a shared memory area in memory and launches various background processes, replication-related processes, and background worker processes. It then awaits incoming connection requests from clients.

Whenever a connection request is received from a client, the postgres server process spawns a backend process. This backend process is responsible for handling all queries issued by the connected client, ensuring the execution of the requested operations.

## 2.1.2. Backend Processes
Backend processes are started by the postgres server process and handle all queries issued by one connected client.These backend processes communicate with the client through a single TCP connection and remain active until the client disconnects. PostgreSQL supports multiple simultaneous client connections, and the maximum number of clients is controlled by the configuration parameter max_connections.

## 2.1.3. Background Processes
PostgreSQL employs a multi-process architecture that incorporates various background processes responsible for specific tasks in database management. These processes include the background writer, checkpointer, autovacuum launcher, WAL writer, statistics collector, logging collector, and archiver.

Each of these processes performs distinct roles within the PostgreSQL server. For instance, the background writer is responsible for writing dirty pages from the shared buffer pool to persistent storage. The checkpointer performs checkpoint processes to ensure data consistency. The autovacuum launcher invokes autovacuum-worker processes at regular intervals to manage data maintenance tasks. The WAL writer handles the writing and flushing of Write-Ahead Log (WAL) data to persistent storage.

Additionally, the statistics collector gathers and maintains statistical information about the database, while the logging collector manages database log files. The archiver is responsible for archiving PostgreSQL transaction log files.

Together, these background processes enable efficient database management in PostgreSQL. They work collaboratively to handle client queries, ensure data integrity, perform maintenance tasks, and facilitate background processing such as replication.

## 2.2. Memory Architecture
Memory architecture of PostgreSQL can be divided into two broad categories: Local Memory Area and Shared Memory Area.

## 2.2.1. Local Memory
Every backend process in PostgreSQL assigns a local memory area for query processing, which is further divided into multiple sub-areas with either fixed or variable sizes. Among these sub-areas, the significant ones include work_mem, maintenance_work_mem, and temp_buffers.
The work_mem sub-area is allocated for sorting tuples during query execution. The maintenance_work_mem sub-area is utilized for maintenance operations, such as index creation or bulk data modifications. Lastly, the temp_buffers sub-area is dedicated to storing temporary tables.
Each of these sub-areas serves a specific purpose in query processing and resource management within PostgreSQL's backend processes.

## 2.2.2. Shared Memory
In this case, the PostgreSQL server allocates a shared memory area that is utilized by all processes. This shared memory area is further divided into several sub-areas with fixed sizes, each serving a specific purpose.

Among the major sub-areas, the shared buffer pool is crucial. It is responsible for loading pages from tables and indexes in persistent storage and enables direct operations on them within PostgreSQL. The WAL (Write-Ahead Log) buffer acts as a buffer for storing WAL data before writing it to persistent storage, ensuring data durability in the event of server failures. Additionally, the commit log maintains the state of all transactions for the Concurrency Control mechanism.

Apart from these key sub-areas, PostgreSQL also allocates sub-areas for various access control mechanisms, background processes, and transaction processing. These include semaphores, lightweight locks, shared and exclusive locks, checkpointer, autovacuum, savepoints, and two-phase commit. All of these sub-areas play vital roles in facilitating efficient query processing, maintenance operations, and transaction handling within PostgreSQL.

Collectively, these shared memory sub-areas are essential for the smooth functioning of PostgreSQL, enabling robust database operations and ensuring data consistency and reliability.

## Conclusion
Hopefully this article has covered the core concepts of Processes and Memory Architecture of PostgreSQL.

## References 
[Chapter-2 The Internal of PostgreSQL](https://www.interdb.jp/pg/pgsql02.html)