PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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/auxprocess.h"
#include "postmaster/interrupt.h"
#include "postmaster/walwriter.h"
#include "storage/aio_subsys.h"
#include "storage/bufmgr.h"
#include "storage/condition_variable.h"
#include "storage/fd.h"
#include "storage/lwlock.h"
#include "storage/proc.h"
#include "storage/procsignal.h"
#include "storage/smgr.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 (const void *startup_data, size_t startup_data_len)
 

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 ( const void *  startup_data,
size_t  startup_data_len 
)

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

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert(), AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), AuxiliaryProcessMainCommon(), B_WAL_WRITER, ConditionVariableCancelSleep(), EmitErrorReport(), error_context_stack, FlushErrorState(), HIBERNATE_FACTOR, HOLD_INTERRUPTS, LOOPS_UNTIL_HIBERNATE, LWLockReleaseAll(), MemoryContextReset(), MemoryContextSwitchTo(), MyBackendType, MyLatch, MyProcNumber, PG_exception_stack, pg_usleep(), pgaio_error_cleanup(), pgstat_report_wait_end(), pgstat_report_wal(), pqsignal, ProcessMainLoopInterrupts(), ProcGlobal, procsignal_sigusr1_handler(), ReleaseAuxProcessResources(), ResetLatch(), RESUME_INTERRUPTS, SetWalWriterSleeping(), 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

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().