Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * walsummarizer.c
4 : *
5 : * Background process to perform WAL summarization, if it is enabled.
6 : * It continuously scans the write-ahead log and periodically emits a
7 : * summary file which indicates which blocks in which relation forks
8 : * were modified by WAL records in the LSN range covered by the summary
9 : * file. See walsummary.c and blkreftable.c for more details on the
10 : * naming and contents of WAL summary files.
11 : *
12 : * If configured to do, this background process will also remove WAL
13 : * summary files when the file timestamp is older than a configurable
14 : * threshold (but only if the WAL has been removed first).
15 : *
16 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
17 : *
18 : * IDENTIFICATION
19 : * src/backend/postmaster/walsummarizer.c
20 : *
21 : *-------------------------------------------------------------------------
22 : */
23 : #include "postgres.h"
24 :
25 : #include "access/timeline.h"
26 : #include "access/xlog.h"
27 : #include "access/xlog_internal.h"
28 : #include "access/xlogrecovery.h"
29 : #include "access/xlogutils.h"
30 : #include "backup/walsummary.h"
31 : #include "catalog/storage_xlog.h"
32 : #include "commands/dbcommands_xlog.h"
33 : #include "common/blkreftable.h"
34 : #include "libpq/pqsignal.h"
35 : #include "miscadmin.h"
36 : #include "pgstat.h"
37 : #include "postmaster/auxprocess.h"
38 : #include "postmaster/interrupt.h"
39 : #include "postmaster/walsummarizer.h"
40 : #include "replication/walreceiver.h"
41 : #include "storage/aio_subsys.h"
42 : #include "storage/fd.h"
43 : #include "storage/ipc.h"
44 : #include "storage/latch.h"
45 : #include "storage/lwlock.h"
46 : #include "storage/proc.h"
47 : #include "storage/procsignal.h"
48 : #include "storage/shmem.h"
49 : #include "utils/guc.h"
50 : #include "utils/memutils.h"
51 : #include "utils/wait_event.h"
52 :
53 : /*
54 : * Data in shared memory related to WAL summarization.
55 : */
56 : typedef struct
57 : {
58 : /*
59 : * These fields are protected by WALSummarizerLock.
60 : *
61 : * Until we've discovered what summary files already exist on disk and
62 : * stored that information in shared memory, initialized is false and the
63 : * other fields here contain no meaningful information. After that has
64 : * been done, initialized is true.
65 : *
66 : * summarized_tli and summarized_lsn indicate the last LSN and TLI at
67 : * which the next summary file will start. Normally, these are the LSN and
68 : * TLI at which the last file ended; in such case, lsn_is_exact is true.
69 : * If, however, the LSN is just an approximation, then lsn_is_exact is
70 : * false. This can happen if, for example, there are no existing WAL
71 : * summary files at startup. In that case, we have to derive the position
72 : * at which to start summarizing from the WAL files that exist on disk,
73 : * and so the LSN might point to the start of the next file even though
74 : * that might happen to be in the middle of a WAL record.
75 : *
76 : * summarizer_pgprocno is the proc number of the summarizer process, if
77 : * one is running, or else INVALID_PROC_NUMBER.
78 : *
79 : * pending_lsn is used by the summarizer to advertise the ending LSN of a
80 : * record it has recently read. It shouldn't ever be less than
81 : * summarized_lsn, but might be greater, because the summarizer buffers
82 : * data for a range of LSNs in memory before writing out a new file.
83 : */
84 : bool initialized;
85 : TimeLineID summarized_tli;
86 : XLogRecPtr summarized_lsn;
87 : bool lsn_is_exact;
88 : ProcNumber summarizer_pgprocno;
89 : XLogRecPtr pending_lsn;
90 :
91 : /*
92 : * This field handles its own synchronization.
93 : */
94 : ConditionVariable summary_file_cv;
95 : } WalSummarizerData;
96 :
97 : /*
98 : * Private data for our xlogreader's page read callback.
99 : */
100 : typedef struct
101 : {
102 : TimeLineID tli;
103 : bool historic;
104 : XLogRecPtr read_upto;
105 : bool end_of_wal;
106 : } SummarizerReadLocalXLogPrivate;
107 :
108 : /* Pointer to shared memory state. */
109 : static WalSummarizerData *WalSummarizerCtl;
110 :
111 : /*
112 : * When we reach end of WAL and need to read more, we sleep for a number of
113 : * milliseconds that is an integer multiple of MS_PER_SLEEP_QUANTUM. This is
114 : * the multiplier. It should vary between 1 and MAX_SLEEP_QUANTA, depending
115 : * on system activity. See summarizer_wait_for_wal() for how we adjust this.
116 : */
117 : static long sleep_quanta = 1;
118 :
119 : /*
120 : * The sleep time will always be a multiple of 200ms and will not exceed
121 : * thirty seconds (150 * 200 = 30 * 1000). Note that the timeout here needs
122 : * to be substantially less than the maximum amount of time for which an
123 : * incremental backup will wait for this process to catch up. Otherwise, an
124 : * incremental backup might time out on an idle system just because we sleep
125 : * for too long.
126 : */
127 : #define MAX_SLEEP_QUANTA 150
128 : #define MS_PER_SLEEP_QUANTUM 200
129 :
130 : /*
131 : * This is a count of the number of pages of WAL that we've read since the
132 : * last time we waited for more WAL to appear.
133 : */
134 : static long pages_read_since_last_sleep = 0;
135 :
136 : /*
137 : * Most recent RedoRecPtr value observed by MaybeRemoveOldWalSummaries.
138 : */
139 : static XLogRecPtr redo_pointer_at_last_summary_removal = InvalidXLogRecPtr;
140 :
141 : /*
142 : * GUC parameters
143 : */
144 : bool summarize_wal = false;
145 : int wal_summary_keep_time = 10 * HOURS_PER_DAY * MINS_PER_HOUR;
146 :
147 : static void WalSummarizerShutdown(int code, Datum arg);
148 : static XLogRecPtr GetLatestLSN(TimeLineID *tli);
149 : static void ProcessWalSummarizerInterrupts(void);
150 : static XLogRecPtr SummarizeWAL(TimeLineID tli, XLogRecPtr start_lsn,
151 : bool exact, XLogRecPtr switch_lsn,
152 : XLogRecPtr maximum_lsn);
153 : static void SummarizeDbaseRecord(XLogReaderState *xlogreader,
154 : BlockRefTable *brtab);
155 : static void SummarizeSmgrRecord(XLogReaderState *xlogreader,
156 : BlockRefTable *brtab);
157 : static void SummarizeXactRecord(XLogReaderState *xlogreader,
158 : BlockRefTable *brtab);
159 : static bool SummarizeXlogRecord(XLogReaderState *xlogreader,
160 : bool *new_fast_forward);
161 : static int summarizer_read_local_xlog_page(XLogReaderState *state,
162 : XLogRecPtr targetPagePtr,
163 : int reqLen,
164 : XLogRecPtr targetRecPtr,
165 : char *cur_page);
166 : static void summarizer_wait_for_wal(void);
167 : static void MaybeRemoveOldWalSummaries(void);
168 :
169 : /*
170 : * Amount of shared memory required for this module.
171 : */
172 : Size
173 3267 : WalSummarizerShmemSize(void)
174 : {
175 3267 : return sizeof(WalSummarizerData);
176 : }
177 :
178 : /*
179 : * Create or attach to shared memory segment for this module.
180 : */
181 : void
182 1140 : WalSummarizerShmemInit(void)
183 : {
184 : bool found;
185 :
186 1140 : WalSummarizerCtl = (WalSummarizerData *)
187 1140 : ShmemInitStruct("Wal Summarizer Ctl", WalSummarizerShmemSize(),
188 : &found);
189 :
190 1140 : if (!found)
191 : {
192 : /*
193 : * First time through, so initialize.
194 : *
195 : * We're just filling in dummy values here -- the real initialization
196 : * will happen when GetOldestUnsummarizedLSN() is called for the first
197 : * time.
198 : */
199 1140 : WalSummarizerCtl->initialized = false;
200 1140 : WalSummarizerCtl->summarized_tli = 0;
201 1140 : WalSummarizerCtl->summarized_lsn = InvalidXLogRecPtr;
202 1140 : WalSummarizerCtl->lsn_is_exact = false;
203 1140 : WalSummarizerCtl->summarizer_pgprocno = INVALID_PROC_NUMBER;
204 1140 : WalSummarizerCtl->pending_lsn = InvalidXLogRecPtr;
205 1140 : ConditionVariableInit(&WalSummarizerCtl->summary_file_cv);
206 : }
207 1140 : }
208 :
209 : /*
210 : * Entry point for walsummarizer process.
211 : */
212 : void
213 3 : WalSummarizerMain(const void *startup_data, size_t startup_data_len)
214 : {
215 : sigjmp_buf local_sigjmp_buf;
216 : MemoryContext context;
217 :
218 : /*
219 : * Within this function, 'current_lsn' and 'current_tli' refer to the
220 : * point from which the next WAL summary file should start. 'exact' is
221 : * true if 'current_lsn' is known to be the start of a WAL record or WAL
222 : * segment, and false if it might be in the middle of a record someplace.
223 : *
224 : * 'switch_lsn' and 'switch_tli', if set, are the LSN at which we need to
225 : * switch to a new timeline and the timeline to which we need to switch.
226 : * If not set, we either haven't figured out the answers yet or we're
227 : * already on the latest timeline.
228 : */
229 : XLogRecPtr current_lsn;
230 : TimeLineID current_tli;
231 : bool exact;
232 3 : XLogRecPtr switch_lsn = InvalidXLogRecPtr;
233 3 : TimeLineID switch_tli = 0;
234 :
235 : Assert(startup_data_len == 0);
236 :
237 3 : AuxiliaryProcessMainCommon();
238 :
239 3 : ereport(DEBUG1,
240 : (errmsg_internal("WAL summarizer started")));
241 :
242 : /*
243 : * Properly accept or ignore signals the postmaster might send us
244 : */
245 3 : pqsignal(SIGHUP, SignalHandlerForConfigReload);
246 3 : pqsignal(SIGINT, SIG_IGN); /* no query to cancel */
247 3 : pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
248 : /* SIGQUIT handler was already set up by InitPostmasterChild */
249 3 : pqsignal(SIGALRM, SIG_IGN);
250 3 : pqsignal(SIGPIPE, SIG_IGN);
251 3 : pqsignal(SIGUSR1, procsignal_sigusr1_handler);
252 3 : pqsignal(SIGUSR2, SIG_IGN); /* not used */
253 :
254 : /* Advertise ourselves. */
255 3 : on_shmem_exit(WalSummarizerShutdown, (Datum) 0);
256 3 : LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
257 3 : WalSummarizerCtl->summarizer_pgprocno = MyProcNumber;
258 3 : LWLockRelease(WALSummarizerLock);
259 :
260 : /* Create and switch to a memory context that we can reset on error. */
261 3 : context = AllocSetContextCreate(TopMemoryContext,
262 : "Wal Summarizer",
263 : ALLOCSET_DEFAULT_SIZES);
264 3 : MemoryContextSwitchTo(context);
265 :
266 : /*
267 : * Reset some signals that are accepted by postmaster but not here
268 : */
269 3 : pqsignal(SIGCHLD, SIG_DFL);
270 :
271 : /*
272 : * If an exception is encountered, processing resumes here.
273 : */
274 3 : if (sigsetjmp(local_sigjmp_buf, 1) != 0)
275 : {
276 : /* Since not using PG_TRY, must reset error stack by hand */
277 0 : error_context_stack = NULL;
278 :
279 : /* Prevent interrupts while cleaning up */
280 0 : HOLD_INTERRUPTS();
281 :
282 : /* Report the error to the server log */
283 0 : EmitErrorReport();
284 :
285 : /* Release resources we might have acquired. */
286 0 : LWLockReleaseAll();
287 0 : ConditionVariableCancelSleep();
288 0 : pgstat_report_wait_end();
289 0 : pgaio_error_cleanup();
290 0 : ReleaseAuxProcessResources(false);
291 0 : AtEOXact_Files(false);
292 0 : AtEOXact_HashTables(false);
293 :
294 : /*
295 : * Now return to normal top-level context and clear ErrorContext for
296 : * next time.
297 : */
298 0 : MemoryContextSwitchTo(context);
299 0 : FlushErrorState();
300 :
301 : /* Flush any leaked data in the top-level context */
302 0 : MemoryContextReset(context);
303 :
304 : /* Now we can allow interrupts again */
305 0 : RESUME_INTERRUPTS();
306 :
307 : /*
308 : * Sleep for 10 seconds before attempting to resume operations in
309 : * order to avoid excessive logging.
310 : *
311 : * Many of the likely error conditions are things that will repeat
312 : * every time. For example, if the WAL can't be read or the summary
313 : * can't be written, only administrator action will cure the problem.
314 : * So a really fast retry time doesn't seem to be especially
315 : * beneficial, and it will clutter the logs.
316 : */
317 0 : (void) WaitLatch(NULL,
318 : WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
319 : 10000,
320 : WAIT_EVENT_WAL_SUMMARIZER_ERROR);
321 : }
322 :
323 : /* We can now handle ereport(ERROR) */
324 3 : PG_exception_stack = &local_sigjmp_buf;
325 :
326 : /*
327 : * Unblock signals (they were blocked when the postmaster forked us)
328 : */
329 3 : sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
330 :
331 : /*
332 : * Fetch information about previous progress from shared memory, and ask
333 : * GetOldestUnsummarizedLSN to reset pending_lsn to summarized_lsn. We
334 : * might be recovering from an error, and if so, pending_lsn might have
335 : * advanced past summarized_lsn, but any WAL we read previously has been
336 : * lost and will need to be reread.
337 : *
338 : * If we discover that WAL summarization is not enabled, just exit.
339 : */
340 3 : current_lsn = GetOldestUnsummarizedLSN(¤t_tli, &exact);
341 3 : if (!XLogRecPtrIsValid(current_lsn))
342 0 : proc_exit(0);
343 :
344 : /*
345 : * Loop forever
346 : */
347 : for (;;)
348 18 : {
349 : XLogRecPtr latest_lsn;
350 : TimeLineID latest_tli;
351 : XLogRecPtr end_of_summary_lsn;
352 :
353 : /* Flush any leaked data in the top-level context */
354 21 : MemoryContextReset(context);
355 :
356 : /* Process any signals received recently. */
357 21 : ProcessWalSummarizerInterrupts();
358 :
359 : /* If it's time to remove any old WAL summaries, do that now. */
360 21 : MaybeRemoveOldWalSummaries();
361 :
362 : /* Find the LSN and TLI up to which we can safely summarize. */
363 21 : latest_lsn = GetLatestLSN(&latest_tli);
364 :
365 : /*
366 : * If we're summarizing a historic timeline and we haven't yet
367 : * computed the point at which to switch to the next timeline, do that
368 : * now.
369 : *
370 : * Note that if this is a standby, what was previously the current
371 : * timeline could become historic at any time.
372 : *
373 : * We could try to make this more efficient by caching the results of
374 : * readTimeLineHistory when latest_tli has not changed, but since we
375 : * only have to do this once per timeline switch, we probably wouldn't
376 : * save any significant amount of work in practice.
377 : */
378 21 : if (current_tli != latest_tli && !XLogRecPtrIsValid(switch_lsn))
379 : {
380 0 : List *tles = readTimeLineHistory(latest_tli);
381 :
382 0 : switch_lsn = tliSwitchPoint(current_tli, tles, &switch_tli);
383 0 : ereport(DEBUG1,
384 : errmsg_internal("switch point from TLI %u to TLI %u is at %X/%08X",
385 : current_tli, switch_tli, LSN_FORMAT_ARGS(switch_lsn)));
386 : }
387 :
388 : /*
389 : * If we've reached the switch LSN, we can't summarize anything else
390 : * on this timeline. Switch to the next timeline and go around again,
391 : * backing up to the exact switch point if we passed it.
392 : */
393 21 : if (XLogRecPtrIsValid(switch_lsn) && current_lsn >= switch_lsn)
394 : {
395 : /* Restart summarization from switch point. */
396 0 : current_tli = switch_tli;
397 0 : current_lsn = switch_lsn;
398 :
399 : /* Next timeline and switch point, if any, not yet known. */
400 0 : switch_lsn = InvalidXLogRecPtr;
401 0 : switch_tli = 0;
402 :
403 : /* Update (really, rewind, if needed) state in shared memory. */
404 0 : LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
405 0 : WalSummarizerCtl->summarized_lsn = current_lsn;
406 0 : WalSummarizerCtl->summarized_tli = current_tli;
407 0 : WalSummarizerCtl->lsn_is_exact = true;
408 0 : WalSummarizerCtl->pending_lsn = current_lsn;
409 0 : LWLockRelease(WALSummarizerLock);
410 :
411 0 : continue;
412 : }
413 :
414 : /* Summarize WAL. */
415 21 : end_of_summary_lsn = SummarizeWAL(current_tli,
416 : current_lsn, exact,
417 : switch_lsn, latest_lsn);
418 : Assert(XLogRecPtrIsValid(end_of_summary_lsn));
419 : Assert(end_of_summary_lsn >= current_lsn);
420 :
421 : /*
422 : * Update state for next loop iteration.
423 : *
424 : * Next summary file should start from exactly where this one ended.
425 : */
426 18 : current_lsn = end_of_summary_lsn;
427 18 : exact = true;
428 :
429 : /* Update state in shared memory. */
430 18 : LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
431 18 : WalSummarizerCtl->summarized_lsn = end_of_summary_lsn;
432 18 : WalSummarizerCtl->summarized_tli = current_tli;
433 18 : WalSummarizerCtl->lsn_is_exact = true;
434 18 : WalSummarizerCtl->pending_lsn = end_of_summary_lsn;
435 18 : LWLockRelease(WALSummarizerLock);
436 :
437 : /* Wake up anyone waiting for more summary files to be written. */
438 18 : ConditionVariableBroadcast(&WalSummarizerCtl->summary_file_cv);
439 : }
440 : }
441 :
442 : /*
443 : * Get information about the state of the WAL summarizer.
444 : */
445 : void
446 0 : GetWalSummarizerState(TimeLineID *summarized_tli, XLogRecPtr *summarized_lsn,
447 : XLogRecPtr *pending_lsn, int *summarizer_pid)
448 : {
449 0 : LWLockAcquire(WALSummarizerLock, LW_SHARED);
450 0 : if (!WalSummarizerCtl->initialized)
451 : {
452 : /*
453 : * If initialized is false, the rest of the structure contents are
454 : * undefined.
455 : */
456 0 : *summarized_tli = 0;
457 0 : *summarized_lsn = InvalidXLogRecPtr;
458 0 : *pending_lsn = InvalidXLogRecPtr;
459 0 : *summarizer_pid = -1;
460 : }
461 : else
462 : {
463 0 : int summarizer_pgprocno = WalSummarizerCtl->summarizer_pgprocno;
464 :
465 0 : *summarized_tli = WalSummarizerCtl->summarized_tli;
466 0 : *summarized_lsn = WalSummarizerCtl->summarized_lsn;
467 0 : if (summarizer_pgprocno == INVALID_PROC_NUMBER)
468 : {
469 : /*
470 : * If the summarizer has exited, the fact that it had processed
471 : * beyond summarized_lsn is irrelevant now.
472 : */
473 0 : *pending_lsn = WalSummarizerCtl->summarized_lsn;
474 0 : *summarizer_pid = -1;
475 : }
476 : else
477 : {
478 0 : *pending_lsn = WalSummarizerCtl->pending_lsn;
479 :
480 : /*
481 : * We're not fussed about inexact answers here, since they could
482 : * become stale instantly, so we don't bother taking the lock, but
483 : * make sure that invalid PID values are normalized to -1.
484 : */
485 0 : *summarizer_pid = GetPGProcByNumber(summarizer_pgprocno)->pid;
486 0 : if (*summarizer_pid <= 0)
487 0 : *summarizer_pid = -1;
488 : }
489 : }
490 0 : LWLockRelease(WALSummarizerLock);
491 0 : }
492 :
493 : /*
494 : * Get the oldest LSN in this server's timeline history that has not yet been
495 : * summarized, and update shared memory state as appropriate.
496 : *
497 : * If *tli != NULL, it will be set to the TLI for the LSN that is returned.
498 : *
499 : * If *lsn_is_exact != NULL, it will be set to true if the returned LSN is
500 : * necessarily the start of a WAL record and false if it's just the beginning
501 : * of a WAL segment.
502 : */
503 : XLogRecPtr
504 2379 : GetOldestUnsummarizedLSN(TimeLineID *tli, bool *lsn_is_exact)
505 : {
506 : TimeLineID latest_tli;
507 : int n;
508 : List *tles;
509 2379 : XLogRecPtr unsummarized_lsn = InvalidXLogRecPtr;
510 2379 : TimeLineID unsummarized_tli = 0;
511 2379 : bool should_make_exact = false;
512 : List *existing_summaries;
513 : ListCell *lc;
514 2379 : bool am_wal_summarizer = AmWalSummarizerProcess();
515 :
516 : /* If not summarizing WAL, do nothing. */
517 2379 : if (!summarize_wal)
518 2369 : return InvalidXLogRecPtr;
519 :
520 : /*
521 : * If we are not the WAL summarizer process, then we normally just want to
522 : * read the values from shared memory. However, as an exception, if shared
523 : * memory hasn't been initialized yet, then we need to do that so that we
524 : * can read legal values and not remove any WAL too early.
525 : */
526 10 : if (!am_wal_summarizer)
527 : {
528 7 : LWLockAcquire(WALSummarizerLock, LW_SHARED);
529 :
530 7 : if (WalSummarizerCtl->initialized)
531 : {
532 6 : unsummarized_lsn = WalSummarizerCtl->summarized_lsn;
533 6 : if (tli != NULL)
534 0 : *tli = WalSummarizerCtl->summarized_tli;
535 6 : if (lsn_is_exact != NULL)
536 0 : *lsn_is_exact = WalSummarizerCtl->lsn_is_exact;
537 6 : LWLockRelease(WALSummarizerLock);
538 6 : return unsummarized_lsn;
539 : }
540 :
541 1 : LWLockRelease(WALSummarizerLock);
542 : }
543 :
544 : /*
545 : * Find the oldest timeline on which WAL still exists, and the earliest
546 : * segment for which it exists.
547 : *
548 : * Note that we do this every time the WAL summarizer process restarts or
549 : * recovers from an error, in case the contents of pg_wal have changed
550 : * under us e.g. if some files were removed, either manually - which
551 : * shouldn't really happen, but might - or by postgres itself, if
552 : * summarize_wal was turned off and then back on again.
553 : */
554 4 : (void) GetLatestLSN(&latest_tli);
555 4 : tles = readTimeLineHistory(latest_tli);
556 4 : for (n = list_length(tles) - 1; n >= 0; --n)
557 : {
558 4 : TimeLineHistoryEntry *tle = list_nth(tles, n);
559 : XLogSegNo oldest_segno;
560 :
561 4 : oldest_segno = XLogGetOldestSegno(tle->tli);
562 4 : if (oldest_segno != 0)
563 : {
564 : /* Compute oldest LSN that still exists on disk. */
565 4 : XLogSegNoOffsetToRecPtr(oldest_segno, 0, wal_segment_size,
566 : unsummarized_lsn);
567 :
568 4 : unsummarized_tli = tle->tli;
569 4 : break;
570 : }
571 : }
572 :
573 : /*
574 : * Don't try to summarize anything older than the end LSN of the newest
575 : * summary file that exists for this timeline.
576 : */
577 : existing_summaries =
578 4 : GetWalSummaries(unsummarized_tli,
579 : InvalidXLogRecPtr, InvalidXLogRecPtr);
580 4 : foreach(lc, existing_summaries)
581 : {
582 0 : WalSummaryFile *ws = lfirst(lc);
583 :
584 0 : if (ws->end_lsn > unsummarized_lsn)
585 : {
586 0 : unsummarized_lsn = ws->end_lsn;
587 0 : should_make_exact = true;
588 : }
589 : }
590 :
591 : /* It really should not be possible for us to find no WAL. */
592 4 : if (unsummarized_tli == 0)
593 0 : ereport(ERROR,
594 : errcode(ERRCODE_INTERNAL_ERROR),
595 : errmsg_internal("no WAL found on timeline %u", latest_tli));
596 :
597 : /*
598 : * If we're the WAL summarizer, we always want to store the values we just
599 : * computed into shared memory, because those are the values we're going
600 : * to use to drive our operation, and so they are the authoritative
601 : * values. Otherwise, we only store values into shared memory if shared
602 : * memory is uninitialized. Our values are not canonical in such a case,
603 : * but it's better to have something than nothing, to guide WAL retention.
604 : */
605 4 : LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
606 4 : if (am_wal_summarizer || !WalSummarizerCtl->initialized)
607 : {
608 4 : WalSummarizerCtl->initialized = true;
609 4 : WalSummarizerCtl->summarized_lsn = unsummarized_lsn;
610 4 : WalSummarizerCtl->summarized_tli = unsummarized_tli;
611 4 : WalSummarizerCtl->lsn_is_exact = should_make_exact;
612 4 : WalSummarizerCtl->pending_lsn = unsummarized_lsn;
613 : }
614 : else
615 0 : unsummarized_lsn = WalSummarizerCtl->summarized_lsn;
616 :
617 : /* Also return the to the caller as required. */
618 4 : if (tli != NULL)
619 3 : *tli = WalSummarizerCtl->summarized_tli;
620 4 : if (lsn_is_exact != NULL)
621 3 : *lsn_is_exact = WalSummarizerCtl->lsn_is_exact;
622 4 : LWLockRelease(WALSummarizerLock);
623 :
624 4 : return unsummarized_lsn;
625 : }
626 :
627 : /*
628 : * Wake up the WAL summarizer process.
629 : *
630 : * This might not work, because there's no guarantee that the WAL summarizer
631 : * process was successfully started, and it also might have started but
632 : * subsequently terminated. So, under normal circumstances, this will get the
633 : * latch set, but there's no guarantee.
634 : */
635 : void
636 1586 : WakeupWalSummarizer(void)
637 : {
638 : ProcNumber pgprocno;
639 :
640 1586 : if (WalSummarizerCtl == NULL)
641 0 : return;
642 :
643 1586 : LWLockAcquire(WALSummarizerLock, LW_SHARED);
644 1586 : pgprocno = WalSummarizerCtl->summarizer_pgprocno;
645 1586 : LWLockRelease(WALSummarizerLock);
646 :
647 1586 : if (pgprocno != INVALID_PROC_NUMBER)
648 3 : SetLatch(&GetPGProcByNumber(pgprocno)->procLatch);
649 : }
650 :
651 : /*
652 : * Wait until WAL summarization reaches the given LSN, but time out with an
653 : * error if the summarizer seems to be stick.
654 : *
655 : * Returns immediately if summarize_wal is turned off while we wait. Caller
656 : * is expected to handle this case, if necessary.
657 : */
658 : void
659 12 : WaitForWalSummarization(XLogRecPtr lsn)
660 : {
661 : TimestampTz initial_time,
662 : cycle_time,
663 : current_time;
664 12 : XLogRecPtr prior_pending_lsn = InvalidXLogRecPtr;
665 12 : int deadcycles = 0;
666 :
667 12 : initial_time = cycle_time = GetCurrentTimestamp();
668 :
669 : while (1)
670 10 : {
671 22 : long timeout_in_ms = 10000;
672 : XLogRecPtr summarized_lsn;
673 : XLogRecPtr pending_lsn;
674 :
675 22 : CHECK_FOR_INTERRUPTS();
676 :
677 : /* If WAL summarization is disabled while we're waiting, give up. */
678 22 : if (!summarize_wal)
679 0 : return;
680 :
681 : /*
682 : * If the LSN summarized on disk has reached the target value, stop.
683 : */
684 22 : LWLockAcquire(WALSummarizerLock, LW_SHARED);
685 22 : summarized_lsn = WalSummarizerCtl->summarized_lsn;
686 22 : pending_lsn = WalSummarizerCtl->pending_lsn;
687 22 : LWLockRelease(WALSummarizerLock);
688 :
689 : /* If WAL summarization has progressed sufficiently, stop waiting. */
690 22 : if (summarized_lsn >= lsn)
691 12 : break;
692 :
693 : /* Recheck current time. */
694 10 : current_time = GetCurrentTimestamp();
695 :
696 : /* Have we finished the current cycle of waiting? */
697 10 : if (TimestampDifferenceMilliseconds(cycle_time,
698 : current_time) >= timeout_in_ms)
699 : {
700 : long elapsed_seconds;
701 :
702 : /* Begin new wait cycle. */
703 0 : cycle_time = TimestampTzPlusMilliseconds(cycle_time,
704 : timeout_in_ms);
705 :
706 : /*
707 : * Keep track of the number of cycles during which there has been
708 : * no progression of pending_lsn. If pending_lsn is not advancing,
709 : * that means that not only are no new files appearing on disk,
710 : * but we're not even incorporating new records into the in-memory
711 : * state.
712 : */
713 0 : if (pending_lsn > prior_pending_lsn)
714 : {
715 0 : prior_pending_lsn = pending_lsn;
716 0 : deadcycles = 0;
717 : }
718 : else
719 0 : ++deadcycles;
720 :
721 : /*
722 : * If we've managed to wait for an entire minute without the WAL
723 : * summarizer absorbing a single WAL record, error out; probably
724 : * something is wrong.
725 : *
726 : * We could consider also erroring out if the summarizer is taking
727 : * too long to catch up, but it's not clear what rate of progress
728 : * would be acceptable and what would be too slow. So instead, we
729 : * just try to error out in the case where there's no progress at
730 : * all. That seems likely to catch a reasonable number of the
731 : * things that can go wrong in practice (e.g. the summarizer
732 : * process is completely hung, say because somebody hooked up a
733 : * debugger to it or something) without giving up too quickly when
734 : * the system is just slow.
735 : */
736 0 : if (deadcycles >= 6)
737 0 : ereport(ERROR,
738 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
739 : errmsg("WAL summarization is not progressing"),
740 : errdetail("Summarization is needed through %X/%08X, but is stuck at %X/%08X on disk and %X/%08X in memory.",
741 : LSN_FORMAT_ARGS(lsn),
742 : LSN_FORMAT_ARGS(summarized_lsn),
743 : LSN_FORMAT_ARGS(pending_lsn))));
744 :
745 :
746 : /*
747 : * Otherwise, just let the user know what's happening.
748 : */
749 0 : elapsed_seconds =
750 0 : TimestampDifferenceMilliseconds(initial_time,
751 : current_time) / 1000;
752 0 : ereport(WARNING,
753 : (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
754 : errmsg_plural("still waiting for WAL summarization through %X/%08X after %ld second",
755 : "still waiting for WAL summarization through %X/%08X after %ld seconds",
756 : elapsed_seconds,
757 : LSN_FORMAT_ARGS(lsn),
758 : elapsed_seconds),
759 : errdetail("Summarization has reached %X/%08X on disk and %X/%08X in memory.",
760 : LSN_FORMAT_ARGS(summarized_lsn),
761 : LSN_FORMAT_ARGS(pending_lsn))));
762 : }
763 :
764 : /*
765 : * Align the wait time to prevent drift. This doesn't really matter,
766 : * but we'd like the warnings about how long we've been waiting to say
767 : * 10 seconds, 20 seconds, 30 seconds, 40 seconds ... without ever
768 : * drifting to something that is not a multiple of ten.
769 : */
770 10 : timeout_in_ms -=
771 10 : TimestampDifferenceMilliseconds(cycle_time, current_time);
772 :
773 : /* Wait and see. */
774 10 : ConditionVariableTimedSleep(&WalSummarizerCtl->summary_file_cv,
775 : timeout_in_ms,
776 : WAIT_EVENT_WAL_SUMMARY_READY);
777 : }
778 :
779 12 : ConditionVariableCancelSleep();
780 : }
781 :
782 : /*
783 : * On exit, update shared memory to make it clear that we're no longer
784 : * running.
785 : */
786 : static void
787 3 : WalSummarizerShutdown(int code, Datum arg)
788 : {
789 3 : LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
790 3 : WalSummarizerCtl->summarizer_pgprocno = INVALID_PROC_NUMBER;
791 3 : LWLockRelease(WALSummarizerLock);
792 3 : }
793 :
794 : /*
795 : * Get the latest LSN that is eligible to be summarized, and set *tli to the
796 : * corresponding timeline.
797 : */
798 : static XLogRecPtr
799 42 : GetLatestLSN(TimeLineID *tli)
800 : {
801 42 : if (!RecoveryInProgress())
802 : {
803 : /* Don't summarize WAL before it's flushed. */
804 41 : return GetFlushRecPtr(tli);
805 : }
806 : else
807 : {
808 : XLogRecPtr flush_lsn;
809 : TimeLineID flush_tli;
810 : XLogRecPtr replay_lsn;
811 : TimeLineID replay_tli;
812 : TimeLineID insert_tli;
813 :
814 : /*
815 : * After the insert TLI has been set and before the control file has
816 : * been updated to show the DB in production, RecoveryInProgress()
817 : * will return true, because it's not yet safe for all backends to
818 : * begin writing WAL. However, replay has already ceased, so from our
819 : * point of view, recovery is already over. We should summarize up to
820 : * where replay stopped and then prepare to resume at the start of the
821 : * insert timeline.
822 : */
823 1 : if ((insert_tli = GetWALInsertionTimeLineIfSet()) != 0)
824 : {
825 1 : *tli = insert_tli;
826 1 : return GetXLogReplayRecPtr(NULL);
827 : }
828 :
829 : /*
830 : * What we really want to know is how much WAL has been flushed to
831 : * disk, but the only flush position available is the one provided by
832 : * the walreceiver, which may not be running, because this could be
833 : * crash recovery or recovery via restore_command. So use either the
834 : * WAL receiver's flush position or the replay position, whichever is
835 : * further ahead, on the theory that if the WAL has been replayed then
836 : * it must also have been flushed to disk.
837 : */
838 0 : flush_lsn = GetWalRcvFlushRecPtr(NULL, &flush_tli);
839 0 : replay_lsn = GetXLogReplayRecPtr(&replay_tli);
840 0 : if (flush_lsn > replay_lsn)
841 : {
842 0 : *tli = flush_tli;
843 0 : return flush_lsn;
844 : }
845 : else
846 : {
847 0 : *tli = replay_tli;
848 0 : return replay_lsn;
849 : }
850 : }
851 : }
852 :
853 : /*
854 : * Interrupt handler for main loop of WAL summarizer process.
855 : */
856 : static void
857 52168 : ProcessWalSummarizerInterrupts(void)
858 : {
859 52168 : if (ProcSignalBarrierPending)
860 0 : ProcessProcSignalBarrier();
861 :
862 52168 : if (ConfigReloadPending)
863 : {
864 0 : ConfigReloadPending = false;
865 0 : ProcessConfigFile(PGC_SIGHUP);
866 : }
867 :
868 52168 : if (ShutdownRequestPending || !summarize_wal)
869 : {
870 3 : ereport(DEBUG1,
871 : errmsg_internal("WAL summarizer shutting down"));
872 3 : proc_exit(0);
873 : }
874 :
875 : /* Perform logging of memory contexts of this process */
876 52165 : if (LogMemoryContextPending)
877 0 : ProcessLogMemoryContextInterrupt();
878 52165 : }
879 :
880 : /*
881 : * Summarize a range of WAL records on a single timeline.
882 : *
883 : * 'tli' is the timeline to be summarized.
884 : *
885 : * 'start_lsn' is the point at which we should start summarizing. If this
886 : * value comes from the end LSN of the previous record as returned by the
887 : * xlogreader machinery, 'exact' should be true; otherwise, 'exact' should
888 : * be false, and this function will search forward for the start of a valid
889 : * WAL record.
890 : *
891 : * 'switch_lsn' is the point at which we should switch to a later timeline,
892 : * if we're summarizing a historic timeline.
893 : *
894 : * 'maximum_lsn' identifies the point beyond which we can't count on being
895 : * able to read any more WAL. It should be the switch point when reading a
896 : * historic timeline, or the most-recently-measured end of WAL when reading
897 : * the current timeline.
898 : *
899 : * The return value is the LSN at which the WAL summary actually ends. Most
900 : * often, a summary file ends because we notice that a checkpoint has
901 : * occurred and reach the redo pointer of that checkpoint, but sometimes
902 : * we stop for other reasons, such as a timeline switch.
903 : */
904 : static XLogRecPtr
905 21 : SummarizeWAL(TimeLineID tli, XLogRecPtr start_lsn, bool exact,
906 : XLogRecPtr switch_lsn, XLogRecPtr maximum_lsn)
907 : {
908 : SummarizerReadLocalXLogPrivate *private_data;
909 : XLogReaderState *xlogreader;
910 : XLogRecPtr summary_start_lsn;
911 21 : XLogRecPtr summary_end_lsn = switch_lsn;
912 : char temp_path[MAXPGPATH];
913 : char final_path[MAXPGPATH];
914 : WalSummaryIO io;
915 21 : BlockRefTable *brtab = CreateEmptyBlockRefTable();
916 21 : bool fast_forward = true;
917 :
918 : /* Initialize private data for xlogreader. */
919 21 : private_data = palloc0_object(SummarizerReadLocalXLogPrivate);
920 21 : private_data->tli = tli;
921 21 : private_data->historic = XLogRecPtrIsValid(switch_lsn);
922 21 : private_data->read_upto = maximum_lsn;
923 :
924 : /* Create xlogreader. */
925 21 : xlogreader = XLogReaderAllocate(wal_segment_size, NULL,
926 21 : XL_ROUTINE(.page_read = &summarizer_read_local_xlog_page,
927 : .segment_open = &wal_segment_open,
928 : .segment_close = &wal_segment_close),
929 : private_data);
930 21 : if (xlogreader == NULL)
931 0 : ereport(ERROR,
932 : (errcode(ERRCODE_OUT_OF_MEMORY),
933 : errmsg("out of memory"),
934 : errdetail("Failed while allocating a WAL reading processor.")));
935 :
936 : /*
937 : * When exact = false, we're starting from an arbitrary point in the WAL
938 : * and must search forward for the start of the next record.
939 : *
940 : * When exact = true, start_lsn should be either the LSN where a record
941 : * begins, or the LSN of a page where the page header is immediately
942 : * followed by the start of a new record. XLogBeginRead should tolerate
943 : * either case.
944 : *
945 : * We need to allow for both cases because the behavior of xlogreader
946 : * varies. When a record spans two or more xlog pages, the ending LSN
947 : * reported by xlogreader will be the starting LSN of the following
948 : * record, but when an xlog page boundary falls between two records, the
949 : * end LSN for the first will be reported as the first byte of the
950 : * following page. We can't know until we read that page how large the
951 : * header will be, but we'll have to skip over it to find the next record.
952 : */
953 21 : if (exact)
954 : {
955 : /*
956 : * Even if start_lsn is the beginning of a page rather than the
957 : * beginning of the first record on that page, we should still use it
958 : * as the start LSN for the summary file. That's because we detect
959 : * missing summary files by looking for cases where the end LSN of one
960 : * file is less than the start LSN of the next file. When only a page
961 : * header is skipped, nothing has been missed.
962 : */
963 18 : XLogBeginRead(xlogreader, start_lsn);
964 18 : summary_start_lsn = start_lsn;
965 : }
966 : else
967 : {
968 3 : summary_start_lsn = XLogFindNextRecord(xlogreader, start_lsn);
969 3 : if (!XLogRecPtrIsValid(summary_start_lsn))
970 : {
971 : /*
972 : * If we hit end-of-WAL while trying to find the next valid
973 : * record, we must be on a historic timeline that has no valid
974 : * records that begin after start_lsn and before end of WAL.
975 : */
976 0 : if (private_data->end_of_wal)
977 : {
978 0 : ereport(DEBUG1,
979 : errmsg_internal("could not read WAL from timeline %u at %X/%08X: end of WAL at %X/%08X",
980 : tli,
981 : LSN_FORMAT_ARGS(start_lsn),
982 : LSN_FORMAT_ARGS(private_data->read_upto)));
983 :
984 : /*
985 : * The timeline ends at or after start_lsn, without containing
986 : * any records. Thus, we must make sure the main loop does not
987 : * iterate. If start_lsn is the end of the timeline, then we
988 : * won't actually emit an empty summary file, but otherwise,
989 : * we must, to capture the fact that the LSN range in question
990 : * contains no interesting WAL records.
991 : */
992 0 : summary_start_lsn = start_lsn;
993 0 : summary_end_lsn = private_data->read_upto;
994 0 : switch_lsn = xlogreader->EndRecPtr;
995 : }
996 : else
997 0 : ereport(ERROR,
998 : errmsg("could not find a valid record after %X/%08X",
999 : LSN_FORMAT_ARGS(start_lsn)));
1000 : }
1001 :
1002 : /* We shouldn't go backward. */
1003 : Assert(summary_start_lsn >= start_lsn);
1004 : }
1005 :
1006 : /*
1007 : * Main loop: read xlog records one by one.
1008 : */
1009 : while (1)
1010 50088 : {
1011 : int block_id;
1012 : char *errormsg;
1013 : XLogRecord *record;
1014 : uint8 rmid;
1015 :
1016 50109 : ProcessWalSummarizerInterrupts();
1017 :
1018 : /* We shouldn't go backward. */
1019 : Assert(summary_start_lsn <= xlogreader->EndRecPtr);
1020 :
1021 : /* Now read the next record. */
1022 50109 : record = XLogReadRecord(xlogreader, &errormsg);
1023 50106 : if (record == NULL)
1024 : {
1025 0 : if (private_data->end_of_wal)
1026 : {
1027 : /*
1028 : * This timeline must be historic and must end before we were
1029 : * able to read a complete record.
1030 : */
1031 0 : ereport(DEBUG1,
1032 : errmsg_internal("could not read WAL from timeline %u at %X/%08X: end of WAL at %X/%08X",
1033 : tli,
1034 : LSN_FORMAT_ARGS(xlogreader->EndRecPtr),
1035 : LSN_FORMAT_ARGS(private_data->read_upto)));
1036 : /* Summary ends at end of WAL. */
1037 0 : summary_end_lsn = private_data->read_upto;
1038 0 : break;
1039 : }
1040 0 : if (errormsg)
1041 0 : ereport(ERROR,
1042 : (errcode_for_file_access(),
1043 : errmsg("could not read WAL from timeline %u at %X/%08X: %s",
1044 : tli, LSN_FORMAT_ARGS(xlogreader->EndRecPtr),
1045 : errormsg)));
1046 : else
1047 0 : ereport(ERROR,
1048 : (errcode_for_file_access(),
1049 : errmsg("could not read WAL from timeline %u at %X/%08X",
1050 : tli, LSN_FORMAT_ARGS(xlogreader->EndRecPtr))));
1051 : }
1052 :
1053 : /* We shouldn't go backward. */
1054 : Assert(summary_start_lsn <= xlogreader->EndRecPtr);
1055 :
1056 50106 : if (XLogRecPtrIsValid(switch_lsn) &&
1057 0 : xlogreader->ReadRecPtr >= switch_lsn)
1058 : {
1059 : /*
1060 : * Whoops! We've read a record that *starts* after the switch LSN,
1061 : * contrary to our goal of reading only until we hit the first
1062 : * record that ends at or after the switch LSN. Pretend we didn't
1063 : * read it after all by bailing out of this loop right here,
1064 : * before we do anything with this record.
1065 : *
1066 : * This can happen because the last record before the switch LSN
1067 : * might be continued across multiple pages, and then we might
1068 : * come to a page with XLP_FIRST_IS_OVERWRITE_CONTRECORD set. In
1069 : * that case, the record that was continued across multiple pages
1070 : * is incomplete and will be disregarded, and the read will
1071 : * restart from the beginning of the page that is flagged
1072 : * XLP_FIRST_IS_OVERWRITE_CONTRECORD.
1073 : *
1074 : * If this case occurs, we can fairly say that the current summary
1075 : * file ends at the switch LSN exactly. The first record on the
1076 : * page marked XLP_FIRST_IS_OVERWRITE_CONTRECORD will be
1077 : * discovered when generating the next summary file.
1078 : */
1079 0 : summary_end_lsn = switch_lsn;
1080 0 : break;
1081 : }
1082 :
1083 : /*
1084 : * Certain types of records require special handling. Redo points and
1085 : * shutdown checkpoints trigger creation of new summary files and can
1086 : * also cause us to enter or exit "fast forward" mode. Other types of
1087 : * records can require special updates to the block reference table.
1088 : */
1089 50106 : rmid = XLogRecGetRmid(xlogreader);
1090 50106 : if (rmid == RM_XLOG_ID)
1091 : {
1092 : bool new_fast_forward;
1093 :
1094 : /*
1095 : * If we've already processed some WAL records when we hit a redo
1096 : * point or shutdown checkpoint, then we stop summarization before
1097 : * including this record in the current file, so that it will be
1098 : * the first record in the next file.
1099 : *
1100 : * When we hit one of those record types as the first record in a
1101 : * file, we adjust our notion of whether we're fast-forwarding.
1102 : * Any WAL generated with wal_level=minimal must be skipped
1103 : * without actually generating any summary file, because an
1104 : * incremental backup that crosses such WAL would be unsafe.
1105 : */
1106 692 : if (SummarizeXlogRecord(xlogreader, &new_fast_forward))
1107 : {
1108 39 : if (xlogreader->ReadRecPtr > summary_start_lsn)
1109 : {
1110 18 : summary_end_lsn = xlogreader->ReadRecPtr;
1111 18 : break;
1112 : }
1113 : else
1114 21 : fast_forward = new_fast_forward;
1115 : }
1116 : }
1117 49414 : else if (!fast_forward)
1118 : {
1119 : /*
1120 : * This switch handles record types that require extra updates to
1121 : * the contents of the block reference table.
1122 : */
1123 49414 : switch (rmid)
1124 : {
1125 4 : case RM_DBASE_ID:
1126 4 : SummarizeDbaseRecord(xlogreader, brtab);
1127 4 : break;
1128 29 : case RM_SMGR_ID:
1129 29 : SummarizeSmgrRecord(xlogreader, brtab);
1130 29 : break;
1131 1536 : case RM_XACT_ID:
1132 1536 : SummarizeXactRecord(xlogreader, brtab);
1133 1536 : break;
1134 : }
1135 : }
1136 :
1137 : /*
1138 : * If we're in fast-forward mode, we don't really need to do anything.
1139 : * Otherwise, feed block references from xlog record to block
1140 : * reference table.
1141 : */
1142 50088 : if (!fast_forward)
1143 : {
1144 99762 : for (block_id = 0; block_id <= XLogRecMaxBlockId(xlogreader);
1145 49674 : block_id++)
1146 : {
1147 : RelFileLocator rlocator;
1148 : ForkNumber forknum;
1149 : BlockNumber blocknum;
1150 :
1151 49674 : if (!XLogRecGetBlockTagExtended(xlogreader, block_id, &rlocator,
1152 : &forknum, &blocknum, NULL))
1153 40 : continue;
1154 :
1155 : /*
1156 : * As we do elsewhere, ignore the FSM fork, because it's not
1157 : * fully WAL-logged.
1158 : */
1159 49634 : if (forknum != FSM_FORKNUM)
1160 49347 : BlockRefTableMarkBlockModified(brtab, &rlocator, forknum,
1161 : blocknum);
1162 : }
1163 : }
1164 :
1165 : /* Update our notion of where this summary file ends. */
1166 50088 : summary_end_lsn = xlogreader->EndRecPtr;
1167 :
1168 : /* Also update shared memory. */
1169 50088 : LWLockAcquire(WALSummarizerLock, LW_EXCLUSIVE);
1170 : Assert(summary_end_lsn >= WalSummarizerCtl->summarized_lsn);
1171 50088 : WalSummarizerCtl->pending_lsn = summary_end_lsn;
1172 50088 : LWLockRelease(WALSummarizerLock);
1173 :
1174 : /*
1175 : * If we have a switch LSN and have reached it, stop before reading
1176 : * the next record.
1177 : */
1178 50088 : if (XLogRecPtrIsValid(switch_lsn) &&
1179 0 : xlogreader->EndRecPtr >= switch_lsn)
1180 0 : break;
1181 : }
1182 :
1183 : /* Destroy xlogreader. */
1184 18 : pfree(xlogreader->private_data);
1185 18 : XLogReaderFree(xlogreader);
1186 :
1187 : /*
1188 : * If a timeline switch occurs, we may fail to make any progress at all
1189 : * before exiting the loop above. If that happens, we don't write a WAL
1190 : * summary file at all. We can also skip writing a file if we're in
1191 : * fast-forward mode.
1192 : */
1193 18 : if (summary_end_lsn > summary_start_lsn && !fast_forward)
1194 : {
1195 : /* Generate temporary and final path name. */
1196 18 : snprintf(temp_path, MAXPGPATH,
1197 : XLOGDIR "/summaries/temp.summary");
1198 18 : snprintf(final_path, MAXPGPATH,
1199 : XLOGDIR "/summaries/%08X%08X%08X%08X%08X.summary",
1200 : tli,
1201 18 : LSN_FORMAT_ARGS(summary_start_lsn),
1202 18 : LSN_FORMAT_ARGS(summary_end_lsn));
1203 :
1204 : /* Open the temporary file for writing. */
1205 18 : io.filepos = 0;
1206 18 : io.file = PathNameOpenFile(temp_path, O_WRONLY | O_CREAT | O_TRUNC);
1207 18 : if (io.file < 0)
1208 0 : ereport(ERROR,
1209 : (errcode_for_file_access(),
1210 : errmsg("could not create file \"%s\": %m", temp_path)));
1211 :
1212 : /* Write the data. */
1213 18 : WriteBlockRefTable(brtab, WriteWalSummary, &io);
1214 :
1215 : /* Close temporary file and shut down xlogreader. */
1216 18 : FileClose(io.file);
1217 :
1218 : /* Tell the user what we did. */
1219 18 : ereport(DEBUG1,
1220 : errmsg_internal("summarized WAL on TLI %u from %X/%08X to %X/%08X",
1221 : tli,
1222 : LSN_FORMAT_ARGS(summary_start_lsn),
1223 : LSN_FORMAT_ARGS(summary_end_lsn)));
1224 :
1225 : /* Durably rename the new summary into place. */
1226 18 : durable_rename(temp_path, final_path, ERROR);
1227 : }
1228 :
1229 : /* If we skipped a non-zero amount of WAL, log a debug message. */
1230 18 : if (summary_end_lsn > summary_start_lsn && fast_forward)
1231 0 : ereport(DEBUG1,
1232 : errmsg_internal("skipped summarizing WAL on TLI %u from %X/%08X to %X/%08X",
1233 : tli,
1234 : LSN_FORMAT_ARGS(summary_start_lsn),
1235 : LSN_FORMAT_ARGS(summary_end_lsn)));
1236 :
1237 18 : return summary_end_lsn;
1238 : }
1239 :
1240 : /*
1241 : * Special handling for WAL records with RM_DBASE_ID.
1242 : */
1243 : static void
1244 4 : SummarizeDbaseRecord(XLogReaderState *xlogreader, BlockRefTable *brtab)
1245 : {
1246 4 : uint8 info = XLogRecGetInfo(xlogreader) & ~XLR_INFO_MASK;
1247 :
1248 : /*
1249 : * We use relfilenode zero for a given database OID and tablespace OID to
1250 : * indicate that all relations with that pair of IDs have been recreated
1251 : * if they exist at all. Effectively, we're setting a limit block of 0 for
1252 : * all such relfilenodes.
1253 : *
1254 : * Technically, this special handling is only needed in the case of
1255 : * XLOG_DBASE_CREATE_FILE_COPY, because that can create a whole bunch of
1256 : * relation files in a directory without logging anything specific to each
1257 : * one. If we didn't mark the whole DB OID/TS OID combination in some way,
1258 : * then a tablespace that was dropped after the reference backup and
1259 : * recreated using the FILE_COPY method prior to the incremental backup
1260 : * would look just like one that was never touched at all, which would be
1261 : * catastrophic.
1262 : *
1263 : * But it seems best to adopt this treatment for all records that drop or
1264 : * create a DB OID/TS OID combination. That's similar to how we treat the
1265 : * limit block for individual relations, and it's an extra layer of safety
1266 : * here. We can never lose data by marking more stuff as needing to be
1267 : * backed up in full.
1268 : */
1269 4 : if (info == XLOG_DBASE_CREATE_FILE_COPY)
1270 : {
1271 : xl_dbase_create_file_copy_rec *xlrec;
1272 : RelFileLocator rlocator;
1273 :
1274 4 : xlrec =
1275 4 : (xl_dbase_create_file_copy_rec *) XLogRecGetData(xlogreader);
1276 4 : rlocator.spcOid = xlrec->tablespace_id;
1277 4 : rlocator.dbOid = xlrec->db_id;
1278 4 : rlocator.relNumber = 0;
1279 4 : BlockRefTableSetLimitBlock(brtab, &rlocator, MAIN_FORKNUM, 0);
1280 : }
1281 0 : else if (info == XLOG_DBASE_CREATE_WAL_LOG)
1282 : {
1283 : xl_dbase_create_wal_log_rec *xlrec;
1284 : RelFileLocator rlocator;
1285 :
1286 0 : xlrec = (xl_dbase_create_wal_log_rec *) XLogRecGetData(xlogreader);
1287 0 : rlocator.spcOid = xlrec->tablespace_id;
1288 0 : rlocator.dbOid = xlrec->db_id;
1289 0 : rlocator.relNumber = 0;
1290 0 : BlockRefTableSetLimitBlock(brtab, &rlocator, MAIN_FORKNUM, 0);
1291 : }
1292 0 : else if (info == XLOG_DBASE_DROP)
1293 : {
1294 : xl_dbase_drop_rec *xlrec;
1295 : RelFileLocator rlocator;
1296 : int i;
1297 :
1298 0 : xlrec = (xl_dbase_drop_rec *) XLogRecGetData(xlogreader);
1299 0 : rlocator.dbOid = xlrec->db_id;
1300 0 : rlocator.relNumber = 0;
1301 0 : for (i = 0; i < xlrec->ntablespaces; ++i)
1302 : {
1303 0 : rlocator.spcOid = xlrec->tablespace_ids[i];
1304 0 : BlockRefTableSetLimitBlock(brtab, &rlocator, MAIN_FORKNUM, 0);
1305 : }
1306 : }
1307 4 : }
1308 :
1309 : /*
1310 : * Special handling for WAL records with RM_SMGR_ID.
1311 : */
1312 : static void
1313 29 : SummarizeSmgrRecord(XLogReaderState *xlogreader, BlockRefTable *brtab)
1314 : {
1315 29 : uint8 info = XLogRecGetInfo(xlogreader) & ~XLR_INFO_MASK;
1316 :
1317 29 : if (info == XLOG_SMGR_CREATE)
1318 : {
1319 : xl_smgr_create *xlrec;
1320 :
1321 : /*
1322 : * If a new relation fork is created on disk, there is no point
1323 : * tracking anything about which blocks have been modified, because
1324 : * the whole thing will be new. Hence, set the limit block for this
1325 : * fork to 0.
1326 : *
1327 : * Ignore the FSM fork, which is not fully WAL-logged.
1328 : */
1329 28 : xlrec = (xl_smgr_create *) XLogRecGetData(xlogreader);
1330 :
1331 28 : if (xlrec->forkNum != FSM_FORKNUM)
1332 28 : BlockRefTableSetLimitBlock(brtab, &xlrec->rlocator,
1333 : xlrec->forkNum, 0);
1334 : }
1335 1 : else if (info == XLOG_SMGR_TRUNCATE)
1336 : {
1337 : xl_smgr_truncate *xlrec;
1338 :
1339 1 : xlrec = (xl_smgr_truncate *) XLogRecGetData(xlogreader);
1340 :
1341 : /*
1342 : * If a relation fork is truncated on disk, there is no point in
1343 : * tracking anything about block modifications beyond the truncation
1344 : * point.
1345 : *
1346 : * We ignore SMGR_TRUNCATE_FSM here because the FSM isn't fully
1347 : * WAL-logged and thus we can't track modified blocks for it anyway.
1348 : */
1349 1 : if ((xlrec->flags & SMGR_TRUNCATE_HEAP) != 0)
1350 1 : BlockRefTableSetLimitBlock(brtab, &xlrec->rlocator,
1351 : MAIN_FORKNUM, xlrec->blkno);
1352 1 : if ((xlrec->flags & SMGR_TRUNCATE_VM) != 0)
1353 1 : BlockRefTableSetLimitBlock(brtab, &xlrec->rlocator,
1354 : VISIBILITYMAP_FORKNUM, xlrec->blkno);
1355 : }
1356 29 : }
1357 :
1358 : /*
1359 : * Special handling for WAL records with RM_XACT_ID.
1360 : */
1361 : static void
1362 1536 : SummarizeXactRecord(XLogReaderState *xlogreader, BlockRefTable *brtab)
1363 : {
1364 1536 : uint8 info = XLogRecGetInfo(xlogreader) & ~XLR_INFO_MASK;
1365 1536 : uint8 xact_info = info & XLOG_XACT_OPMASK;
1366 :
1367 1536 : if (xact_info == XLOG_XACT_COMMIT ||
1368 : xact_info == XLOG_XACT_COMMIT_PREPARED)
1369 1536 : {
1370 1536 : xl_xact_commit *xlrec = (xl_xact_commit *) XLogRecGetData(xlogreader);
1371 : xl_xact_parsed_commit parsed;
1372 : int i;
1373 :
1374 : /*
1375 : * Don't track modified blocks for any relations that were removed on
1376 : * commit.
1377 : */
1378 1536 : ParseCommitRecord(XLogRecGetInfo(xlogreader), xlrec, &parsed);
1379 1536 : for (i = 0; i < parsed.nrels; ++i)
1380 : {
1381 : ForkNumber forknum;
1382 :
1383 0 : for (forknum = 0; forknum <= MAX_FORKNUM; ++forknum)
1384 0 : if (forknum != FSM_FORKNUM)
1385 0 : BlockRefTableSetLimitBlock(brtab, &parsed.xlocators[i],
1386 : forknum, 0);
1387 : }
1388 : }
1389 0 : else if (xact_info == XLOG_XACT_ABORT ||
1390 : xact_info == XLOG_XACT_ABORT_PREPARED)
1391 : {
1392 0 : xl_xact_abort *xlrec = (xl_xact_abort *) XLogRecGetData(xlogreader);
1393 : xl_xact_parsed_abort parsed;
1394 : int i;
1395 :
1396 : /*
1397 : * Don't track modified blocks for any relations that were removed on
1398 : * abort.
1399 : */
1400 0 : ParseAbortRecord(XLogRecGetInfo(xlogreader), xlrec, &parsed);
1401 0 : for (i = 0; i < parsed.nrels; ++i)
1402 : {
1403 : ForkNumber forknum;
1404 :
1405 0 : for (forknum = 0; forknum <= MAX_FORKNUM; ++forknum)
1406 0 : if (forknum != FSM_FORKNUM)
1407 0 : BlockRefTableSetLimitBlock(brtab, &parsed.xlocators[i],
1408 : forknum, 0);
1409 : }
1410 : }
1411 1536 : }
1412 :
1413 : /*
1414 : * Special handling for WAL records with RM_XLOG_ID.
1415 : *
1416 : * The return value is true if WAL summarization should stop before this
1417 : * record and false otherwise. When the return value is true,
1418 : * *new_fast_forward indicates whether future processing should be done
1419 : * in fast forward mode (i.e. read WAL without emitting summaries) or not.
1420 : */
1421 : static bool
1422 692 : SummarizeXlogRecord(XLogReaderState *xlogreader, bool *new_fast_forward)
1423 : {
1424 692 : uint8 info = XLogRecGetInfo(xlogreader) & ~XLR_INFO_MASK;
1425 : int record_wal_level;
1426 :
1427 692 : if (info == XLOG_CHECKPOINT_REDO)
1428 : {
1429 : /* Payload is wal_level at the time record was written. */
1430 23 : memcpy(&record_wal_level, XLogRecGetData(xlogreader), sizeof(int));
1431 : }
1432 669 : else if (info == XLOG_CHECKPOINT_SHUTDOWN)
1433 : {
1434 : CheckPoint rec_ckpt;
1435 :
1436 : /* Extract wal_level at time record was written from payload. */
1437 12 : memcpy(&rec_ckpt, XLogRecGetData(xlogreader), sizeof(CheckPoint));
1438 12 : record_wal_level = rec_ckpt.wal_level;
1439 : }
1440 657 : else if (info == XLOG_PARAMETER_CHANGE)
1441 : {
1442 : xl_parameter_change xlrec;
1443 :
1444 : /* Extract wal_level at time record was written from payload. */
1445 4 : memcpy(&xlrec, XLogRecGetData(xlogreader),
1446 : sizeof(xl_parameter_change));
1447 4 : record_wal_level = xlrec.wal_level;
1448 : }
1449 653 : else if (info == XLOG_END_OF_RECOVERY)
1450 : {
1451 : xl_end_of_recovery xlrec;
1452 :
1453 : /* Extract wal_level at time record was written from payload. */
1454 0 : memcpy(&xlrec, XLogRecGetData(xlogreader), sizeof(xl_end_of_recovery));
1455 0 : record_wal_level = xlrec.wal_level;
1456 : }
1457 : else
1458 : {
1459 : /* No special handling required. Return false. */
1460 653 : return false;
1461 : }
1462 :
1463 : /*
1464 : * Redo can only begin at an XLOG_CHECKPOINT_REDO or
1465 : * XLOG_CHECKPOINT_SHUTDOWN record, so we want WAL summarization to begin
1466 : * at those points. Hence, when those records are encountered, return
1467 : * true, so that we stop just before summarizing either of those records.
1468 : *
1469 : * We also reach here if we just saw XLOG_END_OF_RECOVERY or
1470 : * XLOG_PARAMETER_CHANGE. These are not places where recovery can start,
1471 : * but they're still relevant here. A new timeline can begin with
1472 : * XLOG_END_OF_RECOVERY, so we need to confirm the WAL level at that
1473 : * point; and a restart can provoke XLOG_PARAMETER_CHANGE after an
1474 : * intervening change to postgresql.conf, which might force us to stop
1475 : * summarizing.
1476 : */
1477 39 : *new_fast_forward = (record_wal_level == WAL_LEVEL_MINIMAL);
1478 39 : return true;
1479 : }
1480 :
1481 : /*
1482 : * Similar to read_local_xlog_page, but limited to read from one particular
1483 : * timeline. If the end of WAL is reached, it will wait for more if reading
1484 : * from the current timeline, or give up if reading from a historic timeline.
1485 : * In the latter case, it will also set private_data->end_of_wal = true.
1486 : *
1487 : * Caller must set private_data->tli to the TLI of interest,
1488 : * private_data->read_upto to the lowest LSN that is not known to be safe
1489 : * to read on that timeline, and private_data->historic to true if and only
1490 : * if the timeline is not the current timeline. This function will update
1491 : * private_data->read_upto and private_data->historic if more WAL appears
1492 : * on the current timeline or if the current timeline becomes historic.
1493 : */
1494 : static int
1495 1990 : summarizer_read_local_xlog_page(XLogReaderState *state,
1496 : XLogRecPtr targetPagePtr, int reqLen,
1497 : XLogRecPtr targetRecPtr, char *cur_page)
1498 : {
1499 : int count;
1500 : WALReadError errinfo;
1501 : SummarizerReadLocalXLogPrivate *private_data;
1502 :
1503 1990 : ProcessWalSummarizerInterrupts();
1504 :
1505 1990 : private_data = (SummarizerReadLocalXLogPrivate *)
1506 : state->private_data;
1507 :
1508 : while (1)
1509 : {
1510 2007 : if (targetPagePtr + XLOG_BLCKSZ <= private_data->read_upto)
1511 : {
1512 : /*
1513 : * more than one block available; read only that block, have
1514 : * caller come back if they need more.
1515 : */
1516 1971 : count = XLOG_BLCKSZ;
1517 1971 : break;
1518 : }
1519 36 : else if (targetPagePtr + reqLen > private_data->read_upto)
1520 : {
1521 : /* We don't seem to have enough data. */
1522 20 : if (private_data->historic)
1523 : {
1524 : /*
1525 : * This is a historic timeline, so there will never be any
1526 : * more data than we have currently.
1527 : */
1528 0 : private_data->end_of_wal = true;
1529 0 : return -1;
1530 : }
1531 : else
1532 : {
1533 : XLogRecPtr latest_lsn;
1534 : TimeLineID latest_tli;
1535 :
1536 : /*
1537 : * This is - or at least was up until very recently - the
1538 : * current timeline, so more data might show up. Delay here
1539 : * so we don't tight-loop.
1540 : */
1541 20 : ProcessWalSummarizerInterrupts();
1542 17 : summarizer_wait_for_wal();
1543 :
1544 : /* Recheck end-of-WAL. */
1545 17 : latest_lsn = GetLatestLSN(&latest_tli);
1546 17 : if (private_data->tli == latest_tli)
1547 : {
1548 : /* Still the current timeline, update max LSN. */
1549 : Assert(latest_lsn >= private_data->read_upto);
1550 17 : private_data->read_upto = latest_lsn;
1551 : }
1552 : else
1553 : {
1554 0 : List *tles = readTimeLineHistory(latest_tli);
1555 : XLogRecPtr switchpoint;
1556 :
1557 : /*
1558 : * The timeline we're scanning is no longer the latest
1559 : * one. Figure out when it ended.
1560 : */
1561 0 : private_data->historic = true;
1562 0 : switchpoint = tliSwitchPoint(private_data->tli, tles,
1563 : NULL);
1564 :
1565 : /*
1566 : * Allow reads up to exactly the switch point.
1567 : *
1568 : * It's possible that this will cause read_upto to move
1569 : * backwards, because we might have been promoted before
1570 : * reaching the end of the previous timeline. In that
1571 : * case, the next loop iteration will likely conclude that
1572 : * we've reached end of WAL.
1573 : */
1574 0 : private_data->read_upto = switchpoint;
1575 :
1576 : /* Debugging output. */
1577 0 : ereport(DEBUG1,
1578 : errmsg_internal("timeline %u became historic, can read up to %X/%08X",
1579 : private_data->tli, LSN_FORMAT_ARGS(private_data->read_upto)));
1580 : }
1581 :
1582 : /* Go around and try again. */
1583 : }
1584 : }
1585 : else
1586 : {
1587 : /* enough bytes available to satisfy the request */
1588 16 : count = private_data->read_upto - targetPagePtr;
1589 16 : break;
1590 : }
1591 : }
1592 :
1593 1987 : if (!WALRead(state, cur_page, targetPagePtr, count,
1594 : private_data->tli, &errinfo))
1595 0 : WALReadRaiseError(&errinfo);
1596 :
1597 : /* Track that we read a page, for sleep time calculation. */
1598 1987 : ++pages_read_since_last_sleep;
1599 :
1600 : /* number of valid bytes in the buffer */
1601 1987 : return count;
1602 : }
1603 :
1604 : /*
1605 : * Sleep for long enough that we believe it's likely that more WAL will
1606 : * be available afterwards.
1607 : */
1608 : static void
1609 17 : summarizer_wait_for_wal(void)
1610 : {
1611 17 : if (pages_read_since_last_sleep == 0)
1612 : {
1613 : /*
1614 : * No pages were read since the last sleep, so double the sleep time,
1615 : * but not beyond the maximum allowable value.
1616 : */
1617 5 : sleep_quanta = Min(sleep_quanta * 2, MAX_SLEEP_QUANTA);
1618 : }
1619 12 : else if (pages_read_since_last_sleep > 1)
1620 : {
1621 : /*
1622 : * Multiple pages were read since the last sleep, so reduce the sleep
1623 : * time.
1624 : *
1625 : * A large burst of activity should be able to quickly reduce the
1626 : * sleep time to the minimum, but we don't want a handful of extra WAL
1627 : * records to provoke a strong reaction. We choose to reduce the sleep
1628 : * time by 1 quantum for each page read beyond the first, which is a
1629 : * fairly arbitrary way of trying to be reactive without overreacting.
1630 : */
1631 7 : if (pages_read_since_last_sleep > sleep_quanta - 1)
1632 7 : sleep_quanta = 1;
1633 : else
1634 0 : sleep_quanta -= pages_read_since_last_sleep;
1635 : }
1636 :
1637 : /* Report pending statistics to the cumulative stats system. */
1638 17 : pgstat_report_wal(false);
1639 :
1640 : /* OK, now sleep. */
1641 17 : (void) WaitLatch(MyLatch,
1642 : WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
1643 : sleep_quanta * MS_PER_SLEEP_QUANTUM,
1644 : WAIT_EVENT_WAL_SUMMARIZER_WAL);
1645 17 : ResetLatch(MyLatch);
1646 :
1647 : /* Reset count of pages read. */
1648 17 : pages_read_since_last_sleep = 0;
1649 17 : }
1650 :
1651 : /*
1652 : * Remove WAL summaries whose mtimes are older than wal_summary_keep_time.
1653 : */
1654 : static void
1655 21 : MaybeRemoveOldWalSummaries(void)
1656 : {
1657 21 : XLogRecPtr redo_pointer = GetRedoRecPtr();
1658 : List *wslist;
1659 : time_t cutoff_time;
1660 :
1661 : /* If WAL summary removal is disabled, don't do anything. */
1662 21 : if (wal_summary_keep_time == 0)
1663 0 : return;
1664 :
1665 : /*
1666 : * If the redo pointer has not advanced, don't do anything.
1667 : *
1668 : * This has the effect that we only try to remove old WAL summary files
1669 : * once per checkpoint cycle.
1670 : */
1671 21 : if (redo_pointer == redo_pointer_at_last_summary_removal)
1672 15 : return;
1673 6 : redo_pointer_at_last_summary_removal = redo_pointer;
1674 :
1675 : /*
1676 : * Files should only be removed if the last modification time precedes the
1677 : * cutoff time we compute here.
1678 : */
1679 6 : cutoff_time = time(NULL) - wal_summary_keep_time * SECS_PER_MINUTE;
1680 :
1681 : /* Get all the summaries that currently exist. */
1682 6 : wslist = GetWalSummaries(0, InvalidXLogRecPtr, InvalidXLogRecPtr);
1683 :
1684 : /* Loop until all summaries have been considered for removal. */
1685 9 : while (wslist != NIL)
1686 : {
1687 : ListCell *lc;
1688 : XLogSegNo oldest_segno;
1689 3 : XLogRecPtr oldest_lsn = InvalidXLogRecPtr;
1690 : TimeLineID selected_tli;
1691 :
1692 3 : ProcessWalSummarizerInterrupts();
1693 :
1694 : /*
1695 : * Pick a timeline for which some summary files still exist on disk,
1696 : * and find the oldest LSN that still exists on disk for that
1697 : * timeline.
1698 : */
1699 3 : selected_tli = ((WalSummaryFile *) linitial(wslist))->tli;
1700 3 : oldest_segno = XLogGetOldestSegno(selected_tli);
1701 3 : if (oldest_segno != 0)
1702 3 : XLogSegNoOffsetToRecPtr(oldest_segno, 0, wal_segment_size,
1703 : oldest_lsn);
1704 :
1705 :
1706 : /* Consider each WAL file on the selected timeline in turn. */
1707 28 : foreach(lc, wslist)
1708 : {
1709 25 : WalSummaryFile *ws = lfirst(lc);
1710 :
1711 25 : ProcessWalSummarizerInterrupts();
1712 :
1713 : /* If it's not on this timeline, it's not time to consider it. */
1714 25 : if (selected_tli != ws->tli)
1715 0 : continue;
1716 :
1717 : /*
1718 : * If the WAL doesn't exist any more, we can remove it if the file
1719 : * modification time is old enough.
1720 : */
1721 25 : if (!XLogRecPtrIsValid(oldest_lsn) || ws->end_lsn <= oldest_lsn)
1722 7 : RemoveWalSummaryIfOlderThan(ws, cutoff_time);
1723 :
1724 : /*
1725 : * Whether we removed the file or not, we need not consider it
1726 : * again.
1727 : */
1728 25 : wslist = foreach_delete_current(wslist, lc);
1729 25 : pfree(ws);
1730 : }
1731 : }
1732 : }
|