The Inside Architecture of PostgreSQL: A Guide for Beginners
Tags: postgres, beginners
Published: 2023-04-27 19:12:45

Are you a beginner to PostgreSQL and want to learn about its architecture? This guide is perfect for you! In this blog post, we will provide you with a basic understanding of the process and memory architecture of PostgreSQL, which is essential to understand before proceeding to other chapters. 

## TL;DR 👀

PostgreSQL is a client/server relational database management system with a multi-process architecture. A postgres server process manages a shared memory area, starts background processes, and handles client connection requests. Backend processes handle queries from connected clients, while various background processes perform database management processes. PostgreSQL has three main memory areas: shared buffers, operating system cache, and process-private memory. Shared buffers cache recently accessed data, operating system cache caches the rest of the data, and each backend process has its own private memory. Understanding PostgreSQL's basic architecture is essential for effective usage.

## Process Architecture 

![Process Architecture of PostgreSQL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9diedcy0yx4e5a8gv45.png)


PostgreSQL is a client/server type relational database management system that runs on a single host with a multi-process architecture. A PostgreSQL server consists of a collection of multiple processes that cooperatively manage one database cluster. The following types of processes are included in a PostgreSQL server:

1. Postgres server process
2. Backend processes
3. Various background processes 

**Postgres Server Process**
A postgres server process is the parent of all processes related to a database cluster management. It allocates a shared memory area in memory, starts various background processes, starts replication associated processes and background worker processes if necessary, and waits for connection requests from clients. Whenever receiving a connection request from a client, it starts a backend process. 

**Backend Processes**
A backend process, which is also called postgres, handles all queries issued by one connected client. It communicates with the client through a single TCP connection, and terminates when the client gets disconnected. PostgreSQL allows multiple clients to connect simultaneously, and the configuration parameter max_connections controls the maximum number of clients (default is 100). 

**Background Processes**
Various background processes perform processes of each feature, such as VACUUM and CHECKPOINT processes, for database management. Table 2.1 shows a list of background processes, including background writer, checkpointer, autovacuum launcher, WAL writer, statistics collector, logging collector, and archiver.

## Memory Architecture

![Memory Architecture of PostgreSQL](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xxstlow00tvpxolap0f5.png)

PostgreSQL's memory architecture is designed to enable the efficient use of system memory. It has three main memory areas, namely, shared buffers, the operating system cache, and the process-private memory. 

**Shared Buffers**
The shared buffer is a part of the system memory that PostgreSQL uses to cache recently accessed data. The buffer manager handles buffer allocation and deallocation, as well as the reading and writing of data to and from disk. The size of the shared buffer is set by the shared_buffers configuration parameter. 

**Operating System Cache**
PostgreSQL relies on the operating system cache to cache the rest of the data, including the data that does not fit in the shared buffer. The operating system cache is managed by the operating system kernel, and its size is determined by the amount of available physical memory on the system. 

**Process-Private Memory**
Each backend process has its own private memory, which is allocated from the operating system. The size of the process-private memory is determined by the configuration parameter work_mem. 

## Conclusion
PostgreSQL's process and memory architecture is complex but well-optimized. Understanding the basic architecture is essential for developers who wish to use PostgreSQL effectively. We hope this guide has provided you with a good understanding of the process and memory architecture of PostgreSQL, and you are now ready to proceed to other chapters.