What is Forking in Postgresql? | Interview Q&A

MynotesoracleDBA
Nerd For Tech
Published in
2 min readMar 2, 2021

To Understand The Fork process will run different ID with the same Postmaster ID

Forking Process in Postgresql
  • It is a process of creating the child process that looks similar to the parent process that created it. The child process is known as “Forking”.
  • Both parent and child process has the same code of segments with a different address space.
  • The Forking Process (a) child process has a different process ID.
  • As per the PostgreSQL architecture, the postmaster is the first background process that will starts when the cluster is started. Based on the postmaster ID other background utility processes details are given below.
*BG writer, 
*Check pointer,
*WAL writer,
*Archive WAL writer,
*Logger,
*Statistical collector,
*Autovacuum launcher
  • All of these processes are created or ‘Forking’ by the postmaster as child processes, if you see below each child process have a different ID, but with the same parent ID (a) postmaster ID

To Check Postgres Running Status:

[postgres@Prime~]$ ps -ef|grep datapostgres 818 1 0 Feb25 ? 00:00:06 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/postgres 29802 29777 0 06:21 pts/0 00:00:00 grep --color=auto data

Here our Postgres cluster was running on postmaster ID 818 based on the postmaster ID the other background process is running on a different ID

[postgres@Prime~]$ systemctl status postgresql-12● postgresql-12.service - PostgreSQL 12 database serverLoaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)Active: active (running) since Thu 2021-02-25 00:53:07 UTC; 5 days agoDocs: https://www.postgresql.org/docs/12/static/Process: 799 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)Main PID: 818 (postmaster)Tasks: 8 (limit: 4774)Memory: 196.0MCGroup: /system.slice/postgresql-12.service├─ 818 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/├─ 826 postgres: logger├─1036 postgres: checkpointer├─1037 postgres: background writer├─1038 postgres: walwriter├─1039 postgres: autovacuum launcher├─1040 postgres: stats collector└─1041 postgres: logical replication launcher

To View the Forking process:

[postgres@Prime~]$ ps -ef|grep postgrespostgres 818 1 0 Feb25 ? 00:00:06 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/postgres 826 818 0 Feb25 ? 00:00:00 postgres: loggerpostgres 1036 818 0 Feb25 ? 00:00:00 postgres: checkpointerpostgres 1037 818 0 Feb25 ? 00:00:04 postgres: background writerpostgres 1038 818 0 Feb25 ? 00:00:04 postgres: walwriterpostgres 1039 818 0 Feb25 ? 00:00:04 postgres: autovacuum launcherpostgres 1040 818 0 Feb25 ? 00:00:08 postgres: stats collectorpostgres 1041 818 0 Feb25 ? 00:00:00 postgres: logical replication launcherroot 29776 29754 0 06:21 pts/0 00:00:00 su - postgrespostgres 29777 29776 0 06:21 pts/0 00:00:00 -bashpostgres 29852 29777 0 06:28 pts/0 00:00:00 ps -efpostgres 29853 29777 0 06:28 pts/0 00:00:00 grep --color=auto postgres

Summary:

The postmaster ID will be assigned once the cluster is started based on this ID other background processes will starts and running on different ID with the same parent ID. This postmaster ID will changes when the cluster is stop and starts.

--

--

MynotesoracleDBA
Nerd For Tech

As a DBA we are going to write and discuss multiple database administration concepts. “Nothing Grows In Comfort Zone”.