PostgreSQL Source Code  git master
xloginsert.c File Reference
#include "postgres.h"
#include "access/xact.h"
#include "access/xlog.h"
#include "access/xlog_internal.h"
#include "access/xloginsert.h"
#include "catalog/pg_control.h"
#include "common/pg_lzcompress.h"
#include "executor/instrument.h"
#include "miscadmin.h"
#include "pg_trace.h"
#include "replication/origin.h"
#include "storage/bufmgr.h"
#include "storage/proc.h"
#include "utils/memutils.h"
Include dependency graph for xloginsert.c:

Go to the source code of this file.

Data Structures

struct  registered_buffer
 

Macros

#define PGLZ_MAX_BLCKSZ   PGLZ_MAX_OUTPUT(BLCKSZ)
 
#define SizeOfXlogOrigin   (sizeof(RepOriginId) + sizeof(char))
 
#define HEADER_SCRATCH_SIZE
 

Functions

static XLogRecDataXLogRecordAssemble (RmgrId rmid, uint8 info, XLogRecPtr RedoRecPtr, bool doPageWrites, XLogRecPtr *fpw_lsn, int *num_fpi)
 
static bool XLogCompressBackupBlock (char *page, uint16 hole_offset, uint16 hole_length, char *dest, uint16 *dlen)
 
void XLogBeginInsert (void)
 
void XLogEnsureRecordSpace (int max_block_id, int ndatas)
 
void XLogResetInsertion (void)
 
void XLogRegisterBuffer (uint8 block_id, Buffer buffer, uint8 flags)
 
void XLogRegisterBlock (uint8 block_id, RelFileNode *rnode, ForkNumber forknum, BlockNumber blknum, Page page, uint8 flags)
 
void XLogRegisterData (char *data, int len)
 
void XLogRegisterBufData (uint8 block_id, char *data, int len)
 
void XLogSetRecordFlags (uint8 flags)
 
XLogRecPtr XLogInsert (RmgrId rmid, uint8 info)
 
bool XLogCheckBufferNeedsBackup (Buffer buffer)
 
XLogRecPtr XLogSaveBufferForHint (Buffer buffer, bool buffer_std)
 
XLogRecPtr log_newpage (RelFileNode *rnode, ForkNumber forkNum, BlockNumber blkno, Page page, bool page_std)
 
XLogRecPtr log_newpage_buffer (Buffer buffer, bool page_std)
 
void log_newpage_range (Relation rel, ForkNumber forkNum, BlockNumber startblk, BlockNumber endblk, bool page_std)
 
void InitXLogInsert (void)
 

Variables

static registered_bufferregistered_buffers
 
static int max_registered_buffers
 
static int max_registered_block_id = 0
 
static XLogRecDatamainrdata_head
 
static XLogRecDatamainrdata_last = (XLogRecData *) &mainrdata_head
 
static uint32 mainrdata_len
 
static uint8 curinsert_flags = 0
 
static XLogRecData hdr_rdt
 
static char * hdr_scratch = NULL
 
static XLogRecDatardatas
 
static int num_rdatas
 
static int max_rdatas
 
static bool begininsert_called = false
 
static MemoryContext xloginsert_cxt
 

Macro Definition Documentation

◆ HEADER_SCRATCH_SIZE

#define HEADER_SCRATCH_SIZE
Value:
MaxSizeOfXLogRecordBlockHeader * (XLR_MAX_BLOCK_ID + 1) + \
#define SizeOfXlogOrigin
Definition: xloginsert.c:91
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:221
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define SizeOfXLogRecordDataHeaderLong
Definition: xlogrecord.h:208

Definition at line 93 of file xloginsert.c.

Referenced by InitXLogInsert().

◆ PGLZ_MAX_BLCKSZ

#define PGLZ_MAX_BLCKSZ   PGLZ_MAX_OUTPUT(BLCKSZ)

Definition at line 37 of file xloginsert.c.

◆ SizeOfXlogOrigin

#define SizeOfXlogOrigin   (sizeof(RepOriginId) + sizeof(char))

Definition at line 91 of file xloginsert.c.

Function Documentation

◆ InitXLogInsert()

void InitXLogInsert ( void  )

Definition at line 1121 of file xloginsert.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, hdr_scratch, HEADER_SCRATCH_SIZE, max_rdatas, max_registered_buffers, MemoryContextAlloc(), MemoryContextAllocZero(), TopMemoryContext, XLR_NORMAL_MAX_BLOCK_ID, and XLR_NORMAL_RDATAS.

Referenced by CreateCheckPoint(), and InitXLOGAccess().

1122 {
1123  /* Initialize the working areas */
1124  if (xloginsert_cxt == NULL)
1125  {
1127  "WAL record construction",
1129  }
1130 
1131  if (registered_buffers == NULL)
1132  {
1135  sizeof(registered_buffer) * (XLR_NORMAL_MAX_BLOCK_ID + 1));
1137  }
1138  if (rdatas == NULL)
1139  {
1141  sizeof(XLogRecData) * XLR_NORMAL_RDATAS);
1143  }
1144 
1145  /*
1146  * Allocate a buffer to hold the header information for a WAL record.
1147  */
1148  if (hdr_scratch == NULL)
1151 }
#define AllocSetContextCreate
Definition: memutils.h:170
#define HEADER_SCRATCH_SIZE
Definition: xloginsert.c:93
#define XLR_NORMAL_RDATAS
Definition: xloginsert.h:28
static int max_registered_buffers
Definition: xloginsert.c:65
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
static int max_rdatas
Definition: xloginsert.c:103
MemoryContext TopMemoryContext
Definition: mcxt.c:44
static registered_buffer * registered_buffers
Definition: xloginsert.c:64
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:839
#define XLR_NORMAL_MAX_BLOCK_ID
Definition: xloginsert.h:27
static char * hdr_scratch
Definition: xloginsert.c:89
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:796
static MemoryContext xloginsert_cxt
Definition: xloginsert.c:108
static XLogRecData * rdatas
Definition: xloginsert.c:101

◆ log_newpage()

XLogRecPtr log_newpage ( RelFileNode rnode,
ForkNumber  forkNum,
BlockNumber  blkno,
Page  page,
bool  page_std 
)

Definition at line 977 of file xloginsert.c.

References PageIsNew, PageSetLSN, REGBUF_FORCE_IMAGE, REGBUF_STANDARD, XLOG_FPI, XLogBeginInsert(), XLogInsert(), and XLogRegisterBlock().

Referenced by _bt_blwritepage(), _hash_alloc_buckets(), _hash_init(), blbuildempty(), btbuildempty(), end_heap_rewrite(), log_newpage_buffer(), raw_heap_insert(), RelationCopyStorage(), and spgbuildempty().

979 {
980  int flags;
981  XLogRecPtr recptr;
982 
983  flags = REGBUF_FORCE_IMAGE;
984  if (page_std)
985  flags |= REGBUF_STANDARD;
986 
987  XLogBeginInsert();
988  XLogRegisterBlock(0, rnode, forkNum, blkno, page, flags);
989  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
990 
991  /*
992  * The page may be uninitialized. If so, we can't set the LSN because that
993  * would corrupt the page.
994  */
995  if (!PageIsNew(page))
996  {
997  PageSetLSN(page, recptr);
998  }
999 
1000  return recptr;
1001 }
void XLogRegisterBlock(uint8 block_id, RelFileNode *rnode, ForkNumber forknum, BlockNumber blknum, Page page, uint8 flags)
Definition: xloginsert.c:267
#define REGBUF_STANDARD
Definition: xloginsert.h:35
#define REGBUF_FORCE_IMAGE
Definition: xloginsert.h:31
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:416
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define XLOG_FPI
Definition: pg_control.h:78
#define PageIsNew(page)
Definition: bufpage.h:229
void XLogBeginInsert(void)
Definition: xloginsert.c:121
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368

◆ log_newpage_buffer()

XLogRecPtr log_newpage_buffer ( Buffer  buffer,
bool  page_std 
)

Definition at line 1014 of file xloginsert.c.

References Assert, BufferGetPage, BufferGetTag(), CritSectionCount, and log_newpage().

Referenced by brin_initialize_empty_new_buffer(), brinbuildempty(), FreeSpaceMapPrepareTruncateRel(), ginbuildempty(), gistbuildempty(), lazy_scan_heap(), and visibilitymap_prepare_truncate().

