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-2021, 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 #include "access/xact.h"
23 #include "access/xlog.h"
24 #include "access/xlog_internal.h"
25 #include "access/xloginsert.h"
26 #include "catalog/pg_control.h"
27 #include "common/pg_lzcompress.h"
28 #include "executor/instrument.h"
29 #include "miscadmin.h"
30 #include "pg_trace.h"
31 #include "replication/origin.h"
32 #include "storage/bufmgr.h"
33 #include "storage/proc.h"
34 #include "utils/memutils.h"
35 
36 /*
37  * Guess the maximum buffer size required to store a compressed version of
38  * backup block image.
39  */
40 #ifdef USE_LZ4
41 #include <lz4.h>
42 #define LZ4_MAX_BLCKSZ LZ4_COMPRESSBOUND(BLCKSZ)
43 #else
44 #define LZ4_MAX_BLCKSZ 0
45 #endif
46 
47 #define PGLZ_MAX_BLCKSZ PGLZ_MAX_OUTPUT(BLCKSZ)
48 
49 #define COMPRESS_BUFSIZE Max(PGLZ_MAX_BLCKSZ, LZ4_MAX_BLCKSZ)
50 
51 /*
52  * For each block reference registered with XLogRegisterBuffer, we fill in
53  * a registered_buffer struct.
54  */
55 typedef struct
56 {
57  bool in_use; /* is this slot in use? */
58  uint8 flags; /* REGBUF_* flags */
59  RelFileNode rnode; /* identifies the relation and block */
62  Page page; /* page content */
63  uint32 rdata_len; /* total length of data in rdata chain */
64  XLogRecData *rdata_head; /* head of the chain of data registered with
65  * this block */
66  XLogRecData *rdata_tail; /* last entry in the chain, or &rdata_head if
67  * empty */
68 
69  XLogRecData bkp_rdatas[2]; /* temporary rdatas used to hold references to
70  * backup block data in XLogRecordAssemble() */
71 
72  /* buffer to store a compressed version of backup block image */
73  char compressed_page[COMPRESS_BUFSIZE];
75 
77 static int max_registered_buffers; /* allocated size */
78 static int max_registered_block_id = 0; /* highest block_id + 1 currently
79  * registered */
80 
81 /*
82  * A chain of XLogRecDatas to hold the "main data" of a WAL record, registered
83  * with XLogRegisterData(...).
84  */
86 static XLogRecData *mainrdata_last = (XLogRecData *) &mainrdata_head;
87 static uint32 mainrdata_len; /* total # of bytes in chain */
88 
89 /* flags for the in-progress insertion */
91 
92 /*
93  * These are used to hold the record header while constructing a record.
94  * 'hdr_scratch' is not a plain variable, but is palloc'd at initialization,
95  * because we want it to be MAXALIGNed and padding bytes zeroed.
96  *
97  * For simplicity, it's allocated large enough to hold the headers for any
98  * WAL record.
99  */
101 static char *hdr_scratch = NULL;
102 
103 #define SizeOfXlogOrigin (sizeof(RepOriginId) + sizeof(char))
104 #define SizeOfXLogTransactionId (sizeof(TransactionId) + sizeof(char))
105 
106 #define HEADER_SCRATCH_SIZE \
107  (SizeOfXLogRecord + \
108  MaxSizeOfXLogRecordBlockHeader * (XLR_MAX_BLOCK_ID + 1) + \
109  SizeOfXLogRecordDataHeaderLong + SizeOfXlogOrigin + \
110  SizeOfXLogTransactionId)
111 
112 /*
113  * An array of XLogRecData structs, to hold registered data.
114  */
116 static int num_rdatas; /* entries currently used */
117 static int max_rdatas; /* allocated size */
118 
119 static bool begininsert_called = false;
120 
121 /* Memory context to hold the registered buffer and data references. */
123 
124 static XLogRecData *XLogRecordAssemble(RmgrId rmid, uint8 info,
126  XLogRecPtr *fpw_lsn, int *num_fpi);
127 static bool XLogCompressBackupBlock(char *page, uint16 hole_offset,
128  uint16 hole_length, char *dest, uint16 *dlen);
129 
130 /*
131  * Begin constructing a WAL record. This must be called before the
132  * XLogRegister* functions and XLogInsert().
133  */
134 void
136 {
138  Assert(mainrdata_last == (XLogRecData *) &mainrdata_head);
139  Assert(mainrdata_len == 0);
140 
141  /* cross-check on whether we should be here or not */
142  if (!XLogInsertAllowed())
143  elog(ERROR, "cannot make new WAL entries during recovery");
144 
145  if (begininsert_called)
146  elog(ERROR, "XLogBeginInsert was already called");
147 
148  begininsert_called = true;
149 }
150 
151 /*
152  * Ensure that there are enough buffer and data slots in the working area,
153  * for subsequent XLogRegisterBuffer, XLogRegisterData and XLogRegisterBufData
154  * calls.
155  *
156  * There is always space for a small number of buffers and data chunks, enough
157  * for most record types. This function is for the exceptional cases that need
158  * more.
159  */
160 void
161 XLogEnsureRecordSpace(int max_block_id, int ndatas)
162 {
163  int nbuffers;
164 
165  /*
166  * This must be called before entering a critical section, because
167  * allocating memory inside a critical section can fail. repalloc() will
168  * check the same, but better to check it here too so that we fail
169  * consistently even if the arrays happen to be large enough already.
170  */
171  Assert(CritSectionCount == 0);
172 
173  /* the minimum values can't be decreased */
174  if (max_block_id < XLR_NORMAL_MAX_BLOCK_ID)
175  max_block_id = XLR_NORMAL_MAX_BLOCK_ID;
176  if (ndatas < XLR_NORMAL_RDATAS)
177  ndatas = XLR_NORMAL_RDATAS;
178 
179  if (max_block_id > XLR_MAX_BLOCK_ID)
180  elog(ERROR, "maximum number of WAL record block references exceeded");
181  nbuffers = max_block_id + 1;
182 
183  if (nbuffers > max_registered_buffers)
184  {
185  registered_buffers = (registered_buffer *)
186  repalloc(registered_buffers, sizeof(registered_buffer) * nbuffers);
187 
188  /*
189  * At least the padding bytes in the structs must be zeroed, because
190  * they are included in WAL data, but initialize it all for tidiness.
191  */
192  MemSet(&registered_buffers[max_registered_buffers], 0,
193  (nbuffers - max_registered_buffers) * sizeof(registered_buffer));
194  max_registered_buffers = nbuffers;
195  }
196 
197  if (ndatas > max_rdatas)
198  {
199  rdatas = (XLogRecData *) repalloc(rdatas, sizeof(XLogRecData) * ndatas);
200  max_rdatas = ndatas;
201  }
202 }
203 
204 /*
205  * Reset WAL record construction buffers.
206  */
207 void
209 {
210  int i;
211 
212  /* reset the subxact assignment flag (if needed) */
215 
216  for (i = 0; i < max_registered_block_id; i++)
217  registered_buffers[i].in_use = false;
218 
219  num_rdatas = 0;
220  max_registered_block_id = 0;
221  mainrdata_len = 0;
222  mainrdata_last = (XLogRecData *) &mainrdata_head;
223  curinsert_flags = 0;
224  begininsert_called = false;
225 }
226 
227 /*
228  * Register a reference to a buffer with the WAL record being constructed.
229  * This must be called for every page that the WAL-logged operation modifies.
230  */
231 void
232 XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
233 {
234  registered_buffer *regbuf;
235 
236  /* NO_IMAGE doesn't make sense with FORCE_IMAGE */
237  Assert(!((flags & REGBUF_FORCE_IMAGE) && (flags & (REGBUF_NO_IMAGE))));
239 
240  if (block_id >= max_registered_block_id)
241  {
242  if (block_id >= max_registered_buffers)
243  elog(ERROR, "too many registered buffers");
244  max_registered_block_id = block_id + 1;
245  }
246 
247  regbuf = &registered_buffers[block_id];
248 
249  BufferGetTag(buffer, &regbuf->rnode, &regbuf->forkno, &regbuf->block);
250  regbuf->page = BufferGetPage(buffer);
251  regbuf->flags = flags;
252  regbuf->rdata_tail = (XLogRecData *) &regbuf->rdata_head;
253  regbuf->rdata_len = 0;
254 
255  /*
256  * Check that this page hasn't already been registered with some other
257  * block_id.
258  */
259 #ifdef USE_ASSERT_CHECKING
260  {
261  int i;
262 
263  for (i = 0; i < max_registered_block_id; i++)
264  {
265  registered_buffer *regbuf_old = &registered_buffers[i];
266 
267  if (i == block_id || !regbuf_old->in_use)
268  continue;
269 
270  Assert(!RelFileNodeEquals(regbuf_old->rnode, regbuf->rnode) ||
271  regbuf_old->forkno != regbuf->forkno ||
272  regbuf_old->block != regbuf->block);
273  }
274  }
275 #endif
276 
277  regbuf->in_use = true;
278 }
279 
280 /*
281  * Like XLogRegisterBuffer, but for registering a block that's not in the
282  * shared buffer pool (i.e. when you don't have a Buffer for it).
283  */
284 void
285 XLogRegisterBlock(uint8 block_id, RelFileNode *rnode, ForkNumber forknum,
286  BlockNumber blknum, Page page, uint8 flags)
287 {
288  registered_buffer *regbuf;
289 
291 
292  if (block_id >= max_registered_block_id)
293  max_registered_block_id = block_id + 1;
294 
295  if (block_id >= max_registered_buffers)
296  elog(ERROR, "too many registered buffers");
297 
298  regbuf = &registered_buffers[block_id];
299 
300  regbuf->rnode = *rnode;
301  regbuf->forkno = forknum;
302  regbuf->block = blknum;
303  regbuf->page = page;
304  regbuf->flags = flags;
305  regbuf->rdata_tail = (XLogRecData *) &regbuf->rdata_head;
306  regbuf->rdata_len = 0;
307 
308  /*
309  * Check that this page hasn't already been registered with some other
310  * block_id.
311  */
312 #ifdef USE_ASSERT_CHECKING
313  {
314  int i;
315 
316  for (i = 0; i < max_registered_block_id; i++)
317  {
318  registered_buffer *regbuf_old = &registered_buffers[i];
319 
320  if (i == block_id || !regbuf_old->in_use)
321  continue;
322 
323  Assert(!RelFileNodeEquals(regbuf_old->rnode, regbuf->rnode) ||
324  regbuf_old->forkno != regbuf->forkno ||
325  regbuf_old->block != regbuf->block);
326  }
327  }
328 #endif
329 
330  regbuf->in_use = true;
331 }
332 
333 /*
334  * Add data to the WAL record that's being constructed.
335  *
336  * The data is appended to the "main chunk", available at replay with
337  * XLogRecGetData().
338  */
339 void
340 XLogRegisterData(char *data, int len)
341 {
342  XLogRecData *rdata;
343 
345 
346  if (num_rdatas >= max_rdatas)
347  elog(ERROR, "too much WAL data");
348  rdata = &rdatas[num_rdatas++];
349 
350  rdata->data = data;
351  rdata->len = len;
352 
353  /*
354  * we use the mainrdata_last pointer to track the end of the chain, so no
355  * need to clear 'next' here.
356  */
357 
358  mainrdata_last->next = rdata;
359  mainrdata_last = rdata;
360 
361  mainrdata_len += len;
362 }
363 
364 /*
365  * Add buffer-specific data to the WAL record that's being constructed.
366  *
367  * Block_id must reference a block previously registered with
368  * XLogRegisterBuffer(). If this is called more than once for the same
369  * block_id, the data is appended.
370  *
371  * The maximum amount of data that can be registered per block is 65535
372  * bytes. That should be plenty; if you need more than BLCKSZ bytes to
373  * reconstruct the changes to the page, you might as well just log a full
374  * copy of it. (the "main data" that's not associated with a block is not
375  * limited)
376  */
377 void
378 XLogRegisterBufData(uint8 block_id, char *data, int len)
379 {
380  registered_buffer *regbuf;
381  XLogRecData *rdata;
382 
384 
385  /* find the registered buffer struct */
386  regbuf = &registered_buffers[block_id];
387  if (!regbuf->in_use)
388  elog(ERROR, "no block with id %d registered with WAL insertion",
389  block_id);
390 
391  if (num_rdatas >= max_rdatas)
392  elog(ERROR, "too much WAL data");
393  rdata = &rdatas[num_rdatas++];
394 
395  rdata->data = data;
396  rdata->len = len;
397 
398  regbuf->rdata_tail->next = rdata;
399  regbuf->rdata_tail = rdata;
400  regbuf->rdata_len += len;
401 }
402 
403 /*
404  * Set insert status flags for the upcoming WAL record.
405  *
406  * The flags that can be used here are:
407  * - XLOG_INCLUDE_ORIGIN, to determine if the replication origin should be
408  * included in the record.
409  * - XLOG_MARK_UNIMPORTANT, to signal that the record is not important for
410  * durability, which allows to avoid triggering WAL archiving and other
411  * background activity.
412  */
413 void
415 {
417  curinsert_flags |= flags;
418 }
419 
420 /*
421  * Insert an XLOG record having the specified RMID and info bytes, with the
422  * body of the record being the data and buffer references registered earlier
423  * with XLogRegister* calls.
424  *
425  * Returns XLOG pointer to end of record (beginning of next record).
426  * This can be used as LSN for data pages affected by the logged action.
427  * (LSN is the XLOG point up to which the XLOG must be flushed to disk
428  * before the data page can be written out. This implements the basic
429  * WAL rule "write the log before the data".)
430  */
433 {
434  XLogRecPtr EndPos;
435 
436  /* XLogBeginInsert() must have been called. */
437  if (!begininsert_called)
438  elog(ERROR, "XLogBeginInsert was not called");
439 
440  /*
441  * The caller can set rmgr bits, XLR_SPECIAL_REL_UPDATE and
442  * XLR_CHECK_CONSISTENCY; the rest are reserved for use by me.
443  */
444  if ((info & ~(XLR_RMGR_INFO_MASK |
446  XLR_CHECK_CONSISTENCY)) != 0)
447  elog(PANIC, "invalid xlog info mask %02X", info);
448 
449  TRACE_POSTGRESQL_WAL_INSERT(rmid, info);
450 
451  /*
452  * In bootstrap mode, we don't actually log anything but XLOG resources;
453  * return a phony record pointer.
454  */
455  if (IsBootstrapProcessingMode() && rmid != RM_XLOG_ID)
456  {
458  EndPos = SizeOfXLogLongPHD; /* start of 1st chkpt record */
459  return EndPos;
460  }
461 
462  do
463  {
465  bool doPageWrites;
466  XLogRecPtr fpw_lsn;
467  XLogRecData *rdt;
468  int num_fpi = 0;
469 
470  /*
471  * Get values needed to decide whether to do full-page writes. Since
472  * we don't yet have an insertion lock, these could change under us,
473  * but XLogInsertRecord will recheck them once it has a lock.
474  */
475  GetFullPageWriteInfo(&RedoRecPtr, &doPageWrites);
476 
477  rdt = XLogRecordAssemble(rmid, info, RedoRecPtr, doPageWrites,
478  &fpw_lsn, &num_fpi);
479 
480  EndPos = XLogInsertRecord(rdt, fpw_lsn, curinsert_flags, num_fpi);
481  } while (EndPos == InvalidXLogRecPtr);
482 
484 
485  return EndPos;
486 }
487 
488 /*
489  * Assemble a WAL record from the registered data and buffers into an
490  * XLogRecData chain, ready for insertion with XLogInsertRecord().
491  *
492  * The record header fields are filled in, except for the xl_prev field. The
493  * calculated CRC does not include the record header yet.
494  *
495  * If there are any registered buffers, and a full-page image was not taken
496  * of all of them, *fpw_lsn is set to the lowest LSN among such pages. This
497  * signals that the assembled record is only good for insertion on the
498  * assumption that the RedoRecPtr and doPageWrites values were up-to-date.
499  */
500 static XLogRecData *
503  XLogRecPtr *fpw_lsn, int *num_fpi)
504 {
505  XLogRecData *rdt;
506  uint32 total_len = 0;
507  int block_id;
508  pg_crc32c rdata_crc;
509  registered_buffer *prev_regbuf = NULL;
510  XLogRecData *rdt_datas_last;
511  XLogRecord *rechdr;
512  char *scratch = hdr_scratch;
513 
514  /*
515  * Note: this function can be called multiple times for the same record.
516  * All the modifications we do to the rdata chains below must handle that.
517  */
518 
519  /* The record begins with the fixed-size header */
520  rechdr = (XLogRecord *) scratch;
521  scratch += SizeOfXLogRecord;
522 
523  hdr_rdt.next = NULL;
524  rdt_datas_last = &hdr_rdt;
525  hdr_rdt.data = hdr_scratch;
526 
527  /*
528  * Enforce consistency checks for this record if user is looking for it.
529  * Do this before at the beginning of this routine to give the possibility
530  * for callers of XLogInsert() to pass XLR_CHECK_CONSISTENCY directly for
531  * a record.
532  */
533  if (wal_consistency_checking[rmid])
534  info |= XLR_CHECK_CONSISTENCY;
535 
536  /*
537  * Make an rdata chain containing all the data portions of all block
538  * references. This includes the data for full-page images. Also append
539  * the headers for the block references in the scratch buffer.
540  */
541  *fpw_lsn = InvalidXLogRecPtr;
542  for (block_id = 0; block_id < max_registered_block_id; block_id++)
543  {
544  registered_buffer *regbuf = &registered_buffers[block_id];
545  bool needs_backup;
546  bool needs_data;
549  XLogRecordBlockCompressHeader cbimg = {0};
550  bool samerel;
551  bool is_compressed = false;
552  bool include_image;
553 
554  if (!regbuf->in_use)
555  continue;
556 
557  /* Determine if this block needs to be backed up */
558  if (regbuf->flags & REGBUF_FORCE_IMAGE)
559  needs_backup = true;
560  else if (regbuf->flags & REGBUF_NO_IMAGE)
561  needs_backup = false;
562  else if (!doPageWrites)
563  needs_backup = false;
564  else
565  {
566  /*
567  * We assume page LSN is first data on *every* page that can be
568  * passed to XLogInsert, whether it has the standard page layout
569  * or not.
570  */
571  XLogRecPtr page_lsn = PageGetLSN(regbuf->page);
572 
573  needs_backup = (page_lsn <= RedoRecPtr);
574  if (!needs_backup)
575  {
576  if (*fpw_lsn == InvalidXLogRecPtr || page_lsn < *fpw_lsn)
577  *fpw_lsn = page_lsn;
578  }
579  }
580 
581  /* Determine if the buffer data needs to included */
582  if (regbuf->rdata_len == 0)
583  needs_data = false;
584  else if ((regbuf->flags & REGBUF_KEEP_DATA) != 0)
585  needs_data = true;
586  else
587  needs_data = !needs_backup;
588 
589  bkpb.id = block_id;
590  bkpb.fork_flags = regbuf->forkno;
591  bkpb.data_length = 0;
592 
593  if ((regbuf->flags & REGBUF_WILL_INIT) == REGBUF_WILL_INIT)
595 
596  /*
597  * If needs_backup is true or WAL checking is enabled for current
598  * resource manager, log a full-page write for the current block.
599  */
600  include_image = needs_backup || (info & XLR_CHECK_CONSISTENCY) != 0;
601 
602  if (include_image)
603  {
604  Page page = regbuf->page;
605  uint16 compressed_len = 0;
606 
607  /*
608  * The page needs to be backed up, so calculate its hole length
609  * and offset.
610  */
611  if (regbuf->flags & REGBUF_STANDARD)
612  {
613  /* Assume we can omit data between pd_lower and pd_upper */
614  uint16 lower = ((PageHeader) page)->pd_lower;
615  uint16 upper = ((PageHeader) page)->pd_upper;
616 
617  if (lower >= SizeOfPageHeaderData &&
618  upper > lower &&
619  upper <= BLCKSZ)
620  {
621  bimg.hole_offset = lower;
622  cbimg.hole_length = upper - lower;
623  }
624  else
625  {
626  /* No "hole" to remove */
627  bimg.hole_offset = 0;
628  cbimg.hole_length = 0;
629  }
630  }
631  else
632  {
633  /* Not a standard page header, don't try to eliminate "hole" */
634  bimg.hole_offset = 0;
635  cbimg.hole_length = 0;
636  }
637 
638  /*
639  * Try to compress a block image if wal_compression is enabled
640  */
642  {
643  is_compressed =
645  cbimg.hole_length,
646  regbuf->compressed_page,
647  &compressed_len);
648  }
649 
650  /*
651  * Fill in the remaining fields in the XLogRecordBlockHeader
652  * struct
653  */
655 
656  /* Report a full page image constructed for the WAL record */
657  *num_fpi += 1;
658 
659  /*
660  * Construct XLogRecData entries for the page content.
661  */
662  rdt_datas_last->next = &regbuf->bkp_rdatas[0];
663  rdt_datas_last = rdt_datas_last->next;
664 
665  bimg.bimg_info = (cbimg.hole_length == 0) ? 0 : BKPIMAGE_HAS_HOLE;
666 
667  /*
668  * If WAL consistency checking is enabled for the resource manager
669  * of this WAL record, a full-page image is included in the record
670  * for the block modified. During redo, the full-page is replayed
671  * only if BKPIMAGE_APPLY is set.
672  */
673  if (needs_backup)
674  bimg.bimg_info |= BKPIMAGE_APPLY;
675 
676  if (is_compressed)
677  {
678  /* The current compression is stored in the WAL record */
679  bimg.length = compressed_len;
680 
681  /* Set the compression method used for this block */
683  {
686  break;
687 
688  case WAL_COMPRESSION_LZ4:
689 #ifdef USE_LZ4
691 #else
692  elog(ERROR, "LZ4 is not supported by this build");
693 #endif
694  break;
695 
697  Assert(false); /* cannot happen */
698  break;
699  /* no default case, so that compiler will warn */
700  }
701 
702  rdt_datas_last->data = regbuf->compressed_page;
703  rdt_datas_last->len = compressed_len;
704  }
705  else
706  {
707  bimg.length = BLCKSZ - cbimg.hole_length;
708 
709  if (cbimg.hole_length == 0)
710  {
711  rdt_datas_last->data = page;
712  rdt_datas_last->len = BLCKSZ;
713  }
714  else
715  {
716  /* must skip the hole */
717  rdt_datas_last->data = page;
718  rdt_datas_last->len = bimg.hole_offset;
719 
720  rdt_datas_last->next = &regbuf->bkp_rdatas[1];
721  rdt_datas_last = rdt_datas_last->next;
722 
723  rdt_datas_last->data =
724  page + (bimg.hole_offset + cbimg.hole_length);
725  rdt_datas_last->len =
726  BLCKSZ - (bimg.hole_offset + cbimg.hole_length);
727  }
728  }
729 
730  total_len += bimg.length;
731  }
732 
733  if (needs_data)
734  {
735  /*
736  * Link the caller-supplied rdata chain for this buffer to the
737  * overall list.
738  */
740  bkpb.data_length = regbuf->rdata_len;
741  total_len += regbuf->rdata_len;
742 
743  rdt_datas_last->next = regbuf->rdata_head;
744  rdt_datas_last = regbuf->rdata_tail;
745  }
746 
747  if (prev_regbuf && RelFileNodeEquals(regbuf->rnode, prev_regbuf->rnode))
748  {
749  samerel = true;
751  }
752  else
753  samerel = false;
754  prev_regbuf = regbuf;
755 
756  /* Ok, copy the header to the scratch buffer */
757  memcpy(scratch, &bkpb, SizeOfXLogRecordBlockHeader);
758  scratch += SizeOfXLogRecordBlockHeader;
759  if (include_image)
760  {
761  memcpy(scratch, &bimg, SizeOfXLogRecordBlockImageHeader);
763  if (cbimg.hole_length != 0 && is_compressed)
764  {
765  memcpy(scratch, &cbimg,
768  }
769  }
770  if (!samerel)
771  {
772  memcpy(scratch, &regbuf->rnode, sizeof(RelFileNode));
773  scratch += sizeof(RelFileNode);
774  }
775  memcpy(scratch, &regbuf->block, sizeof(BlockNumber));
776  scratch += sizeof(BlockNumber);
777  }
778 
779  /* followed by the record's origin, if any */
782  {
783  *(scratch++) = (char) XLR_BLOCK_ID_ORIGIN;
784  memcpy(scratch, &replorigin_session_origin, sizeof(replorigin_session_origin));
785  scratch += sizeof(replorigin_session_origin);
786  }
787 
788  /* followed by toplevel XID, if not already included in previous record */
790  {
792 
793  /* update the flag (later used by XLogResetInsertion) */
795 
796  *(scratch++) = (char) XLR_BLOCK_ID_TOPLEVEL_XID;
797  memcpy(scratch, &xid, sizeof(TransactionId));
798  scratch += sizeof(TransactionId);
799  }
800 
801  /* followed by main data, if any */
802  if (mainrdata_len > 0)
803  {
804  if (mainrdata_len > 255)
805  {
806  *(scratch++) = (char) XLR_BLOCK_ID_DATA_LONG;
807  memcpy(scratch, &mainrdata_len, sizeof(uint32));
808  scratch += sizeof(uint32);
809  }
810  else
811  {
812  *(scratch++) = (char) XLR_BLOCK_ID_DATA_SHORT;
813  *(scratch++) = (uint8) mainrdata_len;
814  }
815  rdt_datas_last->next = mainrdata_head;
816  rdt_datas_last = mainrdata_last;
817  total_len += mainrdata_len;
818  }
819  rdt_datas_last->next = NULL;
820 
821  hdr_rdt.len = (scratch - hdr_scratch);
822  total_len += hdr_rdt.len;
823 
824  /*
825  * Calculate CRC of the data
826  *
827  * Note that the record header isn't added into the CRC initially since we
828  * don't know the prev-link yet. Thus, the CRC will represent the CRC of
829  * the whole record in the order: rdata, then backup blocks, then record
830  * header.
831  */
832  INIT_CRC32C(rdata_crc);
834  for (rdt = hdr_rdt.next; rdt != NULL; rdt = rdt->next)
835  COMP_CRC32C(rdata_crc, rdt->data, rdt->len);
836 
837  /*
838  * Fill in the fields in the record header. Prev-link is filled in later,
839  * once we know where in the WAL the record will be inserted. The CRC does
840  * not include the record header yet.
841  */
843  rechdr->xl_tot_len = total_len;
844  rechdr->xl_info = info;
845  rechdr->xl_rmid = rmid;
846  rechdr->xl_prev = InvalidXLogRecPtr;
847  rechdr->xl_crc = rdata_crc;
848 
849  return &hdr_rdt;
850 }
851 
852 /*
853  * Create a compressed version of a backup block image.
854  *
855  * Returns false if compression fails (i.e., compressed result is actually
856  * bigger than original). Otherwise, returns true and sets 'dlen' to
857  * the length of compressed block image.
858  */
859 static bool
860 XLogCompressBackupBlock(char *page, uint16 hole_offset, uint16 hole_length,
861  char *dest, uint16 *dlen)
862 {
863  int32 orig_len = BLCKSZ - hole_length;
864  int32 len = -1;
865  int32 extra_bytes = 0;
866  char *source;
867  PGAlignedBlock tmp;
868 
869  if (hole_length != 0)
870  {
871  /* must skip the hole */
872  source = tmp.data;
873  memcpy(source, page, hole_offset);
874  memcpy(source + hole_offset,
875  page + (hole_offset + hole_length),
876  BLCKSZ - (hole_length + hole_offset));
877 
878  /*
879  * Extra data needs to be stored in WAL record for the compressed
880  * version of block image if the hole exists.
881  */
883  }
884  else
885  source = page;
886 
888  {
890  len = pglz_compress(source, orig_len, dest, PGLZ_strategy_default);
891  break;
892 
893  case WAL_COMPRESSION_LZ4:
894 #ifdef USE_LZ4
895  len = LZ4_compress_default(source, dest, orig_len,
897  if (len <= 0)
898  len = -1; /* failure */
899 #else
900  elog(ERROR, "LZ4 is not supported by this build");
901 #endif
902  break;
903 
905  Assert(false); /* cannot happen */
906  break;
907  /* no default case, so that compiler will warn */
908  }
909 
910  /*
911  * We recheck the actual size even if compression reports success and see
912  * if the number of bytes saved by compression is larger than the length
913  * of extra data needed for the compressed version of block image.
914  */
915  if (len >= 0 &&
916  len + extra_bytes < orig_len)
917  {
918  *dlen = (uint16) len; /* successful compression */
919  return true;
920  }
921  return false;
922 }
923 
924 /*
925  * Determine whether the buffer referenced has to be backed up.
926  *
927  * Since we don't yet have the insert lock, fullPageWrites and forcePageWrites
928  * could change later, so the result should be used for optimization purposes
929  * only.
930  */
931 bool
933 {
935  bool doPageWrites;
936  Page page;
937 
938  GetFullPageWriteInfo(&RedoRecPtr, &doPageWrites);
939 
940  page = BufferGetPage(buffer);
941 
942  if (doPageWrites && PageGetLSN(page) <= RedoRecPtr)
943  return true; /* buffer requires backup */
944 
945  return false; /* buffer does not need to be backed up */
946 }
947 
948 /*
949  * Write a backup block if needed when we are setting a hint. Note that
950  * this may be called for a variety of page types, not just heaps.
951  *
952  * Callable while holding just share lock on the buffer content.
953  *
954  * We can't use the plain backup block mechanism since that relies on the
955  * Buffer being exclusively locked. Since some modifications (setting LSN, hint
956  * bits) are allowed in a sharelocked buffer that can lead to wal checksum
957  * failures. So instead we copy the page and insert the copied data as normal
958  * record data.
959  *
960  * We only need to do something if page has not yet been full page written in
961  * this checkpoint round. The LSN of the inserted wal record is returned if we
962  * had to write, InvalidXLogRecPtr otherwise.
963  *
964  * It is possible that multiple concurrent backends could attempt to write WAL
965  * records. In that case, multiple copies of the same block would be recorded
966  * in separate WAL records by different backends, though that is still OK from
967  * a correctness perspective.
968  */
970 XLogSaveBufferForHint(Buffer buffer, bool buffer_std)
971 {
972  XLogRecPtr recptr = InvalidXLogRecPtr;
973  XLogRecPtr lsn;
975 
976  /*
977  * Ensure no checkpoint can change our view of RedoRecPtr.
978  */
980 
981  /*
982  * Update RedoRecPtr so that we can make the right decision
983  */
984  RedoRecPtr = GetRedoRecPtr();
985 
986  /*
987  * We assume page LSN is first data on *every* page that can be passed to
988  * XLogInsert, whether it has the standard page layout or not. Since we're
989  * only holding a share-lock on the page, we must take the buffer header
990  * lock when we look at the LSN.
991  */
992  lsn = BufferGetLSNAtomic(buffer);
993 
994  if (lsn <= RedoRecPtr)
995  {
996  int flags = 0;
997  PGAlignedBlock copied_buffer;
998  char *origdata = (char *) BufferGetBlock(buffer);
999  RelFileNode rnode;
1000  ForkNumber forkno;
1001  BlockNumber blkno;
1002 
1003  /*
1004  * Copy buffer so we don't have to worry about concurrent hint bit or
1005  * lsn updates. We assume pd_lower/upper cannot be changed without an
1006  * exclusive lock, so the contents bkp are not racy.
1007  */
1008  if (buffer_std)
1009  {
1010  /* Assume we can omit data between pd_lower and pd_upper */
1011  Page page = BufferGetPage(buffer);
1012  uint16 lower = ((PageHeader) page)->pd_lower;
1013  uint16 upper = ((PageHeader) page)->pd_upper;
1014 
1015  memcpy(copied_buffer.data, origdata, lower);
1016  memcpy(copied_buffer.data + upper, origdata + upper, BLCKSZ - upper);
1017  }
1018  else
1019  memcpy(copied_buffer.data, origdata, BLCKSZ);
1020 
1021  XLogBeginInsert();
1022 
1023  if (buffer_std)
1024  flags |= REGBUF_STANDARD;
1025 
1026  BufferGetTag(buffer, &rnode, &forkno, &blkno);
1027  XLogRegisterBlock(0, &rnode, forkno, blkno, copied_buffer.data, flags);
1028 
1029  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI_FOR_HINT);
1030  }
1031 
1032  return recptr;
1033 }
1034 
1035 /*
1036  * Write a WAL record containing a full image of a page. Caller is responsible
1037  * for writing the page to disk after calling this routine.
1038  *
1039  * Note: If you're using this function, you should be building pages in private
1040  * memory and writing them directly to smgr. If you're using buffers, call
1041  * log_newpage_buffer instead.
1042  *
1043  * If the page follows the standard page layout, with a PageHeader and unused
1044  * space between pd_lower and pd_upper, set 'page_std' to true. That allows
1045  * the unused space to be left out from the WAL record, making it smaller.
1046  */
1047 XLogRecPtr
1049  Page page, bool page_std)
1050 {
1051  int flags;
1052  XLogRecPtr recptr;
1053 
1054  flags = REGBUF_FORCE_IMAGE;
1055  if (page_std)
1056  flags |= REGBUF_STANDARD;
1057 
1058  XLogBeginInsert();
1059  XLogRegisterBlock(0, rnode, forkNum, blkno, page, flags);
1060  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
1061 
1062  /*
1063  * The page may be uninitialized. If so, we can't set the LSN because that
1064  * would corrupt the page.
1065  */
1066  if (!PageIsNew(page))
1067  {
1068  PageSetLSN(page, recptr);
1069  }
1070 
1071  return recptr;
1072 }
1073 
1074 /*
1075  * Like log_newpage(), but allows logging multiple pages in one operation.
1076  * It is more efficient than calling log_newpage() for each page separately,
1077  * because we can write multiple pages in a single WAL record.
1078  */
1079 void
1080 log_newpages(RelFileNode *rnode, ForkNumber forkNum, int num_pages,
1081  BlockNumber *blknos, Page *pages, bool page_std)
1082 {
1083  int flags;
1084  XLogRecPtr recptr;
1085  int i;
1086  int j;
1087 
1088  flags = REGBUF_FORCE_IMAGE;
1089  if (page_std)
1090  flags |= REGBUF_STANDARD;
1091 
1092  /*
1093  * Iterate over all the pages. They are collected into batches of
1094  * XLR_MAX_BLOCK_ID pages, and a single WAL-record is written for each
1095  * batch.
1096  */
1098 
1099  i = 0;
1100  while (i < num_pages)
1101  {
1102  int batch_start = i;
1103  int nbatch;
1104 
1105  XLogBeginInsert();
1106 
1107  nbatch = 0;
1108  while (nbatch < XLR_MAX_BLOCK_ID && i < num_pages)
1109  {
1110  XLogRegisterBlock(nbatch, rnode, forkNum, blknos[i], pages[i], flags);
1111  i++;
1112  nbatch++;
1113  }
1114 
1115  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
1116 
1117  for (j = batch_start; j < i; j++)
1118  {
1119  /*
1120  * The page may be uninitialized. If so, we can't set the LSN
1121  * because that would corrupt the page.
1122  */
1123  if (!PageIsNew(pages[j]))
1124  {
1125  PageSetLSN(pages[j], recptr);
1126  }
1127  }
1128  }
1129 }
1130 
1131 /*
1132  * Write a WAL record containing a full image of a page.
1133  *
1134  * Caller should initialize the buffer and mark it dirty before calling this
1135  * function. This function will set the page LSN.
1136  *
1137  * If the page follows the standard page layout, with a PageHeader and unused
1138  * space between pd_lower and pd_upper, set 'page_std' to true. That allows
1139  * the unused space to be left out from the WAL record, making it smaller.
1140  */
1141 XLogRecPtr
1142 log_newpage_buffer(Buffer buffer, bool page_std)
1143 {
1144  Page page = BufferGetPage(buffer);
1145  RelFileNode rnode;
1146  ForkNumber forkNum;
1147  BlockNumber blkno;
1148 
1149  /* Shared buffers should be modified in a critical section. */
1150  Assert(CritSectionCount > 0);
1151 
1152  BufferGetTag(buffer, &rnode, &forkNum, &blkno);
1153 
1154  return log_newpage(&rnode, forkNum, blkno, page, page_std);
1155 }
1156 
1157 /*
1158  * WAL-log a range of blocks in a relation.
1159  *
1160  * An image of all pages with block numbers 'startblk' <= X < 'endblk' is
1161  * written to the WAL. If the range is large, this is done in multiple WAL
1162  * records.
1163  *
1164  * If all page follows the standard page layout, with a PageHeader and unused
1165  * space between pd_lower and pd_upper, set 'page_std' to true. That allows
1166  * the unused space to be left out from the WAL records, making them smaller.
1167  *
1168  * NOTE: This function acquires exclusive-locks on the pages. Typically, this
1169  * is used on a newly-built relation, and the caller is holding a
1170  * AccessExclusiveLock on it, so no other backend can be accessing it at the
1171  * same time. If that's not the case, you must ensure that this does not
1172  * cause a deadlock through some other means.
1173  */
1174 void
1176  BlockNumber startblk, BlockNumber endblk,
1177  bool page_std)
1178 {
1179  int flags;
1180  BlockNumber blkno;
1181 
1182  flags = REGBUF_FORCE_IMAGE;
1183  if (page_std)
1184  flags |= REGBUF_STANDARD;
1185 
1186  /*
1187  * Iterate over all the pages in the range. They are collected into
1188  * batches of XLR_MAX_BLOCK_ID pages, and a single WAL-record is written
1189  * for each batch.
1190  */
1192 
1193  blkno = startblk;
1194  while (blkno < endblk)
1195  {
1196  Buffer bufpack[XLR_MAX_BLOCK_ID];
1197  XLogRecPtr recptr;
1198  int nbufs;
1199  int i;
1200 
1202 
1203  /* Collect a batch of blocks. */
1204  nbufs = 0;
1205  while (nbufs < XLR_MAX_BLOCK_ID && blkno < endblk)
1206  {
1207  Buffer buf = ReadBufferExtended(rel, forkNum, blkno,
1208  RBM_NORMAL, NULL);
1209 
1211 
1212  /*
1213  * Completely empty pages are not WAL-logged. Writing a WAL record
1214  * would change the LSN, and we don't want that. We want the page
1215  * to stay empty.
1216  */
1217  if (!PageIsNew(BufferGetPage(buf)))
1218  bufpack[nbufs++] = buf;
1219  else
1220  UnlockReleaseBuffer(buf);
1221  blkno++;
1222  }
1223 
1224  /* Write WAL record for this batch. */
1225  XLogBeginInsert();
1226 
1228  for (i = 0; i < nbufs; i++)
1229  {
1230  XLogRegisterBuffer(i, bufpack[i], flags);
1231  MarkBufferDirty(bufpack[i]);
1232  }
1233 
1234  recptr = XLogInsert(RM_XLOG_ID, XLOG_FPI);
1235 
1236  for (i = 0; i < nbufs; i++)
1237  {
1238  PageSetLSN(BufferGetPage(bufpack[i]), recptr);
1239  UnlockReleaseBuffer(bufpack[i]);
1240  }
1241  END_CRIT_SECTION();
1242  }
1243 }
1244 
1245 /*
1246  * Allocate working buffers needed for WAL record construction.
1247  */
1248 void
1250 {
1251  /* Initialize the working areas */
1252  if (xloginsert_cxt == NULL)
1253  {
1254  xloginsert_cxt = AllocSetContextCreate(TopMemoryContext,
1255  "WAL record construction",
1257  }
1258 
1259  if (registered_buffers == NULL)
1260  {
1261  registered_buffers = (registered_buffer *)
1262  MemoryContextAllocZero(xloginsert_cxt,
1263  sizeof(registered_buffer) * (XLR_NORMAL_MAX_BLOCK_ID + 1));
1265  }
1266  if (rdatas == NULL)
1267  {
1268  rdatas = MemoryContextAlloc(xloginsert_cxt,
1269  sizeof(XLogRecData) * XLR_NORMAL_RDATAS);
1271  }
1272 
1273  /*
1274  * Allocate a buffer to hold the header information for a WAL record.
1275  */
1276  if (hdr_scratch == NULL)
1277  hdr_scratch = MemoryContextAllocZero(xloginsert_cxt,
1279 }
void XLogRegisterBufData(uint8 block_id, char *data, int len)
Definition: xloginsert.c:378
static XLogRecData hdr_rdt
Definition: xloginsert.c:100
#define INIT_CRC32C(crc)
Definition: pg_crc32c.h:41
void MarkSubTransactionAssigned(void)
Definition: xact.c:6124
XLogRecPtr xl_prev
Definition: xlogrecord.h:45
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
#define AllocSetContextCreate
Definition: memutils.h:173
#define BKPIMAGE_HAS_HOLE
Definition: xlogrecord.h:146
XLogRecPtr log_newpage_buffer(Buffer buffer, bool page_std)
Definition: xloginsert.c:1142
static uint32 mainrdata_len
Definition: xloginsert.c:87
#define XLR_SPECIAL_REL_UPDATE
Definition: xlogrecord.h:71
#define HEADER_SCRATCH_SIZE
Definition: xloginsert.c:106
uint32 TransactionId
Definition: c.h:587
Datum lower(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:46
#define XLR_BLOCK_ID_DATA_LONG
Definition: xlogrecord.h:228
#define COMPRESS_BUFSIZE
Definition: xloginsert.c:49
void MarkBufferDirty(Buffer buffer)
Definition: bufmgr.c:1556
PGPROC * MyProc
Definition: proc.c:68
void XLogRegisterBuffer(uint8 block_id, Buffer buffer, uint8 flags)
Definition: xloginsert.c:232
uint32 pg_crc32c
Definition: pg_crc32c.h:38
#define XLR_NORMAL_RDATAS
Definition: xloginsert.h:28
void InitXLogInsert(void)
Definition: xloginsert.c:1249
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:741
XLogRecPtr XLogSaveBufferForHint(Buffer buffer, bool buffer_std)
Definition: xloginsert.c:970
#define END_CRIT_SECTION()
Definition: miscadmin.h:149
BlockNumber block
Definition: xloginsert.c:61
static bool begininsert_called
Definition: xloginsert.c:119
unsigned char uint8
Definition: c.h:439
#define REGBUF_WILL_INIT
Definition: xloginsert.h:33
#define START_CRIT_SECTION()
Definition: miscadmin.h:147
#define MemSet(start, val, len)
Definition: c.h:1008
RmgrId xl_rmid
Definition: xlogrecord.h:47
#define XLOG_INCLUDE_ORIGIN
Definition: xlog.h:213
uint32 BlockNumber
Definition: block.h:31
Datum upper(PG_FUNCTION_ARGS)
Definition: oracle_compat.c:77
XLogRecData * rdata_head
Definition: xloginsert.c:64
#define BUFFER_LOCK_EXCLUSIVE
Definition: bufmgr.h:98
#define SizeOfPageHeaderData
Definition: bufpage.h:216
#define PANIC
Definition: elog.h:50
#define XLR_CHECK_CONSISTENCY
Definition: xlogrecord.h:80
signed int int32
Definition: c.h:429
RelFileNode rnode
Definition: xloginsert.c:59
char data[BLCKSZ]
Definition: c.h:1141
void XLogRegisterBlock(uint8 block_id, RelFileNode *rnode, ForkNumber forknum, BlockNumber blknum, Page page, uint8 flags)
Definition: xloginsert.c:285
unsigned short uint16
Definition: c.h:440
static bool doPageWrites
Definition: xlog.c:364
void UnlockReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3795
#define ERROR
Definition: elog.h:46
#define XLR_BLOCK_ID_TOPLEVEL_XID
Definition: xlogrecord.h:230
bool delayChkpt
Definition: proc.h:187
static int max_registered_buffers
Definition: xloginsert.c:77
void XLogResetInsertion(void)
Definition: xloginsert.c:208
uint32 xl_tot_len
Definition: xlogrecord.h:43
static XLogRecData * XLogRecordAssemble(RmgrId rmid, uint8 info, XLogRecPtr RedoRecPtr, bool doPageWrites, XLogRecPtr *fpw_lsn, int *num_fpi)
Definition: xloginsert.c:501
#define SizeOfXLogRecordBlockImageHeader
Definition: xlogrecord.h:142
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:195
static XLogRecPtr RedoRecPtr
Definition: xlog.c:357
#define BKPIMAGE_APPLY
Definition: xlogrecord.h:147
XLogRecPtr BufferGetLSNAtomic(Buffer buffer)
Definition: bufmgr.c:3012
struct RelFileNode RelFileNode
static char * buf
Definition: pg_test_fsync.c:68
static XLogRecData * mainrdata_head
Definition: xloginsert.c:85
#define REGBUF_STANDARD
Definition: xloginsert.h:35
TransactionId GetCurrentTransactionIdIfAny(void)
Definition: xact.c:455
unsigned int uint32
Definition: c.h:441
TransactionId GetTopTransactionIdIfAny(void)
Definition: xact.c:425
void XLogSetRecordFlags(uint8 flags)
Definition: xloginsert.c:414
volatile uint32 CritSectionCount
Definition: globals.c:41
#define BufferGetPage(buffer)
Definition: bufmgr.h:169
#define BKPBLOCK_WILL_INIT
Definition: xlogrecord.h:186
static int max_rdatas
Definition: xloginsert.c:117
XLogRecData * rdata_tail
Definition: xloginsert.c:66
bool XLogInsertAllowed(void)
Definition: xlog.c:8330
MemoryContext TopMemoryContext
Definition: mcxt.c:48
ForkNumber
Definition: relpath.h:40
#define XLR_MAX_BLOCK_ID
Definition: xlogrecord.h:225
static registered_buffer * registered_buffers
Definition: xloginsert.c:76
int32 pglz_compress(const char *source, int32 slen, char *dest, const PGLZ_Strategy *strategy)
#define SizeOfXLogRecord
Definition: xlogrecord.h:55
#define REGBUF_FORCE_IMAGE
Definition: xloginsert.h:31
void XLogRegisterData(char *data, int len)
Definition: xloginsert.c:340
XLogRecPtr XLogInsert(RmgrId rmid, uint8 info)
Definition: xloginsert.c:432
bool XLogCheckBufferNeedsBackup(Buffer buffer)
Definition: xloginsert.c:932
int wal_compression
Definition: xlog.c:101
#define BKPBLOCK_SAME_REL
Definition: xlogrecord.h:187
#define REGBUF_KEEP_DATA
Definition: xloginsert.h:38
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:4011
#define BKPBLOCK_HAS_IMAGE
Definition: xlogrecord.h:184
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
bool IsSubTransactionAssignmentPending(void)
Definition: xact.c:6096
#define XLOG_INCLUDE_XID
Definition: xlog.h:215
uint8 RmgrId
Definition: rmgr.h:11
WalCompression
Definition: xlog.h:129
static XLogRecData * mainrdata_last
Definition: xloginsert.c:86
#define XLR_NORMAL_MAX_BLOCK_ID
Definition: xloginsert.h:27
PageHeaderData * PageHeader
Definition: bufpage.h:166
static int num_rdatas
Definition: xloginsert.c:116
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define Assert(condition)
Definition: c.h:804
RepOriginId replorigin_session_origin
Definition: origin.c:154
bool * wal_consistency_checking
Definition: xlog.c:103
#define XLOG_FPI_FOR_HINT
Definition: pg_control.h:77
static rewind_source * source
Definition: pg_rewind.c:79
uint8 xl_info
Definition: xlogrecord.h:46
#define REGBUF_NO_IMAGE
Definition: xloginsert.h:32
void XLogEnsureRecordSpace(int max_block_id, int ndatas)
Definition: xloginsert.c:161
#define XLR_BLOCK_ID_ORIGIN
Definition: xlogrecord.h:229
ForkNumber forkno
Definition: xloginsert.c:60
char compressed_page[COMPRESS_BUFSIZE]
Definition: xloginsert.c:73
XLogRecPtr GetRedoRecPtr(void)
Definition: xlog.c:8509
pg_crc32c xl_crc
Definition: xlogrecord.h:49
struct XLogRecData * next
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1182
#define InvalidRepOriginId
Definition: origin.h:33
#define PageGetLSN(page)
Definition: bufpage.h:366
#define XLR_BLOCK_ID_DATA_SHORT
Definition: xlogrecord.h:227
static char * hdr_scratch
Definition: xloginsert.c:101
void log_newpage_range(Relation rel, ForkNumber forkNum, BlockNumber startblk, BlockNumber endblk, bool page_std)
Definition: xloginsert.c:1175
#define IsBootstrapProcessingMode()
Definition: miscadmin.h:406
void log_newpages(RelFileNode *rnode, ForkNumber forkNum, int num_pages, BlockNumber *blknos, Page *pages, bool page_std)
Definition: xloginsert.c:1080
TransactionId xl_xid
Definition: xlogrecord.h:44
#define BKPIMAGE_COMPRESS_LZ4
Definition: xlogrecord.h:151
static int max_registered_block_id
Definition: xloginsert.c:78
#define XLOG_FPI
Definition: pg_control.h:78
#define PageIsNew(page)
Definition: bufpage.h:229
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:863
void GetFullPageWriteInfo(XLogRecPtr *RedoRecPtr_p, bool *doPageWrites_p)
Definition: xlog.c:8538
#define BKPIMAGE_COMPRESS_PGLZ
Definition: xlogrecord.h:150
XLogRecPtr log_newpage(RelFileNode *rnode, ForkNumber forkNum, BlockNumber blkno, Page page, bool page_std)
Definition: xloginsert.c:1048
#define elog(elevel,...)
Definition: elog.h:232
int i
XLogRecPtr XLogInsertRecord(XLogRecData *rdata, XLogRecPtr fpw_lsn, uint8 flags, int num_fpi)
Definition: xlog.c:998
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:120
const PGLZ_Strategy *const PGLZ_strategy_default
static bool XLogCompressBackupBlock(char *page, uint16 hole_offset, uint16 hole_length, char *dest, uint16 *dlen)
Definition: xloginsert.c:860
static uint8 curinsert_flags
Definition: xloginsert.c:90
XLogRecData bkp_rdatas[2]
Definition: xloginsert.c:69
void BufferGetTag(Buffer buffer, RelFileNode *rnode, ForkNumber *forknum, BlockNumber *blknum)
Definition: bufmgr.c:2773
#define COMP_CRC32C(crc, data, len)
Definition: pg_crc32c.h:89
void XLogBeginInsert(void)
Definition: xloginsert.c:135
#define PageSetLSN(page, lsn)
Definition: bufpage.h:368
int Buffer
Definition: buf.h:23
#define BufferGetBlock(buffer)
Definition: bufmgr.h:136
#define SizeOfXLogRecordBlockCompressHeader
Definition: xlogrecord.h:164
static MemoryContext xloginsert_cxt
Definition: xloginsert.c:122
Pointer Page
Definition: bufpage.h:78
#define RelFileNodeEquals(node1, node2)
Definition: relfilenode.h:88
static XLogRecData * rdatas
Definition: xloginsert.c:115
#define BKPBLOCK_HAS_DATA
Definition: xlogrecord.h:185
#define SizeOfXLogLongPHD
Definition: xlog_internal.h:69
#define XLR_RMGR_INFO_MASK
Definition: xlogrecord.h:63
#define SizeOfXLogRecordBlockHeader
Definition: xlogrecord.h:104