PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xlogreader.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * xlogreader.c
4  * Generic XLog reading facility
5  *
6  * Portions Copyright (c) 2013-2017, 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 "access/transam.h"
21 #include "access/xlogrecord.h"
22 #include "access/xlog_internal.h"
23 #include "access/xlogreader.h"
24 #include "catalog/pg_control.h"
25 #include "common/pg_lzcompress.h"
26 #include "replication/origin.h"
27 
28 static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength);
29 
31  XLogPageHeader hdr);
33  XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess);
34 static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record,
35  XLogRecPtr recptr);
37  int reqLen);
38 static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2, 3);
39 
40 static void ResetDecoder(XLogReaderState *state);
41 
42 /* size of the buffer allocated for error message. */
43 #define MAX_ERRORMSG_LEN 1000
44 
45 /*
46  * Construct a string in state->errormsg_buf explaining what's wrong with
47  * the current record being read.
48  */
49 static void
50 report_invalid_record(XLogReaderState *state, const char *fmt,...)
51 {
52  va_list args;
53 
54  fmt = _(fmt);
55 
56  va_start(args, fmt);
57  vsnprintf(state->errormsg_buf, MAX_ERRORMSG_LEN, fmt, args);
58  va_end(args);
59 }
60 
61 /*
62  * Allocate and initialize a new XLogReader.
63  *
64  * Returns NULL if the xlogreader couldn't be allocated.
65  */
67 XLogReaderAllocate(XLogPageReadCB pagereadfunc, void *private_data)
68 {
69  XLogReaderState *state;
70 
71  state = (XLogReaderState *)
74  if (!state)
75  return NULL;
76 
77  state->max_block_id = -1;
78 
79  /*
80  * Permanently allocate readBuf. We do it this way, rather than just
81  * making a static array, for two reasons: (1) no need to waste the
82  * storage in most instantiations of the backend; (2) a static char array
83  * isn't guaranteed to have any particular alignment, whereas
84  * palloc_extended() will provide MAXALIGN'd storage.
85  */
86  state->readBuf = (char *) palloc_extended(XLOG_BLCKSZ,
88  if (!state->readBuf)
89  {
90  pfree(state);
91  return NULL;
92  }
93 
94  state->read_page = pagereadfunc;
95  /* system_identifier initialized to zeroes above */
96  state->private_data = private_data;
97  /* ReadRecPtr and EndRecPtr initialized to zeroes above */
98  /* readSegNo, readOff, readLen, readPageTLI initialized to zeroes above */
101  if (!state->errormsg_buf)
102  {
103  pfree(state->readBuf);
104  pfree(state);
105  return NULL;
106  }
107  state->errormsg_buf[0] = '\0';
108 
109  /*
110  * Allocate an initial readRecordBuf of minimal size, which can later be
111  * enlarged if necessary.
112  */
113  if (!allocate_recordbuf(state, 0))
114  {
115  pfree(state->errormsg_buf);
116  pfree(state->readBuf);
117  pfree(state);
118  return NULL;
119  }
120 
121  return state;
122 }
123 
124 void
126 {
127  int block_id;
128 
129  for (block_id = 0; block_id <= XLR_MAX_BLOCK_ID; block_id++)
130  {
131  if (state->blocks[block_id].data)
132  pfree(state->blocks[block_id].data);
133  }
134  if (state->main_data)
135  pfree(state->main_data);
136 
137  pfree(state->errormsg_buf);
138  if (state->readRecordBuf)
139  pfree(state->readRecordBuf);
140  pfree(state->readBuf);
141  pfree(state);
142 }
143 
144 /*
145  * Allocate readRecordBuf to fit a record of at least the given length.
146  * Returns true if successful, false if out of memory.
147  *
148  * readRecordBufSize is set to the new buffer size.
149  *
150  * To avoid useless small increases, round its size to a multiple of
151  * XLOG_BLCKSZ, and make sure it's at least 5*Max(BLCKSZ, XLOG_BLCKSZ) to start
152  * with. (That is enough for all "normal" records, but very large commit or
153  * abort records might need more space.)
154  */
155 static bool
157 {
158  uint32 newSize = reclength;
159 
160  newSize += XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ);
161  newSize = Max(newSize, 5 * Max(BLCKSZ, XLOG_BLCKSZ));
162 
163  if (state->readRecordBuf)
164  pfree(state->readRecordBuf);
165  state->readRecordBuf =
166  (char *) palloc_extended(newSize, MCXT_ALLOC_NO_OOM);
167  if (state->readRecordBuf == NULL)
168  {
169  state->readRecordBufSize = 0;
170  return false;
171  }
172  state->readRecordBufSize = newSize;
173  return true;
174 }
175 
176 /*
177  * Attempt to read an XLOG record.
178  *
179  * If RecPtr is valid, try to read a record at that position. Otherwise
180  * try to read a record just after the last one previously read.
181  *
182  * If the read_page callback fails to read the requested data, NULL is
183  * returned. The callback is expected to have reported the error; errormsg
184  * is set to NULL.
185  *
186  * If the reading fails for some other reason, NULL is also returned, and
187  * *errormsg is set to a string with details of the failure.
188  *
189  * The returned pointer (or *errormsg) points to an internal buffer that's
190  * valid until the next call to XLogReadRecord.
191  */
192 XLogRecord *
193 XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
194 {
195  XLogRecord *record;
196  XLogRecPtr targetPagePtr;
197  bool randAccess;
198  uint32 len,
199  total_len;
200  uint32 targetRecOff;
201  uint32 pageHeaderSize;
202  bool gotheader;
203  int readOff;
204 
205  /*
206  * randAccess indicates whether to verify the previous-record pointer of
207  * the record we're reading. We only do this if we're reading
208  * sequentially, which is what we initially assume.
209  */
210  randAccess = false;
211 
212  /* reset error state */
213  *errormsg = NULL;
214  state->errormsg_buf[0] = '\0';
215 
216  ResetDecoder(state);
217 
218  if (RecPtr == InvalidXLogRecPtr)
219  {
220  /* No explicit start point; read the record after the one we just read */
221  RecPtr = state->EndRecPtr;
222 
223  if (state->ReadRecPtr == InvalidXLogRecPtr)
224  randAccess = true;
225 
226  /*
227  * RecPtr is pointing to end+1 of the previous WAL record. If we're
228  * at a page boundary, no more records can fit on the current page. We
229  * must skip over the page header, but we can't do that until we've
230  * read in the page, since the header size is variable.
231  */
232  }
233  else
234  {
235  /*
236  * Caller supplied a position to start at.
237  *
238  * In this case, the passed-in record pointer should already be
239  * pointing to a valid record starting position.
240  */
241  Assert(XRecOffIsValid(RecPtr));
242  randAccess = true;
243  }
244 
245  state->currRecPtr = RecPtr;
246 
247  targetPagePtr = RecPtr - (RecPtr % XLOG_BLCKSZ);
248  targetRecOff = RecPtr % XLOG_BLCKSZ;
249 
250  /*
251  * Read the page containing the record into state->readBuf. Request enough
252  * byte to cover the whole record header, or at least the part of it that
253  * fits on the same page.
254  */
255  readOff = ReadPageInternal(state,
256  targetPagePtr,
257  Min(targetRecOff + SizeOfXLogRecord, XLOG_BLCKSZ));
258  if (readOff < 0)
259  goto err;
260 
261  /*
262  * ReadPageInternal always returns at least the page header, so we can
263  * examine it now.
264  */
265  pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
266  if (targetRecOff == 0)
267  {
268  /*
269  * At page start, so skip over page header.
270  */
271  RecPtr += pageHeaderSize;
272  targetRecOff = pageHeaderSize;
273  }
274  else if (targetRecOff < pageHeaderSize)
275  {
276  report_invalid_record(state, "invalid record offset at %X/%X",
277  (uint32) (RecPtr >> 32), (uint32) RecPtr);
278  goto err;
279  }
280 
281  if ((((XLogPageHeader) state->readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD) &&
282  targetRecOff == pageHeaderSize)
283  {
284  report_invalid_record(state, "contrecord is requested by %X/%X",
285  (uint32) (RecPtr >> 32), (uint32) RecPtr);
286  goto err;
287  }
288 
289  /* ReadPageInternal has verified the page header */
290  Assert(pageHeaderSize <= readOff);
291 
292  /*
293  * Read the record length.
294  *
295  * NB: Even though we use an XLogRecord pointer here, the whole record
296  * header might not fit on this page. xl_tot_len is the first field of the
297  * struct, so it must be on this page (the records are MAXALIGNed), but we
298  * cannot access any other fields until we've verified that we got the
299  * whole header.
300  */
301  record = (XLogRecord *) (state->readBuf + RecPtr % XLOG_BLCKSZ);
302  total_len = record->xl_tot_len;
303 
304  /*
305  * If the whole record header is on this page, validate it immediately.
306  * Otherwise do just a basic sanity check on xl_tot_len, and validate the
307  * rest of the header after reading it from the next page. The xl_tot_len
308  * check is necessary here to ensure that we enter the "Need to reassemble
309  * record" code path below; otherwise we might fail to apply
310  * ValidXLogRecordHeader at all.
311  */
312  if (targetRecOff <= XLOG_BLCKSZ - SizeOfXLogRecord)
313  {
314  if (!ValidXLogRecordHeader(state, RecPtr, state->ReadRecPtr, record,
315  randAccess))
316  goto err;
317  gotheader = true;
318  }
319  else
320  {
321  /* XXX: more validation should be done here */
322  if (total_len < SizeOfXLogRecord)
323  {
324  report_invalid_record(state,
325  "invalid record length at %X/%X: wanted %u, got %u",
326  (uint32) (RecPtr >> 32), (uint32) RecPtr,
327  (uint32) SizeOfXLogRecord, total_len);
328  goto err;
329  }
330  gotheader = false;
331  }
332 
333  /*
334  * Enlarge readRecordBuf as needed.
335  */
336  if (total_len > state->readRecordBufSize &&
337  !allocate_recordbuf(state, total_len))
338  {
339  /* We treat this as a "bogus data" condition */
340  report_invalid_record(state, "record length %u at %X/%X too long",
341  total_len,
342  (uint32) (RecPtr >> 32), (uint32) RecPtr);
343  goto err;
344  }
345 
346  len = XLOG_BLCKSZ - RecPtr % XLOG_BLCKSZ;
347  if (total_len > len)
348  {
349  /* Need to reassemble record */
350  char *contdata;
351  XLogPageHeader pageHeader;
352  char *buffer;
353  uint32 gotlen;
354 
355  /* Copy the first fragment of the record from the first page. */
356  memcpy(state->readRecordBuf,
357  state->readBuf + RecPtr % XLOG_BLCKSZ, len);
358  buffer = state->readRecordBuf + len;
359  gotlen = len;
360 
361  do
362  {
363  /* Calculate pointer to beginning of next page */
364  targetPagePtr += XLOG_BLCKSZ;
365 
366  /* Wait for the next page to become available */
367  readOff = ReadPageInternal(state, targetPagePtr,
368  Min(total_len - gotlen + SizeOfXLogShortPHD,
369  XLOG_BLCKSZ));
370 
371  if (readOff < 0)
372  goto err;
373 
374  Assert(SizeOfXLogShortPHD <= readOff);
375 
376  /* Check that the continuation on next page looks valid */
377  pageHeader = (XLogPageHeader) state->readBuf;
378  if (!(pageHeader->xlp_info & XLP_FIRST_IS_CONTRECORD))
379  {
380  report_invalid_record(state,
381  "there is no contrecord flag at %X/%X",
382  (uint32) (RecPtr >> 32), (uint32) RecPtr);
383  goto err;
384  }
385 
386  /*
387  * Cross-check that xlp_rem_len agrees with how much of the record
388  * we expect there to be left.
389  */
390  if (pageHeader->xlp_rem_len == 0 ||
391  total_len != (pageHeader->xlp_rem_len + gotlen))
392  {
393  report_invalid_record(state,
394  "invalid contrecord length %u at %X/%X",
395  pageHeader->xlp_rem_len,
396  (uint32) (RecPtr >> 32), (uint32) RecPtr);
397  goto err;
398  }
399 
400  /* Append the continuation from this page to the buffer */
401  pageHeaderSize = XLogPageHeaderSize(pageHeader);
402 
403  if (readOff < pageHeaderSize)
404  readOff = ReadPageInternal(state, targetPagePtr,
405  pageHeaderSize);
406 
407  Assert(pageHeaderSize <= readOff);
408 
409  contdata = (char *) state->readBuf + pageHeaderSize;
410  len = XLOG_BLCKSZ - pageHeaderSize;
411  if (pageHeader->xlp_rem_len < len)
412  len = pageHeader->xlp_rem_len;
413 
414  if (readOff < pageHeaderSize + len)
415  readOff = ReadPageInternal(state, targetPagePtr,
416  pageHeaderSize + len);
417 
418  memcpy(buffer, (char *) contdata, len);
419  buffer += len;
420  gotlen += len;
421 
422  /* If we just reassembled the record header, validate it. */
423  if (!gotheader)
424  {
425  record = (XLogRecord *) state->readRecordBuf;
426  if (!ValidXLogRecordHeader(state, RecPtr, state->ReadRecPtr,
427  record, randAccess))
428  goto err;
429  gotheader = true;
430  }
431  } while (gotlen < total_len);
432 
433  Assert(gotheader);
434 
435  record = (XLogRecord *) state->readRecordBuf;
436  if (!ValidXLogRecord(state, record, RecPtr))
437  goto err;
438 
439  pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) state->readBuf);
440  state->ReadRecPtr = RecPtr;
441  state->EndRecPtr = targetPagePtr + pageHeaderSize
442  + MAXALIGN(pageHeader->xlp_rem_len);
443  }
444  else
445  {
446  /* Wait for the record data to become available */
447  readOff = ReadPageInternal(state, targetPagePtr,
448  Min(targetRecOff + total_len, XLOG_BLCKSZ));
449  if (readOff < 0)
450  goto err;
451 
452  /* Record does not cross a page boundary */
453  if (!ValidXLogRecord(state, record, RecPtr))
454  goto err;
455 
456  state->EndRecPtr = RecPtr + MAXALIGN(total_len);
457 
458  state->ReadRecPtr = RecPtr;
459  memcpy(state->readRecordBuf, record, total_len);
460  }
461 
462  /*
463  * Special processing if it's an XLOG SWITCH record
464  */
465  if (record->xl_rmid == RM_XLOG_ID &&
466  (record->xl_info & ~XLR_INFO_MASK) == XLOG_SWITCH)
467  {
468  /* Pretend it extends to end of segment */
469  state->EndRecPtr += XLogSegSize - 1;
470  state->EndRecPtr -= state->EndRecPtr % XLogSegSize;
471  }
472 
473  if (DecodeXLogRecord(state, record, errormsg))
474  return record;
475  else
476  return NULL;
477 
478 err:
479 
480  /*
481  * Invalidate the read state. We might read from a different source after
482  * failure.
483  */
485 
486  if (state->errormsg_buf[0] != '\0')
487  *errormsg = state->errormsg_buf;
488 
489  return NULL;
490 }
491 
492 /*
493  * Read a single xlog page including at least [pageptr, reqLen] of valid data
494  * via the read_page() callback.
495  *
496  * Returns -1 if the required page cannot be read for some reason; errormsg_buf
497  * is set in that case (unless the error occurs in the read_page callback).
498  *
499  * We fetch the page from a reader-local cache if we know we have the required
500  * data and if there hasn't been any error since caching the data.
501  */
502 static int
503 ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
504 {
505  int readLen;
506  uint32 targetPageOff;
507  XLogSegNo targetSegNo;
508  XLogPageHeader hdr;
509 
510  Assert((pageptr % XLOG_BLCKSZ) == 0);
511 
512  XLByteToSeg(pageptr, targetSegNo);
513  targetPageOff = (pageptr % XLogSegSize);
514 
515  /* check whether we have all the requested data already */
516  if (targetSegNo == state->readSegNo && targetPageOff == state->readOff &&
517  reqLen < state->readLen)
518  return state->readLen;
519 
520  /*
521  * Data is not in our buffer.
522  *
523  * Every time we actually read the page, even if we looked at parts of it
524  * before, we need to do verification as the read_page callback might now
525  * be rereading data from a different source.
526  *
527  * Whenever switching to a new WAL segment, we read the first page of the
528  * file and validate its header, even if that's not where the target
529  * record is. This is so that we can check the additional identification
530  * info that is present in the first page's "long" header.
531  */
532  if (targetSegNo != state->readSegNo && targetPageOff != 0)
533  {
534  XLogPageHeader hdr;
535  XLogRecPtr targetSegmentPtr = pageptr - targetPageOff;
536 
537  readLen = state->read_page(state, targetSegmentPtr, XLOG_BLCKSZ,
538  state->currRecPtr,
539  state->readBuf, &state->readPageTLI);
540  if (readLen < 0)
541  goto err;
542 
543  /* we can be sure to have enough WAL available, we scrolled back */
544  Assert(readLen == XLOG_BLCKSZ);
545 
546  hdr = (XLogPageHeader) state->readBuf;
547 
548  if (!ValidXLogPageHeader(state, targetSegmentPtr, hdr))
549  goto err;
550  }
551 
552  /*
553  * First, read the requested data length, but at least a short page header
554  * so that we can validate it.
555  */
556  readLen = state->read_page(state, pageptr, Max(reqLen, SizeOfXLogShortPHD),
557  state->currRecPtr,
558  state->readBuf, &state->readPageTLI);
559  if (readLen < 0)
560  goto err;
561 
562  Assert(readLen <= XLOG_BLCKSZ);
563 
564  /* Do we have enough data to check the header length? */
565  if (readLen <= SizeOfXLogShortPHD)
566  goto err;
567 
568  Assert(readLen >= reqLen);
569 
570  hdr = (XLogPageHeader) state->readBuf;
571 
572  /* still not enough */
573  if (readLen < XLogPageHeaderSize(hdr))
574  {
575  readLen = state->read_page(state, pageptr, XLogPageHeaderSize(hdr),
576  state->currRecPtr,
577  state->readBuf, &state->readPageTLI);
578  if (readLen < 0)
579  goto err;
580  }
581 
582  /*
583  * Now that we know we have the full header, validate it.
584  */
585  if (!ValidXLogPageHeader(state, pageptr, hdr))
586  goto err;
587 
588  /* update read state information */
589  state->readSegNo = targetSegNo;
590  state->readOff = targetPageOff;
591  state->readLen = readLen;
592 
593  return readLen;
594 
595 err:
597  return -1;
598 }
599 
600 /*
601  * Invalidate the xlogreader's read state to force a re-read.
602  */
603 void
605 {
606  state->readSegNo = 0;
607  state->readOff = 0;
608  state->readLen = 0;
609 }
610 
611 /*
612  * Validate an XLOG record header.
613  *
614  * This is just a convenience subroutine to avoid duplicated code in
615  * XLogReadRecord. It's not intended for use from anywhere else.
616  */
617 static bool
619  XLogRecPtr PrevRecPtr, XLogRecord *record,
620  bool randAccess)
621 {
622  if (record->xl_tot_len < SizeOfXLogRecord)
623  {
624  report_invalid_record(state,
625  "invalid record length at %X/%X: wanted %u, got %u",
626  (uint32) (RecPtr >> 32), (uint32) RecPtr,
627  (uint32) SizeOfXLogRecord, record->xl_tot_len);
628  return false;
629  }
630  if (record->xl_rmid > RM_MAX_ID)
631  {
632  report_invalid_record(state,
633  "invalid resource manager ID %u at %X/%X",
634  record->xl_rmid, (uint32) (RecPtr >> 32),
635  (uint32) RecPtr);
636  return false;
637  }
638  if (randAccess)
639  {
640  /*
641  * We can't exactly verify the prev-link, but surely it should be less
642  * than the record's own address.
643  */
644  if (!(record->xl_prev < RecPtr))
645  {
646  report_invalid_record(state,
647  "record with incorrect prev-link %X/%X at %X/%X",
648  (uint32) (record->xl_prev >> 32),
649  (uint32) record->xl_prev,
650  (uint32) (RecPtr >> 32), (uint32) RecPtr);
651  return false;
652  }
653  }
654  else
655  {
656  /*
657  * Record's prev-link should exactly match our previous location. This
658  * check guards against torn WAL pages where a stale but valid-looking
659  * WAL record starts on a sector boundary.
660  */
661  if (record->xl_prev != PrevRecPtr)
662  {
663  report_invalid_record(state,
664  "record with incorrect prev-link %X/%X at %X/%X",
665  (uint32) (record->xl_prev >> 32),
666  (uint32) record->xl_prev,
667  (uint32) (RecPtr >> 32), (uint32) RecPtr);
668  return false;
669  }
670  }
671 
672  return true;
673 }
674 
675 
676 /*
677  * CRC-check an XLOG record. We do not believe the contents of an XLOG
678  * record (other than to the minimal extent of computing the amount of
679  * data to read in) until we've checked the CRCs.
680  *
681  * We assume all of the record (that is, xl_tot_len bytes) has been read
682  * into memory at *record. Also, ValidXLogRecordHeader() has accepted the
683  * record's header, which means in particular that xl_tot_len is at least
684  * SizeOfXlogRecord.
685  */
686 static bool
688 {
689  pg_crc32c crc;
690 
691  /* Calculate the CRC */
692  INIT_CRC32C(crc);
693  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
694  /* include the record header last */
695  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
696  FIN_CRC32C(crc);
697 
698  if (!EQ_CRC32C(record->xl_crc, crc))
699  {
700  report_invalid_record(state,
701  "incorrect resource manager data checksum in record at %X/%X",
702  (uint32) (recptr >> 32), (uint32) recptr);
703  return false;
704  }
705 
706  return true;
707 }
708 
709 /*
710  * Validate a page header
711  */
712 static bool
714  XLogPageHeader hdr)
715 {
716  XLogRecPtr recaddr;
717  XLogSegNo segno;
718  int32 offset;
719 
720  Assert((recptr % XLOG_BLCKSZ) == 0);
721 
722  XLByteToSeg(recptr, segno);
723  offset = recptr % XLogSegSize;
724 
725  XLogSegNoOffsetToRecPtr(segno, offset, recaddr);
726 
727  if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
728  {
729  char fname[MAXFNAMELEN];
730 
731  XLogFileName(fname, state->readPageTLI, segno);
732 
733  report_invalid_record(state,
734  "invalid magic number %04X in log segment %s, offset %u",
735  hdr->xlp_magic,
736  fname,
737  offset);
738  return false;
739  }
740 
741  if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
742  {
743  char fname[MAXFNAMELEN];
744 
745  XLogFileName(fname, state->readPageTLI, segno);
746 
747  report_invalid_record(state,
748  "invalid info bits %04X in log segment %s, offset %u",
749  hdr->xlp_info,
750  fname,
751  offset);
752  return false;
753  }
754 
755  if (hdr->xlp_info & XLP_LONG_HEADER)
756  {
757  XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
758 
759  if (state->system_identifier &&
760  longhdr->xlp_sysid != state->system_identifier)
761  {
762  char fhdrident_str[32];
763  char sysident_str[32];
764 
765  /*
766  * Format sysids separately to keep platform-dependent format code
767  * out of the translatable message string.
768  */
769  snprintf(fhdrident_str, sizeof(fhdrident_str), UINT64_FORMAT,
770  longhdr->xlp_sysid);
771  snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
772  state->system_identifier);
773  report_invalid_record(state,
774  "WAL file is from different database system: WAL file database system identifier is %s, pg_control database system identifier is %s",
775  fhdrident_str, sysident_str);
776  return false;
777  }
778  else if (longhdr->xlp_seg_size != XLogSegSize)
779  {
780  report_invalid_record(state,
781  "WAL file is from different database system: incorrect XLOG_SEG_SIZE in page header");
782  return false;
783  }
784  else if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
785  {
786  report_invalid_record(state,
787  "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
788  return false;
789  }
790  }
791  else if (offset == 0)
792  {
793  char fname[MAXFNAMELEN];
794 
795  XLogFileName(fname, state->readPageTLI, segno);
796 
797  /* hmm, first page of file doesn't have a long header? */
798  report_invalid_record(state,
799  "invalid info bits %04X in log segment %s, offset %u",
800  hdr->xlp_info,
801  fname,
802  offset);
803  return false;
804  }
805 
806  if (hdr->xlp_pageaddr != recaddr)
807  {
808  char fname[MAXFNAMELEN];
809 
810  XLogFileName(fname, state->readPageTLI, segno);
811 
812  report_invalid_record(state,
813  "unexpected pageaddr %X/%X in log segment %s, offset %u",
814  (uint32) (hdr->xlp_pageaddr >> 32), (uint32) hdr->xlp_pageaddr,
815  fname,
816  offset);
817  return false;
818  }
819 
820  /*
821  * Since child timelines are always assigned a TLI greater than their
822  * immediate parent's TLI, we should never see TLI go backwards across
823  * successive pages of a consistent WAL sequence.
824  *
825  * Sometimes we re-read a segment that's already been (partially) read. So
826  * we only verify TLIs for pages that are later than the last remembered
827  * LSN.
828  */
829  if (recptr > state->latestPagePtr)
830  {
831  if (hdr->xlp_tli < state->latestPageTLI)
832  {
833  char fname[MAXFNAMELEN];
834 
835  XLogFileName(fname, state->readPageTLI, segno);
836 
837  report_invalid_record(state,
838  "out-of-sequence timeline ID %u (after %u) in log segment %s, offset %u",
839  hdr->xlp_tli,
840  state->latestPageTLI,
841  fname,
842  offset);
843  return false;
844  }
845  }
846  state->latestPagePtr = recptr;
847  state->latestPageTLI = hdr->xlp_tli;
848 
849  return true;
850 }
851 
852 #ifdef FRONTEND
853 /*
854  * Functions that are currently not needed in the backend, but are better
855  * implemented inside xlogreader.c because of the internal facilities available
856  * here.
857  */
858 
859 /*
860  * Find the first record with an lsn >= RecPtr.
861  *
862  * Useful for checking whether RecPtr is a valid xlog address for reading, and
863  * to find the first valid address after some address when dumping records for
864  * debugging purposes.
865  */
867 XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr)
868 {
869  XLogReaderState saved_state = *state;
870  XLogRecPtr tmpRecPtr;
873  char *errormsg;
874 
875  Assert(!XLogRecPtrIsInvalid(RecPtr));
876 
877  /*
878  * skip over potential continuation data, keeping in mind that it may span
879  * multiple pages
880  */
881  tmpRecPtr = RecPtr;
882  while (true)
883  {
884  XLogRecPtr targetPagePtr;
885  int targetRecOff;
886  uint32 pageHeaderSize;
887  int readLen;
888 
889  /*
890  * Compute targetRecOff. It should typically be equal or greater than
891  * short page-header since a valid record can't start anywhere before
892  * that, except when caller has explicitly specified the offset that
893  * falls somewhere there or when we are skipping multi-page
894  * continuation record. It doesn't matter though because
895  * ReadPageInternal() is prepared to handle that and will read at
896  * least short page-header worth of data
897  */
898  targetRecOff = tmpRecPtr % XLOG_BLCKSZ;
899 
900  /* scroll back to page boundary */
901  targetPagePtr = tmpRecPtr - targetRecOff;
902 
903  /* Read the page containing the record */
904  readLen = ReadPageInternal(state, targetPagePtr, targetRecOff);
905  if (readLen < 0)
906  goto err;
907 
908  header = (XLogPageHeader) state->readBuf;
909 
910  pageHeaderSize = XLogPageHeaderSize(header);
911 
912  /* make sure we have enough data for the page header */
913  readLen = ReadPageInternal(state, targetPagePtr, pageHeaderSize);
914  if (readLen < 0)
915  goto err;
916 
917  /* skip over potential continuation data */
918  if (header->xlp_info & XLP_FIRST_IS_CONTRECORD)
919  {
920  /*
921  * If the length of the remaining continuation data is more than
922  * what can fit in this page, the continuation record crosses over
923  * this page. Read the next page and try again. xlp_rem_len in the
924  * next page header will contain the remaining length of the
925  * continuation data
926  *
927  * Note that record headers are MAXALIGN'ed
928  */
929  if (MAXALIGN(header->xlp_rem_len) > (XLOG_BLCKSZ - pageHeaderSize))
930  tmpRecPtr = targetPagePtr + XLOG_BLCKSZ;
931  else
932  {
933  /*
934  * The previous continuation record ends in this page. Set
935  * tmpRecPtr to point to the first valid record
936  */
937  tmpRecPtr = targetPagePtr + pageHeaderSize
938  + MAXALIGN(header->xlp_rem_len);
939  break;
940  }
941  }
942  else
943  {
944  tmpRecPtr = targetPagePtr + pageHeaderSize;
945  break;
946  }
947  }
948 
949  /*
950  * we know now that tmpRecPtr is an address pointing to a valid XLogRecord
951  * because either we're at the first record after the beginning of a page
952  * or we just jumped over the remaining data of a continuation.
953  */
954  while (XLogReadRecord(state, tmpRecPtr, &errormsg) != NULL)
955  {
956  /* continue after the record */
957  tmpRecPtr = InvalidXLogRecPtr;
958 
959  /* past the record we've found, break out */
960  if (RecPtr <= state->ReadRecPtr)
961  {
962  found = state->ReadRecPtr;
963  goto out;
964  }
965  }
966 
967 err:
968 out:
969  /* Reset state to what we had before finding the record */
970  state->ReadRecPtr = saved_state.ReadRecPtr;
971  state->EndRecPtr = saved_state.EndRecPtr;
973 
974  return found;
975 }
976 
977 #endif /* FRONTEND */
978 
979 
980 /* ----------------------------------------
981  * Functions for decoding the data and block references in a record.
982  * ----------------------------------------
983  */
984 
985 /* private function to reset the state between records */
986 static void
988 {
989  int block_id;
990 
991  state->decoded_record = NULL;
992 
993  state->main_data_len = 0;
994 
995  for (block_id = 0; block_id <= state->max_block_id; block_id++)
996  {
997  state->blocks[block_id].in_use = false;
998  state->blocks[block_id].has_image = false;
999  state->blocks[block_id].has_data = false;
1000  state->blocks[block_id].apply_image = false;
1001  }
1002  state->max_block_id = -1;
1003 }
1004 
1005 /*
1006  * Decode the previously read record.
1007  *
1008  * On error, a human-readable error message is returned in *errormsg, and
1009  * the return value is false.
1010  */
1011 bool
1012 DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, char **errormsg)
1013 {
1014  /*
1015  * read next _size bytes from record buffer, but check for overrun first.
1016  */
1017 #define COPY_HEADER_FIELD(_dst, _size) \
1018  do { \
1019  if (remaining < _size) \
1020  goto shortdata_err; \
1021  memcpy(_dst, ptr, _size); \
1022  ptr += _size; \
1023  remaining -= _size; \
1024  } while(0)
1025 
1026  char *ptr;
1027  uint32 remaining;
1028  uint32 datatotal;
1029  RelFileNode *rnode = NULL;
1030  uint8 block_id;
1031 
1032  ResetDecoder(state);
1033 
1034  state->decoded_record = record;
1036 
1037  ptr = (char *) record;
1038  ptr += SizeOfXLogRecord;
1039  remaining = record->xl_tot_len - SizeOfXLogRecord;
1040 
1041  /* Decode the headers */
1042  datatotal = 0;
1043  while (remaining > datatotal)
1044  {
1045  COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1046 
1047  if (block_id == XLR_BLOCK_ID_DATA_SHORT)
1048  {
1049  /* XLogRecordDataHeaderShort */
1050  uint8 main_data_len;
1051 
1052  COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1053 
1054  state->main_data_len = main_data_len;
1055  datatotal += main_data_len;
1056  break; /* by convention, the main data fragment is
1057  * always last */
1058  }
1059  else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1060  {
1061  /* XLogRecordDataHeaderLong */
1062  uint32 main_data_len;
1063 
1064  COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1065  state->main_data_len = main_data_len;
1066  datatotal += main_data_len;
1067  break; /* by convention, the main data fragment is
1068  * always last */
1069  }
1070  else if (block_id == XLR_BLOCK_ID_ORIGIN)
1071  {
1072  COPY_HEADER_FIELD(&state->record_origin, sizeof(RepOriginId));
1073  }
1074  else if (block_id <= XLR_MAX_BLOCK_ID)
1075  {
1076  /* XLogRecordBlockHeader */
1077  DecodedBkpBlock *blk;
1078  uint8 fork_flags;
1079 
1080  if (block_id <= state->max_block_id)
1081  {
1082  report_invalid_record(state,
1083  "out-of-order block_id %u at %X/%X",
1084  block_id,
1085  (uint32) (state->ReadRecPtr >> 32),
1086  (uint32) state->ReadRecPtr);
1087  goto err;
1088  }
1089  state->max_block_id = block_id;
1090 
1091  blk = &state->blocks[block_id];
1092  blk->in_use = true;
1093  blk->apply_image = false;
1094 
1095  COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1096  blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1097  blk->flags = fork_flags;
1098  blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1099  blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1100 
1101  COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1102  /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1103  if (blk->has_data && blk->data_len == 0)
1104  {
1105  report_invalid_record(state,
1106  "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
1107  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1108  goto err;
1109  }
1110  if (!blk->has_data && blk->data_len != 0)
1111  {
1112  report_invalid_record(state,
1113  "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
1114  (unsigned int) blk->data_len,
1115  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1116  goto err;
1117  }
1118  datatotal += blk->data_len;
1119 
1120  if (blk->has_image)
1121  {
1122  COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
1123  COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
1124  COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1125 
1126  blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1127 
1128  if (blk->bimg_info & BKPIMAGE_IS_COMPRESSED)
1129  {
1130  if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1131  COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1132  else
1133  blk->hole_length = 0;
1134  }
1135  else
1136  blk->hole_length = BLCKSZ - blk->bimg_len;
1137  datatotal += blk->bimg_len;
1138 
1139  /*
1140  * cross-check that hole_offset > 0, hole_length > 0 and
1141  * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1142  */
1143  if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1144  (blk->hole_offset == 0 ||
1145  blk->hole_length == 0 ||
1146  blk->bimg_len == BLCKSZ))
1147  {
1148  report_invalid_record(state,
1149  "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
1150  (unsigned int) blk->hole_offset,
1151  (unsigned int) blk->hole_length,
1152  (unsigned int) blk->bimg_len,
1153  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1154  goto err;
1155  }
1156 
1157  /*
1158  * cross-check that hole_offset == 0 and hole_length == 0 if
1159  * the HAS_HOLE flag is not set.
1160  */
1161  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1162  (blk->hole_offset != 0 || blk->hole_length != 0))
1163  {
1164  report_invalid_record(state,
1165  "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
1166  (unsigned int) blk->hole_offset,
1167  (unsigned int) blk->hole_length,
1168  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1169  goto err;
1170  }
1171 
1172  /*
1173  * cross-check that bimg_len < BLCKSZ if the IS_COMPRESSED
1174  * flag is set.
1175  */
1176  if ((blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1177  blk->bimg_len == BLCKSZ)
1178  {
1179  report_invalid_record(state,
1180  "BKPIMAGE_IS_COMPRESSED set, but block image length %u at %X/%X",
1181  (unsigned int) blk->bimg_len,
1182  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1183  goto err;
1184  }
1185 
1186  /*
1187  * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE nor
1188  * IS_COMPRESSED flag is set.
1189  */
1190  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1191  !(blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1192  blk->bimg_len != BLCKSZ)
1193  {
1194  report_invalid_record(state,
1195  "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_IS_COMPRESSED set, but block image length is %u at %X/%X",
1196  (unsigned int) blk->data_len,
1197  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1198  goto err;
1199  }
1200  }
1201  if (!(fork_flags & BKPBLOCK_SAME_REL))
1202  {
1203  COPY_HEADER_FIELD(&blk->rnode, sizeof(RelFileNode));
1204  rnode = &blk->rnode;
1205  }
1206  else
1207  {
1208  if (rnode == NULL)
1209  {
1210  report_invalid_record(state,
1211  "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
1212  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1213  goto err;
1214  }
1215 
1216  blk->rnode = *rnode;
1217  }
1218  COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1219  }
1220  else
1221  {
1222  report_invalid_record(state,
1223  "invalid block_id %u at %X/%X",
1224  block_id,
1225  (uint32) (state->ReadRecPtr >> 32),
1226  (uint32) state->ReadRecPtr);
1227  goto err;
1228  }
1229  }
1230 
1231  if (remaining != datatotal)
1232  goto shortdata_err;
1233 
1234  /*
1235  * Ok, we've parsed the fragment headers, and verified that the total
1236  * length of the payload in the fragments is equal to the amount of data
1237  * left. Copy the data of each fragment to a separate buffer.
1238  *
1239  * We could just set up pointers into readRecordBuf, but we want to align
1240  * the data for the convenience of the callers. Backup images are not
1241  * copied, however; they don't need alignment.
1242  */
1243 
1244  /* block data first */
1245  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1246  {
1247  DecodedBkpBlock *blk = &state->blocks[block_id];
1248 
1249  if (!blk->in_use)
1250  continue;
1251 
1252  Assert(blk->has_image || !blk->apply_image);
1253 
1254  if (blk->has_image)
1255  {
1256  blk->bkp_image = ptr;
1257  ptr += blk->bimg_len;
1258  }
1259  if (blk->has_data)
1260  {
1261  if (!blk->data || blk->data_len > blk->data_bufsz)
1262  {
1263  if (blk->data)
1264  pfree(blk->data);
1265  blk->data_bufsz = blk->data_len;
1266  blk->data = palloc(blk->data_bufsz);
1267  }
1268  memcpy(blk->data, ptr, blk->data_len);
1269  ptr += blk->data_len;
1270  }
1271  }
1272 
1273  /* and finally, the main data */
1274  if (state->main_data_len > 0)
1275  {
1276  if (!state->main_data || state->main_data_len > state->main_data_bufsz)
1277  {
1278  if (state->main_data)
1279  pfree(state->main_data);
1280  state->main_data_bufsz = state->main_data_len;
1281  state->main_data = palloc(state->main_data_bufsz);
1282  }
1283  memcpy(state->main_data, ptr, state->main_data_len);
1284  ptr += state->main_data_len;
1285  }
1286 
1287  return true;
1288 
1289 shortdata_err:
1290  report_invalid_record(state,
1291  "record with invalid length at %X/%X",
1292  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1293 err:
1294  *errormsg = state->errormsg_buf;
1295 
1296  return false;
1297 }
1298 
1299 /*
1300  * Returns information about the block that a block reference refers to.
1301  *
1302  * If the WAL record contains a block reference with the given ID, *rnode,
1303  * *forknum, and *blknum are filled in (if not NULL), and returns TRUE.
1304  * Otherwise returns FALSE.
1305  */
1306 bool
1308  RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
1309 {
1310  DecodedBkpBlock *bkpb;
1311 
1312  if (!record->blocks[block_id].in_use)
1313  return false;
1314 
1315  bkpb = &record->blocks[block_id];
1316  if (rnode)
1317  *rnode = bkpb->rnode;
1318  if (forknum)
1319  *forknum = bkpb->forknum;
1320  if (blknum)
1321  *blknum = bkpb->blkno;
1322  return true;
1323 }
1324 
1325 /*
1326  * Returns the data associated with a block reference, or NULL if there is
1327  * no data (e.g. because a full-page image was taken instead). The returned
1328  * pointer points to a MAXALIGNed buffer.
1329  */
1330 char *
1332 {
1333  DecodedBkpBlock *bkpb;
1334 
1335  if (!record->blocks[block_id].in_use)
1336  return NULL;
1337 
1338  bkpb = &record->blocks[block_id];
1339 
1340  if (!bkpb->has_data)
1341  {
1342  if (len)
1343  *len = 0;
1344  return NULL;
1345  }
1346  else
1347  {
1348  if (len)
1349  *len = bkpb->data_len;
1350  return bkpb->data;
1351  }
1352 }
1353 
1354 /*
1355  * Restore a full-page image from a backup block attached to an XLOG record.
1356  *
1357  * Returns the buffer number containing the page.
1358  */
1359 bool
1360 RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
1361 {
1362  DecodedBkpBlock *bkpb;
1363  char *ptr;
1364  char tmp[BLCKSZ];
1365 
1366  if (!record->blocks[block_id].in_use)
1367  return false;
1368  if (!record->blocks[block_id].has_image)
1369  return false;
1370 
1371  bkpb = &record->blocks[block_id];
1372  ptr = bkpb->bkp_image;
1373 
1374  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1375  {
1376  /* If a backup block image is compressed, decompress it */
1377  if (pglz_decompress(ptr, bkpb->bimg_len, tmp,
1378  BLCKSZ - bkpb->hole_length) < 0)
1379  {
1380  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1381  (uint32) (record->ReadRecPtr >> 32),
1382  (uint32) record->ReadRecPtr,
1383  block_id);
1384  return false;
1385  }
1386  ptr = tmp;
1387  }
1388 
1389  /* generate page, taking into account hole if necessary */
1390  if (bkpb->hole_length == 0)
1391  {
1392  memcpy(page, ptr, BLCKSZ);
1393  }
1394  else
1395  {
1396  memcpy(page, ptr, bkpb->hole_offset);
1397  /* must zero-fill the hole */
1398  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1399  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1400  ptr + bkpb->hole_offset,
1401  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1402  }
1403 
1404  return true;
1405 }
int remaining
Definition: informix.c:692
BlockNumber blkno
Definition: xlogreader.h:48
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
XLogReaderState * XLogReaderAllocate(XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:67
#define XLogSegSize
Definition: xlog_internal.h:92
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define BKPIMAGE_HAS_HOLE
Definition: xlogrecord.h:147
XLogPageReadCB read_page
Definition: xlogreader.h:97
char * readRecordBuf
Definition: xlogreader.h:184
TimeLineID readPageTLI
Definition: xlogreader.h:153
#define XLogPageHeaderSize(hdr)
Definition: xlog_internal.h:85
#define XLR_BLOCK_ID_DATA_LONG
Definition: xlogrecord.h:225
static XLogRecPtr ReadRecPtr
Definition: xlog.c:817
uint32 pg_crc32c
Definition: pg_crc32c.h:38
static bool ValidXLogPageHeader(XLogReaderState *state, XLogRecPtr recptr, XLogPageHeader hdr)
Definition: xlogreader.c:713
#define Min(x, y)
Definition: c.h:806
uint16 hole_offset
Definition: xlogreader.h:57
#define XLogFileName(fname, tli, logSegNo)
unsigned char uint8
Definition: c.h:266
uint16 RepOriginId
Definition: xlogdefs.h:51
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:909
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:50
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
#define MemSet(start, val, len)
Definition: c.h:857
RmgrId xl_rmid
Definition: xlogrecord.h:47
XLogPageHeaderData * XLogPageHeader
Definition: xlog_internal.h:57
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
uint32 BlockNumber
Definition: block.h:31
void * private_data
Definition: xlogreader.h:108
uint16 bimg_len
Definition: xlogreader.h:59
bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, char **errormsg)
Definition: xlogreader.c:1012
static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
Definition: xlogreader.c:503
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:43
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:193
signed int int32
Definition: c.h:256
#define pg_attribute_printf(f, a)
Definition: c.h:638
#define XLogSegNoOffsetToRecPtr(segno, offset, dest)
Definition: xlog_internal.h:95
XLogRecPtr EndRecPtr
Definition: xlogreader.h:115
XLogLongPageHeaderData * XLogLongPageHeader
Definition: xlog_internal.h:74
int int vsnprintf(char *str, size_t count, const char *fmt, va_list args)
unsigned short uint16
Definition: c.h:267
void pfree(void *pointer)
Definition: mcxt.c:950
XLogRecPtr latestPagePtr
Definition: xlogreader.h:159
static uint32 readOff
Definition: xlog.c:784
uint16 hole_length
Definition: xlogreader.h:58
uint32 xl_tot_len
Definition: xlogrecord.h:43
#define XLOG_PAGE_MAGIC
Definition: xlog_internal.h:34
uint32 main_data_len
Definition: xlogreader.h:128
static bool allocate_recordbuf(XLogReaderState *state, uint32 reclength)
Definition: xlogreader.c:156
#define BKPIMAGE_APPLY
Definition: xlogrecord.h:149
uint64 XLogSegNo
Definition: xlogdefs.h:34
XLogRecPtr ReadRecPtr
Definition: xlogreader.h:114
XLogRecord * decoded_record
Definition: xlogreader.h:125
#define COPY_HEADER_FIELD(_dst, _size)
unsigned int uint32
Definition: c.h:268
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
ForkNumber
Definition: relpath.h:24
void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:604
TimeLineID xlp_tli
Definition: xlog_internal.h:40
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:222
XLogRecPtr xlp_pageaddr
Definition: xlog_internal.h:41
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint32 readRecordBufSize
Definition: xlogreader.h:185
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
void XLogReaderFree(XLogReaderState *state)
Definition: xlogreader.c:125
#define MAXFNAMELEN
#define RM_MAX_ID
Definition: rmgr.h:33
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1307
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1331
#define BKPBLOCK_SAME_REL
Definition: xlogrecord.h:184
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:148
int32 pglz_decompress(const char *source, int32 slen, char *dest, int32 rawsize)
#define BKPBLOCK_HAS_IMAGE
Definition: xlogrecord.h:181
ForkNumber forknum
Definition: xlogreader.h:47
#define XLP_ALL_FLAGS
Definition: xlog_internal.h:83
uint16 data_len
Definition: xlogreader.h:65
XLogRecPtr currRecPtr
Definition: xlogreader.h:163
#define Max(x, y)
Definition: c.h:800
#define XLByteToSeg(xlrp, logSegNo)
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:675
#define XLR_INFO_MASK
Definition: xlogrecord.h:62
#define XLP_LONG_HEADER
Definition: xlog_internal.h:79
Definition: regguts.h:298
static bool ValidXLogRecordHeader(XLogReaderState *state, XLogRecPtr RecPtr, XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess)
Definition: xlogreader.c:618
XLogSegNo readSegNo
Definition: xlogreader.h:151
uint16 data_bufsz
Definition: xlogreader.h:66
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:19
WalTimeSample buffer[LAG_TRACKER_BUFFER_SIZE]
Definition: walsender.c:214
int(* XLogPageReadCB)(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf, TimeLineID *pageTLI)
Definition: xlogreader.h:33
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
size_t Size
Definition: c.h:356
uint8 xl_info
Definition: xlogrecord.h:46
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:226
#define XLP_FIRST_IS_CONTRECORD
Definition: xlog_internal.h:77
#define MAXALIGN(LEN)
Definition: c.h:588
pg_crc32c xl_crc
Definition: xlogrecord.h:49
#define XLOG_SWITCH
Definition: pg_control.h:70
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:207
#define InvalidRepOriginId
Definition: origin.h:34
char * bkp_image
Definition: xlogreader.h:56
bool RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
Definition: xlogreader.c:1360
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:224
uint32 main_data_bufsz
Definition: xlogreader.h:129
static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
Definition: xlogreader.c:687
#define BKPBLOCK_FORK_MASK
Definition: xlogrecord.h:179
#define XRecOffIsValid(xlrp)
void * palloc(Size size)
Definition: mcxt.c:849
uint64 system_identifier
Definition: xlogreader.h:103
char * errormsg_buf
Definition: xlogreader.h:188
char * main_data
Definition: xlogreader.h:127
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:73
TimeLineID latestPageTLI
Definition: xlogreader.h:160
RelFileNode rnode
Definition: xlogreader.h:46
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:78
static uint32 readLen
Definition: xlog.c:785
#define _(x)
Definition: elog.c:84
#define UINT64_FORMAT
Definition: c.h:316
RepOriginId record_origin
Definition: xlogreader.h:131
static void static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:987
#define offsetof(type, field)
Definition: c.h:555
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:134
#define BKPBLOCK_HAS_DATA
Definition: xlogrecord.h:182