PostgreSQL Source Code  git master
xloginsert.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * xloginsert.c
4  * Functions for constructing WAL records
5  *
6  * Constructing a WAL record begins with a call to XLogBeginInsert,
7  * followed by a number of XLogRegister* calls. The registered data is
8  * collected in private working memory, and finally assembled into a chain
9  * of XLogRecData structs by a call to XLogRecordAssemble(). See
10  * access/transam/README for details.
11  *
12  * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  * src/backend/access/transam/xloginsert.c
16  *
17  *-------------------------------------------------------------------------
18  */
19 
20 #include "postgres.h"
21 
22 #ifdef USE_LZ4
23 #include <lz4.h>
24 #endif
25 
26 #ifdef USE_ZSTD
27 #include <zstd.h>
28 #endif
29 
30 #include "access/xact.h"
31 #include "access/xlog.h"
32 #include "access/xlog_internal.h"
33 #include "access/xloginsert.h"
34 #include "catalog/pg_control.h"
35 #include "common/pg_lzcompress.h"
36 #include "executor/instrument.h"
37 #include "miscadmin.h"
38 #include "pg_trace.h"
39 #include "replication/origin.h"
40 #include "storage/bufmgr.h"
41 #include "storage/proc.h"
42 #include "utils/memutils.h"
43 
44 /*
45  * Guess the maximum buffer size required to store a compressed version of
46  * backup block image.
47  */
48 #ifdef USE_LZ4
49 #define LZ4_MAX_BLCKSZ LZ4_COMPRESSBOUND(BLCKSZ)
50 #else
51 #define LZ4_MAX_BLCKSZ 0
52 #endif
53 
54 #ifdef USE_ZSTD
55 #define ZSTD_MAX_BLCKSZ ZSTD_COMPRESSBOUND(BLCKSZ)
56 #else
57 #define ZSTD_MAX_BLCKSZ 0
58 #endif
59 
60 #define PGLZ_MAX_BLCKSZ PGLZ_MAX_OUTPUT(BLCKSZ)
61 
62 /* Buffer size required to store a compressed version of backup block image */
63 #define COMPRESS_BUFSIZE Max(Max(PGLZ_MAX_BLCKSZ, LZ4_MAX_BLCKSZ), ZSTD_MAX_BLCKSZ)
64 
65 /*
66  * For each block reference registered with XLogRegisterBuffer, we fill in
67  * a registered_buffer struct.
68  */
69 typedef struct
70 {
71  bool in_use; /* is this slot in use? */
72  uint8 flags; /* REGBUF_* flags */
73  RelFileLocator rlocator; /* identifies the relation and block */
76  Page page; /* page content */
77  uint32 rdata_len; /* total length of data in rdata chain */
78  XLogRecData *rdata_head; /* head of the chain of data registered with
79  * this block */
80  XLogRecData *rdata_tail; /* last entry in the chain, or &rdata_head if
81  * empty */
82 
83  XLogRecData bkp_rdatas[2]; /* temporary rdatas used to hold references to
84  * backup block data in XLogRecordAssemble() */
85 
86  /* buffer to store a compressed version of backup block image */
87  char compressed_page[COMPRESS_BUFSIZE];
89 
91 static int max_registered_buffers; /* allocated size */
92 static int max_registered_block_id = 0; /* highest block_id + 1 currently
93  * registered */
94 
95 /*
96  * A chain of XLogRecDatas to hold the "main data" of a WAL record, registered
97  * with XLogRegisterData(...).
98  */
101 static uint64 mainrdata_len; /* total # of bytes in chain */
102 
103 /* flags for the in-progress insertion */
105 
106 /*
107  * These are used to hold the record header while constructing a record.
108  * 'hdr_scratch' is not a plain variable, but is palloc'd at initialization,
109  * because we want it to be MAXALIGNed and padding bytes zeroed.
110  *
111  * For simplicity, it's allocated large enough to hold the headers for any
112  * WAL record.
113  */
115 static char *hdr_scratch = NULL;
116 
117 #define SizeOfXlogOrigin (sizeof(RepOriginId) + sizeof(char))
118 #define SizeOfXLogTransactionId (sizeof(TransactionId) + sizeof(char))
119 
120 #define HEADER_SCRATCH_SIZE \
121  (SizeOfXLogRecord + \
122  MaxSizeOfXLogRecordBlockHeader * (XLR_MAX_BLOCK_ID + 1) + \
123  SizeOfXLogRecordDataHeaderLong + SizeOfXlogOrigin + \
124  SizeOfXLogTransactionId)
125 
126 /*
127  * An array of XLogRecData structs, to hold registered data.
128  */
130 static int num_rdatas; /* entries currently used */
131 static int max_rdatas; /* allocated size */
132 
133 static bool begininsert_called = false;
134 
135 /* Memory context to hold the registered buffer and data references. */
137 
138 static XLogRecData *XLogRecordAssemble(RmgrId rmid, uint8 info,
140  XLogRecPtr *fpw_lsn, int *num_fpi,
141  bool *topxid_included);
142 static bool XLogCompressBackupBlock(char *page, uint16 hole_offset,
143  uint16 hole_length, char *dest, uint16 *dlen);
144 
145 /*
146  * Begin constructing a WAL record. This must be called before the
147  * XLogRegister* functions and XLogInsert().
148  */
149 void
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 }
165 
166 /*
167  * Ensure that there are enough buffer and data slots in the working area,
168  * for subsequent XLogRegisterBuffer, XLogRegisterData and XLogRegisterBufData
169  * calls.
170  *
171  * There is always space for a small number of buffers and data chunks, enough
172  * for most record types. This function is for the exceptional cases that need
173  * more.
174  */
175 void
176 XLogEnsureRecordSpace(int max_block_id, int ndatas)
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 }
218 
219 /*
220  * Reset WAL record construction buffers.
221  */
222 void
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 }
237 
238 /*
239  * Register a reference to a buffer with the WAL record being constructed.
240  * This must be called for every page that the WAL-logged operation modifies.
241  */
242 void
243 XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
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 }
290 
291 /*
292  * Like XLogRegisterBuffer, but for registering a block that's not in the
293  * shared buffer pool (i.e. when you don't have a Buffer for it).
294  */
295 void
296 XLogRegisterBlock(uint8 block_id, RelFileLocator *rlocator, ForkNumber forknum,
297  BlockNumber blknum, Page page, uint8 flags)
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 }
343 
344 /*
345  * Add data to the WAL record that's being constructed.
346  *
347  * The data is appended to the "main chunk", available at replay with
348  * XLogRecGetData().
349  */
350 void
352 {
353  XLogRecData *rdata;
354 
356 
357  if (num_rdatas >= max_rdatas)
358  ereport(ERROR,
359  (errmsg_internal("too much WAL data"),
360  errdetail_internal("%d out of %d data segments are already in use.",
362  rdata = &rdatas[num_rdatas++];
363 
364  rdata->data = data;
365  rdata->len = len;
366 
367  /*
368  * we use the mainrdata_last pointer to track the end of the chain, so no
369  * need to clear 'next' here.
370  */
371 
372  mainrdata_last->next = rdata;
373  mainrdata_last = rdata;
374 
375  mainrdata_len += len;
376 }
377 
378 /*
379  * Add buffer-specific data to the WAL record that's being constructed.
380  *
381  * Block_id must reference a block previously registered with
382  * XLogRegisterBuffer(). If this is called more than once for the same
383  * block_id, the data is appended.
384  *
385  * The maximum amount of data that can be registered per block is 65535
386  * bytes. That should be plenty; if you need more than BLCKSZ bytes to
387  * reconstruct the changes to the page, you might as well just log a full
388  * copy of it. (the "main data" that's not associated with a block is not
389  * limited)
390  */
391 void
393 {
394  registered_buffer *regbuf;
395  XLogRecData *rdata;
396 
398 
399  /* find the registered buffer struct */
400  regbuf = &registered_buffers[block_id];
401  if (!regbuf->in_use)
402  elog(ERROR, "no block with id %d registered with WAL insertion",
403  block_id);
404 
405  /*
406  * Check against max_rdatas and ensure we do not register more data per
407  * buffer than can be handled by the physical data format; i.e. that
408  * regbuf->rdata_len does not grow beyond what
409  * XLogRecordBlockHeader->data_length can hold.
410  */
411  if (num_rdatas >= max_rdatas)
412  ereport(ERROR,
413  (errmsg_internal("too much WAL data"),
414  errdetail_internal("%d out of %d data segments are already in use.",
416  if (regbuf->rdata_len + len > UINT16_MAX || len > UINT16_MAX)
417  ereport(ERROR,
418  (errmsg_internal("too much WAL data"),
419  errdetail_internal("Registering more than maximum %u bytes allowed to block %u: current %u bytes, adding %u bytes.",
420  UINT16_MAX, block_id, regbuf->rdata_len, len)));
421 
422  rdata = &rdatas[num_rdatas++];
423 
424  rdata->data = data;
425  rdata->len = len;
426 
427  regbuf->rdata_tail->next = rdata;
428  regbuf->rdata_tail = rdata;
429  regbuf->rdata_len += len;
430 }
431 
432 /*
433  * Set insert status flags for the upcoming WAL record.
434  *
435  * The flags that can be used here are:
436  * - XLOG_INCLUDE_ORIGIN, to determine if the replication origin should be
437  * included in the record.
438  * - XLOG_MARK_UNIMPORTANT, to signal that the record is not important for
439  * durability, which allows to avoid triggering WAL archiving and other
440  * background activity.
441  */
442 void
444 {
446  curinsert_flags |= flags;
447 }
448 
449 /*
450  * Insert an XLOG record having the specified RMID and info bytes, with the
451  * body of the record being the data and buffer references registered earlier
452  * with XLogRegister* calls.
453  *
454  * Returns XLOG pointer to end of record (beginning of next record).
455  * This can be used as LSN for data pages affected by the logged action.
456  * (LSN is the XLOG point up to which the XLOG must be flushed to disk
457  * before the data page can be written out. This implements the basic
458  * WAL rule "write the log before the data".)
459  */
462 {
463  XLogRecPtr EndPos;
464 
465  /* XLogBeginInsert() must have been called. */
466  if (!begininsert_called)
467  elog(ERROR, "XLogBeginInsert was not called");
468 
469  /*
470  * The caller can set rmgr bits, XLR_SPECIAL_REL_UPDATE and
471  * XLR_CHECK_CONSISTENCY; the rest are reserved for use by me.
472  */
473  if ((info & ~(XLR_RMGR_INFO_MASK |
475  XLR_CHECK_CONSISTENCY)) != 0)
476  elog(PANIC, "invalid xlog info mask %02X", info);
477 
478  TRACE_POSTGRESQL_WAL_INSERT(rmid, info);
479 
480  /*
481  * In bootstrap mode, we don't actually log anything but XLOG resources;
482  * return a phony record pointer.
483  */
484  if (IsBootstrapProcessingMode() && rmid != RM_XLOG_ID)
485  {
487  EndPos = SizeOfXLogLongPHD; /* start of 1st chkpt record */
488  return EndPos;
489  }
490 
491  do
492  {
494  bool doPageWrites;
495  bool topxid_included = false;
496  XLogRecPtr fpw_lsn;
497  XLogRecData *rdt;
498  int num_fpi = 0;
499 
500  /*
501  * Get values needed to decide whether to do full-page writes. Since
502  * we don't yet have an insertion lock, these could change under us,
503  * but XLogInsertRecord will recheck them once it has a lock.
504  */
506 
507  rdt = XLogRecordAssemble(rmid, info, RedoRecPtr, doPageWrites,
508  &fpw_lsn, &num_fpi, &topxid_included);
509 
510  EndPos = XLogInsertRecord(rdt, fpw_lsn, curinsert_flags, num_fpi,
511  topxid_included);
512  } while (EndPos == InvalidXLogRecPtr);
513 
515 
516  return EndPos;
517 }
518 
519 /*
520  * Assemble a WAL record from the registered data and buffers into an
521  * XLogRecData chain, ready for insertion with XLogInsertRecord().
522  *
523  * The record header fields are filled in, except for the xl_prev field. The
524  * calculated CRC does not include the record header yet.
525  *
526  * If there are any registered buffers, and a full-page image was not taken
527  * of all of them, *fpw_lsn is set to the lowest LSN among such pages. This
528  * signals that the assembled record is only good for insertion on the
529  * assumption that the RedoRecPtr and doPageWrites values were up-to-date.
530  *
531  * *topxid_included is set if the topmost transaction ID is logged with the
532  * current subtransaction.
533  */
534 static XLogRecData *
537  XLogRecPtr *fpw_lsn, int *num_fpi, bool *topxid_included)
538 {
539  XLogRecData *rdt;
540  uint64 total_len = 0;
541  int block_id;
542  pg_crc32c rdata_crc;
543  registered_buffer *prev_regbuf = NULL;
544  XLogRecData *rdt_datas_last;
545  XLogRecord *rechdr;
546  char *scratch = hdr_scratch;
547 
548  /*
549  * Note: this function can be called multiple times for the same record.
550  * All the modifications we do to the rdata chains below must handle that.
551  */
552 
553  /* The record begins with the fixed-size header */
554  rechdr = (XLogRecord *) scratch;
555  scratch += SizeOfXLogRecord;
556 
557  hdr_rdt.next = NULL;
558  rdt_datas_last = &hdr_rdt;
560 
561  /*
562  * Enforce consistency checks for this record if user is looking for it.
563  * Do this before at the beginning of this routine to give the possibility
564  * for callers of XLogInsert() to pass XLR_CHECK_CONSISTENCY directly for
565  * a record.
566  */
567  if (wal_consistency_checking[rmid])
568  info |= XLR_CHECK_CONSISTENCY;
569 
570  /*
571  * Make an rdata chain containing all the data portions of all block
572  * references. This includes the data for full-page images. Also append
573  * the headers for the block references in the scratch buffer.
574  */
575  *fpw_lsn = InvalidXLogRecPtr;
576  for (block_id = 0; block_id < max_registered_block_id; block_id++)
577  {
578  registered_buffer *regbuf = &registered_buffers[block_id];
579  bool needs_backup;
580  bool needs_data;
583  XLogRecordBlockCompressHeader cbimg = {0};
584  bool samerel;
585  bool is_compressed = false;
586  bool include_image;
587 
588  if (!regbuf->in_use)
589  continue;
590 
591  /* Determine if this block needs to be backed up */
592  if (regbuf->flags & REGBUF_FORCE_IMAGE)
593  needs_backup = true;
594  else if (regbuf->flags & REGBUF_NO_IMAGE)
595  needs_backup = false;
596  else if (!doPageWrites)
597  needs_backup = false;
598  else
599  {
600  /*
601  * We assume page LSN is first data on *every* page that can be
602  * passed to XLogInsert, whether it has the standard page layout
603  * or not.
604  */
605  XLogRecPtr page_lsn = PageGetLSN(regbuf->page);
606 
607  needs_backup = (page_lsn <= RedoRecPtr);
608  if (!needs_backup)
609  {
610  if (*fpw_lsn == InvalidXLogRecPtr || page_lsn < *fpw_lsn)
611  *fpw_lsn = page_lsn;
612  }
613  }
614 
615  /* Determine if the buffer data needs to included */
616  if (regbuf->rdata_len == 0)
617  needs_data = false;
618  else if ((regbuf->flags & REGBUF_KEEP_DATA) != 0)
619  needs_data = true;
620  else
621  needs_data = !needs_backup;
622 
623  bkpb.id = block_id;
624  bkpb.fork_flags = regbuf->forkno;
625  bkpb.data_length = 0;
626 
627  if ((regbuf->flags & REGBUF_WILL_INIT) == REGBUF_WILL_INIT)
629 
630  /*
631  * If needs_backup is true or WAL checking is enabled for current
632  * resource manager, log a full-page write for the current block.
633  */
634  include_image = needs_backup || (info & XLR_CHECK_CONSISTENCY) != 0;
635 
636  if (include_image)
637  {
638  Page page = regbuf->page;
639  uint16 compressed_len = 0;
640 
641  /*
642  * The page needs to be backed up, so calculate its hole length
643  * and offset.
644  */
645  if (regbuf->flags & REGBUF_STANDARD)
646  {
647  /* Assume we can omit data between pd_lower and pd_upper */
648  uint16 lower = ((PageHeader) page)->pd_lower;
649  uint16 upper = ((PageHeader) page)->pd_upper;
650 
651  if (lower >= SizeOfPageHeaderData &&
652  upper > lower &&
653  upper <= BLCKSZ)
654  {
655  bimg.hole_offset = lower;
656  cbimg.hole_length = upper - lower;
657  }
658  else
659  {
660  /* No "hole" to remove */
661  bimg.hole_offset = 0;
662  cbimg.hole_length = 0;
663  }
664  }
665  else
666  {
667  /* Not a standard page header, don't try to eliminate "hole" */
668  bimg.hole_offset = 0;
669  cbimg.hole_length = 0;
670  }
671 
672  /*
673  * Try to compress a block image if wal_compression is enabled
674  */
676  {
677  is_compressed =
679  cbimg.hole_length,
680  regbuf->compressed_page,
681  &compressed_len);
682  }
683 
684  /*
685  * Fill in the remaining fields in the XLogRecordBlockHeader
686  * struct
687  */
689 
690  /* Report a full page image constructed for the WAL record */
691  *num_fpi += 1;
692 
693  /*
694  * Construct XLogRecData entries for the page content.
695  */
696  rdt_datas_last->next = &regbuf->bkp_rdatas[0];
697  rdt_datas_last = rdt_datas_last->next;
698 
699  bimg.bimg_info = (cbimg.hole_length == 0) ? 0 : BKPIMAGE_HAS_HOLE;
700 
701  /*
702  * If WAL consistency checking is enabled for the resource manager
703  * of this WAL record, a full-page image is included in the record
704  * for the block modified. During redo, the full-page is replayed
705  * only if BKPIMAGE_APPLY is set.
706  */
707  if (needs_backup)
708  bimg.bimg_info |= BKPIMAGE_APPLY;
709 
710  if (is_compressed)
711  {
712  /* The current compression is stored in the WAL record */
713  bimg.length = compressed_len;
714 
715  /* Set the compression method used for this block */
717  {
720  break;
721 
722  case WAL_COMPRESSION_LZ4:
723 #ifdef USE_LZ4
725 #else
726  elog(ERROR, "LZ4 is not supported by this build");
727 #endif
728  break;
729 
731 #ifdef USE_ZSTD
733 #else
734  elog(ERROR, "zstd is not supported by this build");
735 #endif
736  break;
737 
739  Assert(false); /* cannot happen */
740  break;
741  /* no default case, so that compiler will warn */
742  }
743 
744  rdt_datas_last->data = regbuf->compressed_page;
745  rdt_datas_last->len = compressed_len;
746  }
747  else
748  {
749  bimg.length = BLCKSZ - cbimg.hole_length;
750 
751  if (cbimg.hole_length == 0)
752  {
753  rdt_datas_last->data = page;
754  rdt_datas_last->len = BLCKSZ;
755  }
756  else
757  {
758  /* must skip the hole */
759  rdt_datas_last->data = page;
760  rdt_datas_last->len = bimg.hole_offset;
761 
762  rdt_datas_last->next = &regbuf->bkp_rdatas[1];
763  rdt_datas_last = rdt_datas_last->next;
764 
765  rdt_datas_last->data =
766  page + (bimg.hole_offset + cbimg.hole_length);
767  rdt_datas_last->len =
768  BLCKSZ - (bimg.hole_offset + cbimg.hole_length);
769  }
770  }
771 
772  total_len += bimg.length;
773  }
774 
775  if (needs_data)
776  {
777  /*
778  * When copying to XLogRecordBlockHeader, the length is narrowed
779  * to an uint16. Double-check that it is still correct.
780  */
781  Assert(regbuf->rdata_len <= UINT16_MAX);
782 
783  /*
784  * Link the caller-supplied rdata chain for this buffer to the
785  * overall list.
786  */
788  bkpb.data_length = (uint16) regbuf->rdata_len;
789  total_len += regbuf->rdata_len;
790 
791  rdt_datas_last->next = regbuf->rdata_head;
792  rdt_datas_last = regbuf->rdata_tail;
793  }
794 
795  if (prev_regbuf && RelFileLocatorEquals(regbuf->rlocator, prev_regbuf->rlocator))
796  {
797  samerel = true;
799  }
800  else
801  samerel = false;
802  prev_regbuf = regbuf;
803 
804  /* Ok, copy the header to the scratch buffer */
805  memcpy(scratch, &bkpb, SizeOfXLogRecordBlockHeader);
806  scratch += SizeOfXLogRecordBlockHeader;
807  if (include_image)
808  {
809  memcpy(scratch, &bimg, SizeOfXLogRecordBlockImageHeader);
811  if (cbimg.hole_length != 0 && is_compressed)
812  {
813  memcpy(scratch, &cbimg,
816  }
817  }
818  if (!samerel)
819  {
820  memcpy(scratch, &regbuf->rlocator, sizeof(RelFileLocator));
821  scratch += sizeof(RelFileLocator);
822  }
823  memcpy(scratch, &regbuf->block, sizeof(BlockNumber));
824  scratch += sizeof(BlockNumber);
825  }
826 
827  /* followed by the record's origin, if any */
830  {
831  *(scratch++) = (char) XLR_BLOCK_ID_ORIGIN;
832  memcpy(scratch, &replorigin_session_origin, sizeof(replorigin_session_origin));
833  scratch += sizeof(replorigin_session_origin);
834  }
835 
836  /* followed by toplevel XID, if not already included in previous record */
838  {
840 
841  /* Set the flag that the top xid is included in the WAL */
842  *topxid_included = true;
843 
844  *(scratch++) = (char) XLR_BLOCK_ID_TOPLEVEL_XID;
845  memcpy(scratch, &xid, sizeof(TransactionId));
846  scratch += sizeof(TransactionId);
847  }
848 
849  /* followed by main data, if any */
850  if (mainrdata_len > 0)
851  {
852  if (mainrdata_len > 255)
853  {
854  uint32 mainrdata_len_4b;
855 
857  ereport(ERROR,
858  (errmsg_internal("too much WAL data"),
859  errdetail_internal("Main data length is %llu bytes for a maximum of %u bytes.",
860  (unsigned long long) mainrdata_len,
861  PG_UINT32_MAX)));
862 
863  mainrdata_len_4b = (uint32) mainrdata_len;
864  *(scratch++) = (char) XLR_BLOCK_ID_DATA_LONG;
865  memcpy(scratch, &mainrdata_len_4b, sizeof(uint32));
866  scratch += sizeof(uint32);
867  }
868  else
869  {
870  *(scratch++) = (char) XLR_BLOCK_ID_DATA_SHORT;
871  *(scratch++) = (uint8) mainrdata_len;
872  }
873  rdt_datas_last->next = mainrdata_head;
874  rdt_datas_last = mainrdata_last;
875  total_len += mainrdata_len;
876  }
877  rdt_datas_last->next = NULL;
878 
879  hdr_rdt.len = (scratch - hdr_scratch);
880  total_len += hdr_rdt.len;
881 
882  /*
883  * Calculate CRC of the data
884  *
885  * Note that the record header isn't added into the CRC initially since we
886  * don't know the prev-link yet. Thus, the CRC will represent the CRC of
887  * the whole record in the order: rdata, then backup blocks, then record
888  * header.
889  */
890  INIT_CRC32C(rdata_crc);
892  for (rdt = hdr_rdt.next; rdt != NULL; rdt = rdt->next)
893  COMP_CRC32C(rdata_crc, rdt->data, rdt->len);
894 
895  /*
896  * Ensure that the XLogRecord is not too large.
897  *
898  * XLogReader machinery is only able to handle records up to a certain
899  * size (ignoring machine resource limitations), so make sure that we will
900  * not emit records larger than the sizes advertised to be supported. This
901  * cap is based on DecodeXLogRecordRequiredSpace().
902  */
903  if (total_len >= XLogRecordMaxSize)
904  ereport(ERROR,
905  (errmsg_internal("oversized WAL record"),
906  errdetail_internal("WAL record would be %llu bytes (of maximum %u bytes); rmid %u flags %u.",
907  (unsigned long long) total_len, XLogRecordMaxSize, rmid, info)));
908 
909  /*
910  * Fill in the fields in the record header. Prev-link is filled in later,
911  * once we know where in the WAL the record will be inserted. The CRC does
912  * not include the record header yet.
913  */
915  rechdr->xl_tot_len = (uint32) total_len;
916  rechdr->xl_info = info;
917  rechdr->xl_rmid = rmid;
918  rechdr->xl_prev = InvalidXLogRecPtr;
919  rechdr->xl_crc = rdata_crc;
920 
921  return &hdr_rdt;
922 }
923 
924 /*
925  * Create a compressed version of a backup block image.
926  *
927  * Returns false if compression fails (i.e., compressed result is actually
928  * bigger than original). Otherwise, returns true and sets 'dlen' to
929  * the length of compressed block image.
930  */
931 static bool
932 XLogCompressBackupBlock(char *page, uint16 hole_offset, uint16 hole_length,
933  char *dest, uint16 *dlen)
934 {
935  int32 orig_len = BLCKSZ - hole_length;
936  int32 len = -1;
937  int32 extra_bytes = 0;
938  char *source;
939  PGAlignedBlock tmp;
940 
941  if (hole_length != 0)
942  {
943  /* must skip the hole */
944  source = tmp.data;
945  memcpy(source, page, hole_offset);
946  memcpy(source + hole_offset,
947  page + (hole_offset + hole_length),
948  BLCKSZ - (hole_length + hole_offset));
949 
950  /*
951  * Extra data needs to be stored in WAL record for the compressed
952  * version of block image if the hole exists.
953  */
955  }
956  else
957  source = page;
958 
960  {
963  break;
964 
965  case WAL_COMPRESSION_LZ4:
966 #ifdef USE_LZ4
967  len = LZ4_compress_default(source, dest, orig_len,
969  if (len <= 0)
970  len = -1; /* failure */
971 #else
972  elog(ERROR, "LZ4 is not supported by this build");
973 #endif
974  break;
975 
977 #ifdef USE_ZSTD
978  len = ZSTD_compress(dest, COMPRESS_BUFSIZE, source, orig_len,
979  ZSTD_CLEVEL_DEFAULT);
980  if (ZSTD_isError(len))
981  len = -1; /* failure */
982 #else
983  elog(ERROR, "zstd is not supported by this build");
984 #endif
985  break;
986 
988  Assert(false); /* cannot happen */
989  break;
990  /* no default case, so that compiler will warn */
991  }
992 
993  /*
994  * We recheck the actual size even if compression reports success and see
995  * if the number of bytes saved by compression is larger than the length
996  * of extra data needed for the compressed version of block image.
997  */
998  if (len >= 0 &&
999  len + extra_bytes < orig_len)
1000  {
1001  *dlen = (uint16) len; /* successful compression */
1002  return true;
1003  }
1004  return false;
1005 }
1006 
1007 /*
1008  * Determine whether the buffer referenced has to be backed up.
1009  *
1010  * Since we don't yet have the insert lock, fullPageWrites and runningBackups
1011  * (which forces full-page writes) could change later, so the result should
1012  * be used for optimization purposes only.
1013  */
1014 bool
1016 {
1018  bool doPageWrites;
1019  Page page;
1020 
1022 
1023  page = BufferGetPage(buffer);
1024 
1025  if (doPageWrites && PageGetLSN(page) <= RedoRecPtr)
1026  return true; /* buffer requires backup */
1027 
1028  return false; /* buffer does not need to be backed up */
1029 }
1030 
1031 /*
1032  * Write a backup block if needed when we are setting a hint. Note that
1033  * this may be called for a variety of page types, not just heaps.
1034  *
1035  * Callable while holding just share lock on the buffer content.
1036  *
1037  * We can't use the plain backup block mechanism since that relies on the
1038  * Buffer being exclusively locked. Since some modifications (setting LSN, hint
1039  * bits) are allowed in a sharelocked buffer that can lead to wal checksum
1040  * failures. So instead we copy the page and insert the copied data as normal
1041  * record data.
1042  *
1043  * We only need to do something if page has not yet been full page written in
1044  * this checkpoint round. The LSN of the inserted wal record is returned if we
1045  * had to write, InvalidXLogRecPtr otherwise.
1046  *
1047  * It is possible that multiple concurrent backends could attempt to write WAL
1048  * records. In that case, multiple copies of the same block would be recorded
1049  * in separate WAL records by different backends, though that is still OK from
1050  * a correctness perspective.
1051  */
1052 XLogRecPtr
1053 XLogSaveBufferForHint(Buffer buffer, bool buffer_std)
1054 {
1055  XLogRecPtr recptr = InvalidXLogRecPtr;
1056  XLogRecPtr lsn;
1058 
1059  /*
1060  * Ensure no checkpoint can change our view of RedoRecPtr.
1061  */
1063 
1064  /*
1065  * Update RedoRecPtr so that we can make the right decision
1066  */
1068 
1069  /*
1070  * We assume page LSN is first data on *every* page that can be passed to
1071  * XLogInsert, whether it has the standard page layout or not. Since we're
1072  * only holding a share-lock on the page, we must take the buffer header
1073  * lock when we look at the LSN.
1074  */
1075  lsn = BufferGetLSNAtomic(buffer);
1076 
1077  if (lsn <= RedoRecPtr)
1078  {
1079  int flags = 0;
1080  PGAlignedBlock copied_buffer;
1081  char *origdata = (char *) BufferGetBlock(buffer);
1082  RelFileLocator rlocator;
1083  ForkNumber forkno;
1084  BlockNumber blkno;
1085 
1086  /*
1087  * Copy buffer so we don't have to worry about concurrent hint bit or
1088  * lsn updates. We assume pd_lower/upper cannot be changed without an
1089  * exclusive lock, so the contents bkp are not racy.
1090  */
1091  if (buffer_std)
1092  {
1093  /* Assume we can omit data between pd_lower and pd_upper */
1094  Page page = BufferGetPage(buffer);
1095  uint16 lower = ((PageHeader) page)->pd_lower;
1096  uint16 upper = ((PageHeader) page)->pd_upper;
1097 
1098  memcpy(copied_buffer.data, origdata, lower);
1099  memcpy(copied_buffer.data + upper, origdata + upper, BLCKSZ - upper);
1100  }
1101  else
1102  memcpy(copied_buffer.data, origdata, BLCKSZ);
1103 
1104  XLogBeginInsert();
1105 
1106  if (buffer_std)
1107  flags |= REGBUF_STANDARD;
1108 
1109  BufferGetTag(buffer, &rlocator, &forkno, &blkno);
1110  XLogRegisterBlock(0, &rlocator, forkno, blkno, copied_buffer.data, flags);
1111 
1112  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI_FOR_HINT);
1113  }
1114 
1115  return recptr;
1116 }
1117 
1118 /*
1119  * Write a WAL record containing a full image of a page. Caller is responsible
1120  * for writing the page to disk after calling this routine.
1121  *
1122  * Note: If you're using this function, you should be building pages in private
1123  * memory and writing them directly to smgr. If you're using buffers, call
1124  * log_newpage_buffer instead.
1125  *
1126  * If the page follows the standard page layout, with a PageHeader and unused
1127  * space between pd_lower and pd_upper, set 'page_std' to true. That allows
1128  * the unused space to be left out from the WAL record, making it smaller.
1129  */
1130 XLogRecPtr
1132  Page page, bool page_std)
1133 {
1134  int flags;
1135  XLogRecPtr recptr;
1136 
1137  flags = REGBUF_FORCE_IMAGE;
1138  if (page_std)
1139  flags |= REGBUF_STANDARD;
1140 
1141  XLogBeginInsert();
1142  XLogRegisterBlock(0, rlocator, forknum, blkno, page, flags);
1143  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
1144 
1145  /*
1146  * The page may be uninitialized. If so, we can't set the LSN because that
1147  * would corrupt the page.
1148  */
1149  if (!PageIsNew(page))
1150  {
1151  PageSetLSN(page, recptr);
1152  }
1153 
1154  return recptr;
1155 }
1156 
1157 /*
1158  * Like log_newpage(), but allows logging multiple pages in one operation.
1159  * It is more efficient than calling log_newpage() for each page separately,
1160  * because we can write multiple pages in a single WAL record.
1161  */
1162 void
1163 log_newpages(RelFileLocator *rlocator, ForkNumber forknum, int num_pages,
1164  BlockNumber *blknos, Page *pages, bool page_std)
1165 {
1166  int flags;
1167  XLogRecPtr recptr;
1168  int i;
1169  int j;
1170 
1171  flags = REGBUF_FORCE_IMAGE;
1172  if (page_std)
1173  flags |= REGBUF_STANDARD;
1174 
1175  /*
1176  * Iterate over all the pages. They are collected into batches of
1177  * XLR_MAX_BLOCK_ID pages, and a single WAL-record is written for each
1178  * batch.
1179  */
1181 
1182  i = 0;
1183  while (i < num_pages)
1184  {
1185  int batch_start = i;
1186  int nbatch;
1187 
1188  XLogBeginInsert();
1189 
1190  nbatch = 0;
1191  while (nbatch < XLR_MAX_BLOCK_ID && i < num_pages)
1192  {
1193  XLogRegisterBlock(nbatch, rlocator, forknum, blknos[i], pages[i], flags);
1194  i++;
1195  nbatch++;
1196  }
1197 
1198  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
1199 
1200  for (j = batch_start; j < i; j++)
1201  {
1202  /*
1203  * The page may be uninitialized. If so, we can't set the LSN
1204  * because that would corrupt the page.
1205  */
1206  if (!PageIsNew(pages[j]))
1207  {
1208  PageSetLSN(pages[j], recptr);
1209  }
1210  }
1211  }
1212 }
1213 
1214 /*
1215  * Write a WAL record containing a full image of a page.
1216  *
1217  * Caller should initialize the buffer and mark it dirty before calling this
1218  * function. This function will set the page LSN.
1219  *
1220  * If the page follows the standard page layout, with a PageHeader and unused
1221  * space between pd_lower and pd_upper, set 'page_std' to true. That allows
1222  * the unused space to be left out from the WAL record, making it smaller.
1223  */
1224 XLogRecPtr
1225 log_newpage_buffer(Buffer buffer, bool page_std)
1226 {
1227  Page page = BufferGetPage(buffer);
1228  RelFileLocator rlocator;
1229  ForkNumber forknum;
1230  BlockNumber blkno;
1231 
1232  /* Shared buffers should be modified in a critical section. */
1233  Assert(CritSectionCount > 0);
1234 
1235  BufferGetTag(buffer, &rlocator, &forknum, &blkno);
1236 
1237  return log_newpage(&rlocator, forknum, blkno, page, page_std);
1238 }
1239 
1240 /*
1241  * WAL-log a range of blocks in a relation.
1242  *
1243  * An image of all pages with block numbers 'startblk' <= X < 'endblk' is
1244  * written to the WAL. If the range is large, this is done in multiple WAL
1245  * records.
1246  *
1247  * If all page follows the standard page layout, with a PageHeader and unused
1248  * space between pd_lower and pd_upper, set 'page_std' to true. That allows
1249  * the unused space to be left out from the WAL records, making them smaller.
1250  *
1251  * NOTE: This function acquires exclusive-locks on the pages. Typically, this
1252  * is used on a newly-built relation, and the caller is holding a
1253  * AccessExclusiveLock on it, so no other backend can be accessing it at the
1254  * same time. If that's not the case, you must ensure that this does not
1255  * cause a deadlock through some other means.
1256  */
1257 void
1259  BlockNumber startblk, BlockNumber endblk,
1260  bool page_std)
1261 {
1262  int flags;
1263  BlockNumber blkno;
1264 
1265  flags = REGBUF_FORCE_IMAGE;
1266  if (page_std)
1267  flags |= REGBUF_STANDARD;
1268 
1269  /*
1270  * Iterate over all the pages in the range. They are collected into
1271  * batches of XLR_MAX_BLOCK_ID pages, and a single WAL-record is written
1272  * for each batch.
1273  */
1275 
1276  blkno = startblk;
1277  while (blkno < endblk)
1278  {
1279  Buffer bufpack[XLR_MAX_BLOCK_ID];
1280  XLogRecPtr recptr;
1281  int nbufs;
1282  int i;
1283 
1285 
1286  /* Collect a batch of blocks. */
1287  nbufs = 0;
1288  while (nbufs < XLR_MAX_BLOCK_ID && blkno < endblk)
1289  {
1290  Buffer buf = ReadBufferExtended(rel, forknum, blkno,
1291  RBM_NORMAL, NULL);
1292 
1294 
1295  /*
1296  * Completely empty pages are not WAL-logged. Writing a WAL record
1297  * would change the LSN, and we don't want that. We want the page
1298  * to stay empty.
1299  */
1300  if (!PageIsNew(BufferGetPage(buf)))
1301  bufpack[nbufs++] = buf;
1302  else
1304  blkno++;
1305  }
1306 
1307  /* Nothing more to do if all remaining blocks were empty. */
1308  if (nbufs == 0)
1309  break;
1310 
1311  /* Write WAL record for this batch. */
1312  XLogBeginInsert();
1313 
1315  for (i = 0; i < nbufs; i++)
1316  {
1317  XLogRegisterBuffer(i, bufpack[i], flags);
1318  MarkBufferDirty(bufpack[i]);
1319  }
1320 
1321  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
1322 
1323  for (i = 0; i < nbufs; i++)
1324  {
1325  PageSetLSN(BufferGetPage(bufpack[i]), recptr);
1326  UnlockReleaseBuffer(bufpack[i]);
1327  }
1328  END_CRIT_SECTION();
1329  }
1330 }
1331 
1332 /*
1333  * Allocate working buffers needed for WAL record construction.
1334  */
1335 void
1337 {
1338 #ifdef USE_ASSERT_CHECKING
1339 
1340  /*
1341  * Check that any records assembled can be decoded. This is capped based
1342  * on what XLogReader would require at its maximum bound. This code path
1343  * is called once per backend, more than enough for this check.
1344  */
1345  size_t max_required = DecodeXLogRecordRequiredSpace(XLogRecordMaxSize);
1346 
1347  Assert(AllocSizeIsValid(max_required));
1348 #endif
1349 
1350  /* Initialize the working areas */
1351  if (xloginsert_cxt == NULL)
1352  {
1354  "WAL record construction",
1356  }
1357 
1358  if (registered_buffers == NULL)
1359  {
1362  sizeof(registered_buffer) * (XLR_NORMAL_MAX_BLOCK_ID + 1));
1364  }
1365  if (rdatas == NULL)
1366  {
1368  sizeof(XLogRecData) * XLR_NORMAL_RDATAS);
1370  }
1371 
1372  /*
1373  * Allocate a buffer to hold the header information for a WAL record.
1374  */
1375  if (hdr_scratch == NULL)
1378 }
uint32 BlockNumber
Definition: block.h:31
int Buffer
Definition: buf.h:23
void BufferGetTag(Buffer buffer, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:3311
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:3551
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:4497
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:2111
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4715
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:755
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:355
static Block BufferGetBlock(Buffer buffer)
Definition: bufmgr.h:319
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:159
@ RBM_NORMAL
Definition: bufmgr.h:44
PageHeaderData * PageHeader
Definition: bufpage.h:170
Pointer Page
Definition: bufpage.h:78
#define SizeOfPageHeaderData
Definition: bufpage.h:213
static bool PageIsNew(Page page)
Definition: bufpage.h:230
static void PageSetLSN(Page page, XLogRecPtr lsn)
Definition: bufpage.h:388
static XLogRecPtr PageGetLSN(Page page)
Definition: bufpage.h:383
unsigned short uint16
Definition: c.h:489
unsigned int uint32
Definition: c.h:490
#define PG_UINT32_MAX
Definition: c.h:574
signed int int32
Definition: c.h:478
unsigned char uint8
Definition: c.h:488
#define MemSet(start, val, len)
Definition: c.h:1004
uint32 TransactionId
Definition: c.h:636
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1156
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1229
#define PANIC
Definition: elog.h:42
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
volatile uint32 CritSectionCount
Definition: globals.c:42
int j
Definition: isn.c:74
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
MemoryContext TopMemoryContext
Definition: mcxt.c:141
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1064
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1476
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1021
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define AllocSizeIsValid(size)
Definition: memutils.h:42
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:414
#define START_CRIT_SECTION()
Definition: miscadmin.h:148
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:121
#define END_CRIT_SECTION()
Definition: miscadmin.h:150
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:49
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:80
RepOriginId replorigin_session_origin
Definition: origin.c:156
#define InvalidRepOriginId
Definition: origin.h:33
#define XLOG_FPI
Definition: pg_control.h:78
#define XLOG_FPI_FOR_HINT
Definition: pg_control.h:77
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
const void size_t len
const void * data
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:87
static char * buf
Definition: pg_test_fsync.c:67
#define DELAY_CHKPT_START
Definition: proc.h:119
struct RelFileLocator RelFileLocator
#define RelFileLocatorEquals(locator1, locator2)
ForkNumber
Definition: relpath.h:48
uint8 RmgrId
Definition: rmgr.h:11
PGPROC * MyProc
Definition: proc.c:66
int delayChkptFlags
Definition: proc.h:231
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
char data[BLCKSZ]
Definition: c.h:1127
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:432
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:462
bool IsSubxactTopXidLogPending(void)
Definition: xact.c:550
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition: xlog.c:6054
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6024
static XLogRecPtr RedoRecPtr
Definition: xlog.c:276
XLogRecPtr XLogInsertRecord(XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi, bool topxid_included)
Definition: xlog.c:731
static bool doPageWrites
Definition: xlog.c:289
int wal_compression
Definition: xlog.c:127
bool XLogInsertAllowed(void)
Definition: xlog.c:5976
bool * wal_consistency_checking
Definition: xlog.c:129
#define XLOG_INCLUDE_ORIGIN
Definition: xlog.h:149
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 SizeOfXLogLongPHD
Definition: xlog_internal.h:69
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
void XLogRegisterData(char *data, uint32 len)
Definition: xloginsert.c:351
static XLogRecData * mainrdata_head
Definition: xloginsert.c:99
void XLogRegisterBlock(uint8 block_id, RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum, Page page, uint8 flags)
Definition: xloginsert.c:296
static int max_registered_buffers
Definition: xloginsert.c:91
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:461
static uint8 curinsert_flags
Definition: xloginsert.c:104
bool XLogCheckBufferNeedsBackup(Buffer buffer)
Definition: xloginsert.c:1015
static uint64 mainrdata_len
Definition: xloginsert.c:101
XLogRecPtr XLogSaveBufferForHint(Buffer buffer, bool buffer_std)
Definition: xloginsert.c:1053
static bool begininsert_called
Definition: xloginsert.c:133
static int max_registered_block_id
Definition: xloginsert.c:92
XLogRecPtr log_newpage(RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blkno, Page page, bool page_std)
Definition: xloginsert.c:1131
void InitXLogInsert(void)
Definition: xloginsert.c:1336
void XLogSetRecordFlags(uint8 flags)
Definition: xloginsert.c:443
static int num_rdatas
Definition: xloginsert.c:130
void log_newpages(RelFileLocator *rlocator, ForkNumber forknum, int num_pages, BlockNumber *blknos, Page *pages, bool page_std)
Definition: xloginsert.c:1163
void XLogRegisterBufData(uint8 block_id, char *data, uint32 len)
Definition: xloginsert.c:392
static XLogRecData * mainrdata_last
Definition: xloginsert.c:100
static MemoryContext xloginsert_cxt
Definition: xloginsert.c:136
void log_newpage_range(Relation rel, ForkNumber forknum, BlockNumber startblk, BlockNumber endblk, bool page_std)
Definition: xloginsert.c:1258
void XLogResetInsertion(void)
Definition: xloginsert.c:223
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1225
static XLogRecData hdr_rdt
Definition: xloginsert.c:114
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:243
static XLogRecData * XLogRecordAssemble(RmgrId rmid, uint8 info, XLogRecPtr RedoRecPtr, bool doPageWrites, XLogRecPtr *fpw_lsn, int *num_fpi, bool *topxid_included)
Definition: xloginsert.c:535
static char * hdr_scratch
Definition: xloginsert.c:115
static XLogRecData * rdatas
Definition: xloginsert.c:129
void XLogBeginInsert(void)
Definition: xloginsert.c:150
void XLogEnsureRecordSpace(int max_block_id, int ndatas)
Definition: xloginsert.c:176
#define COMPRESS_BUFSIZE
Definition: xloginsert.c:63
static registered_buffer * registered_buffers
Definition: xloginsert.c:90
static bool XLogCompressBackupBlock(char *page, uint16 hole_offset, uint16 hole_length, char *dest, uint16 *dlen)
Definition: xloginsert.c:932
static int max_rdatas
Definition: xloginsert.c:131
#define HEADER_SCRATCH_SIZE
Definition: xloginsert.c:120
#define REGBUF_STANDARD
Definition: xloginsert.h:34
#define XLR_NORMAL_MAX_BLOCK_ID
Definition: xloginsert.h:27
#define REGBUF_FORCE_IMAGE
Definition: xloginsert.h:31
#define XLR_NORMAL_RDATAS
Definition: xloginsert.h:28
#define REGBUF_NO_IMAGE
Definition: xloginsert.h:32
#define REGBUF_KEEP_DATA
Definition: xloginsert.h:35
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33
size_t DecodeXLogRecordRequiredSpace(size_t xl_tot_len)
Definition: xlogreader.c:1619
#define SizeOfXLogRecordBlockImageHeader
Definition: xlogrecord.h:153
#define XLogRecordMaxSize
Definition: xlogrecord.h:74
#define BKPIMAGE_COMPRESS_ZSTD
Definition: xlogrecord.h:162
#define BKPBLOCK_HAS_DATA
Definition: xlogrecord.h:198
#define BKPIMAGE_APPLY
Definition: xlogrecord.h:158
#define BKPIMAGE_HAS_HOLE
Definition: xlogrecord.h:157
#define XLR_BLOCK_ID_DATA_LONG
Definition: xlogrecord.h:242
#define BKPBLOCK_WILL_INIT
Definition: xlogrecord.h:199
#define XLR_RMGR_INFO_MASK
Definition: xlogrecord.h:63
#define BKPIMAGE_COMPRESS_LZ4
Definition: xlogrecord.h:161
#define XLR_BLOCK_ID_TOPLEVEL_XID
Definition: xlogrecord.h:244
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:241
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:239
#define SizeOfXLogRecordBlockCompressHeader
Definition: xlogrecord.h:177
#define BKPBLOCK_SAME_REL
Definition: xlogrecord.h:200
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:82
#define SizeOfXLogRecordBlockHeader
Definition: xlogrecord.h:115
#define BKPIMAGE_COMPRESS_PGLZ
Definition: xlogrecord.h:160
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:243
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define BKPBLOCK_HAS_IMAGE
Definition: xlogrecord.h:197
#define XLR_CHECK_CONSISTENCY
Definition: xlogrecord.h:91