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-2017, 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/walwriter.h"
52 : #include "storage/bufmgr.h"
53 : #include "storage/condition_variable.h"
54 : #include "storage/fd.h"
55 : #include "storage/ipc.h"
56 : #include "storage/lwlock.h"
57 : #include "storage/proc.h"
58 : #include "storage/smgr.h"
59 : #include "utils/guc.h"
60 : #include "utils/hsearch.h"
61 : #include "utils/memutils.h"
62 : #include "utils/resowner.h"
63 :
64 :
65 : /*
66 : * GUC parameters
67 : */
68 : int WalWriterDelay = 200;
69 : int WalWriterFlushAfter = 128;
70 :
71 : /*
72 : * Number of do-nothing loops before lengthening the delay time, and the
73 : * multiplier to apply to WalWriterDelay when we do decide to hibernate.
74 : * (Perhaps these need to be configurable?)
75 : */
76 : #define LOOPS_UNTIL_HIBERNATE 50
77 : #define HIBERNATE_FACTOR 25
78 :
79 : /*
80 : * Flags set by interrupt handlers for later service in the main loop.
81 : */
82 : static volatile sig_atomic_t got_SIGHUP = false;
83 : static volatile sig_atomic_t shutdown_requested = false;
84 :
85 : /* Signal handlers */
86 : static void wal_quickdie(SIGNAL_ARGS);
87 : static void WalSigHupHandler(SIGNAL_ARGS);
88 : static void WalShutdownHandler(SIGNAL_ARGS);
89 : static void walwriter_sigusr1_handler(SIGNAL_ARGS);
90 :
91 : /*
92 : * Main entry point for walwriter process
93 : *
94 : * This is invoked from AuxiliaryProcessMain, which has already created the
95 : * basic execution environment, but not enabled signals yet.
96 : */
97 : void
98 1 : WalWriterMain(void)
99 : {
100 : sigjmp_buf local_sigjmp_buf;
101 : MemoryContext walwriter_context;
102 : int left_till_hibernate;
103 : bool hibernating;
104 :
105 : /*
106 : * Properly accept or ignore signals the postmaster might send us
107 : *
108 : * We have no particular use for SIGINT at the moment, but seems
109 : * reasonable to treat like SIGTERM.
110 : */
111 1 : pqsignal(SIGHUP, WalSigHupHandler); /* set flag to read config file */
112 1 : pqsignal(SIGINT, WalShutdownHandler); /* request shutdown */
113 1 : pqsignal(SIGTERM, WalShutdownHandler); /* request shutdown */
114 1 : pqsignal(SIGQUIT, wal_quickdie); /* hard crash time */
115 1 : pqsignal(SIGALRM, SIG_IGN);
116 1 : pqsignal(SIGPIPE, SIG_IGN);
117 1 : pqsignal(SIGUSR1, walwriter_sigusr1_handler);
118 1 : pqsignal(SIGUSR2, SIG_IGN); /* not used */
119 :
120 : /*
121 : * Reset some signals that are accepted by postmaster but not here
122 : */
123 1 : pqsignal(SIGCHLD, SIG_DFL);
124 1 : pqsignal(SIGTTIN, SIG_DFL);
125 1 : pqsignal(SIGTTOU, SIG_DFL);
126 1 : pqsignal(SIGCONT, SIG_DFL);
127 1 : pqsignal(SIGWINCH, SIG_DFL);
128 :
129 : /* We allow SIGQUIT (quickdie) at all times */
130 1 : sigdelset(&BlockSig, SIGQUIT);
131 :
132 : /*
133 : * Create a resource owner to keep track of our resources (not clear that
134 : * we need this, but may as well have one).
135 : */
136 1 : CurrentResourceOwner = ResourceOwnerCreate(NULL, "Wal Writer");
137 :
138 : /*
139 : * Create a memory context that we will do all our work in. We do this so
140 : * that we can reset the context during error recovery and thereby avoid
141 : * possible memory leaks. Formerly this code just ran in
142 : * TopMemoryContext, but resetting that would be a really bad idea.
143 : */
144 1 : walwriter_context = AllocSetContextCreate(TopMemoryContext,
145 : "Wal Writer",
146 : ALLOCSET_DEFAULT_SIZES);
147 1 : MemoryContextSwitchTo(walwriter_context);
148 :
149 : /*
150 : * If an exception is encountered, processing resumes here.
151 : *
152 : * This code is heavily based on bgwriter.c, q.v.
153 : */
154 1 : if (sigsetjmp(local_sigjmp_buf, 1) != 0)
155 : {
156 : /* Since not using PG_TRY, must reset error stack by hand */
157 0 : error_context_stack = NULL;
158 :
159 : /* Prevent interrupts while cleaning up */
160 0 : HOLD_INTERRUPTS();
161 :
162 : /* Report the error to the server log */
163 0 : EmitErrorReport();
164 :
165 : /*
166 : * These operations are really just a minimal subset of
167 : * AbortTransaction(). We don't have very many resources to worry
168 : * about in walwriter, but we do have LWLocks, and perhaps buffers?
169 : */
170 0 : LWLockReleaseAll();
171 0 : ConditionVariableCancelSleep();
172 0 : pgstat_report_wait_end();
173 0 : AbortBufferIO();
174 0 : UnlockBuffers();
175 : /* buffer pins are released here: */
176 0 : ResourceOwnerRelease(CurrentResourceOwner,
177 : RESOURCE_RELEASE_BEFORE_LOCKS,
178 : false, true);
179 : /* we needn't bother with the other ResourceOwnerRelease phases */
180 0 : AtEOXact_Buffers(false);
181 0 : AtEOXact_SMgr();
182 0 : AtEOXact_Files();
183 0 : AtEOXact_HashTables(false);
184 :
185 : /*
186 : * Now return to normal top-level context and clear ErrorContext for
187 : * next time.
188 : */
189 0 : MemoryContextSwitchTo(walwriter_context);
190 0 : FlushErrorState();
191 :
192 : /* Flush any leaked data in the top-level context */
193 0 : MemoryContextResetAndDeleteChildren(walwriter_context);
194 :
195 : /* Now we can allow interrupts again */
196 0 : RESUME_INTERRUPTS();
197 :
198 : /*
199 : * Sleep at least 1 second after any error. A write error is likely
200 : * to be repeated, and we don't want to be filling the error logs as
201 : * fast as we can.
202 : */
203 0 : pg_usleep(1000000L);
204 :
205 : /*
206 : * Close all open files after any error. This is helpful on Windows,
207 : * where holding deleted files open causes various strange errors.
208 : * It's not clear we need it elsewhere, but shouldn't hurt.
209 : */
210 0 : smgrcloseall();
211 : }
212 :
213 : /* We can now handle ereport(ERROR) */
214 1 : PG_exception_stack = &local_sigjmp_buf;
215 :
216 : /*
217 : * Unblock signals (they were blocked when the postmaster forked us)
218 : */
219 1 : PG_SETMASK(&UnBlockSig);
220 :
221 : /*
222 : * Reset hibernation state after any error.
223 : */
224 1 : left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
225 1 : hibernating = false;
226 1 : SetWalWriterSleeping(false);
227 :
228 : /*
229 : * Advertise our latch that backends can use to wake us up while we're
230 : * sleeping.
231 : */
232 1 : ProcGlobal->walwriterLatch = &MyProc->procLatch;
233 :
234 : /*
235 : * Loop forever
236 : */
237 : for (;;)
238 : {
239 : long cur_timeout;
240 : int rc;
241 :
242 : /*
243 : * Advertise whether we might hibernate in this cycle. We do this
244 : * before resetting the latch to ensure that any async commits will
245 : * see the flag set if they might possibly need to wake us up, and
246 : * that we won't miss any signal they send us. (If we discover work
247 : * to do in the last cycle before we would hibernate, the global flag
248 : * will be set unnecessarily, but little harm is done.) But avoid
249 : * touching the global flag if it doesn't need to change.
250 : */
251 905 : if (hibernating != (left_till_hibernate <= 1))
252 : {
253 0 : hibernating = (left_till_hibernate <= 1);
254 0 : SetWalWriterSleeping(hibernating);
255 : }
256 :
257 : /* Clear any already-pending wakeups */
258 905 : ResetLatch(MyLatch);
259 :
260 : /*
261 : * Process any requests or signals received recently.
262 : */
263 905 : if (got_SIGHUP)
264 : {
265 0 : got_SIGHUP = false;
266 0 : ProcessConfigFile(PGC_SIGHUP);
267 : }
268 905 : if (shutdown_requested)
269 : {
270 : /* Normal exit from the walwriter is here */
271 1 : proc_exit(0); /* done */
272 : }
273 :
274 : /*
275 : * Do what we're here for; then, if XLogBackgroundFlush() found useful
276 : * work to do, reset hibernation counter.
277 : */
278 904 : if (XLogBackgroundFlush())
279 582 : left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
280 322 : else if (left_till_hibernate > 0)
281 322 : left_till_hibernate--;
282 :
283 : /*
284 : * Sleep until we are signaled or WalWriterDelay has elapsed. If we
285 : * haven't done anything useful for quite some time, lengthen the
286 : * sleep time so as to reduce the server's idle power consumption.
287 : */
288 904 : if (left_till_hibernate > 0)
289 904 : cur_timeout = WalWriterDelay; /* in ms */
290 : else
291 0 : cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
292 :
293 904 : rc = WaitLatch(MyLatch,
294 : WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
295 : cur_timeout,
296 : WAIT_EVENT_WAL_WRITER_MAIN);
297 :
298 : /*
299 : * Emergency bailout if postmaster has died. This is to avoid the
300 : * necessity for manual cleanup of all postmaster children.
301 : */
302 904 : if (rc & WL_POSTMASTER_DEATH)
303 0 : exit(1);
304 904 : }
305 : }
306 :
307 :
308 : /* --------------------------------
309 : * signal handler routines
310 : * --------------------------------
311 : */
312 :
313 : /*
314 : * wal_quickdie() occurs when signalled SIGQUIT by the postmaster.
315 : *
316 : * Some backend has bought the farm,
317 : * so we need to stop what we're doing and exit.
318 : */
319 : static void
320 0 : wal_quickdie(SIGNAL_ARGS)
321 : {
322 0 : PG_SETMASK(&BlockSig);
323 :
324 : /*
325 : * We DO NOT want to run proc_exit() callbacks -- we're here because
326 : * shared memory may be corrupted, so we don't want to try to clean up our
327 : * transaction. Just nail the windows shut and get out of town. Now that
328 : * there's an atexit callback to prevent third-party code from breaking
329 : * things by calling exit() directly, we have to reset the callbacks
330 : * explicitly to make this work as intended.
331 : */
332 0 : on_exit_reset();
333 :
334 : /*
335 : * Note we do exit(2) not exit(0). This is to force the postmaster into a
336 : * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
337 : * backend. This is necessary precisely because we don't clean up our
338 : * shared memory state. (The "dead man switch" mechanism in pmsignal.c
339 : * should ensure the postmaster sees this as a crash, too, but no harm in
340 : * being doubly sure.)
341 : */
342 0 : exit(2);
343 : }
344 :
345 : /* SIGHUP: set flag to re-read config file at next convenient time */
346 : static void
347 0 : WalSigHupHandler(SIGNAL_ARGS)
348 : {
349 0 : int save_errno = errno;
350 :
351 0 : got_SIGHUP = true;
352 0 : SetLatch(MyLatch);
353 :
354 0 : errno = save_errno;
355 0 : }
356 :
357 : /* SIGTERM: set flag to exit normally */
358 : static void
359 1 : WalShutdownHandler(SIGNAL_ARGS)
360 : {
361 1 : int save_errno = errno;
362 :
363 1 : shutdown_requested = true;
364 1 : SetLatch(MyLatch);
365 :
366 1 : errno = save_errno;
367 1 : }
368 :
369 : /* SIGUSR1: used for latch wakeups */
370 : static void
371 496 : walwriter_sigusr1_handler(SIGNAL_ARGS)
372 : {
373 496 : int save_errno = errno;
374 :
375 496 : latch_sigusr1_handler();
376 :
377 496 : errno = save_errno;
378 496 : }
|