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/interrupt.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/procsignal.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

void WalWriterMain (void)
 

Variables

int WalWriterDelay = 200
 
int WalWriterFlushAfter = DEFAULT_WAL_WRITER_FLUSH_AFTER
 

Macro Definition Documentation

◆ HIBERNATE_FACTOR

#define HIBERNATE_FACTOR   25

Definition at line 79 of file walwriter.c.

◆ LOOPS_UNTIL_HIBERNATE

#define LOOPS_UNTIL_HIBERNATE   50

Definition at line 78 of file walwriter.c.

Function Documentation

◆ WalWriterMain()

void WalWriterMain ( void  )

Definition at line 88 of file walwriter.c.

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  */
104  /* SIGQUIT handler was already set up by InitPostmasterChild */
108  pqsignal(SIGUSR2, SIG_IGN); /* not used */
109 
110  /*
111  * Reset some signals that are accepted by postmaster but not here
112  */
114 
115  /*
116  * Create a memory context that we will do all our work in. We do this so
117  * that we can reset the context during error recovery and thereby avoid
118  * possible memory leaks. Formerly this code just ran in
119  * TopMemoryContext, but resetting that would be a really bad idea.
120  */
121  walwriter_context = AllocSetContextCreate(TopMemoryContext,
122  "Wal Writer",
124  MemoryContextSwitchTo(walwriter_context);
125 
126  /*
127  * If an exception is encountered, processing resumes here.
128  *
129  * You might wonder why this isn't coded as an infinite loop around a
130  * PG_TRY construct. The reason is that this is the bottom of the
131  * exception stack, and so with PG_TRY there would be no exception handler
132  * in force at all during the CATCH part. By leaving the outermost setjmp
133  * always active, we have at least some chance of recovering from an error
134  * during error recovery. (If we get into an infinite loop thereby, it
135  * will soon be stopped by overflow of elog.c's internal state stack.)
136  *
137  * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
138  * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
139  * signals other than SIGQUIT will be blocked until we complete error
140  * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
141  * call redundant, but it is not since InterruptPending might be set
142  * already.
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  UnlockBuffers();
165  AtEOXact_Buffers(false);
166  AtEOXact_SMgr();
167  AtEOXact_Files(false);
168  AtEOXact_HashTables(false);
169 
170  /*
171  * Now return to normal top-level context and clear ErrorContext for
172  * next time.
173  */
174  MemoryContextSwitchTo(walwriter_context);
175  FlushErrorState();
176 
177  /* Flush any leaked data in the top-level context */
178  MemoryContextReset(walwriter_context);
179 
180  /* Now we can allow interrupts again */
182 
183  /*
184  * Sleep at least 1 second after any error. A write error is likely
185  * to be repeated, and we don't want to be filling the error logs as
186  * fast as we can.
187  */
188  pg_usleep(1000000L);
189  }
190 
191  /* We can now handle ereport(ERROR) */
192  PG_exception_stack = &local_sigjmp_buf;
193 
194  /*
195  * Unblock signals (they were blocked when the postmaster forked us)
196  */
197  sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
198 
199  /*
200  * Reset hibernation state after any error.
201  */
202  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
203  hibernating = false;
204  SetWalWriterSleeping(false);
205 
206  /*
207  * Advertise our latch that backends can use to wake us up while we're
208  * sleeping.
209  */
211 
212  /*
213  * Loop forever
214  */
215  for (;;)
216  {
217  long cur_timeout;
218 
219  /*
220  * Advertise whether we might hibernate in this cycle. We do this
221  * before resetting the latch to ensure that any async commits will
222  * see the flag set if they might possibly need to wake us up, and
223  * that we won't miss any signal they send us. (If we discover work
224  * to do in the last cycle before we would hibernate, the global flag
225  * will be set unnecessarily, but little harm is done.) But avoid
226  * touching the global flag if it doesn't need to change.
227  */
228  if (hibernating != (left_till_hibernate <= 1))
229  {
230  hibernating = (left_till_hibernate <= 1);
231  SetWalWriterSleeping(hibernating);
232  }
233 
234  /* Clear any already-pending wakeups */
236 
237  /* Process any signals received recently */
239 
240  /*
241  * Do what we're here for; then, if XLogBackgroundFlush() found useful
242  * work to do, reset hibernation counter.
243  */
244  if (XLogBackgroundFlush())
245  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
246  else if (left_till_hibernate > 0)
247  left_till_hibernate--;
248 
249  /* report pending statistics to the cumulative stats system */
250  pgstat_report_wal(false);
251 
252  /*
253  * Sleep until we are signaled or WalWriterDelay has elapsed. If we
254  * haven't done anything useful for quite some time, lengthen the
255  * sleep time so as to reduce the server's idle power consumption.
256  */
257  if (left_till_hibernate > 0)
258  cur_timeout = WalWriterDelay; /* in ms */
259  else
260  cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
261 
262  (void) WaitLatch(MyLatch,
264  cur_timeout,
265  WAIT_EVENT_WAL_WRITER_MAIN);
266  }
267 }
sigset_t UnBlockSig
Definition: pqsignal.c:22
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:3213
void UnlockBuffers(void)
Definition: bufmgr.c:4768
bool ConditionVariableCancelSleep(void)
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1878
void EmitErrorReport(void)
Definition: elog.c:1675
ErrorContextCallback * error_context_stack
Definition: elog.c:95
void FlushErrorState(void)
Definition: elog.c:1831
sigjmp_buf * PG_exception_stack
Definition: elog.c:97
void AtEOXact_Files(bool isCommit)
Definition: fd.c:3165
struct Latch * MyLatch
Definition: globals.c:59
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:725
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:518
#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:330
MemoryContext TopMemoryContext
Definition: mcxt.c:141
#define AllocSetContextCreate
Definition: memutils.h:128
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:152
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:135
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:133
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
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:639
void ReleaseAuxProcessResources(bool isCommit)
Definition: resowner.c:1002
void pg_usleep(long microsec)
Definition: signal.c:53
void AtEOXact_SMgr(void)
Definition: smgr.c:778
PGPROC * MyProc
Definition: proc.c:67
PROC_HDR * ProcGlobal
Definition: proc.c:79
Latch procLatch
Definition: proc.h:170
Latch * walwriterLatch
Definition: proc.h:394
static void pgstat_report_wait_end(void)
Definition: wait_event.h:104
#define HIBERNATE_FACTOR
Definition: walwriter.c:79
#define LOOPS_UNTIL_HIBERNATE
Definition: walwriter.c:78
int WalWriterDelay
Definition: walwriter.c:70
#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:9346
bool XLogBackgroundFlush(void)
Definition: xlog.c:2923

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), ConditionVariableCancelSleep(), EmitErrorReport(), error_context_stack, FlushErrorState(), HandleMainLoopInterrupts(), HIBERNATE_FACTOR, HOLD_INTERRUPTS, LOOPS_UNTIL_HIBERNATE, LWLockReleaseAll(), MemoryContextReset(), MemoryContextSwitchTo(), MyLatch, MyProc, PG_exception_stack, pg_usleep(), pgstat_report_wait_end(), pgstat_report_wal(), pqsignal(), ProcGlobal, PGPROC::procLatch, 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::walwriterLatch, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_TIMEOUT, and XLogBackgroundFlush().

Referenced by AuxiliaryProcessMain().

Variable Documentation

◆ WalWriterDelay

int WalWriterDelay = 200

Definition at line 70 of file walwriter.c.

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

◆ WalWriterFlushAfter

int WalWriterFlushAfter = DEFAULT_WAL_WRITER_FLUSH_AFTER

Definition at line 71 of file walwriter.c.

Referenced by XLogBackgroundFlush(), and XLogSetAsyncXactLSN().