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 LZ4_MAX_BLCKSZ   0
 
#define ZSTD_MAX_BLCKSZ   0
 
#define PGLZ_MAX_BLCKSZ   PGLZ_MAX_OUTPUT(BLCKSZ)
 
#define COMPRESS_BUFSIZE   Max(Max(PGLZ_MAX_BLCKSZ, LZ4_MAX_BLCKSZ), ZSTD_MAX_BLCKSZ)
 
#define SizeOfXlogOrigin   (sizeof(RepOriginId) + sizeof(char))
 
#define SizeOfXLogTransactionId   (sizeof(TransactionId) + sizeof(char))
 
#define HEADER_SCRATCH_SIZE
 

Functions

static XLogRecDataXLogRecordAssemble (RmgrId rmid, uint8 info, XLogRecPtr RedoRecPtr, bool doPageWrites, XLogRecPtr *fpw_lsn, int *num_fpi, bool *topxid_included)
 
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, RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum, Page page, uint8 flags)
 
void XLogRegisterData (char *data, uint32 len)
 
void XLogRegisterBufData (uint8 block_id, char *data, uint32 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 (RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blkno, Page page, bool page_std)
 
void log_newpages (RelFileLocator *rlocator, ForkNumber forknum, int num_pages, BlockNumber *blknos, Page *pages, 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

◆ COMPRESS_BUFSIZE

#define COMPRESS_BUFSIZE   Max(Max(PGLZ_MAX_BLCKSZ, LZ4_MAX_BLCKSZ), ZSTD_MAX_BLCKSZ)

Definition at line 63 of file xloginsert.c.

◆ HEADER_SCRATCH_SIZE

#define HEADER_SCRATCH_SIZE
Value:
MaxSizeOfXLogRecordBlockHeader * (XLR_MAX_BLOCK_ID + 1) + \
#define SizeOfXlogOrigin
Definition: xloginsert.c:117
#define SizeOfXLogTransactionId
Definition: xloginsert.c:118
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:228
#define SizeOfXLogRecordDataHeaderLong
Definition: xlogrecord.h:214
#define SizeOfXLogRecord
Definition: xlogrecord.h:55

Definition at line 120 of file xloginsert.c.

◆ LZ4_MAX_BLCKSZ

#define LZ4_MAX_BLCKSZ   0

Definition at line 51 of file xloginsert.c.

◆ PGLZ_MAX_BLCKSZ

#define PGLZ_MAX_BLCKSZ   PGLZ_MAX_OUTPUT(BLCKSZ)

Definition at line 60 of file xloginsert.c.

◆ SizeOfXlogOrigin

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

Definition at line 117 of file xloginsert.c.

◆ SizeOfXLogTransactionId

#define SizeOfXLogTransactionId   (sizeof(TransactionId) + sizeof(char))

Definition at line 118 of file xloginsert.c.

◆ ZSTD_MAX_BLCKSZ

#define ZSTD_MAX_BLCKSZ   0

Definition at line 57 of file xloginsert.c.

Function Documentation

◆ InitXLogInsert()

void InitXLogInsert ( void  )

Definition at line 1298 of file xloginsert.c.

1299 {
1300  /* Initialize the working areas */
1301  if (xloginsert_cxt == NULL)
1302  {
1304  "WAL record construction",
1306  }
1307 
1308  if (registered_buffers == NULL)
1309  {
1312  sizeof(registered_buffer) * (XLR_NORMAL_MAX_BLOCK_ID + 1));
1314  }
1315  if (rdatas == NULL)
1316  {
1318  sizeof(XLogRecData) * XLR_NORMAL_RDATAS);
1320  }
1321 
1322  /*
1323  * Allocate a buffer to hold the header information for a WAL record.
1324  */
1325  if (hdr_scratch == NULL)
1328 }
MemoryContext TopMemoryContext
Definition: mcxt.c:130
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1037
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:994
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
static int max_registered_buffers
Definition: xloginsert.c:91
static MemoryContext xloginsert_cxt
Definition: xloginsert.c:136
static char * hdr_scratch
Definition: xloginsert.c:115
static XLogRecData * rdatas
Definition: xloginsert.c:129
static registered_buffer * registered_buffers
Definition: xloginsert.c:90
static int max_rdatas
Definition: xloginsert.c:131
#define HEADER_SCRATCH_SIZE
Definition: xloginsert.c:120
#define XLR_NORMAL_MAX_BLOCK_ID
Definition: xloginsert.h:27
#define XLR_NORMAL_RDATAS
Definition: xloginsert.h:28

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

Referenced by BaseInit().

◆ log_newpage()

XLogRecPtr log_newpage ( RelFileLocator rlocator,
ForkNumber  forknum,
BlockNumber  blkno,
Page  page,
bool  page_std 
)

Definition at line 1097 of file xloginsert.c.

1099 {
1100  int flags;
1101  XLogRecPtr recptr;
1102 
1103  flags = REGBUF_FORCE_IMAGE;
1104  if (page_std)
1105  flags |= REGBUF_STANDARD;
1106 
1107  XLogBeginInsert();
1108  XLogRegisterBlock(0, rlocator, forknum, blkno, page, flags);
1109  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
1110 
1111  /*
1112  * The page may be uninitialized. If so, we can't set the LSN because that
1113  * would corrupt the page.
1114  */
1115  if (!PageIsNew(page))
1116  {
1117  PageSetLSN(page, recptr);
1118  }
1119 
1120  return recptr;
1121 }
static bool PageIsNew(Page page)
Definition: bufpage.h:230
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:388
#define XLOG_FPI
Definition: pg_control.h:78
uint64 XLogRecPtr
Definition: xlogdefs.h:21
void XLogRegisterBlock(uint8 block_id, RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum, Page page, uint8 flags)
Definition: xloginsert.c:296
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:451
void XLogBeginInsert(void)
Definition: xloginsert.c:150
#define REGBUF_STANDARD
Definition: xloginsert.h:34
#define REGBUF_FORCE_IMAGE
Definition: xloginsert.h:31

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(), gist_indexsortbuild(), log_newpage_buffer(), raw_heap_insert(), RelationCopyStorage(), and spgbuildempty().

◆ log_newpage_buffer()

XLogRecPtr log_newpage_buffer ( Buffer  buffer,
bool  page_std 
)

Definition at line 1191 of file xloginsert.c.

1192 {
1193  Page page = BufferGetPage(buffer);
1194  RelFileLocator rlocator;
1195  ForkNumber forknum;
1196  BlockNumber blkno;
1197 
1198  /* Shared buffers should be modified in a critical section. */
1199  Assert(CritSectionCount > 0);
1200 
1201  BufferGetTag(buffer, &rlocator, &forknum, &blkno);
1202 
1203  return log_newpage(&rlocator, forknum, blkno, page, page_std);
1204 }
uint32 BlockNumber
Definition: block.h:31
void BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:2784
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:280
Pointer Page
Definition: bufpage.h:78
volatile uint32 CritSectionCount
Definition: globals.c:42
Assert(fmt[strlen(fmt) - 1] !='\n')
ForkNumber
Definition: relpath.h:48
XLogRecPtr log_newpage(RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blkno, Page page, bool page_std)
Definition: xloginsert.c:1097

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

