PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
walwriter.c File Reference
#include "postgres.h"
#include <signal.h>
#include <unistd.h>
#include "access/xlog.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/walwriter.h"
#include "storage/bufmgr.h"
#include "storage/condition_variable.h"
#include "storage/fd.h"
#include "storage/ipc.h"
#include "storage/lwlock.h"
#include "storage/proc.h"
#include "storage/smgr.h"
#include "utils/guc.h"
#include "utils/hsearch.h"
#include "utils/memutils.h"
#include "utils/resowner.h"
Include dependency graph for walwriter.c:

Go to the source code of this file.

Macros

#define LOOPS_UNTIL_HIBERNATE   50
 
#define HIBERNATE_FACTOR   25
 

Functions

static void wal_quickdie (SIGNAL_ARGS)
 
static void WalSigHupHandler (SIGNAL_ARGS)
 
static void WalShutdownHandler (SIGNAL_ARGS)
 
static void walwriter_sigusr1_handler (SIGNAL_ARGS)
 
void WalWriterMain (void)
 

Variables

int WalWriterDelay = 200
 
int WalWriterFlushAfter = 128
 
static volatile sig_atomic_t got_SIGHUP = false
 
static volatile sig_atomic_t shutdown_requested = false
 

Macro Definition Documentation

#define HIBERNATE_FACTOR   25

Definition at line 77 of file walwriter.c.

Referenced by WalWriterMain().

#define LOOPS_UNTIL_HIBERNATE   50

Definition at line 76 of file walwriter.c.

Referenced by WalWriterMain().

Function Documentation

static void wal_quickdie ( SIGNAL_ARGS  )
static

Definition at line 320 of file walwriter.c.

References BlockSig, on_exit_reset(), and PG_SETMASK.

Referenced by WalWriterMain().

321 {
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  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  exit(2);
343 }
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void on_exit_reset(void)
Definition: ipc.c:396
sigset_t BlockSig
Definition: pqsignal.c:22
static void WalShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 359 of file walwriter.c.

References MyLatch, SetLatch(), and shutdown_requested.

Referenced by WalWriterMain().

360 {
361  int save_errno = errno;
362 
363  shutdown_requested = true;
364  SetLatch(MyLatch);
365 
366  errno = save_errno;
367 }
static volatile sig_atomic_t shutdown_requested
Definition: walwriter.c:83
void SetLatch(volatile Latch *latch)
Definition: latch.c:414
struct Latch * MyLatch
Definition: globals.c:52
static void WalSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 347 of file walwriter.c.

References got_SIGHUP, MyLatch, and SetLatch().

Referenced by WalWriterMain().

348 {
349  int save_errno = errno;
350 
351  got_SIGHUP = true;
352  SetLatch(MyLatch);
353 
354  errno = save_errno;
355 }
static volatile sig_atomic_t got_SIGHUP
Definition: walwriter.c:82
void SetLatch(volatile Latch *latch)
Definition: latch.c:414
struct Latch * MyLatch
Definition: globals.c:52
static void walwriter_sigusr1_handler ( SIGNAL_ARGS  )
static

Definition at line 371 of file walwriter.c.

References latch_sigusr1_handler().

Referenced by WalWriterMain().

372 {
373  int save_errno = errno;
374 
376 
377  errno = save_errno;
378 }
void latch_sigusr1_handler(void)
Definition: latch.c:1473
void WalWriterMain ( void  )

Definition at line 98 of file walwriter.c.

