PostgreSQL Source Code git master
Loading...
Searching...
No Matches
bgwriter.c File Reference
#include "postgres.h"
#include "access/xlog.h"
#include "libpq/pqsignal.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postmaster/auxprocess.h"
#include "postmaster/bgwriter.h"
#include "postmaster/interrupt.h"
#include "storage/aio_subsys.h"
#include "storage/buf_internals.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 "storage/standby.h"
#include "utils/memutils.h"
#include "utils/resowner.h"
#include "utils/timestamp.h"
Include dependency graph for bgwriter.c:

Go to the source code of this file.

Macros

#define HIBERNATE_FACTOR   50
 
#define LOG_SNAPSHOT_INTERVAL_MS   15000
 

Functions

void BackgroundWriterMain (const void *startup_data, size_t startup_data_len)
 

Variables

int BgWriterDelay = 200
 
static TimestampTz last_snapshot_ts
 
static XLogRecPtr last_snapshot_lsn = InvalidXLogRecPtr
 

Macro Definition Documentation

◆ HIBERNATE_FACTOR

#define HIBERNATE_FACTOR   50

Definition at line 64 of file bgwriter.c.

◆ LOG_SNAPSHOT_INTERVAL_MS

#define LOG_SNAPSHOT_INTERVAL_MS   15000

Definition at line 70 of file bgwriter.c.

Function Documentation

◆ BackgroundWriterMain()

void BackgroundWriterMain ( const void startup_data,
size_t  startup_data_len 
)

Definition at line 88 of file bgwriter.c.

