PostgreSQL Source Code  git master
walwriter.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void WalWriterMain (void) pg_attribute_noreturn()
 

Variables

int WalWriterDelay
 
int WalWriterFlushAfter
 

Function Documentation

◆ WalWriterMain()

void WalWriterMain ( void  )

Definition at line 88 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(), HandleMainLoopInterrupts(), HIBERNATE_FACTOR, HOLD_INTERRUPTS, LOOPS_UNTIL_HIBERNATE, LWLockReleaseAll(), MemoryContextResetAndDeleteChildren, MemoryContextSwitchTo(), MyLatch, MyProc, PG_exception_stack, PG_SETMASK, pg_usleep(), pgstat_report_wait_end(), pqsignal(), ProcGlobal, PGPROC::procLatch, procsignal_sigusr1_handler(), ReleaseAuxProcessResources(), ResetLatch(), RESUME_INTERRUPTS, SetWalWriterSleeping(), SIG_DFL, SIG_IGN, SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForCrashExit(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGQUIT, SIGUSR1, SIGUSR2, smgrcloseall(), TopMemoryContext, UnBlockSig, UnlockBuffers(), WAIT_EVENT_WAL_WRITER_MAIN, WaitLatch(), WalWriterDelay, PROC_HDR::walwriterLatch, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_TIMEOUT, and XLogBackgroundFlush().

Referenced by AuxiliaryProcessMain().

89 {
90  sigjmp_buf local_sigjmp_buf;
91  MemoryContext walwriter_context;
92  int left_till_hibernate;
93  bool hibernating;
94 
95  /*
96  * Properly accept or ignore signals the postmaster might send us
97  *
98  * We have no particular use for SIGINT at the moment, but seems
99  * reasonable to treat like SIGTERM.
100  */
108  pqsignal(SIGUSR2, SIG_IGN); /* not used */
109 
110  /*
111  * Reset some signals that are accepted by postmaster but not here
112  */
114 
115  /* We allow SIGQUIT (quickdie) at all times */
116  sigdelset(&BlockSig, SIGQUIT);
117 
118  /*
119  * Create a memory context that we will do all our work in. We do this so
120  * that we can reset the context during error recovery and thereby avoid
121  * possible memory leaks. Formerly this code just ran in
122  * TopMemoryContext, but resetting that would be a really bad idea.
123  */
124  walwriter_context = AllocSetContextCreate(TopMemoryContext,
125  "Wal Writer",
127  MemoryContextSwitchTo(walwriter_context);
128 
129  /*
130  * If an exception is encountered, processing resumes here.
131  *
132  * This code is heavily based on bgwriter.c, q.v.
133  */
134  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
135  {
136  /* Since not using PG_TRY, must reset error stack by hand */
137  error_context_stack = NULL;
138 
139  /* Prevent interrupts while cleaning up */
140  HOLD_INTERRUPTS();
141 
142  /* Report the error to the server log */
143  EmitErrorReport();
144 
145  /*
146  * These operations are really just a minimal subset of
147  * AbortTransaction(). We don't have very many resources to worry
148  * about in walwriter, but we do have LWLocks, and perhaps buffers?
149  */
153  AbortBufferIO();
154  UnlockBuffers();
156  AtEOXact_Buffers(false);
157  AtEOXact_SMgr();
158  AtEOXact_Files(false);
159  AtEOXact_HashTables(false);
160 
161  /*
162  * Now return to normal top-level context and clear ErrorContext for
163  * next time.
164  */
165  MemoryContextSwitchTo(walwriter_context);
166  FlushErrorState();
167 
168  /* Flush any leaked data in the top-level context */
169  MemoryContextResetAndDeleteChildren(walwriter_context);
170 
171  /* Now we can allow interrupts again */
173 
174  /*
175  * Sleep at least 1 second after any error. A write error is likely
176  * to be repeated, and we don't want to be filling the error logs as
177  * fast as we can.
178  */
179  pg_usleep(1000000L);
180 
181  /*
182  * Close all open files after any error. This is helpful on Windows,
183  * where holding deleted files open causes various strange errors.
184  * It's not clear we need it elsewhere, but shouldn't hurt.
185  */
186  smgrcloseall();
187  }
188 
189  /* We can now handle ereport(ERROR) */
190  PG_exception_stack = &local_sigjmp_buf;
191 
192  /*
193  * Unblock signals (they were blocked when the postmaster forked us)
194  */
196 
197  /*
198  * Reset hibernation state after any error.
199  */
200  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
201  hibernating = false;
202  SetWalWriterSleeping(false);
203 
204  /*
205  * Advertise our latch that backends can use to wake us up while we're
206  * sleeping.
207  */
209 
210  /*
211  * Loop forever
212  */
213  for (;;)
214  {
215  long cur_timeout;
216 
217  /*
218  * Advertise whether we might hibernate in this cycle. We do this
219  * before resetting the latch to ensure that any async commits will
220  * see the flag set if they might possibly need to wake us up, and
221  * that we won't miss any signal they send us. (If we discover work
222  * to do in the last cycle before we would hibernate, the global flag
223  * will be set unnecessarily, but little harm is done.) But avoid
224  * touching the global flag if it doesn't need to change.
225  */
226  if (hibernating != (left_till_hibernate <= 1))
227  {
228  hibernating = (left_till_hibernate <= 1);
229  SetWalWriterSleeping(hibernating);
230  }
231 
232  /* Clear any already-pending wakeups */
234 
236 
237  /*
238  * Do what we're here for; then, if XLogBackgroundFlush() found useful
239  * work to do, reset hibernation counter.
240  */
241  if (XLogBackgroundFlush())
242  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
243  else if (left_till_hibernate > 0)
244  left_till_hibernate--;
245 
246  /*
247  * Sleep until we are signaled or WalWriterDelay has elapsed. If we
248  * haven't done anything useful for quite some time, lengthen the
249  * sleep time so as to reduce the server's idle power consumption.
250  */
251  if (left_till_hibernate > 0)
252  cur_timeout = WalWriterDelay; /* in ms */
253  else
254  cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
255 
256  (void) WaitLatch(MyLatch,
258  cur_timeout,
260  }
261 }
#define SIGQUIT
Definition: win32_port.h:154
#define AllocSetContextCreate
Definition: memutils.h:170
#define WL_TIMEOUT
Definition: latch.h:127
void HandleMainLoopInterrupts(void)
Definition: interrupt.c:33
#define SIGUSR1
Definition: win32_port.h:165
#define SIGCHLD
Definition: win32_port.h:163
PGPROC * MyProc
Definition: proc.c:67
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2433
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:56
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:855
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define LOOPS_UNTIL_HIBERNATE
Definition: walwriter.c:78
PROC_HDR * ProcGlobal
Definition: proc.c:80
#define SIGPIPE
Definition: win32_port.h:158
#define SIGUSR2
Definition: win32_port.h:166
void FlushErrorState(void)
Definition: elog.c:1678
void ResetLatch(Latch *latch)
Definition: latch.c:540
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
void AtEOXact_Files(bool isCommit)
Definition: fd.c:2907
Latch procLatch
Definition: proc.h:104
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:365
void smgrcloseall(void)
Definition: smgr.c:286
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:118
ErrorContextCallback * error_context_stack
Definition: elog.c:91
void pg_usleep(long microsec)
Definition: signal.c:53
void AtEOXact_SMgr(void)
Definition: smgr.c:655
void SignalHandlerForCrashExit(SIGNAL_ARGS)
Definition: interrupt.c:72
Latch * walwriterLatch
Definition: proc.h:265
void ConditionVariableCancelSleep(void)
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
bool XLogBackgroundFlush(void)
Definition: xlog.c:3001
#define SIGHUP
Definition: win32_port.h:153
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1344
MemoryContext TopMemoryContext
Definition: mcxt.c:44
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:104
#define SIG_IGN
Definition: win32_port.h:150
void UnlockBuffers(void)
Definition: bufmgr.c:3584
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:67
sigset_t BlockSig
Definition: pqsignal.c:22
void EmitErrorReport(void)
Definition: elog.c:1533
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
#define SIG_DFL
Definition: win32_port.h:148
#define SIGALRM
Definition: win32_port.h:159
void AbortBufferIO(void)
Definition: bufmgr.c:4040
void SetWalWriterSleeping(bool sleeping)
Definition: xlog.c:12344
int WalWriterDelay
Definition: walwriter.c:70
sigjmp_buf * PG_exception_stack
Definition: elog.c:93
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:116
struct Latch * MyLatch
Definition: globals.c:54
void LWLockReleaseAll(void)
Definition: lwlock.c:1826
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:533
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1835
#define WL_LATCH_SET
Definition: latch.h:124
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:129
#define HIBERNATE_FACTOR
Definition: walwriter.c:79

Variable Documentation

◆ WalWriterDelay

int WalWriterDelay

Definition at line 70 of file walwriter.c.

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

◆ WalWriterFlushAfter

int WalWriterFlushAfter

Definition at line 71 of file walwriter.c.

Referenced by XLogBackgroundFlush().