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