PostgreSQL Source Code  git master
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

◆ HIBERNATE_FACTOR

#define HIBERNATE_FACTOR   25

Definition at line 77 of file walwriter.c.

Referenced by WalWriterMain().

◆ LOOPS_UNTIL_HIBERNATE

#define LOOPS_UNTIL_HIBERNATE   50

Definition at line 76 of file walwriter.c.

Referenced by WalWriterMain().

Function Documentation

◆ wal_quickdie()

static void wal_quickdie ( SIGNAL_ARGS  )
static

Definition at line 298 of file walwriter.c.

Referenced by WalWriterMain().

299 {
300  /*
301  * We DO NOT want to run proc_exit() or atexit() callbacks -- we're here
302  * because shared memory may be corrupted, so we don't want to try to
303  * clean up our transaction. Just nail the windows shut and get out of
304  * town. The callbacks wouldn't be safe to run from a signal handler,
305  * anyway.
306  *
307  * Note we do _exit(2) not _exit(0). This is to force the postmaster into
308  * a system reset cycle if someone sends a manual SIGQUIT to a random
309  * backend. This is necessary precisely because we don't clean up our
310  * shared memory state. (The "dead man switch" mechanism in pmsignal.c
311  * should ensure the postmaster sees this as a crash, too, but no harm in
312  * being doubly sure.)
313  */
314  _exit(2);
315 }

◆ WalShutdownHandler()

static void WalShutdownHandler ( SIGNAL_ARGS  )
static

Definition at line 331 of file walwriter.c.

References MyLatch, SetLatch(), and shutdown_requested.

Referenced by WalWriterMain().

332 {
333  int save_errno = errno;
334 
335  shutdown_requested = true;
336  SetLatch(MyLatch);
337 
338  errno = save_errno;
339 }
void SetLatch(Latch *latch)
Definition: latch.c:436
static volatile sig_atomic_t shutdown_requested
Definition: walwriter.c:83
struct Latch * MyLatch
Definition: globals.c:54

◆ WalSigHupHandler()

static void WalSigHupHandler ( SIGNAL_ARGS  )
static

Definition at line 319 of file walwriter.c.

References got_SIGHUP, MyLatch, and SetLatch().

Referenced by WalWriterMain().

320 {
321  int save_errno = errno;
322 
323  got_SIGHUP = true;
324  SetLatch(MyLatch);
325 
326  errno = save_errno;
327 }
void SetLatch(Latch *latch)
Definition: latch.c:436
static volatile sig_atomic_t got_SIGHUP
Definition: walwriter.c:82
struct Latch * MyLatch
Definition: globals.c:54

◆ walwriter_sigusr1_handler()

static void walwriter_sigusr1_handler ( SIGNAL_ARGS  )
static

Definition at line 343 of file walwriter.c.

References latch_sigusr1_handler().

Referenced by WalWriterMain().

344 {
345  int save_errno = errno;
346 
348 
349  errno = save_errno;
350 }
void latch_sigusr1_handler(void)
Definition: latch.c:1515

