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-2022, 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 uint32 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  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 }
374 
375 /*
376  * Add buffer-specific data to the WAL record that's being constructed.
377  *
378  * Block_id must reference a block previously registered with
379  * XLogRegisterBuffer(). If this is called more than once for the same
380  * block_id, the data is appended.
381  *
382  * The maximum amount of data that can be registered per block is 65535
383  * bytes. That should be plenty; if you need more than BLCKSZ bytes to
384  * reconstruct the changes to the page, you might as well just log a full
385  * copy of it. (the "main data" that's not associated with a block is not
386  * limited)
387  */
388 void
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 }
421 
422 /*
423  * Set insert status flags for the upcoming WAL record.
424  *
425  * The flags that can be used here are:
426  * - XLOG_INCLUDE_ORIGIN, to determine if the replication origin should be
427  * included in the record.
428  * - XLOG_MARK_UNIMPORTANT, to signal that the record is not important for
429  * durability, which allows to avoid triggering WAL archiving and other
430  * background activity.
431  */
432 void
434 {
436  curinsert_flags |= flags;
437 }
438 
439 /*
440  * Insert an XLOG record having the specified RMID and info bytes, with the
441  * body of the record being the data and buffer references registered earlier
442  * with XLogRegister* calls.
443  *
444  * Returns XLOG pointer to end of record (beginning of next record).
445  * This can be used as LSN for data pages affected by the logged action.
446  * (LSN is the XLOG point up to which the XLOG must be flushed to disk
447  * before the data page can be written out. This implements the basic
448  * WAL rule "write the log before the data".)
449  */
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 }
508 
509 /*
510  * Assemble a WAL record from the registered data and buffers into an
511  * XLogRecData chain, ready for insertion with XLogInsertRecord().
512  *
513  * The record header fields are filled in, except for the xl_prev field. The
514  * calculated CRC does not include the record header yet.
515  *
516  * If there are any registered buffers, and a full-page image was not taken
517  * of all of them, *fpw_lsn is set to the lowest LSN among such pages. This
518  * signals that the assembled record is only good for insertion on the
519  * assumption that the RedoRecPtr and doPageWrites values were up-to-date.
520  *
521  * *topxid_included is set if the topmost transaction ID is logged with the
522  * current subtransaction.
523  */
524 static XLogRecData *
527  XLogRecPtr *fpw_lsn, int *num_fpi, bool *topxid_included)
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 }
889 
890 /*
891  * Create a compressed version of a backup block image.
892  *
893  * Returns false if compression fails (i.e., compressed result is actually
894  * bigger than original). Otherwise, returns true and sets 'dlen' to
895  * the length of compressed block image.
896  */
897 static bool
898 XLogCompressBackupBlock(char *page, uint16 hole_offset, uint16 hole_length,
899  char *dest, uint16 *dlen)
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 }
972 
973 /*
974  * Determine whether the buffer referenced has to be backed up.
975  *
976  * Since we don't yet have the insert lock, fullPageWrites and runningBackups
977  * (which forces full-page writes) could change later, so the result should
978  * be used for optimization purposes only.
979  */
980 bool
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 }
996 
997 /*
998  * Write a backup block if needed when we are setting a hint. Note that
999  * this may be called for a variety of page types, not just heaps.
1000  *
1001  * Callable while holding just share lock on the buffer content.
1002  *
1003  * We can't use the plain backup block mechanism since that relies on the
1004  * Buffer being exclusively locked. Since some modifications (setting LSN, hint
1005  * bits) are allowed in a sharelocked buffer that can lead to wal checksum
1006  * failures. So instead we copy the page and insert the copied data as normal
1007  * record data.
1008  *
1009  * We only need to do something if page has not yet been full page written in
1010  * this checkpoint round. The LSN of the inserted wal record is returned if we
1011  * had to write, InvalidXLogRecPtr otherwise.
1012  *
1013  * It is possible that multiple concurrent backends could attempt to write WAL
1014  * records. In that case, multiple copies of the same block would be recorded
1015  * in separate WAL records by different backends, though that is still OK from
1016  * a correctness perspective.
1017  */
1018 XLogRecPtr
1019 XLogSaveBufferForHint(Buffer buffer, bool buffer_std)
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 }
1083 
1084 /*
1085  * Write a WAL record containing a full image of a page. Caller is responsible
1086  * for writing the page to disk after calling this routine.
1087  *
1088  * Note: If you're using this function, you should be building pages in private
1089  * memory and writing them directly to smgr. If you're using buffers, call
1090  * log_newpage_buffer instead.
1091  *
1092  * If the page follows the standard page layout, with a PageHeader and unused
1093  * space between pd_lower and pd_upper, set 'page_std' to true. That allows
1094  * the unused space to be left out from the WAL record, making it smaller.
1095  */
1096 XLogRecPtr
1098  Page page, bool page_std)
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 }
1122 
1123 /*
1124  * Like log_newpage(), but allows logging multiple pages in one operation.
1125  * It is more efficient than calling log_newpage() for each page separately,
1126  * because we can write multiple pages in a single WAL record.
1127  */
1128 void
1129 log_newpages(RelFileLocator *rlocator, ForkNumber forknum, int num_pages,
1130  BlockNumber *blknos, Page *pages, bool page_std)
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 }
1179 
1180 /*
1181  * Write a WAL record containing a full image of a page.
1182  *
1183  * Caller should initialize the buffer and mark it dirty before calling this
1184  * function. This function will set the page LSN.
1185  *
1186  * If the page follows the standard page layout, with a PageHeader and unused
1187  * space between pd_lower and pd_upper, set 'page_std' to true. That allows
1188  * the unused space to be left out from the WAL record, making it smaller.
1189  */
1190 XLogRecPtr
1191 log_newpage_buffer(Buffer buffer, bool page_std)
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 }
1205 
1206 /*
1207  * WAL-log a range of blocks in a relation.
1208  *
1209  * An image of all pages with block numbers 'startblk' <= X < 'endblk' is
1210  * written to the WAL. If the range is large, this is done in multiple WAL
1211  * records.
1212  *
1213  * If all page follows the standard page layout, with a PageHeader and unused
1214  * space between pd_lower and pd_upper, set 'page_std' to true. That allows
1215  * the unused space to be left out from the WAL records, making them smaller.
1216  *
1217  * NOTE: This function acquires exclusive-locks on the pages. Typically, this
1218  * is used on a newly-built relation, and the caller is holding a
1219  * AccessExclusiveLock on it, so no other backend can be accessing it at the
1220  * same time. If that's not the case, you must ensure that this does not
1221  * cause a deadlock through some other means.
1222  */
1223 void
1225  BlockNumber startblk, BlockNumber endblk,
1226  bool page_std)
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 }
1293 
1294 /*
1295  * Allocate working buffers needed for WAL record construction.
1296  */
1297 void
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 }
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:2784
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:3012
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
static Page BufferGetPage(Buffer buffer)
Definition: bufmgr.h:280
static Block BufferGetBlock(Buffer buffer)
Definition: bufmgr.h:244
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:107
@ RBM_NORMAL
Definition: bufmgr.h:39
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:441
unsigned int uint32
Definition: c.h:442
signed int int32
Definition: c.h:430
unsigned char uint8
Definition: c.h:440
#define MemSet(start, val, len)
Definition: c.h:953
uint32 TransactionId
Definition: c.h:588
#define PANIC
Definition: elog.h:38
#define ERROR
Definition: elog.h:35
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:130
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1037
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1321
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:994
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:153
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:402
#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: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
#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:81
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:68
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:1079
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:431
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:461
bool IsSubxactTopXidLogPending(void)
Definition: xact.c:549
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition: xlog.c:6045
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:6015
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:5967
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:451
static uint8 curinsert_flags
Definition: xloginsert.c:104
bool XLogCheckBufferNeedsBackup(Buffer buffer)
Definition: xloginsert.c:981
XLogRecPtr XLogSaveBufferForHint(Buffer buffer, bool buffer_std)
Definition: xloginsert.c:1019
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:1097
void InitXLogInsert(void)
Definition: xloginsert.c:1298
void XLogSetRecordFlags(uint8 flags)
Definition: xloginsert.c:433
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:1129
static uint32 mainrdata_len
Definition: xloginsert.c:101
void XLogRegisterBufData(uint8 block_id, char *data, uint32 len)
Definition: xloginsert.c:389
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:1224
void XLogResetInsertion(void)
Definition: xloginsert.c:223
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1191
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:525
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:898
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
#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 XLR_RMGR_INFO_MASK
Definition: xlogrecord.h:63
#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 XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:228
#define SizeOfXLogRecordBlockCompressHeader
Definition: xlogrecord.h:166
#define BKPBLOCK_SAME_REL
Definition: xlogrecord.h:189
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:71
#define SizeOfXLogRecordBlockHeader
Definition: xlogrecord.h:104
#define BKPIMAGE_COMPRESS_PGLZ
Definition: xlogrecord.h:149
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:232
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define BKPBLOCK_HAS_IMAGE
Definition: xlogrecord.h:186
#define XLR_CHECK_CONSISTENCY
Definition: xlogrecord.h:80