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