Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * walwriter.c
4 : : *
5 : : * The WAL writer background process is new as of Postgres 8.3. It attempts
6 : : * to keep regular backends from having to write out (and fsync) WAL pages.
7 : : * Also, it guarantees that transaction commit records that weren't synced
8 : : * to disk immediately upon commit (ie, were "asynchronously committed")
9 : : * will reach disk within a knowable time --- which, as it happens, is at
10 : : * most three times the wal_writer_delay cycle time.
11 : : *
12 : : * Note that as with the bgwriter for shared buffers, regular backends are
13 : : * still empowered to issue WAL writes and fsyncs when the walwriter doesn't
14 : : * keep up. This means that the WALWriter is not an essential process and
15 : : * can shutdown quickly when requested.
16 : : *
17 : : * Because the walwriter's cycle is directly linked to the maximum delay
18 : : * before async-commit transactions are guaranteed committed, it's probably
19 : : * unwise to load additional functionality onto it. For instance, if you've
20 : : * got a yen to create xlog segments further in advance, that'd be better done
21 : : * in bgwriter than in walwriter.
22 : : *
23 : : * The walwriter is started by the postmaster as soon as the startup subprocess
24 : : * finishes. It remains alive until the postmaster commands it to terminate.
25 : : * Normal termination is by SIGTERM, which instructs the walwriter to exit(0).
26 : : * Emergency termination is by SIGQUIT; like any backend, the walwriter will
27 : : * simply abort and exit on SIGQUIT.
28 : : *
29 : : * If the walwriter exits unexpectedly, the postmaster treats that the same
30 : : * as a backend crash: shared memory may be corrupted, so remaining backends
31 : : * should be killed by SIGQUIT and then a recovery cycle started.
32 : : *
33 : : *
34 : : * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
35 : : *
36 : : *
37 : : * IDENTIFICATION
38 : : * src/backend/postmaster/walwriter.c
39 : : *
40 : : *-------------------------------------------------------------------------
41 : : */
42 : : #include "postgres.h"
43 : :
44 : : #include <signal.h>
45 : : #include <unistd.h>
46 : :
47 : : #include "access/xlog.h"
48 : : #include "libpq/pqsignal.h"
49 : : #include "miscadmin.h"
50 : : #include "pgstat.h"
51 : : #include "postmaster/auxprocess.h"
52 : : #include "postmaster/interrupt.h"
53 : : #include "postmaster/walwriter.h"
54 : : #include "storage/bufmgr.h"
55 : : #include "storage/condition_variable.h"
56 : : #include "storage/fd.h"
57 : : #include "storage/ipc.h"
58 : : #include "storage/lwlock.h"
59 : : #include "storage/proc.h"
60 : : #include "storage/procsignal.h"
61 : : #include "storage/smgr.h"
62 : : #include "utils/guc.h"
63 : : #include "utils/hsearch.h"
64 : : #include "utils/memutils.h"
65 : : #include "utils/resowner.h"
66 : :
67 : :
68 : : /*
69 : : * GUC parameters
70 : : */
71 : : int WalWriterDelay = 200;
72 : : int WalWriterFlushAfter = DEFAULT_WAL_WRITER_FLUSH_AFTER;
73 : :
74 : : /*
75 : : * Number of do-nothing loops before lengthening the delay time, and the
76 : : * multiplier to apply to WalWriterDelay when we do decide to hibernate.
77 : : * (Perhaps these need to be configurable?)
78 : : */
79 : : #define LOOPS_UNTIL_HIBERNATE 50
80 : : #define HIBERNATE_FACTOR 25
81 : :
82 : : /*
83 : : * Main entry point for walwriter process
84 : : *
85 : : * This is invoked from AuxiliaryProcessMain, which has already created the
86 : : * basic execution environment, but not enabled signals yet.
87 : : */
88 : : void
27 heikki.linnakangas@i 89 :GNC 642 : WalWriterMain(char *startup_data, size_t startup_data_len)
90 : : {
91 : : sigjmp_buf local_sigjmp_buf;
92 : : MemoryContext walwriter_context;
93 : : int left_till_hibernate;
94 : : bool hibernating;
95 : :
96 [ - + ]: 642 : Assert(startup_data_len == 0);
97 : :
98 : 642 : MyBackendType = B_WAL_WRITER;
99 : 642 : AuxiliaryProcessMainCommon();
100 : :
101 : : /*
102 : : * Properly accept or ignore signals the postmaster might send us
103 : : *
104 : : * We have no particular use for SIGINT at the moment, but seems
105 : : * reasonable to treat like SIGTERM.
106 : : */
1580 rhaas@postgresql.org 107 :CBC 642 : pqsignal(SIGHUP, SignalHandlerForConfigReload);
108 : 642 : pqsignal(SIGINT, SignalHandlerForShutdownRequest);
109 : 642 : pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
110 : : /* SIGQUIT handler was already set up by InitPostmasterChild */
6109 tgl@sss.pgh.pa.us 111 : 642 : pqsignal(SIGALRM, SIG_IGN);
112 : 642 : pqsignal(SIGPIPE, SIG_IGN);
1602 rhaas@postgresql.org 113 : 642 : pqsignal(SIGUSR1, procsignal_sigusr1_handler);
5995 bruce@momjian.us 114 : 642 : pqsignal(SIGUSR2, SIG_IGN); /* not used */
115 : :
116 : : /*
117 : : * Reset some signals that are accepted by postmaster but not here
118 : : */
6109 tgl@sss.pgh.pa.us 119 : 642 : pqsignal(SIGCHLD, SIG_DFL);
120 : :
121 : : /*
122 : : * Create a memory context that we will do all our work in. We do this so
123 : : * that we can reset the context during error recovery and thereby avoid
124 : : * possible memory leaks. Formerly this code just ran in
125 : : * TopMemoryContext, but resetting that would be a really bad idea.
126 : : */
127 : 642 : walwriter_context = AllocSetContextCreate(TopMemoryContext,
128 : : "Wal Writer",
129 : : ALLOCSET_DEFAULT_SIZES);
130 : 642 : MemoryContextSwitchTo(walwriter_context);
131 : :
132 : : /*
133 : : * If an exception is encountered, processing resumes here.
134 : : *
135 : : * You might wonder why this isn't coded as an infinite loop around a
136 : : * PG_TRY construct. The reason is that this is the bottom of the
137 : : * exception stack, and so with PG_TRY there would be no exception handler
138 : : * in force at all during the CATCH part. By leaving the outermost setjmp
139 : : * always active, we have at least some chance of recovering from an error
140 : : * during error recovery. (If we get into an infinite loop thereby, it
141 : : * will soon be stopped by overflow of elog.c's internal state stack.)
142 : : *
143 : : * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
144 : : * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
145 : : * signals other than SIGQUIT will be blocked until we complete error
146 : : * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
147 : : * call redundant, but it is not since InterruptPending might be set
148 : : * already.
149 : : */
150 [ - + ]: 642 : if (sigsetjmp(local_sigjmp_buf, 1) != 0)
151 : : {
152 : : /* Since not using PG_TRY, must reset error stack by hand */
6109 tgl@sss.pgh.pa.us 153 :UBC 0 : error_context_stack = NULL;
154 : :
155 : : /* Prevent interrupts while cleaning up */
156 : 0 : HOLD_INTERRUPTS();
157 : :
158 : : /* Report the error to the server log */
159 : 0 : EmitErrorReport();
160 : :
161 : : /*
162 : : * These operations are really just a minimal subset of
163 : : * AbortTransaction(). We don't have very many resources to worry
164 : : * about in walwriter, but we do have LWLocks, and perhaps buffers?
165 : : */
166 : 0 : LWLockReleaseAll();
2700 rhaas@postgresql.org 167 : 0 : ConditionVariableCancelSleep();
2957 168 : 0 : pgstat_report_wait_end();
6109 tgl@sss.pgh.pa.us 169 : 0 : UnlockBuffers();
2097 170 : 0 : ReleaseAuxProcessResources(false);
6109 171 : 0 : AtEOXact_Buffers(false);
4197 172 : 0 : AtEOXact_SMgr();
2178 173 : 0 : AtEOXact_Files(false);
6060 174 : 0 : AtEOXact_HashTables(false);
175 : :
176 : : /*
177 : : * Now return to normal top-level context and clear ErrorContext for
178 : : * next time.
179 : : */
6109 180 : 0 : MemoryContextSwitchTo(walwriter_context);
181 : 0 : FlushErrorState();
182 : :
183 : : /* Flush any leaked data in the top-level context */
151 nathan@postgresql.or 184 :UNC 0 : MemoryContextReset(walwriter_context);
185 : :
186 : : /* Now we can allow interrupts again */
6109 tgl@sss.pgh.pa.us 187 [ # # ]:UBC 0 : RESUME_INTERRUPTS();
188 : :
189 : : /*
190 : : * Sleep at least 1 second after any error. A write error is likely
191 : : * to be repeated, and we don't want to be filling the error logs as
192 : : * fast as we can.
193 : : */
194 : 0 : pg_usleep(1000000L);
195 : : }
196 : :
197 : : /* We can now handle ereport(ERROR) */
6109 tgl@sss.pgh.pa.us 198 :CBC 642 : PG_exception_stack = &local_sigjmp_buf;
199 : :
200 : : /*
201 : : * Unblock signals (they were blocked when the postmaster forked us)
202 : : */
436 tmunro@postgresql.or 203 : 642 : sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
204 : :
205 : : /*
206 : : * Reset hibernation state after any error.
207 : : */
4359 tgl@sss.pgh.pa.us 208 : 642 : left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
209 : 642 : hibernating = false;
210 : 642 : SetWalWriterSleeping(false);
211 : :
212 : : /*
213 : : * Advertise our latch that backends can use to wake us up while we're
214 : : * sleeping.
215 : : */
216 : 642 : ProcGlobal->walwriterLatch = &MyProc->procLatch;
217 : :
218 : : /*
219 : : * Loop forever
220 : : */
221 : : for (;;)
6109 222 : 14488 : {
223 : : long cur_timeout;
224 : :
225 : : /*
226 : : * Advertise whether we might hibernate in this cycle. We do this
227 : : * before resetting the latch to ensure that any async commits will
228 : : * see the flag set if they might possibly need to wake us up, and
229 : : * that we won't miss any signal they send us. (If we discover work
230 : : * to do in the last cycle before we would hibernate, the global flag
231 : : * will be set unnecessarily, but little harm is done.) But avoid
232 : : * touching the global flag if it doesn't need to change.
233 : : */
4359 234 [ + + ]: 15130 : if (hibernating != (left_till_hibernate <= 1))
235 : : {
236 : 50 : hibernating = (left_till_hibernate <= 1);
237 : 50 : SetWalWriterSleeping(hibernating);
238 : : }
239 : :
240 : : /* Clear any already-pending wakeups */
3378 andres@anarazel.de 241 : 15130 : ResetLatch(MyLatch);
242 : :
243 : : /* Process any signals received recently */
80 fujii@postgresql.org 244 :GNC 15130 : HandleMainLoopInterrupts();
245 : :
246 : : /*
247 : : * Do what we're here for; then, if XLogBackgroundFlush() found useful
248 : : * work to do, reset hibernation counter.
249 : : */
4359 tgl@sss.pgh.pa.us 250 [ + + ]:CBC 14751 : if (XLogBackgroundFlush())
251 : 3619 : left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
252 [ + + ]: 11132 : else if (left_till_hibernate > 0)
253 : 11099 : left_till_hibernate--;
254 : :
255 : : /* report pending statistics to the cumulative stats system */
739 andres@anarazel.de 256 : 14751 : pgstat_report_wal(false);
257 : :
258 : : /*
259 : : * Sleep until we are signaled or WalWriterDelay has elapsed. If we
260 : : * haven't done anything useful for quite some time, lengthen the
261 : : * sleep time so as to reduce the server's idle power consumption.
262 : : */
4359 tgl@sss.pgh.pa.us 263 [ + + ]: 14751 : if (left_till_hibernate > 0)
2489 264 : 14685 : cur_timeout = WalWriterDelay; /* in ms */
265 : : else
4359 266 : 66 : cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
267 : :
1969 tmunro@postgresql.or 268 : 14751 : (void) WaitLatch(MyLatch,
269 : : WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
270 : : cur_timeout,
271 : : WAIT_EVENT_WAL_WRITER_MAIN);
272 : : }
273 : : }
|