PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 (RelFileLocator rlocator, ForkNumber forknum)
 
void XLogDropDatabase (Oid dbid)
 
void XLogTruncateRelation (RelFileLocator rlocator, ForkNumber forkNum, BlockNumber nblocks)
 
XLogRedoAction XLogReadBufferForRedo (XLogReaderState *record, uint8 block_id, Buffer *buf)
 
Buffer XLogInitBufferForRedo (XLogReaderState *record, uint8 block_id)
 
void XLogFlushBufferForRedoIfInit (XLogReaderState *record, uint8 block_id, Buffer buffer)
 
XLogRedoAction XLogReadBufferForRedoExtended (XLogReaderState *record, uint8 block_id, ReadBufferMode mode, bool get_cleanup_lock, Buffer *buf)
 
Buffer XLogReadBufferExtended (RelFileLocator rlocator, ForkNumber forknum, BlockNumber blkno, ReadBufferMode mode, Buffer recent_buffer)
 
Relation CreateFakeRelcacheEntry (RelFileLocator rlocator)
 
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 ignore_invalid_pages
 
PGDLLIMPORT bool InRecovery
 
PGDLLIMPORT HotStandbyState standbyState
 

Macro Definition Documentation

◆ InHotStandby

#define InHotStandby   (standbyState >= STANDBY_SNAPSHOT_PENDING)

Definition at line 60 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 50 of file xlogutils.h.

51{
HotStandbyState
Definition xlogutils.h:51
@ STANDBY_DISABLED
Definition xlogutils.h:52
@ STANDBY_SNAPSHOT_READY
Definition xlogutils.h:55
@ STANDBY_SNAPSHOT_PENDING
Definition xlogutils.h:54
@ STANDBY_INITIALIZED
Definition xlogutils.h:53

◆ XLogRedoAction

Enumerator
BLK_NEEDS_REDO 
BLK_DONE 
BLK_RESTORED 
BLK_NOTFOUND 

Definition at line 72 of file xlogutils.h.

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

Function Documentation

◆ CreateFakeRelcacheEntry()

Relation CreateFakeRelcacheEntry ( RelFileLocator  rlocator)
extern

Definition at line 595 of file xlogutils.c.

596{
598 Relation rel;
599
600 /* Allocate the Relation struct and all related space in one block. */
602 rel = (Relation) fakeentry;
603
604 rel->rd_rel = &fakeentry->pgc;
605 rel->rd_locator = rlocator;
606
607 /*
608 * We will never be working with temp rels during recovery or while
609 * syncing WAL-skipped files.
610 */
612
613 /* It must be a permanent table here */
614 rel->rd_rel->relpersistence = RELPERSISTENCE_PERMANENT;
615
616 /* We don't know the name of the relation; use relfilenumber instead */
617 sprintf(RelationGetRelationName(rel), "%u", rlocator.relNumber);
618
619 /*
620 * We set up the lockRelId in case anything tries to lock the dummy
621 * relation. Note that this is fairly bogus since relNumber may be
622 * different from the relation's OID. It shouldn't really matter though.
623 * In recovery, we are running by ourselves and can't have any lock
624 * conflicts. While syncing, we already hold AccessExclusiveLock.
625 */
626 rel->rd_lockInfo.lockRelId.dbId = rlocator.dbOid;
627 rel->rd_lockInfo.lockRelId.relId = rlocator.relNumber;
628
629 /*
630 * Set up a non-pinned SMgrRelation reference, so that we don't need to
631 * worry about unpinning it on error.
632 */
633 rel->rd_smgr = smgropen(rlocator, INVALID_PROC_NUMBER);
634
635 return rel;
636}
#define palloc0_object(type)
Definition fe_memutils.h:90
struct RelationData * Relation
Definition genam.h:30
#define sprintf
Definition port.h:263
static int fb(int x)
#define INVALID_PROC_NUMBER
Definition procnumber.h:26
#define RelationGetRelationName(relation)
Definition rel.h:550
SMgrRelation smgropen(RelFileLocator rlocator, ProcNumber backend)
Definition smgr.c:240
LockRelId lockRelId
Definition rel.h:46
Oid relId
Definition rel.h:40
Oid dbId
Definition rel.h:41
RelFileNumber relNumber
ProcNumber rd_backend
Definition rel.h:60
LockInfoData rd_lockInfo
Definition rel.h:114
SMgrRelation rd_smgr
Definition rel.h:58
RelFileLocator rd_locator
Definition rel.h:57
Form_pg_class rd_rel
Definition rel.h:111

References LockRelId::dbId, RelFileLocator::dbOid, fb(), INVALID_PROC_NUMBER, LockInfoData::lockRelId, palloc0_object, RelationData::rd_backend, RelationData::rd_locator, RelationData::rd_lockInfo, RelationData::rd_rel, RelationData::rd_smgr, RelationGetRelationName, LockRelId::relId, RelFileLocator::relNumber, smgropen(), and sprintf.

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

◆ FreeFakeRelcacheEntry()

void FreeFakeRelcacheEntry ( Relation  fakerel)
extern

Definition at line 642 of file xlogutils.c.

643{
644 pfree(fakerel);
645}
void pfree(void *pointer)
Definition mcxt.c:1619

References fb(), and pfree().

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

◆ read_local_xlog_page()

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

Definition at line 869 of file xlogutils.c.

871{
873 targetRecPtr, cur_page, true);
874}
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:893

