Learn the PG start-up process today.
The start command for pg./bin/postgres-d Path/to/data.
1. Subject monitoring Process
The postmaster process enters an infinite loop, waiting for the client to request and provide the requested service. In an infinite loop, the postmaster process periodically checks for client service requests by invoking the operating system interface Select, if not, resumes the loop and, if there is one, creates a postgres child process to service it.
In the Serverloop method of the postmaster process, an infinite loop is in progress waiting for the connection request to arrive. Call the OS's select interface to check if there are any requests on the port (see what changes to the file handle that corresponds to the port), call Select, if there is a request to arrive, block all signals, and after the new connection is completed for the service request, the postmaster process loops again waiting for the signal to stop blocking.
If there is a connection request, take a socket listensocket[i] from the socket array listensocket (Listensocket, which is the file descriptor FD prepared for the socket on the server). Call this socket as a parameter Concreate method to create a port structure, mainly the port structure of the file descriptor is set to the server local corresponding fracture of the file descriptor, and to the port structure of the local and remote address, if the port was created successfully, Call Backendstartup (port) to create a portgres service process and transfer the client process to the newly created Postgres service process, which is dedicated to servicing the client process. It then closes the connection that calls Streamclose the client process and the postmaster process, and then calls Connfree to release the port structure object on the server side.
2.postgres Service Process
The START process in main ()->subpostmastermain (), called the following method, the start of Xlog after the end of life.
1) Memorycontextint method, see "What happened during PostgreSQL start";
2) Initializegucoptions method, see "What happens in PostgreSQL during the START process";
3) Read_backend_variablases method, read the previously stored file Pgsql_tmp/pgsql_tmp.backend_var.[pid] for the restructured backendparameters structure. [Tmpfilenum];
4) Pgsharedmemoryreattach method, attach process postmaster the shared memory;
5) Read_nondefault_variables method, read non-default GUC parameters, see what's in PostgreSQL 11: Save non-default GUC parameters to file;
6) Closepostmasterports method, close the "START process" does not use the file handle, of course, in the postmaster process, these files are still open;
7) Securl_initialze method, if the client and server are set up to use SSL connection, initialize the SSL connection related object. PG uses the Open Source Library OpenSSL to achieve the relevant functions, security-related functions after the discussion bar, see the PG startup process of those six.
8) Backendinitialize method, initialize the background process and client interaction LIBPQ protocol related instances, in the port structure to set the client process IP, port and so on.
9) Initshmemaccess method, in initializing this process shared memory global variables: These shmem header Shmemseghdr, Shmem start address shmembase and Shmem end address +1 shmembase. The definition is shown below.
Staticpgshmemheader *shmemseghdr; /* Shared mem Segment Header */
Staticvoid *shmembase; /* Start address of shared memory */
Staticvoid *shmemend; /* End+1 Address of shared memory */
Initprocess method, initialize a PGPROC structure;
One) Createsharedmemoryandsemaphores method, see those things in PostgreSQL seven;
The Backendrun method, which organizes the parameters for this background process (typically "postgres db_name"), and then passes in the Postgresmain method.
The Postgresmain method is the entry function of the background service process Postgres process, and its main work is in this method.
3. Background process Startup
According to the incoming parameters of the boot process "Postgres–forkbackend Null[v_auxproctype]" went "--forkbackend" this branch. There are also autovacuumlauncher processes, autovacuumworker processes, archive processes, statistical processes, and postgres processes that serve the front end in the initial stages of the process, with almost no difference taking 1-6 steps, then taking different branches according to the different entry parameters. , so the process will be discussed in the future, starting directly from these branches.
Postgres Start-Up process analysis