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

static void HandleWalWriterInterrupts (void)
 
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

◆ HandleWalWriterInterrupts()

static void HandleWalWriterInterrupts ( void  )
static

Definition at line 283 of file walwriter.c.

284 {
287 
289  {
290  ConfigReloadPending = false;
292  }
293 
295  proc_exit(0);
296 
297  /* Perform logging of memory contexts of this process */
300 }
volatile sig_atomic_t LogMemoryContextPending
Definition: globals.c:38
volatile sig_atomic_t ProcSignalBarrierPending
Definition: globals.c:37
@ PGC_SIGHUP
Definition: guc.h:71
void ProcessConfigFile(GucContext context)
volatile sig_atomic_t ShutdownRequestPending
Definition: interrupt.c:28
volatile sig_atomic_t ConfigReloadPending
Definition: interrupt.c:27
void proc_exit(int code)
Definition: ipc.c:104
void ProcessLogMemoryContextInterrupt(void)
Definition: mcxt.c:1199
void ProcessProcSignalBarrier(void)
Definition: procsignal.c:468

References ConfigReloadPending, LogMemoryContextPending, PGC_SIGHUP, proc_exit(), ProcessConfigFile(), ProcessLogMemoryContextInterrupt(), ProcessProcSignalBarrier(), ProcSignalBarrierPending, and ShutdownRequestPending.

Referenced by WalWriterMain().

◆ WalWriterMain()

void WalWriterMain ( void  )

Definition at line 91 of file walwriter.c.

