PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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{
93 bool hibernating;
94
96
98
99 /*
100 * Properly accept or ignore signals the postmaster might send us
101 */
103 pqsignal(SIGINT, SIG_IGN); /* no query to cancel */
105 /* SIGQUIT handler was already set up by InitPostmasterChild */
109 pqsignal(SIGUSR2, SIG_IGN); /* not used */
110
111 /*
112 * Reset some signals that are accepted by postmaster but not here
113 */
115
116 /*
117 * Create a memory context that we will do all our work in. We do this so
118 * that we can reset the context during error recovery and thereby avoid
119 * possible memory leaks. Formerly this code just ran in
120 * TopMemoryContext, but resetting that would be a really bad idea.
121 */
123 "Wal Writer",
126
127 /*
128 * If an exception is encountered, processing resumes here.
129 *
130 * You might wonder why this isn't coded as an infinite loop around a
131 * PG_TRY construct. The reason is that this is the bottom of the
132 * exception stack, and so with PG_TRY there would be no exception handler
133 * in force at all during the CATCH part. By leaving the outermost setjmp
134 * always active, we have at least some chance of recovering from an error
135 * during error recovery. (If we get into an infinite loop thereby, it
136 * will soon be stopped by overflow of elog.c's internal state stack.)
137 *
138 * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
139 * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
140 * signals other than SIGQUIT will be blocked until we complete error
141 * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
142 * call redundant, but it is not since InterruptPending might be set
143 * already.
144 */
145 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
146 {
147 /* Since not using PG_TRY, must reset error stack by hand */
149
150 /* Prevent interrupts while cleaning up */
152
153 /* Report the error to the server log */
155
156 /*
157 * These operations are really just a minimal subset of
158 * AbortTransaction(). We don't have very many resources to worry
159 * about in walwriter, but we do have LWLocks, and perhaps buffers?
160 */
167 AtEOXact_Buffers(false);
169 AtEOXact_Files(false);
170 AtEOXact_HashTables(false);
171
172 /*
173 * Now return to normal top-level context and clear ErrorContext for
174 * next time.
175 */
178
179 /* Flush any leaked data in the top-level context */
181
182 /* Now we can allow interrupts again */
184
185 /*
186 * Sleep at least 1 second after any error. A write error is likely
187 * to be repeated, and we don't want to be filling the error logs as
188 * fast as we can.
189 */
190 pg_usleep(1000000L);
191 }
192
193 /* We can now handle ereport(ERROR) */
195
196 /*
197 * Unblock signals (they were blocked when the postmaster forked us)
198 */
200
201 /*
202 * Reset hibernation state after any error.
203 */
205 hibernating = false;
207
208 /*
209 * Advertise our proc number that backends can use to wake us up while
210 * we're sleeping.
211 */
213
214 /*
215 * Loop forever
216 */
217 for (;;)
218 {
219 long cur_timeout;
220
221 /*
222 * Advertise whether we might hibernate in this cycle. We do this
223 * before resetting the latch to ensure that any async commits will
224 * see the flag set if they might possibly need to wake us up, and
225 * that we won't miss any signal they send us. (If we discover work
226 * to do in the last cycle before we would hibernate, the global flag
227 * will be set unnecessarily, but little harm is done.) But avoid
228 * touching the global flag if it doesn't need to change.
229 */
230 if (hibernating != (left_till_hibernate <= 1))
231 {
234 }
235
236 /* Clear any already-pending wakeups */
238
239 /* Process any signals received recently */
241
242 /*
243 * Do what we're here for; then, if XLogBackgroundFlush() found useful
244 * work to do, reset hibernation counter.
245 */
248 else if (left_till_hibernate > 0)
250
251 /* report pending statistics to the cumulative stats system */
252 pgstat_report_wal(false);
253
254 /*
255 * Sleep until we are signaled or WalWriterDelay has elapsed. If we
256 * haven't done anything useful for quite some time, lengthen the
257 * sleep time so as to reduce the server's idle power consumption.
258 */
259 if (left_till_hibernate > 0)
260 cur_timeout = WalWriterDelay; /* in ms */
261 else
263
268 }
269}
void pgaio_error_cleanup(void)
Definition aio.c:1165
void AuxiliaryProcessMainCommon(void)
Definition auxprocess.c:39
sigset_t UnBlockSig
Definition pqsignal.c:22
void AtEOXact_Buffers(bool isCommit)
Definition bufmgr.c:4104
void UnlockBuffers(void)
Definition bufmgr.c:5710
#define Assert(condition)
Definition c.h:885
bool ConditionVariableCancelSleep(void)
void AtEOXact_HashTables(bool isCommit)
Definition dynahash.c:1931
void EmitErrorReport(void)
Definition elog.c:1882
ErrorContextCallback * error_context_stack
Definition elog.c:99
void FlushErrorState(void)
Definition elog.c:2062
sigjmp_buf * PG_exception_stack
Definition elog.c:101
void AtEOXact_Files(bool isCommit)
Definition fd.c:3213
ProcNumber MyProcNumber
Definition globals.c:90
struct Latch * MyLatch
Definition globals.c:63
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition interrupt.c:104
void ProcessMainLoopInterrupts(void)
Definition interrupt.c:34
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition interrupt.c:61
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
void LWLockReleaseAll(void)
Definition lwlock.c:1892
void MemoryContextReset(MemoryContext context)
Definition mcxt.c:403
MemoryContext TopMemoryContext
Definition mcxt.c:166
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define RESUME_INTERRUPTS()
Definition miscadmin.h:136
#define HOLD_INTERRUPTS()
Definition miscadmin.h:134
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:547
static int fb(int x)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:679
void ReleaseAuxProcessResources(bool isCommit)
Definition resowner.c:1016
void pg_usleep(long microsec)
Definition signal.c:53
void AtEOXact_SMgr(void)
Definition smgr.c:1017
PROC_HDR * ProcGlobal
Definition proc.c:70
ProcNumber walwriterProc
Definition proc.h:488
static void pgstat_report_wait_end(void)
Definition wait_event.h:85
#define WL_TIMEOUT
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET
#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:9688
bool XLogBackgroundFlush(void)
Definition xlog.c:2988

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), AuxiliaryProcessMainCommon(), ConditionVariableCancelSleep(), EmitErrorReport(), error_context_stack, fb(), FlushErrorState(), HIBERNATE_FACTOR, HOLD_INTERRUPTS, LOOPS_UNTIL_HIBERNATE, LWLockReleaseAll(), MemoryContextReset(), MemoryContextSwitchTo(), 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

◆ WalWriterFlushAfter

int WalWriterFlushAfter = DEFAULT_WAL_WRITER_FLUSH_AFTER

Definition at line 71 of file walwriter.c.

Referenced by XLogBackgroundFlush(), and XLogSetAsyncXactLSN().