References fb(), and read_local_xlog_page_guts().

Referenced by create_logical_replication_slot(), LogicalReplicationSlotCheckPendingWal(), LogicalSlotAdvanceAndCheckSnapState(), pg_logical_slot_get_changes_guts(), repack_setup_logical_decoding(), 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 
)
extern

Definition at line 881 of file xlogutils.c.

References fb(), and read_local_xlog_page_guts().

Referenced by InitXLogReaderState(), and repack_setup_logical_decoding().

◆ wal_segment_close()

◆ wal_segment_open()

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

Definition at line 830 of file xlogutils.c.

832{
833 TimeLineID tli = *tli_p;
834 char path[MAXPGPATH];
835
836 XLogFilePath(path, tli, nextSegNo, state->segcxt.ws_segsize);
837 state->seg.ws_file = BasicOpenFile(path, O_RDONLY | PG_BINARY);
838 if (state->seg.ws_file >= 0)
839 return;
840
841 if (errno == ENOENT)
844 errmsg("requested WAL segment %s has already been removed",
845 path)));
846 else
849 errmsg("could not open file \"%s\": %m",
850 path)));
851}
#define PG_BINARY
Definition c.h:1445
int errcode_for_file_access(void)
Definition elog.c:898
#define ERROR
Definition elog.h:40
#define ereport(elevel,...)
Definition elog.h:152
int BasicOpenFile(const char *fileName, int fileFlags)
Definition fd.c:1090
static char * errmsg
#define MAXPGPATH
static void XLogFilePath(char *path, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
uint32 TimeLineID
Definition xlogdefs.h:63

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

Referenced by create_logical_replication_slot(), InitXLogReaderState(), LogicalReplicationSlotCheckPendingWal(), LogicalSlotAdvanceAndCheckSnapState(), pg_logical_slot_get_changes_guts(), repack_setup_logical_decoding(), SummarizeWAL(), and XlogReadTwoPhaseData().

◆ WALReadRaiseError()

void WALReadRaiseError ( WALReadError errinfo)
extern

Definition at line 1047 of file xlogutils.c.

1048{
1049 WALOpenSegment *seg = &errinfo->wre_seg;
1050 char fname[MAXFNAMELEN];
1051
1052 XLogFileName(fname, seg->ws_tli, seg->ws_segno, wal_segment_size);
1053
1054 if (errinfo->wre_read < 0)
1055 {
1056 errno = errinfo->wre_errno;
1057 ereport(ERROR,
1059 errmsg("could not read from WAL segment %s, offset %d: %m",
1060 fname, errinfo->wre_off)));
1061 }
1062 else if (errinfo->wre_read == 0)
1063 {
1064 ereport(ERROR,
1066 errmsg("could not read from WAL segment %s, offset %d: read %d of %d",
1067 fname, errinfo->wre_off, errinfo->wre_read,
1068 errinfo->wre_req)));
1069 }
1070}
int errcode(int sqlerrcode)
Definition elog.c:875
#define ERRCODE_DATA_CORRUPTED
XLogSegNo ws_segno
Definition xlogreader.h:48
TimeLineID ws_tli
Definition xlogreader.h:49
int wal_segment_size
Definition xlog.c:150
#define MAXFNAMELEN
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)

