Chapter 2-Process And Memory Architecture
Tags: postgres, database
Published: 2023-04-23 12:13:40

Here is a summary of chapter 2 of the book The Internals of PostgreSQL.

## Process Architecture

PostgreSQL is a client/server type relational database management system with the multi-process architecture and runs on a single host.
A **PostgreSQL server** is a collection of many processes that are managing one database cluster. These processes ae of following types.

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

### Postgres server process

It’s the parent of all in a PostgreSQL server and was called **postmaster** in the earlier version. It starts up by executing pg_ctl utility with start option. After starting it allocates a shared memory area in memory for all processes. These processes are various background processes, replication associated processes and background worker processes. After starting these processes, it waits for the request from the client. Once a request is received from the client it starts a backend process that handles all the queries issued by the client.
A server process listens to one network port (default 5432).  We can run more than one PostgreSQL server on a host, but the running port number should be different.

### Backend Processes

A backend process is started by Postgres server process that handles all the connected client queries.
It uses **TCP connection** to during communication with the client and terminates the connection once the client got disconnected. We must explicitly specify a database during our connection to a PostgreSQL server. Multiple clients can connect to PostgreSQL simultaneously. But if so many clients such as WEB applications frequently connect and disconnect from a PostgreSQL server, it can be costly in term of both connection establishment and backend processes. Unfortunately, PostgreSQL doesn’t have a built-in connection pooling feature to manage this issue. To improve performance and minimize cost, a pooling middleware such as pgbouncer or pgpool-II is used.

### Background processes

 There are so many backend processes. Each is specific for its own task depending on its individual specific feature and PostgreSQL internals. These are background writer, checkpointer, WAL-writer and many others. These all will be discussed in details in the coming chapters.

_Details of **replication associated processes** and **background worker process** will be discussed in the summary of coming chapters._

## Memory Architecture
It can be classified into two main categories.

* Local memory area
* Shared Memory area

### Local Memory Area

Each backend process allocates a local memory area for query processing. Each area is divided into either fixed or variable size sub-areas. These sub areas are work_mem, maintenance_work_mem, temp_buffers

### Shared Memory Area

This shared memory area is allocated by PostgreSQL server when it starts up. Like local memory area it is also divided into sub-areas of fixed size. These sub areas are shared buffer pool, WAL buffer, commit log.


*There are also some other memory allocation areas allocated by PostgreSQL server. These are following.
Sub-areas for the various access control mechanisms , background processes and transaction processing.*

For more details you can visit the site and read more about this.

## References:
[https://www.interdb.jp/pg/pgsql02.html](https://www.interdb.jp/pg/pgsql02.html)