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