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