References ereport, errcode(), ERRCODE_DATA_CORRUPTED, errcode_for_file_access(), errmsg, ERROR, fb(), MAXFNAMELEN, wal_segment_size, WALOpenSegment::ws_segno, WALOpenSegment::ws_tli, and XLogFileName().

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

◆ XLogCheckInvalidPages()

void XLogCheckInvalidPages ( void  )
extern

Definition at line 234 of file xlogutils.c.

235{
236 HASH_SEQ_STATUS status;
238 bool foundone = false;
239
240 if (invalid_page_tab == NULL)
241 return; /* nothing to do */
242
244
245 /*
246 * Our strategy is to emit WARNING messages for all remaining entries and
247 * only PANIC after we've dumped all the available info.
248 */
249 while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
250 {
251 report_invalid_page(WARNING, hentry->key.locator, hentry->key.forkno,
252 hentry->key.blkno, hentry->present);
253 foundone = true;
254 }
255
256 if (foundone)
258 "WAL contains references to invalid pages");
259
262}
void hash_destroy(HTAB *hashp)
Definition dynahash.c:802
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition dynahash.c:1352
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition dynahash.c:1317
#define WARNING
Definition elog.h:37
#define PANIC
Definition elog.h:44
#define elog(elevel,...)
Definition elog.h:228
bool ignore_invalid_pages
Definition xlogutils.c:34
static void report_invalid_page(int elevel, RelFileLocator locator, ForkNumber forkno, BlockNumber blkno, bool present)
Definition xlogutils.c:86
static HTAB * invalid_page_tab
Definition xlogutils.c:78

References elog, fb(), hash_destroy(), hash_seq_init(), hash_seq_search(), ignore_invalid_pages, invalid_page_tab, PANIC, report_invalid_page(), and WARNING.

Referenced by CheckRecoveryConsistency().

◆ XLogDropDatabase()

void XLogDropDatabase ( Oid  dbid)
extern

Definition at line 665 of file xlogutils.c.

666{
667 /*
668 * This is unnecessarily heavy-handed, as it will close SMgrRelation
669 * objects for other databases as well. DROP DATABASE occurs seldom enough
670 * that it's not worth introducing a variant of smgrdestroy for just this
671 * purpose.
672 */
674
676}
void smgrdestroyall(void)
Definition smgr.c:386
static void forget_invalid_pages_db(Oid dbid)
Definition xlogutils.c:196

References forget_invalid_pages_db(), and smgrdestroyall().

Referenced by dbase_redo().

◆ XLogDropRelation()

void XLogDropRelation ( RelFileLocator  rlocator,
ForkNumber  forknum 
)
extern

Definition at line 654 of file xlogutils.c.

655{
656 forget_invalid_pages(rlocator, forknum, 0);
657}
static void forget_invalid_pages(RelFileLocator locator, ForkNumber forkno, BlockNumber minblkno)
Definition xlogutils.c:165

References forget_invalid_pages().

Referenced by DropRelationFiles().

◆ XLogFlushBufferForRedoIfInit()

void XLogFlushBufferForRedoIfInit ( XLogReaderState record,
uint8  block_id,
Buffer  buffer 
)
extern

Definition at line 334 of file xlogutils.c.

