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