]> granicus.if.org Git - postgresql/blob - src/backend/postmaster/bgwriter.c
Adjust our timezone library to use pg_time_t (typedef'd as int64) in
[postgresql] / src / backend / postmaster / bgwriter.c
1 /*-------------------------------------------------------------------------
2  *
3  * bgwriter.c
4  *
5  * The background writer (bgwriter) is new in Postgres 7.5.  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-2003, PostgreSQL Global Development Group
37  *
38  *
39  * IDENTIFICATION
40  *        $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.3 2004/06/03 02:08:03 tgl Exp $
41  *
42  *-------------------------------------------------------------------------
43  */
44 #include "postgres.h"
45
46 #include <signal.h>
47 #include <time.h>
48
49 #include "access/xlog.h"
50 #include "libpq/pqsignal.h"
51 #include "miscadmin.h"
52 #include "postmaster/bgwriter.h"
53 #include "storage/bufmgr.h"
54 #include "storage/freespace.h"
55 #include "storage/ipc.h"
56 #include "storage/pmsignal.h"
57 #include "storage/smgr.h"
58 #include "tcop/tcopprot.h"
59 #include "utils/guc.h"
60
61
62 /*----------
63  * Shared memory area for communication between bgwriter and backends
64  *
65  * The ckpt counters allow backends to watch for completion of a checkpoint
66  * request they send.  Here's how it works:
67  *      * At start of a checkpoint, bgwriter increments ckpt_started.
68  *      * On completion of a checkpoint, bgwriter sets ckpt_done to
69  *        equal ckpt_started.
70  *      * On failure of a checkpoint, bgwrite first increments ckpt_failed,
71  *        then sets ckpt_done to equal ckpt_started.
72  * All three fields are declared sig_atomic_t to ensure they can be read
73  * and written without explicit locking.  The algorithm for backends is:
74  *      1. Record current values of ckpt_failed and ckpt_started (in that
75  *         order!).
76  *      2. Send signal to request checkpoint.
77  *      3. Sleep until ckpt_started changes.  Now you know a checkpoint has
78  *         begun since you started this algorithm (although *not* that it was
79  *         specifically initiated by your signal).
80  *      4. Record new value of ckpt_started.
81  *      5. Sleep until ckpt_done >= saved value of ckpt_started.  (Use modulo
82  *         arithmetic here in case counters wrap around.)  Now you know a
83  *         checkpoint has started and completed, but not whether it was
84  *         successful.
85  *      6. If ckpt_failed is different from the originally saved value,
86  *         assume request failed; otherwise it was definitely successful.
87  *
88  * The requests array holds fsync requests sent by backends and not yet
89  * absorbed by the bgwriter.
90  *----------
91  */
92 typedef struct
93 {
94         RelFileNode             rnode;
95         BlockNumber             segno;
96         /* might add a request-type field later */
97 } BgWriterRequest;
98
99 typedef struct
100 {
101         pid_t   bgwriter_pid;           /* PID of bgwriter (0 if not started) */
102
103         sig_atomic_t    ckpt_started;   /* advances when checkpoint starts */
104         sig_atomic_t    ckpt_done;              /* advances when checkpoint done */
105         sig_atomic_t    ckpt_failed;    /* advances when checkpoint fails */
106
107         int                             num_requests;   /* current # of requests */
108         int                             max_requests;   /* allocated array size */
109         BgWriterRequest requests[1];    /* VARIABLE LENGTH ARRAY */
110 } BgWriterShmemStruct;
111
112 static BgWriterShmemStruct *BgWriterShmem;
113
114 /*
115  * GUC parameters
116  */
117 int                     BgWriterDelay = 200;
118 int                     BgWriterPercent = 1;
119 int                     BgWriterMaxPages = 100;
120
121 int                     CheckPointTimeout = 300;
122 int                     CheckPointWarning = 30;
123
124 /*
125  * Flags set by interrupt handlers for later service in the main loop.
126  */
127 static volatile sig_atomic_t got_SIGHUP = false;
128 static volatile sig_atomic_t checkpoint_requested = false;
129 static volatile sig_atomic_t shutdown_requested = false;
130
131 /*
132  * Private state
133  */
134 static bool             am_bg_writer = false;
135
136 static bool             ckpt_active = false;
137
138 static time_t   last_checkpoint_time;
139
140
141 static void bg_quickdie(SIGNAL_ARGS);
142 static void BgSigHupHandler(SIGNAL_ARGS);
143 static void ReqCheckpointHandler(SIGNAL_ARGS);
144 static void ReqShutdownHandler(SIGNAL_ARGS);
145
146
147 /*
148  * Main entry point for bgwriter process
149  *
150  * This is invoked from BootstrapMain, which has already created the basic
151  * execution environment, but not enabled signals yet.
152  */
153 void
154 BackgroundWriterMain(void)
155 {
156         Assert(BgWriterShmem != NULL);
157         BgWriterShmem->bgwriter_pid = MyProcPid;
158         am_bg_writer = true;
159
160         /*
161          * Properly accept or ignore signals the postmaster might send us
162          *
163          * Note: we deliberately ignore SIGTERM, because during a standard Unix
164          * system shutdown cycle, init will SIGTERM all processes at once.  We
165          * want to wait for the backends to exit, whereupon the postmaster will
166          * tell us it's okay to shut down (via SIGUSR2).
167          *
168          * SIGUSR1 is presently unused; keep it spare in case someday we want
169          * this process to participate in sinval messaging.
170          */
171         pqsignal(SIGHUP, BgSigHupHandler);      /* set flag to read config file */
172         pqsignal(SIGINT, ReqCheckpointHandler);         /* request checkpoint */
173         pqsignal(SIGTERM, SIG_IGN);                     /* ignore SIGTERM */
174         pqsignal(SIGQUIT, bg_quickdie);         /* hard crash time */
175         pqsignal(SIGALRM, SIG_IGN);
176         pqsignal(SIGPIPE, SIG_IGN);
177         pqsignal(SIGUSR1, SIG_IGN);                     /* reserve for sinval */
178         pqsignal(SIGUSR2, ReqShutdownHandler);          /* request shutdown */
179
180         /*
181          * Reset some signals that are accepted by postmaster but not here
182          */
183         pqsignal(SIGCHLD, SIG_DFL);
184         pqsignal(SIGTTIN, SIG_DFL);
185         pqsignal(SIGTTOU, SIG_DFL);
186         pqsignal(SIGCONT, SIG_DFL);
187         pqsignal(SIGWINCH, SIG_DFL);
188
189         /* We allow SIGQUIT (quickdie) at all times */
190 #ifdef HAVE_SIGPROCMASK
191         sigdelset(&BlockSig, SIGQUIT);
192 #else
193         BlockSig &= ~(sigmask(SIGQUIT));
194 #endif
195
196         /*
197          * Initialize so that first time-driven checkpoint happens
198          * at the correct time.
199          */
200         last_checkpoint_time = time(NULL);
201
202         /*
203          * If an exception is encountered, processing resumes here.
204          */
205         if (sigsetjmp(Warn_restart, 1) != 0)
206         {
207                 /*
208                  * Make sure we're not interrupted while cleaning up.  Also forget
209                  * any pending QueryCancel request, since we're aborting anyway.
210                  * Force InterruptHoldoffCount to a known state in case we
211                  * ereport'd from inside a holdoff section.
212                  */
213                 ImmediateInterruptOK = false;
214                 QueryCancelPending = false;
215                 InterruptHoldoffCount = 1;
216                 CritSectionCount = 0;   /* should be unnecessary, but... */
217
218                 /*
219                  * These operations are really just a minimal subset of
220                  * AbortTransaction().  We don't have very many resources
221                  * to worry about in bgwriter, but we do have LWLocks and buffers.
222                  */
223                 LWLockReleaseAll();
224                 AbortBufferIO();
225                 UnlockBuffers();
226
227                 /*
228                  * Clear flag to indicate that we got out of error recovery mode
229                  * successfully.  (Flag was set in elog.c before longjmp().)
230                  */
231                 InError = false;
232
233                 /* Warn any waiting backends that the checkpoint failed. */
234                 if (ckpt_active)
235                 {
236                         /* use volatile pointer to prevent code rearrangement */
237                         volatile BgWriterShmemStruct *bgs = BgWriterShmem;
238
239                         bgs->ckpt_failed++;
240                         bgs->ckpt_done = bgs->ckpt_started;
241                         ckpt_active = false;
242                 }
243
244                 /*
245                  * Exit interrupt holdoff section we implicitly established above.
246                  */
247                 RESUME_INTERRUPTS();
248
249                 /*
250                  * Sleep at least 1 second after any error.  A write error is
251                  * likely to be repeated, and we don't want to be filling the
252                  * error logs as fast as we can.  (XXX think about ways to make
253                  * progress when the LRU dirty buffer cannot be written...)
254                  */
255                 pg_usleep(1000000L);
256         }
257
258         Warn_restart_ready = true;      /* we can now handle ereport(ERROR) */
259
260         /*
261          * Unblock signals (they were blocked when the postmaster forked us)
262          */
263         PG_SETMASK(&UnBlockSig);
264
265         /*
266          * Loop forever 
267          */
268         for (;;)
269         {
270                 bool            do_checkpoint = false;
271                 bool            force_checkpoint = false;
272                 time_t          now;
273                 int                     elapsed_secs;
274                 int                     n;
275                 long            udelay;
276
277                 /*
278                  * Emergency bailout if postmaster has died.  This is to avoid the
279                  * necessity for manual cleanup of all postmaster children.
280                  */
281                 if (!PostmasterIsAlive(true))
282                         exit(1);
283
284                 /*
285                  * Process any requests or signals received recently.
286                  */
287                 AbsorbFsyncRequests();
288
289                 if (got_SIGHUP)
290                 {
291                         got_SIGHUP = false;
292                         ProcessConfigFile(PGC_SIGHUP);
293                 }
294                 if (checkpoint_requested)
295                 {
296                         checkpoint_requested = false;
297                         do_checkpoint = true;
298                         force_checkpoint = true;
299                 }
300                 if (shutdown_requested)
301                 {
302                         ShutdownXLOG(0, 0);
303                         DumpFreeSpaceMap(0, 0);
304                         /* Normal exit from the bgwriter is here */
305                         proc_exit(0);           /* done */
306                 }
307
308                 /*
309                  * Do an unforced checkpoint if too much time has elapsed
310                  * since the last one.
311                  */
312                 now = time(NULL);
313                 elapsed_secs = now - last_checkpoint_time;
314                 if (elapsed_secs >= CheckPointTimeout)
315                         do_checkpoint = true;
316
317                 /*
318                  * Do a checkpoint if requested, otherwise do one cycle of
319                  * dirty-buffer writing.
320                  */
321                 if (do_checkpoint)
322                 {
323                         if (CheckPointWarning != 0)
324                         {
325                                 /*
326                                  * Ideally we should only warn if this checkpoint was
327                                  * requested due to running out of segment files, and not
328                                  * if it was manually requested.  However we can't tell the
329                                  * difference with the current signalling mechanism.
330                                  */
331                                 if (elapsed_secs < CheckPointWarning)
332                                         ereport(LOG,
333                                                         (errmsg("checkpoints are occurring too frequently (%d seconds apart)",
334                                                                         elapsed_secs),
335                                                          errhint("Consider increasing the configuration parameter \"checkpoint_segments\".")));
336                         }
337
338                         /*
339                          * Indicate checkpoint start to any waiting backends.
340                          */
341                         ckpt_active = true;
342                         BgWriterShmem->ckpt_started++;
343
344                         CreateCheckPoint(false, force_checkpoint);
345
346                         /*
347                          * Indicate checkpoint completion to any waiting backends.
348                          */
349                         BgWriterShmem->ckpt_done = BgWriterShmem->ckpt_started;
350                         ckpt_active = false;
351
352                         /*
353                          * Note we record the checkpoint start time not end time as
354                          * last_checkpoint_time.  This is so that time-driven checkpoints
355                          * happen at a predictable spacing.
356                          */
357                         last_checkpoint_time = now;
358
359                         /*
360                          * After any checkpoint, close all smgr files.  This is so we
361                          * won't hang onto smgr references to deleted files indefinitely.
362                          * (It is safe to do this because this process does not have a
363                          * relcache, and so no dangling references could remain.)
364                          */
365                         smgrcloseall();
366
367                         /* Nap for configured time before rechecking */
368                         n = 1;
369                 }
370                 else
371                 {
372                         n = BufferSync(BgWriterPercent, BgWriterMaxPages);
373                 }
374
375                 /*
376                  * Nap for the configured time or sleep for 10 seconds if
377                  * there was nothing to do at all.
378                  *
379                  * On some platforms, signals won't interrupt the sleep.  To ensure
380                  * we respond reasonably promptly when someone signals us,
381                  * break down the sleep into 1-second increments, and check for
382                  * interrupts after each nap.
383                  *
384                  * We absorb pending requests after each short sleep.
385                  */
386                 udelay = ((n > 0) ? BgWriterDelay : 10000) * 1000L;
387                 while (udelay > 1000000L)
388                 {
389                         if (got_SIGHUP || checkpoint_requested || shutdown_requested)
390                                 break;
391                         pg_usleep(1000000L);
392                         AbsorbFsyncRequests();
393                         udelay -= 1000000L;
394                 }
395                 if (!(got_SIGHUP || checkpoint_requested || shutdown_requested))
396                         pg_usleep(udelay);
397         }
398 }
399
400
401 /* --------------------------------
402  *              signal handler routines
403  * --------------------------------
404  */
405
406 /*
407  * bg_quickdie() occurs when signalled SIGQUIT by the postmaster.
408  *
409  * Some backend has bought the farm,
410  * so we need to stop what we're doing and exit.
411  */
412 static void
413 bg_quickdie(SIGNAL_ARGS)
414 {
415         PG_SETMASK(&BlockSig);
416
417         /*
418          * DO NOT proc_exit() -- we're here because shared memory may be
419          * corrupted, so we don't want to try to clean up our transaction.
420          * Just nail the windows shut and get out of town.
421          *
422          * Note we do exit(1) not exit(0).      This is to force the postmaster into
423          * a system reset cycle if some idiot DBA sends a manual SIGQUIT to a
424          * random backend.      This is necessary precisely because we don't clean
425          * up our shared memory state.
426          */
427         exit(1);
428 }
429
430 /* SIGHUP: set flag to re-read config file at next convenient time */
431 static void
432 BgSigHupHandler(SIGNAL_ARGS)
433 {
434         got_SIGHUP = true;
435 }
436
437 /* SIGINT: set flag to run a normal checkpoint right away */
438 static void
439 ReqCheckpointHandler(SIGNAL_ARGS)
440 {
441         checkpoint_requested = true;
442 }
443
444 /* SIGUSR2: set flag to run a shutdown checkpoint and exit */
445 static void
446 ReqShutdownHandler(SIGNAL_ARGS)
447 {
448         shutdown_requested = true;
449 }
450
451
452 /* --------------------------------
453  *              communication with backends
454  * --------------------------------
455  */
456
457 /*
458  * BgWriterShmemSize
459  *              Compute space needed for bgwriter-related shared memory
460  */
461 int
462 BgWriterShmemSize(void)
463 {
464         /*
465          * Currently, the size of the requests[] array is arbitrarily set
466          * equal to NBuffers.  This may prove too large or small ...
467          */
468         return MAXALIGN(sizeof(BgWriterShmemStruct) +
469                                         (NBuffers - 1) * sizeof(BgWriterRequest));
470 }
471
472 /*
473  * BgWriterShmemInit
474  *              Allocate and initialize bgwriter-related shared memory
475  */
476 void
477 BgWriterShmemInit(void)
478 {
479         bool found;
480
481         BgWriterShmem = (BgWriterShmemStruct *)
482                 ShmemInitStruct("Background Writer Data",
483                                                 BgWriterShmemSize(),
484                                                 &found);
485         if (BgWriterShmem == NULL)
486                 ereport(FATAL,
487                                 (errcode(ERRCODE_OUT_OF_MEMORY),
488                                  errmsg("insufficient shared memory for bgwriter")));
489         if (found)
490                 return;                                 /* already initialized */
491
492         MemSet(BgWriterShmem, 0, sizeof(BgWriterShmemStruct));
493         BgWriterShmem->max_requests = NBuffers;
494 }
495
496 /*
497  * RequestCheckpoint
498  *              Called in backend processes to request an immediate checkpoint
499  *
500  * If waitforit is true, wait until the checkpoint is completed
501  * before returning; otherwise, just signal the request and return
502  * immediately.
503  */
504 void
505 RequestCheckpoint(bool waitforit)
506 {
507         /* use volatile pointer to prevent code rearrangement */
508         volatile BgWriterShmemStruct *bgs = BgWriterShmem;
509         sig_atomic_t    old_failed = bgs->ckpt_failed;
510         sig_atomic_t    old_started = bgs->ckpt_started;
511
512         /*
513          * Send signal to request checkpoint.  When waitforit is false,
514          * we consider failure to send the signal to be nonfatal.
515          */
516         if (BgWriterShmem->bgwriter_pid == 0)
517                 elog(waitforit ? ERROR : LOG,
518                          "could not request checkpoint because bgwriter not running");
519         if (kill(BgWriterShmem->bgwriter_pid, SIGINT) != 0)
520                 elog(waitforit ? ERROR : LOG,
521                          "could not signal for checkpoint: %m");
522
523         /*
524          * If requested, wait for completion.  We detect completion according
525          * to the algorithm given above.
526          */
527         if (waitforit)
528         {
529                 while (bgs->ckpt_started == old_started)
530                 {
531                         CHECK_FOR_INTERRUPTS();
532                         pg_usleep(100000L);
533                 }
534                 old_started = bgs->ckpt_started;
535                 /*
536                  * We are waiting for ckpt_done >= old_started, in a modulo
537                  * sense.  This is a little tricky since we don't know the
538                  * width or signedness of sig_atomic_t.  We make the lowest
539                  * common denominator assumption that it is only as wide
540                  * as "char".  This means that this algorithm will cope
541                  * correctly as long as we don't sleep for more than 127
542                  * completed checkpoints.  (If we do, we will get another
543                  * chance to exit after 128 more checkpoints...)
544                  */
545                 while (((signed char) (bgs->ckpt_done - old_started)) < 0)
546                 {
547                         CHECK_FOR_INTERRUPTS();
548                         pg_usleep(100000L);
549                 }
550                 if (bgs->ckpt_failed != old_failed)
551                         ereport(ERROR,
552                                         (errmsg("checkpoint request failed"),
553                                          errhint("Consult the postmaster log for details.")));
554         }
555 }
556
557 /*
558  * ForwardFsyncRequest
559  *              Forward a file-fsync request from a backend to the bgwriter
560  *
561  * Whenever a backend is compelled to write directly to a relation
562  * (which should be seldom, if the bgwriter is getting its job done),
563  * the backend calls this routine to pass over knowledge that the relation
564  * is dirty and must be fsync'd before next checkpoint.
565  *
566  * If we are unable to pass over the request (at present, this can happen
567  * if the shared memory queue is full), we return false.  That forces
568  * the backend to do its own fsync.  We hope that will be even more seldom.
569  *
570  * Note: we presently make no attempt to eliminate duplicate requests
571  * in the requests[] queue.  The bgwriter will have to eliminate dups
572  * internally anyway, so we may as well avoid holding the lock longer
573  * than we have to here.
574  */
575 bool
576 ForwardFsyncRequest(RelFileNode rnode, BlockNumber segno)
577 {
578         BgWriterRequest *request;
579
580         if (!IsUnderPostmaster)
581                 return false;                   /* probably shouldn't even get here */
582         Assert(BgWriterShmem != NULL);
583
584         LWLockAcquire(BgWriterCommLock, LW_EXCLUSIVE);
585         if (BgWriterShmem->bgwriter_pid == 0 ||
586                 BgWriterShmem->num_requests >= BgWriterShmem->max_requests)
587         {
588                 LWLockRelease(BgWriterCommLock);
589                 return false;
590         }
591         request = &BgWriterShmem->requests[BgWriterShmem->num_requests++];
592         request->rnode = rnode;
593         request->segno = segno;
594         LWLockRelease(BgWriterCommLock);
595         return true;
596 }
597
598 /*
599  * AbsorbFsyncRequests
600  *              Retrieve queued fsync requests and pass them to local smgr.
601  *
602  * This is exported because it must be called during CreateCheckpoint;
603  * we have to be sure we have accepted all pending requests *after* we
604  * establish the checkpoint redo pointer.  Since CreateCheckpoint
605  * sometimes runs in non-bgwriter processes, do nothing if not bgwriter.
606  */
607 void
608 AbsorbFsyncRequests(void)
609 {
610         BgWriterRequest *requests = NULL;
611         BgWriterRequest *request;
612         int                     n;
613
614         if (!am_bg_writer)
615                 return;
616
617         /*
618          * We try to avoid holding the lock for a long time by copying the
619          * request array.
620          */
621         LWLockAcquire(BgWriterCommLock, LW_EXCLUSIVE);
622
623         n = BgWriterShmem->num_requests;
624         if (n > 0)
625         {
626                 requests = (BgWriterRequest *) palloc(n * sizeof(BgWriterRequest));
627                 memcpy(requests, BgWriterShmem->requests, n * sizeof(BgWriterRequest));
628         }
629         BgWriterShmem->num_requests = 0;
630
631         LWLockRelease(BgWriterCommLock);
632
633         for (request = requests; n > 0; request++, n--)
634         {
635                 RememberFsyncRequest(request->rnode, request->segno);
636         }
637         if (requests)
638                 pfree(requests);
639 }