336{
337 ForkNumber forknum;
338
339 Assert(BufferIsValid(buffer));
340
341 XLogRecGetBlockTag(record, block_id, NULL, &forknum, NULL);
342 if (forknum == INIT_FORKNUM)
343 FlushOneBuffer(buffer);
344}
void FlushOneBuffer(Buffer buffer)
Definition bufmgr.c:5575
static bool BufferIsValid(Buffer bufnum)
Definition bufmgr.h:419
#define Assert(condition)
Definition c.h:1002
ForkNumber
Definition relpath.h:56
@ INIT_FORKNUM
Definition relpath.h:61
void XLogRecGetBlockTag(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)

References Assert, BufferIsValid(), fb(), FlushOneBuffer(), INIT_FORKNUM, and XLogRecGetBlockTag().

Referenced by hash_xlog_init_bitmap_page(), hash_xlog_init_meta_page(), and seq_redo().

◆ XLogHaveInvalidPages()

bool XLogHaveInvalidPages ( void  )
extern

Definition at line 224 of file xlogutils.c.

225{
226 if (invalid_page_tab != NULL &&
228 return true;
229 return false;
230}
int64 hash_get_num_entries(HTAB *hashp)
Definition dynahash.c:1273

References fb(), hash_get_num_entries(), and invalid_page_tab.

Referenced by RecoveryRestartPoint().

◆ XLogInitBufferForRedo()

◆ XLogReadBufferExtended()

Buffer XLogReadBufferExtended ( RelFileLocator  rlocator,
ForkNumber  forknum,
BlockNumber  blkno,
ReadBufferMode  mode,
Buffer  recent_buffer 
)
extern

Definition at line 484 of file xlogutils.c.

487{
489 Buffer buffer;
490 SMgrRelation smgr;
491
492 Assert(blkno != P_NEW);
493
494 /* Do we have a clue where the buffer might be already? */
495 if (BufferIsValid(recent_buffer) &&
496 mode == RBM_NORMAL &&
497 ReadRecentBuffer(rlocator, forknum, blkno, recent_buffer))
498 {
499 buffer = recent_buffer;
501 }
502
503 /* Open the relation at smgr level */
504 smgr = smgropen(rlocator, INVALID_PROC_NUMBER);
505
506 /*
507 * Create the target file if it doesn't already exist. This lets us cope
508 * if the replay sequence contains writes to a relation that is later
509 * deleted. (The original coding of this routine would instead suppress
510 * the writes, but that seems like it risks losing valuable data if the
511 * filesystem loses an inode during a crash. Better to write the data
512 * until we are actually told to delete the file.)
513 */
514 smgrcreate(smgr, forknum, true);
515
516 lastblock = smgrnblocks(smgr, forknum);
517
518 if (blkno < lastblock)
519 {
520 /* page exists in file */
521 buffer = ReadBufferWithoutRelcache(rlocator, forknum, blkno,
522 mode, NULL, true);
523 }
524 else
525 {
526 /* hm, page doesn't exist in file */
527 if (mode == RBM_NORMAL)
528 {
529 log_invalid_page(rlocator, forknum, blkno, false);
530 return InvalidBuffer;
531 }
532 if (mode == RBM_NORMAL_NO_LOG)
533 return InvalidBuffer;
534 /* OK to extend the file */
535 /* we do this in recovery only - no rel-extension lock needed */
538 forknum,
539 NULL,
542 blkno + 1,
543 mode);
544 }
545
547 if (mode == RBM_NORMAL)
548 {
549 /* check that page has been initialized */
550 Page page = BufferGetPage(buffer);
551
552 /*
553 * We assume that PageIsNew is safe without a lock. During recovery,
554 * there should be no other backends that could modify the buffer at
555 * the same time.
556 */
557 if (PageIsNew(page))
558 {
559 ReleaseBuffer(buffer);
560 log_invalid_page(rlocator, forknum, blkno, true);
561 return InvalidBuffer;
562 }
563 }
564
565 return buffer;
566}
uint32 BlockNumber
Definition block.h:31
#define InvalidBuffer
Definition buf.h:25
Buffer ExtendBufferedRelTo(BufferManagerRelation bmr, ForkNumber fork, BufferAccessStrategy strategy, uint32 flags, BlockNumber extend_to, ReadBufferMode mode)
Definition bufmgr.c:1031
void ReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5595
Buffer ReadBufferWithoutRelcache(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy, bool permanent)
Definition bufmgr.c:954
bool ReadRecentBuffer(RelFileLocator rlocator, ForkNumber forkNum, BlockNumber blockNum, Buffer recent_buffer)
Definition bufmgr.c:818
#define P_NEW
Definition bufmgr.h:200
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:468
#define BMR_SMGR(p_smgr, p_relpersistence)
Definition bufmgr.h:116
@ EB_PERFORMING_RECOVERY
Definition bufmgr.h:78
@ EB_SKIP_EXTENSION_LOCK
Definition bufmgr.h:75
@ RBM_NORMAL
Definition bufmgr.h:46
@ RBM_NORMAL_NO_LOG
Definition bufmgr.h:52
static bool PageIsNew(const PageData *page)
Definition bufpage.h:258
PageData * Page
Definition bufpage.h:81
static PgChecksumMode mode
BlockNumber smgrnblocks(SMgrRelation reln, ForkNumber forknum)
Definition smgr.c:819
void smgrcreate(SMgrRelation reln, ForkNumber forknum, bool isRedo)
Definition smgr.c:481
bool InRecovery
Definition xlogutils.c:50
static void log_invalid_page(RelFileLocator locator, ForkNumber forkno, BlockNumber blkno, bool present)
Definition xlogutils.c:101

