Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * xlogreader.c
4 : * Generic XLog reading facility
5 : *
6 : * Portions Copyright (c) 2013-2023, PostgreSQL Global Development Group
7 : *
8 : * IDENTIFICATION
9 : * src/backend/access/transam/xlogreader.c
10 : *
11 : * NOTES
12 : * See xlogreader.h for more notes on this facility.
13 : *
14 : * This file is compiled as both front-end and backend code, so it
15 : * may not use ereport, server-defined static variables, etc.
16 : *-------------------------------------------------------------------------
17 : */
18 : #include "postgres.h"
19 :
20 : #include <unistd.h>
21 : #ifdef USE_LZ4
22 : #include <lz4.h>
23 : #endif
24 : #ifdef USE_ZSTD
25 : #include <zstd.h>
26 : #endif
27 :
28 : #include "access/transam.h"
29 : #include "access/xlog_internal.h"
30 : #include "access/xlogreader.h"
31 : #include "access/xlogrecord.h"
32 : #include "catalog/pg_control.h"
33 : #include "common/pg_lzcompress.h"
34 : #include "replication/origin.h"
35 :
36 : #ifndef FRONTEND
37 : #include "miscadmin.h"
38 : #include "pgstat.h"
39 : #include "utils/memutils.h"
40 : #else
41 : #include "common/logging.h"
42 : #endif
43 :
44 : static void report_invalid_record(XLogReaderState *state, const char *fmt,...)
45 : pg_attribute_printf(2, 3);
46 : static void allocate_recordbuf(XLogReaderState *state, uint32 reclength);
47 : static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr,
48 : int reqLen);
49 : static void XLogReaderInvalReadState(XLogReaderState *state);
50 : static XLogPageReadResult XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking);
51 : static bool ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr,
52 : XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess);
53 : static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record,
54 : XLogRecPtr recptr);
55 : static void ResetDecoder(XLogReaderState *state);
56 : static void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt,
57 : int segsize, const char *waldir);
58 :
59 : /* size of the buffer allocated for error message. */
60 : #define MAX_ERRORMSG_LEN 1000
61 :
62 : /*
63 : * Default size; large enough that typical users of XLogReader won't often need
64 : * to use the 'oversized' memory allocation code path.
65 : */
66 : #define DEFAULT_DECODE_BUFFER_SIZE (64 * 1024)
67 :
68 : /*
69 : * Construct a string in state->errormsg_buf explaining what's wrong with
70 : * the current record being read.
71 : */
72 : static void
73 0 : report_invalid_record(XLogReaderState *state, const char *fmt,...)
74 : {
75 : va_list args;
76 :
77 0 : fmt = _(fmt);
78 :
79 0 : va_start(args, fmt);
80 0 : vsnprintf(state->errormsg_buf, MAX_ERRORMSG_LEN, fmt, args);
81 0 : va_end(args);
82 :
83 0 : state->errormsg_deferred = true;
84 0 : }
85 :
86 : /*
87 : * Set the size of the decoding buffer. A pointer to a caller supplied memory
88 : * region may also be passed in, in which case non-oversized records will be
89 : * decoded there.
90 : */
91 : void
92 0 : XLogReaderSetDecodeBuffer(XLogReaderState *state, void *buffer, size_t size)
93 : {
94 : Assert(state->decode_buffer == NULL);
95 :
96 0 : state->decode_buffer = buffer;
97 0 : state->decode_buffer_size = size;
98 0 : state->decode_buffer_tail = buffer;
99 0 : state->decode_buffer_head = buffer;
100 0 : }
101 :
102 : /*
103 : * Allocate and initialize a new XLogReader.
104 : *
105 : * Returns NULL if the xlogreader couldn't be allocated.
106 : */
107 : XLogReaderState *
108 78 : XLogReaderAllocate(int wal_segment_size, const char *waldir,
109 : XLogReaderRoutine *routine, void *private_data)
110 : {
111 : XLogReaderState *state;
112 :
113 : state = (XLogReaderState *)
114 78 : palloc_extended(sizeof(XLogReaderState),
115 : MCXT_ALLOC_NO_OOM | MCXT_ALLOC_ZERO);
116 78 : if (!state)
117 0 : return NULL;
118 :
119 : /* initialize caller-provided support functions */
120 78 : state->routine = *routine;
121 :
122 : /*
123 : * Permanently allocate readBuf. We do it this way, rather than just
124 : * making a static array, for two reasons: (1) no need to waste the
125 : * storage in most instantiations of the backend; (2) a static char array
126 : * isn't guaranteed to have any particular alignment, whereas
127 : * palloc_extended() will provide MAXALIGN'd storage.
128 : */
129 78 : state->readBuf = (char *) palloc_extended(XLOG_BLCKSZ,
130 : MCXT_ALLOC_NO_OOM);
131 78 : if (!state->readBuf)
132 : {
133 0 : pfree(state);
134 0 : return NULL;
135 : }
136 :
137 : /* Initialize segment info. */
138 78 : WALOpenSegmentInit(&state->seg, &state->segcxt, wal_segment_size,
139 : waldir);
140 :
141 : /* system_identifier initialized to zeroes above */
142 78 : state->private_data = private_data;
143 : /* ReadRecPtr, EndRecPtr and readLen initialized to zeroes above */
144 78 : state->errormsg_buf = palloc_extended(MAX_ERRORMSG_LEN + 1,
145 : MCXT_ALLOC_NO_OOM);
146 78 : if (!state->errormsg_buf)
147 : {
148 0 : pfree(state->readBuf);
149 0 : pfree(state);
150 0 : return NULL;
151 : }
152 78 : state->errormsg_buf[0] = '\0';
153 :
154 : /*
155 : * Allocate an initial readRecordBuf of minimal size, which can later be
156 : * enlarged if necessary.
157 : */
158 78 : allocate_recordbuf(state, 0);
159 78 : return state;
160 : }
161 :
162 : void
163 78 : XLogReaderFree(XLogReaderState *state)
164 : {
165 78 : if (state->seg.ws_file != -1)
166 0 : state->routine.segment_close(state);
167 :
168 78 : if (state->decode_buffer && state->free_decode_buffer)
169 78 : pfree(state->decode_buffer);
170 :
171 78 : pfree(state->errormsg_buf);
172 78 : if (state->readRecordBuf)
173 78 : pfree(state->readRecordBuf);
174 78 : pfree(state->readBuf);
175 78 : pfree(state);
176 78 : }
177 :
178 : /*
179 : * Allocate readRecordBuf to fit a record of at least the given length.
180 : *
181 : * readRecordBufSize is set to the new buffer size.
182 : *
183 : * To avoid useless small increases, round its size to a multiple of
184 : * XLOG_BLCKSZ, and make sure it's at least 5*Max(BLCKSZ, XLOG_BLCKSZ) to start
185 : * with. (That is enough for all "normal" records, but very large commit or
186 : * abort records might need more space.)
187 : *
188 : * Note: This routine should *never* be called for xl_tot_len until the header
189 : * of the record has been fully validated.
190 : */
191 : static void
192 78 : allocate_recordbuf(XLogReaderState *state, uint32 reclength)
193 : {
194 78 : uint32 newSize = reclength;
195 :
196 78 : newSize += XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ);
197 78 : newSize = Max(newSize, 5 * Max(BLCKSZ, XLOG_BLCKSZ));
198 :
199 78 : if (state->readRecordBuf)
200 0 : pfree(state->readRecordBuf);
201 78 : state->readRecordBuf = (char *) palloc(newSize);
202 78 : state->readRecordBufSize = newSize;
203 78 : }
204 :
205 : /*
206 : * Initialize the passed segment structs.
207 : */
208 : static void
209 78 : WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt,
210 : int segsize, const char *waldir)
211 : {
212 78 : seg->ws_file = -1;
213 78 : seg->ws_segno = 0;
214 78 : seg->ws_tli = 0;
215 :
216 78 : segcxt->ws_segsize = segsize;
217 78 : if (waldir)
218 78 : snprintf(segcxt->ws_dir, MAXPGPATH, "%s", waldir);
219 78 : }
220 :
221 : /*
222 : * Begin reading WAL at 'RecPtr'.
223 : *
224 : * 'RecPtr' should point to the beginning of a valid WAL record. Pointing at
225 : * the beginning of a page is also OK, if there is a new record right after
226 : * the page header, i.e. not a continuation.
227 : *
228 : * This does not make any attempt to read the WAL yet, and hence cannot fail.
229 : * If the starting address is not correct, the first call to XLogReadRecord()
230 : * will error out.
231 : */
232 : void
233 5200 : XLogBeginRead(XLogReaderState *state, XLogRecPtr RecPtr)
234 : {
235 : Assert(!XLogRecPtrIsInvalid(RecPtr));
236 :
237 5200 : ResetDecoder(state);
238 :
239 : /* Begin at the passed-in record pointer. */
240 5200 : state->EndRecPtr = RecPtr;
241 5200 : state->NextRecPtr = RecPtr;
242 5200 : state->ReadRecPtr = InvalidXLogRecPtr;
243 5200 : state->DecodeRecPtr = InvalidXLogRecPtr;
244 5200 : }
245 :
246 : /*
247 : * Release the last record that was returned by XLogNextRecord(), if any, to
248 : * free up space. Returns the LSN past the end of the record.
249 : */
250 : XLogRecPtr
251 357088 : XLogReleasePreviousRecord(XLogReaderState *state)
252 : {
253 : DecodedXLogRecord *record;
254 : XLogRecPtr next_lsn;
255 :
256 357088 : if (!state->record)
257 183744 : return InvalidXLogRecPtr;
258 :
259 : /*
260 : * Remove it from the decoded record queue. It must be the oldest item
261 : * decoded, decode_queue_head.
262 : */
263 173344 : record = state->record;
264 173344 : next_lsn = record->next_lsn;
265 : Assert(record == state->decode_queue_head);
266 173344 : state->record = NULL;
267 173344 : state->decode_queue_head = record->next;
268 :
269 : /* It might also be the newest item decoded, decode_queue_tail. */
270 173344 : if (state->decode_queue_tail == record)
271 173344 : state->decode_queue_tail = NULL;
272 :
273 : /* Release the space. */
274 173344 : if (unlikely(record->oversized))
275 : {
276 : /* It's not in the decode buffer, so free it to release space. */
277 0 : pfree(record);
278 : }
279 : else
280 : {
281 : /* It must be the head (oldest) record in the decode buffer. */
282 : Assert(state->decode_buffer_head == (char *) record);
283 :
284 : /*
285 : * We need to update head to point to the next record that is in the
286 : * decode buffer, if any, being careful to skip oversized ones
287 : * (they're not in the decode buffer).
288 : */
289 173344 : record = record->next;
290 173344 : while (unlikely(record && record->oversized))
291 0 : record = record->next;
292 :
293 173344 : if (record)
294 : {
295 : /* Adjust head to release space up to the next record. */
296 0 : state->decode_buffer_head = (char *) record;
297 : }
298 : else
299 : {
300 : /*
301 : * Otherwise we might as well just reset head and tail to the
302 : * start of the buffer space, because we're empty. This means
303 : * we'll keep overwriting the same piece of memory if we're not
304 : * doing any prefetching.
305 : */
306 173344 : state->decode_buffer_head = state->decode_buffer;
307 173344 : state->decode_buffer_tail = state->decode_buffer;
308 : }
309 : }
310 :
311 173344 : return next_lsn;
312 : }
313 :
314 : /*
315 : * Attempt to read an XLOG record.
316 : *
317 : * XLogBeginRead() or XLogFindNextRecord() and then XLogReadAhead() must be
318 : * called before the first call to XLogNextRecord(). This functions returns
319 : * records and errors that were put into an internal queue by XLogReadAhead().
320 : *
321 : * On success, a record is returned.
322 : *
323 : * The returned record (or *errormsg) points to an internal buffer that's
324 : * valid until the next call to XLogNextRecord.
325 : */
326 : DecodedXLogRecord *
327 178544 : XLogNextRecord(XLogReaderState *state, char **errormsg)
328 : {
329 : /* Release the last record returned by XLogNextRecord(). */
330 178544 : XLogReleasePreviousRecord(state);
331 :
332 178544 : if (state->decode_queue_head == NULL)
333 : {
334 0 : *errormsg = NULL;
335 0 : if (state->errormsg_deferred)
336 : {
337 0 : if (state->errormsg_buf[0] != '\0')
338 0 : *errormsg = state->errormsg_buf;
339 0 : state->errormsg_deferred = false;
340 : }
341 :
342 : /*
343 : * state->EndRecPtr is expected to have been set by the last call to
344 : * XLogBeginRead() or XLogNextRecord(), and is the location of the
345 : * error.
346 : */
347 : Assert(!XLogRecPtrIsInvalid(state->EndRecPtr));
348 :
349 0 : return NULL;
350 : }
351 :
352 : /*
353 : * Record this as the most recent record returned, so that we'll release
354 : * it next time. This also exposes it to the traditional
355 : * XLogRecXXX(xlogreader) macros, which work with the decoder rather than
356 : * the record for historical reasons.
357 : */
358 178544 : state->record = state->decode_queue_head;
359 :
360 : /*
361 : * Update the pointers to the beginning and one-past-the-end of this
362 : * record, again for the benefit of historical code that expected the
363 : * decoder to track this rather than accessing these fields of the record
364 : * itself.
365 : */
366 178544 : state->ReadRecPtr = state->record->lsn;
367 178544 : state->EndRecPtr = state->record->next_lsn;
368 :
369 178544 : *errormsg = NULL;
370 :
371 178544 : return state->record;
372 : }
373 :
374 : /*
375 : * Attempt to read an XLOG record.
376 : *
377 : * XLogBeginRead() or XLogFindNextRecord() must be called before the first call
378 : * to XLogReadRecord().
379 : *
380 : * If the page_read callback fails to read the requested data, NULL is
381 : * returned. The callback is expected to have reported the error; errormsg
382 : * is set to NULL.
383 : *
384 : * If the reading fails for some other reason, NULL is also returned, and
385 : * *errormsg is set to a string with details of the failure.
386 : *
387 : * The returned pointer (or *errormsg) points to an internal buffer that's
388 : * valid until the next call to XLogReadRecord.
389 : */
390 : XLogRecord *
391 178544 : XLogReadRecord(XLogReaderState *state, char **errormsg)
392 : {
393 : DecodedXLogRecord *decoded;
394 :
395 : /*
396 : * Release last returned record, if there is one. We need to do this so
397 : * that we can check for empty decode queue accurately.
398 : */
399 178544 : XLogReleasePreviousRecord(state);
400 :
401 : /*
402 : * Call XLogReadAhead() in blocking mode to make sure there is something
403 : * in the queue, though we don't use the result.
404 : */
405 178544 : if (!XLogReaderHasQueuedRecordOrError(state))
406 178544 : XLogReadAhead(state, false /* nonblocking */ );
407 :
408 : /* Consume the head record or error. */
409 178544 : decoded = XLogNextRecord(state, errormsg);
410 178544 : if (decoded)
411 : {
412 : /*
413 : * This function returns a pointer to the record's header, not the
414 : * actual decoded record. The caller will access the decoded record
415 : * through the XLogRecGetXXX() macros, which reach the decoded
416 : * recorded as xlogreader->record.
417 : */
418 : Assert(state->record == decoded);
419 178544 : return &decoded->header;
420 : }
421 :
422 0 : return NULL;
423 : }
424 :
425 : /*
426 : * Allocate space for a decoded record. The only member of the returned
427 : * object that is initialized is the 'oversized' flag, indicating that the
428 : * decoded record wouldn't fit in the decode buffer and must eventually be
429 : * freed explicitly.
430 : *
431 : * The caller is responsible for adjusting decode_buffer_tail with the real
432 : * size after successfully decoding a record into this space. This way, if
433 : * decoding fails, then there is nothing to undo unless the 'oversized' flag
434 : * was set and pfree() must be called.
435 : *
436 : * Return NULL if there is no space in the decode buffer and allow_oversized
437 : * is false, or if memory allocation fails for an oversized buffer.
438 : */
439 : static DecodedXLogRecord *
440 178544 : XLogReadRecordAlloc(XLogReaderState *state, size_t xl_tot_len, bool allow_oversized)
441 : {
442 178544 : size_t required_space = DecodeXLogRecordRequiredSpace(xl_tot_len);
443 178544 : DecodedXLogRecord *decoded = NULL;
444 :
445 : /* Allocate a circular decode buffer if we don't have one already. */
446 178544 : if (unlikely(state->decode_buffer == NULL))
447 : {
448 78 : if (state->decode_buffer_size == 0)
449 78 : state->decode_buffer_size = DEFAULT_DECODE_BUFFER_SIZE;
450 78 : state->decode_buffer = palloc(state->decode_buffer_size);
451 78 : state->decode_buffer_head = state->decode_buffer;
452 78 : state->decode_buffer_tail = state->decode_buffer;
453 78 : state->free_decode_buffer = true;
454 : }
455 :
456 : /* Try to allocate space in the circular decode buffer. */
457 178544 : if (state->decode_buffer_tail >= state->decode_buffer_head)
458 : {
459 : /* Empty, or tail is to the right of head. */
460 178544 : if (required_space <=
461 178544 : state->decode_buffer_size -
462 178544 : (state->decode_buffer_tail - state->decode_buffer))
463 : {
464 : /*-
465 : * There is space between tail and end.
466 : *
467 : * +-----+--------------------+-----+
468 : * | |////////////////////|here!|
469 : * +-----+--------------------+-----+
470 : * ^ ^
471 : * | |
472 : * h t
473 : */
474 178544 : decoded = (DecodedXLogRecord *) state->decode_buffer_tail;
475 178544 : decoded->oversized = false;
476 178544 : return decoded;
477 : }
478 0 : else if (required_space <
479 0 : state->decode_buffer_head - state->decode_buffer)
480 : {
481 : /*-
482 : * There is space between start and head.
483 : *
484 : * +-----+--------------------+-----+
485 : * |here!|////////////////////| |
486 : * +-----+--------------------+-----+
487 : * ^ ^
488 : * | |
489 : * h t
490 : */
491 0 : decoded = (DecodedXLogRecord *) state->decode_buffer;
492 0 : decoded->oversized = false;
493 0 : return decoded;
494 : }
495 : }
496 : else
497 : {
498 : /* Tail is to the left of head. */
499 0 : if (required_space <
500 0 : state->decode_buffer_head - state->decode_buffer_tail)
501 : {
502 : /*-
503 : * There is space between tail and head.
504 : *
505 : * +-----+--------------------+-----+
506 : * |/////|here! |/////|
507 : * +-----+--------------------+-----+
508 : * ^ ^
509 : * | |
510 : * t h
511 : */
512 0 : decoded = (DecodedXLogRecord *) state->decode_buffer_tail;
513 0 : decoded->oversized = false;
514 0 : return decoded;
515 : }
516 : }
517 :
518 : /* Not enough space in the decode buffer. Are we allowed to allocate? */
519 0 : if (allow_oversized)
520 : {
521 0 : decoded = palloc(required_space);
522 0 : decoded->oversized = true;
523 0 : return decoded;
524 : }
525 :
526 0 : return NULL;
527 : }
528 :
529 : static XLogPageReadResult
530 178544 : XLogDecodeNextRecord(XLogReaderState *state, bool nonblocking)
531 : {
532 : XLogRecPtr RecPtr;
533 : XLogRecord *record;
534 : XLogRecPtr targetPagePtr;
535 : bool randAccess;
536 : uint32 len,
537 : total_len;
538 : uint32 targetRecOff;
539 : uint32 pageHeaderSize;
540 : bool assembled;
541 : bool gotheader;
542 : int readOff;
543 : DecodedXLogRecord *decoded;
544 : char *errormsg; /* not used */
545 :
546 : /*
547 : * randAccess indicates whether to verify the previous-record pointer of
548 : * the record we're reading. We only do this if we're reading
549 : * sequentially, which is what we initially assume.
550 : */
551 178544 : randAccess = false;
552 :
553 : /* reset error state */
554 178544 : state->errormsg_buf[0] = '\0';
555 178544 : decoded = NULL;
556 :
557 178544 : state->abortedRecPtr = InvalidXLogRecPtr;
558 178544 : state->missingContrecPtr = InvalidXLogRecPtr;
559 :
560 178544 : RecPtr = state->NextRecPtr;
561 :
562 178544 : if (state->DecodeRecPtr != InvalidXLogRecPtr)
563 : {
564 : /* read the record after the one we just read */
565 :
566 : /*
567 : * NextRecPtr is pointing to end+1 of the previous WAL record. If
568 : * we're at a page boundary, no more records can fit on the current
569 : * page. We must skip over the page header, but we can't do that until
570 : * we've read in the page, since the header size is variable.
571 : */
572 : }
573 : else
574 : {
575 : /*
576 : * Caller supplied a position to start at.
577 : *
578 : * In this case, NextRecPtr should already be pointing either to a
579 : * valid record starting position or alternatively to the beginning of
580 : * a page. See the header comments for XLogBeginRead.
581 : */
582 : Assert(RecPtr % XLOG_BLCKSZ == 0 || XRecOffIsValid(RecPtr));
583 5200 : randAccess = true;
584 : }
585 :
586 178544 : restart:
587 178544 : state->nonblocking = nonblocking;
588 178544 : state->currRecPtr = RecPtr;
589 178544 : assembled = false;
590 :
591 178544 : targetPagePtr = RecPtr - (RecPtr % XLOG_BLCKSZ);
592 178544 : targetRecOff = RecPtr % XLOG_BLCKSZ;
593 :
594 : /*
595 : * Read the page containing the record into state->readBuf. Request enough
596 : * byte to cover the whole record header, or at least the part of it that
597 : * fits on the same page.
598 : */
599 178544 : readOff = ReadPageInternal(state, targetPagePtr,
600 178544 : Min(targetRecOff + SizeOfXLogRecord, XLOG_BLCKSZ));
601 178544 : if (readOff == XLREAD_WOULDBLOCK)
602 0 : return XLREAD_WOULDBLOCK;
603 178544 : else if (readOff < 0)
604 0 : goto err;
605 :
606 : /*
607 : * ReadPageInternal always returns at least the page header, so we can
608 : * examine it now.
609 : */
610 178544 : pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
611 178544 : if (targetRecOff == 0)
612 : {
613 : /*
614 : * At page start, so skip over page header.
615 : */
616 196 : RecPtr += pageHeaderSize;
617 196 : targetRecOff = pageHeaderSize;
618 : }
619 178348 : else if (targetRecOff < pageHeaderSize)
620 : {
621 0 : report_invalid_record(state, "invalid record offset at %X/%X: expected at least %u, got %u",
622 0 : LSN_FORMAT_ARGS(RecPtr),
623 : pageHeaderSize, targetRecOff);
624 0 : goto err;
625 : }
626 :
627 178544 : if ((((XLogPageHeader) state->readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD) &&
628 : targetRecOff == pageHeaderSize)
629 : {
630 0 : report_invalid_record(state, "contrecord is requested by %X/%X",
631 0 : LSN_FORMAT_ARGS(RecPtr));
632 0 : goto err;
633 : }
634 :
635 : /* ReadPageInternal has verified the page header */
636 : Assert(pageHeaderSize <= readOff);
637 :
638 : /*
639 : * Read the record length.
640 : *
641 : * NB: Even though we use an XLogRecord pointer here, the whole record
642 : * header might not fit on this page. xl_tot_len is the first field of the
643 : * struct, so it must be on this page (the records are MAXALIGNed), but we
644 : * cannot access any other fields until we've verified that we got the
645 : * whole header.
646 : */
647 178544 : record = (XLogRecord *) (state->readBuf + RecPtr % XLOG_BLCKSZ);
648 178544 : total_len = record->xl_tot_len;
649 :
650 : /*
651 : * If the whole record header is on this page, validate it immediately.
652 : * Otherwise do just a basic sanity check on xl_tot_len, and validate the
653 : * rest of the header after reading it from the next page. The xl_tot_len
654 : * check is necessary here to ensure that we enter the "Need to reassemble
655 : * record" code path below; otherwise we might fail to apply
656 : * ValidXLogRecordHeader at all.
657 : */
658 178544 : if (targetRecOff <= XLOG_BLCKSZ - SizeOfXLogRecord)
659 : {
660 178220 : if (!ValidXLogRecordHeader(state, RecPtr, state->DecodeRecPtr, record,
661 : randAccess))
662 0 : goto err;
663 178220 : gotheader = true;
664 : }
665 : else
666 : {
667 : /* There may be no next page if it's too small. */
668 324 : if (total_len < SizeOfXLogRecord)
669 : {
670 0 : report_invalid_record(state,
671 : "invalid record length at %X/%X: expected at least %u, got %u",
672 0 : LSN_FORMAT_ARGS(RecPtr),
673 : (uint32) SizeOfXLogRecord, total_len);
674 0 : goto err;
675 : }
676 : /* We'll validate the header once we have the next page. */
677 324 : gotheader = false;
678 : }
679 :
680 : /*
681 : * Try to find space to decode this record, if we can do so without
682 : * calling palloc. If we can't, we'll try again below after we've
683 : * validated that total_len isn't garbage bytes from a recycled WAL page.
684 : */
685 178544 : decoded = XLogReadRecordAlloc(state,
686 : total_len,
687 : false /* allow_oversized */ );
688 178544 : if (decoded == NULL && nonblocking)
689 : {
690 : /*
691 : * There is no space in the circular decode buffer, and the caller is
692 : * only reading ahead. The caller should consume existing records to
693 : * make space.
694 : */
695 0 : return XLREAD_WOULDBLOCK;
696 : }
697 :
698 178544 : len = XLOG_BLCKSZ - RecPtr % XLOG_BLCKSZ;
699 178544 : if (total_len > len)
700 : {
701 : /* Need to reassemble record */
702 : char *contdata;
703 : XLogPageHeader pageHeader;
704 : char *buffer;
705 : uint32 gotlen;
706 :
707 8604 : assembled = true;
708 :
709 : /*
710 : * We always have space for a couple of pages, enough to validate a
711 : * boundary-spanning record header.
712 : */
713 : Assert(state->readRecordBufSize >= XLOG_BLCKSZ * 2);
714 : Assert(state->readRecordBufSize >= len);
715 :
716 : /* Copy the first fragment of the record from the first page. */
717 8604 : memcpy(state->readRecordBuf,
718 8604 : state->readBuf + RecPtr % XLOG_BLCKSZ, len);
719 8604 : buffer = state->readRecordBuf + len;
720 8604 : gotlen = len;
721 :
722 : do
723 : {
724 : /* Calculate pointer to beginning of next page */
725 8608 : targetPagePtr += XLOG_BLCKSZ;
726 :
727 : /* Wait for the next page to become available */
728 8608 : readOff = ReadPageInternal(state, targetPagePtr,
729 8608 : Min(total_len - gotlen + SizeOfXLogShortPHD,
730 : XLOG_BLCKSZ));
731 :
732 8608 : if (readOff == XLREAD_WOULDBLOCK)
733 0 : return XLREAD_WOULDBLOCK;
734 8608 : else if (readOff < 0)
735 0 : goto err;
736 :
737 : Assert(SizeOfXLogShortPHD <= readOff);
738 :
739 8608 : pageHeader = (XLogPageHeader) state->readBuf;
740 :
741 : /*
742 : * If we were expecting a continuation record and got an
743 : * "overwrite contrecord" flag, that means the continuation record
744 : * was overwritten with a different record. Restart the read by
745 : * assuming the address to read is the location where we found
746 : * this flag; but keep track of the LSN of the record we were
747 : * reading, for later verification.
748 : */
749 8608 : if (pageHeader->xlp_info & XLP_FIRST_IS_OVERWRITE_CONTRECORD)
750 : {
751 0 : state->overwrittenRecPtr = RecPtr;
752 0 : RecPtr = targetPagePtr;
753 0 : goto restart;
754 : }
755 :
756 : /* Check that the continuation on next page looks valid */
757 8608 : if (!(pageHeader->xlp_info & XLP_FIRST_IS_CONTRECORD))
758 : {
759 0 : report_invalid_record(state,
760 : "there is no contrecord flag at %X/%X",
761 0 : LSN_FORMAT_ARGS(RecPtr));
762 0 : goto err;
763 : }
764 :
765 : /*
766 : * Cross-check that xlp_rem_len agrees with how much of the record
767 : * we expect there to be left.
768 : */
769 8608 : if (pageHeader->xlp_rem_len == 0 ||
770 8608 : total_len != (pageHeader->xlp_rem_len + gotlen))
771 : {
772 0 : report_invalid_record(state,
773 : "invalid contrecord length %u (expected %lld) at %X/%X",
774 : pageHeader->xlp_rem_len,
775 0 : ((long long) total_len) - gotlen,
776 0 : LSN_FORMAT_ARGS(RecPtr));
777 0 : goto err;
778 : }
779 :
780 : /* Append the continuation from this page to the buffer */
781 8608 : pageHeaderSize = XLogPageHeaderSize(pageHeader);
782 :
783 8608 : if (readOff < pageHeaderSize)
784 0 : readOff = ReadPageInternal(state, targetPagePtr,
785 : pageHeaderSize);
786 :
787 : Assert(pageHeaderSize <= readOff);
788 :
789 8608 : contdata = (char *) state->readBuf + pageHeaderSize;
790 8608 : len = XLOG_BLCKSZ - pageHeaderSize;
791 8608 : if (pageHeader->xlp_rem_len < len)
792 8604 : len = pageHeader->xlp_rem_len;
793 :
794 8608 : if (readOff < pageHeaderSize + len)
795 0 : readOff = ReadPageInternal(state, targetPagePtr,
796 0 : pageHeaderSize + len);
797 :
798 8608 : memcpy(buffer, (char *) contdata, len);
799 8608 : buffer += len;
800 8608 : gotlen += len;
801 :
802 : /* If we just reassembled the record header, validate it. */
803 8608 : if (!gotheader)
804 : {
805 324 : record = (XLogRecord *) state->readRecordBuf;
806 324 : if (!ValidXLogRecordHeader(state, RecPtr, state->DecodeRecPtr,
807 : record, randAccess))
808 0 : goto err;
809 324 : gotheader = true;
810 : }
811 :
812 : /*
813 : * We might need a bigger buffer. We have validated the record
814 : * header, in the case that it split over a page boundary. We've
815 : * also cross-checked total_len against xlp_rem_len on the second
816 : * page, and verified xlp_pageaddr on both.
817 : */
818 8608 : if (total_len > state->readRecordBufSize)
819 : {
820 : char save_copy[XLOG_BLCKSZ * 2];
821 :
822 : /*
823 : * Save and restore the data we already had. It can't be more
824 : * than two pages.
825 : */
826 : Assert(gotlen <= lengthof(save_copy));
827 : Assert(gotlen <= state->readRecordBufSize);
828 0 : memcpy(save_copy, state->readRecordBuf, gotlen);
829 0 : allocate_recordbuf(state, total_len);
830 0 : memcpy(state->readRecordBuf, save_copy, gotlen);
831 0 : buffer = state->readRecordBuf + gotlen;
832 : }
833 8608 : } while (gotlen < total_len);
834 : Assert(gotheader);
835 :
836 8604 : record = (XLogRecord *) state->readRecordBuf;
837 8604 : if (!ValidXLogRecord(state, record, RecPtr))
838 0 : goto err;
839 :
840 8604 : pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
841 8604 : state->DecodeRecPtr = RecPtr;
842 8604 : state->NextRecPtr = targetPagePtr + pageHeaderSize
843 8604 : + MAXALIGN(pageHeader->xlp_rem_len);
844 : }
845 : else
846 : {
847 : /* Wait for the record data to become available */
848 169940 : readOff = ReadPageInternal(state, targetPagePtr,
849 169940 : Min(targetRecOff + total_len, XLOG_BLCKSZ));
850 169940 : if (readOff == XLREAD_WOULDBLOCK)
851 0 : return XLREAD_WOULDBLOCK;
852 169940 : else if (readOff < 0)
853 0 : goto err;
854 :
855 : /* Record does not cross a page boundary */
856 169940 : if (!ValidXLogRecord(state, record, RecPtr))
857 0 : goto err;
858 :
859 169940 : state->NextRecPtr = RecPtr + MAXALIGN(total_len);
860 :
861 169940 : state->DecodeRecPtr = RecPtr;
862 : }
863 :
864 : /*
865 : * Special processing if it's an XLOG SWITCH record
866 : */
867 178544 : if (record->xl_rmid == RM_XLOG_ID &&
868 12070 : (record->xl_info & ~XLR_INFO_MASK) == XLOG_SWITCH)
869 : {
870 : /* Pretend it extends to end of segment */
871 16 : state->NextRecPtr += state->segcxt.ws_segsize - 1;
872 16 : state->NextRecPtr -= XLogSegmentOffset(state->NextRecPtr, state->segcxt.ws_segsize);
873 : }
874 :
875 : /*
876 : * If we got here without a DecodedXLogRecord, it means we needed to
877 : * validate total_len before trusting it, but by now we've done that.
878 : */
879 178544 : if (decoded == NULL)
880 : {
881 : Assert(!nonblocking);
882 0 : decoded = XLogReadRecordAlloc(state,
883 : total_len,
884 : true /* allow_oversized */ );
885 : /* allocation should always happen under allow_oversized */
886 : Assert(decoded != NULL);
887 : }
888 :
889 178544 : if (DecodeXLogRecord(state, decoded, record, RecPtr, &errormsg))
890 : {
891 : /* Record the location of the next record. */
892 178544 : decoded->next_lsn = state->NextRecPtr;
893 :
894 : /*
895 : * If it's in the decode buffer, mark the decode buffer space as
896 : * occupied.
897 : */
898 178544 : if (!decoded->oversized)
899 : {
900 : /* The new decode buffer head must be MAXALIGNed. */
901 : Assert(decoded->size == MAXALIGN(decoded->size));
902 178544 : if ((char *) decoded == state->decode_buffer)
903 178544 : state->decode_buffer_tail = state->decode_buffer + decoded->size;
904 : else
905 0 : state->decode_buffer_tail += decoded->size;
906 : }
907 :
908 : /* Insert it into the queue of decoded records. */
909 : Assert(state->decode_queue_tail != decoded);
910 178544 : if (state->decode_queue_tail)
911 0 : state->decode_queue_tail->next = decoded;
912 178544 : state->decode_queue_tail = decoded;
913 178544 : if (!state->decode_queue_head)
914 178544 : state->decode_queue_head = decoded;
915 178544 : return XLREAD_SUCCESS;
916 : }
917 :
918 0 : err:
919 0 : if (assembled)
920 : {
921 : /*
922 : * We get here when a record that spans multiple pages needs to be
923 : * assembled, but something went wrong -- perhaps a contrecord piece
924 : * was lost. If caller is WAL replay, it will know where the aborted
925 : * record was and where to direct followup WAL to be written, marking
926 : * the next piece with XLP_FIRST_IS_OVERWRITE_CONTRECORD, which will
927 : * in turn signal downstream WAL consumers that the broken WAL record
928 : * is to be ignored.
929 : */
930 0 : state->abortedRecPtr = RecPtr;
931 0 : state->missingContrecPtr = targetPagePtr;
932 :
933 : /*
934 : * If we got here without reporting an error, make sure an error is
935 : * queued so that XLogPrefetcherReadRecord() doesn't bring us back a
936 : * second time and clobber the above state.
937 : */
938 0 : state->errormsg_deferred = true;
939 : }
940 :
941 0 : if (decoded && decoded->oversized)
942 0 : pfree(decoded);
943 :
944 : /*
945 : * Invalidate the read state. We might read from a different source after
946 : * failure.
947 : */
948 0 : XLogReaderInvalReadState(state);
949 :
950 : /*
951 : * If an error was written to errmsg_buf, it'll be returned to the caller
952 : * of XLogReadRecord() after all successfully decoded records from the
953 : * read queue.
954 : */
955 :
956 0 : return XLREAD_FAIL;
957 : }
958 :
959 : /*
960 : * Try to decode the next available record, and return it. The record will
961 : * also be returned to XLogNextRecord(), which must be called to 'consume'
962 : * each record.
963 : *
964 : * If nonblocking is true, may return NULL due to lack of data or WAL decoding
965 : * space.
966 : */
967 : DecodedXLogRecord *
968 178544 : XLogReadAhead(XLogReaderState *state, bool nonblocking)
969 : {
970 : XLogPageReadResult result;
971 :
972 178544 : if (state->errormsg_deferred)
973 0 : return NULL;
974 :
975 178544 : result = XLogDecodeNextRecord(state, nonblocking);
976 178544 : if (result == XLREAD_SUCCESS)
977 : {
978 : Assert(state->decode_queue_tail != NULL);
979 178544 : return state->decode_queue_tail;
980 : }
981 :
982 0 : return NULL;
983 : }
984 :
985 : /*
986 : * Read a single xlog page including at least [pageptr, reqLen] of valid data
987 : * via the page_read() callback.
988 : *
989 : * Returns XLREAD_FAIL if the required page cannot be read for some
990 : * reason; errormsg_buf is set in that case (unless the error occurs in the
991 : * page_read callback).
992 : *
993 : * Returns XLREAD_WOULDBLOCK if the requested data can't be read without
994 : * waiting. This can be returned only if the installed page_read callback
995 : * respects the state->nonblocking flag, and cannot read the requested data
996 : * immediately.
997 : *
998 : * We fetch the page from a reader-local cache if we know we have the required
999 : * data and if there hasn't been any error since caching the data.
1000 : */
1001 : static int
1002 357092 : ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
1003 : {
1004 : int readLen;
1005 : uint32 targetPageOff;
1006 : XLogSegNo targetSegNo;
1007 : XLogPageHeader hdr;
1008 :
1009 : Assert((pageptr % XLOG_BLCKSZ) == 0);
1010 :
1011 357092 : XLByteToSeg(pageptr, targetSegNo, state->segcxt.ws_segsize);
1012 357092 : targetPageOff = XLogSegmentOffset(pageptr, state->segcxt.ws_segsize);
1013 :
1014 : /* check whether we have all the requested data already */
1015 357092 : if (targetSegNo == state->seg.ws_segno &&
1016 356998 : targetPageOff == state->segoff && reqLen <= state->readLen)
1017 345620 : return state->readLen;
1018 :
1019 : /*
1020 : * Invalidate contents of internal buffer before read attempt. Just set
1021 : * the length to 0, rather than a full XLogReaderInvalReadState(), so we
1022 : * don't forget the segment we last successfully read.
1023 : */
1024 11472 : state->readLen = 0;
1025 :
1026 : /*
1027 : * Data is not in our buffer.
1028 : *
1029 : * Every time we actually read the segment, even if we looked at parts of
1030 : * it before, we need to do verification as the page_read callback might
1031 : * now be rereading data from a different source.
1032 : *
1033 : * Whenever switching to a new WAL segment, we read the first page of the
1034 : * file and validate its header, even if that's not where the target
1035 : * record is. This is so that we can check the additional identification
1036 : * info that is present in the first page's "long" header.
1037 : */
1038 11472 : if (targetSegNo != state->seg.ws_segno && targetPageOff != 0)
1039 : {
1040 46 : XLogRecPtr targetSegmentPtr = pageptr - targetPageOff;
1041 :
1042 46 : readLen = state->routine.page_read(state, targetSegmentPtr, XLOG_BLCKSZ,
1043 : state->currRecPtr,
1044 : state->readBuf);
1045 46 : if (readLen == XLREAD_WOULDBLOCK)
1046 0 : return XLREAD_WOULDBLOCK;
1047 46 : else if (readLen < 0)
1048 0 : goto err;
1049 :
1050 : /* we can be sure to have enough WAL available, we scrolled back */
1051 : Assert(readLen == XLOG_BLCKSZ);
1052 :
1053 46 : if (!XLogReaderValidatePageHeader(state, targetSegmentPtr,
1054 : state->readBuf))
1055 0 : goto err;
1056 : }
1057 :
1058 : /*
1059 : * First, read the requested data length, but at least a short page header
1060 : * so that we can validate it.
1061 : */
1062 11472 : readLen = state->routine.page_read(state, pageptr, Max(reqLen, SizeOfXLogShortPHD),
1063 : state->currRecPtr,
1064 : state->readBuf);
1065 11472 : if (readLen == XLREAD_WOULDBLOCK)
1066 0 : return XLREAD_WOULDBLOCK;
1067 11472 : else if (readLen < 0)
1068 0 : goto err;
1069 :
1070 : Assert(readLen <= XLOG_BLCKSZ);
1071 :
1072 : /* Do we have enough data to check the header length? */
1073 11472 : if (readLen <= SizeOfXLogShortPHD)
1074 0 : goto err;
1075 :
1076 : Assert(readLen >= reqLen);
1077 :
1078 11472 : hdr = (XLogPageHeader) state->readBuf;
1079 :
1080 : /* still not enough */
1081 11472 : if (readLen < XLogPageHeaderSize(hdr))
1082 : {
1083 0 : readLen = state->routine.page_read(state, pageptr, XLogPageHeaderSize(hdr),
1084 : state->currRecPtr,
1085 : state->readBuf);
1086 0 : if (readLen == XLREAD_WOULDBLOCK)
1087 0 : return XLREAD_WOULDBLOCK;
1088 0 : else if (readLen < 0)
1089 0 : goto err;
1090 : }
1091 :
1092 : /*
1093 : * Now that we know we have the full header, validate it.
1094 : */
1095 11472 : if (!XLogReaderValidatePageHeader(state, pageptr, (char *) hdr))
1096 0 : goto err;
1097 :
1098 : /* update read state information */
1099 11472 : state->seg.ws_segno = targetSegNo;
1100 11472 : state->segoff = targetPageOff;
1101 11472 : state->readLen = readLen;
1102 :
1103 11472 : return readLen;
1104 :
1105 0 : err:
1106 0 : XLogReaderInvalReadState(state);
1107 :
1108 0 : return XLREAD_FAIL;
1109 : }
1110 :
1111 : /*
1112 : * Invalidate the xlogreader's read state to force a re-read.
1113 : */
1114 : static void
1115 0 : XLogReaderInvalReadState(XLogReaderState *state)
1116 : {
1117 0 : state->seg.ws_segno = 0;
1118 0 : state->segoff = 0;
1119 0 : state->readLen = 0;
1120 0 : }
1121 :
1122 : /*
1123 : * Validate an XLOG record header.
1124 : *
1125 : * This is just a convenience subroutine to avoid duplicated code in
1126 : * XLogReadRecord. It's not intended for use from anywhere else.
1127 : */
1128 : static bool
1129 178544 : ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr,
1130 : XLogRecPtr PrevRecPtr, XLogRecord *record,
1131 : bool randAccess)
1132 : {
1133 178544 : if (record->xl_tot_len < SizeOfXLogRecord)
1134 : {
1135 0 : report_invalid_record(state,
1136 : "invalid record length at %X/%X: expected at least %u, got %u",
1137 0 : LSN_FORMAT_ARGS(RecPtr),
1138 : (uint32) SizeOfXLogRecord, record->xl_tot_len);
1139 0 : return false;
1140 : }
1141 178544 : if (!RmgrIdIsValid(record->xl_rmid))
1142 : {
1143 0 : report_invalid_record(state,
1144 : "invalid resource manager ID %u at %X/%X",
1145 0 : record->xl_rmid, LSN_FORMAT_ARGS(RecPtr));
1146 0 : return false;
1147 : }
1148 178544 : if (randAccess)
1149 : {
1150 : /*
1151 : * We can't exactly verify the prev-link, but surely it should be less
1152 : * than the record's own address.
1153 : */
1154 5200 : if (!(record->xl_prev < RecPtr))
1155 : {
1156 0 : report_invalid_record(state,
1157 : "record with incorrect prev-link %X/%X at %X/%X",
1158 0 : LSN_FORMAT_ARGS(record->xl_prev),
1159 0 : LSN_FORMAT_ARGS(RecPtr));
1160 0 : return false;
1161 : }
1162 : }
1163 : else
1164 : {
1165 : /*
1166 : * Record's prev-link should exactly match our previous location. This
1167 : * check guards against torn WAL pages where a stale but valid-looking
1168 : * WAL record starts on a sector boundary.
1169 : */
1170 173344 : if (record->xl_prev != PrevRecPtr)
1171 : {
1172 0 : report_invalid_record(state,
1173 : "record with incorrect prev-link %X/%X at %X/%X",
1174 0 : LSN_FORMAT_ARGS(record->xl_prev),
1175 0 : LSN_FORMAT_ARGS(RecPtr));
1176 0 : return false;
1177 : }
1178 : }
1179 :
1180 178544 : return true;
1181 : }
1182 :
1183 :
1184 : /*
1185 : * CRC-check an XLOG record. We do not believe the contents of an XLOG
1186 : * record (other than to the minimal extent of computing the amount of
1187 : * data to read in) until we've checked the CRCs.
1188 : *
1189 : * We assume all of the record (that is, xl_tot_len bytes) has been read
1190 : * into memory at *record. Also, ValidXLogRecordHeader() has accepted the
1191 : * record's header, which means in particular that xl_tot_len is at least
1192 : * SizeOfXLogRecord.
1193 : */
1194 : static bool
1195 178544 : ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
1196 : {
1197 : pg_crc32c crc;
1198 :
1199 : Assert(record->xl_tot_len >= SizeOfXLogRecord);
1200 :
1201 : /* Calculate the CRC */
1202 178544 : INIT_CRC32C(crc);
1203 178544 : COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
1204 : /* include the record header last */
1205 178544 : COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
1206 178544 : FIN_CRC32C(crc);
1207 :
1208 178544 : if (!EQ_CRC32C(record->xl_crc, crc))
1209 : {
1210 0 : report_invalid_record(state,
1211 : "incorrect resource manager data checksum in record at %X/%X",
1212 0 : LSN_FORMAT_ARGS(recptr));
1213 0 : return false;
1214 : }
1215 :
1216 178544 : return true;
1217 : }
1218 :
1219 : /*
1220 : * Validate a page header.
1221 : *
1222 : * Check if 'phdr' is valid as the header of the XLog page at position
1223 : * 'recptr'.
1224 : */
1225 : bool
1226 11518 : XLogReaderValidatePageHeader(XLogReaderState *state, XLogRecPtr recptr,
1227 : char *phdr)
1228 : {
1229 : XLogSegNo segno;
1230 : int32 offset;
1231 11518 : XLogPageHeader hdr = (XLogPageHeader) phdr;
1232 :
1233 : Assert((recptr % XLOG_BLCKSZ) == 0);
1234 :
1235 11518 : XLByteToSeg(recptr, segno, state->segcxt.ws_segsize);
1236 11518 : offset = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
1237 :
1238 11518 : if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
1239 : {
1240 : char fname[MAXFNAMELEN];
1241 :
1242 0 : XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1243 :
1244 0 : report_invalid_record(state,
1245 : "invalid magic number %04X in WAL segment %s, LSN %X/%X, offset %u",
1246 0 : hdr->xlp_magic,
1247 : fname,
1248 0 : LSN_FORMAT_ARGS(recptr),
1249 : offset);
1250 0 : return false;
1251 : }
1252 :
1253 11518 : if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
1254 : {
1255 : char fname[MAXFNAMELEN];
1256 :
1257 0 : XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1258 :
1259 0 : report_invalid_record(state,
1260 : "invalid info bits %04X in WAL segment %s, LSN %X/%X, offset %u",
1261 0 : hdr->xlp_info,
1262 : fname,
1263 0 : LSN_FORMAT_ARGS(recptr),
1264 : offset);
1265 0 : return false;
1266 : }
1267 :
1268 11518 : if (hdr->xlp_info & XLP_LONG_HEADER)
1269 : {
1270 112 : XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
1271 :
1272 112 : if (state->system_identifier &&
1273 0 : longhdr->xlp_sysid != state->system_identifier)
1274 : {
1275 0 : report_invalid_record(state,
1276 : "WAL file is from different database system: WAL file database system identifier is %llu, pg_control database system identifier is %llu",
1277 0 : (unsigned long long) longhdr->xlp_sysid,
1278 0 : (unsigned long long) state->system_identifier);
1279 0 : return false;
1280 : }
1281 112 : else if (longhdr->xlp_seg_size != state->segcxt.ws_segsize)
1282 : {
1283 0 : report_invalid_record(state,
1284 : "WAL file is from different database system: incorrect segment size in page header");
1285 0 : return false;
1286 : }
1287 112 : else if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
1288 : {
1289 0 : report_invalid_record(state,
1290 : "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
1291 0 : return false;
1292 : }
1293 : }
1294 11406 : else if (offset == 0)
1295 : {
1296 : char fname[MAXFNAMELEN];
1297 :
1298 0 : XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1299 :
1300 : /* hmm, first page of file doesn't have a long header? */
1301 0 : report_invalid_record(state,
1302 : "invalid info bits %04X in WAL segment %s, LSN %X/%X, offset %u",
1303 0 : hdr->xlp_info,
1304 : fname,
1305 0 : LSN_FORMAT_ARGS(recptr),
1306 : offset);
1307 0 : return false;
1308 : }
1309 :
1310 : /*
1311 : * Check that the address on the page agrees with what we expected. This
1312 : * check typically fails when an old WAL segment is recycled, and hasn't
1313 : * yet been overwritten with new data yet.
1314 : */
1315 11518 : if (hdr->xlp_pageaddr != recptr)
1316 : {
1317 : char fname[MAXFNAMELEN];
1318 :
1319 0 : XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1320 :
1321 0 : report_invalid_record(state,
1322 : "unexpected pageaddr %X/%X in WAL segment %s, LSN %X/%X, offset %u",
1323 0 : LSN_FORMAT_ARGS(hdr->xlp_pageaddr),
1324 : fname,
1325 0 : LSN_FORMAT_ARGS(recptr),
1326 : offset);
1327 0 : return false;
1328 : }
1329 :
1330 : /*
1331 : * Since child timelines are always assigned a TLI greater than their
1332 : * immediate parent's TLI, we should never see TLI go backwards across
1333 : * successive pages of a consistent WAL sequence.
1334 : *
1335 : * Sometimes we re-read a segment that's already been (partially) read. So
1336 : * we only verify TLIs for pages that are later than the last remembered
1337 : * LSN.
1338 : */
1339 11518 : if (recptr > state->latestPagePtr)
1340 : {
1341 8928 : if (hdr->xlp_tli < state->latestPageTLI)
1342 : {
1343 : char fname[MAXFNAMELEN];
1344 :
1345 0 : XLogFileName(fname, state->seg.ws_tli, segno, state->segcxt.ws_segsize);
1346 :
1347 0 : report_invalid_record(state,
1348 : "out-of-sequence timeline ID %u (after %u) in WAL segment %s, LSN %X/%X, offset %u",
1349 : hdr->xlp_tli,
1350 : state->latestPageTLI,
1351 : fname,
1352 0 : LSN_FORMAT_ARGS(recptr),
1353 : offset);
1354 0 : return false;
1355 : }
1356 : }
1357 11518 : state->latestPagePtr = recptr;
1358 11518 : state->latestPageTLI = hdr->xlp_tli;
1359 :
1360 11518 : return true;
1361 : }
1362 :
1363 : /*
1364 : * Forget about an error produced by XLogReaderValidatePageHeader().
1365 : */
1366 : void
1367 0 : XLogReaderResetError(XLogReaderState *state)
1368 : {
1369 0 : state->errormsg_buf[0] = '\0';
1370 0 : state->errormsg_deferred = false;
1371 0 : }
1372 :
1373 : /*
1374 : * Find the first record with an lsn >= RecPtr.
1375 : *
1376 : * This is different from XLogBeginRead() in that RecPtr doesn't need to point
1377 : * to a valid record boundary. Useful for checking whether RecPtr is a valid
1378 : * xlog address for reading, and to find the first valid address after some
1379 : * address when dumping records for debugging purposes.
1380 : *
1381 : * This positions the reader, like XLogBeginRead(), so that the next call to
1382 : * XLogReadRecord() will read the next valid record.
1383 : */
1384 : XLogRecPtr
1385 0 : XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr)
1386 : {
1387 : XLogRecPtr tmpRecPtr;
1388 0 : XLogRecPtr found = InvalidXLogRecPtr;
1389 : XLogPageHeader header;
1390 : char *errormsg;
1391 :
1392 : Assert(!XLogRecPtrIsInvalid(RecPtr));
1393 :
1394 : /* Make sure ReadPageInternal() can't return XLREAD_WOULDBLOCK. */
1395 0 : state->nonblocking = false;
1396 :
1397 : /*
1398 : * skip over potential continuation data, keeping in mind that it may span
1399 : * multiple pages
1400 : */
1401 0 : tmpRecPtr = RecPtr;
1402 : while (true)
1403 0 : {
1404 : XLogRecPtr targetPagePtr;
1405 : int targetRecOff;
1406 : uint32 pageHeaderSize;
1407 : int readLen;
1408 :
1409 : /*
1410 : * Compute targetRecOff. It should typically be equal or greater than
1411 : * short page-header since a valid record can't start anywhere before
1412 : * that, except when caller has explicitly specified the offset that
1413 : * falls somewhere there or when we are skipping multi-page
1414 : * continuation record. It doesn't matter though because
1415 : * ReadPageInternal() is prepared to handle that and will read at
1416 : * least short page-header worth of data
1417 : */
1418 0 : targetRecOff = tmpRecPtr % XLOG_BLCKSZ;
1419 :
1420 : /* scroll back to page boundary */
1421 0 : targetPagePtr = tmpRecPtr - targetRecOff;
1422 :
1423 : /* Read the page containing the record */
1424 0 : readLen = ReadPageInternal(state, targetPagePtr, targetRecOff);
1425 0 : if (readLen < 0)
1426 0 : goto err;
1427 :
1428 0 : header = (XLogPageHeader) state->readBuf;
1429 :
1430 0 : pageHeaderSize = XLogPageHeaderSize(header);
1431 :
1432 : /* make sure we have enough data for the page header */
1433 0 : readLen = ReadPageInternal(state, targetPagePtr, pageHeaderSize);
1434 0 : if (readLen < 0)
1435 0 : goto err;
1436 :
1437 : /* skip over potential continuation data */
1438 0 : if (header->xlp_info & XLP_FIRST_IS_CONTRECORD)
1439 : {
1440 : /*
1441 : * If the length of the remaining continuation data is more than
1442 : * what can fit in this page, the continuation record crosses over
1443 : * this page. Read the next page and try again. xlp_rem_len in the
1444 : * next page header will contain the remaining length of the
1445 : * continuation data
1446 : *
1447 : * Note that record headers are MAXALIGN'ed
1448 : */
1449 0 : if (MAXALIGN(header->xlp_rem_len) >= (XLOG_BLCKSZ - pageHeaderSize))
1450 0 : tmpRecPtr = targetPagePtr + XLOG_BLCKSZ;
1451 : else
1452 : {
1453 : /*
1454 : * The previous continuation record ends in this page. Set
1455 : * tmpRecPtr to point to the first valid record
1456 : */
1457 0 : tmpRecPtr = targetPagePtr + pageHeaderSize
1458 0 : + MAXALIGN(header->xlp_rem_len);
1459 0 : break;
1460 : }
1461 : }
1462 : else
1463 : {
1464 0 : tmpRecPtr = targetPagePtr + pageHeaderSize;
1465 0 : break;
1466 : }
1467 : }
1468 :
1469 : /*
1470 : * we know now that tmpRecPtr is an address pointing to a valid XLogRecord
1471 : * because either we're at the first record after the beginning of a page
1472 : * or we just jumped over the remaining data of a continuation.
1473 : */
1474 0 : XLogBeginRead(state, tmpRecPtr);
1475 0 : while (XLogReadRecord(state, &errormsg) != NULL)
1476 : {
1477 : /* past the record we've found, break out */
1478 0 : if (RecPtr <= state->ReadRecPtr)
1479 : {
1480 : /* Rewind the reader to the beginning of the last record. */
1481 0 : found = state->ReadRecPtr;
1482 0 : XLogBeginRead(state, found);
1483 0 : return found;
1484 : }
1485 : }
1486 :
1487 0 : err:
1488 0 : XLogReaderInvalReadState(state);
1489 :
1490 0 : return InvalidXLogRecPtr;
1491 : }
1492 :
1493 : /*
1494 : * Helper function to ease writing of XLogReaderRoutine->page_read callbacks.
1495 : * If this function is used, caller must supply a segment_open callback in
1496 : * 'state', as that is used here.
1497 : *
1498 : * Read 'count' bytes into 'buf', starting at location 'startptr', from WAL
1499 : * fetched from timeline 'tli'.
1500 : *
1501 : * Returns true if succeeded, false if an error occurs, in which case
1502 : * 'errinfo' receives error details.
1503 : *
1504 : * XXX probably this should be improved to suck data directly from the
1505 : * WAL buffers when possible.
1506 : */
1507 : bool
1508 0 : WALRead(XLogReaderState *state,
1509 : char *buf, XLogRecPtr startptr, Size count, TimeLineID tli,
1510 : WALReadError *errinfo)
1511 : {
1512 : char *p;
1513 : XLogRecPtr recptr;
1514 : Size nbytes;
1515 :
1516 0 : p = buf;
1517 0 : recptr = startptr;
1518 0 : nbytes = count;
1519 :
1520 0 : while (nbytes > 0)
1521 : {
1522 : uint32 startoff;
1523 : int segbytes;
1524 : int readbytes;
1525 :
1526 0 : startoff = XLogSegmentOffset(recptr, state->segcxt.ws_segsize);
1527 :
1528 : /*
1529 : * If the data we want is not in a segment we have open, close what we
1530 : * have (if anything) and open the next one, using the caller's
1531 : * provided segment_open callback.
1532 : */
1533 0 : if (state->seg.ws_file < 0 ||
1534 0 : !XLByteInSeg(recptr, state->seg.ws_segno, state->segcxt.ws_segsize) ||
1535 0 : tli != state->seg.ws_tli)
1536 : {
1537 : XLogSegNo nextSegNo;
1538 :
1539 0 : if (state->seg.ws_file >= 0)
1540 0 : state->routine.segment_close(state);
1541 :
1542 0 : XLByteToSeg(recptr, nextSegNo, state->segcxt.ws_segsize);
1543 0 : state->routine.segment_open(state, nextSegNo, &tli);
1544 :
1545 : /* This shouldn't happen -- indicates a bug in segment_open */
1546 : Assert(state->seg.ws_file >= 0);
1547 :
1548 : /* Update the current segment info. */
1549 0 : state->seg.ws_tli = tli;
1550 0 : state->seg.ws_segno = nextSegNo;
1551 : }
1552 :
1553 : /* How many bytes are within this segment? */
1554 0 : if (nbytes > (state->segcxt.ws_segsize - startoff))
1555 0 : segbytes = state->segcxt.ws_segsize - startoff;
1556 : else
1557 0 : segbytes = nbytes;
1558 :
1559 : #ifndef FRONTEND
1560 : pgstat_report_wait_start(WAIT_EVENT_WAL_READ);
1561 : #endif
1562 :
1563 : /* Reset errno first; eases reporting non-errno-affecting errors */
1564 0 : errno = 0;
1565 0 : readbytes = pg_pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
1566 :
1567 : #ifndef FRONTEND
1568 : pgstat_report_wait_end();
1569 : #endif
1570 :
1571 0 : if (readbytes <= 0)
1572 : {
1573 0 : errinfo->wre_errno = errno;
1574 0 : errinfo->wre_req = segbytes;
1575 0 : errinfo->wre_read = readbytes;
1576 0 : errinfo->wre_off = startoff;
1577 0 : errinfo->wre_seg = state->seg;
1578 0 : return false;
1579 : }
1580 :
1581 : /* Update state for read */
1582 0 : recptr += readbytes;
1583 0 : nbytes -= readbytes;
1584 0 : p += readbytes;
1585 : }
1586 :
1587 0 : return true;
1588 : }
1589 :
1590 : /* ----------------------------------------
1591 : * Functions for decoding the data and block references in a record.
1592 : * ----------------------------------------
1593 : */
1594 :
1595 : /*
1596 : * Private function to reset the state, forgetting all decoded records, if we
1597 : * are asked to move to a new read position.
1598 : */
1599 : static void
1600 5200 : ResetDecoder(XLogReaderState *state)
1601 : {
1602 : DecodedXLogRecord *r;
1603 :
1604 : /* Reset the decoded record queue, freeing any oversized records. */
1605 10322 : while ((r = state->decode_queue_head) != NULL)
1606 : {
1607 5122 : state->decode_queue_head = r->next;
1608 5122 : if (r->oversized)
1609 0 : pfree(r);
1610 : }
1611 5200 : state->decode_queue_tail = NULL;
1612 5200 : state->decode_queue_head = NULL;
1613 5200 : state->record = NULL;
1614 :
1615 : /* Reset the decode buffer to empty. */
1616 5200 : state->decode_buffer_tail = state->decode_buffer;
1617 5200 : state->decode_buffer_head = state->decode_buffer;
1618 :
1619 : /* Clear error state. */
1620 5200 : state->errormsg_buf[0] = '\0';
1621 5200 : state->errormsg_deferred = false;
1622 5200 : }
1623 :
1624 : /*
1625 : * Compute the maximum possible amount of padding that could be required to
1626 : * decode a record, given xl_tot_len from the record's header. This is the
1627 : * amount of output buffer space that we need to decode a record, though we
1628 : * might not finish up using it all.
1629 : *
1630 : * This computation is pessimistic and assumes the maximum possible number of
1631 : * blocks, due to lack of better information.
1632 : */
1633 : size_t
1634 178544 : DecodeXLogRecordRequiredSpace(size_t xl_tot_len)
1635 : {
1636 178544 : size_t size = 0;
1637 :
1638 : /* Account for the fixed size part of the decoded record struct. */
1639 178544 : size += offsetof(DecodedXLogRecord, blocks[0]);
1640 : /* Account for the flexible blocks array of maximum possible size. */
1641 178544 : size += sizeof(DecodedBkpBlock) * (XLR_MAX_BLOCK_ID + 1);
1642 : /* Account for all the raw main and block data. */
1643 178544 : size += xl_tot_len;
1644 : /* We might insert padding before main_data. */
1645 178544 : size += (MAXIMUM_ALIGNOF - 1);
1646 : /* We might insert padding before each block's data. */
1647 178544 : size += (MAXIMUM_ALIGNOF - 1) * (XLR_MAX_BLOCK_ID + 1);
1648 : /* We might insert padding at the end. */
1649 178544 : size += (MAXIMUM_ALIGNOF - 1);
1650 :
1651 178544 : return size;
1652 : }
1653 :
1654 : /*
1655 : * Decode a record. "decoded" must point to a MAXALIGNed memory area that has
1656 : * space for at least DecodeXLogRecordRequiredSpace(record) bytes. On
1657 : * success, decoded->size contains the actual space occupied by the decoded
1658 : * record, which may turn out to be less.
1659 : *
1660 : * Only decoded->oversized member must be initialized already, and will not be
1661 : * modified. Other members will be initialized as required.
1662 : *
1663 : * On error, a human-readable error message is returned in *errormsg, and
1664 : * the return value is false.
1665 : */
1666 : bool
1667 178544 : DecodeXLogRecord(XLogReaderState *state,
1668 : DecodedXLogRecord *decoded,
1669 : XLogRecord *record,
1670 : XLogRecPtr lsn,
1671 : char **errormsg)
1672 : {
1673 : /*
1674 : * read next _size bytes from record buffer, but check for overrun first.
1675 : */
1676 : #define COPY_HEADER_FIELD(_dst, _size) \
1677 : do { \
1678 : if (remaining < _size) \
1679 : goto shortdata_err; \
1680 : memcpy(_dst, ptr, _size); \
1681 : ptr += _size; \
1682 : remaining -= _size; \
1683 : } while(0)
1684 :
1685 : char *ptr;
1686 : char *out;
1687 : uint32 remaining;
1688 : uint32 datatotal;
1689 178544 : RelFileLocator *rlocator = NULL;
1690 : uint8 block_id;
1691 :
1692 178544 : decoded->header = *record;
1693 178544 : decoded->lsn = lsn;
1694 178544 : decoded->next = NULL;
1695 178544 : decoded->record_origin = InvalidRepOriginId;
1696 178544 : decoded->toplevel_xid = InvalidTransactionId;
1697 178544 : decoded->main_data = NULL;
1698 178544 : decoded->main_data_len = 0;
1699 178544 : decoded->max_block_id = -1;
1700 178544 : ptr = (char *) record;
1701 178544 : ptr += SizeOfXLogRecord;
1702 178544 : remaining = record->xl_tot_len - SizeOfXLogRecord;
1703 :
1704 : /* Decode the headers */
1705 178544 : datatotal = 0;
1706 353880 : while (remaining > datatotal)
1707 : {
1708 341958 : COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1709 :
1710 341958 : if (block_id == XLR_BLOCK_ID_DATA_SHORT)
1711 : {
1712 : /* XLogRecordDataHeaderShort */
1713 : uint8 main_data_len;
1714 :
1715 166590 : COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1716 :
1717 166590 : decoded->main_data_len = main_data_len;
1718 166590 : datatotal += main_data_len;
1719 166590 : break; /* by convention, the main data fragment is
1720 : * always last */
1721 : }
1722 175368 : else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1723 : {
1724 : /* XLogRecordDataHeaderLong */
1725 : uint32 main_data_len;
1726 :
1727 32 : COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1728 32 : decoded->main_data_len = main_data_len;
1729 32 : datatotal += main_data_len;
1730 32 : break; /* by convention, the main data fragment is
1731 : * always last */
1732 : }
1733 175336 : else if (block_id == XLR_BLOCK_ID_ORIGIN)
1734 : {
1735 0 : COPY_HEADER_FIELD(&decoded->record_origin, sizeof(RepOriginId));
1736 : }
1737 175336 : else if (block_id == XLR_BLOCK_ID_TOPLEVEL_XID)
1738 : {
1739 0 : COPY_HEADER_FIELD(&decoded->toplevel_xid, sizeof(TransactionId));
1740 : }
1741 175336 : else if (block_id <= XLR_MAX_BLOCK_ID)
1742 : {
1743 : /* XLogRecordBlockHeader */
1744 : DecodedBkpBlock *blk;
1745 : uint8 fork_flags;
1746 :
1747 : /* mark any intervening block IDs as not in use */
1748 175336 : for (int i = decoded->max_block_id + 1; i < block_id; ++i)
1749 0 : decoded->blocks[i].in_use = false;
1750 :
1751 175336 : if (block_id <= decoded->max_block_id)
1752 : {
1753 0 : report_invalid_record(state,
1754 : "out-of-order block_id %u at %X/%X",
1755 : block_id,
1756 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
1757 0 : goto err;
1758 : }
1759 175336 : decoded->max_block_id = block_id;
1760 :
1761 175336 : blk = &decoded->blocks[block_id];
1762 175336 : blk->in_use = true;
1763 175336 : blk->apply_image = false;
1764 :
1765 175336 : COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1766 175336 : blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1767 175336 : blk->flags = fork_flags;
1768 175336 : blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1769 175336 : blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1770 :
1771 175336 : blk->prefetch_buffer = InvalidBuffer;
1772 :
1773 175336 : COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1774 : /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1775 175336 : if (blk->has_data && blk->data_len == 0)
1776 : {
1777 0 : report_invalid_record(state,
1778 : "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
1779 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
1780 0 : goto err;
1781 : }
1782 175336 : if (!blk->has_data && blk->data_len != 0)
1783 : {
1784 0 : report_invalid_record(state,
1785 : "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
1786 0 : (unsigned int) blk->data_len,
1787 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
1788 0 : goto err;
1789 : }
1790 175336 : datatotal += blk->data_len;
1791 :
1792 175336 : if (blk->has_image)
1793 : {
1794 11998 : COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
1795 11998 : COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
1796 11998 : COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1797 :
1798 11998 : blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1799 :
1800 11998 : if (BKPIMAGE_COMPRESSED(blk->bimg_info))
1801 : {
1802 0 : if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1803 0 : COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1804 : else
1805 0 : blk->hole_length = 0;
1806 : }
1807 : else
1808 11998 : blk->hole_length = BLCKSZ - blk->bimg_len;
1809 11998 : datatotal += blk->bimg_len;
1810 :
1811 : /*
1812 : * cross-check that hole_offset > 0, hole_length > 0 and
1813 : * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1814 : */
1815 11998 : if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1816 11902 : (blk->hole_offset == 0 ||
1817 11902 : blk->hole_length == 0 ||
1818 11902 : blk->bimg_len == BLCKSZ))
1819 : {
1820 0 : report_invalid_record(state,
1821 : "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
1822 0 : (unsigned int) blk->hole_offset,
1823 0 : (unsigned int) blk->hole_length,
1824 0 : (unsigned int) blk->bimg_len,
1825 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
1826 0 : goto err;
1827 : }
1828 :
1829 : /*
1830 : * cross-check that hole_offset == 0 and hole_length == 0 if
1831 : * the HAS_HOLE flag is not set.
1832 : */
1833 11998 : if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1834 96 : (blk->hole_offset != 0 || blk->hole_length != 0))
1835 : {
1836 0 : report_invalid_record(state,
1837 : "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
1838 0 : (unsigned int) blk->hole_offset,
1839 0 : (unsigned int) blk->hole_length,
1840 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
1841 0 : goto err;
1842 : }
1843 :
1844 : /*
1845 : * Cross-check that bimg_len < BLCKSZ if it is compressed.
1846 : */
1847 11998 : if (BKPIMAGE_COMPRESSED(blk->bimg_info) &&
1848 0 : blk->bimg_len == BLCKSZ)
1849 : {
1850 0 : report_invalid_record(state,
1851 : "BKPIMAGE_COMPRESSED set, but block image length %u at %X/%X",
1852 0 : (unsigned int) blk->bimg_len,
1853 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
1854 0 : goto err;
1855 : }
1856 :
1857 : /*
1858 : * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE is
1859 : * set nor COMPRESSED().
1860 : */
1861 11998 : if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1862 96 : !BKPIMAGE_COMPRESSED(blk->bimg_info) &&
1863 96 : blk->bimg_len != BLCKSZ)
1864 : {
1865 0 : report_invalid_record(state,
1866 : "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_COMPRESSED set, but block image length is %u at %X/%X",
1867 0 : (unsigned int) blk->data_len,
1868 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
1869 0 : goto err;
1870 : }
1871 : }
1872 175336 : if (!(fork_flags & BKPBLOCK_SAME_REL))
1873 : {
1874 174640 : COPY_HEADER_FIELD(&blk->rlocator, sizeof(RelFileLocator));
1875 174640 : rlocator = &blk->rlocator;
1876 : }
1877 : else
1878 : {
1879 696 : if (rlocator == NULL)
1880 : {
1881 0 : report_invalid_record(state,
1882 : "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
1883 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
1884 0 : goto err;
1885 : }
1886 :
1887 696 : blk->rlocator = *rlocator;
1888 : }
1889 175336 : COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1890 : }
1891 : else
1892 : {
1893 0 : report_invalid_record(state,
1894 : "invalid block_id %u at %X/%X",
1895 0 : block_id, LSN_FORMAT_ARGS(state->ReadRecPtr));
1896 0 : goto err;
1897 : }
1898 : }
1899 :
1900 178544 : if (remaining != datatotal)
1901 0 : goto shortdata_err;
1902 :
1903 : /*
1904 : * Ok, we've parsed the fragment headers, and verified that the total
1905 : * length of the payload in the fragments is equal to the amount of data
1906 : * left. Copy the data of each fragment to contiguous space after the
1907 : * blocks array, inserting alignment padding before the data fragments so
1908 : * they can be cast to struct pointers by REDO routines.
1909 : */
1910 178544 : out = ((char *) decoded) +
1911 178544 : offsetof(DecodedXLogRecord, blocks) +
1912 178544 : sizeof(decoded->blocks[0]) * (decoded->max_block_id + 1);
1913 :
1914 : /* block data first */
1915 353880 : for (block_id = 0; block_id <= decoded->max_block_id; block_id++)
1916 : {
1917 175336 : DecodedBkpBlock *blk = &decoded->blocks[block_id];
1918 :
1919 175336 : if (!blk->in_use)
1920 0 : continue;
1921 :
1922 : Assert(blk->has_image || !blk->apply_image);
1923 :
1924 175336 : if (blk->has_image)
1925 : {
1926 : /* no need to align image */
1927 11998 : blk->bkp_image = out;
1928 11998 : memcpy(out, ptr, blk->bimg_len);
1929 11998 : ptr += blk->bimg_len;
1930 11998 : out += blk->bimg_len;
1931 : }
1932 175336 : if (blk->has_data)
1933 : {
1934 81826 : out = (char *) MAXALIGN(out);
1935 81826 : blk->data = out;
1936 81826 : memcpy(blk->data, ptr, blk->data_len);
1937 81826 : ptr += blk->data_len;
1938 81826 : out += blk->data_len;
1939 : }
1940 : }
1941 :
1942 : /* and finally, the main data */
1943 178544 : if (decoded->main_data_len > 0)
1944 : {
1945 166622 : out = (char *) MAXALIGN(out);
1946 166622 : decoded->main_data = out;
1947 166622 : memcpy(decoded->main_data, ptr, decoded->main_data_len);
1948 166622 : ptr += decoded->main_data_len;
1949 166622 : out += decoded->main_data_len;
1950 : }
1951 :
1952 : /* Report the actual size we used. */
1953 178544 : decoded->size = MAXALIGN(out - (char *) decoded);
1954 : Assert(DecodeXLogRecordRequiredSpace(record->xl_tot_len) >=
1955 : decoded->size);
1956 :
1957 178544 : return true;
1958 :
1959 0 : shortdata_err:
1960 0 : report_invalid_record(state,
1961 : "record with invalid length at %X/%X",
1962 0 : LSN_FORMAT_ARGS(state->ReadRecPtr));
1963 0 : err:
1964 0 : *errormsg = state->errormsg_buf;
1965 :
1966 0 : return false;
1967 : }
1968 :
1969 : /*
1970 : * Returns information about the block that a block reference refers to.
1971 : *
1972 : * This is like XLogRecGetBlockTagExtended, except that the block reference
1973 : * must exist and there's no access to prefetch_buffer.
1974 : */
1975 : void
1976 0 : XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id,
1977 : RelFileLocator *rlocator, ForkNumber *forknum,
1978 : BlockNumber *blknum)
1979 : {
1980 0 : if (!XLogRecGetBlockTagExtended(record, block_id, rlocator, forknum,
1981 : blknum, NULL))
1982 : {
1983 : #ifndef FRONTEND
1984 : elog(ERROR, "could not locate backup block with ID %d in WAL record",
1985 : block_id);
1986 : #else
1987 0 : pg_fatal("could not locate backup block with ID %d in WAL record",
1988 : block_id);
1989 : #endif
1990 : }
1991 0 : }
1992 :
1993 : /*
1994 : * Returns information about the block that a block reference refers to,
1995 : * optionally including the buffer that the block may already be in.
1996 : *
1997 : * If the WAL record contains a block reference with the given ID, *rlocator,
1998 : * *forknum, *blknum and *prefetch_buffer are filled in (if not NULL), and
1999 : * returns true. Otherwise returns false.
2000 : */
2001 : bool
2002 171432 : XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id,
2003 : RelFileLocator *rlocator, ForkNumber *forknum,
2004 : BlockNumber *blknum,
2005 : Buffer *prefetch_buffer)
2006 : {
2007 : DecodedBkpBlock *bkpb;
2008 :
2009 171432 : if (!XLogRecHasBlockRef(record, block_id))
2010 0 : return false;
2011 :
2012 171432 : bkpb = &record->record->blocks[block_id];
2013 171432 : if (rlocator)
2014 171432 : *rlocator = bkpb->rlocator;
2015 171432 : if (forknum)
2016 171432 : *forknum = bkpb->forknum;
2017 171432 : if (blknum)
2018 171432 : *blknum = bkpb->blkno;
2019 171432 : if (prefetch_buffer)
2020 0 : *prefetch_buffer = bkpb->prefetch_buffer;
2021 171432 : return true;
2022 : }
2023 :
2024 : /*
2025 : * Returns the data associated with a block reference, or NULL if there is
2026 : * no data (e.g. because a full-page image was taken instead). The returned
2027 : * pointer points to a MAXALIGNed buffer.
2028 : */
2029 : char *
2030 0 : XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
2031 : {
2032 : DecodedBkpBlock *bkpb;
2033 :
2034 0 : if (block_id > record->record->max_block_id ||
2035 0 : !record->record->blocks[block_id].in_use)
2036 0 : return NULL;
2037 :
2038 0 : bkpb = &record->record->blocks[block_id];
2039 :
2040 0 : if (!bkpb->has_data)
2041 : {
2042 0 : if (len)
2043 0 : *len = 0;
2044 0 : return NULL;
2045 : }
2046 : else
2047 : {
2048 0 : if (len)
2049 0 : *len = bkpb->data_len;
2050 0 : return bkpb->data;
2051 : }
2052 : }
2053 :
2054 : /*
2055 : * Restore a full-page image from a backup block attached to an XLOG record.
2056 : *
2057 : * Returns true if a full-page image is restored, and false on failure with
2058 : * an error to be consumed by the caller.
2059 : */
2060 : bool
2061 0 : RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
2062 : {
2063 : DecodedBkpBlock *bkpb;
2064 : char *ptr;
2065 : PGAlignedBlock tmp;
2066 :
2067 0 : if (block_id > record->record->max_block_id ||
2068 0 : !record->record->blocks[block_id].in_use)
2069 : {
2070 0 : report_invalid_record(record,
2071 : "could not restore image at %X/%X with invalid block %d specified",
2072 0 : LSN_FORMAT_ARGS(record->ReadRecPtr),
2073 : block_id);
2074 0 : return false;
2075 : }
2076 0 : if (!record->record->blocks[block_id].has_image)
2077 : {
2078 0 : report_invalid_record(record, "could not restore image at %X/%X with invalid state, block %d",
2079 0 : LSN_FORMAT_ARGS(record->ReadRecPtr),
2080 : block_id);
2081 0 : return false;
2082 : }
2083 :
2084 0 : bkpb = &record->record->blocks[block_id];
2085 0 : ptr = bkpb->bkp_image;
2086 :
2087 0 : if (BKPIMAGE_COMPRESSED(bkpb->bimg_info))
2088 : {
2089 : /* If a backup block image is compressed, decompress it */
2090 0 : bool decomp_success = true;
2091 :
2092 0 : if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_PGLZ) != 0)
2093 : {
2094 0 : if (pglz_decompress(ptr, bkpb->bimg_len, tmp.data,
2095 0 : BLCKSZ - bkpb->hole_length, true) < 0)
2096 0 : decomp_success = false;
2097 : }
2098 0 : else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_LZ4) != 0)
2099 : {
2100 : #ifdef USE_LZ4
2101 0 : if (LZ4_decompress_safe(ptr, tmp.data,
2102 0 : bkpb->bimg_len, BLCKSZ - bkpb->hole_length) <= 0)
2103 0 : decomp_success = false;
2104 : #else
2105 : report_invalid_record(record, "could not restore image at %X/%X compressed with %s not supported by build, block %d",
2106 : LSN_FORMAT_ARGS(record->ReadRecPtr),
2107 : "LZ4",
2108 : block_id);
2109 : return false;
2110 : #endif
2111 : }
2112 0 : else if ((bkpb->bimg_info & BKPIMAGE_COMPRESS_ZSTD) != 0)
2113 : {
2114 : #ifdef USE_ZSTD
2115 : size_t decomp_result = ZSTD_decompress(tmp.data,
2116 : BLCKSZ - bkpb->hole_length,
2117 : ptr, bkpb->bimg_len);
2118 :
2119 : if (ZSTD_isError(decomp_result))
2120 : decomp_success = false;
2121 : #else
2122 0 : report_invalid_record(record, "could not restore image at %X/%X compressed with %s not supported by build, block %d",
2123 0 : LSN_FORMAT_ARGS(record->ReadRecPtr),
2124 : "zstd",
2125 : block_id);
2126 0 : return false;
2127 : #endif
2128 : }
2129 : else
2130 : {
2131 0 : report_invalid_record(record, "could not restore image at %X/%X compressed with unknown method, block %d",
2132 0 : LSN_FORMAT_ARGS(record->ReadRecPtr),
2133 : block_id);
2134 0 : return false;
2135 : }
2136 :
2137 0 : if (!decomp_success)
2138 : {
2139 0 : report_invalid_record(record, "could not decompress image at %X/%X, block %d",
2140 0 : LSN_FORMAT_ARGS(record->ReadRecPtr),
2141 : block_id);
2142 0 : return false;
2143 : }
2144 :
2145 0 : ptr = tmp.data;
2146 : }
2147 :
2148 : /* generate page, taking into account hole if necessary */
2149 0 : if (bkpb->hole_length == 0)
2150 : {
2151 0 : memcpy(page, ptr, BLCKSZ);
2152 : }
2153 : else
2154 : {
2155 0 : memcpy(page, ptr, bkpb->hole_offset);
2156 : /* must zero-fill the hole */
2157 0 : MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
2158 0 : memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
2159 0 : ptr + bkpb->hole_offset,
2160 0 : BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
2161 : }
2162 :
2163 0 : return true;
2164 : }
2165 :
2166 : #ifndef FRONTEND
2167 :
2168 : /*
2169 : * Extract the FullTransactionId from a WAL record.
2170 : */
2171 : FullTransactionId
2172 : XLogRecGetFullXid(XLogReaderState *record)
2173 : {
2174 : TransactionId xid,
2175 : next_xid;
2176 : uint32 epoch;
2177 :
2178 : /*
2179 : * This function is only safe during replay, because it depends on the
2180 : * replay state. See AdvanceNextFullTransactionIdPastXid() for more.
2181 : */
2182 : Assert(AmStartupProcess() || !IsUnderPostmaster);
2183 :
2184 : xid = XLogRecGetXid(record);
2185 : next_xid = XidFromFullTransactionId(TransamVariables->nextXid);
2186 : epoch = EpochFromFullTransactionId(TransamVariables->nextXid);
2187 :
2188 : /*
2189 : * If xid is numerically greater than next_xid, it has to be from the last
2190 : * epoch.
2191 : */
2192 : if (unlikely(xid > next_xid))
2193 : --epoch;
2194 :
2195 : return FullTransactionIdFromEpochAndXid(epoch, xid);
2196 : }
2197 :
2198 : #endif
|