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.

Macros

#define DEFAULT_WAL_WRITER_FLUSH_AFTER   ((1024 * 1024) / XLOG_BLCKSZ)
 

Functions

void WalWriterMain (char *startup_data, size_t startup_data_len) pg_attribute_noreturn()
 

Variables

PGDLLIMPORT int WalWriterDelay
 
PGDLLIMPORT int WalWriterFlushAfter
 

Macro Definition Documentation

◆ DEFAULT_WAL_WRITER_FLUSH_AFTER

#define DEFAULT_WAL_WRITER_FLUSH_AFTER   ((1024 * 1024) / XLOG_BLCKSZ)

Definition at line 15 of file walwriter.h.

Function Documentation

◆ WalWriterMain()

void WalWriterMain ( char *  startup_data,
size_t  startup_data_len 
)

Definition at line 87 of file walwriter.c.

88 {
89  sigjmp_buf local_sigjmp_buf;
90  MemoryContext walwriter_context;
91  int left_till_hibernate;
92  bool hibernating;
93 
94  Assert(startup_data_len == 0);
95 
98 
99  /*
100  * Properly accept or ignore signals the postmaster might send us
101  *
102  * We have no particular use for SIGINT at the moment, but seems
103  * reasonable to treat like SIGTERM.
104  */
108  /* SIGQUIT handler was already set up by InitPostmasterChild */
112  pqsignal(SIGUSR2, SIG_IGN); /* not used */
113 
114  /*
115  * Reset some signals that are accepted by postmaster but not here
116  */
118 
119  /*
120  * Create a memory context that we will do all our work in. We do this so
121  * that we can reset the context during error recovery and thereby avoid
122  * possible memory leaks. Formerly this code just ran in
123  * TopMemoryContext, but resetting that would be a really bad idea.
124  */
125  walwriter_context = AllocSetContextCreate(TopMemoryContext,
126  "Wal Writer",
128  MemoryContextSwitchTo(walwriter_context);
129 
130  /*
131  * If an exception is encountered, processing resumes here.
132  *
133  * You might wonder why this isn't coded as an infinite loop around a
134  * PG_TRY construct. The reason is that this is the bottom of the
135  * exception stack, and so with PG_TRY there would be no exception handler
136  * in force at all during the CATCH part. By leaving the outermost setjmp
137  * always active, we have at least some chance of recovering from an error
138  * during error recovery. (If we get into an infinite loop thereby, it
139  * will soon be stopped by overflow of elog.c's internal state stack.)
140  *
141  * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
142  * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
143  * signals other than SIGQUIT will be blocked until we complete error
144  * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
145  * call redundant, but it is not since InterruptPending might be set
146  * already.
147  */
148  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
149  {
150  /* Since not using PG_TRY, must reset error stack by hand */
151  error_context_stack = NULL;
152 
153  /* Prevent interrupts while cleaning up */
154  HOLD_INTERRUPTS();
155 
156  /* Report the error to the server log */
157  EmitErrorReport();
158 
159  /*
160  * These operations are really just a minimal subset of
161  * AbortTransaction(). We don't have very many resources to worry
162  * about in walwriter, but we do have LWLocks, and perhaps buffers?
163  */
167  UnlockBuffers();
169  AtEOXact_Buffers(false);
170  AtEOXact_SMgr();
171  AtEOXact_Files(false);
172  AtEOXact_HashTables(false);
173 
174  /*
175  * Now return to normal top-level context and clear ErrorContext for
176  * next time.
177  */
178  MemoryContextSwitchTo(walwriter_context);
179  FlushErrorState();
180 
181  /* Flush any leaked data in the top-level context */
182  MemoryContextReset(walwriter_context);
183 
184  /* Now we can allow interrupts again */
186 
187  /*
188  * Sleep at least 1 second after any error. A write error is likely
189  * to be repeated, and we don't want to be filling the error logs as
190  * fast as we can.
191  */
192  pg_usleep(1000000L);
193  }
194 
195  /* We can now handle ereport(ERROR) */
196  PG_exception_stack = &local_sigjmp_buf;
197 
198  /*
199  * Unblock signals (they were blocked when the postmaster forked us)
200  */
201  sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
202 
203  /*
204  * Reset hibernation state after any error.
205  */
206  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
207  hibernating = false;
208  SetWalWriterSleeping(false);
209 
210  /*
211  * Advertise our proc number that backends can use to wake us up while
212  * we're sleeping.
213  */
215 
216  /*
217  * Loop forever
218  */
219  for (;;)
220  {
221  long cur_timeout;
222 
223  /*
224  * Advertise whether we might hibernate in this cycle. We do this
225  * before resetting the latch to ensure that any async commits will
226  * see the flag set if they might possibly need to wake us up, and
227  * that we won't miss any signal they send us. (If we discover work
228  * to do in the last cycle before we would hibernate, the global flag
229  * will be set unnecessarily, but little harm is done.) But avoid
230  * touching the global flag if it doesn't need to change.
231  */
232  if (hibernating != (left_till_hibernate <= 1))
233  {
234  hibernating = (left_till_hibernate <= 1);
235  SetWalWriterSleeping(hibernating);
236  }
237 
238  /* Clear any already-pending wakeups */
240 
241  /* Process any signals received recently */
243 
244  /*
245  * Do what we're here for; then, if XLogBackgroundFlush() found useful
246  * work to do, reset hibernation counter.
247  */
248  if (XLogBackgroundFlush())
249  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
250  else if (left_till_hibernate > 0)
251  left_till_hibernate--;
252 
253  /* report pending statistics to the cumulative stats system */
254  pgstat_report_wal(false);
255 
256  /*
257  * Sleep until we are signaled or WalWriterDelay has elapsed. If we
258  * haven't done anything useful for quite some time, lengthen the
259  * sleep time so as to reduce the server's idle power consumption.
260  */
261  if (left_till_hibernate > 0)
262  cur_timeout = WalWriterDelay; /* in ms */
263  else
264  cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
265 
266  (void) WaitLatch(MyLatch,
268  cur_timeout,
269  WAIT_EVENT_WAL_WRITER_MAIN);
270  }
271 }
void AuxiliaryProcessMainCommon(void)
Definition: auxprocess.c:39
sigset_t UnBlockSig
Definition: pqsignal.c:22
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:3559
void UnlockBuffers(void)
Definition: bufmgr.c:5130
#define Assert(condition)
Definition: c.h:812
bool ConditionVariableCancelSleep(void)
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1912
void EmitErrorReport(void)
Definition: elog.c:1687
ErrorContextCallback * error_context_stack
Definition: elog.c:94
void FlushErrorState(void)
Definition: elog.c:1867
sigjmp_buf * PG_exception_stack
Definition: elog.c:96
void AtEOXact_Files(bool isCommit)
Definition: fd.c:3187
ProcNumber MyProcNumber
Definition: globals.c:89
struct Latch * MyLatch
Definition: globals.c:62
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:105
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void HandleMainLoopInterrupts(void)
Definition: interrupt.c:34
void ResetLatch(Latch *latch)
Definition: latch.c:724
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:517
#define WL_TIMEOUT
Definition: latch.h:130
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:132
#define WL_LATCH_SET
Definition: latch.h:127
void LWLockReleaseAll(void)
Definition: lwlock.c:1876
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:383
MemoryContext TopMemoryContext
Definition: mcxt.c:149
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:135
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:133
@ B_WAL_WRITER
Definition: miscadmin.h:359
BackendType MyBackendType
Definition: miscinit.c:64
void pgstat_report_wal(bool force)
Definition: pgstat_wal.c:48
pqsigfunc pqsignal(int signo, pqsigfunc func)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition: procsignal.c:671
MemoryContextSwitchTo(old_ctx)
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:1002
void pg_usleep(long microsec)
Definition: signal.c:53
void AtEOXact_SMgr(void)
Definition: smgr.c:829
PROC_HDR * ProcGlobal
Definition: proc.c:78
ProcNumber walwriterProc
Definition: proc.h:420
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101
#define HIBERNATE_FACTOR
Definition: walwriter.c:78
#define LOOPS_UNTIL_HIBERNATE
Definition: walwriter.c:77
int WalWriterDelay
Definition: walwriter.c:69
#define SIGCHLD
Definition: win32_port.h:178
#define SIGHUP
Definition: win32_port.h:168
#define SIG_DFL
Definition: win32_port.h:163
#define SIGPIPE
Definition: win32_port.h:173
#define SIGUSR1
Definition: win32_port.h:180
#define SIGALRM
Definition: win32_port.h:174
#define SIGUSR2
Definition: win32_port.h:181
#define SIG_IGN
Definition: win32_port.h:165
void SetWalWriterSleeping(bool sleeping)
Definition: xlog.c:9507
bool XLogBackgroundFlush(void)
Definition: xlog.c:2990

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), AuxiliaryProcessMainCommon(), B_WAL_WRITER, ConditionVariableCancelSleep(), EmitErrorReport(), error_context_stack, FlushErrorState(), HandleMainLoopInterrupts(), HIBERNATE_FACTOR, HOLD_INTERRUPTS, LOOPS_UNTIL_HIBERNATE, LWLockReleaseAll(), MemoryContextReset(), MemoryContextSwitchTo(), MyBackendType, MyLatch, MyProcNumber, PG_exception_stack, pg_usleep(), pgstat_report_wait_end(), pgstat_report_wal(), pqsignal(), ProcGlobal, procsignal_sigusr1_handler(), ReleaseAuxProcessResources(), ResetLatch(), RESUME_INTERRUPTS, SetWalWriterSleeping(), SIG_DFL, SIG_IGN, SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, TopMemoryContext, UnBlockSig, UnlockBuffers(), WaitLatch(), WalWriterDelay, PROC_HDR::walwriterProc, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_TIMEOUT, and XLogBackgroundFlush().

Variable Documentation

◆ WalWriterDelay

PGDLLIMPORT int WalWriterDelay
extern

Definition at line 69 of file walwriter.c.

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

◆ WalWriterFlushAfter

PGDLLIMPORT int WalWriterFlushAfter
extern

Definition at line 70 of file walwriter.c.

Referenced by XLogBackgroundFlush(), and XLogSetAsyncXactLSN().