1 /*-------------------------------------------------------------------------
5 * The background writer (bgwriter) is new as of Postgres 8.0. It attempts
6 * to keep regular backends from having to write out dirty shared buffers
7 * (which they would only do when needing to free a shared buffer to read in
8 * another page). In the best scenario all writes from shared buffers will
9 * be issued by the background writer process. However, regular backends are
10 * still empowered to issue writes if the bgwriter fails to maintain enough
11 * clean shared buffers.
13 * The bgwriter is also charged with handling all checkpoints. It will
14 * automatically dispatch a checkpoint after a certain amount of time has
15 * elapsed since the last one, and it can be signaled to perform requested
16 * checkpoints as well. (The GUC parameter that mandates a checkpoint every
17 * so many WAL segments is implemented by having backends signal the bgwriter
18 * when they fill WAL segments; the bgwriter itself doesn't watch for the
21 * The bgwriter is started by the postmaster as soon as the startup subprocess
22 * finishes. It remains alive until the postmaster commands it to terminate.
23 * Normal termination is by SIGUSR2, which instructs the bgwriter to execute
24 * a shutdown checkpoint and then exit(0). (All backends must be stopped
25 * before SIGUSR2 is issued!) Emergency termination is by SIGQUIT; like any
26 * backend, the bgwriter will simply abort and exit on SIGQUIT.
28 * If the bgwriter exits unexpectedly, the postmaster treats that the same
29 * as a backend crash: shared memory may be corrupted, so remaining backends
30 * should be killed by SIGQUIT and then a recovery cycle started. (Even if
31 * shared memory isn't corrupted, we have lost information about which
32 * files need to be fsync'd for the next checkpoint, and so a system
33 * restart needs to be forced.)
36 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
40 * $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.47 2007/11/15 21:14:37 momjian Exp $
42 *-------------------------------------------------------------------------
51 #include "access/xlog_internal.h"
52 #include "libpq/pqsignal.h"
53 #include "miscadmin.h"
55 #include "postmaster/bgwriter.h"
56 #include "storage/fd.h"
57 #include "storage/freespace.h"
58 #include "storage/ipc.h"
59 #include "storage/lwlock.h"
60 #include "storage/pmsignal.h"
61 #include "storage/shmem.h"
62 #include "storage/smgr.h"
63 #include "storage/spin.h"
64 #include "tcop/tcopprot.h"
65 #include "utils/guc.h"
66 #include "utils/memutils.h"
67 #include "utils/resowner.h"
71 * Shared memory area for communication between bgwriter and backends
73 * The ckpt counters allow backends to watch for completion of a checkpoint
74 * request they send. Here's how it works:
75 * * At start of a checkpoint, bgwriter reads (and clears) the request flags
76 * and increments ckpt_started, while holding ckpt_lck.
77 * * On completion of a checkpoint, bgwriter sets ckpt_done to
79 * * On failure of a checkpoint, bgwriter increments ckpt_failed
80 * and sets ckpt_done to equal ckpt_started.
82 * The algorithm for backends is:
83 * 1. Record current values of ckpt_failed and ckpt_started, and
84 * set request flags, while holding ckpt_lck.
85 * 2. Send signal to request checkpoint.
86 * 3. Sleep until ckpt_started changes. Now you know a checkpoint has
87 * begun since you started this algorithm (although *not* that it was
88 * specifically initiated by your signal), and that it is using your flags.
89 * 4. Record new value of ckpt_started.
90 * 5. Sleep until ckpt_done >= saved value of ckpt_started. (Use modulo
91 * arithmetic here in case counters wrap around.) Now you know a
92 * checkpoint has started and completed, but not whether it was
94 * 6. If ckpt_failed is different from the originally saved value,
95 * assume request failed; otherwise it was definitely successful.
97 * ckpt_flags holds the OR of the checkpoint request flags sent by all
98 * requesting backends since the last checkpoint start. The flags are
99 * chosen so that OR'ing is the correct way to combine multiple requests.
101 * num_backend_writes is used to count the number of buffer writes performed
102 * by non-bgwriter processes. This counter should be wide enough that it
103 * can't overflow during a single bgwriter cycle.
105 * The requests array holds fsync requests sent by backends and not yet
106 * absorbed by the bgwriter.
108 * Unlike the checkpoint fields, num_backend_writes and the requests
109 * fields are protected by BgWriterCommLock.
115 BlockNumber segno; /* see md.c for special values */
116 /* might add a real request-type field later; not needed yet */
121 pid_t bgwriter_pid; /* PID of bgwriter (0 if not started) */
123 slock_t ckpt_lck; /* protects all the ckpt_* fields */
125 int ckpt_started; /* advances when checkpoint starts */
126 int ckpt_done; /* advances when checkpoint done */
127 int ckpt_failed; /* advances when checkpoint fails */
129 int ckpt_flags; /* checkpoint flags, as defined in xlog.h */
131 uint32 num_backend_writes; /* counts non-bgwriter buffer writes */
133 int num_requests; /* current # of requests */
134 int max_requests; /* allocated array size */
135 BgWriterRequest requests[1]; /* VARIABLE LENGTH ARRAY */
136 } BgWriterShmemStruct;
138 static BgWriterShmemStruct *BgWriterShmem;
140 /* interval for calling AbsorbFsyncRequests in CheckpointWriteDelay */
141 #define WRITES_PER_ABSORB 1000
146 int BgWriterDelay = 200;
147 int CheckPointTimeout = 300;
148 int CheckPointWarning = 30;
149 double CheckPointCompletionTarget = 0.5;
152 * Flags set by interrupt handlers for later service in the main loop.
154 static volatile sig_atomic_t got_SIGHUP = false;
155 static volatile sig_atomic_t checkpoint_requested = false;
156 static volatile sig_atomic_t shutdown_requested = false;
161 static bool am_bg_writer = false;
163 static bool ckpt_active = false;
165 /* these values are valid when ckpt_active is true: */
166 static time_t ckpt_start_time;
167 static XLogRecPtr ckpt_start_recptr;
168 static double ckpt_cached_elapsed;
170 static time_t last_checkpoint_time;
171 static time_t last_xlog_switch_time;
173 /* Prototypes for private functions */
175 static void CheckArchiveTimeout(void);
176 static void BgWriterNap(void);
177 static bool IsCheckpointOnSchedule(double progress);
178 static bool ImmediateCheckpointRequested(void);
180 /* Signal handlers */
182 static void bg_quickdie(SIGNAL_ARGS);
183 static void BgSigHupHandler(SIGNAL_ARGS);
184 static void ReqCheckpointHandler(SIGNAL_ARGS);
185 static void ReqShutdownHandler(SIGNAL_ARGS);
189 * Main entry point for bgwriter process
191 * This is invoked from BootstrapMain, which has already created the basic
192 * execution environment, but not enabled signals yet.
195 BackgroundWriterMain(void)
197 sigjmp_buf local_sigjmp_buf;
198 MemoryContext bgwriter_context;
200 BgWriterShmem->bgwriter_pid = MyProcPid;
204 * If possible, make this process a group leader, so that the postmaster
205 * can signal any child processes too. (bgwriter probably never has any
206 * child processes, but for consistency we make all postmaster child
207 * processes do this.)
211 elog(FATAL, "setsid() failed: %m");
215 * Properly accept or ignore signals the postmaster might send us
217 * Note: we deliberately ignore SIGTERM, because during a standard Unix
218 * system shutdown cycle, init will SIGTERM all processes at once. We
219 * want to wait for the backends to exit, whereupon the postmaster will
220 * tell us it's okay to shut down (via SIGUSR2).
222 * SIGUSR1 is presently unused; keep it spare in case someday we want this
223 * process to participate in sinval messaging.
225 pqsignal(SIGHUP, BgSigHupHandler); /* set flag to read config file */
226 pqsignal(SIGINT, ReqCheckpointHandler); /* request checkpoint */
227 pqsignal(SIGTERM, SIG_IGN); /* ignore SIGTERM */
228 pqsignal(SIGQUIT, bg_quickdie); /* hard crash time */
229 pqsignal(SIGALRM, SIG_IGN);
230 pqsignal(SIGPIPE, SIG_IGN);
231 pqsignal(SIGUSR1, SIG_IGN); /* reserve for sinval */
232 pqsignal(SIGUSR2, ReqShutdownHandler); /* request shutdown */
235 * Reset some signals that are accepted by postmaster but not here
237 pqsignal(SIGCHLD, SIG_DFL);
238 pqsignal(SIGTTIN, SIG_DFL);
239 pqsignal(SIGTTOU, SIG_DFL);
240 pqsignal(SIGCONT, SIG_DFL);
241 pqsignal(SIGWINCH, SIG_DFL);
243 /* We allow SIGQUIT (quickdie) at all times */
244 #ifdef HAVE_SIGPROCMASK
245 sigdelset(&BlockSig, SIGQUIT);
247 BlockSig &= ~(sigmask(SIGQUIT));
251 * Initialize so that first time-driven event happens at the correct time.
253 last_checkpoint_time = last_xlog_switch_time = time(NULL);
256 * Create a resource owner to keep track of our resources (currently only
259 CurrentResourceOwner = ResourceOwnerCreate(NULL, "Background Writer");
262 * Create a memory context that we will do all our work in. We do this so
263 * that we can reset the context during error recovery and thereby avoid
264 * possible memory leaks. Formerly this code just ran in
265 * TopMemoryContext, but resetting that would be a really bad idea.
267 bgwriter_context = AllocSetContextCreate(TopMemoryContext,
269 ALLOCSET_DEFAULT_MINSIZE,
270 ALLOCSET_DEFAULT_INITSIZE,
271 ALLOCSET_DEFAULT_MAXSIZE);
272 MemoryContextSwitchTo(bgwriter_context);
275 * If an exception is encountered, processing resumes here.
277 * See notes in postgres.c about the design of this coding.
279 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
281 /* Since not using PG_TRY, must reset error stack by hand */
282 error_context_stack = NULL;
284 /* Prevent interrupts while cleaning up */
287 /* Report the error to the server log */
291 * These operations are really just a minimal subset of
292 * AbortTransaction(). We don't have very many resources to worry
293 * about in bgwriter, but we do have LWLocks, buffers, and temp files.
298 /* buffer pins are released here: */
299 ResourceOwnerRelease(CurrentResourceOwner,
300 RESOURCE_RELEASE_BEFORE_LOCKS,
302 /* we needn't bother with the other ResourceOwnerRelease phases */
303 AtEOXact_Buffers(false);
305 AtEOXact_HashTables(false);
307 /* Warn any waiting backends that the checkpoint failed. */
310 /* use volatile pointer to prevent code rearrangement */
311 volatile BgWriterShmemStruct *bgs = BgWriterShmem;
313 SpinLockAcquire(&bgs->ckpt_lck);
315 bgs->ckpt_done = bgs->ckpt_started;
316 SpinLockRelease(&bgs->ckpt_lck);
322 * Now return to normal top-level context and clear ErrorContext for
325 MemoryContextSwitchTo(bgwriter_context);
328 /* Flush any leaked data in the top-level context */
329 MemoryContextResetAndDeleteChildren(bgwriter_context);
331 /* Now we can allow interrupts again */
335 * Sleep at least 1 second after any error. A write error is likely
336 * to be repeated, and we don't want to be filling the error logs as
342 * Close all open files after any error. This is helpful on Windows,
343 * where holding deleted files open causes various strange errors.
344 * It's not clear we need it elsewhere, but shouldn't hurt.
349 /* We can now handle ereport(ERROR) */
350 PG_exception_stack = &local_sigjmp_buf;
353 * Unblock signals (they were blocked when the postmaster forked us)
355 PG_SETMASK(&UnBlockSig);
362 bool do_checkpoint = false;
368 * Emergency bailout if postmaster has died. This is to avoid the
369 * necessity for manual cleanup of all postmaster children.
371 if (!PostmasterIsAlive(true))
375 * Process any requests or signals received recently.
377 AbsorbFsyncRequests();
382 ProcessConfigFile(PGC_SIGHUP);
384 if (checkpoint_requested)
386 checkpoint_requested = false;
387 do_checkpoint = true;
388 BgWriterStats.m_requested_checkpoints++;
390 if (shutdown_requested)
393 * From here on, elog(ERROR) should end with exit(1), not send
394 * control back to the sigsetjmp block above
396 ExitOnAnyError = true;
397 /* Close down the database */
399 DumpFreeSpaceMap(0, 0);
400 /* Normal exit from the bgwriter is here */
401 proc_exit(0); /* done */
405 * Force a checkpoint if too much time has elapsed since the last one.
406 * Note that we count a timed checkpoint in stats only when this
407 * occurs without an external request, but we set the CAUSE_TIME flag
408 * bit even if there is also an external request.
411 elapsed_secs = now - last_checkpoint_time;
412 if (elapsed_secs >= CheckPointTimeout)
415 BgWriterStats.m_timed_checkpoints++;
416 do_checkpoint = true;
417 flags |= CHECKPOINT_CAUSE_TIME;
421 * Do a checkpoint if requested, otherwise do one cycle of
422 * dirty-buffer writing.
426 /* use volatile pointer to prevent code rearrangement */
427 volatile BgWriterShmemStruct *bgs = BgWriterShmem;
430 * Atomically fetch the request flags to figure out what kind of a
431 * checkpoint we should perform, and increase the started-counter
432 * to acknowledge that we've started a new checkpoint.
434 SpinLockAcquire(&bgs->ckpt_lck);
435 flags |= bgs->ckpt_flags;
438 SpinLockRelease(&bgs->ckpt_lck);
441 * We will warn if (a) too soon since last checkpoint (whatever
442 * caused it) and (b) somebody set the CHECKPOINT_CAUSE_XLOG flag
443 * since the last checkpoint start. Note in particular that this
444 * implementation will not generate warnings caused by
445 * CheckPointTimeout < CheckPointWarning.
447 if ((flags & CHECKPOINT_CAUSE_XLOG) &&
448 elapsed_secs < CheckPointWarning)
450 (errmsg("checkpoints are occurring too frequently (%d seconds apart)",
452 errhint("Consider increasing the configuration parameter \"checkpoint_segments\".")));
455 * Initialize bgwriter-private variables used during checkpoint.
458 ckpt_start_recptr = GetInsertRecPtr();
459 ckpt_start_time = now;
460 ckpt_cached_elapsed = 0;
465 CreateCheckPoint(flags);
468 * After any checkpoint, close all smgr files. This is so we
469 * won't hang onto smgr references to deleted files indefinitely.
474 * Indicate checkpoint completion to any waiting backends.
476 SpinLockAcquire(&bgs->ckpt_lck);
477 bgs->ckpt_done = bgs->ckpt_started;
478 SpinLockRelease(&bgs->ckpt_lck);
483 * Note we record the checkpoint start time not end time as
484 * last_checkpoint_time. This is so that time-driven checkpoints
485 * happen at a predictable spacing.
487 last_checkpoint_time = now;
492 /* Check for archive_timeout and switch xlog files if necessary. */
493 CheckArchiveTimeout();
495 /* Nap for the configured time. */
501 * CheckArchiveTimeout -- check for archive_timeout and switch xlog files
505 CheckArchiveTimeout(void)
510 if (XLogArchiveTimeout <= 0)
515 /* First we do a quick check using possibly-stale local state. */
516 if ((int) (now - last_xlog_switch_time) < XLogArchiveTimeout)
520 * Update local state ... note that last_xlog_switch_time is the last time
521 * a switch was performed *or requested*.
523 last_time = GetLastSegSwitchTime();
525 last_xlog_switch_time = Max(last_xlog_switch_time, last_time);
527 /* Now we can do the real check */
528 if ((int) (now - last_xlog_switch_time) >= XLogArchiveTimeout)
530 XLogRecPtr switchpoint;
532 /* OK, it's time to switch */
533 switchpoint = RequestXLogSwitch();
536 * If the returned pointer points exactly to a segment boundary,
537 * assume nothing happened.
539 if ((switchpoint.xrecoff % XLogSegSize) != 0)
541 (errmsg("transaction log switch forced (archive_timeout=%d)",
542 XLogArchiveTimeout)));
545 * Update state in any case, so we don't retry constantly when the
548 last_xlog_switch_time = now;
553 * BgWriterNap -- Nap for the configured time or until a signal is received.
561 * Send off activity statistics to the stats collector
563 pgstat_send_bgwriter();
566 * Nap for the configured time, or sleep for 10 seconds if there is no
567 * bgwriter activity configured.
569 * On some platforms, signals won't interrupt the sleep. To ensure we
570 * respond reasonably promptly when someone signals us, break down the
571 * sleep into 1-second increments, and check for interrupts after each
574 * We absorb pending requests after each short sleep.
576 if (bgwriter_lru_maxpages > 0 || ckpt_active)
577 udelay = BgWriterDelay * 1000L;
578 else if (XLogArchiveTimeout > 0)
579 udelay = 1000000L; /* One second */
581 udelay = 10000000L; /* Ten seconds */
583 while (udelay > 999999L)
585 if (got_SIGHUP || shutdown_requested ||
586 (ckpt_active ? ImmediateCheckpointRequested() : checkpoint_requested))
589 AbsorbFsyncRequests();
593 if (!(got_SIGHUP || shutdown_requested ||
594 (ckpt_active ? ImmediateCheckpointRequested() : checkpoint_requested)))
599 * Returns true if an immediate checkpoint request is pending. (Note that
600 * this does not check the *current* checkpoint's IMMEDIATE flag, but whether
601 * there is one pending behind it.)
604 ImmediateCheckpointRequested(void)
606 if (checkpoint_requested)
608 volatile BgWriterShmemStruct *bgs = BgWriterShmem;
611 * We don't need to acquire the ckpt_lck in this case because we're
612 * only looking at a single flag bit.
614 if (bgs->ckpt_flags & CHECKPOINT_IMMEDIATE)
621 * CheckpointWriteDelay -- yield control to bgwriter during a checkpoint
623 * This function is called after each page write performed by BufferSync().
624 * It is responsible for keeping the bgwriter's normal activities in
625 * progress during a long checkpoint, and for throttling BufferSync()'s
626 * write rate to hit checkpoint_completion_target.
628 * The checkpoint request flags should be passed in; currently the only one
629 * examined is CHECKPOINT_IMMEDIATE, which disables delays between writes.
631 * 'progress' is an estimate of how much of the work has been done, as a
632 * fraction between 0.0 meaning none, and 1.0 meaning all done.
635 CheckpointWriteDelay(int flags, double progress)
637 static int absorb_counter = WRITES_PER_ABSORB;
639 /* Do nothing if checkpoint is being executed by non-bgwriter process */
644 * Perform the usual bgwriter duties and take a nap, unless we're behind
645 * schedule, in which case we just try to catch up as quickly as possible.
647 if (!(flags & CHECKPOINT_IMMEDIATE) &&
648 !shutdown_requested &&
649 !ImmediateCheckpointRequested() &&
650 IsCheckpointOnSchedule(progress))
655 ProcessConfigFile(PGC_SIGHUP);
658 AbsorbFsyncRequests();
659 absorb_counter = WRITES_PER_ABSORB;
662 CheckArchiveTimeout();
665 else if (--absorb_counter <= 0)
668 * Absorb pending fsync requests after each WRITES_PER_ABSORB write
669 * operations even when we don't sleep, to prevent overflow of the
670 * fsync request queue.
672 AbsorbFsyncRequests();
673 absorb_counter = WRITES_PER_ABSORB;
678 * IsCheckpointOnSchedule -- are we on schedule to finish this checkpoint
681 * Compares the current progress against the time/segments elapsed since last
682 * checkpoint, and returns true if the progress we've made this far is greater
683 * than the elapsed time/segments.
686 IsCheckpointOnSchedule(double progress)
690 double elapsed_xlogs,
695 /* Scale progress according to checkpoint_completion_target. */
696 progress *= CheckPointCompletionTarget;
699 * Check against the cached value first. Only do the more expensive
700 * calculations once we reach the target previously calculated. Since
701 * neither time or WAL insert pointer moves backwards, a freshly
702 * calculated value can only be greater than or equal to the cached value.
704 if (progress < ckpt_cached_elapsed)
708 * Check progress against WAL segments written and checkpoint_segments.
710 * We compare the current WAL insert location against the location
711 * computed before calling CreateCheckPoint. The code in XLogInsert that
712 * actually triggers a checkpoint when checkpoint_segments is exceeded
713 * compares against RedoRecptr, so this is not completely accurate.
714 * However, it's good enough for our purposes, we're only calculating an
717 recptr = GetInsertRecPtr();
719 (((double) (int32) (recptr.xlogid - ckpt_start_recptr.xlogid)) * XLogSegsPerFile +
720 ((double) recptr.xrecoff - (double) ckpt_start_recptr.xrecoff) / XLogSegSize) /
723 if (progress < elapsed_xlogs)
725 ckpt_cached_elapsed = elapsed_xlogs;
730 * Check progress against time elapsed and checkpoint_timeout.
732 gettimeofday(&now, NULL);
733 elapsed_time = ((double) (now.tv_sec - ckpt_start_time) +
734 now.tv_usec / 1000000.0) / CheckPointTimeout;
736 if (progress < elapsed_time)
738 ckpt_cached_elapsed = elapsed_time;
742 /* It looks like we're on schedule. */
747 /* --------------------------------
748 * signal handler routines
749 * --------------------------------
753 * bg_quickdie() occurs when signalled SIGQUIT by the postmaster.
755 * Some backend has bought the farm,
756 * so we need to stop what we're doing and exit.
759 bg_quickdie(SIGNAL_ARGS)
761 PG_SETMASK(&BlockSig);
764 * DO NOT proc_exit() -- we're here because shared memory may be
765 * corrupted, so we don't want to try to clean up our transaction. Just
766 * nail the windows shut and get out of town.
768 * Note we do exit(2) not exit(0). This is to force the postmaster into a
769 * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
770 * backend. This is necessary precisely because we don't clean up our
771 * shared memory state.
776 /* SIGHUP: set flag to re-read config file at next convenient time */
778 BgSigHupHandler(SIGNAL_ARGS)
783 /* SIGINT: set flag to run a normal checkpoint right away */
785 ReqCheckpointHandler(SIGNAL_ARGS)
787 checkpoint_requested = true;
790 /* SIGUSR2: set flag to run a shutdown checkpoint and exit */
792 ReqShutdownHandler(SIGNAL_ARGS)
794 shutdown_requested = true;
798 /* --------------------------------
799 * communication with backends
800 * --------------------------------
805 * Compute space needed for bgwriter-related shared memory
808 BgWriterShmemSize(void)
813 * Currently, the size of the requests[] array is arbitrarily set equal to
814 * NBuffers. This may prove too large or small ...
816 size = offsetof(BgWriterShmemStruct, requests);
817 size = add_size(size, mul_size(NBuffers, sizeof(BgWriterRequest)));
824 * Allocate and initialize bgwriter-related shared memory
827 BgWriterShmemInit(void)
831 BgWriterShmem = (BgWriterShmemStruct *)
832 ShmemInitStruct("Background Writer Data",
835 if (BgWriterShmem == NULL)
837 (errcode(ERRCODE_OUT_OF_MEMORY),
838 errmsg("not enough shared memory for background writer")));
840 return; /* already initialized */
842 MemSet(BgWriterShmem, 0, sizeof(BgWriterShmemStruct));
843 SpinLockInit(&BgWriterShmem->ckpt_lck);
844 BgWriterShmem->max_requests = NBuffers;
849 * Called in backend processes to request a checkpoint
851 * flags is a bitwise OR of the following:
852 * CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown.
853 * CHECKPOINT_IMMEDIATE: finish the checkpoint ASAP,
854 * ignoring checkpoint_completion_target parameter.
855 * CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occured
856 * since the last one (implied by CHECKPOINT_IS_SHUTDOWN).
857 * CHECKPOINT_WAIT: wait for completion before returning (otherwise,
858 * just signal bgwriter to do it, and return).
859 * CHECKPOINT_CAUSE_XLOG: checkpoint is requested due to xlog filling.
860 * (This affects logging, and in particular enables CheckPointWarning.)
863 RequestCheckpoint(int flags)
865 /* use volatile pointer to prevent code rearrangement */
866 volatile BgWriterShmemStruct *bgs = BgWriterShmem;
871 * If in a standalone backend, just do it ourselves.
873 if (!IsPostmasterEnvironment)
876 * There's no point in doing slow checkpoints in a standalone backend,
877 * because there's no other backends the checkpoint could disrupt.
879 CreateCheckPoint(flags | CHECKPOINT_IMMEDIATE);
882 * After any checkpoint, close all smgr files. This is so we won't
883 * hang onto smgr references to deleted files indefinitely.
891 * Atomically set the request flags, and take a snapshot of the counters.
892 * When we see ckpt_started > old_started, we know the flags we set here
893 * have been seen by bgwriter.
895 * Note that we OR the flags with any existing flags, to avoid overriding
896 * a "stronger" request by another backend. The flag senses must be
897 * chosen to make this work!
899 SpinLockAcquire(&bgs->ckpt_lck);
901 old_failed = bgs->ckpt_failed;
902 old_started = bgs->ckpt_started;
903 bgs->ckpt_flags |= flags;
905 SpinLockRelease(&bgs->ckpt_lck);
908 * Send signal to request checkpoint. When not waiting, we consider
909 * failure to send the signal to be nonfatal.
911 if (BgWriterShmem->bgwriter_pid == 0)
912 elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
913 "could not request checkpoint because bgwriter not running");
914 if (kill(BgWriterShmem->bgwriter_pid, SIGINT) != 0)
915 elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
916 "could not signal for checkpoint: %m");
919 * If requested, wait for completion. We detect completion according to
920 * the algorithm given above.
922 if (flags & CHECKPOINT_WAIT)
927 /* Wait for a new checkpoint to start. */
930 SpinLockAcquire(&bgs->ckpt_lck);
931 new_started = bgs->ckpt_started;
932 SpinLockRelease(&bgs->ckpt_lck);
934 if (new_started != old_started)
937 CHECK_FOR_INTERRUPTS();
942 * We are waiting for ckpt_done >= new_started, in a modulo sense.
948 SpinLockAcquire(&bgs->ckpt_lck);
949 new_done = bgs->ckpt_done;
950 new_failed = bgs->ckpt_failed;
951 SpinLockRelease(&bgs->ckpt_lck);
953 if (new_done - new_started >= 0)
956 CHECK_FOR_INTERRUPTS();
960 if (new_failed != old_failed)
962 (errmsg("checkpoint request failed"),
963 errhint("Consult recent messages in the server log for details.")));
968 * ForwardFsyncRequest
969 * Forward a file-fsync request from a backend to the bgwriter
971 * Whenever a backend is compelled to write directly to a relation
972 * (which should be seldom, if the bgwriter is getting its job done),
973 * the backend calls this routine to pass over knowledge that the relation
974 * is dirty and must be fsync'd before next checkpoint. We also use this
975 * opportunity to count such writes for statistical purposes.
977 * segno specifies which segment (not block!) of the relation needs to be
978 * fsync'd. (Since the valid range is much less than BlockNumber, we can
979 * use high values for special flags; that's all internal to md.c, which
982 * If we are unable to pass over the request (at present, this can happen
983 * if the shared memory queue is full), we return false. That forces
984 * the backend to do its own fsync. We hope that will be even more seldom.
986 * Note: we presently make no attempt to eliminate duplicate requests
987 * in the requests[] queue. The bgwriter will have to eliminate dups
988 * internally anyway, so we may as well avoid holding the lock longer
989 * than we have to here.
992 ForwardFsyncRequest(RelFileNode rnode, BlockNumber segno)
994 BgWriterRequest *request;
996 if (!IsUnderPostmaster)
997 return false; /* probably shouldn't even get here */
1000 elog(ERROR, "ForwardFsyncRequest must not be called in bgwriter");
1002 LWLockAcquire(BgWriterCommLock, LW_EXCLUSIVE);
1004 /* we count non-bgwriter writes even when the request queue overflows */
1005 BgWriterShmem->num_backend_writes++;
1007 if (BgWriterShmem->bgwriter_pid == 0 ||
1008 BgWriterShmem->num_requests >= BgWriterShmem->max_requests)
1010 LWLockRelease(BgWriterCommLock);
1013 request = &BgWriterShmem->requests[BgWriterShmem->num_requests++];
1014 request->rnode = rnode;
1015 request->segno = segno;
1016 LWLockRelease(BgWriterCommLock);
1021 * AbsorbFsyncRequests
1022 * Retrieve queued fsync requests and pass them to local smgr.
1024 * This is exported because it must be called during CreateCheckPoint;
1025 * we have to be sure we have accepted all pending requests just before
1026 * we start fsync'ing. Since CreateCheckPoint sometimes runs in
1027 * non-bgwriter processes, do nothing if not bgwriter.
1030 AbsorbFsyncRequests(void)
1032 BgWriterRequest *requests = NULL;
1033 BgWriterRequest *request;
1040 * We have to PANIC if we fail to absorb all the pending requests (eg,
1041 * because our hashtable runs out of memory). This is because the system
1042 * cannot run safely if we are unable to fsync what we have been told to
1043 * fsync. Fortunately, the hashtable is so small that the problem is
1044 * quite unlikely to arise in practice.
1046 START_CRIT_SECTION();
1049 * We try to avoid holding the lock for a long time by copying the request
1052 LWLockAcquire(BgWriterCommLock, LW_EXCLUSIVE);
1054 /* Transfer write count into pending pgstats message */
1055 BgWriterStats.m_buf_written_backend += BgWriterShmem->num_backend_writes;
1056 BgWriterShmem->num_backend_writes = 0;
1058 n = BgWriterShmem->num_requests;
1061 requests = (BgWriterRequest *) palloc(n * sizeof(BgWriterRequest));
1062 memcpy(requests, BgWriterShmem->requests, n * sizeof(BgWriterRequest));
1064 BgWriterShmem->num_requests = 0;
1066 LWLockRelease(BgWriterCommLock);
1068 for (request = requests; n > 0; request++, n--)
1069 RememberFsyncRequest(request->rnode, request->segno);