References Assert, BMR_SMGR, BufferGetPage(), BufferIsValid(), EB_PERFORMING_RECOVERY, EB_SKIP_EXTENSION_LOCK, ExtendBufferedRelTo(), fb(), InRecovery, INVALID_PROC_NUMBER, InvalidBuffer, log_invalid_page(), mode, P_NEW, PageIsNew(), RBM_NORMAL, RBM_NORMAL_NO_LOG, ReadBufferWithoutRelcache(), ReadRecentBuffer(), ReleaseBuffer(), smgrcreate(), smgrnblocks(), and smgropen().

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

◆ XLogReadBufferForRedo()

XLogRedoAction XLogReadBufferForRedo ( XLogReaderState record,
uint8  block_id,
Buffer buf 
)
extern

◆ XLogReadBufferForRedoExtended()

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

Definition at line 362 of file xlogutils.c.

366{
367 XLogRecPtr lsn = record->EndRecPtr;
368 RelFileLocator rlocator;
369 ForkNumber forknum;
370 BlockNumber blkno;
371 Buffer prefetch_buffer;
372 Page page;
373 bool zeromode;
374 bool willinit;
375
376 if (!XLogRecGetBlockTagExtended(record, block_id, &rlocator, &forknum, &blkno,
377 &prefetch_buffer))
378 {
379 /* Caller specified a bogus block_id */
380 elog(PANIC, "failed to locate backup block with ID %d in WAL record",
381 block_id);
382 }
383
384 /*
385 * Make sure that if the block is marked with WILL_INIT, the caller is
386 * going to initialize it. And vice versa.
387 */
389 willinit = (XLogRecGetBlock(record, block_id)->flags & BKPBLOCK_WILL_INIT) != 0;
390 if (willinit && !zeromode)
391 elog(PANIC, "block with WILL_INIT flag in WAL record must be zeroed by redo routine");
392 if (!willinit && zeromode)
393 elog(PANIC, "block to be initialized in redo routine must be marked with WILL_INIT flag in the WAL record");
394
395 /* If it has a full-page image and it should be restored, do it. */
396 if (XLogRecBlockImageApply(record, block_id))
397 {
399 *buf = XLogReadBufferExtended(rlocator, forknum, blkno,
401 prefetch_buffer);
402 page = BufferGetPage(*buf);
403 if (!RestoreBlockImage(record, block_id, page))
406 errmsg_internal("%s", record->errormsg_buf)));
407
408 /*
409 * The page may be uninitialized. If so, we can't set the LSN because
410 * that would corrupt the page.
411 */
412 if (!PageIsNew(page))
413 {
414 PageSetLSN(page, lsn);
415 }
416
418
419 /*
420 * At the end of crash recovery the init forks of unlogged relations
421 * are copied, without going through shared buffers. So we need to
422 * force the on-disk state of init forks to always be in sync with the
423 * state in shared buffers. Use XLogFlushBufferForRedoIfInit() for
424 * redo routines that dirty init-fork buffers without restoring a
425 * full-page image.
426 */
427 if (forknum == INIT_FORKNUM)
429
430 return BLK_RESTORED;
431 }
432 else
433 {
434 *buf = XLogReadBufferExtended(rlocator, forknum, blkno, mode, prefetch_buffer);
435 if (BufferIsValid(*buf))
436 {
438 {
441 else
443 }
444 if (lsn <= PageGetLSN(BufferGetPage(*buf)))
445 return BLK_DONE;
446 else
447 return BLK_NEEDS_REDO;
448 }
449 else
450 return BLK_NOTFOUND;
451 }
452}
void MarkBufferDirty(Buffer buffer)
Definition bufmgr.c:3156
void LockBufferForCleanup(Buffer buffer)
Definition bufmgr.c:6679
@ BUFFER_LOCK_EXCLUSIVE
Definition bufmgr.h:222
static void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition bufmgr.h:334
@ RBM_ZERO_AND_CLEANUP_LOCK
Definition bufmgr.h:49
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition bufpage.h:416
static XLogRecPtr PageGetLSN(const PageData *page)
Definition bufpage.h:410
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
char * errormsg_buf
Definition xlogreader.h:310
XLogRecPtr EndRecPtr
Definition xlogreader.h:206
uint64 XLogRecPtr
Definition xlogdefs.h:21
bool XLogRecGetBlockTagExtended(XLogReaderState *record, uint8 block_id, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum, Buffer *prefetch_buffer)
bool RestoreBlockImage(XLogReaderState *record, uint8 block_id, char *page)
#define XLogRecBlockImageApply(decoder, block_id)
Definition xlogreader.h:425
#define XLogRecGetBlock(decoder, i)
Definition xlogreader.h:419
#define XLogRecHasBlockImage(decoder, block_id)
Definition xlogreader.h:423
#define BKPBLOCK_WILL_INIT
Definition xlogrecord.h:199
Buffer XLogReadBufferExtended(RelFileLocator rlocator, ForkNumber forknum, BlockNumber blkno, ReadBufferMode mode, Buffer recent_buffer)
Definition xlogutils.c:484