1015 {
1016  Page page = BufferGetPage(buffer);
1017  RelFileNode rnode;
1018  ForkNumber forkNum;
1019  BlockNumber blkno;
1020 
1021  /* Shared buffers should be modified in a critical section. */
1022  Assert(CritSectionCount > 0);
1023 
1024  BufferGetTag(buffer, &rnode, &forkNum, &blkno);
1025 
1026  return log_newpage(&rnode, forkNum, blkno, page, page_std);
1027 }
uint32 BlockNumber
Definition: block.h:31
volatile uint32 CritSectionCount
Definition: globals.c:38
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
ForkNumber
Definition: relpath.h:40
#define Assert(condition)
Definition: c.h:745
XLogRecPtr log_newpage(RelFileNode *rnode, ForkNumber forkNum, BlockNumber blkno, Page page, bool page_std)
Definition: xloginsert.c:977
void BufferGetTag(Buffer buffer, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:2654
Pointer Page
Definition: bufpage.h:78

◆ log_newpage_range()

void log_newpage_range ( Relation  rel,
ForkNumber  forkNum,
BlockNumber  startblk,
BlockNumber  endblk,
bool  page_std 
)

Definition at line 1047 of file xloginsert.c.

References buf, BUFFER_LOCK_EXCLUSIVE, BufferGetPage, CHECK_FOR_INTERRUPTS, END_CRIT_SECTION, i, LockBuffer(), MarkBufferDirty(), PageIsNew, PageSetLSN, RBM_NORMAL, ReadBufferExtended(), REGBUF_FORCE_IMAGE, REGBUF_STANDARD, START_CRIT_SECTION, UnlockReleaseBuffer(), XLOG_FPI, XLogBeginInsert(), XLogEnsureRecordSpace(), XLogInsert(), XLogRegisterBuffer(), and XLR_MAX_BLOCK_ID.

Referenced by ginbuild(), gistbuild(), smgrDoPendingSyncs(), and spgbuild().

1050 {
1051  int flags;
1052  BlockNumber blkno;
1053 
1054  flags = REGBUF_FORCE_IMAGE;
1055  if (page_std)
1056  flags |= REGBUF_STANDARD;
1057 
1058  /*
1059  * Iterate over all the pages in the range. They are collected into
1060  * batches of XLR_MAX_BLOCK_ID pages, and a single WAL-record is written
1061  * for each batch.
1062  */
1064 
1065  blkno = startblk;
1066  while (blkno < endblk)
1067  {
1068  Buffer bufpack[XLR_MAX_BLOCK_ID];
1069  XLogRecPtr recptr;
1070  int nbufs;
1071  int i;
1072 
1074 
1075  /* Collect a batch of blocks. */
1076  nbufs = 0;
1077  while (nbufs < XLR_MAX_BLOCK_ID && blkno < endblk)
1078  {
1079  Buffer buf = ReadBufferExtended(rel, forkNum, blkno,
1080  RBM_NORMAL, NULL);
1081 
1083 
1084  /*
1085  * Completely empty pages are not WAL-logged. Writing a WAL record
1086  * would change the LSN, and we don't want that. We want the page
1087  * to stay empty.
1088  */
1089  if (!PageIsNew(BufferGetPage(buf)))
1090  bufpack[nbufs++] = buf;
1091  else
1092  UnlockReleaseBuffer(buf);
1093  blkno++;
1094  }
1095 
1096  /* Write WAL record for this batch. */
1097  XLogBeginInsert();
1098 
1100  for (i = 0; i < nbufs; i++)
1101  {
1102  XLogRegisterBuffer(i, bufpack[i], flags);
1103  MarkBufferDirty(bufpack[i]);
1104  }
1105 
1106  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
1107 
1108  for (i = 0; i < nbufs; i++)
1109  {
1110  PageSetLSN(BufferGetPage(bufpack[i]), recptr);
1111  UnlockReleaseBuffer(bufpack[i]);
1112  }
1113  END_CRIT_SECTION();
1114  }
1115 }
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1468
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:214
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:652
#define END_CRIT_SECTION()
Definition: miscadmin.h:134
#define START_CRIT_SECTION()
Definition: miscadmin.h:132
uint32 BlockNumber
Definition: block.h:31
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3506
static char * buf
Definition: pg_test_fsync.c:67
#define REGBUF_STANDARD
Definition: xloginsert.h:35
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:221
#define REGBUF_FORCE_IMAGE
Definition: xloginsert.h:31
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:416
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3722
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void XLogEnsureRecordSpace(int max_block_id, int ndatas)
Definition: xloginsert.c:147
#define XLOG_FPI
Definition: pg_control.h:78
#define PageIsNew(page)
Definition: bufpage.h:229
int i
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
void XLogBeginInsert(void)
Definition: xloginsert.c:121
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23

◆ XLogBeginInsert()

void XLogBeginInsert ( void  )

Definition at line 121 of file xloginsert.c.

References Assert, begininsert_called, elog, ERROR, mainrdata_len, max_registered_block_id, and XLogInsertAllowed().

Referenced by _bt_dedup_one_page(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_getroot(), _bt_insertonpg(), _bt_log_reuse_page(), _bt_mark_page_halfdead(), _bt_newroot(), _bt_split(), _bt_unlink_halfdead_page(), _bt_update_meta_cleanup_info(), _hash_addovflpage(), _hash_doinsert(), _hash_expandtable(), _hash_freeovflpage(), _hash_init(), _hash_splitbucket(), _hash_squeezebucket(), _hash_vacuum_one_page(), addLeafTuple(), AssignTransactionId(), brin_doinsert(), brin_doupdate(), brinbuild(), brinRevmapDesummarizeRange(), CreateCheckPoint(), createdb(), CreateEndOfRecoveryRecord(), createPostingTree(), CreateTableSpace(), do_pg_stop_backup(), do_setval(), doPickSplit(), DropTableSpace(), EndPrepare(), ExecuteTruncateGuts(), fill_seq_with_data(), GenericXLogFinish(), ginDeletePage(), ginHeapTupleFastInsert(), ginPlaceToPage(), ginUpdateStats(), ginVacuumPostingTreeLeaf(), gistXLogAssignLSN(), gistXLogDelete(), gistXLogPageDelete(), gistXLogPageReuse(), gistXLogSplit(), gistXLogUpdate(), hashbucketcleanup(), hashbulkdelete(), heap_abort_speculative(), heap_delete(), heap_finish_speculative(), heap_inplace_update(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_update(), log_heap_clean(), log_heap_cleanup_info(), log_heap_freeze(), log_heap_new_cid(), log_heap_update(), log_heap_visible(), log_newpage(), log_newpage_range(), log_smgrcreate(), log_split_page(), LogAccessExclusiveLocks(), LogCurrentRunningXacts(), logical_heap_rewrite_flush_mappings(), LogLogicalMessage(), LogStandbyInvalidations(), movedb(), moveLeafs(), MultiXactIdCreateFromMembers(), nextval_internal(), pg_truncate_visibility_map(), RelationTruncate(), remove_dbtablespaces(), replorigin_advance(), replorigin_drop(), RequestXLogSwitch(), revmap_physical_extend(), shiftList(), spgAddNodeAction(), spgSplitNodeAction(), UpdateFullPageWrites(), vacuumLeafPage(), vacuumLeafRoot(), vacuumRedirectAndPlaceholder(), write_relmap_file(), writeListPage(), WriteMTruncateXlogRec(), WriteMZeroPageXlogRec(), WriteSetTimestampXlogRec(), WriteTruncateXlogRec(), WriteZeroPageXlogRec(), XactLogAbortRecord(), XactLogCommitRecord(), XLogPutNextOid(), XLogReportParameters(), XLogRestorePoint(), XLogSaveBufferForHint(), and xlogVacuumPage().

122 {
125  Assert(mainrdata_len == 0);
126 
127  /* cross-check on whether we should be here or not */
128  if (!XLogInsertAllowed())
129  elog(ERROR, "cannot make new WAL entries during recovery");
130 
131  if (begininsert_called)
132  elog(ERROR, "XLogBeginInsert was already called");
133 
134  begininsert_called = true;
135 }
static uint32 mainrdata_len
Definition: xloginsert.c:75
static bool begininsert_called
Definition: xloginsert.c:105
#define ERROR
Definition: elog.h:43
static XLogRecData * mainrdata_head
Definition: xloginsert.c:73
bool XLogInsertAllowed(void)
Definition: xlog.c:8185
static XLogRecData * mainrdata_last
Definition: xloginsert.c:74
#define Assert(condition)
Definition: c.h:745
static int max_registered_block_id
Definition: xloginsert.c:66
#define elog(elevel,...)
Definition: elog.h:214

◆ XLogCheckBufferNeedsBackup()

bool XLogCheckBufferNeedsBackup ( Buffer  buffer)

Definition at line 860 of file xloginsert.c.

References BufferGetPage, doPageWrites, GetFullPageWriteInfo(), PageGetLSN, and RedoRecPtr.

Referenced by log_heap_update().

861 {
863  bool doPageWrites;
864  Page page;
865 
866  GetFullPageWriteInfo(&RedoRecPtr, &doPageWrites);
867 
868  page = BufferGetPage(buffer);
869 
870  if (doPageWrites && PageGetLSN(page) <= RedoRecPtr)
871  return true; /* buffer requires backup */
872 
873  return false; /* buffer does not need to be backed up */
874 }
static bool doPageWrites
Definition: xlog.c:383
static XLogRecPtr RedoRecPtr
Definition: xlog.c:376
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define PageGetLSN(page)
Definition: bufpage.h:366
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition: xlog.c:8393
Pointer Page
Definition: bufpage.h:78

◆ XLogCompressBackupBlock()

static bool XLogCompressBackupBlock ( char *  page,
uint16  hole_offset,
uint16  hole_length,
char *  dest,
uint16 dlen 
)
static

Definition at line 810 of file xloginsert.c.

References PGAlignedBlock::data, pglz_compress(), PGLZ_strategy_default, and SizeOfXLogRecordBlockCompressHeader.

Referenced by XLogRecordAssemble().

812 {
813  int32 orig_len = BLCKSZ - hole_length;
814  int32 len;
815  int32 extra_bytes = 0;
816  char *source;
817  PGAlignedBlock tmp;
818 
819  if (hole_length != 0)
820  {
821  /* must skip the hole */
822  source = tmp.data;
823  memcpy(source, page, hole_offset);
824  memcpy(source + hole_offset,
825  page + (hole_offset + hole_length),
826  BLCKSZ - (hole_length + hole_offset));
827 
828  /*
829  * Extra data needs to be stored in WAL record for the compressed
830  * version of block image if the hole exists.
831  */
833  }
834  else
835  source = page;
836 
837  /*
838  * We recheck the actual size even if pglz_compress() reports success and
839  * see if the number of bytes saved by compression is larger than the
840  * length of extra data needed for the compressed version of block image.
841  */
842  len = pglz_compress(source, orig_len, dest, PGLZ_strategy_default);
843  if (len >= 0 &&
844  len + extra_bytes < orig_len)
845  {
846  *dlen = (uint16) len; /* successful compression */
847  return true;
848  }
849  return false;
850 }
signed int int32
Definition: c.h:362
char data[BLCKSZ]
Definition: c.h:1111
unsigned short uint16
Definition: c.h:373
int32 pglz_compress(const char *source, int32 slen, char *dest, const PGLZ_Strategy *strategy)
const PGLZ_Strategy *const PGLZ_strategy_default
#define SizeOfXLogRecordBlockCompressHeader
Definition: xlogrecord.h:160

◆ XLogEnsureRecordSpace()

void XLogEnsureRecordSpace ( int  max_block_id,
int  ndatas 
)

Definition at line 147 of file xloginsert.c.

References Assert, CritSectionCount, elog, ERROR, max_rdatas, max_registered_buffers, MemSet, repalloc(), XLR_MAX_BLOCK_ID, XLR_NORMAL_MAX_BLOCK_ID, and XLR_NORMAL_RDATAS.

Referenced by _hash_freeovflpage(), _hash_squeezebucket(), EndPrepare(), gistplacetopage(), log_newpage_range(), and shiftList().

148 {
149  int nbuffers;
150 
151  /*
152  * This must be called before entering a critical section, because
153  * allocating memory inside a critical section can fail. repalloc() will
154  * check the same, but better to check it here too so that we fail
155  * consistently even if the arrays happen to be large enough already.
156  */
157  Assert(CritSectionCount == 0);
158 
159  /* the minimum values can't be decreased */
160  if (max_block_id < XLR_NORMAL_MAX_BLOCK_ID)
161  max_block_id = XLR_NORMAL_MAX_BLOCK_ID;
162  if (ndatas < XLR_NORMAL_RDATAS)
163  ndatas = XLR_NORMAL_RDATAS;
164 
165  if (max_block_id > XLR_MAX_BLOCK_ID)
166  elog(ERROR, "maximum number of WAL record block references exceeded");
167  nbuffers = max_block_id + 1;
168 
169  if (nbuffers > max_registered_buffers)
170  {
172  repalloc(registered_buffers, sizeof(registered_buffer) * nbuffers);
173 
174  /*
175  * At least the padding bytes in the structs must be zeroed, because
176  * they are included in WAL data, but initialize it all for tidiness.
177  */
179  (nbuffers - max_registered_buffers) * sizeof(registered_buffer));
180  max_registered_buffers = nbuffers;
181  }
182 
183  if (ndatas > max_rdatas)
184  {
185  rdatas = (XLogRecData *) repalloc(rdatas, sizeof(XLogRecData) * ndatas);
186  max_rdatas = ndatas;
187  }
188 }
#define XLR_NORMAL_RDATAS
Definition: xloginsert.h:28
#define MemSet(start, val, len)
Definition: c.h:978
#define ERROR
Definition: elog.h:43
static int max_registered_buffers
Definition: xloginsert.c:65
volatile uint32 CritSectionCount
Definition: globals.c:38
static int max_rdatas
Definition: xloginsert.c:103
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:221
static registered_buffer * registered_buffers
Definition: xloginsert.c:64
#define XLR_NORMAL_MAX_BLOCK_ID
Definition: xloginsert.h:27
#define Assert(condition)
Definition: c.h:745
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1069
#define elog(elevel,...)
Definition: elog.h:214
static XLogRecData * rdatas
Definition: xloginsert.c:101

◆ XLogInsert()

XLogRecPtr XLogInsert ( RmgrId  rmid,
uint8  info 
)

Definition at line 416 of file xloginsert.c.

References begininsert_called, curinsert_flags, doPageWrites, elog, ERROR, GetFullPageWriteInfo(), InvalidXLogRecPtr, IsBootstrapProcessingMode, PANIC, RedoRecPtr, SizeOfXLogLongPHD, XLogInsertRecord(), XLogRecordAssemble(), XLogResetInsertion(), XLR_CHECK_CONSISTENCY, XLR_RMGR_INFO_MASK, and XLR_SPECIAL_REL_UPDATE.

Referenced by _bt_dedup_one_page(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_getroot(), _bt_insertonpg(), _bt_log_reuse_page(), _bt_mark_page_halfdead(), _bt_newroot(), _bt_split(), _bt_unlink_halfdead_page(), _bt_update_meta_cleanup_info(), _hash_addovflpage(), _hash_doinsert(), _hash_expandtable(), _hash_freeovflpage(), _hash_init(), _hash_splitbucket(), _hash_squeezebucket(), _hash_vacuum_one_page(), addLeafTuple(), AssignTransactionId(), brin_doinsert(), brin_doupdate(), brinbuild(), brinRevmapDesummarizeRange(), CreateCheckPoint(), createdb(), CreateEndOfRecoveryRecord(), createPostingTree(), CreateTableSpace(), do_pg_stop_backup(), do_setval(), doPickSplit(), DropTableSpace(), EndPrepare(), ExecuteTruncateGuts(), fill_seq_with_data(), GenericXLogFinish(), ginDeletePage(), ginHeapTupleFastInsert(), ginPlaceToPage(), ginUpdateStats(), ginVacuumPostingTreeLeaf(), gistXLogAssignLSN(), gistXLogDelete(), gistXLogPageDelete(), gistXLogPageReuse(), gistXLogSplit(), gistXLogUpdate(), hashbucketcleanup(), hashbulkdelete(), heap_abort_speculative(), heap_delete(), heap_finish_speculative(), heap_inplace_update(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_update(), log_heap_clean(), log_heap_cleanup_info(), log_heap_freeze(), log_heap_new_cid(), log_heap_update(), log_heap_visible(), log_newpage(), log_newpage_range(), log_smgrcreate(), log_split_page(), LogAccessExclusiveLocks(), LogCurrentRunningXacts(), logical_heap_rewrite_flush_mappings(), LogLogicalMessage(), LogStandbyInvalidations(), movedb(), moveLeafs(), MultiXactIdCreateFromMembers(), nextval_internal(), pg_truncate_visibility_map(), RelationTruncate(), remove_dbtablespaces(), replorigin_advance(), replorigin_drop(), RequestXLogSwitch(), revmap_physical_extend(), shiftList(), spgAddNodeAction(), spgSplitNodeAction(), UpdateFullPageWrites(), vacuumLeafPage(), vacuumLeafRoot(), vacuumRedirectAndPlaceholder(), write_relmap_file(), writeListPage(), WriteMTruncateXlogRec(), WriteMZeroPageXlogRec(), WriteSetTimestampXlogRec(), WriteTruncateXlogRec(), WriteZeroPageXlogRec(), XactLogAbortRecord(), XactLogCommitRecord(), XLogPutNextOid(), XLogReportParameters(), XLogRestorePoint(), XLogSaveBufferForHint(), and xlogVacuumPage().

417 {
418  XLogRecPtr EndPos;
419 
420  /* XLogBeginInsert() must have been called. */
421  if (!begininsert_called)
422  elog(ERROR, "XLogBeginInsert was not called");
423 
424  /*
425  * The caller can set rmgr bits, XLR_SPECIAL_REL_UPDATE and
426  * XLR_CHECK_CONSISTENCY; the rest are reserved for use by me.
427  */
428  if ((info & ~(XLR_RMGR_INFO_MASK |
430  XLR_CHECK_CONSISTENCY)) != 0)
431  elog(PANIC, "invalid xlog info mask %02X", info);
432 
433  TRACE_POSTGRESQL_WAL_INSERT(rmid, info);
434 
435  /*
436  * In bootstrap mode, we don't actually log anything but XLOG resources;
437  * return a phony record pointer.
438  */
439  if (IsBootstrapProcessingMode() && rmid != RM_XLOG_ID)
440  {
442  EndPos = SizeOfXLogLongPHD; /* start of 1st chkpt record */
443  return EndPos;
444  }
445 
446  do
447  {
449  bool doPageWrites;
450  XLogRecPtr fpw_lsn;
451  XLogRecData *rdt;
452  int num_fpi = 0;
453 
454  /*
455  * Get values needed to decide whether to do full-page writes. Since
456  * we don't yet have an insertion lock, these could change under us,
457  * but XLogInsertRecord will recheck them once it has a lock.
458  */
459  GetFullPageWriteInfo(&RedoRecPtr, &doPageWrites);
460 
461  rdt = XLogRecordAssemble(rmid, info, RedoRecPtr, doPageWrites,
462  &fpw_lsn, &num_fpi);
463 
464  EndPos = XLogInsertRecord(rdt, fpw_lsn, curinsert_flags, num_fpi);
465  } while (EndPos == InvalidXLogRecPtr);
466 
468 
469  return EndPos;
470 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:71
static bool begininsert_called
Definition: xloginsert.c:105
#define PANIC
Definition: elog.h:53
#define XLR_CHECK_CONSISTENCY
Definition: xlogrecord.h:80
static bool doPageWrites
Definition: xlog.c:383
#define ERROR
Definition: elog.h:43
void XLogResetInsertion(void)
Definition: xloginsert.c:194
static XLogRecData * XLogRecordAssemble(RmgrId rmid, uint8 info, XLogRecPtr RedoRecPtr, bool doPageWrites, XLogRecPtr *fpw_lsn, int *num_fpi)
Definition: xloginsert.c:485
static XLogRecPtr RedoRecPtr
Definition: xlog.c:376
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:392
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition: xlog.c:8393
#define elog(elevel,...)
Definition: elog.h:214
XLogRecPtr XLogInsertRecord(XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi)
Definition: xlog.c:1001
static uint8 curinsert_flags
Definition: xloginsert.c:78
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:72
#define XLR_RMGR_INFO_MASK
Definition: xlogrecord.h:63

◆ XLogRecordAssemble()

static XLogRecData * XLogRecordAssemble ( RmgrId  rmid,
uint8  info,
XLogRecPtr  RedoRecPtr,
bool  doPageWrites,
XLogRecPtr fpw_lsn,
int *  num_fpi 
)
static

Definition at line 485 of file xloginsert.c.

References XLogRecordBlockImageHeader::bimg_info, registered_buffer::bkp_rdatas, BKPBLOCK_HAS_DATA, BKPBLOCK_HAS_IMAGE, BKPBLOCK_SAME_REL, BKPBLOCK_WILL_INIT, BKPIMAGE_APPLY, BKPIMAGE_HAS_HOLE, BKPIMAGE_IS_COMPRESSED, registered_buffer::block, COMP_CRC32C, registered_buffer::compressed_page, curinsert_flags, XLogRecData::data, XLogRecordBlockHeader::data_length, registered_buffer::flags, XLogRecordBlockHeader::fork_flags, registered_buffer::forkno, GetCurrentTransactionIdIfAny(), hdr_rdt, hdr_scratch, XLogRecordBlockCompressHeader::hole_length, XLogRecordBlockImageHeader::hole_offset, XLogRecordBlockHeader::id, registered_buffer::in_use, INIT_CRC32C, InvalidRepOriginId, InvalidXLogRecPtr, XLogRecData::len, XLogRecordBlockImageHeader::length, lower(), mainrdata_head, mainrdata_last, mainrdata_len, max_registered_block_id, XLogRecData::next, registered_buffer::page, PageGetLSN, registered_buffer::rdata_head, registered_buffer::rdata_len, registered_buffer::rdata_tail, RedoRecPtr, REGBUF_FORCE_IMAGE, REGBUF_KEEP_DATA, REGBUF_NO_IMAGE, REGBUF_STANDARD, REGBUF_WILL_INIT, RelFileNodeEquals, replorigin_session_origin, registered_buffer::rnode, SizeOfPageHeaderData, SizeOfXLogRecord, SizeOfXLogRecordBlockCompressHeader, SizeOfXLogRecordBlockHeader, SizeOfXLogRecordBlockImageHeader, upper(), wal_compression, wal_consistency_checking, XLogRecord::xl_crc, XLogRecord::xl_info, XLogRecord::xl_prev, XLogRecord::xl_rmid, XLogRecord::xl_tot_len, XLogRecord::xl_xid, XLOG_INCLUDE_ORIGIN, XLogCompressBackupBlock(), XLR_BLOCK_ID_DATA_LONG, XLR_BLOCK_ID_DATA_SHORT, XLR_BLOCK_ID_ORIGIN, and XLR_CHECK_CONSISTENCY.

Referenced by XLogInsert().

488 {
489  XLogRecData *rdt;
490  uint32 total_len = 0;
491  int block_id;
492  pg_crc32c rdata_crc;
493  registered_buffer *prev_regbuf = NULL;
494  XLogRecData *rdt_datas_last;
495  XLogRecord *rechdr;
496  char *scratch = hdr_scratch;
497 
498  /*
499  * Note: this function can be called multiple times for the same record.
500  * All the modifications we do to the rdata chains below must handle that.
501  */
502 
503  /* The record begins with the fixed-size header */
504  rechdr = (XLogRecord *) scratch;
505  scratch += SizeOfXLogRecord;
506 
507  hdr_rdt.next = NULL;
508  rdt_datas_last = &hdr_rdt;
510 
511  /*
512  * Enforce consistency checks for this record if user is looking for it.
513  * Do this before at the beginning of this routine to give the possibility
514  * for callers of XLogInsert() to pass XLR_CHECK_CONSISTENCY directly for
515  * a record.
516  */
517  if (wal_consistency_checking[rmid])
518  info |= XLR_CHECK_CONSISTENCY;
519 
520  /*
521  * Make an rdata chain containing all the data portions of all block
522  * references. This includes the data for full-page images. Also append
523  * the headers for the block references in the scratch buffer.
524  */
525  *fpw_lsn = InvalidXLogRecPtr;
526  for (block_id = 0; block_id < max_registered_block_id; block_id++)
527  {
528  registered_buffer *regbuf = &registered_buffers[block_id];
529  bool needs_backup;
530  bool needs_data;
533  XLogRecordBlockCompressHeader cbimg = {0};
534  bool samerel;
535  bool is_compressed = false;
536  bool include_image;
537 
538  if (!regbuf->in_use)
539  continue;
540 
541  /* Determine if this block needs to be backed up */
542  if (regbuf->flags & REGBUF_FORCE_IMAGE)
543  needs_backup = true;
544  else if (regbuf->flags & REGBUF_NO_IMAGE)
545  needs_backup = false;
546  else if (!doPageWrites)
547  needs_backup = false;
548  else
549  {
550  /*
551  * We assume page LSN is first data on *every* page that can be
552  * passed to XLogInsert, whether it has the standard page layout
553  * or not.
554  */
555  XLogRecPtr page_lsn = PageGetLSN(regbuf->page);
556 
557  needs_backup = (page_lsn <= RedoRecPtr);
558  if (!needs_backup)
559  {
560  if (*fpw_lsn == InvalidXLogRecPtr || page_lsn < *fpw_lsn)
561  *fpw_lsn = page_lsn;
562  }
563  }
564 
565  /* Determine if the buffer data needs to included */
566  if (regbuf->rdata_len == 0)
567  needs_data = false;
568  else if ((regbuf->flags & REGBUF_KEEP_DATA) != 0)
569  needs_data = true;
570  else
571  needs_data = !needs_backup;
572 
573  bkpb.id = block_id;
574  bkpb.fork_flags = regbuf->forkno;
575  bkpb.data_length = 0;
576 
577  if ((regbuf->flags & REGBUF_WILL_INIT) == REGBUF_WILL_INIT)
579 
580  /*
581  * If needs_backup is true or WAL checking is enabled for current
582  * resource manager, log a full-page write for the current block.
583  */
584  include_image = needs_backup || (info & XLR_CHECK_CONSISTENCY) != 0;
585 
586  if (include_image)
587  {
588  Page page = regbuf->page;
589  uint16 compressed_len = 0;
590 
591  /*
592  * The page needs to be backed up, so calculate its hole length
593  * and offset.
594  */
595  if (regbuf->flags & REGBUF_STANDARD)
596  {
597  /* Assume we can omit data between pd_lower and pd_upper */
598  uint16 lower = ((PageHeader) page)->pd_lower;
599  uint16 upper = ((PageHeader) page)->pd_upper;
600 
601  if (lower >= SizeOfPageHeaderData &&
602  upper > lower &&
603  upper <= BLCKSZ)
604  {
605  bimg.hole_offset = lower;
606  cbimg.hole_length = upper - lower;
607  }
608  else
609  {
610  /* No "hole" to remove */
611  bimg.hole_offset = 0;
612  cbimg.hole_length = 0;
613  }
614  }
615  else
616  {
617  /* Not a standard page header, don't try to eliminate "hole" */
618  bimg.hole_offset = 0;
619  cbimg.hole_length = 0;
620  }
621 
622  /*
623  * Try to compress a block image if wal_compression is enabled
624  */
625  if (wal_compression)
626  {
627  is_compressed =
629  cbimg.hole_length,
630  regbuf->compressed_page,
631  &compressed_len);
632  }
633 
634  /*
635  * Fill in the remaining fields in the XLogRecordBlockHeader
636  * struct
637  */
639 
640  /* Report a full page image constructed for the WAL record */
641  *num_fpi += 1;
642 
643  /*
644  * Construct XLogRecData entries for the page content.
645  */
646  rdt_datas_last->next = &regbuf->bkp_rdatas[0];
647  rdt_datas_last = rdt_datas_last->next;
648 
649  bimg.bimg_info = (cbimg.hole_length == 0) ? 0 : BKPIMAGE_HAS_HOLE;
650 
651  /*
652  * If WAL consistency checking is enabled for the resource manager
653  * of this WAL record, a full-page image is included in the record
654  * for the block modified. During redo, the full-page is replayed
655  * only if BKPIMAGE_APPLY is set.
656  */
657  if (needs_backup)
658  bimg.bimg_info |= BKPIMAGE_APPLY;
659 
660  if (is_compressed)
661  {
662  bimg.length = compressed_len;
664 
665  rdt_datas_last->data = regbuf->compressed_page;
666  rdt_datas_last->len = compressed_len;
667  }
668  else
669  {
670  bimg.length = BLCKSZ - cbimg.hole_length;
671 
672  if (cbimg.hole_length == 0)
673  {
674  rdt_datas_last->data = page;
675  rdt_datas_last->len = BLCKSZ;
676  }
677  else
678  {
679  /* must skip the hole */
680  rdt_datas_last->data = page;
681  rdt_datas_last->len = bimg.hole_offset;
682 
683  rdt_datas_last->next = &regbuf->bkp_rdatas[1];
684  rdt_datas_last = rdt_datas_last->next;
685 
686  rdt_datas_last->data =
687  page + (bimg.hole_offset + cbimg.hole_length);
688  rdt_datas_last->len =
689  BLCKSZ - (bimg.hole_offset + cbimg.hole_length);
690  }
691  }
692 
693  total_len += bimg.length;
694  }
695 
696  if (needs_data)
697  {
698  /*
699  * Link the caller-supplied rdata chain for this buffer to the
700  * overall list.
701  */
703  bkpb.data_length = regbuf->rdata_len;
704  total_len += regbuf->rdata_len;
705 
706  rdt_datas_last->next = regbuf->rdata_head;
707  rdt_datas_last = regbuf->rdata_tail;
708  }
709 
710  if (prev_regbuf && RelFileNodeEquals(regbuf->rnode, prev_regbuf->rnode))
711  {
712  samerel = true;
714  }
715  else
716  samerel = false;
717  prev_regbuf = regbuf;
718 
719  /* Ok, copy the header to the scratch buffer */
720  memcpy(scratch, &bkpb, SizeOfXLogRecordBlockHeader);
721  scratch += SizeOfXLogRecordBlockHeader;
722  if (include_image)
723  {
724  memcpy(scratch, &bimg, SizeOfXLogRecordBlockImageHeader);
726  if (cbimg.hole_length != 0 && is_compressed)
727  {
728  memcpy(scratch, &cbimg,
731  }
732  }
733  if (!samerel)
734  {
735  memcpy(scratch, &regbuf->rnode, sizeof(RelFileNode));
736  scratch += sizeof(RelFileNode);
737  }
738  memcpy(scratch, &regbuf->block, sizeof(BlockNumber));
739  scratch += sizeof(BlockNumber);
740  }
741 
742  /* followed by the record's origin, if any */
745  {
746  *(scratch++) = (char) XLR_BLOCK_ID_ORIGIN;
747  memcpy(scratch, &replorigin_session_origin, sizeof(replorigin_session_origin));
748  scratch += sizeof(replorigin_session_origin);
749  }
750 
751  /* followed by main data, if any */
752  if (mainrdata_len > 0)
753  {
754  if (mainrdata_len > 255)
755  {
756  *(scratch++) = (char) XLR_BLOCK_ID_DATA_LONG;
757  memcpy(scratch, &mainrdata_len, sizeof(uint32));
758  scratch += sizeof(uint32);
759  }
760  else
761  {
762  *(scratch++) = (char) XLR_BLOCK_ID_DATA_SHORT;
763  *(scratch++) = (uint8) mainrdata_len;
764  }
765  rdt_datas_last->next = mainrdata_head;
766  rdt_datas_last = mainrdata_last;
767  total_len += mainrdata_len;
768  }
769  rdt_datas_last->next = NULL;
770 
771  hdr_rdt.len = (scratch - hdr_scratch);
772  total_len += hdr_rdt.len;
773 
774  /*
775  * Calculate CRC of the data
776  *
777  * Note that the record header isn't added into the CRC initially since we
778  * don't know the prev-link yet. Thus, the CRC will represent the CRC of
779  * the whole record in the order: rdata, then backup blocks, then record
780  * header.
781  */
782  INIT_CRC32C(rdata_crc);
784  for (rdt = hdr_rdt.next; rdt != NULL; rdt = rdt->next)
785  COMP_CRC32C(rdata_crc, rdt->data, rdt->len);
786 
787  /*
788  * Fill in the fields in the record header. Prev-link is filled in later,
789  * once we know where in the WAL the record will be inserted. The CRC does
790  * not include the record header yet.
791  */
793  rechdr->xl_tot_len = total_len;
794  rechdr->xl_info = info;
795  rechdr->xl_rmid = rmid;
796  rechdr->xl_prev = InvalidXLogRecPtr;
797  rechdr->xl_crc = rdata_crc;
798 
799  return &hdr_rdt;
800 }
static XLogRecData hdr_rdt
Definition: xloginsert.c:88
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define BKPIMAGE_HAS_HOLE
Definition: xlogrecord.h:146
static uint32 mainrdata_len
Definition: xloginsert.c:75
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:44
#define XLR_BLOCK_ID_DATA_LONG
Definition: xlogrecord.h:224
uint32 pg_crc32c
Definition: pg_crc32c.h:38
BlockNumber block
Definition: xloginsert.c:49
unsigned char uint8
Definition: c.h:372
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33
RmgrId xl_rmid
Definition: xlogrecord.h:47
#define XLOG_INCLUDE_ORIGIN
Definition: xlog.h:238
uint32 BlockNumber
Definition: block.h:31
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:75
XLogRecData * rdata_head
Definition: xloginsert.c:52
#define SizeOfPageHeaderData
Definition: bufpage.h:216
#define XLR_CHECK_CONSISTENCY
Definition: xlogrecord.h:80
RelFileNode rnode
Definition: xloginsert.c:47
unsigned short uint16
Definition: c.h:373
static bool doPageWrites
Definition: xlog.c:383
uint32 xl_tot_len
Definition: xlogrecord.h:43
#define SizeOfXLogRecordBlockImageHeader
Definition: xlogrecord.h:142
static XLogRecPtr RedoRecPtr
Definition: xlog.c:376
#define BKPIMAGE_APPLY
Definition: xlogrecord.h:148
struct RelFileNode RelFileNode
static XLogRecData * mainrdata_head
Definition: xloginsert.c:73
#define REGBUF_STANDARD
Definition: xloginsert.h:35
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:440
unsigned int uint32
Definition: c.h:374
#define BKPBLOCK_WILL_INIT
Definition: xlogrecord.h:182
XLogRecData * rdata_tail
Definition: xloginsert.c:54
static registered_buffer * registered_buffers
Definition: xloginsert.c:64
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define REGBUF_FORCE_IMAGE
Definition: xloginsert.h:31
#define BKPBLOCK_SAME_REL
Definition: xlogrecord.h:183
#define BKPIMAGE_IS_COMPRESSED
Definition: xlogrecord.h:147
#define REGBUF_KEEP_DATA
Definition: xloginsert.h:38
#define BKPBLOCK_HAS_IMAGE
Definition: xlogrecord.h:180
static XLogRecData * mainrdata_last
Definition: xloginsert.c:74
PageHeaderData * PageHeader
Definition: bufpage.h:166
uint64 XLogRecPtr
Definition: xlogdefs.h:21
RepOriginId replorigin_session_origin
Definition: origin.c:154
bool * wal_consistency_checking
Definition: xlog.c:101
uint8 xl_info
Definition: xlogrecord.h:46
#define REGBUF_NO_IMAGE
Definition: xloginsert.h:32
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:225
ForkNumber forkno
Definition: xloginsert.c:48
pg_crc32c xl_crc
Definition: xlogrecord.h:49
struct XLogRecData * next
#define InvalidRepOriginId
Definition: origin.h:33
#define PageGetLSN(page)
Definition: bufpage.h:366
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:223
static char * hdr_scratch
Definition: xloginsert.c:89
TransactionId xl_xid
Definition: xlogrecord.h:44
static int max_registered_block_id
Definition: xloginsert.c:66
char compressed_page[PGLZ_MAX_BLCKSZ]
Definition: xloginsert.c:61
bool wal_compression
Definition: xlog.c:99
static bool XLogCompressBackupBlock(char *page, uint16 hole_offset, uint16 hole_length, char *dest, uint16 *dlen)
Definition: xloginsert.c:810
static uint8 curinsert_flags
Definition: xloginsert.c:78
XLogRecData bkp_rdatas[2]
Definition: xloginsert.c:57
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
#define SizeOfXLogRecordBlockCompressHeader
Definition: xlogrecord.h:160
Pointer Page
Definition: bufpage.h:78
#define RelFileNodeEquals(node1, node2)
Definition: relfilenode.h:88
#define BKPBLOCK_HAS_DATA
Definition: xlogrecord.h:181
#define SizeOfXLogRecordBlockHeader
Definition: xlogrecord.h:104

◆ XLogRegisterBlock()

void XLogRegisterBlock ( uint8  block_id,
RelFileNode rnode,
ForkNumber  forknum,
BlockNumber  blknum,
Page  page,
uint8  flags 
)

Definition at line 267 of file xloginsert.c.

References Assert, begininsert_called, registered_buffer::block, elog, ERROR, registered_buffer::flags, registered_buffer::forkno, i, registered_buffer::in_use, max_registered_block_id, max_registered_buffers, registered_buffer::page, registered_buffer::rdata_head, registered_buffer::rdata_len, registered_buffer::rdata_tail, REGBUF_FORCE_IMAGE, RelFileNodeEquals, and registered_buffer::rnode.

Referenced by log_newpage(), and XLogSaveBufferForHint().

269 {
270  registered_buffer *regbuf;
271 
272  /* This is currently only used to WAL-log a full-page image of a page */
273  Assert(flags & REGBUF_FORCE_IMAGE);
275 
276  if (block_id >= max_registered_block_id)
277  max_registered_block_id = block_id + 1;
278 
279  if (block_id >= max_registered_buffers)
280  elog(ERROR, "too many registered buffers");
281 
282  regbuf = &registered_buffers[block_id];
283 
284  regbuf->rnode = *rnode;
285  regbuf->forkno = forknum;
286  regbuf->block = blknum;
287  regbuf->page = page;
288  regbuf->flags = flags;
289  regbuf->rdata_tail = (XLogRecData *) &regbuf->rdata_head;
290  regbuf->rdata_len = 0;
291 
292  /*
293  * Check that this page hasn't already been registered with some other
294  * block_id.
295  */
296 #ifdef USE_ASSERT_CHECKING
297  {
298  int i;
299 
300  for (i = 0; i < max_registered_block_id; i++)
301  {
302  registered_buffer *regbuf_old = &registered_buffers[i];
303 
304  if (i == block_id || !regbuf_old->in_use)
305  continue;
306 
307  Assert(!RelFileNodeEquals(regbuf_old->rnode, regbuf->rnode) ||
308  regbuf_old->forkno != regbuf->forkno ||
309  regbuf_old->block != regbuf->block);
310  }
311  }
312 #endif
313 
314  regbuf->in_use = true;
315 }
BlockNumber block
Definition: xloginsert.c:49
static bool begininsert_called
Definition: xloginsert.c:105
XLogRecData * rdata_head
Definition: xloginsert.c:52
RelFileNode rnode
Definition: xloginsert.c:47
#define ERROR
Definition: elog.h:43
static int max_registered_buffers
Definition: xloginsert.c:65
XLogRecData * rdata_tail
Definition: xloginsert.c:54
static registered_buffer * registered_buffers
Definition: xloginsert.c:64
#define REGBUF_FORCE_IMAGE
Definition: xloginsert.h:31
#define Assert(condition)
Definition: c.h:745
ForkNumber forkno
Definition: xloginsert.c:48
static int max_registered_block_id
Definition: xloginsert.c:66
#define elog(elevel,...)
Definition: elog.h:214
int i
#define RelFileNodeEquals(node1, node2)
Definition: relfilenode.h:88

◆ XLogRegisterBufData()

void XLogRegisterBufData ( uint8  block_id,
char *  data,
int  len 
)

Definition at line 362 of file xloginsert.c.

References Assert, begininsert_called, XLogRecData::data, elog, ERROR, registered_buffer::in_use, XLogRecData::len, max_rdatas, XLogRecData::next, num_rdatas, registered_buffer::rdata_len, and registered_buffer::rdata_tail.

Referenced by _bt_dedup_one_page(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_getroot(), _bt_insertonpg(), _bt_newroot(), _bt_split(), _bt_unlink_halfdead_page(), _bt_update_meta_cleanup_info(), _hash_addovflpage(), _hash_doinsert(), _hash_expandtable(), _hash_freeovflpage(), _hash_squeezebucket(), brin_doinsert(), brin_doupdate(), dataExecPlaceToPageInternal(), dataExecPlaceToPageLeaf(), entryExecPlaceToPage(), GenericXLogFinish(), ginHeapTupleFastInsert(), ginVacuumPostingTreeLeaf(), gistXLogSplit(), gistXLogUpdate(), hashbucketcleanup(), heap_inplace_update(), heap_insert(), heap_multi_insert(), log_heap_clean(), log_heap_freeze(), log_heap_update(), and writeListPage().

363 {
364  registered_buffer *regbuf;
365  XLogRecData *rdata;
366 
368 
369  /* find the registered buffer struct */
370  regbuf = &registered_buffers[block_id];
371  if (!regbuf->in_use)
372  elog(ERROR, "no block with id %d registered with WAL insertion",
373  block_id);
374 
375  if (num_rdatas >= max_rdatas)
376  elog(ERROR, "too much WAL data");
377  rdata = &rdatas[num_rdatas++];
378 
379  rdata->data = data;
380  rdata->len = len;
381 
382  regbuf->rdata_tail->next = rdata;
383  regbuf->rdata_tail = rdata;
384  regbuf->rdata_len += len;
385 }
static bool begininsert_called
Definition: xloginsert.c:105
#define ERROR
Definition: elog.h:43
XLogRecData * rdata_tail
Definition: xloginsert.c:54
static int max_rdatas
Definition: xloginsert.c:103
static registered_buffer * registered_buffers
Definition: xloginsert.c:64
static int num_rdatas
Definition: xloginsert.c:102
#define Assert(condition)
Definition: c.h:745
struct XLogRecData * next
#define elog(elevel,...)
Definition: elog.h:214
static XLogRecData * rdatas
Definition: xloginsert.c:101

◆ XLogRegisterBuffer()

void XLogRegisterBuffer ( uint8  block_id,
Buffer  buffer,
uint8  flags 
)

Definition at line 214 of file xloginsert.c.

References Assert, begininsert_called, registered_buffer::block, BufferGetPage, BufferGetTag(), elog, ERROR, registered_buffer::flags, registered_buffer::forkno, i, registered_buffer::in_use, max_registered_block_id, max_registered_buffers, registered_buffer::page, registered_buffer::rdata_head, registered_buffer::rdata_len, registered_buffer::rdata_tail, REGBUF_FORCE_IMAGE, REGBUF_NO_IMAGE, RelFileNodeEquals, and registered_buffer::rnode.

Referenced by _bt_dedup_one_page(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_getroot(), _bt_insertonpg(), _bt_mark_page_halfdead(), _bt_newroot(), _bt_split(), _bt_unlink_halfdead_page(), _bt_update_meta_cleanup_info(), _hash_addovflpage(), _hash_doinsert(), _hash_expandtable(), _hash_freeovflpage(), _hash_init(), _hash_splitbucket(), _hash_squeezebucket(), _hash_vacuum_one_page(), addLeafTuple(), brin_doinsert(), brin_doupdate(), brinbuild(), brinRevmapDesummarizeRange(), createPostingTree(), do_setval(), doPickSplit(), fill_seq_with_data(), GenericXLogFinish(), ginDeletePage(), ginHeapTupleFastInsert(), ginPlaceToPage(), ginUpdateStats(), ginVacuumPostingTreeLeaf(), gistXLogDelete(), gistXLogPageDelete(), gistXLogSplit(), gistXLogUpdate(), hashbucketcleanup(), hashbulkdelete(), heap_abort_speculative(), heap_delete(), heap_finish_speculative(), heap_inplace_update(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_update(), log_heap_clean(), log_heap_freeze(), log_heap_update(), log_heap_visible(), log_newpage_range(), log_split_page(), moveLeafs(), nextval_internal(), revmap_physical_extend(), shiftList(), spgAddNodeAction(), spgSplitNodeAction(), vacuumLeafPage(), vacuumLeafRoot(), vacuumRedirectAndPlaceholder(), writeListPage(), and xlogVacuumPage().

215 {
216  registered_buffer *regbuf;
217 
218  /* NO_IMAGE doesn't make sense with FORCE_IMAGE */
219  Assert(!((flags & REGBUF_FORCE_IMAGE) && (flags & (REGBUF_NO_IMAGE))));
221 
222  if (block_id >= max_registered_block_id)
223  {
224  if (block_id >= max_registered_buffers)
225  elog(ERROR, "too many registered buffers");
226  max_registered_block_id = block_id + 1;
227  }
228 
229  regbuf = &registered_buffers[block_id];
230 
231  BufferGetTag(buffer, &regbuf->rnode, &regbuf->forkno, &regbuf->block);
232  regbuf->page = BufferGetPage(buffer);
233  regbuf->flags = flags;
234  regbuf->rdata_tail = (XLogRecData *) &regbuf->rdata_head;
235  regbuf->rdata_len = 0;
236 
237  /*
238  * Check that this page hasn't already been registered with some other
239  * block_id.
240  */
241 #ifdef USE_ASSERT_CHECKING
242  {
243  int i;
244 
245  for (i = 0; i < max_registered_block_id; i++)
246  {
247  registered_buffer *regbuf_old = &registered_buffers[i];
248 
249  if (i == block_id || !regbuf_old->in_use)
250  continue;
251 
252  Assert(!RelFileNodeEquals(regbuf_old->rnode, regbuf->rnode) ||
253  regbuf_old->forkno != regbuf->forkno ||
254  regbuf_old->block != regbuf->block);
255  }
256  }
257 #endif
258 
259  regbuf->in_use = true;
260 }
BlockNumber block
Definition: xloginsert.c:49
static bool begininsert_called
Definition: xloginsert.c:105
XLogRecData * rdata_head
Definition: xloginsert.c:52
RelFileNode rnode
Definition: xloginsert.c:47
#define ERROR
Definition: elog.h:43
static int max_registered_buffers
Definition: xloginsert.c:65
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
XLogRecData * rdata_tail
Definition: xloginsert.c:54
static registered_buffer * registered_buffers
Definition: xloginsert.c:64
#define REGBUF_FORCE_IMAGE
Definition: xloginsert.h:31
#define Assert(condition)
Definition: c.h:745
#define REGBUF_NO_IMAGE
Definition: xloginsert.h:32
ForkNumber forkno
Definition: xloginsert.c:48
static int max_registered_block_id
Definition: xloginsert.c:66
#define elog(elevel,...)
Definition: elog.h:214
int i
void BufferGetTag(Buffer buffer, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:2654
#define RelFileNodeEquals(node1, node2)
Definition: relfilenode.h:88

◆ XLogRegisterData()

void XLogRegisterData ( char *  data,
int  len 
)

Definition at line 324 of file xloginsert.c.

References Assert, begininsert_called, XLogRecData::data, elog, ERROR, XLogRecData::len, mainrdata_len, max_rdatas, XLogRecData::next, and num_rdatas.

Referenced by _bt_dedup_one_page(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_getroot(), _bt_insertonpg(), _bt_log_reuse_page(), _bt_mark_page_halfdead(), _bt_newroot(), _bt_split(), _bt_unlink_halfdead_page(), _hash_addovflpage(), _hash_doinsert(), _hash_expandtable(), _hash_freeovflpage(), _hash_init(), _hash_splitbucket(), _hash_squeezebucket(), _hash_vacuum_one_page(), addLeafTuple(), AssignTransactionId(), brin_doinsert(), brin_doupdate(), brinbuild(), brinRevmapDesummarizeRange(), CreateCheckPoint(), createdb(), CreateEndOfRecoveryRecord(), createPostingTree(), CreateTableSpace(), do_pg_stop_backup(), do_setval(), doPickSplit(), DropTableSpace(), EndPrepare(), ExecuteTruncateGuts(), fill_seq_with_data(), ginDeletePage(), ginHeapTupleFastInsert(), ginPlaceToPage(), ginUpdateStats(), gistXLogAssignLSN(), gistXLogDelete(), gistXLogPageDelete(), gistXLogPageReuse(), gistXLogSplit(), gistXLogUpdate(), hashbucketcleanup(), hashbulkdelete(), heap_abort_speculative(), heap_delete(), heap_finish_speculative(), heap_inplace_update(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_update(), log_heap_clean(), log_heap_cleanup_info(), log_heap_freeze(), log_heap_new_cid(), log_heap_update(), log_heap_visible(), log_smgrcreate(), LogAccessExclusiveLocks(), LogCurrentRunningXacts(), logical_heap_rewrite_flush_mappings(), LogLogicalMessage(), LogStandbyInvalidations(), movedb(), moveLeafs(), MultiXactIdCreateFromMembers(), nextval_internal(), pg_truncate_visibility_map(), RelationTruncate(), remove_dbtablespaces(), replorigin_advance(), replorigin_drop(), revmap_physical_extend(), shiftList(), spgAddNodeAction(), spgSplitNodeAction(), UpdateFullPageWrites(), vacuumLeafPage(), vacuumLeafRoot(), vacuumRedirectAndPlaceholder(), write_relmap_file(), writeListPage(), WriteMTruncateXlogRec(), WriteMZeroPageXlogRec(), WriteSetTimestampXlogRec(), WriteTruncateXlogRec(), WriteZeroPageXlogRec(), XactLogAbortRecord(), XactLogCommitRecord(), XLogPutNextOid(), XLogReportParameters(), and XLogRestorePoint().

325 {
326  XLogRecData *rdata;
327 
329 
330  if (num_rdatas >= max_rdatas)
331  elog(ERROR, "too much WAL data");
332  rdata = &rdatas[num_rdatas++];
333 
334  rdata->data = data;
335  rdata->len = len;
336 
337  /*
338  * we use the mainrdata_last pointer to track the end of the chain, so no
339  * need to clear 'next' here.
340  */
341 
342  mainrdata_last->next = rdata;
343  mainrdata_last = rdata;
344 
345  mainrdata_len += len;
346 }
static uint32 mainrdata_len
Definition: xloginsert.c:75
static bool begininsert_called
Definition: xloginsert.c:105
#define ERROR
Definition: elog.h:43
static int max_rdatas
Definition: xloginsert.c:103
static XLogRecData * mainrdata_last
Definition: xloginsert.c:74
static int num_rdatas
Definition: xloginsert.c:102
#define Assert(condition)
Definition: c.h:745
struct XLogRecData * next
#define elog(elevel,...)
Definition: elog.h:214
static XLogRecData * rdatas
Definition: xloginsert.c:101

◆ XLogResetInsertion()

void XLogResetInsertion ( void  )

Definition at line 194 of file xloginsert.c.

References begininsert_called, curinsert_flags, i, mainrdata_len, max_registered_block_id, and num_rdatas.

Referenced by AbortSubTransaction(), AbortTransaction(), and XLogInsert().

195 {
196  int i;
197 
198  for (i = 0; i < max_registered_block_id; i++)
199  registered_buffers[i].in_use = false;
200 
201  num_rdatas = 0;
202  max_registered_block_id = 0;
203  mainrdata_len = 0;
205  curinsert_flags = 0;
206  begininsert_called = false;
207 }
static uint32 mainrdata_len
Definition: xloginsert.c:75
static bool begininsert_called
Definition: xloginsert.c:105
static XLogRecData * mainrdata_head
Definition: xloginsert.c:73
static registered_buffer * registered_buffers
Definition: xloginsert.c:64
static XLogRecData * mainrdata_last
Definition: xloginsert.c:74
static int num_rdatas
Definition: xloginsert.c:102
static int max_registered_block_id
Definition: xloginsert.c:66
int i
static uint8 curinsert_flags
Definition: xloginsert.c:78

◆ XLogSaveBufferForHint()

XLogRecPtr XLogSaveBufferForHint ( Buffer  buffer,
bool  buffer_std 
)

Definition at line 898 of file xloginsert.c.

References Assert, BufferGetBlock, BufferGetLSNAtomic(), BufferGetPage, BufferGetTag(), PGAlignedBlock::data, PGPROC::delayChkpt, GetRedoRecPtr(), InvalidXLogRecPtr, lower(), MyProc, RedoRecPtr, REGBUF_FORCE_IMAGE, REGBUF_STANDARD, upper(), XLOG_FPI_FOR_HINT, XLogBeginInsert(), XLogInsert(), and XLogRegisterBlock().

Referenced by MarkBufferDirtyHint().

899 {
900  XLogRecPtr recptr = InvalidXLogRecPtr;
901  XLogRecPtr lsn;
903 
904  /*
905  * Ensure no checkpoint can change our view of RedoRecPtr.
906  */
908 
909  /*
910  * Update RedoRecPtr so that we can make the right decision
911  */
912  RedoRecPtr = GetRedoRecPtr();
913 
914  /*
915  * We assume page LSN is first data on *every* page that can be passed to
916  * XLogInsert, whether it has the standard page layout or not. Since we're
917  * only holding a share-lock on the page, we must take the buffer header
918  * lock when we look at the LSN.
919  */
920  lsn = BufferGetLSNAtomic(buffer);
921 
922  if (lsn <= RedoRecPtr)
923  {
924  int flags;
925  PGAlignedBlock copied_buffer;
926  char *origdata = (char *) BufferGetBlock(buffer);
927  RelFileNode rnode;
928  ForkNumber forkno;
929  BlockNumber blkno;
930 
931  /*
932  * Copy buffer so we don't have to worry about concurrent hint bit or
933  * lsn updates. We assume pd_lower/upper cannot be changed without an
934  * exclusive lock, so the contents bkp are not racy.
935  */
936  if (buffer_std)
937  {
938  /* Assume we can omit data between pd_lower and pd_upper */
939  Page page = BufferGetPage(buffer);
940  uint16 lower = ((PageHeader) page)->pd_lower;
941  uint16 upper = ((PageHeader) page)->pd_upper;
942 
943  memcpy(copied_buffer.data, origdata, lower);
944  memcpy(copied_buffer.data + upper, origdata + upper, BLCKSZ - upper);
945  }
946  else
947  memcpy(copied_buffer.data, origdata, BLCKSZ);
948 
949  XLogBeginInsert();
950 
951  flags = REGBUF_FORCE_IMAGE;
952  if (buffer_std)
953  flags |= REGBUF_STANDARD;
954 
955  BufferGetTag(buffer, &rnode, &forkno, &blkno);
956  XLogRegisterBlock(0, &rnode, forkno, blkno, copied_buffer.data, flags);
957 
958  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI_FOR_HINT);
959  }
960 
961  return recptr;
962 }
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:44
PGPROC * MyProc
Definition: proc.c:67
uint32 BlockNumber
Definition: block.h:31
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:75
char data[BLCKSZ]
Definition: c.h:1111
void XLogRegisterBlock(uint8 block_id, RelFileNode *rnode, ForkNumber forknum, BlockNumber blknum, Page page, uint8 flags)
Definition: xloginsert.c:267
unsigned short uint16
Definition: c.h:373
bool delayChkpt
Definition: proc.h:152
static XLogRecPtr RedoRecPtr
Definition: xlog.c:376
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:2896
#define REGBUF_STANDARD
Definition: xloginsert.h:35
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
ForkNumber
Definition: relpath.h:40
#define REGBUF_FORCE_IMAGE
Definition: xloginsert.h:31
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:416
PageHeaderData * PageHeader
Definition: bufpage.h:166
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:745
#define XLOG_FPI_FOR_HINT
Definition: pg_control.h:77
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8364
void BufferGetTag(Buffer buffer, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:2654
void XLogBeginInsert(void)
Definition: xloginsert.c:121
#define BufferGetBlock(buffer)
Definition: bufmgr.h:136
Pointer Page
Definition: bufpage.h:78

◆ XLogSetRecordFlags()

void XLogSetRecordFlags ( uint8  flags)

Variable Documentation

◆ begininsert_called

◆ curinsert_flags

uint8 curinsert_flags = 0
static

◆ hdr_rdt

XLogRecData hdr_rdt
static

Definition at line 88 of file xloginsert.c.

Referenced by XLogRecordAssemble().

◆ hdr_scratch

char* hdr_scratch = NULL
static

Definition at line 89 of file xloginsert.c.

Referenced by InitXLogInsert(), and XLogRecordAssemble().

◆ mainrdata_head

XLogRecData* mainrdata_head
static

Definition at line 73 of file xloginsert.c.

Referenced by XLogRecordAssemble().

◆ mainrdata_last

XLogRecData* mainrdata_last = (XLogRecData *) &mainrdata_head
static

Definition at line 74 of file xloginsert.c.

Referenced by XLogRecordAssemble().

◆ mainrdata_len

uint32 mainrdata_len
static

◆ max_rdatas

int max_rdatas
static

◆ max_registered_block_id

int max_registered_block_id = 0
static

◆ max_registered_buffers

int max_registered_buffers
static

◆ num_rdatas

int num_rdatas
static

Definition at line 102 of file xloginsert.c.

Referenced by XLogRegisterBufData(), XLogRegisterData(), and XLogResetInsertion().

◆ rdatas

XLogRecData* rdatas
static

Definition at line 101 of file xloginsert.c.

◆ registered_buffers

registered_buffer* registered_buffers
static

Definition at line 64 of file xloginsert.c.

◆ xloginsert_cxt

MemoryContext xloginsert_cxt
static

Definition at line 108 of file xloginsert.c.