Line data Source code
1 : /*-------------------------------------------------------------------------
2 : * auxprocess.c
3 : * functions related to auxiliary processes.
4 : *
5 : *
6 : * Portions Copyright (c) 1996-2026, 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 "access/xlog.h"
19 : #include "miscadmin.h"
20 : #include "pgstat.h"
21 : #include "postmaster/auxprocess.h"
22 : #include "storage/condition_variable.h"
23 : #include "storage/ipc.h"
24 : #include "storage/proc.h"
25 : #include "storage/procsignal.h"
26 : #include "utils/memutils.h"
27 : #include "utils/ps_status.h"
28 : #include "utils/wait_event.h"
29 :
30 :
31 : static void ShutdownAuxiliaryProcess(int code, Datum arg);
32 :
33 :
34 : /*
35 : * AuxiliaryProcessMainCommon
36 : *
37 : * Common initialization code for auxiliary processes, such as the bgwriter,
38 : * walwriter, walreceiver, and the startup process.
39 : */
40 : void
41 4903 : AuxiliaryProcessMainCommon(void)
42 : {
43 : Assert(IsUnderPostmaster);
44 :
45 : /* Release postmaster's working memory context */
46 4903 : if (PostmasterContext)
47 : {
48 4903 : MemoryContextDelete(PostmasterContext);
49 4903 : PostmasterContext = NULL;
50 : }
51 :
52 4903 : init_ps_display(NULL);
53 :
54 : Assert(GetProcessingMode() == InitProcessing);
55 :
56 4903 : IgnoreSystemIndexes = true;
57 :
58 : /*
59 : * As an auxiliary process, we aren't going to do the full InitPostgres
60 : * pushups, but there are a couple of things that need to get lit up even
61 : * in an auxiliary process.
62 : */
63 :
64 : /*
65 : * Create a PGPROC so we can use LWLocks and access shared memory.
66 : */
67 4903 : InitAuxiliaryProcess();
68 :
69 4903 : BaseInit();
70 :
71 4903 : ProcSignalInit(NULL, 0);
72 :
73 : /*
74 : * Initialize a local cache of the data_checksum_version, to be updated by
75 : * the procsignal-based barriers.
76 : *
77 : * This intentionally happens after initializing the procsignal, otherwise
78 : * we might miss a state change. This means we can get a barrier for the
79 : * state we've just initialized - but it can happen only once.
80 : *
81 : * The postmaster (which is what gets forked into the new child process)
82 : * does not handle barriers, therefore it may not have the current value
83 : * of LocalDataChecksumVersion value (it'll have the value read from the
84 : * control file, which may be arbitrarily old).
85 : *
86 : * NB: Even if the postmaster handled barriers, the value might still be
87 : * stale, as it might have changed after this process forked.
88 : */
89 4903 : InitLocalDataChecksumState();
90 :
91 : /*
92 : * Auxiliary processes don't run transactions, but they may need a
93 : * resource owner anyway to manage buffer pins acquired outside
94 : * transactions (and, perhaps, other things in future).
95 : */
96 4903 : CreateAuxProcessResourceOwner();
97 :
98 :
99 : /* Initialize backend status information */
100 4903 : pgstat_beinit();
101 4903 : pgstat_bestart_initial();
102 4903 : pgstat_bestart_final();
103 :
104 : /* register a before-shutdown callback for LWLock cleanup */
105 4903 : before_shmem_exit(ShutdownAuxiliaryProcess, 0);
106 :
107 4903 : SetProcessingMode(NormalProcessing);
108 4903 : }
109 :
110 : /*
111 : * Begin shutdown of an auxiliary process. This is approximately the equivalent
112 : * of ShutdownPostgres() in postinit.c. We can't run transactions in an
113 : * auxiliary process, so most of the work of AbortTransaction() is not needed,
114 : * but we do need to make sure we've released any LWLocks we are holding.
115 : * (This is only critical during an error exit.)
116 : */
117 : static void
118 4903 : ShutdownAuxiliaryProcess(int code, Datum arg)
119 : {
120 4903 : LWLockReleaseAll();
121 4903 : ConditionVariableCancelSleep();
122 4903 : pgstat_report_wait_end();
123 4903 : }
|