Referenced by brin_initialize_empty_new_buffer(), brinbuildempty(), FreeSpaceMapPrepareTruncateRel(), ginbuildempty(), gistbuildempty(), heap_force_common(), lazy_scan_new_or_empty(), RelationCopyStorageUsingBuffer(), and visibilitymap_prepare_truncate().

◆ log_newpage_range()

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

Definition at line 1224 of file xloginsert.c.

1227 {
1228  int flags;
1229  BlockNumber blkno;
1230 
1231  flags = REGBUF_FORCE_IMAGE;
1232  if (page_std)
1233  flags |= REGBUF_STANDARD;
1234 
1235  /*
1236  * Iterate over all the pages in the range. They are collected into
1237  * batches of XLR_MAX_BLOCK_ID pages, and a single WAL-record is written
1238  * for each batch.
1239  */
1241 
1242  blkno = startblk;
1243  while (blkno < endblk)
1244  {
1245  Buffer bufpack[XLR_MAX_BLOCK_ID];
1246  XLogRecPtr recptr;
1247  int nbufs;
1248  int i;
1249 
1251 
1252  /* Collect a batch of blocks. */
1253  nbufs = 0;
1254  while (nbufs < XLR_MAX_BLOCK_ID && blkno < endblk)
1255  {
1256  Buffer buf = ReadBufferExtended(rel, forknum, blkno,
1257  RBM_NORMAL, NULL);
1258 
1260 
1261  /*
1262  * Completely empty pages are not WAL-logged. Writing a WAL record
1263  * would change the LSN, and we don't want that. We want the page
1264  * to stay empty.
1265  */
1266  if (!PageIsNew(BufferGetPage(buf)))
1267  bufpack[nbufs++] = buf;
1268  else
1270  blkno++;
1271  }
1272 
1273  /* Write WAL record for this batch. */
1274  XLogBeginInsert();
1275 
1277  for (i = 0; i < nbufs; i++)
1278  {
1279  XLogRegisterBuffer(i, bufpack[i], flags);
1280  MarkBufferDirty(bufpack[i]);
1281  }
1282 
1283  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
1284 
1285  for (i = 0; i < nbufs; i++)
1286  {
1287  PageSetLSN(BufferGetPage(bufpack[i]), recptr);
1288  UnlockReleaseBuffer(bufpack[i]);
1289  }
1290  END_CRIT_SECTION();
1291  }
1292 }
int Buffer
Definition: buf.h:23
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3954
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1583
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4172
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:759
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:107
@ RBM_NORMAL
Definition: bufmgr.h:39
int i
Definition: isn.c:73
#define START_CRIT_SECTION()
Definition: miscadmin.h:148
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
#define END_CRIT_SECTION()
Definition: miscadmin.h:150
static char * buf
Definition: pg_test_fsync.c:67
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:243
void XLogEnsureRecordSpace(int max_block_id, int ndatas)
Definition: xloginsert.c:176

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().

◆ log_newpages()

void log_newpages ( RelFileLocator rlocator,
ForkNumber  forknum,
int  num_pages,
BlockNumber blknos,
Page pages,
bool  page_std 
)

Definition at line 1129 of file xloginsert.c.

1131 {
1132  int flags;
1133  XLogRecPtr recptr;
1134  int i;
1135  int j;
1136 
1137  flags = REGBUF_FORCE_IMAGE;
1138  if (page_std)
1139  flags |= REGBUF_STANDARD;
1140 
1141  /*
1142  * Iterate over all the pages. They are collected into batches of
1143  * XLR_MAX_BLOCK_ID pages, and a single WAL-record is written for each
1144  * batch.
1145  */
1147 
1148  i = 0;
1149  while (i < num_pages)
1150  {
1151  int batch_start = i;
1152  int nbatch;
1153 
1154  XLogBeginInsert();
1155 
1156  nbatch = 0;
1157  while (nbatch < XLR_MAX_BLOCK_ID && i < num_pages)
1158  {
1159  XLogRegisterBlock(nbatch, rlocator, forknum, blknos[i], pages[i], flags);
1160  i++;
1161  nbatch++;
1162  }
1163 
1164  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
1165 
1166  for (j = batch_start; j < i; j++)
1167  {
1168  /*
1169  * The page may be uninitialized. If so, we can't set the LSN
1170  * because that would corrupt the page.
1171  */
1172  if (!PageIsNew(pages[j]))
1173  {
1174  PageSetLSN(pages[j], recptr);
1175  }
1176  }
1177  }
1178 }
int j
Definition: isn.c:74

References i, j, PageIsNew(), PageSetLSN(), REGBUF_FORCE_IMAGE, REGBUF_STANDARD, XLOG_FPI, XLogBeginInsert(), XLogEnsureRecordSpace(), XLogInsert(), XLogRegisterBlock(), and XLR_MAX_BLOCK_ID.

Referenced by gist_indexsortbuild_flush_ready_pages().

◆ XLogBeginInsert()

void XLogBeginInsert ( void  )

Definition at line 150 of file xloginsert.c.

151 {
154  Assert(mainrdata_len == 0);
155 
156  /* cross-check on whether we should be here or not */
157  if (!XLogInsertAllowed())
158  elog(ERROR, "cannot make new WAL entries during recovery");
159 
160  if (begininsert_called)
161  elog(ERROR, "XLogBeginInsert was already called");
162 
163  begininsert_called = true;
164 }
#define ERROR
Definition: elog.h:35
bool XLogInsertAllowed(void)
Definition: xlog.c:5967
static XLogRecData * mainrdata_head
Definition: xloginsert.c:99
static bool begininsert_called
Definition: xloginsert.c:133
static int max_registered_block_id
Definition: xloginsert.c:92
static uint32 mainrdata_len
Definition: xloginsert.c:101
static XLogRecData * mainrdata_last
Definition: xloginsert.c:100

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