References Assert, BKPBLOCK_WILL_INIT, BLK_DONE, BLK_NEEDS_REDO, BLK_NOTFOUND, BLK_RESTORED, buf, BUFFER_LOCK_EXCLUSIVE, BufferGetPage(), BufferIsValid(), elog, XLogReaderState::EndRecPtr, ereport, errcode(), errmsg_internal(), ERROR, XLogReaderState::errormsg_buf, fb(), 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_multi_insert(), heap_xlog_prune_freeze(), XLogInitBufferForRedo(), and XLogReadBufferForRedo().

◆ XLogReadDetermineTimeline()

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

Definition at line 731 of file xlogutils.c.

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

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

Referenced by logical_read_xlog_page(), and read_local_xlog_page_guts().

◆ XLogTruncateRelation()

void XLogTruncateRelation ( RelFileLocator  rlocator,
ForkNumber  forkNum,
BlockNumber  nblocks 
)
extern

Definition at line 684 of file xlogutils.c.

686{
687 forget_invalid_pages(rlocator, forkNum, nblocks);
688}

References forget_invalid_pages().

Referenced by smgr_redo().

Variable Documentation

◆ ignore_invalid_pages

PGDLLIMPORT bool ignore_invalid_pages
extern

Definition at line 34 of file xlogutils.c.

Referenced by log_invalid_page(), and XLogCheckInvalidPages().

◆ InRecovery

◆ standbyState