PostgreSQL Source Code git master
Loading...
Searching...
No Matches
walwriter.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define DEFAULT_WAL_WRITER_FLUSH_AFTER   ((1024 * 1024) / XLOG_BLCKSZ)
 

Functions

pg_noreturn void WalWriterMain (const void *startup_data, size_t startup_data_len)
 

Variables

PGDLLIMPORT int WalWriterDelay
 
PGDLLIMPORT int WalWriterFlushAfter
 

Macro Definition Documentation

◆ DEFAULT_WAL_WRITER_FLUSH_AFTER

#define DEFAULT_WAL_WRITER_FLUSH_AFTER   ((1024 * 1024) / XLOG_BLCKSZ)

Definition at line 15 of file walwriter.h.

Function Documentation

◆ WalWriterMain()

pg_noreturn void WalWriterMain ( const void startup_data,
size_t  startup_data_len 
)
extern

Definition at line 89 of file walwriter.c.

90{
94 bool hibernating;
95
97
99
100 /*
101 * Properly accept or ignore signals the postmaster might send us
102 */
104 pqsignal(SIGINT, SIG_IGN); /* no query to cancel */
106 /* SIGQUIT handler was already set up by InitPostmasterChild */
110 pqsignal(SIGUSR2, SIG_IGN); /* not used */
111
112 /*
113 * Reset some signals that are accepted by postmaster but not here
114 */
116
117 /*
118 * Create a memory context that we will do all our work in. We do this so
119 * that we can reset the context during error recovery and thereby avoid
120 * possible memory leaks. Formerly this code just ran in
121 * TopMemoryContext, but resetting that would be a really bad idea.
122 */
124 "Wal Writer",
127
128 /*
129 * If an exception is encountered, processing resumes here.
130 *
131 * You might wonder why this isn't coded as an infinite loop around a
132 * PG_TRY construct. The reason is that this is the bottom of the
133 * exception stack, and so with PG_TRY there would be no exception handler
134 * in force at all during the CATCH part. By leaving the outermost setjmp
135 * always active, we have at least some chance of recovering from an error
136 * during error recovery. (If we get into an infinite loop thereby, it
137 * will soon be stopped by overflow of elog.c's internal state stack.)
138 *
139 * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
140 * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
141 * signals other than SIGQUIT will be blocked until we complete error
142 * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
143 * call redundant, but it is not since InterruptPending might be set
144 * already.
145 */
146 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
147 {
148 /* Since not using PG_TRY, must reset error stack by hand */
150
151 /* Prevent interrupts while cleaning up */
153
154 /* Report the error to the server log */
156
157 /*
158 * These operations are really just a minimal subset of
159 * AbortTransaction(). We don't have very many resources to worry
160 * about in walwriter, but we do have LWLocks, and perhaps buffers?
161 */
168 AtEOXact_Buffers(false);
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 */
179
180 /* Flush any leaked data in the top-level 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 /* We can now handle ereport(ERROR) */
196
197 /*
198 * Unblock signals (they were blocked when the postmaster forked us)
199 */
201
202 /*
203 * Reset hibernation state after any error.
204 */
206 hibernating = false;
208
209 /*
210 * Advertise our proc number that backends can use to wake us up while
211 * we're sleeping.
212 */
214
215 /*
216 * Loop forever
217 */
218 for (;;)
219 {
220 long cur_timeout;
221
222 /*
223 * Advertise whether we might hibernate in this cycle. We do this
224 * before resetting the latch to ensure that any async commits will
225 * see the flag set if they might possibly need to wake us up, and
226 * that we won't miss any signal they send us. (If we discover work
227 * to do in the last cycle before we would hibernate, the global flag
228 * will be set unnecessarily, but little harm is done.) But avoid
229 * touching the global flag if it doesn't need to change.
230 */
231 if (hibernating != (left_till_hibernate <= 1))
232 {
235 }
236
237 /* Clear any already-pending wakeups */
239
240 /* Process any signals received recently */
242
243 /*
244 * Do what we're here for; then, if XLogBackgroundFlush() found useful
245 * work to do, reset hibernation counter.
246 */
249 else if (left_till_hibernate > 0)
251
252 /* report pending statistics to the cumulative stats system */
253 pgstat_report_wal(false);
254
255 /*
256 * Sleep until we are signaled or WalWriterDelay has elapsed. If we
257 * haven't done anything useful for quite some time, lengthen the
258 * sleep time so as to reduce the server's idle power consumption.
259 */
260 if (left_till_hibernate > 0)
261 cur_timeout = WalWriterDelay; /* in ms */
262 else
264
269 }
270}
void pgaio_error_cleanup(void)
Definition aio.c:1165
void AuxiliaryProcessMainCommon(void)
Definition auxprocess.c:40
sigset_t UnBlockSig
Definition pqsignal.c:22
void AtEOXact_Buffers(bool isCommit)
Definition bufmgr.c:4110
void UnlockBuffers(void)
Definition bufmgr.c:5719
#define Assert(condition)
Definition c.h:945
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:3214
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:1893
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:680
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:71
ProcNumber walwriterProc
Definition proc.h:485
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:80
#define LOOPS_UNTIL_HIBERNATE
Definition walwriter.c:79
int WalWriterDelay
Definition walwriter.c:71
#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:9710
bool XLogBackgroundFlush(void)
Definition xlog.c:2972

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

PGDLLIMPORT int WalWriterDelay
extern

◆ WalWriterFlushAfter

PGDLLIMPORT int WalWriterFlushAfter
extern

Definition at line 72 of file walwriter.c.

Referenced by XLogBackgroundFlush(), and XLogSetAsyncXactLSN().