89{
90 sigjmp_buf local_sigjmp_buf;
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 */
114
115 /*
116 * Reset some signals that are accepted by postmaster but not here
117 */
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 */
127 "Wal Writer",
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 */
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 */
175
176 /*
177 * Now return to normal top-level context and clear ErrorContext for
178 * next time.
179 */
182
183 /* Flush any leaked data in the top-level 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 */
195 }
196
197 /* We can now handle ereport(ERROR) */
199
200 /*
201 * Unblock signals (they were blocked when the postmaster forked us)
202 */
204
205 /*
206 * Reset hibernation state after any error.
207 */
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);
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 */
252 else if (left_till_hibernate > 0)
253 left_till_hibernate--;
254
255 /* report pending statistics to the cumulative stats system */
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)
265 else
267
270 cur_timeout,
271 WAIT_EVENT_WAL_WRITER_MAIN);
272 }
273}
void pgaio_error_cleanup(void)
void AuxiliaryProcessMainCommon(void)
void AtEOXact_Buffers(bool isCommit)
bool ConditionVariableCancelSleep(void)
void AtEOXact_HashTables(bool isCommit)
void EmitErrorReport(void)
ErrorContextCallback * error_context_stack
void FlushErrorState(void)
sigjmp_buf * PG_exception_stack
void AtEOXact_Files(bool isCommit)
Assert(PointerIsAligned(start, uint64))
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
void ProcessMainLoopInterrupts(void)
void SignalHandlerForConfigReload(SIGNAL_ARGS)
void ResetLatch(Latch *latch)
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
void LWLockReleaseAll(void)
void MemoryContextReset(MemoryContext context)
MemoryContext TopMemoryContext
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define RESUME_INTERRUPTS()
#define HOLD_INTERRUPTS()
BackendType MyBackendType
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
void pgstat_report_wal(bool force)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
void ReleaseAuxProcessResources(bool isCommit)
void pg_usleep(long microsec)
static void pgstat_report_wait_end(void)
#define WL_EXIT_ON_PM_DEATH
#define LOOPS_UNTIL_HIBERNATE
void SetWalWriterSleeping(bool sleeping)
bool XLogBackgroundFlush(void)