89{
92 bool prev_hibernate;
94
96
98
99 /*
100 * Properly accept or ignore signals that might be sent to us.
101 */
105 /* SIGQUIT handler was already set up by InitPostmasterChild */
110
111 /*
112 * Reset some signals that are accepted by postmaster but not here
113 */
115
116 /*
117 * We just started, assume there has been either a shutdown or
118 * end-of-recovery snapshot.
119 */
121
122 /*
123 * Create a memory context that we will do all our work in. We do this so
124 * that we can reset the context during error recovery and thereby avoid
125 * possible memory leaks. Formerly this code just ran in
126 * TopMemoryContext, but resetting that would be a really bad idea.
127 */
129 "Background Writer",
132
134
135 /*
136 * If an exception is encountered, processing resumes here.
137 *
138 * You might wonder why this isn't coded as an infinite loop around a
139 * PG_TRY construct. The reason is that this is the bottom of the
140 * exception stack, and so with PG_TRY there would be no exception handler
141 * in force at all during the CATCH part. By leaving the outermost setjmp
142 * always active, we have at least some chance of recovering from an error
143 * during error recovery. (If we get into an infinite loop thereby, it
144 * will soon be stopped by overflow of elog.c's internal state stack.)
145 *
146 * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
147 * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
148 * signals other than SIGQUIT will be blocked until we complete error
149 * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
150 * call redundant, but it is not since InterruptPending might be set
151 * already.
152 */
153 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
154 {
155 /* Since not using PG_TRY, must reset error stack by hand */
157
158 /* Prevent interrupts while cleaning up */
160
161 /* Report the error to the server log */
163
164 /*
165 * These operations are really just a minimal subset of
166 * AbortTransaction(). We don't have very many resources to worry
167 * about in bgwriter, but we do have LWLocks, buffers, and temp files.
168 */
174 AtEOXact_Buffers(false);
176 AtEOXact_Files(false);
177 AtEOXact_HashTables(false);
178
179 /*
180 * Now return to normal top-level context and clear ErrorContext for
181 * next time.
182 */
185
186 /* Flush any leaked data in the top-level context */
188
189 /* re-initialize to avoid repeated errors causing problems */
191
192 /* Now we can allow interrupts again */
194
195 /*
196 * Sleep at least 1 second after any error. A write error is likely
197 * to be repeated, and we don't want to be filling the error logs as
198 * fast as we can.
199 */
200 pg_usleep(1000000L);
201
202 /* Report wait end here, when there is no further possibility of wait */
204 }
205
206 /* We can now handle ereport(ERROR) */
208
209 /*
210 * Unblock signals (they were blocked when the postmaster forked us)
211 */
213
214 /*
215 * Reset hibernation state after any error.
216 */
217 prev_hibernate = false;
218
219 /*
220 * Loop forever
221 */
222 for (;;)
223 {
224 bool can_hibernate;
225 int rc;
226
227 /* Clear any already-pending wakeups */
229
231
232 /*
233 * Do one cycle of dirty-buffer writing.
234 */
236
237 /* Report pending statistics to the cumulative stats system */
239 pgstat_report_wal(true);
240
242 {
243 /*
244 * After any checkpoint, free all smgr objects. Otherwise we
245 * would never do so for dropped relations, as the bgwriter does
246 * not process shared invalidation messages or call
247 * AtEOXact_SMgr().
248 */
250 }
251
252 /*
253 * Log a new xl_running_xacts every now and then so replication can
254 * get into a consistent state faster (think of suboverflowed
255 * snapshots) and clean up resources (locks, KnownXids*) more
256 * frequently. The costs of this are relatively low, so doing it 4
257 * times (LOG_SNAPSHOT_INTERVAL_MS) a minute seems fine.
258 *
259 * We assume the interval for writing xl_running_xacts is
260 * significantly bigger than BgWriterDelay, so we don't complicate the
261 * overall timeout handling but just assume we're going to get called
262 * often enough even if hibernation mode is active. It's not that
263 * important that LOG_SNAPSHOT_INTERVAL_MS is met strictly. To make
264 * sure we're not waking the disk up unnecessarily on an idle system
265 * we check whether there has been any WAL inserted since the last
266 * time we've logged a running xacts.
267 *
268 * We do this logging in the bgwriter as it is the only process that
269 * is run regularly and returns to its mainloop all the time. E.g.
270 * Checkpointer, when active, is barely ever in its mainloop and thus
271 * makes it hard to log regularly.
272 */
274 {
277
280
281 /*
282 * Only log if enough time has passed and interesting records have
283 * been inserted since the last snapshot. Have to compare with <=
284 * instead of < because GetLastImportantRecPtr() points at the
285 * start of a record, whereas last_snapshot_lsn points just past
286 * the end of the record.
287 */
288 if (now >= timeout &&
290 {
293 }
294 }
295
296 /*
297 * Sleep until we are signaled or BgWriterDelay has elapsed.
298 *
299 * Note: the feedback control loop in BgBufferSync() expects that we
300 * will call it every BgWriterDelay msec. While it's not critical for
301 * correctness that that be exact, the feedback loop might misbehave
302 * if we stray too far from that. Hence, avoid loading this process
303 * down with latch events that are likely to happen frequently during
304 * normal operation.
305 */
306 rc = WaitLatch(MyLatch,
309
310 /*
311 * If no latch event and BgBufferSync says nothing's happening, extend
312 * the sleep in "hibernation" mode, where we sleep for much longer
313 * than bgwriter_delay says. Fewer wakeups save electricity. When a
314 * backend starts using buffers again, it will wake us up by setting
315 * our latch. Because the extra sleep will persist only as long as no
316 * buffer allocations happen, this should not distort the behavior of
317 * BgBufferSync's control loop too badly; essentially, it will think
318 * that the system-wide idle interval didn't exist.
319 *
320 * There is a race condition here, in that a backend might allocate a
321 * buffer between the time BgBufferSync saw the alloc count as zero
322 * and the time we call StrategyNotifyBgWriter. While it's not
323 * critical that we not hibernate anyway, we try to reduce the odds of
324 * that by only hibernating when BgBufferSync says nothing's happening
325 * for two consecutive cycles. Also, we mitigate any possible
326 * consequences of a missed wakeup by not hibernating forever.
327 */
329 {
330 /* Ask for notification at next buffer allocation */
332 /* Sleep ... */
337 /* Reset the notification request in case we timed out */
339 }
340
342 }
343}
void pgaio_error_cleanup(void)
Definition aio.c:1165
void AuxiliaryProcessMainCommon(void)
Definition auxprocess.c:39
sigset_t UnBlockSig
Definition pqsignal.c:22
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1645
Datum now(PG_FUNCTION_ARGS)
Definition timestamp.c:1609
static XLogRecPtr last_snapshot_lsn
Definition bgwriter.c:78
static TimestampTz last_snapshot_ts
Definition bgwriter.c:77
int BgWriterDelay
Definition bgwriter.c:58
#define HIBERNATE_FACTOR
Definition bgwriter.c:64
#define LOG_SNAPSHOT_INTERVAL_MS
Definition bgwriter.c:70
void AtEOXact_Buffers(bool isCommit)
Definition bufmgr.c:4103
bool BgBufferSync(WritebackContext *wb_context)
Definition bufmgr.c:3735
void UnlockBuffers(void)
Definition bufmgr.c:5709
int bgwriter_flush_after
Definition bufmgr.c:208
void WritebackContextInit(WritebackContext *context, int *max_pending)
Definition bufmgr.c:7267
#define Assert(condition)
Definition c.h:873
bool FirstCallSinceLastCheckpoint(void)
bool ConditionVariableCancelSleep(void)
int64 TimestampTz
Definition timestamp.h:39
void AtEOXact_HashTables(bool isCommit)
Definition dynahash.c:1931
void EmitErrorReport(void)
Definition elog.c:1704
ErrorContextCallback * error_context_stack
Definition elog.c:95
void FlushErrorState(void)
Definition elog.c:1884
sigjmp_buf * PG_exception_stack
Definition elog.c:97
void AtEOXact_Files(bool isCommit)
Definition fd.c:3213
void StrategyNotifyBgWriter(int bgwprocno)
Definition freelist.c:358
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_bgwriter(void)
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:677
void ReleaseAuxProcessResources(bool isCommit)
Definition resowner.c:1016
void pg_usleep(long microsec)
Definition signal.c:53
void smgrdestroyall(void)
Definition smgr.c:386
void AtEOXact_SMgr(void)
Definition smgr.c:1017
XLogRecPtr LogStandbySnapshot(void)
Definition standby.c:1281
#define TimestampTzPlusMilliseconds(tz, ms)
Definition timestamp.h:85
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 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
bool RecoveryInProgress(void)
Definition xlog.c:6460
XLogRecPtr GetLastImportantRecPtr(void)
Definition xlog.c:6682
#define XLogStandbyInfoActive()
Definition xlog.h:125

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), AuxiliaryProcessMainCommon(), BgBufferSync(), bgwriter_flush_after, BgWriterDelay, ConditionVariableCancelSleep(), EmitErrorReport(), error_context_stack, fb(), FirstCallSinceLastCheckpoint(), FlushErrorState(), GetCurrentTimestamp(), GetLastImportantRecPtr(), HIBERNATE_FACTOR, HOLD_INTERRUPTS, last_snapshot_lsn, last_snapshot_ts, LOG_SNAPSHOT_INTERVAL_MS, LogStandbySnapshot(), LWLockReleaseAll(), MemoryContextReset(), MemoryContextSwitchTo(), MyLatch, MyProcNumber, now(), PG_exception_stack, pg_usleep(), pgaio_error_cleanup(), pgstat_report_bgwriter(), pgstat_report_wait_end(), pgstat_report_wal(), pqsignal, ProcessMainLoopInterrupts(), procsignal_sigusr1_handler(), RecoveryInProgress(), ReleaseAuxProcessResources(), ResetLatch(), RESUME_INTERRUPTS, SIGALRM, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, smgrdestroyall(), StrategyNotifyBgWriter(), TimestampTzPlusMilliseconds, TopMemoryContext, UnBlockSig, UnlockBuffers(), WaitLatch(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_TIMEOUT, WritebackContextInit(), and XLogStandbyInfoActive.

Variable Documentation

◆ BgWriterDelay

int BgWriterDelay = 200

Definition at line 58 of file bgwriter.c.

Referenced by BackgroundWriterMain(), and BgBufferSync().

◆ last_snapshot_lsn

XLogRecPtr last_snapshot_lsn = InvalidXLogRecPtr
static

Definition at line 78 of file bgwriter.c.

Referenced by BackgroundWriterMain().

◆ last_snapshot_ts

TimestampTz last_snapshot_ts
static

Definition at line 77 of file bgwriter.c.

Referenced by BackgroundWriterMain().