LCOV - code coverage report
Current view: top level - src/backend/postmaster - auxprocess.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 51 55 92.7 %
Date: 2021-12-09 04:09:06 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*-------------------------------------------------------------------------
       2             :  * auxprocess.c
       3             :  *    functions related to auxiliary processes.
       4             :  *
       5             :  *
       6             :  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
       7             :  * Portions Copyright (c) 1994, Regents of the University of California
       8             :  *
       9             :  * IDENTIFICATION
      10             :  *    src/backend/postmaster/auxprocess.c
      11             :  *-------------------------------------------------------------------------
      12             :  */
      13             : #include "postgres.h"
      14             : 
      15             : #include <unistd.h>
      16             : #include <signal.h>
      17             : 
      18             : #include "libpq/pqsignal.h"
      19             : #include "miscadmin.h"
      20             : #include "pgstat.h"
      21             : #include "postmaster/auxprocess.h"
      22             : #include "postmaster/bgwriter.h"
      23             : #include "postmaster/startup.h"
      24             : #include "postmaster/walwriter.h"
      25             : #include "replication/walreceiver.h"
      26             : #include "storage/bufmgr.h"
      27             : #include "storage/bufpage.h"
      28             : #include "storage/condition_variable.h"
      29             : #include "storage/ipc.h"
      30             : #include "storage/proc.h"
      31             : #include "tcop/tcopprot.h"
      32             : #include "utils/memutils.h"
      33             : #include "utils/ps_status.h"
      34             : #include "utils/rel.h"
      35             : 
      36             : 
      37             : static void ShutdownAuxiliaryProcess(int code, Datum arg);
      38             : 
      39             : 
      40             : /* ----------------
      41             :  *      global variables
      42             :  * ----------------
      43             :  */
      44             : 
      45             : AuxProcType MyAuxProcType = NotAnAuxProcess;    /* declared in miscadmin.h */
      46             : 
      47             : 
      48             : /*
      49             :  *   AuxiliaryProcessMain
      50             :  *
      51             :  *   The main entry point for auxiliary processes, such as the bgwriter,
      52             :  *   walwriter, walreceiver, bootstrapper and the shared memory checker code.
      53             :  *
      54             :  *   This code is here just because of historical reasons.
      55             :  */
      56             : void
      57        2746 : AuxiliaryProcessMain(AuxProcType auxtype)
      58             : {
      59             :     Assert(IsUnderPostmaster);
      60             : 
      61        2746 :     MyAuxProcType = auxtype;
      62             : 
      63        2746 :     switch (MyAuxProcType)
      64             :     {
      65         878 :         case StartupProcess:
      66         878 :             MyBackendType = B_STARTUP;
      67         878 :             break;
      68           6 :         case ArchiverProcess:
      69           6 :             MyBackendType = B_ARCHIVER;
      70           6 :             break;
      71         550 :         case BgWriterProcess:
      72         550 :             MyBackendType = B_BG_WRITER;
      73         550 :             break;
      74         550 :         case CheckpointerProcess:
      75         550 :             MyBackendType = B_CHECKPOINTER;
      76         550 :             break;
      77         504 :         case WalWriterProcess:
      78         504 :             MyBackendType = B_WAL_WRITER;
      79         504 :             break;
      80         258 :         case WalReceiverProcess:
      81         258 :             MyBackendType = B_WAL_RECEIVER;
      82         258 :             break;
      83           0 :         default:
      84           0 :             elog(ERROR, "something has gone wrong");
      85             :             MyBackendType = B_INVALID;
      86             :     }
      87             : 
      88        2746 :     init_ps_display(NULL);
      89             : 
      90        2746 :     SetProcessingMode(BootstrapProcessing);
      91        2746 :     IgnoreSystemIndexes = true;
      92             : 
      93             :     /*
      94             :      * As an auxiliary process, we aren't going to do the full InitPostgres
      95             :      * pushups, but there are a couple of things that need to get lit up even
      96             :      * in an auxiliary process.
      97             :      */
      98             : 
      99             :     /*
     100             :      * Create a PGPROC so we can use LWLocks.  In the EXEC_BACKEND case, this
     101             :      * was already done by SubPostmasterMain().
     102             :      */
     103             : #ifndef EXEC_BACKEND
     104        2746 :     InitAuxiliaryProcess();
     105             : #endif
     106             : 
     107        2746 :     BaseInit();
     108             : 
     109             :     /*
     110             :      * Assign the ProcSignalSlot for an auxiliary process.  Since it doesn't
     111             :      * have a BackendId, the slot is statically allocated based on the
     112             :      * auxiliary process type (MyAuxProcType).  Backends use slots indexed in
     113             :      * the range from 1 to MaxBackends (inclusive), so we use MaxBackends +
     114             :      * AuxProcType + 1 as the index of the slot for an auxiliary process.
     115             :      *
     116             :      * This will need rethinking if we ever want more than one of a particular
     117             :      * auxiliary process type.
     118             :      */
     119        2746 :     ProcSignalInit(MaxBackends + MyAuxProcType + 1);
     120             : 
     121             :     /*
     122             :      * Auxiliary processes don't run transactions, but they may need a
     123             :      * resource owner anyway to manage buffer pins acquired outside
     124             :      * transactions (and, perhaps, other things in future).
     125             :      */
     126        2746 :     CreateAuxProcessResourceOwner();
     127             : 
     128             : 
     129             :     /* Initialize backend status information */
     130        2746 :     pgstat_beinit();
     131        2746 :     pgstat_bestart();
     132             : 
     133             :     /* register a before-shutdown callback for LWLock cleanup */
     134        2746 :     before_shmem_exit(ShutdownAuxiliaryProcess, 0);
     135             : 
     136        2746 :     SetProcessingMode(NormalProcessing);
     137             : 
     138        2746 :     switch (MyAuxProcType)
     139             :     {
     140         878 :         case StartupProcess:
     141         878 :             StartupProcessMain();
     142             :             proc_exit(1);
     143             : 
     144           6 :         case ArchiverProcess:
     145           6 :             PgArchiverMain();
     146             :             proc_exit(1);
     147             : 
     148         550 :         case BgWriterProcess:
     149         550 :             BackgroundWriterMain();
     150             :             proc_exit(1);
     151             : 
     152         550 :         case CheckpointerProcess:
     153         550 :             CheckpointerMain();
     154             :             proc_exit(1);
     155             : 
     156         504 :         case WalWriterProcess:
     157         504 :             InitXLOGAccess();
     158         504 :             WalWriterMain();
     159             :             proc_exit(1);
     160             : 
     161         258 :         case WalReceiverProcess:
     162         258 :             WalReceiverMain();
     163             :             proc_exit(1);
     164             : 
     165           0 :         default:
     166           0 :             elog(PANIC, "unrecognized process type: %d", (int) MyAuxProcType);
     167             :             proc_exit(1);
     168             :     }
     169             : }
     170             : 
     171             : /*
     172             :  * Begin shutdown of an auxiliary process.  This is approximately the equivalent
     173             :  * of ShutdownPostgres() in postinit.c.  We can't run transactions in an
     174             :  * auxiliary process, so most of the work of AbortTransaction() is not needed,
     175             :  * but we do need to make sure we've released any LWLocks we are holding.
     176             :  * (This is only critical during an error exit.)
     177             :  */
     178             : static void
     179        2746 : ShutdownAuxiliaryProcess(int code, Datum arg)
     180             : {
     181        2746 :     LWLockReleaseAll();
     182        2746 :     ConditionVariableCancelSleep();
     183        2746 :     pgstat_report_wait_end();
     184        2746 : }

Generated by: LCOV version 1.14