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