Referenced by _bt_dedup_pass(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_getroot(), _bt_insertonpg(), _bt_log_reuse_page(), _bt_mark_page_halfdead(), _bt_newroot(), _bt_set_cleanup_info(), _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(), CreateDatabaseUsingFileCopy(), CreateDirAndVersionFile(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), createPostingTree(), CreateTableSpace(), do_pg_backup_stop(), do_setval(), doPickSplit(), DropTableSpace(), EndPrepare(), ExecuteTruncateGuts(), fill_seq_fork_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_freeze_execute_prepared(), heap_inplace_update(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_page_prune(), heap_update(), lazy_vacuum_heap_page(), log_heap_new_cid(), log_heap_update(), log_heap_visible(), log_newpage(), log_newpage_range(), log_newpages(), log_smgrcreate(), log_split_page(), LogAccessExclusiveLocks(), LogCurrentRunningXacts(), logical_heap_rewrite_flush_mappings(), LogLogicalInvalidations(), LogLogicalMessage(), LogStandbyInvalidations(), movedb(), moveLeafs(), MultiXactIdCreateFromMembers(), nextval_internal(), pg_truncate_visibility_map(), RelationTruncate(), remove_dbtablespaces(), replorigin_advance(), replorigin_drop_guts(), RequestXLogSwitch(), revmap_physical_extend(), shiftList(), spgAddNodeAction(), spgSplitNodeAction(), test_custom_rmgrs_insert_wal_record(), UpdateFullPageWrites(), vacuumLeafPage(), vacuumLeafRoot(), vacuumRedirectAndPlaceholder(), write_relmap_file(), writeListPage(), WriteMTruncateXlogRec(), WriteMZeroPageXlogRec(), WriteTruncateXlogRec(), WriteZeroPageXlogRec(), XactLogAbortRecord(), XactLogCommitRecord(), XLogPutNextOid(), XLogReportParameters(), XLogRestorePoint(), XLogSaveBufferForHint(), and xlogVacuumPage().

◆ XLogCheckBufferNeedsBackup()

bool XLogCheckBufferNeedsBackup ( Buffer  buffer)

Definition at line 981 of file xloginsert.c.

982 {
984  bool doPageWrites;
985  Page page;
986 
988 
989  page = BufferGetPage(buffer);
990 
991  if (doPageWrites && PageGetLSN(page) <= RedoRecPtr)
992  return true; /* buffer requires backup */
993 
994  return false; /* buffer does not need to be backed up */
995 }
static XLogRecPtr PageGetLSN(Page page)
Definition: bufpage.h:383
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition: xlog.c:6045
static XLogRecPtr RedoRecPtr
Definition: xlog.c:276
static bool doPageWrites
Definition: xlog.c:289

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

Referenced by log_heap_update().

◆ XLogCompressBackupBlock()

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

Definition at line 898 of file xloginsert.c.

900 {
901  int32 orig_len = BLCKSZ - hole_length;
902  int32 len = -1;
903  int32 extra_bytes = 0;
904  char *source;
905  PGAlignedBlock tmp;
906 
907  if (hole_length != 0)
908  {
909  /* must skip the hole */
910  source = tmp.data;
911  memcpy(source, page, hole_offset);
912  memcpy(source + hole_offset,
913  page + (hole_offset + hole_length),
914  BLCKSZ - (hole_length + hole_offset));
915 
916  /*
917  * Extra data needs to be stored in WAL record for the compressed
918  * version of block image if the hole exists.
919  */
921  }
922  else
923  source = page;
924 
926  {
929  break;
930 
931  case WAL_COMPRESSION_LZ4:
932 #ifdef USE_LZ4
933  len = LZ4_compress_default(source, dest, orig_len,
935  if (len <= 0)
936  len = -1; /* failure */
937 #else
938  elog(ERROR, "LZ4 is not supported by this build");
939 #endif
940  break;
941 
943 #ifdef USE_ZSTD
944  len = ZSTD_compress(dest, COMPRESS_BUFSIZE, source, orig_len,
945  ZSTD_CLEVEL_DEFAULT);
946  if (ZSTD_isError(len))
947  len = -1; /* failure */
948 #else
949  elog(ERROR, "zstd is not supported by this build");
950 #endif
951  break;
952 
954  Assert(false); /* cannot happen */
955  break;
956  /* no default case, so that compiler will warn */
957  }
958 
959  /*
960  * We recheck the actual size even if compression reports success and see
961  * if the number of bytes saved by compression is larger than the length
962  * of extra data needed for the compressed version of block image.
963  */
964  if (len >= 0 &&
965  len + extra_bytes < orig_len)
966  {
967  *dlen = (uint16) len; /* successful compression */
968  return true;
969  }
970  return false;
971 }
unsigned short uint16
Definition: c.h:441
signed int int32
Definition: c.h:430
const void size_t len
const PGLZ_Strategy *const PGLZ_strategy_default
int32 pglz_compress(const char *source, int32 slen, char *dest, const PGLZ_Strategy *strategy)
static rewind_source * source
Definition: pg_rewind.c:81
char data[BLCKSZ]
Definition: c.h:1079
int wal_compression
Definition: xlog.c:127
WalCompression
Definition: xlog.h:76
@ WAL_COMPRESSION_NONE
Definition: xlog.h:77
@ WAL_COMPRESSION_LZ4
Definition: xlog.h:79
@ WAL_COMPRESSION_PGLZ
Definition: xlog.h:78
@ WAL_COMPRESSION_ZSTD
Definition: xlog.h:80
#define COMPRESS_BUFSIZE
Definition: xloginsert.c:63
#define SizeOfXLogRecordBlockCompressHeader
Definition: xlogrecord.h:166

References Assert(), COMPRESS_BUFSIZE, PGAlignedBlock::data, generate_unaccent_rules::dest, elog(), ERROR, len, pglz_compress(), PGLZ_strategy_default, SizeOfXLogRecordBlockCompressHeader, source, wal_compression, WAL_COMPRESSION_LZ4, WAL_COMPRESSION_NONE, WAL_COMPRESSION_PGLZ, and WAL_COMPRESSION_ZSTD.

Referenced by XLogRecordAssemble().

◆ XLogEnsureRecordSpace()

void XLogEnsureRecordSpace ( int  max_block_id,
int  ndatas 
)

Definition at line 176 of file xloginsert.c.

177 {
178  int nbuffers;
179 
180  /*
181  * This must be called before entering a critical section, because
182  * allocating memory inside a critical section can fail. repalloc() will
183  * check the same, but better to check it here too so that we fail
184  * consistently even if the arrays happen to be large enough already.
185  */
186  Assert(CritSectionCount == 0);
187 
188  /* the minimum values can't be decreased */
189  if (max_block_id < XLR_NORMAL_MAX_BLOCK_ID)
190  max_block_id = XLR_NORMAL_MAX_BLOCK_ID;
191  if (ndatas < XLR_NORMAL_RDATAS)
192  ndatas = XLR_NORMAL_RDATAS;
193 
194  if (max_block_id > XLR_MAX_BLOCK_ID)
195  elog(ERROR, "maximum number of WAL record block references exceeded");
196  nbuffers = max_block_id + 1;
197 
198  if (nbuffers > max_registered_buffers)
199  {
201  repalloc(registered_buffers, sizeof(registered_buffer) * nbuffers);
202 
203  /*
204  * At least the padding bytes in the structs must be zeroed, because
205  * they are included in WAL data, but initialize it all for tidiness.
206  */
208  (nbuffers - max_registered_buffers) * sizeof(registered_buffer));
209  max_registered_buffers = nbuffers;
210  }
211 
212  if (ndatas > max_rdatas)
213  {
214  rdatas = (XLogRecData *) repalloc(rdatas, sizeof(XLogRecData) * ndatas);
215  max_rdatas = ndatas;
216  }
217 }
#define MemSet(start, val, len)
Definition: c.h:953
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1321

References Assert(), CritSectionCount, elog(), ERROR, max_rdatas, max_registered_buffers, MemSet, rdatas, registered_buffers, 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(), log_newpages(), and shiftList().

◆ XLogInsert()

XLogRecPtr XLogInsert ( RmgrId  rmid,
uint8  info 
)

Definition at line 451 of file xloginsert.c.

452 {
453  XLogRecPtr EndPos;
454 
455  /* XLogBeginInsert() must have been called. */
456  if (!begininsert_called)
457  elog(ERROR, "XLogBeginInsert was not called");
458 
459  /*
460  * The caller can set rmgr bits, XLR_SPECIAL_REL_UPDATE and
461  * XLR_CHECK_CONSISTENCY; the rest are reserved for use by me.
462  */
463  if ((info & ~(XLR_RMGR_INFO_MASK |
465  XLR_CHECK_CONSISTENCY)) != 0)
466  elog(PANIC, "invalid xlog info mask %02X", info);
467 
468  TRACE_POSTGRESQL_WAL_INSERT(rmid, info);
469 
470  /*
471  * In bootstrap mode, we don't actually log anything but XLOG resources;
472  * return a phony record pointer.
473  */
474  if (IsBootstrapProcessingMode() && rmid != RM_XLOG_ID)
475  {
477  EndPos = SizeOfXLogLongPHD; /* start of 1st chkpt record */
478  return EndPos;
479  }
480 
481  do
482  {
484  bool doPageWrites;
485  bool topxid_included = false;
486  XLogRecPtr fpw_lsn;
487  XLogRecData *rdt;
488  int num_fpi = 0;
489 
490  /*
491  * Get values needed to decide whether to do full-page writes. Since
492  * we don't yet have an insertion lock, these could change under us,
493  * but XLogInsertRecord will recheck them once it has a lock.
494  */
496 
497  rdt = XLogRecordAssemble(rmid, info, RedoRecPtr, doPageWrites,
498  &fpw_lsn, &num_fpi, &topxid_included);
499 
500  EndPos = XLogInsertRecord(rdt, fpw_lsn, curinsert_flags, num_fpi,
501  topxid_included);
502  } while (EndPos == InvalidXLogRecPtr);
503 
505 
506  return EndPos;
507 }
#define PANIC
Definition: elog.h:38
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:402
XLogRecPtr XLogInsertRecord(XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi, bool topxid_included)
Definition: xlog.c:731
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:69
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
static uint8 curinsert_flags
Definition: xloginsert.c:104
void XLogResetInsertion(void)
Definition: xloginsert.c:223
static XLogRecData * XLogRecordAssemble(RmgrId rmid, uint8 info, XLogRecPtr RedoRecPtr, bool doPageWrites, XLogRecPtr *fpw_lsn, int *num_fpi, bool *topxid_included)
Definition: xloginsert.c:525
#define XLR_RMGR_INFO_MASK
Definition: xlogrecord.h:63
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:71
#define XLR_CHECK_CONSISTENCY
Definition: xlogrecord.h:80

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_pass(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_getroot(), _bt_insertonpg(), _bt_log_reuse_page(), _bt_mark_page_halfdead(), _bt_newroot(), _bt_set_cleanup_info(), _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(), CreateDatabaseUsingFileCopy(), CreateDirAndVersionFile(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), createPostingTree(), CreateTableSpace(), do_pg_backup_stop(), do_setval(), doPickSplit(), DropTableSpace(), EndPrepare(), ExecuteTruncateGuts(), fill_seq_fork_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_freeze_execute_prepared(), heap_inplace_update(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_page_prune(), heap_update(), lazy_vacuum_heap_page(), log_heap_new_cid(), log_heap_update(), log_heap_visible(), log_newpage(), log_newpage_range(), log_newpages(), log_smgrcreate(), log_split_page(), LogAccessExclusiveLocks(), LogCurrentRunningXacts(), logical_heap_rewrite_flush_mappings(), LogLogicalInvalidations(), LogLogicalMessage(), LogStandbyInvalidations(), movedb(), moveLeafs(), MultiXactIdCreateFromMembers(), nextval_internal(), pg_truncate_visibility_map(), RelationTruncate(), remove_dbtablespaces(), replorigin_advance(), replorigin_drop_guts(), RequestXLogSwitch(), revmap_physical_extend(), shiftList(), spgAddNodeAction(), spgSplitNodeAction(), test_custom_rmgrs_insert_wal_record(), UpdateFullPageWrites(), vacuumLeafPage(), vacuumLeafRoot(), vacuumRedirectAndPlaceholder(), write_relmap_file(), writeListPage(), WriteMTruncateXlogRec(), WriteMZeroPageXlogRec(), WriteTruncateXlogRec(), WriteZeroPageXlogRec(), XactLogAbortRecord(), XactLogCommitRecord(), XLogPutNextOid(), XLogReportParameters(), XLogRestorePoint(), XLogSaveBufferForHint(), and xlogVacuumPage().

◆ XLogRecordAssemble()

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

Definition at line 525 of file xloginsert.c.

528 {
529  XLogRecData *rdt;
530  uint32 total_len = 0;
531  int block_id;
532  pg_crc32c rdata_crc;
533  registered_buffer *prev_regbuf = NULL;
534  XLogRecData *rdt_datas_last;
535  XLogRecord *rechdr;
536  char *scratch = hdr_scratch;
537 
538  /*
539  * Note: this function can be called multiple times for the same record.
540  * All the modifications we do to the rdata chains below must handle that.
541  */
542 
543  /* The record begins with the fixed-size header */
544  rechdr = (XLogRecord *) scratch;
545  scratch += SizeOfXLogRecord;
546 
547  hdr_rdt.next = NULL;
548  rdt_datas_last = &hdr_rdt;
550 
551  /*
552  * Enforce consistency checks for this record if user is looking for it.
553  * Do this before at the beginning of this routine to give the possibility
554  * for callers of XLogInsert() to pass XLR_CHECK_CONSISTENCY directly for
555  * a record.
556  */
557  if (wal_consistency_checking[rmid])
558  info |= XLR_CHECK_CONSISTENCY;
559 
560  /*
561  * Make an rdata chain containing all the data portions of all block
562  * references. This includes the data for full-page images. Also append
563  * the headers for the block references in the scratch buffer.
564  */
565  *fpw_lsn = InvalidXLogRecPtr;
566  for (block_id = 0; block_id < max_registered_block_id; block_id++)
567  {
568  registered_buffer *regbuf = &registered_buffers[block_id];
569  bool needs_backup;
570  bool needs_data;
573  XLogRecordBlockCompressHeader cbimg = {0};
574  bool samerel;
575  bool is_compressed = false;
576  bool include_image;
577 
578  if (!regbuf->in_use)
579  continue;
580 
581  /* Determine if this block needs to be backed up */
582  if (regbuf->flags & REGBUF_FORCE_IMAGE)
583  needs_backup = true;
584  else if (regbuf->flags & REGBUF_NO_IMAGE)
585  needs_backup = false;
586  else if (!doPageWrites)
587  needs_backup = false;
588  else
589  {
590  /*
591  * We assume page LSN is first data on *every* page that can be
592  * passed to XLogInsert, whether it has the standard page layout
593  * or not.
594  */
595  XLogRecPtr page_lsn = PageGetLSN(regbuf->page);
596 
597  needs_backup = (page_lsn <= RedoRecPtr);
598  if (!needs_backup)
599  {
600  if (*fpw_lsn == InvalidXLogRecPtr || page_lsn < *fpw_lsn)
601  *fpw_lsn = page_lsn;
602  }
603  }
604 
605  /* Determine if the buffer data needs to included */
606  if (regbuf->rdata_len == 0)
607  needs_data = false;
608  else if ((regbuf->flags & REGBUF_KEEP_DATA) != 0)
609  needs_data = true;
610  else
611  needs_data = !needs_backup;
612 
613  bkpb.id = block_id;
614  bkpb.fork_flags = regbuf->forkno;
615  bkpb.data_length = 0;
616 
617  if ((regbuf->flags & REGBUF_WILL_INIT) == REGBUF_WILL_INIT)
619 
620  /*
621  * If needs_backup is true or WAL checking is enabled for current
622  * resource manager, log a full-page write for the current block.
623  */
624  include_image = needs_backup || (info & XLR_CHECK_CONSISTENCY) != 0;
625 
626  if (include_image)
627  {
628  Page page = regbuf->page;
629  uint16 compressed_len = 0;
630 
631  /*
632  * The page needs to be backed up, so calculate its hole length
633  * and offset.
634  */
635  if (regbuf->flags & REGBUF_STANDARD)
636  {
637  /* Assume we can omit data between pd_lower and pd_upper */
638  uint16 lower = ((PageHeader) page)->pd_lower;
639  uint16 upper = ((PageHeader) page)->pd_upper;
640 
641  if (lower >= SizeOfPageHeaderData &&
642  upper > lower &&
643  upper <= BLCKSZ)
644  {
645  bimg.hole_offset = lower;
646  cbimg.hole_length = upper - lower;
647  }
648  else
649  {
650  /* No "hole" to remove */
651  bimg.hole_offset = 0;
652  cbimg.hole_length = 0;
653  }
654  }
655  else
656  {
657  /* Not a standard page header, don't try to eliminate "hole" */
658  bimg.hole_offset = 0;
659  cbimg.hole_length = 0;
660  }
661 
662  /*
663  * Try to compress a block image if wal_compression is enabled
664  */
666  {
667  is_compressed =
669  cbimg.hole_length,
670  regbuf->compressed_page,
671  &compressed_len);
672  }
673 
674  /*
675  * Fill in the remaining fields in the XLogRecordBlockHeader
676  * struct
677  */
679 
680  /* Report a full page image constructed for the WAL record */
681  *num_fpi += 1;
682 
683  /*
684  * Construct XLogRecData entries for the page content.
685  */
686  rdt_datas_last->next = &regbuf->bkp_rdatas[0];
687  rdt_datas_last = rdt_datas_last->next;
688 
689  bimg.bimg_info = (cbimg.hole_length == 0) ? 0 : BKPIMAGE_HAS_HOLE;
690 
691  /*
692  * If WAL consistency checking is enabled for the resource manager
693  * of this WAL record, a full-page image is included in the record
694  * for the block modified. During redo, the full-page is replayed
695  * only if BKPIMAGE_APPLY is set.
696  */
697  if (needs_backup)
698  bimg.bimg_info |= BKPIMAGE_APPLY;
699 
700  if (is_compressed)
701  {
702  /* The current compression is stored in the WAL record */
703  bimg.length = compressed_len;
704 
705  /* Set the compression method used for this block */
707  {
710  break;
711 
712  case WAL_COMPRESSION_LZ4:
713 #ifdef USE_LZ4
715 #else
716  elog(ERROR, "LZ4 is not supported by this build");
717 #endif
718  break;
719 
721 #ifdef USE_ZSTD
723 #else
724  elog(ERROR, "zstd is not supported by this build");
725 #endif
726  break;
727 
729  Assert(false); /* cannot happen */
730  break;
731  /* no default case, so that compiler will warn */
732  }
733 
734  rdt_datas_last->data = regbuf->compressed_page;
735  rdt_datas_last->len = compressed_len;
736  }
737  else
738  {
739  bimg.length = BLCKSZ - cbimg.hole_length;
740 
741  if (cbimg.hole_length == 0)
742  {
743  rdt_datas_last->data = page;
744  rdt_datas_last->len = BLCKSZ;
745  }
746  else
747  {
748  /* must skip the hole */
749  rdt_datas_last->data = page;
750  rdt_datas_last->len = bimg.hole_offset;
751 
752  rdt_datas_last->next = &regbuf->bkp_rdatas[1];
753  rdt_datas_last = rdt_datas_last->next;
754 
755  rdt_datas_last->data =
756  page + (bimg.hole_offset + cbimg.hole_length);
757  rdt_datas_last->len =
758  BLCKSZ - (bimg.hole_offset + cbimg.hole_length);
759  }
760  }
761 
762  total_len += bimg.length;
763  }
764 
765  if (needs_data)
766  {
767  /*
768  * When copying to XLogRecordBlockHeader, the length is narrowed
769  * to an uint16. Double-check that it is still correct.
770  */
771  Assert(regbuf->rdata_len <= UINT16_MAX);
772 
773  /*
774  * Link the caller-supplied rdata chain for this buffer to the
775  * overall list.
776  */
778  bkpb.data_length = (uint16) regbuf->rdata_len;
779  total_len += regbuf->rdata_len;
780 
781  rdt_datas_last->next = regbuf->rdata_head;
782  rdt_datas_last = regbuf->rdata_tail;
783  }
784 
785  if (prev_regbuf && RelFileLocatorEquals(regbuf->rlocator, prev_regbuf->rlocator))
786  {
787  samerel = true;
789  }
790  else
791  samerel = false;
792  prev_regbuf = regbuf;
793 
794  /* Ok, copy the header to the scratch buffer */
795  memcpy(scratch, &bkpb, SizeOfXLogRecordBlockHeader);
796  scratch += SizeOfXLogRecordBlockHeader;
797  if (include_image)
798  {
799  memcpy(scratch, &bimg, SizeOfXLogRecordBlockImageHeader);
801  if (cbimg.hole_length != 0 && is_compressed)
802  {
803  memcpy(scratch, &cbimg,
806  }
807  }
808  if (!samerel)
809  {
810  memcpy(scratch, &regbuf->rlocator, sizeof(RelFileLocator));
811  scratch += sizeof(RelFileLocator);
812  }
813  memcpy(scratch, &regbuf->block, sizeof(BlockNumber));
814  scratch += sizeof(BlockNumber);
815  }
816 
817  /* followed by the record's origin, if any */
820  {
821  *(scratch++) = (char) XLR_BLOCK_ID_ORIGIN;
822  memcpy(scratch, &replorigin_session_origin, sizeof(replorigin_session_origin));
823  scratch += sizeof(replorigin_session_origin);
824  }
825 
826  /* followed by toplevel XID, if not already included in previous record */
828  {
830 
831  /* Set the flag that the top xid is included in the WAL */
832  *topxid_included = true;
833 
834  *(scratch++) = (char) XLR_BLOCK_ID_TOPLEVEL_XID;
835  memcpy(scratch, &xid, sizeof(TransactionId));
836  scratch += sizeof(TransactionId);
837  }
838 
839  /* followed by main data, if any */
840  if (mainrdata_len > 0)
841  {
842  if (mainrdata_len > 255)
843  {
844  *(scratch++) = (char) XLR_BLOCK_ID_DATA_LONG;
845  memcpy(scratch, &mainrdata_len, sizeof(uint32));
846  scratch += sizeof(uint32);
847  }
848  else
849  {
850  *(scratch++) = (char) XLR_BLOCK_ID_DATA_SHORT;
851  *(scratch++) = (uint8) mainrdata_len;
852  }
853  rdt_datas_last->next = mainrdata_head;
854  rdt_datas_last = mainrdata_last;
855  total_len += mainrdata_len;
856  }
857  rdt_datas_last->next = NULL;
858 
859  hdr_rdt.len = (scratch - hdr_scratch);
860  total_len += hdr_rdt.len;
861 
862  /*
863  * Calculate CRC of the data
864  *
865  * Note that the record header isn't added into the CRC initially since we
866  * don't know the prev-link yet. Thus, the CRC will represent the CRC of
867  * the whole record in the order: rdata, then backup blocks, then record
868  * header.
869  */
870  INIT_CRC32C(rdata_crc);
872  for (rdt = hdr_rdt.next; rdt != NULL; rdt = rdt->next)
873  COMP_CRC32C(rdata_crc, rdt->data, rdt->len);
874 
875  /*
876  * Fill in the fields in the record header. Prev-link is filled in later,
877  * once we know where in the WAL the record will be inserted. The CRC does
878  * not include the record header yet.
879  */
881  rechdr->xl_tot_len = total_len;
882  rechdr->xl_info = info;
883  rechdr->xl_rmid = rmid;
884  rechdr->xl_prev = InvalidXLogRecPtr;
885  rechdr->xl_crc = rdata_crc;
886 
887  return &hdr_rdt;
888 }
PageHeaderData * PageHeader
Definition: bufpage.h:170
#define SizeOfPageHeaderData
Definition: bufpage.h:213
unsigned int uint32
Definition: c.h:442
unsigned char uint8
Definition: c.h:440
uint32 TransactionId
Definition: c.h:588
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:48
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:79
RepOriginId replorigin_session_origin
Definition: origin.c:156
#define InvalidRepOriginId
Definition: origin.h:33
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
struct RelFileLocator RelFileLocator
#define RelFileLocatorEquals(locator1, locator2)
struct XLogRecData * next
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
pg_crc32c xl_crc
Definition: xlogrecord.h:49
uint8 xl_info
Definition: xlogrecord.h:46
uint32 xl_tot_len
Definition: xlogrecord.h:43
TransactionId xl_xid
Definition: xlogrecord.h:44
RmgrId xl_rmid
Definition: xlogrecord.h:47
XLogRecData bkp_rdatas[2]
Definition: xloginsert.c:83
char compressed_page[COMPRESS_BUFSIZE]
Definition: xloginsert.c:87
XLogRecData * rdata_tail
Definition: xloginsert.c:80
BlockNumber block
Definition: xloginsert.c:75
XLogRecData * rdata_head
Definition: xloginsert.c:78
ForkNumber forkno
Definition: xloginsert.c:74
RelFileLocator rlocator
Definition: xloginsert.c:73
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:431
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:461
bool IsSubxactTopXidLogPending(void)
Definition: xact.c:549
bool * wal_consistency_checking
Definition: xlog.c:129
#define XLOG_INCLUDE_ORIGIN
Definition: xlog.h:149
static XLogRecData hdr_rdt
Definition: xloginsert.c:114
static bool XLogCompressBackupBlock(char *page, uint16 hole_offset, uint16 hole_length, char *dest, uint16 *dlen)
Definition: xloginsert.c:898
#define REGBUF_NO_IMAGE
Definition: xloginsert.h:32
#define REGBUF_KEEP_DATA
Definition: xloginsert.h:35
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33
#define SizeOfXLogRecordBlockImageHeader
Definition: xlogrecord.h:142
#define BKPIMAGE_COMPRESS_ZSTD
Definition: xlogrecord.h:151
#define BKPBLOCK_HAS_DATA
Definition: xlogrecord.h:187
#define BKPIMAGE_APPLY
Definition: xlogrecord.h:147
#define BKPIMAGE_HAS_HOLE
Definition: xlogrecord.h:146
#define XLR_BLOCK_ID_DATA_LONG
Definition: xlogrecord.h:231
#define BKPBLOCK_WILL_INIT
Definition: xlogrecord.h:188
#define BKPIMAGE_COMPRESS_LZ4
Definition: xlogrecord.h:150
#define XLR_BLOCK_ID_TOPLEVEL_XID
Definition: xlogrecord.h:233
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:230
#define BKPBLOCK_SAME_REL
Definition: xlogrecord.h:189
#define SizeOfXLogRecordBlockHeader
Definition: xlogrecord.h:104
#define BKPIMAGE_COMPRESS_PGLZ
Definition: xlogrecord.h:149
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:232
#define BKPBLOCK_HAS_IMAGE
Definition: xlogrecord.h:186

References Assert(), XLogRecordBlockImageHeader::bimg_info, registered_buffer::bkp_rdatas, BKPBLOCK_HAS_DATA, BKPBLOCK_HAS_IMAGE, BKPBLOCK_SAME_REL, BKPBLOCK_WILL_INIT, BKPIMAGE_APPLY, BKPIMAGE_COMPRESS_LZ4, BKPIMAGE_COMPRESS_PGLZ, BKPIMAGE_COMPRESS_ZSTD, BKPIMAGE_HAS_HOLE, registered_buffer::block, COMP_CRC32C, registered_buffer::compressed_page, curinsert_flags, XLogRecData::data, XLogRecordBlockHeader::data_length, doPageWrites, elog(), ERROR, registered_buffer::flags, XLogRecordBlockHeader::fork_flags, registered_buffer::forkno, GetCurrentTransactionIdIfAny(), GetTopTransactionIdIfAny(), hdr_rdt, hdr_scratch, XLogRecordBlockCompressHeader::hole_length, XLogRecordBlockImageHeader::hole_offset, XLogRecordBlockHeader::id, registered_buffer::in_use, INIT_CRC32C, InvalidRepOriginId, InvalidXLogRecPtr, IsSubxactTopXidLogPending(), 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, registered_buffers, RelFileLocatorEquals, replorigin_session_origin, registered_buffer::rlocator, SizeOfPageHeaderData, SizeOfXLogRecord, SizeOfXLogRecordBlockCompressHeader, SizeOfXLogRecordBlockHeader, SizeOfXLogRecordBlockImageHeader, upper(), wal_compression, WAL_COMPRESSION_LZ4, WAL_COMPRESSION_NONE, WAL_COMPRESSION_PGLZ, WAL_COMPRESSION_ZSTD, 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, XLR_BLOCK_ID_TOPLEVEL_XID, and XLR_CHECK_CONSISTENCY.

Referenced by XLogInsert().

◆ XLogRegisterBlock()

void XLogRegisterBlock ( uint8  block_id,
RelFileLocator rlocator,
ForkNumber  forknum,
BlockNumber  blknum,
Page  page,
uint8  flags 
)

Definition at line 296 of file xloginsert.c.

298 {
299  registered_buffer *regbuf;
300 
302 
303  if (block_id >= max_registered_block_id)
304  max_registered_block_id = block_id + 1;
305 
306  if (block_id >= max_registered_buffers)
307  elog(ERROR, "too many registered buffers");
308 
309  regbuf = &registered_buffers[block_id];
310 
311  regbuf->rlocator = *rlocator;
312  regbuf->forkno = forknum;
313  regbuf->block = blknum;
314  regbuf->page = page;
315  regbuf->flags = flags;
316  regbuf->rdata_tail = (XLogRecData *) &regbuf->rdata_head;
317  regbuf->rdata_len = 0;
318 
319  /*
320  * Check that this page hasn't already been registered with some other
321  * block_id.
322  */
323 #ifdef USE_ASSERT_CHECKING
324  {
325  int i;
326 
327  for (i = 0; i < max_registered_block_id; i++)
328  {
329  registered_buffer *regbuf_old = &registered_buffers[i];
330 
331  if (i == block_id || !regbuf_old->in_use)
332  continue;
333 
334  Assert(!RelFileLocatorEquals(regbuf_old->rlocator, regbuf->rlocator) ||
335  regbuf_old->forkno != regbuf->forkno ||
336  regbuf_old->block != regbuf->block);
337  }
338  }
339 #endif
340 
341  regbuf->in_use = true;
342 }

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, registered_buffers, RelFileLocatorEquals, and registered_buffer::rlocator.

Referenced by log_newpage(), log_newpages(), and XLogSaveBufferForHint().

◆ XLogRegisterBufData()

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

Definition at line 389 of file xloginsert.c.

390 {
391  registered_buffer *regbuf;
392  XLogRecData *rdata;
393 
395 
396  /* find the registered buffer struct */
397  regbuf = &registered_buffers[block_id];
398  if (!regbuf->in_use)
399  elog(ERROR, "no block with id %d registered with WAL insertion",
400  block_id);
401 
402  /*
403  * Check against max_rdatas and ensure we do not register more data per
404  * buffer than can be handled by the physical data format; i.e. that
405  * regbuf->rdata_len does not grow beyond what
406  * XLogRecordBlockHeader->data_length can hold.
407  */
408  if (num_rdatas >= max_rdatas ||
409  regbuf->rdata_len + len > UINT16_MAX)
410  elog(ERROR, "too much WAL data");
411 
412  rdata = &rdatas[num_rdatas++];
413 
414  rdata->data = data;
415  rdata->len = len;
416 
417  regbuf->rdata_tail->next = rdata;
418  regbuf->rdata_tail = rdata;
419  regbuf->rdata_len += len;
420 }
const void * data
static int num_rdatas
Definition: xloginsert.c:130

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

Referenced by _bt_dedup_pass(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_getroot(), _bt_insertonpg(), _bt_newroot(), _bt_set_cleanup_info(), _bt_split(), _bt_unlink_halfdead_page(), _hash_addovflpage(), _hash_doinsert(), _hash_expandtable(), _hash_freeovflpage(), _hash_squeezebucket(), brin_doinsert(), brin_doupdate(), dataExecPlaceToPageInternal(), dataExecPlaceToPageLeaf(), entryExecPlaceToPage(), GenericXLogFinish(), ginHeapTupleFastInsert(), ginVacuumPostingTreeLeaf(), gistXLogSplit(), gistXLogUpdate(), hashbucketcleanup(), heap_freeze_execute_prepared(), heap_inplace_update(), heap_insert(), heap_multi_insert(), heap_page_prune(), lazy_vacuum_heap_page(), log_heap_update(), and writeListPage().

◆ XLogRegisterBuffer()

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

Definition at line 243 of file xloginsert.c.

244 {
245  registered_buffer *regbuf;
246 
247  /* NO_IMAGE doesn't make sense with FORCE_IMAGE */
248  Assert(!((flags & REGBUF_FORCE_IMAGE) && (flags & (REGBUF_NO_IMAGE))));
250 
251  if (block_id >= max_registered_block_id)
252  {
253  if (block_id >= max_registered_buffers)
254  elog(ERROR, "too many registered buffers");
255  max_registered_block_id = block_id + 1;
256  }
257 
258  regbuf = &registered_buffers[block_id];
259 
260  BufferGetTag(buffer, &regbuf->rlocator, &regbuf->forkno, &regbuf->block);
261  regbuf->page = BufferGetPage(buffer);
262  regbuf->flags = flags;
263  regbuf->rdata_tail = (XLogRecData *) &regbuf->rdata_head;
264  regbuf->rdata_len = 0;
265 
266  /*
267  * Check that this page hasn't already been registered with some other
268  * block_id.
269  */
270 #ifdef USE_ASSERT_CHECKING
271  {
272  int i;
273 
274  for (i = 0; i < max_registered_block_id; i++)
275  {
276  registered_buffer *regbuf_old = &registered_buffers[i];
277 
278  if (i == block_id || !regbuf_old->in_use)
279  continue;
280 
281  Assert(!RelFileLocatorEquals(regbuf_old->rlocator, regbuf->rlocator) ||
282  regbuf_old->forkno != regbuf->forkno ||
283  regbuf_old->block != regbuf->block);
284  }
285  }
286 #endif
287 
288  regbuf->in_use = true;
289 }

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, registered_buffers, RelFileLocatorEquals, and registered_buffer::rlocator.

Referenced by _bt_dedup_pass(), _bt_delitems_delete(), _bt_delitems_vacuum(), _bt_getroot(), _bt_insertonpg(), _bt_mark_page_halfdead(), _bt_newroot(), _bt_set_cleanup_info(), _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(), brin_doinsert(), brin_doupdate(), brinbuild(), brinRevmapDesummarizeRange(), createPostingTree(), do_setval(), doPickSplit(), fill_seq_fork_with_data(), GenericXLogFinish(), ginDeletePage(), ginHeapTupleFastInsert(), ginPlaceToPage(), ginUpdateStats(), ginVacuumPostingTreeLeaf(), gistXLogDelete(), gistXLogPageDelete(), gistXLogSplit(), gistXLogUpdate(), hashbucketcleanup(), hashbulkdelete(), heap_abort_speculative(), heap_delete(), heap_finish_speculative(), heap_freeze_execute_prepared(), heap_inplace_update(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_page_prune(), heap_update(), lazy_vacuum_heap_page(), 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().

◆ XLogRegisterData()

void XLogRegisterData ( char *  data,
uint32  len 
)

Definition at line 351 of file xloginsert.c.

352 {
353  XLogRecData *rdata;
354 
356 
357  if (num_rdatas >= max_rdatas)
358  elog(ERROR, "too much WAL data");
359  rdata = &rdatas[num_rdatas++];
360 
361  rdata->data = data;
362  rdata->len = len;
363 
364  /*
365  * we use the mainrdata_last pointer to track the end of the chain, so no
366  * need to clear 'next' here.
367  */
368 
369  mainrdata_last->next = rdata;
370  mainrdata_last = rdata;
371 
372  mainrdata_len += len;
373 }

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

Referenced by _bt_dedup_pass(), _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(), CreateDatabaseUsingFileCopy(), CreateDirAndVersionFile(), CreateEndOfRecoveryRecord(), CreateOverwriteContrecordRecord(), createPostingTree(), CreateTableSpace(), do_pg_backup_stop(), do_setval(), doPickSplit(), DropTableSpace(), EndPrepare(), ExecuteTruncateGuts(), fill_seq_fork_with_data(), ginDeletePage(), ginHeapTupleFastInsert(), ginPlaceToPage(), ginUpdateStats(), gistXLogAssignLSN(), gistXLogDelete(), gistXLogPageDelete(), gistXLogPageReuse(), gistXLogSplit(), gistXLogUpdate(), hashbucketcleanup(), hashbulkdelete(), heap_abort_speculative(), heap_delete(), heap_finish_speculative(), heap_freeze_execute_prepared(), heap_inplace_update(), heap_insert(), heap_lock_tuple(), heap_lock_updated_tuple_rec(), heap_multi_insert(), heap_page_prune(), heap_update(), lazy_vacuum_heap_page(), log_heap_new_cid(), log_heap_update(), log_heap_visible(), log_smgrcreate(), LogAccessExclusiveLocks(), LogCurrentRunningXacts(), logical_heap_rewrite_flush_mappings(), LogLogicalInvalidations(), LogLogicalMessage(), LogStandbyInvalidations(), movedb(), moveLeafs(), MultiXactIdCreateFromMembers(), nextval_internal(), pg_truncate_visibility_map(), RelationTruncate(), remove_dbtablespaces(), replorigin_advance(), replorigin_drop_guts(), revmap_physical_extend(), shiftList(), spgAddNodeAction(), spgSplitNodeAction(), test_custom_rmgrs_insert_wal_record(), UpdateFullPageWrites(), vacuumLeafPage(), vacuumLeafRoot(), vacuumRedirectAndPlaceholder(), write_relmap_file(), writeListPage(), WriteMTruncateXlogRec(), WriteMZeroPageXlogRec(), WriteTruncateXlogRec(), WriteZeroPageXlogRec(), XactLogAbortRecord(), XactLogCommitRecord(), XLogPutNextOid(), XLogReportParameters(), and XLogRestorePoint().

◆ XLogResetInsertion()

void XLogResetInsertion ( void  )

Definition at line 223 of file xloginsert.c.

224 {
225  int i;
226 
227  for (i = 0; i < max_registered_block_id; i++)
228  registered_buffers[i].in_use = false;
229 
230  num_rdatas = 0;
232  mainrdata_len = 0;
234  curinsert_flags = 0;
235  begininsert_called = false;
236 }

References begininsert_called, curinsert_flags, i, mainrdata_head, mainrdata_last, mainrdata_len, max_registered_block_id, num_rdatas, and registered_buffers.

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

◆ XLogSaveBufferForHint()

XLogRecPtr XLogSaveBufferForHint ( Buffer  buffer,
bool  buffer_std 
)

Definition at line 1019 of file xloginsert.c.

1020 {
1021  XLogRecPtr recptr = InvalidXLogRecPtr;
1022  XLogRecPtr lsn;
1024 
1025  /*
1026  * Ensure no checkpoint can change our view of RedoRecPtr.
1027  */
1029 
1030  /*
1031  * Update RedoRecPtr so that we can make the right decision
1032  */
1034 
1035  /*
1036  * We assume page LSN is first data on *every* page that can be passed to
1037  * XLogInsert, whether it has the standard page layout or not. Since we're
1038  * only holding a share-lock on the page, we must take the buffer header
1039  * lock when we look at the LSN.
1040  */
1041  lsn = BufferGetLSNAtomic(buffer);
1042 
1043  if (lsn <= RedoRecPtr)
1044  {
1045  int flags = 0;
1046  PGAlignedBlock copied_buffer;
1047  char *origdata = (char *) BufferGetBlock(buffer);
1048  RelFileLocator rlocator;
1049  ForkNumber forkno;
1050  BlockNumber blkno;
1051 
1052  /*
1053  * Copy buffer so we don't have to worry about concurrent hint bit or
1054  * lsn updates. We assume pd_lower/upper cannot be changed without an
1055  * exclusive lock, so the contents bkp are not racy.
1056  */
1057  if (buffer_std)
1058  {
1059  /* Assume we can omit data between pd_lower and pd_upper */
1060  Page page = BufferGetPage(buffer);
1061  uint16 lower = ((PageHeader) page)->pd_lower;
1062  uint16 upper = ((PageHeader) page)->pd_upper;
1063 
1064  memcpy(copied_buffer.data, origdata, lower);
1065  memcpy(copied_buffer.data + upper, origdata + upper, BLCKSZ - upper);
1066  }
1067  else
1068  memcpy(copied_buffer.data, origdata, BLCKSZ);
1069 
1070  XLogBeginInsert();
1071 
1072  if (buffer_std)
1073  flags |= REGBUF_STANDARD;
1074 
1075  BufferGetTag(buffer, &rlocator, &forkno, &blkno);
1076  XLogRegisterBlock(0, &rlocator, forkno, blkno, copied_buffer.data, flags);
1077 
1078  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI_FOR_HINT);
1079  }
1080 
1081  return recptr;
1082 }
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:3012
static Block BufferGetBlock(Buffer buffer)
Definition: bufmgr.h:244
#define XLOG_FPI_FOR_HINT
Definition: pg_control.h:77
#define DELAY_CHKPT_START
Definition: proc.h:119
PGPROC * MyProc
Definition: proc.c:68
int delayChkptFlags
Definition: proc.h:231
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6015

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

Referenced by MarkBufferDirtyHint().

◆ XLogSetRecordFlags()

Variable Documentation

◆ begininsert_called

◆ curinsert_flags

uint8 curinsert_flags = 0
static

◆ hdr_rdt

XLogRecData hdr_rdt
static

Definition at line 114 of file xloginsert.c.

Referenced by XLogRecordAssemble().

◆ hdr_scratch

char* hdr_scratch = NULL
static

Definition at line 115 of file xloginsert.c.

Referenced by InitXLogInsert(), and XLogRecordAssemble().

◆ mainrdata_head

XLogRecData* mainrdata_head
static

Definition at line 99 of file xloginsert.c.

Referenced by XLogBeginInsert(), XLogRecordAssemble(), and XLogResetInsertion().

◆ mainrdata_last

XLogRecData* mainrdata_last = (XLogRecData *) &mainrdata_head
static

◆ 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 130 of file xloginsert.c.

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

◆ rdatas

XLogRecData* rdatas
static

◆ registered_buffers

◆ xloginsert_cxt

MemoryContext xloginsert_cxt
static

Definition at line 136 of file xloginsert.c.

Referenced by InitXLogInsert().