References AbortBufferIO(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), BlockSig, ConditionVariableCancelSleep(), CurrentResourceOwner, EmitErrorReport(), error_context_stack, FlushErrorState(), got_SIGHUP, HIBERNATE_FACTOR, HOLD_INTERRUPTS, LOOPS_UNTIL_HIBERNATE, LWLockReleaseAll(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), MyLatch, MyProc, NULL, PG_exception_stack, PG_SETMASK, pg_usleep(), PGC_SIGHUP, pgstat_report_wait_end(), pqsignal(), proc_exit(), ProcessConfigFile(), ProcGlobal, PGPROC::procLatch, ResetLatch(), RESOURCE_RELEASE_BEFORE_LOCKS, ResourceOwnerCreate(), ResourceOwnerRelease(), RESUME_INTERRUPTS, SetWalWriterSleeping(), shutdown_requested, SIG_DFL, SIG_IGN, SIGALRM, SIGCHLD, SIGCONT, SIGHUP, SIGPIPE, SIGQUIT, SIGTTIN, SIGTTOU, SIGUSR1, SIGUSR2, SIGWINCH, smgrcloseall(), TopMemoryContext, UnBlockSig, UnlockBuffers(), WAIT_EVENT_WAL_WRITER_MAIN, WaitLatch(), wal_quickdie(), WalShutdownHandler(), WalSigHupHandler(), walwriter_sigusr1_handler(), WalWriterDelay, PROC_HDR::walwriterLatch, WL_LATCH_SET, WL_POSTMASTER_DEATH, WL_TIMEOUT, and XLogBackgroundFlush().

