PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
xlogutils.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * xlogutils.c
4  *
5  * PostgreSQL transaction log manager utility routines
6  *
7  * This file contains support routines that are used by XLOG replay functions.
8  * None of this code is used during normal system operation.
9  *
10  *
11  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
12  * Portions Copyright (c) 1994, Regents of the University of California
13  *
14  * src/backend/access/transam/xlogutils.c
15  *
16  *-------------------------------------------------------------------------
17  */
18 #include "postgres.h"
19 
20 #include <unistd.h>
21 
22 #include "access/xlog.h"
23 #include "access/xlog_internal.h"
24 #include "access/xlogutils.h"
25 #include "catalog/catalog.h"
26 #include "miscadmin.h"
27 #include "storage/smgr.h"
28 #include "utils/guc.h"
29 #include "utils/hsearch.h"
30 #include "utils/rel.h"
31 
32 
33 /*
34  * During XLOG replay, we may see XLOG records for incremental updates of
35  * pages that no longer exist, because their relation was later dropped or
36  * truncated. (Note: this is only possible when full_page_writes = OFF,
37  * since when it's ON, the first reference we see to a page should always
38  * be a full-page rewrite not an incremental update.) Rather than simply
39  * ignoring such records, we make a note of the referenced page, and then
40  * complain if we don't actually see a drop or truncate covering the page
41  * later in replay.
42  */
43 typedef struct xl_invalid_page_key
44 {
45  RelFileNode node; /* the relation */
46  ForkNumber forkno; /* the fork number */
47  BlockNumber blkno; /* the page */
49 
50 typedef struct xl_invalid_page
51 {
52  xl_invalid_page_key key; /* hash key ... must be first */
53  bool present; /* page existed but contained zeroes */
55 
57 
58 
59 /* Report a reference to an invalid page */
60 static void
62  BlockNumber blkno, bool present)
63 {
64  char *path = relpathperm(node, forkno);
65 
66  if (present)
67  elog(elevel, "page %u of relation %s is uninitialized",
68  blkno, path);
69  else
70  elog(elevel, "page %u of relation %s does not exist",
71  blkno, path);
72  pfree(path);
73 }
74 
75 /* Log a reference to an invalid page */
76 static void
78  bool present)
79 {
81  xl_invalid_page *hentry;
82  bool found;
83 
84  /*
85  * Once recovery has reached a consistent state, the invalid-page table
86  * should be empty and remain so. If a reference to an invalid page is
87  * found after consistency is reached, PANIC immediately. This might seem
88  * aggressive, but it's better than letting the invalid reference linger
89  * in the hash table until the end of recovery and PANIC there, which
90  * might come only much later if this is a standby server.
91  */
93  {
94  report_invalid_page(WARNING, node, forkno, blkno, present);
95  elog(PANIC, "WAL contains references to invalid pages");
96  }
97 
98  /*
99  * Log references to invalid pages at DEBUG1 level. This allows some
100  * tracing of the cause (note the elog context mechanism will tell us
101  * something about the XLOG record that generated the reference).
102  */
104  report_invalid_page(DEBUG1, node, forkno, blkno, present);
105 
106  if (invalid_page_tab == NULL)
107  {
108  /* create hash table when first needed */
109  HASHCTL ctl;
110 
111  memset(&ctl, 0, sizeof(ctl));
112  ctl.keysize = sizeof(xl_invalid_page_key);
113  ctl.entrysize = sizeof(xl_invalid_page);
114 
115  invalid_page_tab = hash_create("XLOG invalid-page table",
116  100,
117  &ctl,
119  }
120 
121  /* we currently assume xl_invalid_page_key contains no padding */
122  key.node = node;
123  key.forkno = forkno;
124  key.blkno = blkno;
125  hentry = (xl_invalid_page *)
126  hash_search(invalid_page_tab, (void *) &key, HASH_ENTER, &found);
127 
128  if (!found)
129  {
130  /* hash_search already filled in the key */
131  hentry->present = present;
132  }
133  else
134  {
135  /* repeat reference ... leave "present" as it was */
136  }
137 }
138 
139 /* Forget any invalid pages >= minblkno, because they've been dropped */
140 static void
142 {
144  xl_invalid_page *hentry;
145 
146  if (invalid_page_tab == NULL)
147  return; /* nothing to do */
148 
149  hash_seq_init(&status, invalid_page_tab);
150 
151  while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
152  {
153  if (RelFileNodeEquals(hentry->key.node, node) &&
154  hentry->key.forkno == forkno &&
155  hentry->key.blkno >= minblkno)
156  {
158  {
159  char *path = relpathperm(hentry->key.node, forkno);
160 
161  elog(DEBUG2, "page %u of relation %s has been dropped",
162  hentry->key.blkno, path);
163  pfree(path);
164  }
165 
166  if (hash_search(invalid_page_tab,
167  (void *) &hentry->key,
168  HASH_REMOVE, NULL) == NULL)
169  elog(ERROR, "hash table corrupted");
170  }
171  }
172 }
173 
174 /* Forget any invalid pages in a whole database */
175 static void
177 {
179  xl_invalid_page *hentry;
180 
181  if (invalid_page_tab == NULL)
182  return; /* nothing to do */
183 
184  hash_seq_init(&status, invalid_page_tab);
185 
186  while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
187  {
188  if (hentry->key.node.dbNode == dbid)
189  {
191  {
192  char *path = relpathperm(hentry->key.node, hentry->key.forkno);
193 
194  elog(DEBUG2, "page %u of relation %s has been dropped",
195  hentry->key.blkno, path);
196  pfree(path);
197  }
198 
199  if (hash_search(invalid_page_tab,
200  (void *) &hentry->key,
201  HASH_REMOVE, NULL) == NULL)
202  elog(ERROR, "hash table corrupted");
203  }
204  }
205 }
206 
207 /* Are there any unresolved references to invalid pages? */
208 bool
210 {
211  if (invalid_page_tab != NULL &&
212  hash_get_num_entries(invalid_page_tab) > 0)
213  return true;
214  return false;
215 }
216 
217 /* Complain about any remaining invalid-page entries */
218 void
220 {
222  xl_invalid_page *hentry;
223  bool foundone = false;
224 
225  if (invalid_page_tab == NULL)
226  return; /* nothing to do */
227 
228  hash_seq_init(&status, invalid_page_tab);
229 
230  /*
231  * Our strategy is to emit WARNING messages for all remaining entries and
232  * only PANIC after we've dumped all the available info.
233  */
234  while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
235  {
236  report_invalid_page(WARNING, hentry->key.node, hentry->key.forkno,
237  hentry->key.blkno, hentry->present);
238  foundone = true;
239  }
240 
241  if (foundone)
242  elog(PANIC, "WAL contains references to invalid pages");
243 
244  hash_destroy(invalid_page_tab);
245  invalid_page_tab = NULL;
246 }
247 
248 
249 /*
250  * XLogReadBufferForRedo
251  * Read a page during XLOG replay
252  *
253  * Reads a block referenced by a WAL record into shared buffer cache, and
254  * determines what needs to be done to redo the changes to it. If the WAL
255  * record includes a full-page image of the page, it is restored.
256  *
257  * 'lsn' is the LSN of the record being replayed. It is compared with the
258  * page's LSN to determine if the record has already been replayed.
259  * 'block_id' is the ID number the block was registered with, when the WAL
260  * record was created.
261  *
262  * Returns one of the following:
263  *
264  * BLK_NEEDS_REDO - changes from the WAL record need to be applied
265  * BLK_DONE - block doesn't need replaying
266  * BLK_RESTORED - block was restored from a full-page image included in
267  * the record
268  * BLK_NOTFOUND - block was not found (because it was truncated away by
269  * an operation later in the WAL stream)
270  *
271  * On return, the buffer is locked in exclusive-mode, and returned in *buf.
272  * Note that the buffer is locked and returned even if it doesn't need
273  * replaying. (Getting the buffer lock is not really necessary during
274  * single-process crash recovery, but some subroutines such as MarkBufferDirty
275  * will complain if we don't have the lock. In hot standby mode it's
276  * definitely necessary.)
277  *
278  * Note: when a backup block is available in XLOG, we restore it
279  * unconditionally, even if the page in the database appears newer. This is
280  * to protect ourselves against database pages that were partially or
281  * incorrectly written during a crash. We assume that the XLOG data must be
282  * good because it has passed a CRC check, while the database page might not
283  * be. This will force us to replay all subsequent modifications of the page
284  * that appear in XLOG, rather than possibly ignoring them as already
285  * applied, but that's not a huge drawback.
286  */
289  Buffer *buf)
290 {
291  return XLogReadBufferForRedoExtended(record, block_id, RBM_NORMAL,
292  false, buf);
293 }
294 
295 /*
296  * Pin and lock a buffer referenced by a WAL record, for the purpose of
297  * re-initializing it.
298  */
299 Buffer
301 {
302  Buffer buf;
303 
304  XLogReadBufferForRedoExtended(record, block_id, RBM_ZERO_AND_LOCK, false,
305  &buf);
306  return buf;
307 }
308 
309 /*
310  * XLogReadBufferForRedoExtended
311  * Like XLogReadBufferForRedo, but with extra options.
312  *
313  * In RBM_ZERO_* modes, if the page doesn't exist, the relation is extended
314  * with all-zeroes pages up to the referenced block number. In
315  * RBM_ZERO_AND_LOCK and RBM_ZERO_AND_CLEANUP_LOCK modes, the return value
316  * is always BLK_NEEDS_REDO.
317  *
318  * (The RBM_ZERO_AND_CLEANUP_LOCK mode is redundant with the get_cleanup_lock
319  * parameter. Do not use an inconsistent combination!)
320  *
321  * If 'get_cleanup_lock' is true, a "cleanup lock" is acquired on the buffer
322  * using LockBufferForCleanup(), instead of a regular exclusive lock.
323  */
326  uint8 block_id,
327  ReadBufferMode mode, bool get_cleanup_lock,
328  Buffer *buf)
329 {
330  XLogRecPtr lsn = record->EndRecPtr;
331  RelFileNode rnode;
332  ForkNumber forknum;
333  BlockNumber blkno;
334  Page page;
335  bool zeromode;
336  bool willinit;
337 
338  if (!XLogRecGetBlockTag(record, block_id, &rnode, &forknum, &blkno))
339  {
340  /* Caller specified a bogus block_id */
341  elog(PANIC, "failed to locate backup block with ID %d", block_id);
342  }
343 
344  /*
345  * Make sure that if the block is marked with WILL_INIT, the caller is
346  * going to initialize it. And vice versa.
347  */
348  zeromode = (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK);
349  willinit = (record->blocks[block_id].flags & BKPBLOCK_WILL_INIT) != 0;
350  if (willinit && !zeromode)
351  elog(PANIC, "block with WILL_INIT flag in WAL record must be zeroed by redo routine");
352  if (!willinit && zeromode)
353  elog(PANIC, "block to be initialized in redo routine must be marked with WILL_INIT flag in the WAL record");
354 
355  /* If it's a full-page image, restore it. */
356  if (XLogRecHasBlockImage(record, block_id))
357  {
358  *buf = XLogReadBufferExtended(rnode, forknum, blkno,
359  get_cleanup_lock ? RBM_ZERO_AND_CLEANUP_LOCK : RBM_ZERO_AND_LOCK);
360  page = BufferGetPage(*buf);
361  if (!RestoreBlockImage(record, block_id, page))
362  elog(ERROR, "failed to restore block image");
363 
364  /*
365  * The page may be uninitialized. If so, we can't set the LSN because
366  * that would corrupt the page.
367  */
368  if (!PageIsNew(page))
369  {
370  PageSetLSN(page, lsn);
371  }
372 
373  MarkBufferDirty(*buf);
374 
375  /*
376  * At the end of crash recovery the init forks of unlogged relations
377  * are copied, without going through shared buffers. So we need to
378  * force the on-disk state of init forks to always be in sync with the
379  * state in shared buffers.
380  */
381  if (forknum == INIT_FORKNUM)
382  FlushOneBuffer(*buf);
383 
384  return BLK_RESTORED;
385  }
386  else
387  {
388  *buf = XLogReadBufferExtended(rnode, forknum, blkno, mode);
389  if (BufferIsValid(*buf))
390  {
391  if (mode != RBM_ZERO_AND_LOCK && mode != RBM_ZERO_AND_CLEANUP_LOCK)
392  {
393  if (get_cleanup_lock)
394  LockBufferForCleanup(*buf);
395  else
397  }
398  if (lsn <= PageGetLSN(BufferGetPage(*buf)))
399  return BLK_DONE;
400  else
401  return BLK_NEEDS_REDO;
402  }
403  else
404  return BLK_NOTFOUND;
405  }
406 }
407 
408 /*
409  * XLogReadBufferExtended
410  * Read a page during XLOG replay
411  *
412  * This is functionally comparable to ReadBufferExtended. There's some
413  * differences in the behavior wrt. the "mode" argument:
414  *
415  * In RBM_NORMAL mode, if the page doesn't exist, or contains all-zeroes, we
416  * return InvalidBuffer. In this case the caller should silently skip the
417  * update on this page. (In this situation, we expect that the page was later
418  * dropped or truncated. If we don't see evidence of that later in the WAL
419  * sequence, we'll complain at the end of WAL replay.)
420  *
421  * In RBM_ZERO_* modes, if the page doesn't exist, the relation is extended
422  * with all-zeroes pages up to the given block number.
423  *
424  * In RBM_NORMAL_NO_LOG mode, we return InvalidBuffer if the page doesn't
425  * exist, and we don't check for all-zeroes. Thus, no log entry is made
426  * to imply that the page should be dropped or truncated later.
427  *
428  * NB: A redo function should normally not call this directly. To get a page
429  * to modify, use XLogReadBufferForRedoExtended instead. It is important that
430  * all pages modified by a WAL record are registered in the WAL records, or
431  * they will be invisible to tools that that need to know which pages are
432  * modified.
433  */
434 Buffer
436  BlockNumber blkno, ReadBufferMode mode)
437 {
438  BlockNumber lastblock;
439  Buffer buffer;
440  SMgrRelation smgr;
441 
442  Assert(blkno != P_NEW);
443 
444  /* Open the relation at smgr level */
445  smgr = smgropen(rnode, InvalidBackendId);
446 
447  /*
448  * Create the target file if it doesn't already exist. This lets us cope
449  * if the replay sequence contains writes to a relation that is later
450  * deleted. (The original coding of this routine would instead suppress
451  * the writes, but that seems like it risks losing valuable data if the
452  * filesystem loses an inode during a crash. Better to write the data
453  * until we are actually told to delete the file.)
454  */
455  smgrcreate(smgr, forknum, true);
456 
457  lastblock = smgrnblocks(smgr, forknum);
458 
459  if (blkno < lastblock)
460  {
461  /* page exists in file */
462  buffer = ReadBufferWithoutRelcache(rnode, forknum, blkno,
463  mode, NULL);
464  }
465  else
466  {
467  /* hm, page doesn't exist in file */
468  if (mode == RBM_NORMAL)
469  {
470  log_invalid_page(rnode, forknum, blkno, false);
471  return InvalidBuffer;
472  }
473  if (mode == RBM_NORMAL_NO_LOG)
474  return InvalidBuffer;
475  /* OK to extend the file */
476  /* we do this in recovery only - no rel-extension lock needed */
478  buffer = InvalidBuffer;
479  do
480  {
481  if (buffer != InvalidBuffer)
482  {
483  if (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK)
485  ReleaseBuffer(buffer);
486  }
487  buffer = ReadBufferWithoutRelcache(rnode, forknum,
488  P_NEW, mode, NULL);
489  }
490  while (BufferGetBlockNumber(buffer) < blkno);
491  /* Handle the corner case that P_NEW returns non-consecutive pages */
492  if (BufferGetBlockNumber(buffer) != blkno)
493  {
494  if (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK)
496  ReleaseBuffer(buffer);
497  buffer = ReadBufferWithoutRelcache(rnode, forknum, blkno,
498  mode, NULL);
499  }
500  }
501 
502  if (mode == RBM_NORMAL)
503  {
504  /* check that page has been initialized */
505  Page page = (Page) BufferGetPage(buffer);
506 
507  /*
508  * We assume that PageIsNew is safe without a lock. During recovery,
509  * there should be no other backends that could modify the buffer at
510  * the same time.
511  */
512  if (PageIsNew(page))
513  {
514  ReleaseBuffer(buffer);
515  log_invalid_page(rnode, forknum, blkno, true);
516  return InvalidBuffer;
517  }
518  }
519 
520  return buffer;
521 }
522 
523 /*
524  * Struct actually returned by XLogFakeRelcacheEntry, though the declared
525  * return type is Relation.
526  */
527 typedef struct
528 {
529  RelationData reldata; /* Note: this must be first */
532 
534 
535 /*
536  * Create a fake relation cache entry for a physical relation
537  *
538  * It's often convenient to use the same functions in XLOG replay as in the
539  * main codepath, but those functions typically work with a relcache entry.
540  * We don't have a working relation cache during XLOG replay, but this
541  * function can be used to create a fake relcache entry instead. Only the
542  * fields related to physical storage, like rd_rel, are initialized, so the
543  * fake entry is only usable in low-level operations like ReadBuffer().
544  *
545  * Caller must free the returned entry with FreeFakeRelcacheEntry().
546  */
547 Relation
549 {
550  FakeRelCacheEntry fakeentry;
551  Relation rel;
552 
554 
555  /* Allocate the Relation struct and all related space in one block. */
556  fakeentry = palloc0(sizeof(FakeRelCacheEntryData));
557  rel = (Relation) fakeentry;
558 
559  rel->rd_rel = &fakeentry->pgc;
560  rel->rd_node = rnode;
561  /* We will never be working with temp rels during recovery */
563 
564  /* It must be a permanent table if we're in recovery. */
565  rel->rd_rel->relpersistence = RELPERSISTENCE_PERMANENT;
566 
567  /* We don't know the name of the relation; use relfilenode instead */
568  sprintf(RelationGetRelationName(rel), "%u", rnode.relNode);
569 
570  /*
571  * We set up the lockRelId in case anything tries to lock the dummy
572  * relation. Note that this is fairly bogus since relNode may be
573  * different from the relation's OID. It shouldn't really matter though,
574  * since we are presumably running by ourselves and can't have any lock
575  * conflicts ...
576  */
577  rel->rd_lockInfo.lockRelId.dbId = rnode.dbNode;
578  rel->rd_lockInfo.lockRelId.relId = rnode.relNode;
579 
580  rel->rd_smgr = NULL;
581 
582  return rel;
583 }
584 
585 /*
586  * Free a fake relation cache entry.
587  */
588 void
590 {
591  /* make sure the fakerel is not referenced by the SmgrRelation anymore */
592  if (fakerel->rd_smgr != NULL)
593  smgrclearowner(&fakerel->rd_smgr, fakerel->rd_smgr);
594  pfree(fakerel);
595 }
596 
597 /*
598  * Drop a relation during XLOG replay
599  *
600  * This is called when the relation is about to be deleted; we need to remove
601  * any open "invalid-page" records for the relation.
602  */
603 void
605 {
606  forget_invalid_pages(rnode, forknum, 0);
607 }
608 
609 /*
610  * Drop a whole database during XLOG replay
611  *
612  * As above, but for DROP DATABASE instead of dropping a single rel
613  */
614 void
616 {
617  /*
618  * This is unnecessarily heavy-handed, as it will close SMgrRelation
619  * objects for other databases as well. DROP DATABASE occurs seldom enough
620  * that it's not worth introducing a variant of smgrclose for just this
621  * purpose. XXX: Or should we rather leave the smgr entries dangling?
622  */
623  smgrcloseall();
624 
626 }
627 
628 /*
629  * Truncate a relation during XLOG replay
630  *
631  * We need to clean up any open "invalid-page" records for the dropped pages.
632  */
633 void
635  BlockNumber nblocks)
636 {
637  forget_invalid_pages(rnode, forkNum, nblocks);
638 }
639 
640 /*
641  * Read 'count' bytes from WAL into 'buf', starting at location 'startptr'
642  * in timeline 'tli'.
643  *
644  * Will open, and keep open, one WAL segment stored in the static file
645  * descriptor 'sendFile'. This means if XLogRead is used once, there will
646  * always be one descriptor left open until the process ends, but never
647  * more than one.
648  *
649  * XXX This is very similar to pg_xlogdump's XLogDumpXLogRead and to XLogRead
650  * in walsender.c but for small differences (such as lack of elog() in
651  * frontend). Probably these should be merged at some point.
652  */
653 static void
654 XLogRead(char *buf, TimeLineID tli, XLogRecPtr startptr, Size count)
655 {
656  char *p;
657  XLogRecPtr recptr;
658  Size nbytes;
659 
660  /* state maintained across calls */
661  static int sendFile = -1;
662  static XLogSegNo sendSegNo = 0;
663  static uint32 sendOff = 0;
664 
665  p = buf;
666  recptr = startptr;
667  nbytes = count;
668 
669  while (nbytes > 0)
670  {
671  uint32 startoff;
672  int segbytes;
673  int readbytes;
674 
675  startoff = recptr % XLogSegSize;
676 
677  /* Do we need to switch to a different xlog segment? */
678  if (sendFile < 0 || !XLByteInSeg(recptr, sendSegNo))
679  {
680  char path[MAXPGPATH];
681 
682  if (sendFile >= 0)
683  close(sendFile);
684 
685  XLByteToSeg(recptr, sendSegNo);
686 
687  XLogFilePath(path, tli, sendSegNo);
688 
689  sendFile = BasicOpenFile(path, O_RDONLY | PG_BINARY, 0);
690 
691  if (sendFile < 0)
692  {
693  if (errno == ENOENT)
694  ereport(ERROR,
696  errmsg("requested WAL segment %s has already been removed",
697  path)));
698  else
699  ereport(ERROR,
701  errmsg("could not open file \"%s\": %m",
702  path)));
703  }
704  sendOff = 0;
705  }
706 
707  /* Need to seek in the file? */
708  if (sendOff != startoff)
709  {
710  if (lseek(sendFile, (off_t) startoff, SEEK_SET) < 0)
711  {
712  char path[MAXPGPATH];
713 
714  XLogFilePath(path, tli, sendSegNo);
715 
716  ereport(ERROR,
718  errmsg("could not seek in log segment %s to offset %u: %m",
719  path, startoff)));
720  }
721  sendOff = startoff;
722  }
723 
724  /* How many bytes are within this segment? */
725  if (nbytes > (XLogSegSize - startoff))
726  segbytes = XLogSegSize - startoff;
727  else
728  segbytes = nbytes;
729 
730  readbytes = read(sendFile, p, segbytes);
731  if (readbytes <= 0)
732  {
733  char path[MAXPGPATH];
734 
735  XLogFilePath(path, tli, sendSegNo);
736 
737  ereport(ERROR,
739  errmsg("could not read from log segment %s, offset %u, length %lu: %m",
740  path, sendOff, (unsigned long) segbytes)));
741  }
742 
743  /* Update state for read */
744  recptr += readbytes;
745 
746  sendOff += readbytes;
747  nbytes -= readbytes;
748  p += readbytes;
749  }
750 }
751 
752 /*
753  * read_page callback for reading local xlog files
754  *
755  * Public because it would likely be very helpful for someone writing another
756  * output method outside walsender, e.g. in a bgworker.
757  *
758  * TODO: The walsender has its own version of this, but it relies on the
759  * walsender's latch being set whenever WAL is flushed. No such infrastructure
760  * exists for normal backends, so we have to do a check/sleep/repeat style of
761  * loop for now.
762  */
763 int
765  int reqLen, XLogRecPtr targetRecPtr, char *cur_page,
766  TimeLineID *pageTLI)
767 {
768  XLogRecPtr read_upto,
769  loc;
770  int count;
771 
772  loc = targetPagePtr + reqLen;
773  while (1)
774  {
775  /*
776  * TODO: we're going to have to do something more intelligent about
777  * timelines on standbys. Use readTimeLineHistory() and
778  * tliOfPointInHistory() to get the proper LSN? For now we'll catch
779  * that case earlier, but the code and TODO is left in here for when
780  * that changes.
781  */
782  if (!RecoveryInProgress())
783  {
784  *pageTLI = ThisTimeLineID;
785  read_upto = GetFlushRecPtr();
786  }
787  else
788  read_upto = GetXLogReplayRecPtr(pageTLI);
789 
790  if (loc <= read_upto)
791  break;
792 
794  pg_usleep(1000L);
795  }
796 
797  if (targetPagePtr + XLOG_BLCKSZ <= read_upto)
798  {
799  /*
800  * more than one block available; read only that block, have caller
801  * come back if they need more.
802  */
803  count = XLOG_BLCKSZ;
804  }
805  else if (targetPagePtr + reqLen > read_upto)
806  {
807  /* not enough data there */
808  return -1;
809  }
810  else
811  {
812  /* enough bytes available to satisfy the request */
813  count = read_upto - targetPagePtr;
814  }
815 
816  /*
817  * Even though we just determined how much of the page can be validly read
818  * as 'count', read the whole page anyway. It's guaranteed to be
819  * zero-padded up to the page boundary if it's incomplete.
820  */
821  XLogRead(cur_page, *pageTLI, targetPagePtr, XLOG_BLCKSZ);
822 
823  /* number of valid bytes in the buffer */
824  return count;
825 }
#define XLogSegSize
Definition: xlog_internal.h:92
bool XLogHaveInvalidPages(void)
Definition: xlogutils.c:209
void XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum, BlockNumber nblocks)
Definition: xlogutils.c:634
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:99
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:795
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:3586
LockRelId lockRelId
Definition: rel.h:43
#define relpathperm(rnode, forknum)
Definition: relpath.h:67
#define DEBUG1
Definition: elog.h:25
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:376
uint32 TimeLineID
Definition: xlogdefs.h:45
void smgrclearowner(SMgrRelation *owner, SMgrRelation reln)
Definition: smgr.c:222
RelationData reldata
Definition: xlogutils.c:529
struct xl_invalid_page xl_invalid_page
#define HASH_ELEM
Definition: hsearch.h:87
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:206
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1445
static void forget_invalid_pages_db(Oid dbid)
Definition: xlogutils.c:176
struct SMgrRelationData * rd_smgr
Definition: rel.h:57
void XLogCheckInvalidPages(void)
Definition: xlogutils.c:219
bool InRecovery
Definition: xlog.c:187
Oid dbId
Definition: rel.h:38
static int sendFile
Definition: walsender.c:122
Buffer XLogReadBufferExtended(RelFileNode rnode, ForkNumber forknum, BlockNumber blkno, ReadBufferMode mode)
Definition: xlogutils.c:435
unsigned char uint8
Definition: c.h:263
#define InvalidBuffer
Definition: buf.h:25
Size entrysize
Definition: hsearch.h:73
Buffer ReadBufferWithoutRelcache(RelFileNode rnode, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:682
static void XLogRead(char *buf, TimeLineID tli, XLogRecPtr startptr, Size count)
Definition: xlogutils.c:654
struct xl_invalid_page_key xl_invalid_page_key
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1299
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:7894
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3292
ForkNumber forkno
Definition: xlogutils.c:46
#define P_NEW
Definition: bufmgr.h:94
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:887
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:101
Form_pg_class rd_rel
Definition: rel.h:83
unsigned int Oid
Definition: postgres_ext.h:31
bool RecoveryInProgress(void)
Definition: xlog.c:7547
#define PANIC
Definition: elog.h:53
#define PG_BINARY
Definition: c.h:1018
XLogRecPtr EndRecPtr
Definition: xlogreader.h:114
void smgrcloseall(void)
Definition: smgr.c:326
#define RELPERSISTENCE_PERMANENT
Definition: pg_class.h:165
int read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page, TimeLineID *pageTLI)
Definition: xlogutils.c:764
RelFileNode node
Definition: xlogutils.c:45
void pg_usleep(long microsec)
Definition: signal.c:53
Definition: dynahash.c:193
struct RelationData * Relation
Definition: relcache.h:21
void pfree(void *pointer)
Definition: mcxt.c:995
static void report_invalid_page(int elevel, RelFileNode node, ForkNumber forkno, BlockNumber blkno, bool present)
Definition: xlogutils.c:61
#define ERROR
Definition: elog.h:43
Buffer XLogInitBufferForRedo(XLogReaderState *record, uint8 block_id)
Definition: xlogutils.c:300
static void log_invalid_page(RelFileNode node, ForkNumber forkno, BlockNumber blkno, bool present)
Definition: xlogutils.c:77
#define MAXPGPATH
XLogRecPtr GetXLogReplayRecPtr(TimeLineID *replayTLI)
Definition: xlog.c:10616
#define XLogFilePath(path, tli, logSegNo)
#define DEBUG2
Definition: elog.h:24
Relation CreateFakeRelcacheEntry(RelFileNode rnode)
Definition: xlogutils.c:548
static void forget_invalid_pages(RelFileNode node, ForkNumber forkno, BlockNumber minblkno)
Definition: xlogutils.c:141
LockInfoData rd_lockInfo
Definition: rel.h:86
static char * buf
Definition: pg_test_fsync.c:65
uint64 XLogSegNo
Definition: xlogdefs.h:34
BlockNumber blkno
Definition: xlogutils.c:47
xl_invalid_page_key key
Definition: xlogutils.c:52
int errcode_for_file_access(void)
Definition: elog.c:598
#define RelationGetRelationName(relation)
Definition: rel.h:391
unsigned int uint32
Definition: c.h:265
#define BufferGetPage(buffer)
Definition: bufmgr.h:172
#define BKPBLOCK_WILL_INIT
Definition: xlogrecord.h:172
#define ereport(elevel, rest)
Definition: elog.h:122
SMgrRelation smgropen(RelFileNode rnode, BackendId backend)
Definition: smgr.c:137
ForkNumber
Definition: relpath.h:24
#define WARNING
Definition: elog.h:40
ReadBufferMode
Definition: bufmgr.h:39
void FreeFakeRelcacheEntry(Relation fakerel)
Definition: xlogutils.c:589
static int elevel
Definition: vacuumlazy.c:130
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1261
#define HASH_BLOBS
Definition: hsearch.h:88
#define InvalidBackendId
Definition: backendid.h:23
void * palloc0(Size size)
Definition: mcxt.c:923
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3529
Size keysize
Definition: hsearch.h:72
int log_min_messages
Definition: guc.c:429
TimeLineID ThisTimeLineID
Definition: xlog.c:174
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:288
RelFileNode rd_node
Definition: rel.h:55
bool reachedConsistency
Definition: xlog.c:775
#define XLByteToSeg(xlrp, logSegNo)
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:672
#define NULL
Definition: c.h:226
FakeRelCacheEntryData * FakeRelCacheEntry
Definition: xlogutils.c:533
uint64 XLogRecPtr
Definition: xlogdefs.h:21
BackendId rd_backend
Definition: rel.h:59
#define Assert(condition)
Definition: c.h:667
Definition: regguts.h:313
XLogRedoAction
Definition: xlogutils.h:27
size_t Size
Definition: c.h:352
#define BufferIsValid(bufnum)
Definition: bufmgr.h:126
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1355
static XLogSegNo sendSegNo
Definition: walsender.c:123
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1345
void FlushOneBuffer(Buffer buffer)
Definition: bufmgr.c:3272
bool RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
Definition: xlogreader.c:1314
#define PageGetLSN(page)
Definition: bufpage.h:363
FormData_pg_class
Definition: pg_class.h:82
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2588
void XLogDropRelation(RelFileNode rnode, ForkNumber forknum)
Definition: xlogutils.c:604
#define PageIsNew(page)
Definition: bufpage.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
XLogRedoAction XLogReadBufferForRedoExtended(XLogReaderState *record, uint8 block_id, ReadBufferMode mode, bool get_cleanup_lock, Buffer *buf)
Definition: xlogutils.c:325
void XLogDropDatabase(Oid dbid)
Definition: xlogutils.c:615
static HTAB * invalid_page_tab
Definition: xlogutils.c:56
FormData_pg_class pgc
Definition: xlogutils.c:530
int client_min_messages
Definition: guc.c:430
static uint32 sendOff
Definition: walsender.c:124
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:218
#define close(a)
Definition: win32.h:17
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
#define XLByteInSeg(xlrp, logSegNo)
#define PageSetLSN(page, lsn)
Definition: bufpage.h:365
int Buffer
Definition: buf.h:23
#define read(a, b, c)
Definition: win32.h:18
Pointer Page
Definition: bufpage.h:74
#define RelFileNodeEquals(node1, node2)
Definition: relfilenode.h:88
DecodedBkpBlock blocks[XLR_MAX_BLOCK_ID+1]
Definition: xlogreader.h:133
Oid relId
Definition: rel.h:37
int BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:892