PostgreSQL Source Code  git master
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-2018, 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  XLogRecPtr PrevRecPtr, XLogRecord *record, bool randAccess);
32 static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record,
33  XLogRecPtr recptr);
35  int reqLen);
36 static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2, 3);
37 
38 static void ResetDecoder(XLogReaderState *state);
39 
40 /* size of the buffer allocated for error message. */
41 #define MAX_ERRORMSG_LEN 1000
42 
43 /*
44  * Construct a string in state->errormsg_buf explaining what's wrong with
45  * the current record being read.
46  */
47 static void
48 report_invalid_record(XLogReaderState *state, const char *fmt,...)
49 {
50  va_list args;
51 
52  fmt = _(fmt);
53 
54  va_start(args, fmt);
55  vsnprintf(state->errormsg_buf, MAX_ERRORMSG_LEN, fmt, args);
56  va_end(args);
57 }
58 
59 /*
60  * Allocate and initialize a new XLogReader.
61  *
62  * Returns NULL if the xlogreader couldn't be allocated.
63  */
66  void *private_data)
67 {
68  XLogReaderState *state;
69 
70  state = (XLogReaderState *)
73  if (!state)
74  return NULL;
75 
76  state->max_block_id = -1;
77 
78  /*
79  * Permanently allocate readBuf. We do it this way, rather than just
80  * making a static array, for two reasons: (1) no need to waste the
81  * storage in most instantiations of the backend; (2) a static char array
82  * isn't guaranteed to have any particular alignment, whereas
83  * palloc_extended() will provide MAXALIGN'd storage.
84  */
85  state->readBuf = (char *) palloc_extended(XLOG_BLCKSZ,
87  if (!state->readBuf)
88  {
89  pfree(state);
90  return NULL;
91  }
92 
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 += state->wal_segment_size - 1;
470  state->EndRecPtr -= XLogSegmentOffset(state->EndRecPtr, state->wal_segment_size);
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, state->wal_segment_size);
513  targetPageOff = XLogSegmentOffset(pageptr, state->wal_segment_size);
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  XLogRecPtr targetSegmentPtr = pageptr - targetPageOff;
535 
536  readLen = state->read_page(state, targetSegmentPtr, XLOG_BLCKSZ,
537  state->currRecPtr,
538  state->readBuf, &state->readPageTLI);
539  if (readLen < 0)
540  goto err;
541 
542  /* we can be sure to have enough WAL available, we scrolled back */
543  Assert(readLen == XLOG_BLCKSZ);
544 
545  if (!XLogReaderValidatePageHeader(state, targetSegmentPtr,
546  state->readBuf))
547  goto err;
548  }
549 
550  /*
551  * First, read the requested data length, but at least a short page header
552  * so that we can validate it.
553  */
554  readLen = state->read_page(state, pageptr, Max(reqLen, SizeOfXLogShortPHD),
555  state->currRecPtr,
556  state->readBuf, &state->readPageTLI);
557  if (readLen < 0)
558  goto err;
559 
560  Assert(readLen <= XLOG_BLCKSZ);
561 
562  /* Do we have enough data to check the header length? */
563  if (readLen <= SizeOfXLogShortPHD)
564  goto err;
565 
566  Assert(readLen >= reqLen);
567 
568  hdr = (XLogPageHeader) state->readBuf;
569 
570  /* still not enough */
571  if (readLen < XLogPageHeaderSize(hdr))
572  {
573  readLen = state->read_page(state, pageptr, XLogPageHeaderSize(hdr),
574  state->currRecPtr,
575  state->readBuf, &state->readPageTLI);
576  if (readLen < 0)
577  goto err;
578  }
579 
580  /*
581  * Now that we know we have the full header, validate it.
582  */
583  if (!XLogReaderValidatePageHeader(state, pageptr, (char *) hdr))
584  goto err;
585 
586  /* update read state information */
587  state->readSegNo = targetSegNo;
588  state->readOff = targetPageOff;
589  state->readLen = readLen;
590 
591  return readLen;
592 
593 err:
595  return -1;
596 }
597 
598 /*
599  * Invalidate the xlogreader's read state to force a re-read.
600  */
601 void
603 {
604  state->readSegNo = 0;
605  state->readOff = 0;
606  state->readLen = 0;
607 }
608 
609 /*
610  * Validate an XLOG record header.
611  *
612  * This is just a convenience subroutine to avoid duplicated code in
613  * XLogReadRecord. It's not intended for use from anywhere else.
614  */
615 static bool
617  XLogRecPtr PrevRecPtr, XLogRecord *record,
618  bool randAccess)
619 {
620  if (record->xl_tot_len < SizeOfXLogRecord)
621  {
622  report_invalid_record(state,
623  "invalid record length at %X/%X: wanted %u, got %u",
624  (uint32) (RecPtr >> 32), (uint32) RecPtr,
625  (uint32) SizeOfXLogRecord, record->xl_tot_len);
626  return false;
627  }
628  if (record->xl_rmid > RM_MAX_ID)
629  {
630  report_invalid_record(state,
631  "invalid resource manager ID %u at %X/%X",
632  record->xl_rmid, (uint32) (RecPtr >> 32),
633  (uint32) RecPtr);
634  return false;
635  }
636  if (randAccess)
637  {
638  /*
639  * We can't exactly verify the prev-link, but surely it should be less
640  * than the record's own address.
641  */
642  if (!(record->xl_prev < RecPtr))
643  {
644  report_invalid_record(state,
645  "record with incorrect prev-link %X/%X at %X/%X",
646  (uint32) (record->xl_prev >> 32),
647  (uint32) record->xl_prev,
648  (uint32) (RecPtr >> 32), (uint32) RecPtr);
649  return false;
650  }
651  }
652  else
653  {
654  /*
655  * Record's prev-link should exactly match our previous location. This
656  * check guards against torn WAL pages where a stale but valid-looking
657  * WAL record starts on a sector boundary.
658  */
659  if (record->xl_prev != PrevRecPtr)
660  {
661  report_invalid_record(state,
662  "record with incorrect prev-link %X/%X at %X/%X",
663  (uint32) (record->xl_prev >> 32),
664  (uint32) record->xl_prev,
665  (uint32) (RecPtr >> 32), (uint32) RecPtr);
666  return false;
667  }
668  }
669 
670  return true;
671 }
672 
673 
674 /*
675  * CRC-check an XLOG record. We do not believe the contents of an XLOG
676  * record (other than to the minimal extent of computing the amount of
677  * data to read in) until we've checked the CRCs.
678  *
679  * We assume all of the record (that is, xl_tot_len bytes) has been read
680  * into memory at *record. Also, ValidXLogRecordHeader() has accepted the
681  * record's header, which means in particular that xl_tot_len is at least
682  * SizeOfXlogRecord.
683  */
684 static bool
686 {
687  pg_crc32c crc;
688 
689  /* Calculate the CRC */
690  INIT_CRC32C(crc);
691  COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);
692  /* include the record header last */
693  COMP_CRC32C(crc, (char *) record, offsetof(XLogRecord, xl_crc));
694  FIN_CRC32C(crc);
695 
696  if (!EQ_CRC32C(record->xl_crc, crc))
697  {
698  report_invalid_record(state,
699  "incorrect resource manager data checksum in record at %X/%X",
700  (uint32) (recptr >> 32), (uint32) recptr);
701  return false;
702  }
703 
704  return true;
705 }
706 
707 /*
708  * Validate a page header.
709  *
710  * Check if 'phdr' is valid as the header of the XLog page at position
711  * 'recptr'.
712  */
713 bool
715  char *phdr)
716 {
717  XLogRecPtr recaddr;
718  XLogSegNo segno;
719  int32 offset;
720  XLogPageHeader hdr = (XLogPageHeader) phdr;
721 
722  Assert((recptr % XLOG_BLCKSZ) == 0);
723 
724  XLByteToSeg(recptr, segno, state->wal_segment_size);
725  offset = XLogSegmentOffset(recptr, state->wal_segment_size);
726 
727  XLogSegNoOffsetToRecPtr(segno, offset, recaddr, state->wal_segment_size);
728 
729  if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
730  {
731  char fname[MAXFNAMELEN];
732 
733  XLogFileName(fname, state->readPageTLI, segno, state->wal_segment_size);
734 
735  report_invalid_record(state,
736  "invalid magic number %04X in log segment %s, offset %u",
737  hdr->xlp_magic,
738  fname,
739  offset);
740  return false;
741  }
742 
743  if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
744  {
745  char fname[MAXFNAMELEN];
746 
747  XLogFileName(fname, state->readPageTLI, segno, state->wal_segment_size);
748 
749  report_invalid_record(state,
750  "invalid info bits %04X in log segment %s, offset %u",
751  hdr->xlp_info,
752  fname,
753  offset);
754  return false;
755  }
756 
757  if (hdr->xlp_info & XLP_LONG_HEADER)
758  {
759  XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
760 
761  if (state->system_identifier &&
762  longhdr->xlp_sysid != state->system_identifier)
763  {
764  char fhdrident_str[32];
765  char sysident_str[32];
766 
767  /*
768  * Format sysids separately to keep platform-dependent format code
769  * out of the translatable message string.
770  */
771  snprintf(fhdrident_str, sizeof(fhdrident_str), UINT64_FORMAT,
772  longhdr->xlp_sysid);
773  snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
774  state->system_identifier);
775  report_invalid_record(state,
776  "WAL file is from different database system: WAL file database system identifier is %s, pg_control database system identifier is %s",
777  fhdrident_str, sysident_str);
778  return false;
779  }
780  else if (longhdr->xlp_seg_size != state->wal_segment_size)
781  {
782  report_invalid_record(state,
783  "WAL file is from different database system: incorrect segment size in page header");
784  return false;
785  }
786  else if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
787  {
788  report_invalid_record(state,
789  "WAL file is from different database system: incorrect XLOG_BLCKSZ in page header");
790  return false;
791  }
792  }
793  else if (offset == 0)
794  {
795  char fname[MAXFNAMELEN];
796 
797  XLogFileName(fname, state->readPageTLI, segno, state->wal_segment_size);
798 
799  /* hmm, first page of file doesn't have a long header? */
800  report_invalid_record(state,
801  "invalid info bits %04X in log segment %s, offset %u",
802  hdr->xlp_info,
803  fname,
804  offset);
805  return false;
806  }
807 
808  /*
809  * Check that the address on the page agrees with what we expected.
810  * This check typically fails when an old WAL segment is recycled,
811  * and hasn't yet been overwritten with new data yet.
812  */
813  if (hdr->xlp_pageaddr != recaddr)
814  {
815  char fname[MAXFNAMELEN];
816 
817  XLogFileName(fname, state->readPageTLI, segno, state->wal_segment_size);
818 
819  report_invalid_record(state,
820  "unexpected pageaddr %X/%X in log segment %s, offset %u",
821  (uint32) (hdr->xlp_pageaddr >> 32), (uint32) hdr->xlp_pageaddr,
822  fname,
823  offset);
824  return false;
825  }
826 
827  /*
828  * Since child timelines are always assigned a TLI greater than their
829  * immediate parent's TLI, we should never see TLI go backwards across
830  * successive pages of a consistent WAL sequence.
831  *
832  * Sometimes we re-read a segment that's already been (partially) read. So
833  * we only verify TLIs for pages that are later than the last remembered
834  * LSN.
835  */
836  if (recptr > state->latestPagePtr)
837  {
838  if (hdr->xlp_tli < state->latestPageTLI)
839  {
840  char fname[MAXFNAMELEN];
841 
842  XLogFileName(fname, state->readPageTLI, segno, state->wal_segment_size);
843 
844  report_invalid_record(state,
845  "out-of-sequence timeline ID %u (after %u) in log segment %s, offset %u",
846  hdr->xlp_tli,
847  state->latestPageTLI,
848  fname,
849  offset);
850  return false;
851  }
852  }
853  state->latestPagePtr = recptr;
854  state->latestPageTLI = hdr->xlp_tli;
855 
856  return true;
857 }
858 
859 #ifdef FRONTEND
860 /*
861  * Functions that are currently not needed in the backend, but are better
862  * implemented inside xlogreader.c because of the internal facilities available
863  * here.
864  */
865 
866 /*
867  * Find the first record with an lsn >= RecPtr.
868  *
869  * Useful for checking whether RecPtr is a valid xlog address for reading, and
870  * to find the first valid address after some address when dumping records for
871  * debugging purposes.
872  */
874 XLogFindNextRecord(XLogReaderState *state, XLogRecPtr RecPtr)
875 {
876  XLogReaderState saved_state = *state;
877  XLogRecPtr tmpRecPtr;
880  char *errormsg;
881 
882  Assert(!XLogRecPtrIsInvalid(RecPtr));
883 
884  /*
885  * skip over potential continuation data, keeping in mind that it may span
886  * multiple pages
887  */
888  tmpRecPtr = RecPtr;
889  while (true)
890  {
891  XLogRecPtr targetPagePtr;
892  int targetRecOff;
893  uint32 pageHeaderSize;
894  int readLen;
895 
896  /*
897  * Compute targetRecOff. It should typically be equal or greater than
898  * short page-header since a valid record can't start anywhere before
899  * that, except when caller has explicitly specified the offset that
900  * falls somewhere there or when we are skipping multi-page
901  * continuation record. It doesn't matter though because
902  * ReadPageInternal() is prepared to handle that and will read at
903  * least short page-header worth of data
904  */
905  targetRecOff = tmpRecPtr % XLOG_BLCKSZ;
906 
907  /* scroll back to page boundary */
908  targetPagePtr = tmpRecPtr - targetRecOff;
909 
910  /* Read the page containing the record */
911  readLen = ReadPageInternal(state, targetPagePtr, targetRecOff);
912  if (readLen < 0)
913  goto err;
914 
915  header = (XLogPageHeader) state->readBuf;
916 
917  pageHeaderSize = XLogPageHeaderSize(header);
918 
919  /* make sure we have enough data for the page header */
920  readLen = ReadPageInternal(state, targetPagePtr, pageHeaderSize);
921  if (readLen < 0)
922  goto err;
923 
924  /* skip over potential continuation data */
925  if (header->xlp_info & XLP_FIRST_IS_CONTRECORD)
926  {
927  /*
928  * If the length of the remaining continuation data is more than
929  * what can fit in this page, the continuation record crosses over
930  * this page. Read the next page and try again. xlp_rem_len in the
931  * next page header will contain the remaining length of the
932  * continuation data
933  *
934  * Note that record headers are MAXALIGN'ed
935  */
936  if (MAXALIGN(header->xlp_rem_len) > (XLOG_BLCKSZ - pageHeaderSize))
937  tmpRecPtr = targetPagePtr + XLOG_BLCKSZ;
938  else
939  {
940  /*
941  * The previous continuation record ends in this page. Set
942  * tmpRecPtr to point to the first valid record
943  */
944  tmpRecPtr = targetPagePtr + pageHeaderSize
945  + MAXALIGN(header->xlp_rem_len);
946  break;
947  }
948  }
949  else
950  {
951  tmpRecPtr = targetPagePtr + pageHeaderSize;
952  break;
953  }
954  }
955 
956  /*
957  * we know now that tmpRecPtr is an address pointing to a valid XLogRecord
958  * because either we're at the first record after the beginning of a page
959  * or we just jumped over the remaining data of a continuation.
960  */
961  while (XLogReadRecord(state, tmpRecPtr, &errormsg) != NULL)
962  {
963  /* continue after the record */
964  tmpRecPtr = InvalidXLogRecPtr;
965 
966  /* past the record we've found, break out */
967  if (RecPtr <= state->ReadRecPtr)
968  {
969  found = state->ReadRecPtr;
970  goto out;
971  }
972  }
973 
974 err:
975 out:
976  /* Reset state to what we had before finding the record */
977  state->ReadRecPtr = saved_state.ReadRecPtr;
978  state->EndRecPtr = saved_state.EndRecPtr;
980 
981  return found;
982 }
983 
984 #endif /* FRONTEND */
985 
986 
987 /* ----------------------------------------
988  * Functions for decoding the data and block references in a record.
989  * ----------------------------------------
990  */
991 
992 /* private function to reset the state between records */
993 static void
995 {
996  int block_id;
997 
998  state->decoded_record = NULL;
999 
1000  state->main_data_len = 0;
1001 
1002  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1003  {
1004  state->blocks[block_id].in_use = false;
1005  state->blocks[block_id].has_image = false;
1006  state->blocks[block_id].has_data = false;
1007  state->blocks[block_id].apply_image = false;
1008  }
1009  state->max_block_id = -1;
1010 }
1011 
1012 /*
1013  * Decode the previously read record.
1014  *
1015  * On error, a human-readable error message is returned in *errormsg, and
1016  * the return value is false.
1017  */
1018 bool
1019 DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, char **errormsg)
1020 {
1021  /*
1022  * read next _size bytes from record buffer, but check for overrun first.
1023  */
1024 #define COPY_HEADER_FIELD(_dst, _size) \
1025  do { \
1026  if (remaining < _size) \
1027  goto shortdata_err; \
1028  memcpy(_dst, ptr, _size); \
1029  ptr += _size; \
1030  remaining -= _size; \
1031  } while(0)
1032 
1033  char *ptr;
1034  uint32 remaining;
1035  uint32 datatotal;
1036  RelFileNode *rnode = NULL;
1037  uint8 block_id;
1038 
1039  ResetDecoder(state);
1040 
1041  state->decoded_record = record;
1043 
1044  ptr = (char *) record;
1045  ptr += SizeOfXLogRecord;
1046  remaining = record->xl_tot_len - SizeOfXLogRecord;
1047 
1048  /* Decode the headers */
1049  datatotal = 0;
1050  while (remaining > datatotal)
1051  {
1052  COPY_HEADER_FIELD(&block_id, sizeof(uint8));
1053 
1054  if (block_id == XLR_BLOCK_ID_DATA_SHORT)
1055  {
1056  /* XLogRecordDataHeaderShort */
1057  uint8 main_data_len;
1058 
1059  COPY_HEADER_FIELD(&main_data_len, sizeof(uint8));
1060 
1061  state->main_data_len = main_data_len;
1062  datatotal += main_data_len;
1063  break; /* by convention, the main data fragment is
1064  * always last */
1065  }
1066  else if (block_id == XLR_BLOCK_ID_DATA_LONG)
1067  {
1068  /* XLogRecordDataHeaderLong */
1069  uint32 main_data_len;
1070 
1071  COPY_HEADER_FIELD(&main_data_len, sizeof(uint32));
1072  state->main_data_len = main_data_len;
1073  datatotal += main_data_len;
1074  break; /* by convention, the main data fragment is
1075  * always last */
1076  }
1077  else if (block_id == XLR_BLOCK_ID_ORIGIN)
1078  {
1079  COPY_HEADER_FIELD(&state->record_origin, sizeof(RepOriginId));
1080  }
1081  else if (block_id <= XLR_MAX_BLOCK_ID)
1082  {
1083  /* XLogRecordBlockHeader */
1084  DecodedBkpBlock *blk;
1085  uint8 fork_flags;
1086 
1087  if (block_id <= state->max_block_id)
1088  {
1089  report_invalid_record(state,
1090  "out-of-order block_id %u at %X/%X",
1091  block_id,
1092  (uint32) (state->ReadRecPtr >> 32),
1093  (uint32) state->ReadRecPtr);
1094  goto err;
1095  }
1096  state->max_block_id = block_id;
1097 
1098  blk = &state->blocks[block_id];
1099  blk->in_use = true;
1100  blk->apply_image = false;
1101 
1102  COPY_HEADER_FIELD(&fork_flags, sizeof(uint8));
1103  blk->forknum = fork_flags & BKPBLOCK_FORK_MASK;
1104  blk->flags = fork_flags;
1105  blk->has_image = ((fork_flags & BKPBLOCK_HAS_IMAGE) != 0);
1106  blk->has_data = ((fork_flags & BKPBLOCK_HAS_DATA) != 0);
1107 
1108  COPY_HEADER_FIELD(&blk->data_len, sizeof(uint16));
1109  /* cross-check that the HAS_DATA flag is set iff data_length > 0 */
1110  if (blk->has_data && blk->data_len == 0)
1111  {
1112  report_invalid_record(state,
1113  "BKPBLOCK_HAS_DATA set, but no data included at %X/%X",
1114  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1115  goto err;
1116  }
1117  if (!blk->has_data && blk->data_len != 0)
1118  {
1119  report_invalid_record(state,
1120  "BKPBLOCK_HAS_DATA not set, but data length is %u at %X/%X",
1121  (unsigned int) blk->data_len,
1122  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1123  goto err;
1124  }
1125  datatotal += blk->data_len;
1126 
1127  if (blk->has_image)
1128  {
1129  COPY_HEADER_FIELD(&blk->bimg_len, sizeof(uint16));
1130  COPY_HEADER_FIELD(&blk->hole_offset, sizeof(uint16));
1131  COPY_HEADER_FIELD(&blk->bimg_info, sizeof(uint8));
1132 
1133  blk->apply_image = ((blk->bimg_info & BKPIMAGE_APPLY) != 0);
1134 
1135  if (blk->bimg_info & BKPIMAGE_IS_COMPRESSED)
1136  {
1137  if (blk->bimg_info & BKPIMAGE_HAS_HOLE)
1138  COPY_HEADER_FIELD(&blk->hole_length, sizeof(uint16));
1139  else
1140  blk->hole_length = 0;
1141  }
1142  else
1143  blk->hole_length = BLCKSZ - blk->bimg_len;
1144  datatotal += blk->bimg_len;
1145 
1146  /*
1147  * cross-check that hole_offset > 0, hole_length > 0 and
1148  * bimg_len < BLCKSZ if the HAS_HOLE flag is set.
1149  */
1150  if ((blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1151  (blk->hole_offset == 0 ||
1152  blk->hole_length == 0 ||
1153  blk->bimg_len == BLCKSZ))
1154  {
1155  report_invalid_record(state,
1156  "BKPIMAGE_HAS_HOLE set, but hole offset %u length %u block image length %u at %X/%X",
1157  (unsigned int) blk->hole_offset,
1158  (unsigned int) blk->hole_length,
1159  (unsigned int) blk->bimg_len,
1160  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1161  goto err;
1162  }
1163 
1164  /*
1165  * cross-check that hole_offset == 0 and hole_length == 0 if
1166  * the HAS_HOLE flag is not set.
1167  */
1168  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1169  (blk->hole_offset != 0 || blk->hole_length != 0))
1170  {
1171  report_invalid_record(state,
1172  "BKPIMAGE_HAS_HOLE not set, but hole offset %u length %u at %X/%X",
1173  (unsigned int) blk->hole_offset,
1174  (unsigned int) blk->hole_length,
1175  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1176  goto err;
1177  }
1178 
1179  /*
1180  * cross-check that bimg_len < BLCKSZ if the IS_COMPRESSED
1181  * flag is set.
1182  */
1183  if ((blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1184  blk->bimg_len == BLCKSZ)
1185  {
1186  report_invalid_record(state,
1187  "BKPIMAGE_IS_COMPRESSED set, but block image length %u at %X/%X",
1188  (unsigned int) blk->bimg_len,
1189  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1190  goto err;
1191  }
1192 
1193  /*
1194  * cross-check that bimg_len = BLCKSZ if neither HAS_HOLE nor
1195  * IS_COMPRESSED flag is set.
1196  */
1197  if (!(blk->bimg_info & BKPIMAGE_HAS_HOLE) &&
1198  !(blk->bimg_info & BKPIMAGE_IS_COMPRESSED) &&
1199  blk->bimg_len != BLCKSZ)
1200  {
1201  report_invalid_record(state,
1202  "neither BKPIMAGE_HAS_HOLE nor BKPIMAGE_IS_COMPRESSED set, but block image length is %u at %X/%X",
1203  (unsigned int) blk->data_len,
1204  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1205  goto err;
1206  }
1207  }
1208  if (!(fork_flags & BKPBLOCK_SAME_REL))
1209  {
1210  COPY_HEADER_FIELD(&blk->rnode, sizeof(RelFileNode));
1211  rnode = &blk->rnode;
1212  }
1213  else
1214  {
1215  if (rnode == NULL)
1216  {
1217  report_invalid_record(state,
1218  "BKPBLOCK_SAME_REL set but no previous rel at %X/%X",
1219  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1220  goto err;
1221  }
1222 
1223  blk->rnode = *rnode;
1224  }
1225  COPY_HEADER_FIELD(&blk->blkno, sizeof(BlockNumber));
1226  }
1227  else
1228  {
1229  report_invalid_record(state,
1230  "invalid block_id %u at %X/%X",
1231  block_id,
1232  (uint32) (state->ReadRecPtr >> 32),
1233  (uint32) state->ReadRecPtr);
1234  goto err;
1235  }
1236  }
1237 
1238  if (remaining != datatotal)
1239  goto shortdata_err;
1240 
1241  /*
1242  * Ok, we've parsed the fragment headers, and verified that the total
1243  * length of the payload in the fragments is equal to the amount of data
1244  * left. Copy the data of each fragment to a separate buffer.
1245  *
1246  * We could just set up pointers into readRecordBuf, but we want to align
1247  * the data for the convenience of the callers. Backup images are not
1248  * copied, however; they don't need alignment.
1249  */
1250 
1251  /* block data first */
1252  for (block_id = 0; block_id <= state->max_block_id; block_id++)
1253  {
1254  DecodedBkpBlock *blk = &state->blocks[block_id];
1255 
1256  if (!blk->in_use)
1257  continue;
1258 
1259  Assert(blk->has_image || !blk->apply_image);
1260 
1261  if (blk->has_image)
1262  {
1263  blk->bkp_image = ptr;
1264  ptr += blk->bimg_len;
1265  }
1266  if (blk->has_data)
1267  {
1268  if (!blk->data || blk->data_len > blk->data_bufsz)
1269  {
1270  if (blk->data)
1271  pfree(blk->data);
1272 
1273  /*
1274  * Force the initial request to be BLCKSZ so that we don't
1275  * waste time with lots of trips through this stanza as a
1276  * result of WAL compression.
1277  */
1278  blk->data_bufsz = MAXALIGN(Max(blk->data_len, BLCKSZ));
1279  blk->data = palloc(blk->data_bufsz);
1280  }
1281  memcpy(blk->data, ptr, blk->data_len);
1282  ptr += blk->data_len;
1283  }
1284  }
1285 
1286  /* and finally, the main data */
1287  if (state->main_data_len > 0)
1288  {
1289  if (!state->main_data || state->main_data_len > state->main_data_bufsz)
1290  {
1291  if (state->main_data)
1292  pfree(state->main_data);
1293 
1294  /*
1295  * main_data_bufsz must be MAXALIGN'ed. In many xlog record
1296  * types, we omit trailing struct padding on-disk to save a few
1297  * bytes; but compilers may generate accesses to the xlog struct
1298  * that assume that padding bytes are present. If the palloc
1299  * request is not large enough to include such padding bytes then
1300  * we'll get valgrind complaints due to otherwise-harmless fetches
1301  * of the padding bytes.
1302  *
1303  * In addition, force the initial request to be reasonably large
1304  * so that we don't waste time with lots of trips through this
1305  * stanza. BLCKSZ / 2 seems like a good compromise choice.
1306  */
1307  state->main_data_bufsz = MAXALIGN(Max(state->main_data_len,
1308  BLCKSZ / 2));
1309  state->main_data = palloc(state->main_data_bufsz);
1310  }
1311  memcpy(state->main_data, ptr, state->main_data_len);
1312  ptr += state->main_data_len;
1313  }
1314 
1315  return true;
1316 
1317 shortdata_err:
1318  report_invalid_record(state,
1319  "record with invalid length at %X/%X",
1320  (uint32) (state->ReadRecPtr >> 32), (uint32) state->ReadRecPtr);
1321 err:
1322  *errormsg = state->errormsg_buf;
1323 
1324  return false;
1325 }
1326 
1327 /*
1328  * Returns information about the block that a block reference refers to.
1329  *
1330  * If the WAL record contains a block reference with the given ID, *rnode,
1331  * *forknum, and *blknum are filled in (if not NULL), and returns true.
1332  * Otherwise returns false.
1333  */
1334 bool
1336  RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
1337 {
1338  DecodedBkpBlock *bkpb;
1339 
1340  if (!record->blocks[block_id].in_use)
1341  return false;
1342 
1343  bkpb = &record->blocks[block_id];
1344  if (rnode)
1345  *rnode = bkpb->rnode;
1346  if (forknum)
1347  *forknum = bkpb->forknum;
1348  if (blknum)
1349  *blknum = bkpb->blkno;
1350  return true;
1351 }
1352 
1353 /*
1354  * Returns the data associated with a block reference, or NULL if there is
1355  * no data (e.g. because a full-page image was taken instead). The returned
1356  * pointer points to a MAXALIGNed buffer.
1357  */
1358 char *
1360 {
1361  DecodedBkpBlock *bkpb;
1362 
1363  if (!record->blocks[block_id].in_use)
1364  return NULL;
1365 
1366  bkpb = &record->blocks[block_id];
1367 
1368  if (!bkpb->has_data)
1369  {
1370  if (len)
1371  *len = 0;
1372  return NULL;
1373  }
1374  else
1375  {
1376  if (len)
1377  *len = bkpb->data_len;
1378  return bkpb->data;
1379  }
1380 }
1381 
1382 /*
1383  * Restore a full-page image from a backup block attached to an XLOG record.
1384  *
1385  * Returns the buffer number containing the page.
1386  */
1387 bool
1388 RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
1389 {
1390  DecodedBkpBlock *bkpb;
1391  char *ptr;
1392  char tmp[BLCKSZ];
1393 
1394  if (!record->blocks[block_id].in_use)
1395  return false;
1396  if (!record->blocks[block_id].has_image)
1397  return false;
1398 
1399  bkpb = &record->blocks[block_id];
1400  ptr = bkpb->bkp_image;
1401 
1402  if (bkpb->bimg_info & BKPIMAGE_IS_COMPRESSED)
1403  {
1404  /* If a backup block image is compressed, decompress it */
1405  if (pglz_decompress(ptr, bkpb->bimg_len, tmp,
1406  BLCKSZ - bkpb->hole_length) < 0)
1407  {
1408  report_invalid_record(record, "invalid compressed image at %X/%X, block %d",
1409  (uint32) (record->ReadRecPtr >> 32),
1410  (uint32) record->ReadRecPtr,
1411  block_id);
1412  return false;
1413  }
1414  ptr = tmp;
1415  }
1416 
1417  /* generate page, taking into account hole if necessary */
1418  if (bkpb->hole_length == 0)
1419  {
1420  memcpy(page, ptr, BLCKSZ);
1421  }
1422  else
1423  {
1424  memcpy(page, ptr, bkpb->hole_offset);
1425  /* must zero-fill the hole */
1426  MemSet(page + bkpb->hole_offset, 0, bkpb->hole_length);
1427  memcpy(page + (bkpb->hole_offset + bkpb->hole_length),
1428  ptr + bkpb->hole_offset,
1429  BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
1430  }
1431 
1432  return true;
1433 }
int remaining
Definition: informix.c:692
BlockNumber blkno
Definition: xlogreader.h:48
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
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:102
char * readRecordBuf
Definition: xlogreader.h:189
int wal_segment_size
Definition: xlog.c:113
TimeLineID readPageTLI
Definition: xlogreader.h:158
#define XLogPageHeaderSize(hdr)
Definition: xlog_internal.h:85
#define XLR_BLOCK_ID_DATA_LONG
Definition: xlogrecord.h:225
static XLogRecPtr ReadRecPtr
Definition: xlog.c:821
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define Min(x, y)
Definition: c.h:857
uint16 hole_offset
Definition: xlogreader.h:57
unsigned char uint8
Definition: c.h:323
uint16 RepOriginId
Definition: xlogdefs.h:51
void * palloc_extended(Size size, int flags)
Definition: mcxt.c:988
static void report_invalid_record(XLogReaderState *state, const char *fmt,...) pg_attribute_printf(2
Definition: xlogreader.c:48
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
#define MemSet(start, val, len)
Definition: c.h:908
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:113
uint16 bimg_len
Definition: xlogreader.h:59
bool DecodeXLogRecord(XLogReaderState *state, XLogRecord *record, char **errormsg)
Definition: xlogreader.c:1019
static int ReadPageInternal(XLogReaderState *state, XLogRecPtr pageptr, int reqLen)
Definition: xlogreader.c:503
#define MAX_ERRORMSG_LEN
Definition: xlogreader.c:41
XLogRecord * XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
Definition: xlogreader.c:193
signed int int32
Definition: c.h:313
bool XLogReaderValidatePageHeader(XLogReaderState *state, XLogRecPtr recptr, char *phdr)
Definition: xlogreader.c:714
#define pg_attribute_printf(f, a)
Definition: c.h:132
XLogRecPtr EndRecPtr
Definition: xlogreader.h:120
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:324
void pfree(void *pointer)
Definition: mcxt.c:1031
XLogRecPtr latestPagePtr
Definition: xlogreader.h:164
static uint32 readOff
Definition: xlog.c:788
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:133
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:119
XLogRecord * decoded_record
Definition: xlogreader.h:130
#define COPY_HEADER_FIELD(_dst, _size)
#define XLogSegNoOffsetToRecPtr(segno, offset, dest, wal_segsz_bytes)
unsigned int uint32
Definition: c.h:325
int wal_segment_size
Definition: xlogreader.h:79
#define EQ_CRC32C(c1, c2)
Definition: pg_crc32c.h:42
ForkNumber
Definition: relpath.h:40
void XLogReaderInvalReadState(XLogReaderState *state)
Definition: xlogreader.c:602
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:190
#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:1335
XLogReaderState * XLogReaderAllocate(int wal_segment_size, XLogPageReadCB pagereadfunc, void *private_data)
Definition: xlogreader.c:65
char * XLogRecGetBlockData(XLogReaderState *record, uint8 block_id, Size *len)
Definition: xlogreader.c:1359
#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
#define XLogSegmentOffset(xlogptr, wal_segsz_bytes)
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:168
#define Max(x, y)
Definition: c.h:851
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:699
#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:616
XLogSegNo readSegNo
Definition: xlogreader.h:156
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:215
#define SizeOfXLogShortPHD
Definition: xlog_internal.h:55
size_t Size
Definition: c.h:433
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
uint8 xl_info
Definition: xlogrecord.h:46
int(* XLogPageReadCB)(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *readBuf, TimeLineID *pageTLI)
Definition: xlogreader.h:33
#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:652
#define XLOG_SWITCH
Definition: pg_control.h:71
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:208
#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:1388
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:224
uint32 main_data_bufsz
Definition: xlogreader.h:134
static bool ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
Definition: xlogreader.c:685
#define BKPBLOCK_FORK_MASK
Definition: xlogrecord.h:179
#define XRecOffIsValid(xlrp)
void * palloc(Size size)
Definition: mcxt.c:924
uint64 system_identifier
Definition: xlogreader.h:108
char * errormsg_buf
Definition: xlogreader.h:193
char * main_data
Definition: xlogreader.h:132
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
TimeLineID latestPageTLI
Definition: xlogreader.h:165
RelFileNode rnode
Definition: xlogreader.h:46
#define FIN_CRC32C(crc)
Definition: pg_crc32c.h:94
static uint32 readLen
Definition: xlog.c:789
#define _(x)
Definition: elog.c:84
#define UINT64_FORMAT
Definition: c.h:368
RepOriginId record_origin
Definition: xlogreader.h:136
static void static void ResetDecoder(XLogReaderState *state)
Definition: xlogreader.c:994
#define offsetof(type, field)
Definition: c.h:622
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:139
#define BKPBLOCK_HAS_DATA
Definition: xlogrecord.h:182
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)