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