◆ WalWriterMain()

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(), EmitErrorReport(), error_context_stack, FlushErrorState(), got_SIGHUP, HIBERNATE_FACTOR, HOLD_INTERRUPTS, LOOPS_UNTIL_HIBERNATE, LWLockReleaseAll(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), MyLatch, MyProc, PG_exception_stack, PG_SETMASK, pg_usleep(), PGC_SIGHUP, pgstat_report_wait_end(), pqsignal(), proc_exit(), ProcessConfigFile(), ProcGlobal, PGPROC::procLatch, ReleaseAuxProcessResources(), ResetLatch(), RESUME_INTERRUPTS, SetWalWriterSleeping(), shutdown_requested, SIG_DFL, SIG_IGN, SIGALRM, SIGCHLD, SIGHUP, SIGPIPE, SIGQUIT, SIGUSR1, SIGUSR2, smgrcloseall(), TopMemoryContext, UnBlockSig, UnlockBuffers(), WAIT_EVENT_WAL_WRITER_MAIN, WaitLatch(), wal_quickdie(), WalShutdownHandler(), WalSigHupHandler(), walwriter_sigusr1_handler(), WalWriterDelay, PROC_HDR::walwriterLatch, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, 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  */
124 
125  /* We allow SIGQUIT (quickdie) at all times */
126  sigdelset(&BlockSig, SIGQUIT);
127 
128  /*
129  * Create a memory context that we will do all our work in. We do this so
130  * that we can reset the context during error recovery and thereby avoid
131  * possible memory leaks. Formerly this code just ran in
132  * TopMemoryContext, but resetting that would be a really bad idea.
133  */
134  walwriter_context = AllocSetContextCreate(TopMemoryContext,
135  "Wal Writer",
137  MemoryContextSwitchTo(walwriter_context);
138 
139  /*
140  * If an exception is encountered, processing resumes here.
141  *
142  * This code is heavily based on bgwriter.c, q.v.
143  */
144  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
145  {
146  /* Since not using PG_TRY, must reset error stack by hand */
147  error_context_stack = NULL;
148 
149  /* Prevent interrupts while cleaning up */
150  HOLD_INTERRUPTS();
151 
152  /* Report the error to the server log */
153  EmitErrorReport();
154 
155  /*
156  * These operations are really just a minimal subset of
157  * AbortTransaction(). We don't have very many resources to worry
158  * about in walwriter, but we do have LWLocks, and perhaps buffers?
159  */
163  AbortBufferIO();
164  UnlockBuffers();
166  AtEOXact_Buffers(false);
167  AtEOXact_SMgr();
168  AtEOXact_Files(false);
169  AtEOXact_HashTables(false);
170 
171  /*
172  * Now return to normal top-level context and clear ErrorContext for
173  * next time.
174  */
175  MemoryContextSwitchTo(walwriter_context);
176  FlushErrorState();
177 
178  /* Flush any leaked data in the top-level context */
179  MemoryContextResetAndDeleteChildren(walwriter_context);
180 
181  /* Now we can allow interrupts again */
183 
184  /*
185  * Sleep at least 1 second after any error. A write error is likely
186  * to be repeated, and we don't want to be filling the error logs as
187  * fast as we can.
188  */
189  pg_usleep(1000000L);
190 
191  /*
192  * Close all open files after any error. This is helpful on Windows,
193  * where holding deleted files open causes various strange errors.
194  * It's not clear we need it elsewhere, but shouldn't hurt.
195  */
196  smgrcloseall();
197  }
198 
199  /* We can now handle ereport(ERROR) */
200  PG_exception_stack = &local_sigjmp_buf;
201 
202  /*
203  * Unblock signals (they were blocked when the postmaster forked us)
204  */
206 
207  /*
208  * Reset hibernation state after any error.
209  */
210  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
211  hibernating = false;
212  SetWalWriterSleeping(false);
213 
214  /*
215  * Advertise our latch that backends can use to wake us up while we're
216  * sleeping.
217  */
219 
220  /*
221  * Loop forever
222  */
223  for (;;)
224  {
225  long cur_timeout;
226 
227  /*
228  * Advertise whether we might hibernate in this cycle. We do this
229  * before resetting the latch to ensure that any async commits will
230  * see the flag set if they might possibly need to wake us up, and
231  * that we won't miss any signal they send us. (If we discover work
232  * to do in the last cycle before we would hibernate, the global flag
233  * will be set unnecessarily, but little harm is done.) But avoid
234  * touching the global flag if it doesn't need to change.
235  */
236  if (hibernating != (left_till_hibernate <= 1))
237  {
238  hibernating = (left_till_hibernate <= 1);
239  SetWalWriterSleeping(hibernating);
240  }
241 
242  /* Clear any already-pending wakeups */
244 
245  /*
246  * Process any requests or signals received recently.
247  */
248  if (got_SIGHUP)
249  {
250  got_SIGHUP = false;
252  }
253  if (shutdown_requested)
254  {
255  /* Normal exit from the walwriter is here */
256  proc_exit(0); /* done */
257  }
258 
259  /*
260  * Do what we're here for; then, if XLogBackgroundFlush() found useful
261  * work to do, reset hibernation counter.
262  */
263  if (XLogBackgroundFlush())
264  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
265  else if (left_till_hibernate > 0)
266  left_till_hibernate--;
267 
268  /*
269  * Sleep until we are signaled or WalWriterDelay has elapsed. If we
270  * haven't done anything useful for quite some time, lengthen the
271  * sleep time so as to reduce the server's idle power consumption.
272  */
273  if (left_till_hibernate > 0)
274  cur_timeout = WalWriterDelay; /* in ms */
275  else
276  cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
277 
278  (void) WaitLatch(MyLatch,
280  cur_timeout,
282  }
283 }
#define SIGQUIT
Definition: win32_port.h:164
#define AllocSetContextCreate
Definition: memutils.h:169
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
#define SIGUSR1
Definition: win32_port.h:175
#define SIGCHLD
Definition: win32_port.h:173
PGPROC * MyProc
Definition: proc.c:68
static void walwriter_sigusr1_handler(SIGNAL_ARGS)
Definition: walwriter.c:343
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2423
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:855
static void WalSigHupHandler(SIGNAL_ARGS)
Definition: walwriter.c:319
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:104
PROC_HDR * ProcGlobal
Definition: proc.c:81
#define SIGPIPE
Definition: win32_port.h:168
#define SIGUSR2
Definition: win32_port.h:176
void FlushErrorState(void)
Definition: elog.c:1574
void ResetLatch(Latch *latch)
Definition: latch.c:519
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2783
Latch procLatch
Definition: proc.h:104
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:344
void smgrcloseall(void)
Definition: smgr.c:286
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
ErrorContextCallback * error_context_stack
Definition: elog.c:88
void pg_usleep(long microsec)
Definition: signal.c:53
void AtEOXact_SMgr(void)
Definition: smgr.c:689
Latch * walwriterLatch
Definition: proc.h:265
void ConditionVariableCancelSleep(void)
static void WalShutdownHandler(SIGNAL_ARGS)
Definition: walwriter.c:331
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:191
bool XLogBackgroundFlush(void)
Definition: xlog.c:2987
#define SIGHUP
Definition: win32_port.h:163
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:1342
MemoryContext TopMemoryContext
Definition: mcxt.c:44
Definition: guc.h:72
static void wal_quickdie(SIGNAL_ARGS)
Definition: walwriter.c:298
#define SIG_IGN
Definition: win32_port.h:160
void UnlockBuffers(void)
Definition: bufmgr.c:3562
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
sigset_t BlockSig
Definition: pqsignal.c:22
void EmitErrorReport(void)
Definition: elog.c:1433
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
#define SIG_DFL
Definition: win32_port.h:158
#define SIGALRM
Definition: win32_port.h:169
void AbortBufferIO(void)
Definition: bufmgr.c:4018
void SetWalWriterSleeping(bool sleeping)
Definition: xlog.c:12234
int WalWriterDelay
Definition: walwriter.c:68
sigjmp_buf * PG_exception_stack
Definition: elog.c:90
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
struct Latch * MyLatch
Definition: globals.c:54
void LWLockReleaseAll(void)
Definition: lwlock.c:1825
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1834
#define WL_LATCH_SET
Definition: latch.h:124
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
#define HIBERNATE_FACTOR
Definition: walwriter.c:77

Variable Documentation

◆ got_SIGHUP

volatile sig_atomic_t got_SIGHUP = false
static

Definition at line 82 of file walwriter.c.

Referenced by WalSigHupHandler(), and WalWriterMain().

◆ shutdown_requested

volatile sig_atomic_t shutdown_requested = false
static

Definition at line 83 of file walwriter.c.

Referenced by WalShutdownHandler(), and WalWriterMain().

◆ WalWriterDelay

int WalWriterDelay = 200

Definition at line 68 of file walwriter.c.

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

◆ WalWriterFlushAfter

int WalWriterFlushAfter = 128

Definition at line 69 of file walwriter.c.

Referenced by XLogBackgroundFlush().