Referenced by AuxiliaryProcessMain().

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  pqsignal(SIGHUP, WalSigHupHandler); /* set flag to read config file */
112  pqsignal(SIGINT, WalShutdownHandler); /* request shutdown */
113  pqsignal(SIGTERM, WalShutdownHandler); /* request shutdown */
114  pqsignal(SIGQUIT, wal_quickdie); /* hard crash time */
118  pqsignal(SIGUSR2, SIG_IGN); /* not used */
119 
120  /*
121  * Reset some signals that are accepted by postmaster but not here
122  */
128 
129  /* We allow SIGQUIT (quickdie) at all times */
130  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  */
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  walwriter_context = AllocSetContextCreate(TopMemoryContext,
145  "Wal Writer",
147  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  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
155  {
156  /* Since not using PG_TRY, must reset error stack by hand */
158 
159  /* Prevent interrupts while cleaning up */
160  HOLD_INTERRUPTS();
161 
162  /* Report the error to the server log */
163  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  */
173  AbortBufferIO();
174  UnlockBuffers();
175  /* buffer pins are released here: */
178  false, true);
179  /* we needn't bother with the other ResourceOwnerRelease phases */
180  AtEOXact_Buffers(false);
181  AtEOXact_SMgr();
182  AtEOXact_Files();
183  AtEOXact_HashTables(false);
184 
185  /*
186  * Now return to normal top-level context and clear ErrorContext for
187  * next time.
188  */
189  MemoryContextSwitchTo(walwriter_context);
190  FlushErrorState();
191 
192  /* Flush any leaked data in the top-level context */
193  MemoryContextResetAndDeleteChildren(walwriter_context);
194 
195  /* Now we can allow interrupts again */
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  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  smgrcloseall();
211  }
212 
213  /* We can now handle ereport(ERROR) */
214  PG_exception_stack = &local_sigjmp_buf;
215 
216  /*
217  * Unblock signals (they were blocked when the postmaster forked us)
218  */
220 
221  /*
222  * Reset hibernation state after any error.
223  */
224  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
225  hibernating = false;
226  SetWalWriterSleeping(false);
227 
228  /*
229  * Advertise our latch that backends can use to wake us up while we're
230  * sleeping.
231  */
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  if (hibernating != (left_till_hibernate <= 1))
252  {
253  hibernating = (left_till_hibernate <= 1);
254  SetWalWriterSleeping(hibernating);
255  }
256 
257  /* Clear any already-pending wakeups */
259 
260  /*
261  * Process any requests or signals received recently.
262  */
263  if (got_SIGHUP)
264  {
265  got_SIGHUP = false;
267  }
268  if (shutdown_requested)
269  {
270  /* Normal exit from the walwriter is here */
271  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  if (XLogBackgroundFlush())
279  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
280  else if (left_till_hibernate > 0)
281  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  if (left_till_hibernate > 0)
289  cur_timeout = WalWriterDelay; /* in ms */
290  else
291  cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
292 
293  rc = WaitLatch(MyLatch,
295  cur_timeout,
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  if (rc & WL_POSTMASTER_DEATH)
303  exit(1);
304  }
305 }
#define SIGUSR1
Definition: win32.h:202
#define SIGCONT
Definition: win32.h:197
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
PGPROC * MyProc
Definition: proc.c:67
static void walwriter_sigusr1_handler(SIGNAL_ARGS)
Definition: walwriter.c:371
#define SIGWINCH
Definition: win32.h:201
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2415
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
static void WalSigHupHandler(SIGNAL_ARGS)
Definition: walwriter.c:347
#define SIGTTIN
Definition: win32.h:199
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define LOOPS_UNTIL_HIBERNATE
Definition: walwriter.c:76
void proc_exit(int code)
Definition: ipc.c:99
PROC_HDR * ProcGlobal
Definition: proc.c:80
void ResetLatch(volatile Latch *latch)
Definition: latch.c:497
#define SIGQUIT
Definition: win32.h:189
void FlushErrorState(void)
Definition: elog.c:1587
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
Latch procLatch
Definition: proc.h:103
void smgrcloseall(void)
Definition: smgr.c:326
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:117
ErrorContextCallback * error_context_stack
Definition: elog.c:88
void pg_usleep(long microsec)
Definition: signal.c:53
void AtEOXact_SMgr(void)
Definition: smgr.c:798
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:336
Latch * walwriterLatch
Definition: proc.h:246
#define SIG_IGN
Definition: win32.h:185
void ConditionVariableCancelSleep(void)
static void WalShutdownHandler(SIGNAL_ARGS)
Definition: walwriter.c:359
void AtEOXact_Files(void)
Definition: fd.c:2617
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:165
bool XLogBackgroundFlush(void)
Definition: xlog.c:2946
static volatile sig_atomic_t got_SIGHUP
Definition: walwriter.c:82
static volatile sig_atomic_t shutdown_requested
Definition: walwriter.c:83
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1235
MemoryContext TopMemoryContext
Definition: mcxt.c:43
Definition: guc.h:72
static void wal_quickdie(SIGNAL_ARGS)
Definition: walwriter.c:320
void UnlockBuffers(void)
Definition: bufmgr.c:3518
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
sigset_t BlockSig
Definition: pqsignal.c:22
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:322
void EmitErrorReport(void)
Definition: elog.c:1446
#define SIGPIPE
Definition: win32.h:193
#define SIGHUP
Definition: win32.h:188
#define SIG_DFL
Definition: win32.h:183
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
#define NULL
Definition: c.h:229
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
void AbortBufferIO(void)
Definition: bufmgr.c:3974
void SetWalWriterSleeping(bool sleeping)
Definition: xlog.c:12115
int WalWriterDelay
Definition: walwriter.c:68
sigjmp_buf * PG_exception_stack
Definition: elog.c:90
#define SIGTTOU
Definition: win32.h:200
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:115
struct Latch * MyLatch
Definition: globals.c:52
void LWLockReleaseAll(void)
Definition: lwlock.c:1814
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1830
#define SIGCHLD
Definition: win32.h:198
#define WL_LATCH_SET
Definition: latch.h:124
#define SIGALRM
Definition: win32.h:194
#define SIGUSR2
Definition: win32.h:203
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416
#define HIBERNATE_FACTOR
Definition: walwriter.c:77

Variable Documentation

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 82 of file walwriter.c.

Referenced by WalSigHupHandler(), and WalWriterMain().

volatile sig_atomic_t shutdown_requested = false
static

Definition at line 83 of file walwriter.c.

Referenced by WalShutdownHandler(), and WalWriterMain().

int WalWriterDelay = 200

Definition at line 68 of file walwriter.c.

Referenced by LogicalRepApplyLoop(), WalWriterMain(), and XLogBackgroundFlush().

int WalWriterFlushAfter = 128

Definition at line 69 of file walwriter.c.

Referenced by XLogBackgroundFlush().