]> granicus.if.org Git - postgresql/blob - src/backend/postmaster/bgwriter.c
pgindent run for 8.3.
[postgresql] / src / backend / postmaster / bgwriter.c
1 /*-------------------------------------------------------------------------
2  *
3  * bgwriter.c
4  *
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.
12  *
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
19  * condition.)
20  *
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.
27  *
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.)
34  *
35  *
36  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
37  *
38  *
39  * IDENTIFICATION
40  *        $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.47 2007/11/15 21:14:37 momjian Exp $
41  *
42  *-------------------------------------------------------------------------
43  */
44 #include "postgres.h"
45
46 #include <signal.h>
47 #include <sys/time.h>
48 #include <time.h>
49 #include <unistd.h>
50
51 #include "access/xlog_internal.h"
52 #include "libpq/pqsignal.h"
53 #include "miscadmin.h"
54 #include "pgstat.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"
68
69
70 /*----------
71  * Shared memory area for communication between bgwriter and backends
72  *
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
78  *        equal ckpt_started.
79  *      * On failure of a checkpoint, bgwriter increments ckpt_failed
80  *        and sets ckpt_done to equal ckpt_started.
81  *
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
93  *         successful.
94  *      6. If ckpt_failed is different from the originally saved value,
95  *         assume request failed; otherwise it was definitely successful.
96  *
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.
100  *
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.
104  *
105  * The requests array holds fsync requests sent by backends and not yet
106  * absorbed by the bgwriter.
107  *
108  * Unlike the checkpoint fields, num_backend_writes and the requests
109  * fields are protected by BgWriterCommLock.
110  *----------
111  */
112 typedef struct
113 {
114         RelFileNode rnode;
115         BlockNumber segno;                      /* see md.c for special values */
116         /* might add a real request-type field later; not needed yet */
117 } BgWriterRequest;
118
119 typedef struct
120 {
121         pid_t           bgwriter_pid;   /* PID of bgwriter (0 if not started) */
122
123         slock_t         ckpt_lck;               /* protects all the ckpt_* fields */
124
125         int                     ckpt_started;   /* advances when checkpoint starts */
126         int                     ckpt_done;              /* advances when checkpoint done */
127         int                     ckpt_failed;    /* advances when checkpoint fails */
128
129         int                     ckpt_flags;             /* checkpoint flags, as defined in xlog.h */
130
131         uint32          num_backend_writes;             /* counts non-bgwriter buffer writes */
132
133         int                     num_requests;   /* current # of requests */
134         int                     max_requests;   /* allocated array size */
135         BgWriterRequest requests[1];    /* VARIABLE LENGTH ARRAY */
136 } BgWriterShmemStruct;
137
138 static BgWriterShmemStruct *BgWriterShmem;
139
140 /* interval for calling AbsorbFsyncRequests in CheckpointWriteDelay */
141 #define WRITES_PER_ABSORB               1000
142
143 /*
144  * GUC parameters
145  */
146 int                     BgWriterDelay = 200;
147 int                     CheckPointTimeout = 300;
148 int                     CheckPointWarning = 30;
149 double          CheckPointCompletionTarget = 0.5;
150
151 /*
152  * Flags set by interrupt handlers for later service in the main loop.
153  */
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;
157
158 /*
159  * Private state
160  */
161 static bool am_bg_writer = false;
162
163 static bool ckpt_active = false;
164
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;
169
170 static time_t last_checkpoint_time;
171 static time_t last_xlog_switch_time;
172
173 /* Prototypes for private functions */
174
175 static void CheckArchiveTimeout(void);
176 static void BgWriterNap(void);
177 static bool IsCheckpointOnSchedule(double progress);
178 static bool ImmediateCheckpointRequested(void);
179
180 /* Signal handlers */
181
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);
186
187
188 /*
189  * Main entry point for bgwriter process
190  *
191  * This is invoked from BootstrapMain, which has already created the basic
192  * execution environment, but not enabled signals yet.
193  */
194 void
195 BackgroundWriterMain(void)
196 {
197         sigjmp_buf      local_sigjmp_buf;
198         MemoryContext bgwriter_context;
199
200         BgWriterShmem->bgwriter_pid = MyProcPid;
201         am_bg_writer = true;
202
203         /*
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.)
208          */
209 #ifdef HAVE_SETSID
210         if (setsid() < 0)
211                 elog(FATAL, "setsid() failed: %m");
212 #endif
213
214         /*
215          * Properly accept or ignore signals the postmaster might send us
216          *
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).
221          *
222          * SIGUSR1 is presently unused; keep it spare in case someday we want this
223          * process to participate in sinval messaging.
224          */
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 */
233
234         /*
235          * Reset some signals that are accepted by postmaster but not here
236          */
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);
242
243         /* We allow SIGQUIT (quickdie) at all times */
244 #ifdef HAVE_SIGPROCMASK
245         sigdelset(&BlockSig, SIGQUIT);
246 #else
247         BlockSig &= ~(sigmask(SIGQUIT));
248 #endif
249
250         /*
251          * Initialize so that first time-driven event happens at the correct time.
252          */
253         last_checkpoint_time = last_xlog_switch_time = time(NULL);
254
255         /*
256          * Create a resource owner to keep track of our resources (currently only
257          * buffer pins).
258          */
259         CurrentResourceOwner = ResourceOwnerCreate(NULL, "Background Writer");
260
261         /*
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.
266          */
267         bgwriter_context = AllocSetContextCreate(TopMemoryContext,
268                                                                                          "Background Writer",
269                                                                                          ALLOCSET_DEFAULT_MINSIZE,
270                                                                                          ALLOCSET_DEFAULT_INITSIZE,
271                                                                                          ALLOCSET_DEFAULT_MAXSIZE);
272         MemoryContextSwitchTo(bgwriter_context);
273
274         /*
275          * If an exception is encountered, processing resumes here.
276          *
277          * See notes in postgres.c about the design of this coding.
278          */
279         if (sigsetjmp(local_sigjmp_buf, 1) != 0)
280         {
281                 /* Since not using PG_TRY, must reset error stack by hand */
282                 error_context_stack = NULL;
283
284                 /* Prevent interrupts while cleaning up */
285                 HOLD_INTERRUPTS();
286
287                 /* Report the error to the server log */
288                 EmitErrorReport();
289
290                 /*
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.
294                  */
295                 LWLockReleaseAll();
296                 AbortBufferIO();
297                 UnlockBuffers();
298                 /* buffer pins are released here: */
299                 ResourceOwnerRelease(CurrentResourceOwner,
300                                                          RESOURCE_RELEASE_BEFORE_LOCKS,
301                                                          false, true);
302                 /* we needn't bother with the other ResourceOwnerRelease phases */
303                 AtEOXact_Buffers(false);
304                 AtEOXact_Files();
305                 AtEOXact_HashTables(false);
306
307                 /* Warn any waiting backends that the checkpoint failed. */
308                 if (ckpt_active)
309                 {
310                         /* use volatile pointer to prevent code rearrangement */
311                         volatile BgWriterShmemStruct *bgs = BgWriterShmem;
312
313                         SpinLockAcquire(&bgs->ckpt_lck);
314                         bgs->ckpt_failed++;
315                         bgs->ckpt_done = bgs->ckpt_started;
316                         SpinLockRelease(&bgs->ckpt_lck);
317
318                         ckpt_active = false;
319                 }
320
321                 /*
322                  * Now return to normal top-level context and clear ErrorContext for
323                  * next time.
324                  */
325                 MemoryContextSwitchTo(bgwriter_context);
326                 FlushErrorState();
327
328                 /* Flush any leaked data in the top-level context */
329                 MemoryContextResetAndDeleteChildren(bgwriter_context);
330
331                 /* Now we can allow interrupts again */
332                 RESUME_INTERRUPTS();
333
334                 /*
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
337                  * fast as we can.
338                  */
339                 pg_usleep(1000000L);
340
341                 /*
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.
345                  */
346                 smgrcloseall();
347         }
348
349         /* We can now handle ereport(ERROR) */
350         PG_exception_stack = &local_sigjmp_buf;
351
352         /*
353          * Unblock signals (they were blocked when the postmaster forked us)
354          */
355         PG_SETMASK(&UnBlockSig);
356
357         /*
358          * Loop forever
359          */
360         for (;;)
361         {
362                 bool            do_checkpoint = false;
363                 int                     flags = 0;
364                 time_t          now;
365                 int                     elapsed_secs;
366
367                 /*
368                  * Emergency bailout if postmaster has died.  This is to avoid the
369                  * necessity for manual cleanup of all postmaster children.
370                  */
371                 if (!PostmasterIsAlive(true))
372                         exit(1);
373
374                 /*
375                  * Process any requests or signals received recently.
376                  */
377                 AbsorbFsyncRequests();
378
379                 if (got_SIGHUP)
380                 {
381                         got_SIGHUP = false;
382                         ProcessConfigFile(PGC_SIGHUP);
383                 }
384                 if (checkpoint_requested)
385                 {
386                         checkpoint_requested = false;
387                         do_checkpoint = true;
388                         BgWriterStats.m_requested_checkpoints++;
389                 }
390                 if (shutdown_requested)
391                 {
392                         /*
393                          * From here on, elog(ERROR) should end with exit(1), not send
394                          * control back to the sigsetjmp block above
395                          */
396                         ExitOnAnyError = true;
397                         /* Close down the database */
398                         ShutdownXLOG(0, 0);
399                         DumpFreeSpaceMap(0, 0);
400                         /* Normal exit from the bgwriter is here */
401                         proc_exit(0);           /* done */
402                 }
403
404                 /*
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.
409                  */
410                 now = time(NULL);
411                 elapsed_secs = now - last_checkpoint_time;
412                 if (elapsed_secs >= CheckPointTimeout)
413                 {
414                         if (!do_checkpoint)
415                                 BgWriterStats.m_timed_checkpoints++;
416                         do_checkpoint = true;
417                         flags |= CHECKPOINT_CAUSE_TIME;
418                 }
419
420                 /*
421                  * Do a checkpoint if requested, otherwise do one cycle of
422                  * dirty-buffer writing.
423                  */
424                 if (do_checkpoint)
425                 {
426                         /* use volatile pointer to prevent code rearrangement */
427                         volatile BgWriterShmemStruct *bgs = BgWriterShmem;
428
429                         /*
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.
433                          */
434                         SpinLockAcquire(&bgs->ckpt_lck);
435                         flags |= bgs->ckpt_flags;
436                         bgs->ckpt_flags = 0;
437                         bgs->ckpt_started++;
438                         SpinLockRelease(&bgs->ckpt_lck);
439
440                         /*
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.
446                          */
447                         if ((flags & CHECKPOINT_CAUSE_XLOG) &&
448                                 elapsed_secs < CheckPointWarning)
449                                 ereport(LOG,
450                                                 (errmsg("checkpoints are occurring too frequently (%d seconds apart)",
451                                                                 elapsed_secs),
452                                                  errhint("Consider increasing the configuration parameter \"checkpoint_segments\".")));
453
454                         /*
455                          * Initialize bgwriter-private variables used during checkpoint.
456                          */
457                         ckpt_active = true;
458                         ckpt_start_recptr = GetInsertRecPtr();
459                         ckpt_start_time = now;
460                         ckpt_cached_elapsed = 0;
461
462                         /*
463                          * Do the checkpoint.
464                          */
465                         CreateCheckPoint(flags);
466
467                         /*
468                          * After any checkpoint, close all smgr files.  This is so we
469                          * won't hang onto smgr references to deleted files indefinitely.
470                          */
471                         smgrcloseall();
472
473                         /*
474                          * Indicate checkpoint completion to any waiting backends.
475                          */
476                         SpinLockAcquire(&bgs->ckpt_lck);
477                         bgs->ckpt_done = bgs->ckpt_started;
478                         SpinLockRelease(&bgs->ckpt_lck);
479
480                         ckpt_active = false;
481
482                         /*
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.
486                          */
487                         last_checkpoint_time = now;
488                 }
489                 else
490                         BgBufferSync();
491
492                 /* Check for archive_timeout and switch xlog files if necessary. */
493                 CheckArchiveTimeout();
494
495                 /* Nap for the configured time. */
496                 BgWriterNap();
497         }
498 }
499
500 /*
501  * CheckArchiveTimeout -- check for archive_timeout and switch xlog files
502  *              if needed
503  */
504 static void
505 CheckArchiveTimeout(void)
506 {
507         time_t          now;
508         time_t          last_time;
509
510         if (XLogArchiveTimeout <= 0)
511                 return;
512
513         now = time(NULL);
514
515         /* First we do a quick check using possibly-stale local state. */
516         if ((int) (now - last_xlog_switch_time) < XLogArchiveTimeout)
517                 return;
518
519         /*
520          * Update local state ... note that last_xlog_switch_time is the last time
521          * a switch was performed *or requested*.
522          */
523         last_time = GetLastSegSwitchTime();
524
525         last_xlog_switch_time = Max(last_xlog_switch_time, last_time);
526
527         /* Now we can do the real check */
528         if ((int) (now - last_xlog_switch_time) >= XLogArchiveTimeout)
529         {
530                 XLogRecPtr      switchpoint;
531
532                 /* OK, it's time to switch */
533                 switchpoint = RequestXLogSwitch();
534
535                 /*
536                  * If the returned pointer points exactly to a segment boundary,
537                  * assume nothing happened.
538                  */
539                 if ((switchpoint.xrecoff % XLogSegSize) != 0)
540                         ereport(DEBUG1,
541                                 (errmsg("transaction log switch forced (archive_timeout=%d)",
542                                                 XLogArchiveTimeout)));
543
544                 /*
545                  * Update state in any case, so we don't retry constantly when the
546                  * system is idle.
547                  */
548                 last_xlog_switch_time = now;
549         }
550 }
551
552 /*
553  * BgWriterNap -- Nap for the configured time or until a signal is received.
554  */
555 static void
556 BgWriterNap(void)
557 {
558         long            udelay;
559
560         /*
561          * Send off activity statistics to the stats collector
562          */
563         pgstat_send_bgwriter();
564
565         /*
566          * Nap for the configured time, or sleep for 10 seconds if there is no
567          * bgwriter activity configured.
568          *
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
572          * nap.
573          *
574          * We absorb pending requests after each short sleep.
575          */
576         if (bgwriter_lru_maxpages > 0 || ckpt_active)
577                 udelay = BgWriterDelay * 1000L;
578         else if (XLogArchiveTimeout > 0)
579                 udelay = 1000000L;              /* One second */
580         else
581                 udelay = 10000000L;             /* Ten seconds */
582
583         while (udelay > 999999L)
584         {
585                 if (got_SIGHUP || shutdown_requested ||
586                 (ckpt_active ? ImmediateCheckpointRequested() : checkpoint_requested))
587                         break;
588                 pg_usleep(1000000L);
589                 AbsorbFsyncRequests();
590                 udelay -= 1000000L;
591         }
592
593         if (!(got_SIGHUP || shutdown_requested ||
594           (ckpt_active ? ImmediateCheckpointRequested() : checkpoint_requested)))
595                 pg_usleep(udelay);
596 }
597
598 /*
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.)
602  */
603 static bool
604 ImmediateCheckpointRequested(void)
605 {
606         if (checkpoint_requested)
607         {
608                 volatile BgWriterShmemStruct *bgs = BgWriterShmem;
609
610                 /*
611                  * We don't need to acquire the ckpt_lck in this case because we're
612                  * only looking at a single flag bit.
613                  */
614                 if (bgs->ckpt_flags & CHECKPOINT_IMMEDIATE)
615                         return true;
616         }
617         return false;
618 }
619
620 /*
621  * CheckpointWriteDelay -- yield control to bgwriter during a checkpoint
622  *
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.
627  *
628  * The checkpoint request flags should be passed in; currently the only one
629  * examined is CHECKPOINT_IMMEDIATE, which disables delays between writes.
630  *
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.
633  */
634 void
635 CheckpointWriteDelay(int flags, double progress)
636 {
637         static int      absorb_counter = WRITES_PER_ABSORB;
638
639         /* Do nothing if checkpoint is being executed by non-bgwriter process */
640         if (!am_bg_writer)
641                 return;
642
643         /*
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.
646          */
647         if (!(flags & CHECKPOINT_IMMEDIATE) &&
648                 !shutdown_requested &&
649                 !ImmediateCheckpointRequested() &&
650                 IsCheckpointOnSchedule(progress))
651         {
652                 if (got_SIGHUP)
653                 {
654                         got_SIGHUP = false;
655                         ProcessConfigFile(PGC_SIGHUP);
656                 }
657
658                 AbsorbFsyncRequests();
659                 absorb_counter = WRITES_PER_ABSORB;
660
661                 BgBufferSync();
662                 CheckArchiveTimeout();
663                 BgWriterNap();
664         }
665         else if (--absorb_counter <= 0)
666         {
667                 /*
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.
671                  */
672                 AbsorbFsyncRequests();
673                 absorb_counter = WRITES_PER_ABSORB;
674         }
675 }
676
677 /*
678  * IsCheckpointOnSchedule -- are we on schedule to finish this checkpoint
679  *               in time?
680  *
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.
684  */
685 static bool
686 IsCheckpointOnSchedule(double progress)
687 {
688         XLogRecPtr      recptr;
689         struct timeval now;
690         double          elapsed_xlogs,
691                                 elapsed_time;
692
693         Assert(ckpt_active);
694
695         /* Scale progress according to checkpoint_completion_target. */
696         progress *= CheckPointCompletionTarget;
697
698         /*
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.
703          */
704         if (progress < ckpt_cached_elapsed)
705                 return false;
706
707         /*
708          * Check progress against WAL segments written and checkpoint_segments.
709          *
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
715          * estimate anyway.
716          */
717         recptr = GetInsertRecPtr();
718         elapsed_xlogs =
719                 (((double) (int32) (recptr.xlogid - ckpt_start_recptr.xlogid)) * XLogSegsPerFile +
720                  ((double) recptr.xrecoff - (double) ckpt_start_recptr.xrecoff) / XLogSegSize) /
721                 CheckPointSegments;
722
723         if (progress < elapsed_xlogs)
724         {
725                 ckpt_cached_elapsed = elapsed_xlogs;
726                 return false;
727         }
728
729         /*
730          * Check progress against time elapsed and checkpoint_timeout.
731          */
732         gettimeofday(&now, NULL);
733         elapsed_time = ((double) (now.tv_sec - ckpt_start_time) +
734                                         now.tv_usec / 1000000.0) / CheckPointTimeout;
735
736         if (progress < elapsed_time)
737         {
738                 ckpt_cached_elapsed = elapsed_time;
739                 return false;
740         }
741
742         /* It looks like we're on schedule. */
743         return true;
744 }
745
746
747 /* --------------------------------
748  *              signal handler routines
749  * --------------------------------
750  */
751
752 /*
753  * bg_quickdie() occurs when signalled SIGQUIT by the postmaster.
754  *
755  * Some backend has bought the farm,
756  * so we need to stop what we're doing and exit.
757  */
758 static void
759 bg_quickdie(SIGNAL_ARGS)
760 {
761         PG_SETMASK(&BlockSig);
762
763         /*
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.
767          *
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.
772          */
773         exit(2);
774 }
775
776 /* SIGHUP: set flag to re-read config file at next convenient time */
777 static void
778 BgSigHupHandler(SIGNAL_ARGS)
779 {
780         got_SIGHUP = true;
781 }
782
783 /* SIGINT: set flag to run a normal checkpoint right away */
784 static void
785 ReqCheckpointHandler(SIGNAL_ARGS)
786 {
787         checkpoint_requested = true;
788 }
789
790 /* SIGUSR2: set flag to run a shutdown checkpoint and exit */
791 static void
792 ReqShutdownHandler(SIGNAL_ARGS)
793 {
794         shutdown_requested = true;
795 }
796
797
798 /* --------------------------------
799  *              communication with backends
800  * --------------------------------
801  */
802
803 /*
804  * BgWriterShmemSize
805  *              Compute space needed for bgwriter-related shared memory
806  */
807 Size
808 BgWriterShmemSize(void)
809 {
810         Size            size;
811
812         /*
813          * Currently, the size of the requests[] array is arbitrarily set equal to
814          * NBuffers.  This may prove too large or small ...
815          */
816         size = offsetof(BgWriterShmemStruct, requests);
817         size = add_size(size, mul_size(NBuffers, sizeof(BgWriterRequest)));
818
819         return size;
820 }
821
822 /*
823  * BgWriterShmemInit
824  *              Allocate and initialize bgwriter-related shared memory
825  */
826 void
827 BgWriterShmemInit(void)
828 {
829         bool            found;
830
831         BgWriterShmem = (BgWriterShmemStruct *)
832                 ShmemInitStruct("Background Writer Data",
833                                                 BgWriterShmemSize(),
834                                                 &found);
835         if (BgWriterShmem == NULL)
836                 ereport(FATAL,
837                                 (errcode(ERRCODE_OUT_OF_MEMORY),
838                                  errmsg("not enough shared memory for background writer")));
839         if (found)
840                 return;                                 /* already initialized */
841
842         MemSet(BgWriterShmem, 0, sizeof(BgWriterShmemStruct));
843         SpinLockInit(&BgWriterShmem->ckpt_lck);
844         BgWriterShmem->max_requests = NBuffers;
845 }
846
847 /*
848  * RequestCheckpoint
849  *              Called in backend processes to request a checkpoint
850  *
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.)
861  */
862 void
863 RequestCheckpoint(int flags)
864 {
865         /* use volatile pointer to prevent code rearrangement */
866         volatile BgWriterShmemStruct *bgs = BgWriterShmem;
867         int                     old_failed,
868                                 old_started;
869
870         /*
871          * If in a standalone backend, just do it ourselves.
872          */
873         if (!IsPostmasterEnvironment)
874         {
875                 /*
876                  * There's no point in doing slow checkpoints in a standalone backend,
877                  * because there's no other backends the checkpoint could disrupt.
878                  */
879                 CreateCheckPoint(flags | CHECKPOINT_IMMEDIATE);
880
881                 /*
882                  * After any checkpoint, close all smgr files.  This is so we won't
883                  * hang onto smgr references to deleted files indefinitely.
884                  */
885                 smgrcloseall();
886
887                 return;
888         }
889
890         /*
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.
894          *
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!
898          */
899         SpinLockAcquire(&bgs->ckpt_lck);
900
901         old_failed = bgs->ckpt_failed;
902         old_started = bgs->ckpt_started;
903         bgs->ckpt_flags |= flags;
904
905         SpinLockRelease(&bgs->ckpt_lck);
906
907         /*
908          * Send signal to request checkpoint.  When not waiting, we consider
909          * failure to send the signal to be nonfatal.
910          */
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");
917
918         /*
919          * If requested, wait for completion.  We detect completion according to
920          * the algorithm given above.
921          */
922         if (flags & CHECKPOINT_WAIT)
923         {
924                 int                     new_started,
925                                         new_failed;
926
927                 /* Wait for a new checkpoint to start. */
928                 for (;;)
929                 {
930                         SpinLockAcquire(&bgs->ckpt_lck);
931                         new_started = bgs->ckpt_started;
932                         SpinLockRelease(&bgs->ckpt_lck);
933
934                         if (new_started != old_started)
935                                 break;
936
937                         CHECK_FOR_INTERRUPTS();
938                         pg_usleep(100000L);
939                 }
940
941                 /*
942                  * We are waiting for ckpt_done >= new_started, in a modulo sense.
943                  */
944                 for (;;)
945                 {
946                         int                     new_done;
947
948                         SpinLockAcquire(&bgs->ckpt_lck);
949                         new_done = bgs->ckpt_done;
950                         new_failed = bgs->ckpt_failed;
951                         SpinLockRelease(&bgs->ckpt_lck);
952
953                         if (new_done - new_started >= 0)
954                                 break;
955
956                         CHECK_FOR_INTERRUPTS();
957                         pg_usleep(100000L);
958                 }
959
960                 if (new_failed != old_failed)
961                         ereport(ERROR,
962                                         (errmsg("checkpoint request failed"),
963                                          errhint("Consult recent messages in the server log for details.")));
964         }
965 }
966
967 /*
968  * ForwardFsyncRequest
969  *              Forward a file-fsync request from a backend to the bgwriter
970  *
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.
976  *
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
980  * see for details.)
981  *
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.
985  *
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.
990  */
991 bool
992 ForwardFsyncRequest(RelFileNode rnode, BlockNumber segno)
993 {
994         BgWriterRequest *request;
995
996         if (!IsUnderPostmaster)
997                 return false;                   /* probably shouldn't even get here */
998
999         if (am_bg_writer)
1000                 elog(ERROR, "ForwardFsyncRequest must not be called in bgwriter");
1001
1002         LWLockAcquire(BgWriterCommLock, LW_EXCLUSIVE);
1003
1004         /* we count non-bgwriter writes even when the request queue overflows */
1005         BgWriterShmem->num_backend_writes++;
1006
1007         if (BgWriterShmem->bgwriter_pid == 0 ||
1008                 BgWriterShmem->num_requests >= BgWriterShmem->max_requests)
1009         {
1010                 LWLockRelease(BgWriterCommLock);
1011                 return false;
1012         }
1013         request = &BgWriterShmem->requests[BgWriterShmem->num_requests++];
1014         request->rnode = rnode;
1015         request->segno = segno;
1016         LWLockRelease(BgWriterCommLock);
1017         return true;
1018 }
1019
1020 /*
1021  * AbsorbFsyncRequests
1022  *              Retrieve queued fsync requests and pass them to local smgr.
1023  *
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.
1028  */
1029 void
1030 AbsorbFsyncRequests(void)
1031 {
1032         BgWriterRequest *requests = NULL;
1033         BgWriterRequest *request;
1034         int                     n;
1035
1036         if (!am_bg_writer)
1037                 return;
1038
1039         /*
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.
1045          */
1046         START_CRIT_SECTION();
1047
1048         /*
1049          * We try to avoid holding the lock for a long time by copying the request
1050          * array.
1051          */
1052         LWLockAcquire(BgWriterCommLock, LW_EXCLUSIVE);
1053
1054         /* Transfer write count into pending pgstats message */
1055         BgWriterStats.m_buf_written_backend += BgWriterShmem->num_backend_writes;
1056         BgWriterShmem->num_backend_writes = 0;
1057
1058         n = BgWriterShmem->num_requests;
1059         if (n > 0)
1060         {
1061                 requests = (BgWriterRequest *) palloc(n * sizeof(BgWriterRequest));
1062                 memcpy(requests, BgWriterShmem->requests, n * sizeof(BgWriterRequest));
1063         }
1064         BgWriterShmem->num_requests = 0;
1065
1066         LWLockRelease(BgWriterCommLock);
1067
1068         for (request = requests; n > 0; request++, n--)
1069                 RememberFsyncRequest(request->rnode, request->segno);
1070
1071         if (requests)
1072                 pfree(requests);
1073
1074         END_CRIT_SECTION();
1075 }