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