PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
checkpointer.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * checkpointer.c
4  *
5  * The checkpointer is new as of Postgres 9.2. It handles all checkpoints.
6  * Checkpoints are automatically dispatched after a certain amount of time has
7  * elapsed since the last one, and it can be signaled to perform requested
8  * checkpoints as well. (The GUC parameter that mandates a checkpoint every
9  * so many WAL segments is implemented by having backends signal when they
10  * fill WAL segments; the checkpointer itself doesn't watch for the
11  * condition.)
12  *
13  * The checkpointer is started by the postmaster as soon as the startup
14  * subprocess finishes, or as soon as recovery begins if we are doing archive
15  * recovery. It remains alive until the postmaster commands it to terminate.
16  * Normal termination is by SIGUSR2, which instructs the checkpointer to
17  * execute a shutdown checkpoint and then exit(0). (All backends must be
18  * stopped before SIGUSR2 is issued!) Emergency termination is by SIGQUIT;
19  * like any backend, the checkpointer will simply abort and exit on SIGQUIT.
20  *
21  * If the checkpointer exits unexpectedly, the postmaster treats that the same
22  * as a backend crash: shared memory may be corrupted, so remaining backends
23  * should be killed by SIGQUIT and then a recovery cycle started. (Even if
24  * shared memory isn't corrupted, we have lost information about which
25  * files need to be fsync'd for the next checkpoint, and so a system
26  * restart needs to be forced.)
27  *
28  *
29  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
30  *
31  *
32  * IDENTIFICATION
33  * src/backend/postmaster/checkpointer.c
34  *
35  *-------------------------------------------------------------------------
36  */
37 #include "postgres.h"
38 
39 #include <signal.h>
40 #include <sys/time.h>
41 #include <time.h>
42 #include <unistd.h>
43 
44 #include "access/xlog.h"
45 #include "access/xlog_internal.h"
46 #include "libpq/pqsignal.h"
47 #include "miscadmin.h"
48 #include "pgstat.h"
49 #include "postmaster/bgwriter.h"
50 #include "replication/syncrep.h"
51 #include "storage/bufmgr.h"
53 #include "storage/fd.h"
54 #include "storage/ipc.h"
55 #include "storage/lwlock.h"
56 #include "storage/proc.h"
57 #include "storage/shmem.h"
58 #include "storage/smgr.h"
59 #include "storage/spin.h"
60 #include "utils/guc.h"
61 #include "utils/memutils.h"
62 #include "utils/resowner.h"
63 
64 
65 /*----------
66  * Shared memory area for communication between checkpointer and backends
67  *
68  * The ckpt counters allow backends to watch for completion of a checkpoint
69  * request they send. Here's how it works:
70  * * At start of a checkpoint, checkpointer reads (and clears) the request
71  * flags and increments ckpt_started, while holding ckpt_lck.
72  * * On completion of a checkpoint, checkpointer sets ckpt_done to
73  * equal ckpt_started.
74  * * On failure of a checkpoint, checkpointer increments ckpt_failed
75  * and sets ckpt_done to equal ckpt_started.
76  *
77  * The algorithm for backends is:
78  * 1. Record current values of ckpt_failed and ckpt_started, and
79  * set request flags, while holding ckpt_lck.
80  * 2. Send signal to request checkpoint.
81  * 3. Sleep until ckpt_started changes. Now you know a checkpoint has
82  * begun since you started this algorithm (although *not* that it was
83  * specifically initiated by your signal), and that it is using your flags.
84  * 4. Record new value of ckpt_started.
85  * 5. Sleep until ckpt_done >= saved value of ckpt_started. (Use modulo
86  * arithmetic here in case counters wrap around.) Now you know a
87  * checkpoint has started and completed, but not whether it was
88  * successful.
89  * 6. If ckpt_failed is different from the originally saved value,
90  * assume request failed; otherwise it was definitely successful.
91  *
92  * ckpt_flags holds the OR of the checkpoint request flags sent by all
93  * requesting backends since the last checkpoint start. The flags are
94  * chosen so that OR'ing is the correct way to combine multiple requests.
95  *
96  * num_backend_writes is used to count the number of buffer writes performed
97  * by user backend processes. This counter should be wide enough that it
98  * can't overflow during a single processing cycle. num_backend_fsync
99  * counts the subset of those writes that also had to do their own fsync,
100  * because the checkpointer failed to absorb their request.
101  *
102  * The requests array holds fsync requests sent by backends and not yet
103  * absorbed by the checkpointer.
104  *
105  * Unlike the checkpoint fields, num_backend_writes, num_backend_fsync, and
106  * the requests fields are protected by CheckpointerCommLock.
107  *----------
108  */
109 typedef struct
110 {
113  BlockNumber segno; /* see md.c for special values */
114  /* might add a real request-type field later; not needed yet */
116 
117 typedef struct
118 {
119  pid_t checkpointer_pid; /* PID (0 if not started) */
120 
121  slock_t ckpt_lck; /* protects all the ckpt_* fields */
122 
123  int ckpt_started; /* advances when checkpoint starts */
124  int ckpt_done; /* advances when checkpoint done */
125  int ckpt_failed; /* advances when checkpoint fails */
126 
127  int ckpt_flags; /* checkpoint flags, as defined in xlog.h */
128 
129  uint32 num_backend_writes; /* counts user backend buffer writes */
130  uint32 num_backend_fsync; /* counts user backend fsync calls */
131 
132  int num_requests; /* current # of requests */
133  int max_requests; /* allocated array size */
134  CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER];
136 
138 
139 /* interval for calling AbsorbFsyncRequests in CheckpointWriteDelay */
140 #define WRITES_PER_ABSORB 1000
141 
142 /*
143  * GUC parameters
144  */
148 
149 /*
150  * Flags set by interrupt handlers for later service in the main loop.
151  */
152 static volatile sig_atomic_t got_SIGHUP = false;
153 static volatile sig_atomic_t checkpoint_requested = false;
154 static volatile sig_atomic_t shutdown_requested = false;
155 
156 /*
157  * Private state
158  */
159 static bool ckpt_active = false;
160 
161 /* these values are valid when ckpt_active is true: */
164 static double ckpt_cached_elapsed;
165 
168 
169 /* Prototypes for private functions */
170 
171 static void CheckArchiveTimeout(void);
172 static bool IsCheckpointOnSchedule(double progress);
173 static bool ImmediateCheckpointRequested(void);
174 static bool CompactCheckpointerRequestQueue(void);
175 static void UpdateSharedMemoryConfig(void);
176 
177 /* Signal handlers */
178 
179 static void chkpt_quickdie(SIGNAL_ARGS);
180 static void ChkptSigHupHandler(SIGNAL_ARGS);
181 static void ReqCheckpointHandler(SIGNAL_ARGS);
183 static void ReqShutdownHandler(SIGNAL_ARGS);
184 
185 
186 /*
187  * Main entry point for checkpointer process
188  *
189  * This is invoked from AuxiliaryProcessMain, which has already created the
190  * basic execution environment, but not enabled signals yet.
191  */
192 void
194 {
195  sigjmp_buf local_sigjmp_buf;
196  MemoryContext checkpointer_context;
197 
198  CheckpointerShmem->checkpointer_pid = MyProcPid;
199 
200  /*
201  * Properly accept or ignore signals the postmaster might send us
202  *
203  * Note: we deliberately ignore SIGTERM, because during a standard Unix
204  * system shutdown cycle, init will SIGTERM all processes at once. We
205  * want to wait for the backends to exit, whereupon the postmaster will
206  * tell us it's okay to shut down (via SIGUSR2).
207  */
208  pqsignal(SIGHUP, ChkptSigHupHandler); /* set flag to read config
209  * file */
210  pqsignal(SIGINT, ReqCheckpointHandler); /* request checkpoint */
211  pqsignal(SIGTERM, SIG_IGN); /* ignore SIGTERM */
212  pqsignal(SIGQUIT, chkpt_quickdie); /* hard crash time */
216  pqsignal(SIGUSR2, ReqShutdownHandler); /* request shutdown */
217 
218  /*
219  * Reset some signals that are accepted by postmaster but not here
220  */
226 
227  /* We allow SIGQUIT (quickdie) at all times */
228  sigdelset(&BlockSig, SIGQUIT);
229 
230  /*
231  * Initialize so that first time-driven event happens at the correct time.
232  */
234 
235  /*
236  * Create a resource owner to keep track of our resources (currently only
237  * buffer pins).
238  */
239  CurrentResourceOwner = ResourceOwnerCreate(NULL, "Checkpointer");
240 
241  /*
242  * Create a memory context that we will do all our work in. We do this so
243  * that we can reset the context during error recovery and thereby avoid
244  * possible memory leaks. Formerly this code just ran in
245  * TopMemoryContext, but resetting that would be a really bad idea.
246  */
247  checkpointer_context = AllocSetContextCreate(TopMemoryContext,
248  "Checkpointer",
250  MemoryContextSwitchTo(checkpointer_context);
251 
252  /*
253  * If an exception is encountered, processing resumes here.
254  *
255  * See notes in postgres.c about the design of this coding.
256  */
257  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
258  {
259  /* Since not using PG_TRY, must reset error stack by hand */
261 
262  /* Prevent interrupts while cleaning up */
263  HOLD_INTERRUPTS();
264 
265  /* Report the error to the server log */
266  EmitErrorReport();
267 
268  /*
269  * These operations are really just a minimal subset of
270  * AbortTransaction(). We don't have very many resources to worry
271  * about in checkpointer, but we do have LWLocks, buffers, and temp
272  * files.
273  */
277  AbortBufferIO();
278  UnlockBuffers();
279  /* buffer pins are released here: */
282  false, true);
283  /* we needn't bother with the other ResourceOwnerRelease phases */
284  AtEOXact_Buffers(false);
285  AtEOXact_SMgr();
286  AtEOXact_Files();
287  AtEOXact_HashTables(false);
288 
289  /* Warn any waiting backends that the checkpoint failed. */
290  if (ckpt_active)
291  {
292  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
293  CheckpointerShmem->ckpt_failed++;
294  CheckpointerShmem->ckpt_done = CheckpointerShmem->ckpt_started;
295  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
296 
297  ckpt_active = false;
298  }
299 
300  /*
301  * Now return to normal top-level context and clear ErrorContext for
302  * next time.
303  */
304  MemoryContextSwitchTo(checkpointer_context);
305  FlushErrorState();
306 
307  /* Flush any leaked data in the top-level context */
308  MemoryContextResetAndDeleteChildren(checkpointer_context);
309 
310  /* Now we can allow interrupts again */
312 
313  /*
314  * Sleep at least 1 second after any error. A write error is likely
315  * to be repeated, and we don't want to be filling the error logs as
316  * fast as we can.
317  */
318  pg_usleep(1000000L);
319 
320  /*
321  * Close all open files after any error. This is helpful on Windows,
322  * where holding deleted files open causes various strange errors.
323  * It's not clear we need it elsewhere, but shouldn't hurt.
324  */
325  smgrcloseall();
326  }
327 
328  /* We can now handle ereport(ERROR) */
329  PG_exception_stack = &local_sigjmp_buf;
330 
331  /*
332  * Unblock signals (they were blocked when the postmaster forked us)
333  */
335 
336  /*
337  * Ensure all shared memory values are set correctly for the config. Doing
338  * this here ensures no race conditions from other concurrent updaters.
339  */
341 
342  /*
343  * Advertise our latch that backends can use to wake us up while we're
344  * sleeping.
345  */
347 
348  /*
349  * Loop forever
350  */
351  for (;;)
352  {
353  bool do_checkpoint = false;
354  int flags = 0;
355  pg_time_t now;
356  int elapsed_secs;
357  int cur_timeout;
358  int rc;
359 
360  /* Clear any already-pending wakeups */
362 
363  /*
364  * Process any requests or signals received recently.
365  */
367 
368  if (got_SIGHUP)
369  {
370  got_SIGHUP = false;
372 
373  /*
374  * Checkpointer is the last process to shut down, so we ask it to
375  * hold the keys for a range of other tasks required most of which
376  * have nothing to do with checkpointing at all.
377  *
378  * For various reasons, some config values can change dynamically
379  * so the primary copy of them is held in shared memory to make
380  * sure all backends see the same value. We make Checkpointer
381  * responsible for updating the shared memory copy if the
382  * parameter setting changes because of SIGHUP.
383  */
385  }
387  {
388  checkpoint_requested = false;
389  do_checkpoint = true;
391  }
392  if (shutdown_requested)
393  {
394  /*
395  * From here on, elog(ERROR) should end with exit(1), not send
396  * control back to the sigsetjmp block above
397  */
398  ExitOnAnyError = true;
399  /* Close down the database */
400  ShutdownXLOG(0, 0);
401  /* Normal exit from the checkpointer is here */
402  proc_exit(0); /* done */
403  }
404 
405  /*
406  * Force a checkpoint if too much time has elapsed since the last one.
407  * Note that we count a timed checkpoint in stats only when this
408  * occurs without an external request, but we set the CAUSE_TIME flag
409  * bit even if there is also an external request.
410  */
411  now = (pg_time_t) time(NULL);
412  elapsed_secs = now - last_checkpoint_time;
413  if (elapsed_secs >= CheckPointTimeout)
414  {
415  if (!do_checkpoint)
417  do_checkpoint = true;
418  flags |= CHECKPOINT_CAUSE_TIME;
419  }
420 
421  /*
422  * Do a checkpoint if requested.
423  */
424  if (do_checkpoint)
425  {
426  bool ckpt_performed = false;
427  bool do_restartpoint;
428 
429  /*
430  * Check if we should perform a checkpoint or a restartpoint. As a
431  * side-effect, RecoveryInProgress() initializes TimeLineID if
432  * it's not set yet.
433  */
434  do_restartpoint = RecoveryInProgress();
435 
436  /*
437  * Atomically fetch the request flags to figure out what kind of a
438  * checkpoint we should perform, and increase the started-counter
439  * to acknowledge that we've started a new checkpoint.
440  */
441  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
442  flags |= CheckpointerShmem->ckpt_flags;
443  CheckpointerShmem->ckpt_flags = 0;
444  CheckpointerShmem->ckpt_started++;
445  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
446 
447  /*
448  * The end-of-recovery checkpoint is a real checkpoint that's
449  * performed while we're still in recovery.
450  */
451  if (flags & CHECKPOINT_END_OF_RECOVERY)
452  do_restartpoint = false;
453 
454  /*
455  * We will warn if (a) too soon since last checkpoint (whatever
456  * caused it) and (b) somebody set the CHECKPOINT_CAUSE_XLOG flag
457  * since the last checkpoint start. Note in particular that this
458  * implementation will not generate warnings caused by
459  * CheckPointTimeout < CheckPointWarning.
460  */
461  if (!do_restartpoint &&
462  (flags & CHECKPOINT_CAUSE_XLOG) &&
463  elapsed_secs < CheckPointWarning)
464  ereport(LOG,
465  (errmsg_plural("checkpoints are occurring too frequently (%d second apart)",
466  "checkpoints are occurring too frequently (%d seconds apart)",
467  elapsed_secs,
468  elapsed_secs),
469  errhint("Consider increasing the configuration parameter \"max_wal_size\".")));
470 
471  /*
472  * Initialize checkpointer-private variables used during
473  * checkpoint.
474  */
475  ckpt_active = true;
476  if (do_restartpoint)
478  else
482 
483  /*
484  * Do the checkpoint.
485  */
486  if (!do_restartpoint)
487  {
488  CreateCheckPoint(flags);
489  ckpt_performed = true;
490  }
491  else
492  ckpt_performed = CreateRestartPoint(flags);
493 
494  /*
495  * After any checkpoint, close all smgr files. This is so we
496  * won't hang onto smgr references to deleted files indefinitely.
497  */
498  smgrcloseall();
499 
500  /*
501  * Indicate checkpoint completion to any waiting backends.
502  */
503  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
504  CheckpointerShmem->ckpt_done = CheckpointerShmem->ckpt_started;
505  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
506 
507  if (ckpt_performed)
508  {
509  /*
510  * Note we record the checkpoint start time not end time as
511  * last_checkpoint_time. This is so that time-driven
512  * checkpoints happen at a predictable spacing.
513  */
514  last_checkpoint_time = now;
515  }
516  else
517  {
518  /*
519  * We were not able to perform the restartpoint (checkpoints
520  * throw an ERROR in case of error). Most likely because we
521  * have not received any new checkpoint WAL records since the
522  * last restartpoint. Try again in 15 s.
523  */
524  last_checkpoint_time = now - CheckPointTimeout + 15;
525  }
526 
527  ckpt_active = false;
528  }
529 
530  /* Check for archive_timeout and switch xlog files if necessary. */
532 
533  /*
534  * Send off activity statistics to the stats collector. (The reason
535  * why we re-use bgwriter-related code for this is that the bgwriter
536  * and checkpointer used to be just one process. It's probably not
537  * worth the trouble to split the stats support into two independent
538  * stats message types.)
539  */
541 
542  /*
543  * Sleep until we are signaled or it's time for another checkpoint or
544  * xlog file switch.
545  */
546  now = (pg_time_t) time(NULL);
547  elapsed_secs = now - last_checkpoint_time;
548  if (elapsed_secs >= CheckPointTimeout)
549  continue; /* no sleep for us ... */
550  cur_timeout = CheckPointTimeout - elapsed_secs;
552  {
553  elapsed_secs = now - last_xlog_switch_time;
554  if (elapsed_secs >= XLogArchiveTimeout)
555  continue; /* no sleep for us ... */
556  cur_timeout = Min(cur_timeout, XLogArchiveTimeout - elapsed_secs);
557  }
558 
559  rc = WaitLatch(MyLatch,
561  cur_timeout * 1000L /* convert to ms */,
563 
564  /*
565  * Emergency bailout if postmaster has died. This is to avoid the
566  * necessity for manual cleanup of all postmaster children.
567  */
568  if (rc & WL_POSTMASTER_DEATH)
569  exit(1);
570  }
571 }
572 
573 /*
574  * CheckArchiveTimeout -- check for archive_timeout and switch xlog files
575  *
576  * This will switch to a new WAL file and force an archive file write if
577  * meaningful activity is recorded in the current WAL file. This includes most
578  * writes, including just a single checkpoint record, but excludes WAL records
579  * that were inserted with the XLOG_MARK_UNIMPORTANT flag being set (like
580  * snapshots of running transactions). Such records, depending on
581  * configuration, occur on regular intervals and don't contain important
582  * information. This avoids generating archives with a few unimportant
583  * records.
584  */
585 static void
587 {
588  pg_time_t now;
589  pg_time_t last_time;
590  XLogRecPtr last_switch_lsn;
591 
593  return;
594 
595  now = (pg_time_t) time(NULL);
596 
597  /* First we do a quick check using possibly-stale local state. */
598  if ((int) (now - last_xlog_switch_time) < XLogArchiveTimeout)
599  return;
600 
601  /*
602  * Update local state ... note that last_xlog_switch_time is the last time
603  * a switch was performed *or requested*.
604  */
605  last_time = GetLastSegSwitchData(&last_switch_lsn);
606 
608 
609  /* Now we can do the real checks */
610  if ((int) (now - last_xlog_switch_time) >= XLogArchiveTimeout)
611  {
612  /*
613  * Switch segment only when "important" WAL has been logged since the
614  * last segment switch.
615  */
616  if (GetLastImportantRecPtr() > last_switch_lsn)
617  {
618  XLogRecPtr switchpoint;
619 
620  /* mark switch as unimportant, avoids triggering checkpoints */
621  switchpoint = RequestXLogSwitch(true);
622 
623  /*
624  * If the returned pointer points exactly to a segment boundary,
625  * assume nothing happened.
626  */
627  if ((switchpoint % XLogSegSize) != 0)
628  ereport(DEBUG1,
629  (errmsg("transaction log switch forced (archive_timeout=%d)",
631  }
632 
633  /*
634  * Update state in any case, so we don't retry constantly when the
635  * system is idle.
636  */
638  }
639 }
640 
641 /*
642  * Returns true if an immediate checkpoint request is pending. (Note that
643  * this does not check the *current* checkpoint's IMMEDIATE flag, but whether
644  * there is one pending behind it.)
645  */
646 static bool
648 {
650  {
652 
653  /*
654  * We don't need to acquire the ckpt_lck in this case because we're
655  * only looking at a single flag bit.
656  */
657  if (cps->ckpt_flags & CHECKPOINT_IMMEDIATE)
658  return true;
659  }
660  return false;
661 }
662 
663 /*
664  * CheckpointWriteDelay -- control rate of checkpoint
665  *
666  * This function is called after each page write performed by BufferSync().
667  * It is responsible for throttling BufferSync()'s write rate to hit
668  * checkpoint_completion_target.
669  *
670  * The checkpoint request flags should be passed in; currently the only one
671  * examined is CHECKPOINT_IMMEDIATE, which disables delays between writes.
672  *
673  * 'progress' is an estimate of how much of the work has been done, as a
674  * fraction between 0.0 meaning none, and 1.0 meaning all done.
675  */
676 void
677 CheckpointWriteDelay(int flags, double progress)
678 {
679  static int absorb_counter = WRITES_PER_ABSORB;
680 
681  /* Do nothing if checkpoint is being executed by non-checkpointer process */
682  if (!AmCheckpointerProcess())
683  return;
684 
685  /*
686  * Perform the usual duties and take a nap, unless we're behind schedule,
687  * in which case we just try to catch up as quickly as possible.
688  */
689  if (!(flags & CHECKPOINT_IMMEDIATE) &&
692  IsCheckpointOnSchedule(progress))
693  {
694  if (got_SIGHUP)
695  {
696  got_SIGHUP = false;
698  /* update shmem copies of config variables */
700  }
701 
703  absorb_counter = WRITES_PER_ABSORB;
704 
706 
707  /*
708  * Report interim activity statistics to the stats collector.
709  */
711 
712  /*
713  * This sleep used to be connected to bgwriter_delay, typically 200ms.
714  * That resulted in more frequent wakeups if not much work to do.
715  * Checkpointer and bgwriter are no longer related so take the Big
716  * Sleep.
717  */
718  pg_usleep(100000L);
719  }
720  else if (--absorb_counter <= 0)
721  {
722  /*
723  * Absorb pending fsync requests after each WRITES_PER_ABSORB write
724  * operations even when we don't sleep, to prevent overflow of the
725  * fsync request queue.
726  */
728  absorb_counter = WRITES_PER_ABSORB;
729  }
730 }
731 
732 /*
733  * IsCheckpointOnSchedule -- are we on schedule to finish this checkpoint
734  * (or restartpoint) in time?
735  *
736  * Compares the current progress against the time/segments elapsed since last
737  * checkpoint, and returns true if the progress we've made this far is greater
738  * than the elapsed time/segments.
739  */
740 static bool
742 {
743  XLogRecPtr recptr;
744  struct timeval now;
745  double elapsed_xlogs,
746  elapsed_time;
747 
749 
750  /* Scale progress according to checkpoint_completion_target. */
751  progress *= CheckPointCompletionTarget;
752 
753  /*
754  * Check against the cached value first. Only do the more expensive
755  * calculations once we reach the target previously calculated. Since
756  * neither time or WAL insert pointer moves backwards, a freshly
757  * calculated value can only be greater than or equal to the cached value.
758  */
759  if (progress < ckpt_cached_elapsed)
760  return false;
761 
762  /*
763  * Check progress against WAL segments written and CheckPointSegments.
764  *
765  * We compare the current WAL insert location against the location
766  * computed before calling CreateCheckPoint. The code in XLogInsert that
767  * actually triggers a checkpoint when CheckPointSegments is exceeded
768  * compares against RedoRecptr, so this is not completely accurate.
769  * However, it's good enough for our purposes, we're only calculating an
770  * estimate anyway.
771  *
772  * During recovery, we compare last replayed WAL record's location with
773  * the location computed before calling CreateRestartPoint. That maintains
774  * the same pacing as we have during checkpoints in normal operation, but
775  * we might exceed max_wal_size by a fair amount. That's because there can
776  * be a large gap between a checkpoint's redo-pointer and the checkpoint
777  * record itself, and we only start the restartpoint after we've seen the
778  * checkpoint record. (The gap is typically up to CheckPointSegments *
779  * checkpoint_completion_target where checkpoint_completion_target is the
780  * value that was in effect when the WAL was generated).
781  */
782  if (RecoveryInProgress())
783  recptr = GetXLogReplayRecPtr(NULL);
784  else
785  recptr = GetInsertRecPtr();
786  elapsed_xlogs = (((double) (recptr - ckpt_start_recptr)) / XLogSegSize) / CheckPointSegments;
787 
788  if (progress < elapsed_xlogs)
789  {
790  ckpt_cached_elapsed = elapsed_xlogs;
791  return false;
792  }
793 
794  /*
795  * Check progress against time elapsed and checkpoint_timeout.
796  */
797  gettimeofday(&now, NULL);
798  elapsed_time = ((double) ((pg_time_t) now.tv_sec - ckpt_start_time) +
799  now.tv_usec / 1000000.0) / CheckPointTimeout;
800 
801  if (progress < elapsed_time)
802  {
804  return false;
805  }
806 
807  /* It looks like we're on schedule. */
808  return true;
809 }
810 
811 
812 /* --------------------------------
813  * signal handler routines
814  * --------------------------------
815  */
816 
817 /*
818  * chkpt_quickdie() occurs when signalled SIGQUIT by the postmaster.
819  *
820  * Some backend has bought the farm,
821  * so we need to stop what we're doing and exit.
822  */
823 static void
825 {
827 
828  /*
829  * We DO NOT want to run proc_exit() callbacks -- we're here because
830  * shared memory may be corrupted, so we don't want to try to clean up our
831  * transaction. Just nail the windows shut and get out of town. Now that
832  * there's an atexit callback to prevent third-party code from breaking
833  * things by calling exit() directly, we have to reset the callbacks
834  * explicitly to make this work as intended.
835  */
836  on_exit_reset();
837 
838  /*
839  * Note we do exit(2) not exit(0). This is to force the postmaster into a
840  * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
841  * backend. This is necessary precisely because we don't clean up our
842  * shared memory state. (The "dead man switch" mechanism in pmsignal.c
843  * should ensure the postmaster sees this as a crash, too, but no harm in
844  * being doubly sure.)
845  */
846  exit(2);
847 }
848 
849 /* SIGHUP: set flag to re-read config file at next convenient time */
850 static void
852 {
853  int save_errno = errno;
854 
855  got_SIGHUP = true;
856  SetLatch(MyLatch);
857 
858  errno = save_errno;
859 }
860 
861 /* SIGINT: set flag to run a normal checkpoint right away */
862 static void
864 {
865  int save_errno = errno;
866 
867  checkpoint_requested = true;
868  SetLatch(MyLatch);
869 
870  errno = save_errno;
871 }
872 
873 /* SIGUSR1: used for latch wakeups */
874 static void
876 {
877  int save_errno = errno;
878 
880 
881  errno = save_errno;
882 }
883 
884 /* SIGUSR2: set flag to run a shutdown checkpoint and exit */
885 static void
887 {
888  int save_errno = errno;
889 
890  shutdown_requested = true;
891  SetLatch(MyLatch);
892 
893  errno = save_errno;
894 }
895 
896 
897 /* --------------------------------
898  * communication with backends
899  * --------------------------------
900  */
901 
902 /*
903  * CheckpointerShmemSize
904  * Compute space needed for checkpointer-related shared memory
905  */
906 Size
908 {
909  Size size;
910 
911  /*
912  * Currently, the size of the requests[] array is arbitrarily set equal to
913  * NBuffers. This may prove too large or small ...
914  */
915  size = offsetof(CheckpointerShmemStruct, requests);
916  size = add_size(size, mul_size(NBuffers, sizeof(CheckpointerRequest)));
917 
918  return size;
919 }
920 
921 /*
922  * CheckpointerShmemInit
923  * Allocate and initialize checkpointer-related shared memory
924  */
925 void
927 {
928  Size size = CheckpointerShmemSize();
929  bool found;
930 
931  CheckpointerShmem = (CheckpointerShmemStruct *)
932  ShmemInitStruct("Checkpointer Data",
933  size,
934  &found);
935 
936  if (!found)
937  {
938  /*
939  * First time through, so initialize. Note that we zero the whole
940  * requests array; this is so that CompactCheckpointerRequestQueue can
941  * assume that any pad bytes in the request structs are zeroes.
942  */
943  MemSet(CheckpointerShmem, 0, size);
944  SpinLockInit(&CheckpointerShmem->ckpt_lck);
945  CheckpointerShmem->max_requests = NBuffers;
946  }
947 }
948 
949 /*
950  * RequestCheckpoint
951  * Called in backend processes to request a checkpoint
952  *
953  * flags is a bitwise OR of the following:
954  * CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown.
955  * CHECKPOINT_END_OF_RECOVERY: checkpoint is for end of WAL recovery.
956  * CHECKPOINT_IMMEDIATE: finish the checkpoint ASAP,
957  * ignoring checkpoint_completion_target parameter.
958  * CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occurred
959  * since the last one (implied by CHECKPOINT_IS_SHUTDOWN or
960  * CHECKPOINT_END_OF_RECOVERY).
961  * CHECKPOINT_WAIT: wait for completion before returning (otherwise,
962  * just signal checkpointer to do it, and return).
963  * CHECKPOINT_CAUSE_XLOG: checkpoint is requested due to xlog filling.
964  * (This affects logging, and in particular enables CheckPointWarning.)
965  */
966 void
968 {
969  int ntries;
970  int old_failed,
971  old_started;
972 
973  /*
974  * If in a standalone backend, just do it ourselves.
975  */
977  {
978  /*
979  * There's no point in doing slow checkpoints in a standalone backend,
980  * because there's no other backends the checkpoint could disrupt.
981  */
983 
984  /*
985  * After any checkpoint, close all smgr files. This is so we won't
986  * hang onto smgr references to deleted files indefinitely.
987  */
988  smgrcloseall();
989 
990  return;
991  }
992 
993  /*
994  * Atomically set the request flags, and take a snapshot of the counters.
995  * When we see ckpt_started > old_started, we know the flags we set here
996  * have been seen by checkpointer.
997  *
998  * Note that we OR the flags with any existing flags, to avoid overriding
999  * a "stronger" request by another backend. The flag senses must be
1000  * chosen to make this work!
1001  */
1002  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
1003 
1004  old_failed = CheckpointerShmem->ckpt_failed;
1005  old_started = CheckpointerShmem->ckpt_started;
1006  CheckpointerShmem->ckpt_flags |= flags;
1007 
1008  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
1009 
1010  /*
1011  * Send signal to request checkpoint. It's possible that the checkpointer
1012  * hasn't started yet, or is in process of restarting, so we will retry a
1013  * few times if needed. Also, if not told to wait for the checkpoint to
1014  * occur, we consider failure to send the signal to be nonfatal and merely
1015  * LOG it.
1016  */
1017  for (ntries = 0;; ntries++)
1018  {
1019  if (CheckpointerShmem->checkpointer_pid == 0)
1020  {
1021  if (ntries >= 20) /* max wait 2.0 sec */
1022  {
1023  elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
1024  "could not request checkpoint because checkpointer not running");
1025  break;
1026  }
1027  }
1028  else if (kill(CheckpointerShmem->checkpointer_pid, SIGINT) != 0)
1029  {
1030  if (ntries >= 20) /* max wait 2.0 sec */
1031  {
1032  elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
1033  "could not signal for checkpoint: %m");
1034  break;
1035  }
1036  }
1037  else
1038  break; /* signal sent successfully */
1039 
1041  pg_usleep(100000L); /* wait 0.1 sec, then retry */
1042  }
1043 
1044  /*
1045  * If requested, wait for completion. We detect completion according to
1046  * the algorithm given above.
1047  */
1048  if (flags & CHECKPOINT_WAIT)
1049  {
1050  int new_started,
1051  new_failed;
1052 
1053  /* Wait for a new checkpoint to start. */
1054  for (;;)
1055  {
1056  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
1057  new_started = CheckpointerShmem->ckpt_started;
1058  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
1059 
1060  if (new_started != old_started)
1061  break;
1062 
1064  pg_usleep(100000L);
1065  }
1066 
1067  /*
1068  * We are waiting for ckpt_done >= new_started, in a modulo sense.
1069  */
1070  for (;;)
1071  {
1072  int new_done;
1073 
1074  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
1075  new_done = CheckpointerShmem->ckpt_done;
1076  new_failed = CheckpointerShmem->ckpt_failed;
1077  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
1078 
1079  if (new_done - new_started >= 0)
1080  break;
1081 
1083  pg_usleep(100000L);
1084  }
1085 
1086  if (new_failed != old_failed)
1087  ereport(ERROR,
1088  (errmsg("checkpoint request failed"),
1089  errhint("Consult recent messages in the server log for details.")));
1090  }
1091 }
1092 
1093 /*
1094  * ForwardFsyncRequest
1095  * Forward a file-fsync request from a backend to the checkpointer
1096  *
1097  * Whenever a backend is compelled to write directly to a relation
1098  * (which should be seldom, if the background writer is getting its job done),
1099  * the backend calls this routine to pass over knowledge that the relation
1100  * is dirty and must be fsync'd before next checkpoint. We also use this
1101  * opportunity to count such writes for statistical purposes.
1102  *
1103  * This functionality is only supported for regular (not backend-local)
1104  * relations, so the rnode argument is intentionally RelFileNode not
1105  * RelFileNodeBackend.
1106  *
1107  * segno specifies which segment (not block!) of the relation needs to be
1108  * fsync'd. (Since the valid range is much less than BlockNumber, we can
1109  * use high values for special flags; that's all internal to md.c, which
1110  * see for details.)
1111  *
1112  * To avoid holding the lock for longer than necessary, we normally write
1113  * to the requests[] queue without checking for duplicates. The checkpointer
1114  * will have to eliminate dups internally anyway. However, if we discover
1115  * that the queue is full, we make a pass over the entire queue to compact
1116  * it. This is somewhat expensive, but the alternative is for the backend
1117  * to perform its own fsync, which is far more expensive in practice. It
1118  * is theoretically possible a backend fsync might still be necessary, if
1119  * the queue is full and contains no duplicate entries. In that case, we
1120  * let the backend know by returning false.
1121  */
1122 bool
1124 {
1125  CheckpointerRequest *request;
1126  bool too_full;
1127 
1128  if (!IsUnderPostmaster)
1129  return false; /* probably shouldn't even get here */
1130 
1131  if (AmCheckpointerProcess())
1132  elog(ERROR, "ForwardFsyncRequest must not be called in checkpointer");
1133 
1134  LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1135 
1136  /* Count all backend writes regardless of if they fit in the queue */
1138  CheckpointerShmem->num_backend_writes++;
1139 
1140  /*
1141  * If the checkpointer isn't running or the request queue is full, the
1142  * backend will have to perform its own fsync request. But before forcing
1143  * that to happen, we can try to compact the request queue.
1144  */
1145  if (CheckpointerShmem->checkpointer_pid == 0 ||
1146  (CheckpointerShmem->num_requests >= CheckpointerShmem->max_requests &&
1148  {
1149  /*
1150  * Count the subset of writes where backends have to do their own
1151  * fsync
1152  */
1154  CheckpointerShmem->num_backend_fsync++;
1155  LWLockRelease(CheckpointerCommLock);
1156  return false;
1157  }
1158 
1159  /* OK, insert request */
1160  request = &CheckpointerShmem->requests[CheckpointerShmem->num_requests++];
1161  request->rnode = rnode;
1162  request->forknum = forknum;
1163  request->segno = segno;
1164 
1165  /* If queue is more than half full, nudge the checkpointer to empty it */
1166  too_full = (CheckpointerShmem->num_requests >=
1167  CheckpointerShmem->max_requests / 2);
1168 
1169  LWLockRelease(CheckpointerCommLock);
1170 
1171  /* ... but not till after we release the lock */
1172  if (too_full && ProcGlobal->checkpointerLatch)
1174 
1175  return true;
1176 }
1177 
1178 /*
1179  * CompactCheckpointerRequestQueue
1180  * Remove duplicates from the request queue to avoid backend fsyncs.
1181  * Returns "true" if any entries were removed.
1182  *
1183  * Although a full fsync request queue is not common, it can lead to severe
1184  * performance problems when it does happen. So far, this situation has
1185  * only been observed to occur when the system is under heavy write load,
1186  * and especially during the "sync" phase of a checkpoint. Without this
1187  * logic, each backend begins doing an fsync for every block written, which
1188  * gets very expensive and can slow down the whole system.
1189  *
1190  * Trying to do this every time the queue is full could lose if there
1191  * aren't any removable entries. But that should be vanishingly rare in
1192  * practice: there's one queue entry per shared buffer.
1193  */
1194 static bool
1196 {
1197  struct CheckpointerSlotMapping
1198  {
1199  CheckpointerRequest request;
1200  int slot;
1201  };
1202 
1203  int n,
1204  preserve_count;
1205  int num_skipped = 0;
1206  HASHCTL ctl;
1207  HTAB *htab;
1208  bool *skip_slot;
1209 
1210  /* must hold CheckpointerCommLock in exclusive mode */
1211  Assert(LWLockHeldByMe(CheckpointerCommLock));
1212 
1213  /* Initialize skip_slot array */
1214  skip_slot = palloc0(sizeof(bool) * CheckpointerShmem->num_requests);
1215 
1216  /* Initialize temporary hash table */
1217  MemSet(&ctl, 0, sizeof(ctl));
1218  ctl.keysize = sizeof(CheckpointerRequest);
1219  ctl.entrysize = sizeof(struct CheckpointerSlotMapping);
1220  ctl.hcxt = CurrentMemoryContext;
1221 
1222  htab = hash_create("CompactCheckpointerRequestQueue",
1223  CheckpointerShmem->num_requests,
1224  &ctl,
1226 
1227  /*
1228  * The basic idea here is that a request can be skipped if it's followed
1229  * by a later, identical request. It might seem more sensible to work
1230  * backwards from the end of the queue and check whether a request is
1231  * *preceded* by an earlier, identical request, in the hopes of doing less
1232  * copying. But that might change the semantics, if there's an
1233  * intervening FORGET_RELATION_FSYNC or FORGET_DATABASE_FSYNC request, so
1234  * we do it this way. It would be possible to be even smarter if we made
1235  * the code below understand the specific semantics of such requests (it
1236  * could blow away preceding entries that would end up being canceled
1237  * anyhow), but it's not clear that the extra complexity would buy us
1238  * anything.
1239  */
1240  for (n = 0; n < CheckpointerShmem->num_requests; n++)
1241  {
1242  CheckpointerRequest *request;
1243  struct CheckpointerSlotMapping *slotmap;
1244  bool found;
1245 
1246  /*
1247  * We use the request struct directly as a hashtable key. This
1248  * assumes that any padding bytes in the structs are consistently the
1249  * same, which should be okay because we zeroed them in
1250  * CheckpointerShmemInit. Note also that RelFileNode had better
1251  * contain no pad bytes.
1252  */
1253  request = &CheckpointerShmem->requests[n];
1254  slotmap = hash_search(htab, request, HASH_ENTER, &found);
1255  if (found)
1256  {
1257  /* Duplicate, so mark the previous occurrence as skippable */
1258  skip_slot[slotmap->slot] = true;
1259  num_skipped++;
1260  }
1261  /* Remember slot containing latest occurrence of this request value */
1262  slotmap->slot = n;
1263  }
1264 
1265  /* Done with the hash table. */
1266  hash_destroy(htab);
1267 
1268  /* If no duplicates, we're out of luck. */
1269  if (!num_skipped)
1270  {
1271  pfree(skip_slot);
1272  return false;
1273  }
1274 
1275  /* We found some duplicates; remove them. */
1276  preserve_count = 0;
1277  for (n = 0; n < CheckpointerShmem->num_requests; n++)
1278  {
1279  if (skip_slot[n])
1280  continue;
1281  CheckpointerShmem->requests[preserve_count++] = CheckpointerShmem->requests[n];
1282  }
1283  ereport(DEBUG1,
1284  (errmsg("compacted fsync request queue from %d entries to %d entries",
1285  CheckpointerShmem->num_requests, preserve_count)));
1286  CheckpointerShmem->num_requests = preserve_count;
1287 
1288  /* Cleanup. */
1289  pfree(skip_slot);
1290  return true;
1291 }
1292 
1293 /*
1294  * AbsorbFsyncRequests
1295  * Retrieve queued fsync requests and pass them to local smgr.
1296  *
1297  * This is exported because it must be called during CreateCheckPoint;
1298  * we have to be sure we have accepted all pending requests just before
1299  * we start fsync'ing. Since CreateCheckPoint sometimes runs in
1300  * non-checkpointer processes, do nothing if not checkpointer.
1301  */
1302 void
1304 {
1305  CheckpointerRequest *requests = NULL;
1306  CheckpointerRequest *request;
1307  int n;
1308 
1309  if (!AmCheckpointerProcess())
1310  return;
1311 
1312  LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1313 
1314  /* Transfer stats counts into pending pgstats message */
1316  BgWriterStats.m_buf_fsync_backend += CheckpointerShmem->num_backend_fsync;
1317 
1318  CheckpointerShmem->num_backend_writes = 0;
1319  CheckpointerShmem->num_backend_fsync = 0;
1320 
1321  /*
1322  * We try to avoid holding the lock for a long time by copying the request
1323  * array, and processing the requests after releasing the lock.
1324  *
1325  * Once we have cleared the requests from shared memory, we have to PANIC
1326  * if we then fail to absorb them (eg, because our hashtable runs out of
1327  * memory). This is because the system cannot run safely if we are unable
1328  * to fsync what we have been told to fsync. Fortunately, the hashtable
1329  * is so small that the problem is quite unlikely to arise in practice.
1330  */
1331  n = CheckpointerShmem->num_requests;
1332  if (n > 0)
1333  {
1334  requests = (CheckpointerRequest *) palloc(n * sizeof(CheckpointerRequest));
1335  memcpy(requests, CheckpointerShmem->requests, n * sizeof(CheckpointerRequest));
1336  }
1337 
1339 
1340  CheckpointerShmem->num_requests = 0;
1341 
1342  LWLockRelease(CheckpointerCommLock);
1343 
1344  for (request = requests; n > 0; request++, n--)
1345  RememberFsyncRequest(request->rnode, request->forknum, request->segno);
1346 
1347  END_CRIT_SECTION();
1348 
1349  if (requests)
1350  pfree(requests);
1351 }
1352 
1353 /*
1354  * Update any shared memory configurations based on config parameters
1355  */
1356 static void
1358 {
1359  /* update global shmem state for sync rep */
1361 
1362  /*
1363  * If full_page_writes has been changed by SIGHUP, we update it in shared
1364  * memory and write an XLOG_FPW_CHANGE record.
1365  */
1367 
1368  elog(DEBUG2, "checkpointer updated shared memory configuration values");
1369 }
1370 
1371 /*
1372  * FirstCallSinceLastCheckpoint allows a process to take an action once
1373  * per checkpoint cycle by asynchronously checking for checkpoint completion.
1374  */
1375 bool
1377 {
1378  static int ckpt_done = 0;
1379  int new_done;
1380  bool FirstCall = false;
1381 
1382  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
1383  new_done = CheckpointerShmem->ckpt_done;
1384  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
1385 
1386  if (new_done != ckpt_done)
1387  FirstCall = true;
1388 
1389  ckpt_done = new_done;
1390 
1391  return FirstCall;
1392 }
XLogRecPtr GetLastImportantRecPtr(void)
Definition: xlog.c:8172
void RememberFsyncRequest(RelFileNode rnode, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1513
int slock_t
Definition: s_lock.h:888
PgStat_Counter m_buf_fsync_backend
Definition: pgstat.h:419
void SyncRepUpdateSyncStandbysDefined(void)
Definition: syncrep.c:1022
#define XLogSegSize
Definition: xlog_internal.h:92
bool IsPostmasterEnvironment
Definition: globals.c:99
void CheckpointWriteDelay(int flags, double progress)
Definition: checkpointer.c:677
#define SIGUSR1
Definition: win32.h:211
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:793
static bool IsCheckpointOnSchedule(double progress)
Definition: checkpointer.c:741
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
XLogRecPtr RequestXLogSwitch(bool mark_unimportant)
Definition: xlog.c:9322
#define DEBUG1
Definition: elog.h:25
XLogRecPtr GetInsertRecPtr(void)
Definition: xlog.c:8138
int MyProcPid
Definition: globals.c:38
int errhint(const char *fmt,...)
Definition: elog.c:987
#define SIGCONT
Definition: win32.h:205
int64 pg_time_t
Definition: pgtime.h:23
pg_time_t GetLastSegSwitchData(XLogRecPtr *lastSwitchLSN)
Definition: xlog.c:8201
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
#define WL_TIMEOUT
Definition: latch.h:127
void ProcessConfigFile(GucContext context)
static void ReqCheckpointHandler(SIGNAL_ARGS)
Definition: checkpointer.c:863
int XLogArchiveTimeout
Definition: xlog.c:92
MemoryContext hcxt
Definition: hsearch.h:78
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1830
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:850
bool ForwardFsyncRequest(RelFileNode rnode, ForkNumber forknum, BlockNumber segno)
void CreateCheckPoint(int flags)
Definition: xlog.c:8449
PGPROC * MyProc
Definition: proc.c:67
PgStat_Counter m_timed_checkpoints
Definition: pgstat.h:413
#define SIGWINCH
Definition: win32.h:209
void AtEOXact_Buffers(bool isCommit)
Definition: bufmgr.c:2398
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
#define SpinLockInit(lock)
Definition: spin.h:60
#define Min(x, y)
Definition: c.h:802
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
bool CreateRestartPoint(int flags)
Definition: xlog.c:8991
#define SIGTTIN
Definition: win32.h:207
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void CheckpointerShmemInit(void)
Definition: checkpointer.c:926
PgStat_MsgBgWriter BgWriterStats
Definition: pgstat.c:127
Size entrysize
Definition: hsearch.h:73
int CheckPointWarning
Definition: checkpointer.c:146
CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER]
Definition: checkpointer.c:134
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
void proc_exit(int code)
Definition: ipc.c:99
PROC_HDR * ProcGlobal
Definition: proc.c:80
#define MemSet(start, val, len)
Definition: c.h:853
uint32 BlockNumber
Definition: block.h:31
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
void ResetLatch(volatile Latch *latch)
Definition: latch.c:461
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:7805
#define SIGQUIT
Definition: win32.h:197
void FlushErrorState(void)
Definition: elog.c:1587
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
Latch procLatch
Definition: proc.h:93
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
void smgrcloseall(void)
Definition: smgr.c:326
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:116
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define CHECKPOINT_CAUSE_XLOG
Definition: xlog.h:186
PgStat_Counter m_requested_checkpoints
Definition: pgstat.h:414
#define SpinLockAcquire(lock)
Definition: spin.h:62
static bool ImmediateCheckpointRequested(void)
Definition: checkpointer.c:647
void pg_usleep(long microsec)
Definition: signal.c:53
Definition: dynahash.c:193
void AtEOXact_SMgr(void)
Definition: smgr.c:798
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition: latch.c:300
void pfree(void *pointer)
Definition: mcxt.c:992
#define SIG_IGN
Definition: win32.h:193
void ConditionVariableCancelSleep(void)
#define AmBackgroundWriterProcess()
Definition: miscadmin.h:404
#define ERROR
Definition: elog.h:43
void on_exit_reset(void)
Definition: ipc.c:396
#define AmCheckpointerProcess()
Definition: miscadmin.h:405
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:372
void AtEOXact_Files(void)
Definition: fd.c:2566
static bool CompactCheckpointerRequestQueue(void)
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:10984
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
#define DEBUG2
Definition: elog.h:24
static void ChkptSigHupHandler(SIGNAL_ARGS)
Definition: checkpointer.c:851
bool IsUnderPostmaster
Definition: globals.c:100
int CheckPointTimeout
Definition: checkpointer.c:145
#define CHECKPOINT_END_OF_RECOVERY
Definition: xlog.h:176
unsigned int uint32
Definition: c.h:265
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1124
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:43
ForkNumber
Definition: relpath.h:24
Definition: guc.h:72
int CheckPointSegments
Definition: xlog.c:123
static CheckpointerShmemStruct * CheckpointerShmem
Definition: checkpointer.c:137
void ShutdownXLOG(int code, Datum arg)
Definition: xlog.c:8253
void UnlockBuffers(void)
Definition: bufmgr.c:3501
int progress
Definition: pgbench.c:172
#define MemoryContextResetAndDeleteChildren(ctx)
Definition: memutils.h:88
#define SpinLockRelease(lock)
Definition: spin.h:64
#define HASH_BLOBS
Definition: hsearch.h:88
bool ExitOnAnyError
Definition: globals.c:104
static void UpdateSharedMemoryConfig(void)
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
sigset_t BlockSig
Definition: pqsignal.c:22
#define WL_POSTMASTER_DEATH
Definition: latch.h:128
void UpdateFullPageWrites(void)
Definition: xlog.c:9421
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
void * palloc0(Size size)
Definition: mcxt.c:920
bool FirstCallSinceLastCheckpoint(void)
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
#define WRITES_PER_ABSORB
Definition: checkpointer.c:140
static void chkpt_quickdie(SIGNAL_ARGS)
Definition: checkpointer.c:824
Size keysize
Definition: hsearch.h:72
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:907
void EmitErrorReport(void)
Definition: elog.c:1446
static pg_time_t last_xlog_switch_time
Definition: checkpointer.c:167
void pgstat_send_bgwriter(void)
Definition: pgstat.c:3668
#define SIGPIPE
Definition: win32.h:201
#define SIGHUP
Definition: win32.h:196
void CheckpointerMain(void)
Definition: checkpointer.c:193
#define SIG_DFL
Definition: win32.h:191
static bool ckpt_active
Definition: checkpointer.c:159
static pg_time_t ckpt_start_time
Definition: checkpointer.c:162
static volatile sig_atomic_t got_SIGHUP
Definition: checkpointer.c:152
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static XLogRecPtr ckpt_start_recptr
Definition: checkpointer.c:163
#define CHECKPOINT_WAIT
Definition: xlog.h:184
Latch * checkpointerLatch
Definition: proc.h:238
#define Max(x, y)
Definition: c.h:796
void SetLatch(volatile Latch *latch)
Definition: latch.c:379
#define SIGNAL_ARGS
Definition: c.h:1079
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:671
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
PgStat_Counter m_buf_written_backend
Definition: pgstat.h:418
static void ReqShutdownHandler(SIGNAL_ARGS)
Definition: checkpointer.c:886
size_t Size
Definition: c.h:353
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
void AbortBufferIO(void)
Definition: bufmgr.c:3957
static double elapsed_time(instr_time *starttime)
Definition: explain.c:751
sigjmp_buf * PG_exception_stack
Definition: elog.c:90
static volatile sig_atomic_t checkpoint_requested
Definition: checkpointer.c:153
#define SIGTTOU
Definition: win32.h:208
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
static double ckpt_cached_elapsed
Definition: checkpointer.c:164
static void CheckArchiveTimeout(void)
Definition: checkpointer.c:586
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
#define CHECKPOINT_CAUSE_TIME
Definition: xlog.h:187
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:179
int NBuffers
Definition: globals.c:122
struct Latch * MyLatch
Definition: globals.c:51
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:219
void LWLockReleaseAll(void)
Definition: lwlock.c:1813
void latch_sigusr1_handler(void)
Definition: latch.c:1540
void AbsorbFsyncRequests(void)
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1798
#define SIGCHLD
Definition: win32.h:206
static void chkpt_sigusr1_handler(SIGNAL_ARGS)
Definition: checkpointer.c:875
static pg_time_t last_checkpoint_time
Definition: checkpointer.c:166
#define WL_LATCH_SET
Definition: latch.h:124
#define SIGALRM
Definition: win32.h:202
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1533
#define SIGUSR2
Definition: win32.h:212
static volatile sig_atomic_t shutdown_requested
Definition: checkpointer.c:154
#define offsetof(type, field)
Definition: c.h:551
void RequestCheckpoint(int flags)
Definition: checkpointer.c:967
double CheckPointCompletionTarget
Definition: checkpointer.c:147
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416