DEV Community

Cover image for PostgreSQL Process Architecture
Maruf13
Maruf13

Posted on

PostgreSQL Process Architecture

In this post, we will discuss the Process Architecture of PostgreSQL. PostgreSQL is a client/server type relational database management system(RDMS) with the multi-process architecture and runs on a single host.

A collection of multiple processes cooperatively managing one database cluster is usually referred to as a 'PostgreSQL server', and its process tree will be show below figure:

Process Tree

✔️ Postgres server process:

  • It is a parent of all processes related to a database cluster management. Previously, it was called ‘postmaster’.

  • By executing the pg_ctl utility with start option, a postgres server process starts up.

Postgres server process

  • Then, 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.

  • After receiving a connection request from a client, it starts a backend process. After that, Backend process handles all queries issued by the connected client.

  • A postgres server process work with one network port, the default port is 5432. Although more than one PostgreSQL server can be run on the same host, then port number can be set as 5432, 5433 etc.

✔️ Backend process:

  • A backend process, which is also called postgres, is started by the postgres server process and handles all queries issued by one connected client.

  • It communicates with the client by a single TCP connection, and terminates when the client gets disconnected.

  • As it is allowed to operate only one database, you have to specify a database you want to use explicitly when connecting to a PostgreSQL server.

  • PostgreSQL allows multiple clients to connect simultaneously; the configuration parameter max_connections controls the maximum number of the clients (default is 100).

  • If many clients repeat the connection and disconnection with a PostgreSQL server, it increases both costs of establishing connections and of creating backend processes because PostgreSQL has not implemented a native connection pooling feature.

  • Such circumstance has a negative effect on the performance of database server. To deal with such a case, a pooling middleware (either pgbouncer or pgpool-II) is usually used.

✔️ Background processes:

Various background processes perform processes of each feature for database management. Below table shows a list of background processes.

Background Process

✔️ Replication associated processes:

  • In the replication associated processes, they perform the streaming replication.

  • In Streaming Replication, three kinds of processes work cooperatively. A walsender process on the primary server sends WAL data to standby server; and then, a walreceiver and a startup processes on standby server receives and replays these data. A walsender and a walreceiver communicate using a single TCP connection.

✔️ Background worker process:

  • It can perform any processing implemented by users or to run user-supplied code in separate processes. Such processes are started, stopped and monitored by postgres, which permits them to have a lifetime linked to the server's status.

  • These processes are attached to PostgreSQL's shared memory area and have the option to connect to databases internally; they can also run multiple transactions serially, just like a regular client-connected server process.

  • Background workers can be initialized at the time that PostgreSQL is started by including the module name in shared_preload_libraries.

  • A module wishing to run a background worker can register it by calling RegisterBackgroundWorker(BackgroundWorker *worker) from its _PG_init() function.

  • Background workers can also be started after the system is up and running by calling RegisterDynamicBackgroundWorker function.

In the following figure, the details of the first three types of processes are shown:

Details
The actual processes of a PostgreSQL server is shown below. In the following example, one postgres server process (pid is 9687), two backend processes (pids are 9697 and 9717) and the several background processes listed here:

postgres> pstree -p 9687
-+= 00001 root /sbin/launchd
 \-+- 09687 postgres /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
   |--= 09688 postgres postgres: logger process     
   |--= 09690 postgres postgres: checkpointer process     
   |--= 09691 postgres postgres: writer process     
   |--= 09692 postgres postgres: wal writer process     
   |--= 09693 postgres postgres: autovacuum launcher process     
   |--= 09694 postgres postgres: archiver process     
   |--= 09695 postgres postgres: stats collector process     
   |--= 09697 postgres postgres: postgres sampledb 192.168.1.100(54924) idle  
   \--= 09717 postgres postgres: postgres sampledb 192.168.1.100(54964) idle in transaction  
Enter fullscreen mode Exit fullscreen mode

✔️ References:

  1. https://age.apache.org/
  2. https://github.com/apache/age
  3. https://www.interdb.jp/pg/index.html

Top comments (0)