The Internals of PostgreSQL - Chapter 2 Process and Memory Architecture
The Internals of PostgreSQL - Chapter 2 Process and Memory Architecture
Chapter 2
Process and Memory Architecture
I
n this chapter, the process architecture and memory architecture in PostgreSQL are summarized to help to read the subsequent chapters. If you are already familiar
with them, you may skip over this chapter.
A collection of multiple processes cooperatively managing one database cluster is usually referred to as a 'PostgreSQL server', and it contains the following types of
processes:
A postgres server process is a parent of all processes related to a database cluster management.
Each backend process handles all queries and statements issued by a connected client.
Various background processes perform processes of each feature (e.g., VACUUM and CHECKPOINT processes) for database management.
In the replication associated processes, they perform the streaming replication. The details are described in Chapter 11.
In the background worker process supported from version 9.3, it can perform any processing implemented by users. As not going into detail here, refer to the
official document.
In the following subsections, the details of the first three types of processes are described.
http://www.interdb.jp/pg/pgsql02.html 1/3
2017/7/6 The Internals of PostgreSQL : Chapter 2 Process and Memory Architecture
This figure shows processes of a PostgreSQL server: a postgres server process, two backend processes, seven background processes, and two client processes. The database
cluster, the shared memory, and two client processes are also illustrated.
By executing the pg_ctl utility with start option, a postgres server process starts up. 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. Whenever receiving a
connection request from a client, it starts a backend process. (And then, the started backend process handles all queries issued by the connected client.)
A postgres server process listens to one network port, the default port is 5432. Although more than one PostgreSQL server can be run on the same host, each server
should be set to listen to different port number in each other, e.g., 5432, 5433, etc.
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 such as WEB applications frequently 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 In this process, dirty pages on the shared buffer pool are written to a persistent storage (e.g., HDD, SSD) on a regular basis gradually. (In Section 8.6
writer version 9.1 or earlier, it was also responsible for checkpoint process.)
checkpointer In this process in version 9.2 or later, checkpoint process is performed. Section 8.6,
Section 9.7
autovacuum The autovacuum-worker processes are invoked for vacuum process periodically. (More precisely, it requests to create the autovacuum Section 6.5
launcher workers to the postgres server.)
WAL writer This process writes and flushes periodically the WAL data on the WAL buffer to persistent storage. Section 9.9
statistics In this process, statistics information such as for pg_stat_activity and for pg_stat_database, etc. is collected.
collector
http://www.interdb.jp/pg/pgsql02.html 2/3
2017/7/6 The Internals of PostgreSQL : Chapter 2 Process and Memory Architecture
The actual processes of a PostgreSQL server is shown here. 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 in Table 2.1 are running. See also Figure 2.1.
Local memory area – allocated by each backend process for its own use.
Shared memory area – used by all processes of a PostgreSQL server.
work_mem Executor uses this area for sorting tuples by ORDER BY and DISTINCT operations, and for joining tables by merge-join and hash-join Chapter 3
operations.
maintenance_work_mem Some kinds of maintenance operations (e.g., VACUUM, REINDEX) use this area. Section
6.1
http://www.interdb.jp/pg/pgsql02.html 3/3