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-2016, 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"
52 #include "storage/fd.h"
53 #include "storage/ipc.h"
54 #include "storage/lwlock.h"
55 #include "storage/proc.h"
56 #include "storage/shmem.h"
57 #include "storage/smgr.h"
58 #include "storage/spin.h"
59 #include "utils/guc.h"
60 #include "utils/memutils.h"
61 #include "utils/resowner.h"
62 
63 
64 /*----------
65  * Shared memory area for communication between checkpointer and backends
66  *
67  * The ckpt counters allow backends to watch for completion of a checkpoint
68  * request they send. Here's how it works:
69  * * At start of a checkpoint, checkpointer reads (and clears) the request
70  * flags and increments ckpt_started, while holding ckpt_lck.
71  * * On completion of a checkpoint, checkpointer sets ckpt_done to
72  * equal ckpt_started.
73  * * On failure of a checkpoint, checkpointer increments ckpt_failed
74  * and sets ckpt_done to equal ckpt_started.
75  *
76  * The algorithm for backends is:
77  * 1. Record current values of ckpt_failed and ckpt_started, and
78  * set request flags, while holding ckpt_lck.
79  * 2. Send signal to request checkpoint.
80  * 3. Sleep until ckpt_started changes. Now you know a checkpoint has
81  * begun since you started this algorithm (although *not* that it was
82  * specifically initiated by your signal), and that it is using your flags.
83  * 4. Record new value of ckpt_started.
84  * 5. Sleep until ckpt_done >= saved value of ckpt_started. (Use modulo
85  * arithmetic here in case counters wrap around.) Now you know a
86  * checkpoint has started and completed, but not whether it was
87  * successful.
88  * 6. If ckpt_failed is different from the originally saved value,
89  * assume request failed; otherwise it was definitely successful.
90  *
91  * ckpt_flags holds the OR of the checkpoint request flags sent by all
92  * requesting backends since the last checkpoint start. The flags are
93  * chosen so that OR'ing is the correct way to combine multiple requests.
94  *
95  * num_backend_writes is used to count the number of buffer writes performed
96  * by user backend processes. This counter should be wide enough that it
97  * can't overflow during a single processing cycle. num_backend_fsync
98  * counts the subset of those writes that also had to do their own fsync,
99  * because the checkpointer failed to absorb their request.
100  *
101  * The requests array holds fsync requests sent by backends and not yet
102  * absorbed by the checkpointer.
103  *
104  * Unlike the checkpoint fields, num_backend_writes, num_backend_fsync, and
105  * the requests fields are protected by CheckpointerCommLock.
106  *----------
107  */
108 typedef struct
109 {
112  BlockNumber segno; /* see md.c for special values */
113  /* might add a real request-type field later; not needed yet */
115 
116 typedef struct
117 {
118  pid_t checkpointer_pid; /* PID (0 if not started) */
119 
120  slock_t ckpt_lck; /* protects all the ckpt_* fields */
121 
122  int ckpt_started; /* advances when checkpoint starts */
123  int ckpt_done; /* advances when checkpoint done */
124  int ckpt_failed; /* advances when checkpoint fails */
125 
126  int ckpt_flags; /* checkpoint flags, as defined in xlog.h */
127 
128  uint32 num_backend_writes; /* counts user backend buffer writes */
129  uint32 num_backend_fsync; /* counts user backend fsync calls */
130 
131  int num_requests; /* current # of requests */
132  int max_requests; /* allocated array size */
133  CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER];
135 
137 
138 /* interval for calling AbsorbFsyncRequests in CheckpointWriteDelay */
139 #define WRITES_PER_ABSORB 1000
140 
141 /*
142  * GUC parameters
143  */
147 
148 /*
149  * Flags set by interrupt handlers for later service in the main loop.
150  */
151 static volatile sig_atomic_t got_SIGHUP = false;
152 static volatile sig_atomic_t checkpoint_requested = false;
153 static volatile sig_atomic_t shutdown_requested = false;
154 
155 /*
156  * Private state
157  */
158 static bool ckpt_active = false;
159 
160 /* these values are valid when ckpt_active is true: */
163 static double ckpt_cached_elapsed;
164 
167 
168 /* Prototypes for private functions */
169 
170 static void CheckArchiveTimeout(void);
171 static bool IsCheckpointOnSchedule(double progress);
172 static bool ImmediateCheckpointRequested(void);
173 static bool CompactCheckpointerRequestQueue(void);
174 static void UpdateSharedMemoryConfig(void);
175 
176 /* Signal handlers */
177 
178 static void chkpt_quickdie(SIGNAL_ARGS);
179 static void ChkptSigHupHandler(SIGNAL_ARGS);
180 static void ReqCheckpointHandler(SIGNAL_ARGS);
182 static void ReqShutdownHandler(SIGNAL_ARGS);
183 
184 
185 /*
186  * Main entry point for checkpointer process
187  *
188  * This is invoked from AuxiliaryProcessMain, which has already created the
189  * basic execution environment, but not enabled signals yet.
190  */
191 void
193 {
194  sigjmp_buf local_sigjmp_buf;
195  MemoryContext checkpointer_context;
196 
197  CheckpointerShmem->checkpointer_pid = MyProcPid;
198 
199  /*
200  * Properly accept or ignore signals the postmaster might send us
201  *
202  * Note: we deliberately ignore SIGTERM, because during a standard Unix
203  * system shutdown cycle, init will SIGTERM all processes at once. We
204  * want to wait for the backends to exit, whereupon the postmaster will
205  * tell us it's okay to shut down (via SIGUSR2).
206  */
207  pqsignal(SIGHUP, ChkptSigHupHandler); /* set flag to read config
208  * file */
209  pqsignal(SIGINT, ReqCheckpointHandler); /* request checkpoint */
210  pqsignal(SIGTERM, SIG_IGN); /* ignore SIGTERM */
211  pqsignal(SIGQUIT, chkpt_quickdie); /* hard crash time */
215  pqsignal(SIGUSR2, ReqShutdownHandler); /* request shutdown */
216 
217  /*
218  * Reset some signals that are accepted by postmaster but not here
219  */
225 
226  /* We allow SIGQUIT (quickdie) at all times */
227  sigdelset(&BlockSig, SIGQUIT);
228 
229  /*
230  * Initialize so that first time-driven event happens at the correct time.
231  */
233 
234  /*
235  * Create a resource owner to keep track of our resources (currently only
236  * buffer pins).
237  */
238  CurrentResourceOwner = ResourceOwnerCreate(NULL, "Checkpointer");
239 
240  /*
241  * Create a memory context that we will do all our work in. We do this so
242  * that we can reset the context during error recovery and thereby avoid
243  * possible memory leaks. Formerly this code just ran in
244  * TopMemoryContext, but resetting that would be a really bad idea.
245  */
246  checkpointer_context = AllocSetContextCreate(TopMemoryContext,
247  "Checkpointer",
251  MemoryContextSwitchTo(checkpointer_context);
252 
253  /*
254  * If an exception is encountered, processing resumes here.
255  *
256  * See notes in postgres.c about the design of this coding.
257  */
258  if (sigsetjmp(local_sigjmp_buf, 1) != 0)
259  {
260  /* Since not using PG_TRY, must reset error stack by hand */
262 
263  /* Prevent interrupts while cleaning up */
264  HOLD_INTERRUPTS();
265 
266  /* Report the error to the server log */
267  EmitErrorReport();
268 
269  /*
270  * These operations are really just a minimal subset of
271  * AbortTransaction(). We don't have very many resources to worry
272  * about in checkpointer, but we do have LWLocks, buffers, and temp
273  * files.
274  */
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 */ );
562 
563  /*
564  * Emergency bailout if postmaster has died. This is to avoid the
565  * necessity for manual cleanup of all postmaster children.
566  */
567  if (rc & WL_POSTMASTER_DEATH)
568  exit(1);
569  }
570 }
571 
572 /*
573  * CheckArchiveTimeout -- check for archive_timeout and switch xlog files
574  *
575  * This will switch to a new WAL file and force an archive file write
576  * if any activity is recorded in the current WAL file, including just
577  * a single checkpoint record.
578  */
579 static void
581 {
582  pg_time_t now;
583  pg_time_t last_time;
584 
586  return;
587 
588  now = (pg_time_t) time(NULL);
589 
590  /* First we do a quick check using possibly-stale local state. */
591  if ((int) (now - last_xlog_switch_time) < XLogArchiveTimeout)
592  return;
593 
594  /*
595  * Update local state ... note that last_xlog_switch_time is the last time
596  * a switch was performed *or requested*.
597  */
598  last_time = GetLastSegSwitchTime();
599 
601 
602  /* Now we can do the real check */
603  if ((int) (now - last_xlog_switch_time) >= XLogArchiveTimeout)
604  {
605  XLogRecPtr switchpoint;
606 
607  /* OK, it's time to switch */
608  switchpoint = RequestXLogSwitch();
609 
610  /*
611  * If the returned pointer points exactly to a segment boundary,
612  * assume nothing happened.
613  */
614  if ((switchpoint % XLogSegSize) != 0)
615  ereport(DEBUG1,
616  (errmsg("transaction log switch forced (archive_timeout=%d)",
618 
619  /*
620  * Update state in any case, so we don't retry constantly when the
621  * system is idle.
622  */
624  }
625 }
626 
627 /*
628  * Returns true if an immediate checkpoint request is pending. (Note that
629  * this does not check the *current* checkpoint's IMMEDIATE flag, but whether
630  * there is one pending behind it.)
631  */
632 static bool
634 {
636  {
638 
639  /*
640  * We don't need to acquire the ckpt_lck in this case because we're
641  * only looking at a single flag bit.
642  */
643  if (cps->ckpt_flags & CHECKPOINT_IMMEDIATE)
644  return true;
645  }
646  return false;
647 }
648 
649 /*
650  * CheckpointWriteDelay -- control rate of checkpoint
651  *
652  * This function is called after each page write performed by BufferSync().
653  * It is responsible for throttling BufferSync()'s write rate to hit
654  * checkpoint_completion_target.
655  *
656  * The checkpoint request flags should be passed in; currently the only one
657  * examined is CHECKPOINT_IMMEDIATE, which disables delays between writes.
658  *
659  * 'progress' is an estimate of how much of the work has been done, as a
660  * fraction between 0.0 meaning none, and 1.0 meaning all done.
661  */
662 void
663 CheckpointWriteDelay(int flags, double progress)
664 {
665  static int absorb_counter = WRITES_PER_ABSORB;
666 
667  /* Do nothing if checkpoint is being executed by non-checkpointer process */
668  if (!AmCheckpointerProcess())
669  return;
670 
671  /*
672  * Perform the usual duties and take a nap, unless we're behind schedule,
673  * in which case we just try to catch up as quickly as possible.
674  */
675  if (!(flags & CHECKPOINT_IMMEDIATE) &&
678  IsCheckpointOnSchedule(progress))
679  {
680  if (got_SIGHUP)
681  {
682  got_SIGHUP = false;
684  /* update shmem copies of config variables */
686  }
687 
689  absorb_counter = WRITES_PER_ABSORB;
690 
692 
693  /*
694  * Report interim activity statistics to the stats collector.
695  */
697 
698  /*
699  * This sleep used to be connected to bgwriter_delay, typically 200ms.
700  * That resulted in more frequent wakeups if not much work to do.
701  * Checkpointer and bgwriter are no longer related so take the Big
702  * Sleep.
703  */
704  pg_usleep(100000L);
705  }
706  else if (--absorb_counter <= 0)
707  {
708  /*
709  * Absorb pending fsync requests after each WRITES_PER_ABSORB write
710  * operations even when we don't sleep, to prevent overflow of the
711  * fsync request queue.
712  */
714  absorb_counter = WRITES_PER_ABSORB;
715  }
716 }
717 
718 /*
719  * IsCheckpointOnSchedule -- are we on schedule to finish this checkpoint
720  * (or restartpoint) in time?
721  *
722  * Compares the current progress against the time/segments elapsed since last
723  * checkpoint, and returns true if the progress we've made this far is greater
724  * than the elapsed time/segments.
725  */
726 static bool
728 {
729  XLogRecPtr recptr;
730  struct timeval now;
731  double elapsed_xlogs,
732  elapsed_time;
733 
735 
736  /* Scale progress according to checkpoint_completion_target. */
737  progress *= CheckPointCompletionTarget;
738 
739  /*
740  * Check against the cached value first. Only do the more expensive
741  * calculations once we reach the target previously calculated. Since
742  * neither time or WAL insert pointer moves backwards, a freshly
743  * calculated value can only be greater than or equal to the cached value.
744  */
745  if (progress < ckpt_cached_elapsed)
746  return false;
747 
748  /*
749  * Check progress against WAL segments written and CheckPointSegments.
750  *
751  * We compare the current WAL insert location against the location
752  * computed before calling CreateCheckPoint. The code in XLogInsert that
753  * actually triggers a checkpoint when CheckPointSegments is exceeded
754  * compares against RedoRecptr, so this is not completely accurate.
755  * However, it's good enough for our purposes, we're only calculating an
756  * estimate anyway.
757  *
758  * During recovery, we compare last replayed WAL record's location with
759  * the location computed before calling CreateRestartPoint. That maintains
760  * the same pacing as we have during checkpoints in normal operation, but
761  * we might exceed max_wal_size by a fair amount. That's because there can
762  * be a large gap between a checkpoint's redo-pointer and the checkpoint
763  * record itself, and we only start the restartpoint after we've seen the
764  * checkpoint record. (The gap is typically up to CheckPointSegments *
765  * checkpoint_completion_target where checkpoint_completion_target is the
766  * value that was in effect when the WAL was generated).
767  */
768  if (RecoveryInProgress())
769  recptr = GetXLogReplayRecPtr(NULL);
770  else
771  recptr = GetInsertRecPtr();
772  elapsed_xlogs = (((double) (recptr - ckpt_start_recptr)) / XLogSegSize) / CheckPointSegments;
773 
774  if (progress < elapsed_xlogs)
775  {
776  ckpt_cached_elapsed = elapsed_xlogs;
777  return false;
778  }
779 
780  /*
781  * Check progress against time elapsed and checkpoint_timeout.
782  */
783  gettimeofday(&now, NULL);
784  elapsed_time = ((double) ((pg_time_t) now.tv_sec - ckpt_start_time) +
785  now.tv_usec / 1000000.0) / CheckPointTimeout;
786 
787  if (progress < elapsed_time)
788  {
790  return false;
791  }
792 
793  /* It looks like we're on schedule. */
794  return true;
795 }
796 
797 
798 /* --------------------------------
799  * signal handler routines
800  * --------------------------------
801  */
802 
803 /*
804  * chkpt_quickdie() occurs when signalled SIGQUIT by the postmaster.
805  *
806  * Some backend has bought the farm,
807  * so we need to stop what we're doing and exit.
808  */
809 static void
811 {
813 
814  /*
815  * We DO NOT want to run proc_exit() callbacks -- we're here because
816  * shared memory may be corrupted, so we don't want to try to clean up our
817  * transaction. Just nail the windows shut and get out of town. Now that
818  * there's an atexit callback to prevent third-party code from breaking
819  * things by calling exit() directly, we have to reset the callbacks
820  * explicitly to make this work as intended.
821  */
822  on_exit_reset();
823 
824  /*
825  * Note we do exit(2) not exit(0). This is to force the postmaster into a
826  * system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
827  * backend. This is necessary precisely because we don't clean up our
828  * shared memory state. (The "dead man switch" mechanism in pmsignal.c
829  * should ensure the postmaster sees this as a crash, too, but no harm in
830  * being doubly sure.)
831  */
832  exit(2);
833 }
834 
835 /* SIGHUP: set flag to re-read config file at next convenient time */
836 static void
838 {
839  int save_errno = errno;
840 
841  got_SIGHUP = true;
842  SetLatch(MyLatch);
843 
844  errno = save_errno;
845 }
846 
847 /* SIGINT: set flag to run a normal checkpoint right away */
848 static void
850 {
851  int save_errno = errno;
852 
853  checkpoint_requested = true;
854  SetLatch(MyLatch);
855 
856  errno = save_errno;
857 }
858 
859 /* SIGUSR1: used for latch wakeups */
860 static void
862 {
863  int save_errno = errno;
864 
866 
867  errno = save_errno;
868 }
869 
870 /* SIGUSR2: set flag to run a shutdown checkpoint and exit */
871 static void
873 {
874  int save_errno = errno;
875 
876  shutdown_requested = true;
877  SetLatch(MyLatch);
878 
879  errno = save_errno;
880 }
881 
882 
883 /* --------------------------------
884  * communication with backends
885  * --------------------------------
886  */
887 
888 /*
889  * CheckpointerShmemSize
890  * Compute space needed for checkpointer-related shared memory
891  */
892 Size
894 {
895  Size size;
896 
897  /*
898  * Currently, the size of the requests[] array is arbitrarily set equal to
899  * NBuffers. This may prove too large or small ...
900  */
901  size = offsetof(CheckpointerShmemStruct, requests);
902  size = add_size(size, mul_size(NBuffers, sizeof(CheckpointerRequest)));
903 
904  return size;
905 }
906 
907 /*
908  * CheckpointerShmemInit
909  * Allocate and initialize checkpointer-related shared memory
910  */
911 void
913 {
914  Size size = CheckpointerShmemSize();
915  bool found;
916 
917  CheckpointerShmem = (CheckpointerShmemStruct *)
918  ShmemInitStruct("Checkpointer Data",
919  size,
920  &found);
921 
922  if (!found)
923  {
924  /*
925  * First time through, so initialize. Note that we zero the whole
926  * requests array; this is so that CompactCheckpointerRequestQueue can
927  * assume that any pad bytes in the request structs are zeroes.
928  */
929  MemSet(CheckpointerShmem, 0, size);
930  SpinLockInit(&CheckpointerShmem->ckpt_lck);
931  CheckpointerShmem->max_requests = NBuffers;
932  }
933 }
934 
935 /*
936  * RequestCheckpoint
937  * Called in backend processes to request a checkpoint
938  *
939  * flags is a bitwise OR of the following:
940  * CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown.
941  * CHECKPOINT_END_OF_RECOVERY: checkpoint is for end of WAL recovery.
942  * CHECKPOINT_IMMEDIATE: finish the checkpoint ASAP,
943  * ignoring checkpoint_completion_target parameter.
944  * CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occurred
945  * since the last one (implied by CHECKPOINT_IS_SHUTDOWN or
946  * CHECKPOINT_END_OF_RECOVERY).
947  * CHECKPOINT_WAIT: wait for completion before returning (otherwise,
948  * just signal checkpointer to do it, and return).
949  * CHECKPOINT_CAUSE_XLOG: checkpoint is requested due to xlog filling.
950  * (This affects logging, and in particular enables CheckPointWarning.)
951  */
952 void
954 {
955  int ntries;
956  int old_failed,
957  old_started;
958 
959  /*
960  * If in a standalone backend, just do it ourselves.
961  */
963  {
964  /*
965  * There's no point in doing slow checkpoints in a standalone backend,
966  * because there's no other backends the checkpoint could disrupt.
967  */
969 
970  /*
971  * After any checkpoint, close all smgr files. This is so we won't
972  * hang onto smgr references to deleted files indefinitely.
973  */
974  smgrcloseall();
975 
976  return;
977  }
978 
979  /*
980  * Atomically set the request flags, and take a snapshot of the counters.
981  * When we see ckpt_started > old_started, we know the flags we set here
982  * have been seen by checkpointer.
983  *
984  * Note that we OR the flags with any existing flags, to avoid overriding
985  * a "stronger" request by another backend. The flag senses must be
986  * chosen to make this work!
987  */
988  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
989 
990  old_failed = CheckpointerShmem->ckpt_failed;
991  old_started = CheckpointerShmem->ckpt_started;
992  CheckpointerShmem->ckpt_flags |= flags;
993 
994  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
995 
996  /*
997  * Send signal to request checkpoint. It's possible that the checkpointer
998  * hasn't started yet, or is in process of restarting, so we will retry a
999  * few times if needed. Also, if not told to wait for the checkpoint to
1000  * occur, we consider failure to send the signal to be nonfatal and merely
1001  * LOG it.
1002  */
1003  for (ntries = 0;; ntries++)
1004  {
1005  if (CheckpointerShmem->checkpointer_pid == 0)
1006  {
1007  if (ntries >= 20) /* max wait 2.0 sec */
1008  {
1009  elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
1010  "could not request checkpoint because checkpointer not running");
1011  break;
1012  }
1013  }
1014  else if (kill(CheckpointerShmem->checkpointer_pid, SIGINT) != 0)
1015  {
1016  if (ntries >= 20) /* max wait 2.0 sec */
1017  {
1018  elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
1019  "could not signal for checkpoint: %m");
1020  break;
1021  }
1022  }
1023  else
1024  break; /* signal sent successfully */
1025 
1027  pg_usleep(100000L); /* wait 0.1 sec, then retry */
1028  }
1029 
1030  /*
1031  * If requested, wait for completion. We detect completion according to
1032  * the algorithm given above.
1033  */
1034  if (flags & CHECKPOINT_WAIT)
1035  {
1036  int new_started,
1037  new_failed;
1038 
1039  /* Wait for a new checkpoint to start. */
1040  for (;;)
1041  {
1042  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
1043  new_started = CheckpointerShmem->ckpt_started;
1044  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
1045 
1046  if (new_started != old_started)
1047  break;
1048 
1050  pg_usleep(100000L);
1051  }
1052 
1053  /*
1054  * We are waiting for ckpt_done >= new_started, in a modulo sense.
1055  */
1056  for (;;)
1057  {
1058  int new_done;
1059 
1060  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
1061  new_done = CheckpointerShmem->ckpt_done;
1062  new_failed = CheckpointerShmem->ckpt_failed;
1063  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
1064 
1065  if (new_done - new_started >= 0)
1066  break;
1067 
1069  pg_usleep(100000L);
1070  }
1071 
1072  if (new_failed != old_failed)
1073  ereport(ERROR,
1074  (errmsg("checkpoint request failed"),
1075  errhint("Consult recent messages in the server log for details.")));
1076  }
1077 }
1078 
1079 /*
1080  * ForwardFsyncRequest
1081  * Forward a file-fsync request from a backend to the checkpointer
1082  *
1083  * Whenever a backend is compelled to write directly to a relation
1084  * (which should be seldom, if the background writer is getting its job done),
1085  * the backend calls this routine to pass over knowledge that the relation
1086  * is dirty and must be fsync'd before next checkpoint. We also use this
1087  * opportunity to count such writes for statistical purposes.
1088  *
1089  * This functionality is only supported for regular (not backend-local)
1090  * relations, so the rnode argument is intentionally RelFileNode not
1091  * RelFileNodeBackend.
1092  *
1093  * segno specifies which segment (not block!) of the relation needs to be
1094  * fsync'd. (Since the valid range is much less than BlockNumber, we can
1095  * use high values for special flags; that's all internal to md.c, which
1096  * see for details.)
1097  *
1098  * To avoid holding the lock for longer than necessary, we normally write
1099  * to the requests[] queue without checking for duplicates. The checkpointer
1100  * will have to eliminate dups internally anyway. However, if we discover
1101  * that the queue is full, we make a pass over the entire queue to compact
1102  * it. This is somewhat expensive, but the alternative is for the backend
1103  * to perform its own fsync, which is far more expensive in practice. It
1104  * is theoretically possible a backend fsync might still be necessary, if
1105  * the queue is full and contains no duplicate entries. In that case, we
1106  * let the backend know by returning false.
1107  */
1108 bool
1110 {
1111  CheckpointerRequest *request;
1112  bool too_full;
1113 
1114  if (!IsUnderPostmaster)
1115  return false; /* probably shouldn't even get here */
1116 
1117  if (AmCheckpointerProcess())
1118  elog(ERROR, "ForwardFsyncRequest must not be called in checkpointer");
1119 
1120  LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1121 
1122  /* Count all backend writes regardless of if they fit in the queue */
1124  CheckpointerShmem->num_backend_writes++;
1125 
1126  /*
1127  * If the checkpointer isn't running or the request queue is full, the
1128  * backend will have to perform its own fsync request. But before forcing
1129  * that to happen, we can try to compact the request queue.
1130  */
1131  if (CheckpointerShmem->checkpointer_pid == 0 ||
1132  (CheckpointerShmem->num_requests >= CheckpointerShmem->max_requests &&
1134  {
1135  /*
1136  * Count the subset of writes where backends have to do their own
1137  * fsync
1138  */
1140  CheckpointerShmem->num_backend_fsync++;
1141  LWLockRelease(CheckpointerCommLock);
1142  return false;
1143  }
1144 
1145  /* OK, insert request */
1146  request = &CheckpointerShmem->requests[CheckpointerShmem->num_requests++];
1147  request->rnode = rnode;
1148  request->forknum = forknum;
1149  request->segno = segno;
1150 
1151  /* If queue is more than half full, nudge the checkpointer to empty it */
1152  too_full = (CheckpointerShmem->num_requests >=
1153  CheckpointerShmem->max_requests / 2);
1154 
1155  LWLockRelease(CheckpointerCommLock);
1156 
1157  /* ... but not till after we release the lock */
1158  if (too_full && ProcGlobal->checkpointerLatch)
1160 
1161  return true;
1162 }
1163 
1164 /*
1165  * CompactCheckpointerRequestQueue
1166  * Remove duplicates from the request queue to avoid backend fsyncs.
1167  * Returns "true" if any entries were removed.
1168  *
1169  * Although a full fsync request queue is not common, it can lead to severe
1170  * performance problems when it does happen. So far, this situation has
1171  * only been observed to occur when the system is under heavy write load,
1172  * and especially during the "sync" phase of a checkpoint. Without this
1173  * logic, each backend begins doing an fsync for every block written, which
1174  * gets very expensive and can slow down the whole system.
1175  *
1176  * Trying to do this every time the queue is full could lose if there
1177  * aren't any removable entries. But that should be vanishingly rare in
1178  * practice: there's one queue entry per shared buffer.
1179  */
1180 static bool
1182 {
1183  struct CheckpointerSlotMapping
1184  {
1185  CheckpointerRequest request;
1186  int slot;
1187  };
1188 
1189  int n,
1190  preserve_count;
1191  int num_skipped = 0;
1192  HASHCTL ctl;
1193  HTAB *htab;
1194  bool *skip_slot;
1195 
1196  /* must hold CheckpointerCommLock in exclusive mode */
1197  Assert(LWLockHeldByMe(CheckpointerCommLock));
1198 
1199  /* Initialize skip_slot array */
1200  skip_slot = palloc0(sizeof(bool) * CheckpointerShmem->num_requests);
1201 
1202  /* Initialize temporary hash table */
1203  MemSet(&ctl, 0, sizeof(ctl));
1204  ctl.keysize = sizeof(CheckpointerRequest);
1205  ctl.entrysize = sizeof(struct CheckpointerSlotMapping);
1206  ctl.hcxt = CurrentMemoryContext;
1207 
1208  htab = hash_create("CompactCheckpointerRequestQueue",
1209  CheckpointerShmem->num_requests,
1210  &ctl,
1212 
1213  /*
1214  * The basic idea here is that a request can be skipped if it's followed
1215  * by a later, identical request. It might seem more sensible to work
1216  * backwards from the end of the queue and check whether a request is
1217  * *preceded* by an earlier, identical request, in the hopes of doing less
1218  * copying. But that might change the semantics, if there's an
1219  * intervening FORGET_RELATION_FSYNC or FORGET_DATABASE_FSYNC request, so
1220  * we do it this way. It would be possible to be even smarter if we made
1221  * the code below understand the specific semantics of such requests (it
1222  * could blow away preceding entries that would end up being canceled
1223  * anyhow), but it's not clear that the extra complexity would buy us
1224  * anything.
1225  */
1226  for (n = 0; n < CheckpointerShmem->num_requests; n++)
1227  {
1228  CheckpointerRequest *request;
1229  struct CheckpointerSlotMapping *slotmap;
1230  bool found;
1231 
1232  /*
1233  * We use the request struct directly as a hashtable key. This
1234  * assumes that any padding bytes in the structs are consistently the
1235  * same, which should be okay because we zeroed them in
1236  * CheckpointerShmemInit. Note also that RelFileNode had better
1237  * contain no pad bytes.
1238  */
1239  request = &CheckpointerShmem->requests[n];
1240  slotmap = hash_search(htab, request, HASH_ENTER, &found);
1241  if (found)
1242  {
1243  /* Duplicate, so mark the previous occurrence as skippable */
1244  skip_slot[slotmap->slot] = true;
1245  num_skipped++;
1246  }
1247  /* Remember slot containing latest occurrence of this request value */
1248  slotmap->slot = n;
1249  }
1250 
1251  /* Done with the hash table. */
1252  hash_destroy(htab);
1253 
1254  /* If no duplicates, we're out of luck. */
1255  if (!num_skipped)
1256  {
1257  pfree(skip_slot);
1258  return false;
1259  }
1260 
1261  /* We found some duplicates; remove them. */
1262  preserve_count = 0;
1263  for (n = 0; n < CheckpointerShmem->num_requests; n++)
1264  {
1265  if (skip_slot[n])
1266  continue;
1267  CheckpointerShmem->requests[preserve_count++] = CheckpointerShmem->requests[n];
1268  }
1269  ereport(DEBUG1,
1270  (errmsg("compacted fsync request queue from %d entries to %d entries",
1271  CheckpointerShmem->num_requests, preserve_count)));
1272  CheckpointerShmem->num_requests = preserve_count;
1273 
1274  /* Cleanup. */
1275  pfree(skip_slot);
1276  return true;
1277 }
1278 
1279 /*
1280  * AbsorbFsyncRequests
1281  * Retrieve queued fsync requests and pass them to local smgr.
1282  *
1283  * This is exported because it must be called during CreateCheckPoint;
1284  * we have to be sure we have accepted all pending requests just before
1285  * we start fsync'ing. Since CreateCheckPoint sometimes runs in
1286  * non-checkpointer processes, do nothing if not checkpointer.
1287  */
1288 void
1290 {
1291  CheckpointerRequest *requests = NULL;
1292  CheckpointerRequest *request;
1293  int n;
1294 
1295  if (!AmCheckpointerProcess())
1296  return;
1297 
1298  LWLockAcquire(CheckpointerCommLock, LW_EXCLUSIVE);
1299 
1300  /* Transfer stats counts into pending pgstats message */
1302  BgWriterStats.m_buf_fsync_backend += CheckpointerShmem->num_backend_fsync;
1303 
1304  CheckpointerShmem->num_backend_writes = 0;
1305  CheckpointerShmem->num_backend_fsync = 0;
1306 
1307  /*
1308  * We try to avoid holding the lock for a long time by copying the request
1309  * array, and processing the requests after releasing the lock.
1310  *
1311  * Once we have cleared the requests from shared memory, we have to PANIC
1312  * if we then fail to absorb them (eg, because our hashtable runs out of
1313  * memory). This is because the system cannot run safely if we are unable
1314  * to fsync what we have been told to fsync. Fortunately, the hashtable
1315  * is so small that the problem is quite unlikely to arise in practice.
1316  */
1317  n = CheckpointerShmem->num_requests;
1318  if (n > 0)
1319  {
1320  requests = (CheckpointerRequest *) palloc(n * sizeof(CheckpointerRequest));
1321  memcpy(requests, CheckpointerShmem->requests, n * sizeof(CheckpointerRequest));
1322  }
1323 
1325 
1326  CheckpointerShmem->num_requests = 0;
1327 
1328  LWLockRelease(CheckpointerCommLock);
1329 
1330  for (request = requests; n > 0; request++, n--)
1331  RememberFsyncRequest(request->rnode, request->forknum, request->segno);
1332 
1333  END_CRIT_SECTION();
1334 
1335  if (requests)
1336  pfree(requests);
1337 }
1338 
1339 /*
1340  * Update any shared memory configurations based on config parameters
1341  */
1342 static void
1344 {
1345  /* update global shmem state for sync rep */
1347 
1348  /*
1349  * If full_page_writes has been changed by SIGHUP, we update it in shared
1350  * memory and write an XLOG_FPW_CHANGE record.
1351  */
1353 
1354  elog(DEBUG2, "checkpointer updated shared memory configuration values");
1355 }
1356 
1357 /*
1358  * FirstCallSinceLastCheckpoint allows a process to take an action once
1359  * per checkpoint cycle by asynchronously checking for checkpoint completion.
1360  */
1361 bool
1363 {
1364  static int ckpt_done = 0;
1365  int new_done;
1366  bool FirstCall = false;
1367 
1368  SpinLockAcquire(&CheckpointerShmem->ckpt_lck);
1369  new_done = CheckpointerShmem->ckpt_done;
1370  SpinLockRelease(&CheckpointerShmem->ckpt_lck);
1371 
1372  if (new_done != ckpt_done)
1373  FirstCall = true;
1374 
1375  ckpt_done = new_done;
1376 
1377  return FirstCall;
1378 }
void RememberFsyncRequest(RelFileNode rnode, ForkNumber forknum, BlockNumber segno)
Definition: md.c:1514
int slock_t
Definition: s_lock.h:911
PgStat_Counter m_buf_fsync_backend
Definition: pgstat.h:405
void SyncRepUpdateSyncStandbysDefined(void)
Definition: syncrep.c:830
#define XLogSegSize
Definition: xlog_internal.h:92
bool IsPostmasterEnvironment
Definition: globals.c:97
void CheckpointWriteDelay(int flags, double progress)
Definition: checkpointer.c:663
#define SIGUSR1
Definition: win32.h:211
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:795
static bool IsCheckpointOnSchedule(double progress)
Definition: checkpointer.c:727
int gettimeofday(struct timeval *tp, struct timezone *tzp)
Definition: gettimeofday.c:105
#define DEBUG1
Definition: elog.h:25
XLogRecPtr GetInsertRecPtr(void)
Definition: xlog.c:7878
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
#define HASH_CONTEXT
Definition: hsearch.h:93
#define HASH_ELEM
Definition: hsearch.h:87
#define WL_TIMEOUT
Definition: latch.h:111
void ProcessConfigFile(GucContext context)
static void ReqCheckpointHandler(SIGNAL_ARGS)
Definition: checkpointer.c:849
int XLogArchiveTimeout
Definition: xlog.c:90
MemoryContext hcxt
Definition: hsearch.h:78
bool LWLockHeldByMe(LWLock *l)
Definition: lwlock.c:1892
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:8154
PGPROC * MyProc
Definition: proc.c:65
PgStat_Counter m_timed_checkpoints
Definition: pgstat.h:399
#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:798
#define END_CRIT_SECTION()
Definition: miscadmin.h:132
bool CreateRestartPoint(int flags)
Definition: xlog.c:8700
#define SIGTTIN
Definition: win32.h:207
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void CheckpointerShmemInit(void)
Definition: checkpointer.c:912
PgStat_MsgBgWriter BgWriterStats
Definition: pgstat.c:125
Size entrysize
Definition: hsearch.h:73
int CheckPointWarning
Definition: checkpointer.c:145
CheckpointerRequest requests[FLEXIBLE_ARRAY_MEMBER]
Definition: checkpointer.c:133
#define START_CRIT_SECTION()
Definition: miscadmin.h:130
void proc_exit(int code)
Definition: ipc.c:99
PROC_HDR * ProcGlobal
Definition: proc.c:78
#define MemSet(start, val, len)
Definition: c.h:849
uint32 BlockNumber
Definition: block.h:31
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:887
void ResetLatch(volatile Latch *latch)
Definition: latch.c:459
#define LOG
Definition: elog.h:26
bool RecoveryInProgress(void)
Definition: xlog.c:7547
#define SIGQUIT
Definition: win32.h:197
void FlushErrorState(void)
Definition: elog.c:1587
#define PG_SETMASK(mask)
Definition: pqsignal.h:19
#define ALLOCSET_DEFAULT_MINSIZE
Definition: memutils.h:142
Latch procLatch
Definition: proc.h:92
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1774
void smgrcloseall(void)
Definition: smgr.c:326
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:116
ErrorContextCallback * error_context_stack
Definition: elog.c:89
#define CHECKPOINT_CAUSE_XLOG
Definition: xlog.h:183
PgStat_Counter m_requested_checkpoints
Definition: pgstat.h:400
#define SpinLockAcquire(lock)
Definition: spin.h:62
static bool ImmediateCheckpointRequested(void)
Definition: checkpointer.c:633
void pg_usleep(long microsec)
Definition: signal.c:53
Definition: dynahash.c:193
void AtEOXact_SMgr(void)
Definition: smgr.c:798
void pfree(void *pointer)
Definition: mcxt.c:995
#define SIG_IGN
Definition: win32.h:193
#define AmBackgroundWriterProcess()
Definition: miscadmin.h:403
#define ERROR
Definition: elog.h:43
pg_time_t GetLastSegSwitchTime(void)
Definition: xlog.c:7907
void on_exit_reset(void)
Definition: ipc.c:396
#define AmCheckpointerProcess()
Definition: miscadmin.h:404
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition: shmem.c:334
void AtEOXact_Files(void)
Definition: fd.c:2489
static bool CompactCheckpointerRequestQueue(void)
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:10616
#define DEBUG2
Definition: elog.h:24
static void ChkptSigHupHandler(SIGNAL_ARGS)
Definition: checkpointer.c:837
bool IsUnderPostmaster
Definition: globals.c:98
int CheckPointTimeout
Definition: checkpointer.c:144
#define CHECKPOINT_END_OF_RECOVERY
Definition: xlog.h:173
unsigned int uint32
Definition: c.h:265
sigset_t UnBlockSig
Definition: pqsignal.c:22
static void pgstat_report_wait_end(void)
Definition: pgstat.h:1035
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:119
static CheckpointerShmemStruct * CheckpointerShmem
Definition: checkpointer.c:136
void ShutdownXLOG(int code, Datum arg)
Definition: xlog.c:7958
void UnlockBuffers(void)
Definition: bufmgr.c:3501
int progress
Definition: pgbench.c:171
#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:102
static void UpdateSharedMemoryConfig(void)
Size mul_size(Size s1, Size s2)
Definition: shmem.c:460
sigset_t BlockSig
Definition: pqsignal.c:22
#define WL_POSTMASTER_DEATH
Definition: latch.h:112
void UpdateFullPageWrites(void)
Definition: xlog.c:9104
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:436
void * palloc0(Size size)
Definition: mcxt.c:923
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:443
#define WRITES_PER_ABSORB
Definition: checkpointer.c:139
static void chkpt_quickdie(SIGNAL_ARGS)
Definition: checkpointer.c:810
Size keysize
Definition: hsearch.h:72
Size CheckpointerShmemSize(void)
Definition: checkpointer.c:893
void EmitErrorReport(void)
Definition: elog.c:1446
static pg_time_t last_xlog_switch_time
Definition: checkpointer.c:166
void pgstat_send_bgwriter(void)
Definition: pgstat.c:3432
#define SIGPIPE
Definition: win32.h:201
#define SIGHUP
Definition: win32.h:196
void CheckpointerMain(void)
Definition: checkpointer.c:192
#define SIG_DFL
Definition: win32.h:191
static bool ckpt_active
Definition: checkpointer.c:158
static pg_time_t ckpt_start_time
Definition: checkpointer.c:161
static volatile sig_atomic_t got_SIGHUP
Definition: checkpointer.c:151
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
static XLogRecPtr ckpt_start_recptr
Definition: checkpointer.c:162
#define CHECKPOINT_WAIT
Definition: xlog.h:181
Latch * checkpointerLatch
Definition: proc.h:231
#define Max(x, y)
Definition: c.h:792
void SetLatch(volatile Latch *latch)
Definition: latch.c:377
#define SIGNAL_ARGS
Definition: c.h:1059
#define NULL
Definition: c.h:226
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:667
void ResourceOwnerRelease(ResourceOwner owner, ResourceReleasePhase phase, bool isCommit, bool isTopLevel)
Definition: resowner.c:471
PgStat_Counter m_buf_written_backend
Definition: pgstat.h:404
static void ReqShutdownHandler(SIGNAL_ARGS)
Definition: checkpointer.c:872
size_t Size
Definition: c.h:352
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1167
void AbortBufferIO(void)
Definition: bufmgr.c:3913
static double elapsed_time(instr_time *starttime)
Definition: explain.c:720
sigjmp_buf * PG_exception_stack
Definition: elog.c:91
static volatile sig_atomic_t checkpoint_requested
Definition: checkpointer.c:152
#define SIGTTOU
Definition: win32.h:208
int WaitLatch(volatile Latch *latch, int wakeEvents, long timeout)
Definition: latch.c:300
void * palloc(Size size)
Definition: mcxt.c:894
int errmsg(const char *fmt,...)
Definition: elog.c:797
static double ckpt_cached_elapsed
Definition: checkpointer.c:163
static void CheckArchiveTimeout(void)
Definition: checkpointer.c:580
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
#define ALLOCSET_DEFAULT_INITSIZE
Definition: memutils.h:143
#define CHECKPOINT_CAUSE_TIME
Definition: xlog.h:184
#define CHECKPOINT_IMMEDIATE
Definition: xlog.h:176
int NBuffers
Definition: globals.c:120
struct Latch * MyLatch
Definition: globals.c:51
#define ALLOCSET_DEFAULT_MAXSIZE
Definition: memutils.h:144
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:218
void LWLockReleaseAll(void)
Definition: lwlock.c:1874
void latch_sigusr1_handler(void)
Definition: latch.c:1500
void AbsorbFsyncRequests(void)
void AtEOXact_HashTables(bool isCommit)
Definition: dynahash.c:1800
#define SIGCHLD
Definition: win32.h:206
static void chkpt_sigusr1_handler(SIGNAL_ARGS)
Definition: checkpointer.c:861
static pg_time_t last_checkpoint_time
Definition: checkpointer.c:165
XLogRecPtr RequestXLogSwitch(void)
Definition: xlog.c:9008
#define WL_LATCH_SET
Definition: latch.h:108
#define SIGALRM
Definition: win32.h:202
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1590
#define SIGUSR2
Definition: win32.h:212
static volatile sig_atomic_t shutdown_requested
Definition: checkpointer.c:153
#define offsetof(type, field)
Definition: c.h:547
void RequestCheckpoint(int flags)
Definition: checkpointer.c:953
double CheckPointCompletionTarget
Definition: checkpointer.c:146
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:416