Age Owner Branch data TLA 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
803 heikki.linnakangas@i 41 :CBC 4389 : AuxiliaryProcessMainCommon(void)
42 : : {
1759 andres@anarazel.de 43 [ - + ]: 4389 : Assert(IsUnderPostmaster);
44 : :
45 : : /* Release postmaster's working memory context */
803 heikki.linnakangas@i 46 [ + - ]: 4389 : if (PostmasterContext)
47 : : {
48 : 4389 : MemoryContextDelete(PostmasterContext);
49 : 4389 : PostmasterContext = NULL;
50 : : }
51 : :
1759 andres@anarazel.de 52 : 4389 : init_ps_display(NULL);
53 : :
697 heikki.linnakangas@i 54 [ - + ]: 4389 : Assert(GetProcessingMode() == InitProcessing);
55 : :
1759 andres@anarazel.de 56 : 4389 : 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 : 4389 : InitAuxiliaryProcess();
68 : :
69 : 4389 : BaseInit();
70 : :
71 : : /*
72 : : * Prevent consuming interrupts between setting ProcSignalInit and setting
73 : : * the initial local data checksum value. If a barrier is emitted, and
74 : : * absorbed, before local cached state is initialized the state transition
75 : : * can be invalid.
76 : : */
30 dgustafsson@postgres 77 :GNC 4389 : HOLD_INTERRUPTS();
78 : :
416 heikki.linnakangas@i 79 :CBC 4389 : ProcSignalInit(NULL, 0);
80 : :
81 : : /*
82 : : * Initialize a local cache of the data_checksum_version, to be updated by
83 : : * the procsignal-based barriers.
84 : : *
85 : : * This intentionally happens after initializing the procsignal, otherwise
86 : : * we might miss a state change. This means we can get a barrier for the
87 : : * state we've just initialized - but it can happen only once.
88 : : *
89 : : * The postmaster (which is what gets forked into the new child process)
90 : : * does not handle barriers, therefore it may not have the current value
91 : : * of LocalDataChecksumState value (it'll have the value read from the
92 : : * control file, which may be arbitrarily old).
93 : : *
94 : : * NB: Even if the postmaster handled barriers, the value might still be
95 : : * stale, as it might have changed after this process forked.
96 : : */
57 dgustafsson@postgres 97 :GNC 4389 : InitLocalDataChecksumState();
98 : :
30 99 [ - + ]: 4389 : RESUME_INTERRUPTS();
100 : :
101 : : /*
102 : : * Initialize the process-local logical info WAL logging state.
103 : : *
104 : : * This must be called after ProcSignalInit() so that the process can
105 : : * participate in procsignal-based barriers that update this state.
106 : : */
23 msawada@postgresql.o 107 : 4389 : InitializeProcessXLogLogicalInfo();
108 : :
109 : : /*
110 : : * Auxiliary processes don't run transactions, but they may need a
111 : : * resource owner anyway to manage buffer pins acquired outside
112 : : * transactions (and, perhaps, other things in future).
113 : : */
1759 andres@anarazel.de 114 :CBC 4389 : CreateAuxProcessResourceOwner();
115 : :
116 : :
117 : : /* Initialize backend status information */
118 : 4389 : pgstat_beinit();
452 michael@paquier.xyz 119 : 4389 : pgstat_bestart_initial();
120 : 4389 : pgstat_bestart_final();
121 : :
122 : : /* register a before-shutdown callback for LWLock cleanup */
1759 andres@anarazel.de 123 : 4389 : before_shmem_exit(ShutdownAuxiliaryProcess, 0);
124 : :
125 : 4389 : SetProcessingMode(NormalProcessing);
126 : 4389 : }
127 : :
128 : : /*
129 : : * Begin shutdown of an auxiliary process. This is approximately the equivalent
130 : : * of ShutdownPostgres() in postinit.c. We can't run transactions in an
131 : : * auxiliary process, so most of the work of AbortTransaction() is not needed,
132 : : * but we do need to make sure we've released any LWLocks we are holding.
133 : : * (This is only critical during an error exit.)
134 : : */
135 : : static void
136 : 4389 : ShutdownAuxiliaryProcess(int code, Datum arg)
137 : : {
138 : 4389 : LWLockReleaseAll();
139 : 4389 : ConditionVariableCancelSleep();
140 : 4389 : pgstat_report_wait_end();
141 : 4389 : }
|