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-2017, 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 with the BKPIMAGE_APPLY flag
279  * set, we restore it, even if the page in the database appears newer. This
280  * is 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 has a full-page image and it should be restored, do it. */
356  if (XLogRecBlockImageApply(record, block_id))
357  {
358  Assert(XLogRecHasBlockImage(record, block_id));
359  *buf = XLogReadBufferExtended(rnode, forknum, blkno,
360  get_cleanup_lock ? RBM_ZERO_AND_CLEANUP_LOCK : RBM_ZERO_AND_LOCK);
361  page = BufferGetPage(*buf);
362  if (!RestoreBlockImage(record, block_id, page))
363  elog(ERROR, "failed to restore block image");
364 
365  /*
366  * The page may be uninitialized. If so, we can't set the LSN because
367  * that would corrupt the page.
368  */
369  if (!PageIsNew(page))
370  {
371  PageSetLSN(page, lsn);
372  }
373 
374  MarkBufferDirty(*buf);
375 
376  /*
377  * At the end of crash recovery the init forks of unlogged relations
378  * are copied, without going through shared buffers. So we need to
379  * force the on-disk state of init forks to always be in sync with the
380  * state in shared buffers.
381  */
382  if (forknum == INIT_FORKNUM)
383  FlushOneBuffer(*buf);
384 
385  return BLK_RESTORED;
386  }
387  else
388  {
389  *buf = XLogReadBufferExtended(rnode, forknum, blkno, mode);
390  if (BufferIsValid(*buf))
391  {
392  if (mode != RBM_ZERO_AND_LOCK && mode != RBM_ZERO_AND_CLEANUP_LOCK)
393  {
394  if (get_cleanup_lock)
395  LockBufferForCleanup(*buf);
396  else
398  }
399  if (lsn <= PageGetLSN(BufferGetPage(*buf)))
400  return BLK_DONE;
401  else
402  return BLK_NEEDS_REDO;
403  }
404  else
405  return BLK_NOTFOUND;
406  }
407 }
408 
409 /*
410  * XLogReadBufferExtended
411  * Read a page during XLOG replay
412  *
413  * This is functionally comparable to ReadBufferExtended. There's some
414  * differences in the behavior wrt. the "mode" argument:
415  *
416  * In RBM_NORMAL mode, if the page doesn't exist, or contains all-zeroes, we
417  * return InvalidBuffer. In this case the caller should silently skip the
418  * update on this page. (In this situation, we expect that the page was later
419  * dropped or truncated. If we don't see evidence of that later in the WAL
420  * sequence, we'll complain at the end of WAL replay.)
421  *
422  * In RBM_ZERO_* modes, if the page doesn't exist, the relation is extended
423  * with all-zeroes pages up to the given block number.
424  *
425  * In RBM_NORMAL_NO_LOG mode, we return InvalidBuffer if the page doesn't
426  * exist, and we don't check for all-zeroes. Thus, no log entry is made
427  * to imply that the page should be dropped or truncated later.
428  *
429  * NB: A redo function should normally not call this directly. To get a page
430  * to modify, use XLogReadBufferForRedoExtended instead. It is important that
431  * all pages modified by a WAL record are registered in the WAL records, or
432  * they will be invisible to tools that that need to know which pages are
433  * modified.
434  */
435 Buffer
437  BlockNumber blkno, ReadBufferMode mode)
438 {
439  BlockNumber lastblock;
440  Buffer buffer;
441  SMgrRelation smgr;
442 
443  Assert(blkno != P_NEW);
444 
445  /* Open the relation at smgr level */
446  smgr = smgropen(rnode, InvalidBackendId);
447 
448  /*
449  * Create the target file if it doesn't already exist. This lets us cope
450  * if the replay sequence contains writes to a relation that is later
451  * deleted. (The original coding of this routine would instead suppress
452  * the writes, but that seems like it risks losing valuable data if the
453  * filesystem loses an inode during a crash. Better to write the data
454  * until we are actually told to delete the file.)
455  */
456  smgrcreate(smgr, forknum, true);
457 
458  lastblock = smgrnblocks(smgr, forknum);
459 
460  if (blkno < lastblock)
461  {
462  /* page exists in file */
463  buffer = ReadBufferWithoutRelcache(rnode, forknum, blkno,
464  mode, NULL);
465  }
466  else
467  {
468  /* hm, page doesn't exist in file */
469  if (mode == RBM_NORMAL)
470  {
471  log_invalid_page(rnode, forknum, blkno, false);
472  return InvalidBuffer;
473  }
474  if (mode == RBM_NORMAL_NO_LOG)
475  return InvalidBuffer;
476  /* OK to extend the file */
477  /* we do this in recovery only - no rel-extension lock needed */
479  buffer = InvalidBuffer;
480  do
481  {
482  if (buffer != InvalidBuffer)
483  {
484  if (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK)
486  ReleaseBuffer(buffer);
487  }
488  buffer = ReadBufferWithoutRelcache(rnode, forknum,
489  P_NEW, mode, NULL);
490  }
491  while (BufferGetBlockNumber(buffer) < blkno);
492  /* Handle the corner case that P_NEW returns non-consecutive pages */
493  if (BufferGetBlockNumber(buffer) != blkno)
494  {
495  if (mode == RBM_ZERO_AND_LOCK || mode == RBM_ZERO_AND_CLEANUP_LOCK)
497  ReleaseBuffer(buffer);
498  buffer = ReadBufferWithoutRelcache(rnode, forknum, blkno,
499  mode, NULL);
500  }
501  }
502 
503  if (mode == RBM_NORMAL)
504  {
505  /* check that page has been initialized */
506  Page page = (Page) BufferGetPage(buffer);
507 
508  /*
509  * We assume that PageIsNew is safe without a lock. During recovery,
510  * there should be no other backends that could modify the buffer at
511  * the same time.
512  */
513  if (PageIsNew(page))
514  {
515  ReleaseBuffer(buffer);
516  log_invalid_page(rnode, forknum, blkno, true);
517  return InvalidBuffer;
518  }
519  }
520 
521  return buffer;
522 }
523 
524 /*
525  * Struct actually returned by XLogFakeRelcacheEntry, though the declared
526  * return type is Relation.
527  */
528 typedef struct
529 {
530  RelationData reldata; /* Note: this must be first */
533 
535 
536 /*
537  * Create a fake relation cache entry for a physical relation
538  *
539  * It's often convenient to use the same functions in XLOG replay as in the
540  * main codepath, but those functions typically work with a relcache entry.
541  * We don't have a working relation cache during XLOG replay, but this
542  * function can be used to create a fake relcache entry instead. Only the
543  * fields related to physical storage, like rd_rel, are initialized, so the
544  * fake entry is only usable in low-level operations like ReadBuffer().
545  *
546  * Caller must free the returned entry with FreeFakeRelcacheEntry().
547  */
548 Relation
550 {
551  FakeRelCacheEntry fakeentry;
552  Relation rel;
553 
555 
556  /* Allocate the Relation struct and all related space in one block. */
557  fakeentry = palloc0(sizeof(FakeRelCacheEntryData));
558  rel = (Relation) fakeentry;
559 
560  rel->rd_rel = &fakeentry->pgc;
561  rel->rd_node = rnode;
562  /* We will never be working with temp rels during recovery */
564 
565  /* It must be a permanent table if we're in recovery. */
566  rel->rd_rel->relpersistence = RELPERSISTENCE_PERMANENT;
567 
568  /* We don't know the name of the relation; use relfilenode instead */
569  sprintf(RelationGetRelationName(rel), "%u", rnode.relNode);
570 
571  /*
572  * We set up the lockRelId in case anything tries to lock the dummy
573  * relation. Note that this is fairly bogus since relNode may be
574  * different from the relation's OID. It shouldn't really matter though,
575  * since we are presumably running by ourselves and can't have any lock
576  * conflicts ...
577  */
578  rel->rd_lockInfo.lockRelId.dbId = rnode.dbNode;
579  rel->rd_lockInfo.lockRelId.relId = rnode.relNode;
580 
581  rel->rd_smgr = NULL;
582 
583  return rel;
584 }
585 
586 /*
587  * Free a fake relation cache entry.
588  */
589 void
591 {
592  /* make sure the fakerel is not referenced by the SmgrRelation anymore */
593  if (fakerel->rd_smgr != NULL)
594  smgrclearowner(&fakerel->rd_smgr, fakerel->rd_smgr);
595  pfree(fakerel);
596 }
597 
598 /*
599  * Drop a relation during XLOG replay
600  *
601  * This is called when the relation is about to be deleted; we need to remove
602  * any open "invalid-page" records for the relation.
603  */
604 void
606 {
607  forget_invalid_pages(rnode, forknum, 0);
608 }
609 
610 /*
611  * Drop a whole database during XLOG replay
612  *
613  * As above, but for DROP DATABASE instead of dropping a single rel
614  */
615 void
617 {
618  /*
619  * This is unnecessarily heavy-handed, as it will close SMgrRelation
620  * objects for other databases as well. DROP DATABASE occurs seldom enough
621  * that it's not worth introducing a variant of smgrclose for just this
622  * purpose. XXX: Or should we rather leave the smgr entries dangling?
623  */
624  smgrcloseall();
625 
627 }
628 
629 /*
630  * Truncate a relation during XLOG replay
631  *
632  * We need to clean up any open "invalid-page" records for the dropped pages.
633  */
634 void
636  BlockNumber nblocks)
637 {
638  forget_invalid_pages(rnode, forkNum, nblocks);
639 }
640 
641 /*
642  * Read 'count' bytes from WAL into 'buf', starting at location 'startptr'
643  * in timeline 'tli'.
644  *
645  * Will open, and keep open, one WAL segment stored in the static file
646  * descriptor 'sendFile'. This means if XLogRead is used once, there will
647  * always be one descriptor left open until the process ends, but never
648  * more than one.
649  *
650  * XXX This is very similar to pg_waldump's XLogDumpXLogRead and to XLogRead
651  * in walsender.c but for small differences (such as lack of elog() in
652  * frontend). Probably these should be merged at some point.
653  */
654 static void
655 XLogRead(char *buf, TimeLineID tli, XLogRecPtr startptr, Size count)
656 {
657  char *p;
658  XLogRecPtr recptr;
659  Size nbytes;
660 
661  /* state maintained across calls */
662  static int sendFile = -1;
663  static XLogSegNo sendSegNo = 0;
664  static uint32 sendOff = 0;
665 
666  p = buf;
667  recptr = startptr;
668  nbytes = count;
669 
670  while (nbytes > 0)
671  {
672  uint32 startoff;
673  int segbytes;
674  int readbytes;
675 
676  startoff = recptr % XLogSegSize;
677 
678  /* Do we need to switch to a different xlog segment? */
679  if (sendFile < 0 || !XLByteInSeg(recptr, sendSegNo))
680  {
681  char path[MAXPGPATH];
682 
683  if (sendFile >= 0)
684  close(sendFile);
685 
686  XLByteToSeg(recptr, sendSegNo);
687 
688  XLogFilePath(path, tli, sendSegNo);
689 
690  sendFile = BasicOpenFile(path, O_RDONLY | PG_BINARY, 0);
691 
692  if (sendFile < 0)
693  {
694  if (errno == ENOENT)
695  ereport(ERROR,
697  errmsg("requested WAL segment %s has already been removed",
698  path)));
699  else
700  ereport(ERROR,
702  errmsg("could not open file \"%s\": %m",
703  path)));
704  }
705  sendOff = 0;
706  }
707 
708  /* Need to seek in the file? */
709  if (sendOff != startoff)
710  {
711  if (lseek(sendFile, (off_t) startoff, SEEK_SET) < 0)
712  {
713  char path[MAXPGPATH];
714 
715  XLogFilePath(path, tli, sendSegNo);
716 
717  ereport(ERROR,
719  errmsg("could not seek in log segment %s to offset %u: %m",
720  path, startoff)));
721  }
722  sendOff = startoff;
723  }
724 
725  /* How many bytes are within this segment? */
726  if (nbytes > (XLogSegSize - startoff))
727  segbytes = XLogSegSize - startoff;
728  else
729  segbytes = nbytes;
730 
731  readbytes = read(sendFile, p, segbytes);
732  if (readbytes <= 0)
733  {
734  char path[MAXPGPATH];
735 
736  XLogFilePath(path, tli, sendSegNo);
737 
738  ereport(ERROR,
740  errmsg("could not read from log segment %s, offset %u, length %lu: %m",
741  path, sendOff, (unsigned long) segbytes)));
742  }
743 
744  /* Update state for read */
745  recptr += readbytes;
746 
747  sendOff += readbytes;
748  nbytes -= readbytes;
749  p += readbytes;
750  }
751 }
752 
753 /*
754  * read_page callback for reading local xlog files
755  *
756  * Public because it would likely be very helpful for someone writing another
757  * output method outside walsender, e.g. in a bgworker.
758  *
759  * TODO: The walsender has its own version of this, but it relies on the
760  * walsender's latch being set whenever WAL is flushed. No such infrastructure
761  * exists for normal backends, so we have to do a check/sleep/repeat style of
762  * loop for now.
763  */
764 int
766  int reqLen, XLogRecPtr targetRecPtr, char *cur_page,
767  TimeLineID *pageTLI)
768 {
769  XLogRecPtr read_upto,
770  loc;
771  int count;
772 
773  loc = targetPagePtr + reqLen;
774  while (1)
775  {
776  /*
777  * TODO: we're going to have to do something more intelligent about
778  * timelines on standbys. Use readTimeLineHistory() and
779  * tliOfPointInHistory() to get the proper LSN? For now we'll catch
780  * that case earlier, but the code and TODO is left in here for when
781  * that changes.
782  */
783  if (!RecoveryInProgress())
784  {
785  *pageTLI = ThisTimeLineID;
786  read_upto = GetFlushRecPtr();
787  }
788  else
789  read_upto = GetXLogReplayRecPtr(pageTLI);
790 
791  if (loc <= read_upto)
792  break;
793 
795  pg_usleep(1000L);
796  }
797 
798  if (targetPagePtr + XLOG_BLCKSZ <= read_upto)
799  {
800  /*
801  * more than one block available; read only that block, have caller
802  * come back if they need more.
803  */
804  count = XLOG_BLCKSZ;
805  }
806  else if (targetPagePtr + reqLen > read_upto)
807  {
808  /* not enough data there */
809  return -1;
810  }
811  else
812  {
813  /* enough bytes available to satisfy the request */
814  count = read_upto - targetPagePtr;
815  }
816 
817  /*
818  * Even though we just determined how much of the page can be validly read
819  * as 'count', read the whole page anyway. It's guaranteed to be
820  * zero-padded up to the page boundary if it's incomplete.
821  */
822  XLogRead(cur_page, *pageTLI, targetPagePtr, XLOG_BLCKSZ);
823 
824  /* number of valid bytes in the buffer */
825  return count;
826 }
#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:635
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:793
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:3586
LockRelId lockRelId
Definition: rel.h:44
#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:530
struct xl_invalid_page xl_invalid_page
#define HASH_ELEM
Definition: hsearch.h:87
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:207
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:87
void XLogCheckInvalidPages(void)
Definition: xlogutils.c:219
bool InRecovery
Definition: xlog.c:191
Oid dbId
Definition: rel.h:39
static int sendFile
Definition: walsender.c:126
Buffer XLogReadBufferExtended(RelFileNode rnode, ForkNumber forknum, BlockNumber blkno, ReadBufferMode mode)
Definition: xlogutils.c:436
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:655
struct xl_invalid_page_key xl_invalid_page_key
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1297
XLogRecPtr GetFlushRecPtr(void)
Definition: xlog.c:8154
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:82
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:89
Form_pg_class rd_rel
Definition: rel.h:113
unsigned int Oid
Definition: postgres_ext.h:31
bool RecoveryInProgress(void)
Definition: xlog.c:7805
#define PANIC
Definition: elog.h:53
#define PG_BINARY
Definition: c.h:1038
XLogRecPtr EndRecPtr
Definition: xlogreader.h:115
void smgrcloseall(void)
Definition: smgr.c:326
#define RELPERSISTENCE_PERMANENT
Definition: pg_class.h:170
int read_local_xlog_page(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page, TimeLineID *pageTLI)
Definition: xlogutils.c:765
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:992
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:10984
#define XLogFilePath(path, tli, logSegNo)
#define DEBUG2
Definition: elog.h:24
Relation CreateFakeRelcacheEntry(RelFileNode rnode)
Definition: xlogutils.c:549
static void forget_invalid_pages(RelFileNode node, ForkNumber forkno, BlockNumber minblkno)
Definition: xlogutils.c:141
LockInfoData rd_lockInfo
Definition: rel.h:116
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:433
unsigned int uint32
Definition: c.h:265
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define BKPBLOCK_WILL_INIT
Definition: xlogrecord.h:182
#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:38
void FreeFakeRelcacheEntry(Relation fakerel)
Definition: xlogutils.c:590
static int elevel
Definition: vacuumlazy.c:136
bool XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: xlogreader.c:1307
#define HASH_BLOBS
Definition: hsearch.h:88
#define InvalidBackendId
Definition: backendid.h:23
void * palloc0(Size size)
Definition: mcxt.c:920
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:453
TimeLineID ThisTimeLineID
Definition: xlog.c:178
XLogRedoAction XLogReadBufferForRedo(XLogReaderState *record, uint8 block_id, Buffer *buf)
Definition: xlogutils.c:288
RelFileNode rd_node
Definition: rel.h:85
bool reachedConsistency
Definition: xlog.c:821
#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:534
uint64 XLogRecPtr
Definition: xlogdefs.h:21
BackendId rd_backend
Definition: rel.h:89
#define Assert(condition)
Definition: c.h:671
Definition: regguts.h:298
XLogRedoAction
Definition: xlogutils.h:27
size_t Size
Definition: c.h:353
#define BufferIsValid(bufnum)
Definition: bufmgr.h:114
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1353
static XLogSegNo sendSegNo
Definition: walsender.c:127
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1343
void FlushOneBuffer(Buffer buffer)
Definition: bufmgr.c:3272
bool RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
Definition: xlogreader.c:1360
#define PageGetLSN(page)
Definition: bufpage.h:363
FormData_pg_class
Definition: pg_class.h:84
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2588
void XLogDropRelation(RelFileNode rnode, ForkNumber forknum)
Definition: xlogutils.c:605
#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:616
static HTAB * invalid_page_tab
Definition: xlogutils.c:56
FormData_pg_class pgc
Definition: xlogutils.c:531
int client_min_messages
Definition: guc.c:454
static uint32 sendOff
Definition: walsender.c:128
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:219
#define XLogRecBlockImageApply(decoder, block_id)
Definition: xlogreader.h:209
#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:134
Oid relId
Definition: rel.h:38
int BasicOpenFile(FileName fileName, int fileFlags, int fileMode)
Definition: fd.c:899