PostgreSQL Source Code  git master
xlogutils.h File Reference
#include "access/xlogreader.h"
#include "storage/bufmgr.h"
Include dependency graph for xlogutils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ReadLocalXLogPageNoWaitPrivate
 

Macros

#define InHotStandby   (standbyState >= STANDBY_SNAPSHOT_PENDING)
 

Typedefs

typedef struct ReadLocalXLogPageNoWaitPrivate ReadLocalXLogPageNoWaitPrivate
 

Enumerations

enum  HotStandbyState { STANDBY_DISABLED , STANDBY_INITIALIZED , STANDBY_SNAPSHOT_PENDING , STANDBY_SNAPSHOT_READY }
 
enum  XLogRedoAction { BLK_NEEDS_REDO , BLK_DONE , BLK_RESTORED , BLK_NOTFOUND }
 

Functions

bool XLogHaveInvalidPages (void)
 
void XLogCheckInvalidPages (void)
 
void XLogDropRelation (RelFileNode rnode, ForkNumber forknum)
 
void XLogDropDatabase (Oid dbid)
 
void XLogTruncateRelation (RelFileNode rnode, ForkNumber forkNum, BlockNumber nblocks)
 
XLogRedoAction XLogReadBufferForRedo (XLogReaderState *record, uint8 buffer_id, Buffer *buf)
 
Buffer XLogInitBufferForRedo (XLogReaderState *record, uint8 block_id)
 
XLogRedoAction XLogReadBufferForRedoExtended (XLogReaderState *record, uint8 buffer_id, ReadBufferMode mode, bool get_cleanup_lock, Buffer *buf)
 
Buffer XLogReadBufferExtended (RelFileNode rnode, ForkNumber forknum, BlockNumber blkno, ReadBufferMode mode, Buffer recent_buffer)
 
Relation CreateFakeRelcacheEntry (RelFileNode rnode)
 
void FreeFakeRelcacheEntry (Relation fakerel)
 