92 {
93  sigjmp_buf local_sigjmp_buf;
94  MemoryContext walwriter_context;
95  int left_till_hibernate;
96  bool hibernating;
97 
98  /*
99  * Properly accept or ignore signals the postmaster might send us
100  *
101  * We have no particular use for SIGINT at the moment, but seems
102  * reasonable to treat like SIGTERM.
103  */
107  /* SIGQUIT handler was already set up by InitPostmasterChild */
111  pqsignal(SIGUSR2, SIG_IGN); /* not used */
112 
113  /*
114  * Reset some signals that are accepted by postmaster but not here
115  */
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  * You might wonder why this isn't coded as an infinite loop around a
133  * PG_TRY construct. The reason is that this is the bottom of the
134  * exception stack, and so with PG_TRY there would be no exception handler
135  * in force at all during the CATCH part. By leaving the outermost setjmp
136  * always active, we have at least some chance of recovering from an error
137  * during error recovery. (If we get into an infinite loop thereby, it
138  * will soon be stopped by overflow of elog.c's internal state stack.)
139  *
140  * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
141  * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
142  * signals other than SIGQUIT will be blocked until we complete error
143  * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
144  * call redundant, but it is not since InterruptPending might be set
145  * already.
146  */
147  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
148  {
149  /* Since not using PG_TRY, must reset error stack by hand */
150  error_context_stack = NULL;
151 
152  /* Prevent interrupts while cleaning up */
153  HOLD_INTERRUPTS();
154 
155  /* Report the error to the server log */
156  EmitErrorReport();
157 
158  /*
159  * These operations are really just a minimal subset of
160  * AbortTransaction(). We don't have very many resources to worry
161  * about in walwriter, but we do have LWLocks, and perhaps buffers?
162  */
166  UnlockBuffers();
168  AtEOXact_Buffers(false);
169  AtEOXact_SMgr();
170  AtEOXact_Files(false);
171  AtEOXact_HashTables(false);
172 
173  /*
174  * Now return to normal top-level context and clear ErrorContext for
175  * next time.
176  */
177  MemoryContextSwitchTo(walwriter_context);
178  FlushErrorState();
179 
180  /* Flush any leaked data in the top-level context */
181  MemoryContextResetAndDeleteChildren(walwriter_context);
182 
183  /* Now we can allow interrupts again */
185 
186  /*
187  * Sleep at least 1 second after any error. A write error is likely
188  * to be repeated, and we don't want to be filling the error logs as
189  * fast as we can.
190  */
191  pg_usleep(1000000L);
192 
193  /*
194  * Close all open files after any error. This is helpful on Windows,
195  * where holding deleted files open causes various strange errors.
196  * It's not clear we need it elsewhere, but shouldn't hurt.
197  */
198  smgrcloseall();
199  }
200 
201  /* We can now handle ereport(ERROR) */
202  PG_exception_stack = &local_sigjmp_buf;
203 
204  /*
205  * Unblock signals (they were blocked when the postmaster forked us)
206  */
207  sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
208 
209  /*
210  * Reset hibernation state after any error.
211  */
212  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
213  hibernating = false;
214  SetWalWriterSleeping(false);
215 
216  /*
217  * Advertise our latch that backends can use to wake us up while we're
218  * sleeping.
219  */
221 
222  /*
223  * Loop forever
224  */
225  for (;;)
226  {
227  long cur_timeout;
228 
229  /*
230  * Advertise whether we might hibernate in this cycle. We do this
231  * before resetting the latch to ensure that any async commits will
232  * see the flag set if they might possibly need to wake us up, and
233  * that we won't miss any signal they send us. (If we discover work
234  * to do in the last cycle before we would hibernate, the global flag
235  * will be set unnecessarily, but little harm is done.) But avoid
236  * touching the global flag if it doesn't need to change.
237  */
238  if (hibernating != (left_till_hibernate <= 1))
239  {
240  hibernating = (left_till_hibernate <= 1);
241  SetWalWriterSleeping(hibernating);
242  }
243 
244  /* Clear any already-pending wakeups */
246 
247  /* Process any signals received recently */
249 
250  /*
251  * Do what we're here for; then, if XLogBackgroundFlush() found useful
252  * work to do, reset hibernation counter.
253  */
254  if (XLogBackgroundFlush())
255  left_till_hibernate = LOOPS_UNTIL_HIBERNATE;
256  else if (left_till_hibernate > 0)
257  left_till_hibernate--;
258 
259  /* report pending statistics to the cumulative stats system */
260  pgstat_report_wal(false);
261 
262  /*
263  * Sleep until we are signaled or WalWriterDelay has elapsed. If we
264  * haven't done anything useful for quite some time, lengthen the
265  * sleep time so as to reduce the server's idle power consumption.
266  */
267  if (left_till_hibernate > 0)
268  cur_timeout = WalWriterDelay; /* in ms */
269  else
270  cur_timeout = WalWriterDelay * HIBERNATE_FACTOR;
271 
272  (void) WaitLatch(MyLatch,
274  cur_timeout,
275  WAIT_EVENT_WAL_WRITER_MAIN);
276  }
277 }
sigset_t UnBlockSig
Definition: pqsignal.c:22
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:3132
void UnlockBuffers(void)
Definition: bufmgr.c:4687
bool ConditionVariableCancelSleep(void)
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1878
void EmitErrorReport(void)
Definition: elog.c:1669
ErrorContextCallback * error_context_stack
Definition: elog.c:95
void FlushErrorState(void)
Definition: elog.c:1825
sigjmp_buf * PG_exception_stack
Definition: elog.c:97
void AtEOXact_Files(bool isCommit)
Definition: fd.c:3110
struct Latch * MyLatch
Definition: globals.c:58
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition: interrupt.c:109
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition: interrupt.c:61
void ResetLatch(Latch *latch)
Definition: latch.c:697
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:490
#define WL_TIMEOUT
Definition: latch.h:128
#define WL_EXIT_ON_PM_DEATH
Definition: latch.h:130
#define WL_LATCH_SET
Definition: latch.h:125
void LWLockReleaseAll(void)
Definition: lwlock.c:1903
MemoryContext TopMemoryContext
Definition: mcxt.c:141
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:70
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:134
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:132
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:138
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:934
void pg_usleep(long microsec)
Definition: signal.c:53
void smgrcloseall(void)
Definition: smgr.c:327
void AtEOXact_SMgr(void)
Definition: smgr.c:739
PGPROC * MyProc
Definition: proc.c:66
PROC_HDR * ProcGlobal
Definition: proc.c:78
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
static void HandleWalWriterInterrupts(void)
Definition: walwriter.c:283
#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:8997
bool XLogBackgroundFlush(void)
Definition: xlog.c:2725

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), ConditionVariableCancelSleep(), EmitErrorReport(), error_context_stack, FlushErrorState(), HandleWalWriterInterrupts(), HIBERNATE_FACTOR, HOLD_INTERRUPTS, LOOPS_UNTIL_HIBERNATE, LWLockReleaseAll(), MemoryContextResetAndDeleteChildren, 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, smgrcloseall(), 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().