int read_local_xlog_page (XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
 
int read_local_xlog_page_no_wait (XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page)
 
void wal_segment_open (XLogReaderState *state, XLogSegNo nextSegNo, TimeLineID *tli_p)
 
void wal_segment_close (XLogReaderState *state)
 
void XLogReadDetermineTimeline (XLogReaderState *state, XLogRecPtr wantPage, uint32 wantLength, TimeLineID currTLI)
 
void WALReadRaiseError (WALReadError *errinfo)
 

Variables

PGDLLIMPORT bool InRecovery
 
PGDLLIMPORT HotStandbyState standbyState
 

Macro Definition Documentation

◆ InHotStandby

#define InHotStandby   (standbyState >= STANDBY_SNAPSHOT_PENDING)

Definition at line 57 of file xlogutils.h.

Typedef Documentation

◆ ReadLocalXLogPageNoWaitPrivate

Enumeration Type Documentation

◆ HotStandbyState

Enumerator
STANDBY_DISABLED 
STANDBY_INITIALIZED 
STANDBY_SNAPSHOT_PENDING 
STANDBY_SNAPSHOT_READY 

Definition at line 47 of file xlogutils.h.

48 {
HotStandbyState
Definition: xlogutils.h:48
@ STANDBY_DISABLED
Definition: xlogutils.h:49
@ STANDBY_SNAPSHOT_READY
Definition: xlogutils.h:52
@ STANDBY_SNAPSHOT_PENDING
Definition: xlogutils.h:51
@ STANDBY_INITIALIZED
Definition: xlogutils.h:50

◆ XLogRedoAction

Enumerator
BLK_NEEDS_REDO 
BLK_DONE 
BLK_RESTORED 
BLK_NOTFOUND 

Definition at line 69 of file xlogutils.h.

70 {
71  BLK_NEEDS_REDO, /* changes from WAL record need to be applied */
72  BLK_DONE, /* block is already up-to-date */
73  BLK_RESTORED, /* block was restored from a full-page image */
74  BLK_NOTFOUND /* block was not found (and hence does not
75  * need to be replayed) */
XLogRedoAction
Definition: xlogutils.h:70
@ BLK_RESTORED
Definition: xlogutils.h:73
@ BLK_NEEDS_REDO
Definition: xlogutils.h:71
@ BLK_DONE
Definition: xlogutils.h:72
@ BLK_NOTFOUND
Definition: xlogutils.h:74

Function Documentation

◆ CreateFakeRelcacheEntry()

Relation CreateFakeRelcacheEntry ( RelFileNode  rnode)

Definition at line 597 of file xlogutils.c.

598 {
599  FakeRelCacheEntry fakeentry;
600  Relation rel;
601 
602  /* Allocate the Relation struct and all related space in one block. */
603  fakeentry = palloc0(sizeof(FakeRelCacheEntryData));
604  rel = (Relation) fakeentry;
605 
606  rel->rd_rel = &fakeentry->pgc;
607  rel->rd_node = rnode;
608 
609  /*
610  * We will never be working with temp rels during recovery or while
611  * syncing WAL-skipped files.
612  */
614 
615  /* It must be a permanent table here */
616  rel->rd_rel->relpersistence = RELPERSISTENCE_PERMANENT;
617 
618  /* We don't know the name of the relation; use relfilenode instead */
619  sprintf(RelationGetRelationName(rel), "%u", rnode.relNode);
620 
621  /*
622  * We set up the lockRelId in case anything tries to lock the dummy
623  * relation. Note that this is fairly bogus since relNode may be
624  * different from the relation's OID. It shouldn't really matter though.
625  * In recovery, we are running by ourselves and can't have any lock
626  * conflicts. While syncing, we already hold AccessExclusiveLock.
627  */
628  rel->rd_lockInfo.lockRelId.dbId = rnode.dbNode;
629  rel->rd_lockInfo.lockRelId.relId = rnode.relNode;
630 
631  rel->rd_smgr = NULL;
632 
633  return rel;
634 }
#define InvalidBackendId
Definition: backendid.h:23
void * palloc0(Size size)
Definition: mcxt.c:1099
#define sprintf
Definition: port.h:227
#define RelationGetRelationName(relation)
Definition: rel.h:522
struct RelationData * Relation
Definition: relcache.h:26
FormData_pg_class pgc
Definition: xlogutils.c:577
LockRelId lockRelId
Definition: rel.h:45
Oid relId
Definition: rel.h:39
Oid dbId
Definition: rel.h:40
LockInfoData rd_lockInfo
Definition: rel.h:112
RelFileNode rd_node
Definition: rel.h:56
BackendId rd_backend
Definition: rel.h:59
SMgrRelation rd_smgr
Definition: rel.h:57
Form_pg_class rd_rel
Definition: rel.h:109

References LockRelId::dbId, RelFileNode::dbNode, InvalidBackendId, LockInfoData::lockRelId, palloc0(), FakeRelCacheEntryData::pgc, RelationData::rd_backend, RelationData::rd_lockInfo, RelationData::rd_node, RelationData::rd_rel, RelationData::rd_smgr, RelationGetRelationName, LockRelId::relId, RelFileNode::relNode, and sprintf.

Referenced by CreateAndCopyRelationData(), heap_xlog_delete(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_update(), heap_xlog_visible(), ScanSourceDatabasePgClass(), smgr_redo(), and smgrDoPendingSyncs().

◆ FreeFakeRelcacheEntry()

void FreeFakeRelcacheEntry ( Relation  fakerel)

Definition at line 640 of file xlogutils.c.

641 {
642  /* make sure the fakerel is not referenced by the SmgrRelation anymore */
643  if (fakerel->rd_smgr != NULL)
644  smgrclearowner(&fakerel->rd_smgr, fakerel->rd_smgr);
645  pfree(fakerel);
646 }
void pfree(void *pointer)
Definition: mcxt.c:1175
void smgrclearowner(SMgrRelation *owner, SMgrRelation reln)
Definition: smgr.c:227

References pfree(), RelationData::rd_smgr, and smgrclearowner().

Referenced by CreateAndCopyRelationData(), heap_xlog_delete(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_update(), heap_xlog_visible(), ScanSourceDatabasePgClass(), smgr_redo(), and smgrDoPendingSyncs().

◆ read_local_xlog_page()

int read_local_xlog_page ( XLogReaderState state,
XLogRecPtr  targetPagePtr,
int  reqLen,
XLogRecPtr  targetRecPtr,
char *  cur_page 
)

Definition at line 875 of file xlogutils.c.

877 {
878  return read_local_xlog_page_guts(state, targetPagePtr, reqLen,
879  targetRecPtr, cur_page, true);
880 }
Definition: regguts.h:318
static int read_local_xlog_page_guts(XLogReaderState *state, XLogRecPtr targetPagePtr, int reqLen, XLogRecPtr targetRecPtr, char *cur_page, bool wait_for_wal)
Definition: xlogutils.c:899

References read_local_xlog_page_guts().

Referenced by create_logical_replication_slot(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and XlogReadTwoPhaseData().

◆ read_local_xlog_page_no_wait()

int read_local_xlog_page_no_wait ( XLogReaderState state,
XLogRecPtr  targetPagePtr,
int  reqLen,
XLogRecPtr  targetRecPtr,
char *  cur_page 
)

Definition at line 887 of file xlogutils.c.

890 {
891  return read_local_xlog_page_guts(state, targetPagePtr, reqLen,
892  targetRecPtr, cur_page, false);
893 }

References read_local_xlog_page_guts().

Referenced by InitXLogReaderState().

◆ wal_segment_close()

void wal_segment_close ( XLogReaderState state)

◆ wal_segment_open()

void wal_segment_open ( XLogReaderState state,
XLogSegNo  nextSegNo,
TimeLineID tli_p 
)

Definition at line 831 of file xlogutils.c.

833 {
834  TimeLineID tli = *tli_p;
835  char path[MAXPGPATH];
836 
837  XLogFilePath(path, tli, nextSegNo, state->segcxt.ws_segsize);
838  state->seg.ws_file = BasicOpenFile(path, O_RDONLY | PG_BINARY);
839  if (state->seg.ws_file >= 0)
840  return;
841 
842  if (errno == ENOENT)
843  ereport(ERROR,
845  errmsg("requested WAL segment %s has already been removed",
846  path)));
847  else
848  ereport(ERROR,
850  errmsg("could not open file \"%s\": %m",
851  path)));
852 }
#define PG_BINARY
Definition: c.h:1268
int errcode_for_file_access(void)
Definition: elog.c:716
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define ereport(elevel,...)
Definition: elog.h:143
int BasicOpenFile(const char *fileName, int fileFlags)
Definition: fd.c:1071
#define MAXPGPATH
#define XLogFilePath(path, tli, logSegNo, wal_segsz_bytes)
uint32 TimeLineID
Definition: xlogdefs.h:59

References BasicOpenFile(), ereport, errcode_for_file_access(), errmsg(), ERROR, MAXPGPATH, PG_BINARY, and XLogFilePath.

Referenced by create_logical_replication_slot(), InitXLogReaderState(), pg_logical_replication_slot_advance(), pg_logical_slot_get_changes_guts(), and XlogReadTwoPhaseData().

◆ WALReadRaiseError()

void WALReadRaiseError ( WALReadError errinfo)

Definition at line 1039 of file xlogutils.c.

1040 {
1041  WALOpenSegment *seg = &errinfo->wre_seg;
1042  char fname[MAXFNAMELEN];
1043 
1044  XLogFileName(fname, seg->ws_tli, seg->ws_segno, wal_segment_size);
1045 
1046  if (errinfo->wre_read < 0)
1047  {
1048  errno = errinfo->wre_errno;
1049  ereport(ERROR,
1051  errmsg("could not read from log segment %s, offset %d: %m",
1052  fname, errinfo->wre_off)));
1053  }
1054  else if (errinfo->wre_read == 0)
1055  {
1056  ereport(ERROR,
1058  errmsg("could not read from log segment %s, offset %d: read %d of %d",
1059  fname, errinfo->wre_off, errinfo->wre_read,
1060  errinfo->wre_req)));
1061  }
1062 }
int errcode(int sqlerrcode)
Definition: elog.c:693
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:43
XLogSegNo ws_segno
Definition: xlogreader.h:48
TimeLineID ws_tli
Definition: xlogreader.h:49
WALOpenSegment wre_seg
Definition: xlogreader.h:386
int wal_segment_size
Definition: xlog.c:144
#define XLogFileName(fname, tli, logSegNo, wal_segsz_bytes)
#define MAXFNAMELEN

References ereport, errcode(), ERRCODE_DATA_CORRUPTED, errcode_for_file_access(), errmsg(), ERROR, MAXFNAMELEN, wal_segment_size, WALReadError::wre_errno, WALReadError::wre_off, WALReadError::wre_read, WALReadError::wre_req, WALReadError::wre_seg, WALOpenSegment::ws_segno, WALOpenSegment::ws_tli, and XLogFileName.

Referenced by logical_read_xlog_page(), read_local_xlog_page_guts(), and XLogSendPhysical().

◆ XLogCheckInvalidPages()

void XLogCheckInvalidPages ( void  )

Definition at line 247 of file xlogutils.c.

248 {
250  xl_invalid_page *hentry;
251  bool foundone = false;
252 
253  if (invalid_page_tab == NULL)
254  return; /* nothing to do */
255 
257 
258  /*
259  * Our strategy is to emit WARNING messages for all remaining entries and
260  * only PANIC after we've dumped all the available info.
261  */
262  while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
263  {
264  report_invalid_page(WARNING, hentry->key.node, hentry->key.forkno,
265  hentry->key.blkno, hentry->present);
266  foundone = true;
267  }
268 
269  if (foundone)
271  "WAL contains references to invalid pages");
272 
274  invalid_page_tab = NULL;
275 }
void hash_destroy(HTAB *hashp)
Definition: dynahash.c:862
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1436
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1426
#define WARNING
Definition: elog.h:30
#define PANIC
Definition: elog.h:36
#define elog(elevel,...)
Definition: elog.h:218
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:229
RelFileNode node
Definition: xlogutils.c:70
BlockNumber blkno
Definition: xlogutils.c:72
ForkNumber forkno
Definition: xlogutils.c:71
xl_invalid_page_key key
Definition: xlogutils.c:77
bool ignore_invalid_pages
Definition: xlogutils.c:37
static void report_invalid_page(int elevel, RelFileNode node, ForkNumber forkno, BlockNumber blkno, bool present)
Definition: xlogutils.c:89
static HTAB * invalid_page_tab
Definition: xlogutils.c:81

References xl_invalid_page_key::blkno, elog, xl_invalid_page_key::forkno, hash_destroy(), hash_seq_init(), hash_seq_search(), ignore_invalid_pages, invalid_page_tab, xl_invalid_page::key, xl_invalid_page_key::node, PANIC, xl_invalid_page::present, report_invalid_page(), status(), and WARNING.

Referenced by CheckRecoveryConsistency().

◆ XLogDropDatabase()

void XLogDropDatabase ( Oid  dbid)

Definition at line 666 of file xlogutils.c.

667 {
668  /*
669  * This is unnecessarily heavy-handed, as it will close SMgrRelation
670  * objects for other databases as well. DROP DATABASE occurs seldom enough
671  * that it's not worth introducing a variant of smgrclose for just this
672  * purpose. XXX: Or should we rather leave the smgr entries dangling?
673  */
674  smgrcloseall();
675 
677 }
void smgrcloseall(void)
Definition: smgr.c:322
static void forget_invalid_pages_db(Oid dbid)
Definition: xlogutils.c:204

References forget_invalid_pages_db(), and smgrcloseall().

Referenced by dbase_redo().

◆ XLogDropRelation()

void XLogDropRelation ( RelFileNode  rnode,
ForkNumber  forknum 
)

Definition at line 655 of file xlogutils.c.

656 {
657  forget_invalid_pages(rnode, forknum, 0);
658 }
static void forget_invalid_pages(RelFileNode node, ForkNumber forkno, BlockNumber minblkno)
Definition: xlogutils.c:169

References forget_invalid_pages().

Referenced by DropRelationFiles().

◆ XLogHaveInvalidPages()

bool XLogHaveInvalidPages ( void  )

Definition at line 237 of file xlogutils.c.

238 {
239  if (invalid_page_tab != NULL &&
241  return true;
242  return false;
243 }
long hash_get_num_entries(HTAB *hashp)
Definition: dynahash.c:1382

References hash_get_num_entries(), and invalid_page_tab.

Referenced by RecoveryRestartPoint().

◆ XLogInitBufferForRedo()

◆ XLogReadBufferExtended()

Buffer XLogReadBufferExtended ( RelFileNode  rnode,
ForkNumber  forknum,
BlockNumber  blkno,
ReadBufferMode  mode,
Buffer  recent_buffer 
)

Definition at line 471 of file xlogutils.c.

474 {
475  BlockNumber lastblock;
476  Buffer buffer;
477  SMgrRelation smgr;
478 
479  Assert(blkno != P_NEW);
480 
481  /* Do we have a clue where the buffer might be already? */
482  if (BufferIsValid(recent_buffer) &&
483  mode == RBM_NORMAL &&
484  ReadRecentBuffer(rnode, forknum, blkno, recent_buffer))
485  {
486  buffer = recent_buffer;
487  goto recent_buffer_fast_path;
488  }
489 
490  /* Open the relation at smgr level */
491  smgr = smgropen(rnode, InvalidBackendId);
492 
493  /*
494  * Create the target file if it doesn't already exist. This lets us cope
495  * if the replay sequence contains writes to a relation that is later
496  * deleted. (The original coding of this routine would instead suppress
497  * the writes, but that seems like it risks losing valuable data if the
498  * filesystem loses an inode during a crash. Better to write the data
499  * until we are actually told to delete the file.)
500  */
501  smgrcreate(smgr, forknum, true);
502 
503  lastblock = smgrnblocks(smgr, forknum);
504 
505  if (blkno < lastblock)
506  {
507  /* page exists in file */
508  buffer = ReadBufferWithoutRelcache(rnode, forknum, blkno,
509  mode, NULL, true);
510  }
511  else
512  {
513  /* hm, page doesn't exist in file */
514  if (mode == RBM_NORMAL)
515  {
516  log_invalid_page(rnode, forknum, blkno, false);
517  return InvalidBuffer;
518  }
519  if (mode == RBM_NORMAL_NO_LOG)
520  return InvalidBuffer;
521  /* OK to extend the file */
522  /* we do this in recovery only - no rel-extension lock needed */
524  buffer = InvalidBuffer;
525  do
526  {
527  if (buffer != InvalidBuffer)
528  {
531  ReleaseBuffer(buffer);
532  }
533  buffer = ReadBufferWithoutRelcache(rnode, forknum,
534  P_NEW, mode, NULL, true);
535  }
536  while (BufferGetBlockNumber(buffer) < blkno);
537  /* Handle the corner case that P_NEW returns non-consecutive pages */
538  if (BufferGetBlockNumber(buffer) != blkno)
539  {
542  ReleaseBuffer(buffer);
543  buffer = ReadBufferWithoutRelcache(rnode, forknum, blkno,
544  mode, NULL, true);
545  }
546  }
547 
548 recent_buffer_fast_path:
549  if (mode == RBM_NORMAL)
550  {
551  /* check that page has been initialized */
552  Page page = (Page) BufferGetPage(buffer);
553 
554  /*
555  * We assume that PageIsNew is safe without a lock. During recovery,
556  * there should be no other backends that could modify the buffer at
557  * the same time.
558  */
559  if (PageIsNew(page))
560  {
561  ReleaseBuffer(buffer);
562  log_invalid_page(rnode, forknum, blkno, true);
563  return InvalidBuffer;
564  }
565  }
566 
567  return buffer;
568 }
uint32 BlockNumber
Definition: block.h:31
#define InvalidBuffer
Definition: buf.h:25
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition: bufmgr.c:2755
Buffer ReadBufferWithoutRelcache(RelFileNode rnode, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool permanent)
Definition: bufmgr.c:789
bool ReadRecentBuffer(RelFileNode rnode, ForkNumber forkNum, BlockNumber blockNum, Buffer recent_buffer)
Definition: bufmgr.c:623
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3915
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4156
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:96
#define P_NEW
Definition: bufmgr.h:91
#define BufferIsValid(bufnum)
Definition: bufmgr.h:123
@ RBM_ZERO_AND_CLEANUP_LOCK
Definition: bufmgr.h:42
@ RBM_NORMAL
Definition: bufmgr.h:39
@ RBM_NORMAL_NO_LOG
Definition: bufmgr.h:45
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
Pointer Page
Definition: bufpage.h:78
#define PageIsNew(page)
Definition: bufpage.h:228
Assert(fmt[strlen(fmt) - 1] !='\n')
static PgChecksumMode mode
Definition: pg_checksums.c:65
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition: smgr.c:579
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition: smgr.c:369
SMgrRelation smgropen(RelFileNode rnode, BackendId backend)
Definition: smgr.c:146
bool InRecovery
Definition: xlogutils.c:53
static void log_invalid_page(RelFileNode node, ForkNumber forkno, BlockNumber blkno, bool present)
Definition: xlogutils.c:105

References Assert(), BUFFER_LOCK_UNLOCK, BufferGetBlockNumber(), BufferGetPage, BufferIsValid, InRecovery, InvalidBackendId, InvalidBuffer, LockBuffer(), log_invalid_page(), mode, P_NEW, PageIsNew, RBM_NORMAL, RBM_NORMAL_NO_LOG, RBM_ZERO_AND_CLEANUP_LOCK, RBM_ZERO_AND_LOCK, ReadBufferWithoutRelcache(), ReadRecentBuffer(), ReleaseBuffer(), smgrcreate(), smgrnblocks(), and smgropen().

Referenced by verifyBackupPageConsistency(), XLogReadBufferForRedoExtended(), and XLogRecordPageWithFreeSpace().

◆ XLogReadBufferForRedo()

XLogRedoAction XLogReadBufferForRedo ( XLogReaderState record,
uint8  buffer_id,
Buffer buf 
)

Definition at line 316 of file xlogutils.c.

318 {
319  return XLogReadBufferForRedoExtended(record, block_id, RBM_NORMAL,
320  false, buf);
321 }

References buf, RBM_NORMAL, and XLogReadBufferForRedoExtended().

Referenced by _bt_clear_incomplete_split(), brin_xlog_desummarize_page(), brin_xlog_insert_update(), brin_xlog_revmap_extend(), brin_xlog_samepage_update(), brin_xlog_update(), btree_xlog_dedup(), btree_xlog_delete(), btree_xlog_insert(), btree_xlog_mark_page_halfdead(), btree_xlog_split(), btree_xlog_unlink_page(), generic_redo(), ginRedoClearIncompleteSplit(), ginRedoDeletePage(), ginRedoInsert(), ginRedoSplit(), ginRedoUpdateMetapage(), ginRedoVacuumDataLeafPage(), ginRedoVacuumPage(), gistRedoClearFollowRight(), gistRedoDeleteRecord(), gistRedoPageDelete(), gistRedoPageUpdateRecord(), hash_xlog_add_ovfl_page(), hash_xlog_delete(), hash_xlog_init_bitmap_page(), hash_xlog_insert(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_split_cleanup(), hash_xlog_split_complete(), hash_xlog_split_page(), hash_xlog_squeeze_page(), hash_xlog_update_meta_page(), hash_xlog_vacuum_one_page(), heap_xlog_confirm(), heap_xlog_delete(), heap_xlog_freeze_page(), heap_xlog_inplace(), heap_xlog_insert(), heap_xlog_lock(), heap_xlog_lock_updated(), heap_xlog_multi_insert(), heap_xlog_update(), heap_xlog_visible(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoSplitTuple(), spgRedoVacuumLeaf(), spgRedoVacuumRedirect(), spgRedoVacuumRoot(), and xlog_redo().

◆ XLogReadBufferForRedoExtended()

XLogRedoAction XLogReadBufferForRedoExtended ( XLogReaderState record,
uint8  buffer_id,
ReadBufferMode  mode,
bool  get_cleanup_lock,
Buffer buf 
)

Definition at line 353 of file xlogutils.c.

357 {
358  XLogRecPtr lsn = record->EndRecPtr;
359  RelFileNode rnode;
360  ForkNumber forknum;
361  BlockNumber blkno;
362  Buffer prefetch_buffer;
363  Page page;
364  bool zeromode;
365  bool willinit;
366 
367  if (!XLogRecGetBlockTagExtended(record, block_id, &rnode, &forknum, &blkno,
368  &prefetch_buffer))
369  {
370  /* Caller specified a bogus block_id */
371  elog(PANIC, "failed to locate backup block with ID %d in WAL record",
372  block_id);
373  }
374 
375  /*
376  * Make sure that if the block is marked with WILL_INIT, the caller is
377  * going to initialize it. And vice versa.
378  */
380  willinit = (XLogRecGetBlock(record, block_id)->flags & BKPBLOCK_WILL_INIT) != 0;
381  if (willinit && !zeromode)
382  elog(PANIC, "block with WILL_INIT flag in WAL record must be zeroed by redo routine");
383  if (!willinit && zeromode)
384  elog(PANIC, "block to be initialized in redo routine must be marked with WILL_INIT flag in the WAL record");
385 
386  /* If it has a full-page image and it should be restored, do it. */
387  if (XLogRecBlockImageApply(record, block_id))
388  {
389  Assert(XLogRecHasBlockImage(record, block_id));
390  *buf = XLogReadBufferExtended(rnode, forknum, blkno,
391  get_cleanup_lock ? RBM_ZERO_AND_CLEANUP_LOCK : RBM_ZERO_AND_LOCK,
392  prefetch_buffer);
393  page = BufferGetPage(*buf);
394  if (!RestoreBlockImage(record, block_id, page))
395  elog(ERROR, "failed to restore block image");
396 
397  /*
398  * The page may be uninitialized. If so, we can't set the LSN because
399  * that would corrupt the page.
400  */
401  if (!PageIsNew(page))
402  {
403  PageSetLSN(page, lsn);
404  }
405 
407 
408  /*
409  * At the end of crash recovery the init forks of unlogged relations
410  * are copied, without going through shared buffers. So we need to
411  * force the on-disk state of init forks to always be in sync with the
412  * state in shared buffers.
413  */
414  if (forknum == INIT_FORKNUM)
416 
417  return BLK_RESTORED;
418  }
419  else
420  {
421  *buf = XLogReadBufferExtended(rnode, forknum, blkno, mode, prefetch_buffer);
422  if (BufferIsValid(*buf))
423  {
425  {
426  if (get_cleanup_lock)
428  else
430  }
431  if (lsn <= PageGetLSN(BufferGetPage(*buf)))
432  return BLK_DONE;
433  else
434  return BLK_NEEDS_REDO;
435  }
436  else
437  return BLK_NOTFOUND;
438  }
439 }
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1573
void LockBufferForCleanup(Buffer buffer)
Definition: bufmgr.c:4213
void FlushOneBuffer(Buffer buffer)
Definition: bufmgr.c:3895
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define PageSetLSN(page, lsn)
Definition: bufpage.h:367
#define PageGetLSN(page)
Definition: bufpage.h:365
ForkNumber
Definition: relpath.h:41
@ INIT_FORKNUM
Definition: relpath.h:46
XLogRecPtr EndRecPtr
Definition: xlogreader.h:207
uint64 XLogRecPtr
Definition: xlogdefs.h:21
bool RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
Definition: xlogreader.c:2011
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
Definition: xlogreader.c:1953
#define XLogRecBlockImageApply(decoder, block_id)
Definition: xlogreader.h:423
#define XLogRecGetBlock(decoder, i)
Definition: xlogreader.h:417
#define XLogRecHasBlockImage(decoder, block_id)
Definition: xlogreader.h:421
#define BKPBLOCK_WILL_INIT
Definition: xlogrecord.h:188
Buffer XLogReadBufferExtended(RelFileNode rnode, ForkNumber forknum, BlockNumber blkno, ReadBufferMode mode, Buffer recent_buffer)
Definition: xlogutils.c:471

References Assert(), BKPBLOCK_WILL_INIT, BLK_DONE, BLK_NEEDS_REDO, BLK_NOTFOUND, BLK_RESTORED, buf, BUFFER_LOCK_EXCLUSIVE, BufferGetPage, BufferIsValid, elog, XLogReaderState::EndRecPtr, ERROR, FlushOneBuffer(), INIT_FORKNUM, LockBuffer(), LockBufferForCleanup(), MarkBufferDirty(), mode, PageGetLSN, PageIsNew, PageSetLSN, PANIC, RBM_ZERO_AND_CLEANUP_LOCK, RBM_ZERO_AND_LOCK, RestoreBlockImage(), XLogReadBufferExtended(), XLogRecBlockImageApply, XLogRecGetBlock, XLogRecGetBlockTagExtended(), and XLogRecHasBlockImage.

Referenced by btree_xlog_vacuum(), hash_xlog_delete(), hash_xlog_move_page_contents(), hash_xlog_split_allocate_page(), hash_xlog_squeeze_page(), hash_xlog_vacuum_one_page(), heap_xlog_prune(), heap_xlog_vacuum(), heap_xlog_visible(), XLogInitBufferForRedo(), and XLogReadBufferForRedo().

◆ XLogReadDetermineTimeline()

void XLogReadDetermineTimeline ( XLogReaderState state,
XLogRecPtr  wantPage,
uint32  wantLength,
TimeLineID  currTLI 
)

Definition at line 732 of file xlogutils.c.

734 {
735  const XLogRecPtr lastReadPage = (state->seg.ws_segno *
736  state->segcxt.ws_segsize + state->segoff);
737 
738  Assert(wantPage != InvalidXLogRecPtr && wantPage % XLOG_BLCKSZ == 0);
739  Assert(wantLength <= XLOG_BLCKSZ);
740  Assert(state->readLen == 0 || state->readLen <= XLOG_BLCKSZ);
741  Assert(currTLI != 0);
742 
743  /*
744  * If the desired page is currently read in and valid, we have nothing to
745  * do.
746  *
747  * The caller should've ensured that it didn't previously advance readOff
748  * past the valid limit of this timeline, so it doesn't matter if the
749  * current TLI has since become historical.
750  */
751  if (lastReadPage == wantPage &&
752  state->readLen != 0 &&
753  lastReadPage + state->readLen >= wantPage + Min(wantLength, XLOG_BLCKSZ - 1))
754  return;
755 
756  /*
757  * If we're reading from the current timeline, it hasn't become historical
758  * and the page we're reading is after the last page read, we can again
759  * just carry on. (Seeking backwards requires a check to make sure the
760  * older page isn't on a prior timeline).
761  *
762  * currTLI might've become historical since the caller obtained the value,
763  * but the caller is required not to read past the flush limit it saw at
764  * the time it looked up the timeline. There's nothing we can do about it
765  * if StartupXLOG() renames it to .partial concurrently.
766  */
767  if (state->currTLI == currTLI && wantPage >= lastReadPage)
768  {
769  Assert(state->currTLIValidUntil == InvalidXLogRecPtr);
770  return;
771  }
772 
773  /*
774  * If we're just reading pages from a previously validated historical
775  * timeline and the timeline we're reading from is valid until the end of
776  * the current segment we can just keep reading.
777  */
778  if (state->currTLIValidUntil != InvalidXLogRecPtr &&
779  state->currTLI != currTLI &&
780  state->currTLI != 0 &&
781  ((wantPage + wantLength) / state->segcxt.ws_segsize) <
782  (state->currTLIValidUntil / state->segcxt.ws_segsize))
783  return;
784 
785  /*
786  * If we reach this point we're either looking up a page for random
787  * access, the current timeline just became historical, or we're reading
788  * from a new segment containing a timeline switch. In all cases we need
789  * to determine the newest timeline on the segment.
790  *
791  * If it's the current timeline we can just keep reading from here unless
792  * we detect a timeline switch that makes the current timeline historical.
793  * If it's a historical timeline we can read all the segment on the newest
794  * timeline because it contains all the old timelines' data too. So only
795  * one switch check is required.
796  */
797  {
798  /*
799  * We need to re-read the timeline history in case it's been changed
800  * by a promotion or replay from a cascaded replica.
801  */
802  List *timelineHistory = readTimeLineHistory(currTLI);
803  XLogRecPtr endOfSegment;
804 
805  endOfSegment = ((wantPage / state->segcxt.ws_segsize) + 1) *
806  state->segcxt.ws_segsize - 1;
807  Assert(wantPage / state->segcxt.ws_segsize ==
808  endOfSegment / state->segcxt.ws_segsize);
809 
810  /*
811  * Find the timeline of the last LSN on the segment containing
812  * wantPage.
813  */
814  state->currTLI = tliOfPointInHistory(endOfSegment, timelineHistory);
815  state->currTLIValidUntil = tliSwitchPoint(state->currTLI, timelineHistory,
816  &state->nextTLI);
817 
818  Assert(state->currTLIValidUntil == InvalidXLogRecPtr ||
819  wantPage + wantLength < state->currTLIValidUntil);
820 
821  list_free_deep(timelineHistory);
822 
823  elog(DEBUG3, "switched to timeline %u valid until %X/%X",
824  state->currTLI,
825  LSN_FORMAT_ARGS(state->currTLIValidUntil));
826  }
827 }
TimeLineID tliOfPointInHistory(XLogRecPtr ptr, List *history)
Definition: timeline.c:552
XLogRecPtr tliSwitchPoint(TimeLineID tli, List *history, TimeLineID *nextTLI)
Definition: timeline.c:580
List * readTimeLineHistory(TimeLineID targetTLI)
Definition: timeline.c:76
#define Min(x, y)
Definition: c.h:986
#define DEBUG3
Definition: elog.h:22
void list_free_deep(List *list)
Definition: list.c:1519
Definition: pg_list.h:51
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28

References Assert(), DEBUG3, elog, InvalidXLogRecPtr, list_free_deep(), LSN_FORMAT_ARGS, Min, readTimeLineHistory(), tliOfPointInHistory(), and tliSwitchPoint().

Referenced by logical_read_xlog_page(), and read_local_xlog_page_guts().

◆ XLogTruncateRelation()

void XLogTruncateRelation ( RelFileNode  rnode,
ForkNumber  forkNum,
BlockNumber  nblocks 
)

Definition at line 685 of file xlogutils.c.

687 {
688  forget_invalid_pages(rnode, forkNum, nblocks);
689 }

References forget_invalid_pages().

Referenced by smgr_redo().

Variable Documentation

◆ InRecovery

◆ standbyState