PostgreSQL Source Code git master
Loading...
Searching...
No Matches
basebackup.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * basebackup.c
4 * code for taking a base backup and streaming it to a standby
5 *
6 * Portions Copyright (c) 2010-2026, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/backend/backup/basebackup.c
10 *
11 *-------------------------------------------------------------------------
12 */
13#include "postgres.h"
14
15#include <sys/stat.h>
16#include <unistd.h>
17#include <time.h>
18
20#include "access/xlogbackup.h"
22#include "backup/basebackup.h"
26#include "catalog/pg_tablespace_d.h"
27#include "commands/defrem.h"
28#include "common/compression.h"
29#include "common/file_perm.h"
30#include "common/file_utils.h"
31#include "lib/stringinfo.h"
32#include "miscadmin.h"
33#include "nodes/pg_list.h"
34#include "pgstat.h"
35#include "pgtar.h"
38#include "replication/slot.h"
41#include "storage/bufpage.h"
42#include "storage/checksum.h"
43#include "storage/dsm_impl.h"
44#include "storage/ipc.h"
45#include "storage/reinit.h"
46#include "utils/builtins.h"
47#include "utils/guc.h"
48#include "utils/ps_status.h"
49#include "utils/relcache.h"
50#include "utils/resowner.h"
51#include "utils/wait_event.h"
52
53/*
54 * How much data do we want to send in one CopyData message? Note that
55 * this may also result in reading the underlying files in chunks of this
56 * size.
57 *
58 * NB: The buffer size is required to be a multiple of the system block
59 * size, so use that value instead if it's bigger than our preference.
60 */
61#define SINK_BUFFER_LENGTH Max(32768, BLCKSZ)
62
81
82#define TAR_NUM_TERMINATION_BLOCKS 2
83
85 "BLCKSZ too small for " CppAsString2(TAR_NUM_TERMINATION_BLOCKS) " tar termination blocks");
86
87static int64 sendTablespace(bbsink *sink, char *path, Oid spcoid, bool sizeonly,
90static int64 sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly,
91 List *tablespaces, bool sendtblspclinks,
94static bool sendFile(bbsink *sink, const char *readfilename, const char *tarfilename,
95 struct stat *statbuf, bool missing_ok,
96 Oid dboid, Oid spcoid, RelFileNumber relfilenumber,
97 unsigned segno,
101 unsigned truncation_block_length);
103 const char *readfilename, int fd,
104 off_t offset, size_t length,
105 BlockNumber blkno,
106 bool verify_checksum,
107 int *checksum_failures);
108static void push_to_sink(bbsink *sink, pg_checksum_context *checksum_ctx,
109 size_t *bytes_done, void *data, size_t length);
110static bool verify_page_checksum(Page page, XLogRecPtr start_lsn,
111 BlockNumber blkno,
113static void sendFileWithContent(bbsink *sink, const char *filename,
114 const char *content, int len,
116static int64 _tarWriteHeader(bbsink *sink, const char *filename,
117 const char *linktarget, struct stat *statbuf,
118 bool sizeonly);
119static void _tarWritePadding(bbsink *sink, int len);
120static void convert_link_to_directory(const char *pathbuf, struct stat *statbuf);
124static int compareWalFileNames(const ListCell *a, const ListCell *b);
125static ssize_t basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset,
126 const char *filename, bool partial_read_ok);
127
128/* Was the backup currently in-progress initiated in recovery mode? */
129static bool backup_started_in_recovery = false;
130
131/* Total number of checksum failures during base backup. */
132static long long int total_checksum_failures;
133
134/* Do not verify checksums. */
135static bool noverify_checksums = false;
136
137/*
138 * Definition of one element part of an exclusion list, used for paths part
139 * of checksum validation or base backups. "name" is the name of the file
140 * or path to check for exclusion. If "match_prefix" is true, any items
141 * matching the name as prefix are excluded.
142 */
144{
145 const char *name;
147};
148
149/*
150 * The contents of these directories are removed or recreated during server
151 * start so they are not included in backups. The directories themselves are
152 * kept and included as empty to preserve access permissions.
153 *
154 * Note: this list should be kept in sync with the filter lists in pg_rewind's
155 * filemap.c.
156 */
157static const char *const excludeDirContents[] =
158{
159 /*
160 * Skip temporary statistics files. PG_STAT_TMP_DIR must be skipped
161 * because extensions like pg_stat_statements store data there.
162 */
164
165 /*
166 * It is generally not useful to backup the contents of this directory
167 * even if the intention is to restore to another primary. See backup.sgml
168 * for a more detailed description.
169 */
171
172 /* Contents removed on startup, see dsm_cleanup_for_mmap(). */
174
175 /* Contents removed on startup, see AsyncShmemInit(). */
176 "pg_notify",
177
178 /*
179 * Old contents are loaded for possible debugging but are not required for
180 * normal operation, see SerialInit().
181 */
182 "pg_serial",
183
184 /* Contents removed on startup, see DeleteAllExportedSnapshotFiles(). */
185 "pg_snapshots",
186
187 /* Contents zeroed on startup, see StartupSUBTRANS(). */
188 "pg_subtrans",
189
190 /* end of list */
191 NULL
192};
193
194/*
195 * List of files excluded from backups.
196 */
197static const struct exclude_list_item excludeFiles[] =
198{
199 /* Skip auto conf temporary file. */
200 {PG_AUTOCONF_FILENAME ".tmp", false},
201
202 /* Skip current log file temporary file */
204
205 /*
206 * Skip relation cache because it is rebuilt on startup. This includes
207 * temporary files.
208 */
210
211 /*
212 * backup_label and tablespace_map should not exist in a running cluster
213 * capable of doing an online backup, but exclude them just in case.
214 */
215 {BACKUP_LABEL_FILE, false},
216 {TABLESPACE_MAP, false},
217
218 /*
219 * If there's a backup_manifest, it belongs to a backup that was used to
220 * start this server. It is *not* correct for this backup. Our
221 * backup_manifest is injected into the backup separately if users want
222 * it.
223 */
224 {"backup_manifest", false},
225
226 {"postmaster.pid", false},
227 {"postmaster.opts", false},
228
229 /* end of list */
230 {NULL, false}
231};
232
233/*
234 * Actually do a base backup for the specified tablespaces.
235 *
236 * This is split out mainly to avoid complaints about "variable might be
237 * clobbered by longjmp" from stupider versions of gcc.
238 */
239static void
242{
244 XLogRecPtr endptr;
249
250 /* Initial backup state, insofar as we know it now. */
251 state.tablespaces = NIL;
252 state.tablespace_num = 0;
253 state.bytes_done = 0;
254 state.bytes_total = 0;
255 state.bytes_total_is_valid = false;
256
257 /* we're going to use a BufFile, so we need a ResourceOwner */
262
264
267
269
270 /* Allocate backup related variables. */
273
275 do_pg_backup_start(opt->label, opt->fastcheckpoint, &state.tablespaces,
277
278 state.startptr = backup_state->startpoint;
279 state.starttli = backup_state->starttli;
280
281 /*
282 * Once do_pg_backup_start has been called, ensure that any failure causes
283 * us to abort the backup so we don't "leak" a backup counter. For this
284 * reason, *all* functionality between do_pg_backup_start() and the end of
285 * do_pg_backup_stop() should be inside the error cleanup block!
286 */
287
289 {
290 ListCell *lc;
292
293 /* If this is an incremental backup, execute preparatory steps. */
294 if (ib != NULL)
296
297 /* Add a node for the base directory at the end */
299 newti->size = -1;
300 state.tablespaces = lappend(state.tablespaces, newti);
301
302 /*
303 * Calculate the total backup size by summing up the size of each
304 * tablespace
305 */
306 if (opt->progress)
307 {
309
310 foreach(lc, state.tablespaces)
311 {
313
314 if (tmp->path == NULL)
315 tmp->size = sendDir(sink, ".", 1, true, state.tablespaces,
316 true, NULL, InvalidOid, NULL);
317 else
318 tmp->size = sendTablespace(sink, tmp->path, tmp->oid, true,
319 NULL, NULL);
320 state.bytes_total += tmp->size;
321 }
322 state.bytes_total_is_valid = true;
323 }
324
325 /* notify basebackup sink about start of backup */
327
328 /* Send off our tablespaces one by one */
329 foreach(lc, state.tablespaces)
330 {
332
333 if (ti->path == NULL)
334 {
335 struct stat statbuf;
336 bool sendtblspclinks = true;
337 char *backup_label;
338
339 bbsink_begin_archive(sink, "base.tar");
340
341 /* In the main tar, include the backup_label first... */
344 backup_label, -1, &manifest);
346
347 /* Then the tablespace_map file, if required... */
348 if (opt->sendtblspcmapfile)
349 {
352 sendtblspclinks = false;
353 }
354
355 /* Then the bulk of the files... */
356 sendDir(sink, ".", 1, false, state.tablespaces,
358
359 /* ... and pg_control after everything else. */
360 if (lstat(XLOG_CONTROL_FILE, &statbuf) != 0)
363 errmsg("could not stat file \"%s\": %m",
366 false, InvalidOid, InvalidOid,
368 }
369 else
370 {
371 char *archive_name = psprintf("%u.tar", ti->oid);
372
373 bbsink_begin_archive(sink, archive_name);
374
375 sendTablespace(sink, ti->path, ti->oid, false, &manifest, ib);
376 }
377
378 /*
379 * If we're including WAL, and this is the main data directory we
380 * don't treat this as the end of the tablespace. Instead, we will
381 * include the xlog files below and stop afterwards. This is safe
382 * since the main data directory is always sent *last*.
383 */
384 if (opt->includewal && ti->path == NULL)
385 {
386 Assert(lnext(state.tablespaces, lc) == NULL);
387 }
388 else
389 {
390 /* Properly terminate the tarfile. */
393
394 /* OK, that's the end of the archive. */
396 }
397 }
398
401
402 endptr = backup_state->stoppoint;
404
405 /* Deallocate backup-related variables. */
408 }
410
411
412 if (opt->includewal)
413 {
414 /*
415 * We've left the last tar file "open", so we can now append the
416 * required WAL files to it.
417 */
418 char pathbuf[MAXPGPATH];
419 XLogSegNo segno;
422 struct stat statbuf;
425 char firstoff[MAXFNAMELEN];
426 char lastoff[MAXFNAMELEN];
427 DIR *dir;
428 struct dirent *de;
429 ListCell *lc;
430 TimeLineID tli;
431
433
434 /*
435 * I'd rather not worry about timelines here, so scan pg_wal and
436 * include all WAL files in the range between 'startptr' and 'endptr',
437 * regardless of the timeline the file is stamped with. If there are
438 * some spurious WAL files belonging to timelines that don't belong in
439 * this server's history, they will be included too. Normally there
440 * shouldn't be such files, but if there are, there's little harm in
441 * including them.
442 */
447
448 dir = AllocateDir("pg_wal");
449 while ((de = ReadDir(dir, "pg_wal")) != NULL)
450 {
451 /* Does it look like a WAL segment, and is it in the range? */
452 if (IsXLogFileName(de->d_name) &&
453 strcmp(de->d_name + 8, firstoff + 8) >= 0 &&
454 strcmp(de->d_name + 8, lastoff + 8) <= 0)
455 {
457 }
458 /* Does it look like a timeline history file? */
459 else if (IsTLHistoryFileName(de->d_name))
460 {
462 }
463 }
464 FreeDir(dir);
465
466 /*
467 * Before we go any further, check that none of the WAL segments we
468 * need were removed.
469 */
471
472 /*
473 * Sort the WAL filenames. We want to send the files in order from
474 * oldest to newest, to reduce the chance that a file is recycled
475 * before we get a chance to send it over.
476 */
478
479 /*
480 * There must be at least one xlog file in the pg_wal directory, since
481 * we are doing backup-including-xlog.
482 */
483 if (walFileList == NIL)
485 (errmsg("could not find any WAL files")));
486
487 /*
488 * Sanity check: the first and last segment should cover startptr and
489 * endptr, with no gaps in between.
490 */
492 &tli, &segno, wal_segment_size);
493 if (segno != startsegno)
494 {
496
500 (errmsg("could not find WAL file \"%s\"", startfname)));
501 }
502 foreach(lc, walFileList)
503 {
504 char *walFileName = (char *) lfirst(lc);
505 XLogSegNo currsegno = segno;
506 XLogSegNo nextsegno = segno + 1;
507
509 if (!(nextsegno == segno || currsegno == segno))
510 {
512
515 (errmsg("could not find WAL file \"%s\"", nextfname)));
516 }
517 }
518 if (segno != endsegno)
519 {
520 char endfname[MAXFNAMELEN];
521
524 (errmsg("could not find WAL file \"%s\"", endfname)));
525 }
526
527 /* Ok, we have everything we need. Send the WAL files. */
528 foreach(lc, walFileList)
529 {
530 char *walFileName = (char *) lfirst(lc);
531 int fd;
532 ssize_t cnt;
533 pgoff_t len = 0;
534
537
539 if (fd < 0)
540 {
541 int save_errno = errno;
542
543 /*
544 * Most likely reason for this is that the file was already
545 * removed by a checkpoint, so check for that to get a better
546 * error message.
547 */
548 CheckXLogRemoved(segno, tli);
549
553 errmsg("could not open file \"%s\": %m", pathbuf)));
554 }
555
556 if (fstat(fd, &statbuf) != 0)
559 errmsg("could not stat file \"%s\": %m",
560 pathbuf)));
561 if (statbuf.st_size != wal_segment_size)
562 {
563 CheckXLogRemoved(segno, tli);
566 errmsg("unexpected WAL file size \"%s\"", walFileName)));
567 }
568
569 /* send the WAL file itself */
571
572 while ((cnt = basebackup_read_file(fd, sink->bbs_buffer,
573 Min(sink->bbs_buffer_length,
575 len, pathbuf, true)) > 0)
576 {
577 CheckXLogRemoved(segno, tli);
579
580 len += cnt;
581
582 if (len == wal_segment_size)
583 break;
584 }
585
586 if (len != wal_segment_size)
587 {
588 CheckXLogRemoved(segno, tli);
591 errmsg("unexpected WAL file size \"%s\"", walFileName)));
592 }
593
594 /*
595 * wal_segment_size is a multiple of TAR_BLOCK_SIZE, so no need
596 * for padding.
597 */
599
601
602 /*
603 * Mark file as archived, otherwise files can get archived again
604 * after promotion of a new node. This is in line with
605 * walreceiver.c always doing an XLogArchiveForceDone() after a
606 * complete segment.
607 */
610 }
611
612 /*
613 * Send timeline history files too. Only the latest timeline history
614 * file is required for recovery, and even that only if there happens
615 * to be a timeline switch in the first WAL segment that contains the
616 * checkpoint record, or if we're taking a base backup from a standby
617 * server and the target timeline changes while the backup is taken.
618 * But they are small and highly useful for debugging purposes, so
619 * better include them all, always.
620 */
621 foreach(lc, historyFileList)
622 {
623 char *fname = lfirst(lc);
624
625 snprintf(pathbuf, MAXPGPATH, XLOGDIR "/%s", fname);
626
627 if (lstat(pathbuf, &statbuf) != 0)
630 errmsg("could not stat file \"%s\": %m", pathbuf)));
631
634 &manifest, 0, NULL, 0);
635
636 /* unconditionally mark file as archived */
637 StatusFilePath(pathbuf, fname, ".done");
639 }
640
641 /* Properly terminate the tar file. */
644
645 /* OK, that's the end of the archive. */
647 }
648
649 AddWALInfoToBackupManifest(&manifest, state.startptr, state.starttli,
650 endptr, endtli);
651
653
654 bbsink_end_backup(sink, endptr, endtli);
655
657 {
660 (errmsg_plural("%lld total checksum verification failure",
661 "%lld total checksum verification failures",
664
667 errmsg("checksum verification failure during base backup")));
668 }
669
670 /*
671 * Make sure to free the manifest before the resource owners as manifests
672 * use cryptohash contexts that may depend on resource owners (like
673 * OpenSSL).
674 */
676
677 /* clean up the resource owner we created */
679
681}
682
683/*
684 * list_sort comparison function, to compare log/seg portion of WAL segment
685 * filenames, ignoring the timeline portion.
686 */
687static int
689{
690 char *fna = (char *) lfirst(a);
691 char *fnb = (char *) lfirst(b);
692
693 return strcmp(fna + 8, fnb + 8);
694}
695
696/*
697 * Parse the base backup options passed down by the parser
698 */
699static void
701{
702 ListCell *lopt;
703 bool o_label = false;
704 bool o_progress = false;
705 bool o_checkpoint = false;
706 bool o_nowait = false;
707 bool o_wal = false;
708 bool o_incremental = false;
709 bool o_maxrate = false;
710 bool o_tablespace_map = false;
711 bool o_noverify_checksums = false;
712 bool o_manifest = false;
713 bool o_manifest_checksums = false;
714 bool o_target = false;
715 bool o_target_detail = false;
716 char *target_str = NULL;
717 char *target_detail_str = NULL;
718 bool o_compression = false;
719 bool o_compression_detail = false;
721
722 MemSet(opt, 0, sizeof(*opt));
727
728 foreach(lopt, options)
729 {
731
732 if (strcmp(defel->defname, "label") == 0)
733 {
734 if (o_label)
737 errmsg("duplicate option \"%s\"", defel->defname)));
738 opt->label = defGetString(defel);
739 o_label = true;
740 }
741 else if (strcmp(defel->defname, "progress") == 0)
742 {
743 if (o_progress)
746 errmsg("duplicate option \"%s\"", defel->defname)));
748 o_progress = true;
749 }
750 else if (strcmp(defel->defname, "checkpoint") == 0)
751 {
752 char *optval = defGetString(defel);
753
754 if (o_checkpoint)
757 errmsg("duplicate option \"%s\"", defel->defname)));
758 if (pg_strcasecmp(optval, "fast") == 0)
759 opt->fastcheckpoint = true;
760 else if (pg_strcasecmp(optval, "spread") == 0)
761 opt->fastcheckpoint = false;
762 else
765 errmsg("unrecognized checkpoint type: \"%s\"",
766 optval)));
767 o_checkpoint = true;
768 }
769 else if (strcmp(defel->defname, "wait") == 0)
770 {
771 if (o_nowait)
774 errmsg("duplicate option \"%s\"", defel->defname)));
775 opt->nowait = !defGetBoolean(defel);
776 o_nowait = true;
777 }
778 else if (strcmp(defel->defname, "wal") == 0)
779 {
780 if (o_wal)
783 errmsg("duplicate option \"%s\"", defel->defname)));
785 o_wal = true;
786 }
787 else if (strcmp(defel->defname, "incremental") == 0)
788 {
789 if (o_incremental)
792 errmsg("duplicate option \"%s\"", defel->defname)));
794 if (opt->incremental && !summarize_wal)
797 errmsg("incremental backups cannot be taken unless WAL summarization is enabled")));
798 o_incremental = true;
799 }
800 else if (strcmp(defel->defname, "max_rate") == 0)
801 {
803
804 if (o_maxrate)
807 errmsg("duplicate option \"%s\"", defel->defname)));
808
813 errmsg("%" PRId64 " is outside the valid range for parameter \"%s\" (%d .. %d)",
814 maxrate, "MAX_RATE", MAX_RATE_LOWER, MAX_RATE_UPPER)));
815
816 opt->maxrate = (uint32) maxrate;
817 o_maxrate = true;
818 }
819 else if (strcmp(defel->defname, "tablespace_map") == 0)
820 {
824 errmsg("duplicate option \"%s\"", defel->defname)));
826 o_tablespace_map = true;
827 }
828 else if (strcmp(defel->defname, "verify_checksums") == 0)
829 {
833 errmsg("duplicate option \"%s\"", defel->defname)));
836 }
837 else if (strcmp(defel->defname, "manifest") == 0)
838 {
839 char *optval = defGetString(defel);
840 bool manifest_bool;
841
842 if (o_manifest)
845 errmsg("duplicate option \"%s\"", defel->defname)));
847 {
848 if (manifest_bool)
850 else
852 }
853 else if (pg_strcasecmp(optval, "force-encode") == 0)
855 else
858 errmsg("unrecognized manifest option: \"%s\"",
859 optval)));
860 o_manifest = true;
861 }
862 else if (strcmp(defel->defname, "manifest_checksums") == 0)
863 {
864 char *optval = defGetString(defel);
865
869 errmsg("duplicate option \"%s\"", defel->defname)));
874 errmsg("unrecognized checksum algorithm: \"%s\"",
875 optval)));
877 }
878 else if (strcmp(defel->defname, "target") == 0)
879 {
880 if (o_target)
883 errmsg("duplicate option \"%s\"", defel->defname)));
885 o_target = true;
886 }
887 else if (strcmp(defel->defname, "target_detail") == 0)
888 {
889 char *optval = defGetString(defel);
890
891 if (o_target_detail)
894 errmsg("duplicate option \"%s\"", defel->defname)));
896 o_target_detail = true;
897 }
898 else if (strcmp(defel->defname, "compression") == 0)
899 {
900 char *optval = defGetString(defel);
901
902 if (o_compression)
905 errmsg("duplicate option \"%s\"", defel->defname)));
909 errmsg("unrecognized compression algorithm: \"%s\"",
910 optval)));
911 o_compression = true;
912 }
913 else if (strcmp(defel->defname, "compression_detail") == 0)
914 {
918 errmsg("duplicate option \"%s\"", defel->defname)));
921 }
922 else
925 errmsg("unrecognized base backup option: \"%s\"",
926 defel->defname)));
927 }
928
929 if (opt->label == NULL)
930 opt->label = "base backup";
931 if (opt->manifest == MANIFEST_OPTION_NO)
932 {
936 errmsg("manifest checksums require a backup manifest")));
938 }
939
940 if (target_str == NULL)
941 {
942 if (target_detail_str != NULL)
945 errmsg("target detail cannot be used without target")));
946 opt->use_copytblspc = true;
947 opt->send_to_client = true;
948 }
949 else if (strcmp(target_str, "client") == 0)
950 {
951 if (target_detail_str != NULL)
954 errmsg("target \"%s\" does not accept a target detail",
955 target_str)));
956 opt->send_to_client = true;
957 }
958 else
959 opt->target_handle =
961
965 errmsg("compression detail cannot be specified unless compression is enabled")));
966
967 if (o_compression)
968 {
969 char *error_detail;
970
975 if (error_detail != NULL)
978 errmsg("invalid compression specification: %s",
979 error_detail));
980 }
981}
982
983
984/*
985 * SendBaseBackup() - send a complete base backup.
986 *
987 * The function will put the system into backup mode like pg_backup_start()
988 * does, so that the backup is consistent even though we read directly from
989 * the filesystem, bypassing the buffer cache.
990 */
991void
993{
995 bbsink *sink;
997
998 if (status == SESSION_BACKUP_RUNNING)
1001 errmsg("a backup is already in progress in this session")));
1002
1004
1006
1008 {
1009 char activitymsg[50];
1010
1011 snprintf(activitymsg, sizeof(activitymsg), "sending backup \"%s\"",
1012 opt.label);
1014 }
1015
1016 /*
1017 * If we're asked to perform an incremental backup and the user has not
1018 * supplied a manifest, that's an ERROR.
1019 *
1020 * If we're asked to perform a full backup and the user did supply a
1021 * manifest, just ignore it.
1022 */
1023 if (!opt.incremental)
1024 ib = NULL;
1025 else if (ib == NULL)
1026 ereport(ERROR,
1028 errmsg("must UPLOAD_MANIFEST before performing an incremental BASE_BACKUP")));
1029
1030 /*
1031 * If the target is specifically 'client' then set up to stream the backup
1032 * to the client; otherwise, it's being sent someplace else and should not
1033 * be sent to the client. BaseBackupGetSink has the job of setting up a
1034 * sink to send the backup data wherever it needs to go.
1035 */
1037 if (opt.target_handle != NULL)
1039
1040 /* Set up network throttling, if client requested it */
1041 if (opt.maxrate > 0)
1043
1044 /* Set up server-side compression, if client requested it */
1047 else if (opt.compression == PG_COMPRESSION_LZ4)
1049 else if (opt.compression == PG_COMPRESSION_ZSTD)
1051
1052 /* Set up progress reporting. */
1054
1055 /*
1056 * Perform the base backup, but make sure we clean up the bbsink even if
1057 * an error occurs.
1058 */
1059 PG_TRY();
1060 {
1061 perform_base_backup(&opt, sink, ib);
1062 }
1063 PG_FINALLY();
1064 {
1066 }
1067 PG_END_TRY();
1068}
1069
1070/*
1071 * Inject a file with given name and content in the output tar stream.
1072 *
1073 * "len" can optionally be set to an arbitrary length of data sent. If set
1074 * to -1, the content sent is treated as a string with strlen() as length.
1075 */
1076static void
1077sendFileWithContent(bbsink *sink, const char *filename, const char *content,
1079{
1080 struct stat statbuf;
1081 int bytes_done = 0;
1082 pg_checksum_context checksum_ctx;
1083
1084 if (pg_checksum_init(&checksum_ctx, manifest->checksum_type) < 0)
1085 elog(ERROR, "could not initialize checksum of file \"%s\"",
1086 filename);
1087
1088 if (len < 0)
1089 len = strlen(content);
1090
1091 /*
1092 * Construct a stat struct for the file we're injecting in the tar.
1093 */
1094
1095 /* Windows doesn't have the concept of uid and gid */
1096#ifdef WIN32
1097 statbuf.st_uid = 0;
1098 statbuf.st_gid = 0;
1099#else
1100 statbuf.st_uid = geteuid();
1101 statbuf.st_gid = getegid();
1102#endif
1103 statbuf.st_mtime = time(NULL);
1104 statbuf.st_mode = pg_file_create_mode;
1105 statbuf.st_size = len;
1106
1108
1109 if (pg_checksum_update(&checksum_ctx, (const uint8 *) content, len) < 0)
1110 elog(ERROR, "could not update checksum of file \"%s\"",
1111 filename);
1112
1113 while (bytes_done < len)
1114 {
1115 size_t remaining = len - bytes_done;
1116 size_t nbytes = Min(sink->bbs_buffer_length, remaining);
1117
1118 memcpy(sink->bbs_buffer, content, nbytes);
1120 bytes_done += nbytes;
1121 content += nbytes;
1122 }
1123
1125
1127 (pg_time_t) statbuf.st_mtime, &checksum_ctx);
1128}
1129
1130/*
1131 * Include the tablespace directory pointed to by 'path' in the output tar
1132 * stream. If 'sizeonly' is true, we just calculate a total length and return
1133 * it, without actually sending anything.
1134 *
1135 * Only used to send auxiliary tablespaces, not PGDATA.
1136 */
1137static int64
1140{
1141 int64 size;
1142 char pathbuf[MAXPGPATH];
1143 struct stat statbuf;
1144
1145 /*
1146 * 'path' points to the tablespace location, but we only want to include
1147 * the version directory in it that belongs to us.
1148 */
1149 snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path,
1151
1152 /*
1153 * Store a directory entry in the tar file so we get the permissions
1154 * right.
1155 */
1156 if (lstat(pathbuf, &statbuf) != 0)
1157 {
1158 if (errno != ENOENT)
1159 ereport(ERROR,
1161 errmsg("could not stat file or directory \"%s\": %m",
1162 pathbuf)));
1163
1164 /* If the tablespace went away while scanning, it's no error. */
1165 return 0;
1166 }
1167
1169 sizeonly);
1170
1171 /* Send all the files in the tablespace version directory */
1172 size += sendDir(sink, pathbuf, strlen(path), sizeonly, NIL, true, manifest,
1173 spcoid, ib);
1174
1175 return size;
1176}
1177
1178/*
1179 * Include all files from the given directory in the output tar stream. If
1180 * 'sizeonly' is true, we just calculate a total length and return it, without
1181 * actually sending anything.
1182 *
1183 * Omit any directory in the tablespaces list, to avoid backing up
1184 * tablespaces twice when they were created inside PGDATA.
1185 *
1186 * If sendtblspclinks is true, we need to include symlink
1187 * information in the tar file. If not, we can skip that
1188 * as it will be sent separately in the tablespace_map file.
1189 */
1190static int64
1191sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly,
1194{
1195 DIR *dir;
1196 struct dirent *de;
1197 char pathbuf[MAXPGPATH * 2];
1198 struct stat statbuf;
1199 int64 size = 0;
1200 const char *lastDir; /* Split last dir from parent path. */
1201 bool isRelationDir = false; /* Does directory contain relations? */
1202 bool isGlobalDir = false;
1203 Oid dboid = InvalidOid;
1204 BlockNumber *relative_block_numbers = NULL;
1205
1206 /*
1207 * Since this array is relatively large, avoid putting it on the stack.
1208 * But we don't need it at all if this is not an incremental backup.
1209 */
1210 if (ib != NULL)
1211 relative_block_numbers = palloc_array(BlockNumber, RELSEG_SIZE);
1212
1213 /*
1214 * Determine if the current path is a database directory that can contain
1215 * relations.
1216 *
1217 * Start by finding the location of the delimiter between the parent path
1218 * and the current path.
1219 */
1221
1222 /* Does this path look like a database path (i.e. all digits)? */
1223 if (lastDir != NULL &&
1224 strspn(lastDir + 1, "0123456789") == strlen(lastDir + 1))
1225 {
1226 /* Part of path that contains the parent directory. */
1227 int parentPathLen = lastDir - path;
1228
1229 /*
1230 * Mark path as a database directory if the parent path is either
1231 * $PGDATA/base or a tablespace version path.
1232 */
1233 if (strncmp(path, "./base", parentPathLen) == 0 ||
1234 (parentPathLen >= (sizeof(TABLESPACE_VERSION_DIRECTORY) - 1) &&
1237 sizeof(TABLESPACE_VERSION_DIRECTORY) - 1) == 0))
1238 {
1239 isRelationDir = true;
1240 dboid = atooid(lastDir + 1);
1241 }
1242 }
1243 else if (strcmp(path, "./global") == 0)
1244 {
1245 isRelationDir = true;
1246 isGlobalDir = true;
1247 }
1248
1249 dir = AllocateDir(path);
1250 while ((de = ReadDir(dir, path)) != NULL)
1251 {
1252 int excludeIdx;
1253 bool excludeFound;
1254 RelFileNumber relfilenumber = InvalidRelFileNumber;
1256 unsigned segno = 0;
1257 bool isRelationFile = false;
1258
1259 /* Skip special stuff */
1260 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1261 continue;
1262
1263 /* Skip temporary files */
1264 if (strncmp(de->d_name,
1267 continue;
1268
1269 /* Skip macOS system files */
1270 if (strcmp(de->d_name, ".DS_Store") == 0)
1271 continue;
1272
1273 /*
1274 * Check if the postmaster has signaled us to exit, and abort with an
1275 * error in that case. The error handler further up will call
1276 * do_pg_abort_backup() for us. Also check that if the backup was
1277 * started while still in recovery, the server wasn't promoted.
1278 * do_pg_backup_stop() will check that too, but it's better to stop
1279 * the backup early than continue to the end and fail there.
1280 */
1283 ereport(ERROR,
1285 errmsg("the standby was promoted during online backup"),
1286 errhint("This means that the backup being taken is corrupt "
1287 "and should not be used. "
1288 "Try taking another online backup.")));
1289
1290 /* Scan for files that should be excluded */
1291 excludeFound = false;
1293 {
1295
1296 if (!excludeFiles[excludeIdx].match_prefix)
1297 cmplen++;
1298 if (strncmp(de->d_name, excludeFiles[excludeIdx].name, cmplen) == 0)
1299 {
1300 elog(DEBUG1, "file \"%s\" excluded from backup", de->d_name);
1301 excludeFound = true;
1302 break;
1303 }
1304 }
1305
1306 if (excludeFound)
1307 continue;
1308
1309 /*
1310 * If there could be non-temporary relation files in this directory,
1311 * try to parse the filename.
1312 */
1313 if (isRelationDir)
1316 &relfilenumber,
1317 &relForkNum, &segno);
1318
1319 /* Exclude all forks for unlogged tables except the init fork */
1321 {
1322 char initForkFile[MAXPGPATH];
1323
1324 /*
1325 * If any other type of fork, check if there is an init fork with
1326 * the same RelFileNumber. If so, the file can be excluded.
1327 */
1328 snprintf(initForkFile, sizeof(initForkFile), "%s/%u_init",
1329 path, relfilenumber);
1330
1331 if (lstat(initForkFile, &statbuf) == 0)
1332 {
1333 elog(DEBUG2,
1334 "unlogged relation file \"%s\" excluded from backup",
1335 de->d_name);
1336
1337 continue;
1338 }
1339 }
1340
1341 /* Exclude temporary relations */
1342 if (OidIsValid(dboid) && looks_like_temp_rel_name(de->d_name))
1343 {
1344 elog(DEBUG2,
1345 "temporary relation file \"%s\" excluded from backup",
1346 de->d_name);
1347
1348 continue;
1349 }
1350
1351 snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path, de->d_name);
1352
1353 /* Skip pg_control here to back up it last */
1354 if (strcmp(pathbuf, "./" XLOG_CONTROL_FILE) == 0)
1355 continue;
1356
1357 if (lstat(pathbuf, &statbuf) != 0)
1358 {
1359 if (errno != ENOENT)
1360 ereport(ERROR,
1362 errmsg("could not stat file or directory \"%s\": %m",
1363 pathbuf)));
1364
1365 /* If the file went away while scanning, it's not an error. */
1366 continue;
1367 }
1368
1369 /* Scan for directories whose contents should be excluded */
1370 excludeFound = false;
1372 {
1373 if (strcmp(de->d_name, excludeDirContents[excludeIdx]) == 0)
1374 {
1375 elog(DEBUG1, "contents of directory \"%s\" excluded from backup", de->d_name);
1378 &statbuf, sizeonly);
1379 excludeFound = true;
1380 break;
1381 }
1382 }
1383
1384 if (excludeFound)
1385 continue;
1386
1387 /*
1388 * We can skip pg_wal, the WAL segments need to be fetched from the
1389 * WAL archive anyway. But include it as an empty directory anyway, so
1390 * we get permissions right.
1391 */
1392 if (strcmp(pathbuf, "./pg_wal") == 0)
1393 {
1394 /* If pg_wal is a symlink, write it as a directory anyway */
1397 &statbuf, sizeonly);
1398
1399 /*
1400 * Also send archive_status and summaries directories (by
1401 * hackishly reusing statbuf from above ...).
1402 */
1403 size += _tarWriteHeader(sink, "./pg_wal/archive_status", NULL,
1404 &statbuf, sizeonly);
1405 size += _tarWriteHeader(sink, "./pg_wal/summaries", NULL,
1406 &statbuf, sizeonly);
1407
1408 continue; /* don't recurse into pg_wal */
1409 }
1410
1411 /* Allow symbolic links in pg_tblspc only */
1412 if (strcmp(path, "./pg_tblspc") == 0 && S_ISLNK(statbuf.st_mode))
1413 {
1414 char linkpath[MAXPGPATH];
1415 int rllen;
1416
1417 rllen = readlink(pathbuf, linkpath, sizeof(linkpath));
1418 if (rllen < 0)
1419 ereport(ERROR,
1421 errmsg("could not read symbolic link \"%s\": %m",
1422 pathbuf)));
1423 if (rllen >= sizeof(linkpath))
1424 ereport(ERROR,
1426 errmsg("symbolic link \"%s\" target is too long",
1427 pathbuf)));
1428 linkpath[rllen] = '\0';
1429
1431 &statbuf, sizeonly);
1432 }
1433 else if (S_ISDIR(statbuf.st_mode))
1434 {
1435 bool skip_this_dir = false;
1436 ListCell *lc;
1437
1438 /*
1439 * Store a directory entry in the tar file so we can get the
1440 * permissions right.
1441 */
1443 sizeonly);
1444
1445 /*
1446 * Call ourselves recursively for a directory, unless it happens
1447 * to be a separate tablespace located within PGDATA.
1448 */
1449 foreach(lc, tablespaces)
1450 {
1452
1453 /*
1454 * ti->rpath is the tablespace relative path within PGDATA, or
1455 * NULL if the tablespace has been properly located somewhere
1456 * else.
1457 *
1458 * Skip past the leading "./" in pathbuf when comparing.
1459 */
1460 if (ti->rpath && strcmp(ti->rpath, pathbuf + 2) == 0)
1461 {
1462 skip_this_dir = true;
1463 break;
1464 }
1465 }
1466
1467 /*
1468 * skip sending directories inside pg_tblspc, if not required.
1469 */
1470 if (strcmp(pathbuf, "./pg_tblspc") == 0 && !sendtblspclinks)
1471 skip_this_dir = true;
1472
1473 if (!skip_this_dir)
1474 size += sendDir(sink, pathbuf, basepathlen, sizeonly, tablespaces,
1476 }
1477 else if (S_ISREG(statbuf.st_mode))
1478 {
1479 bool sent = false;
1480 unsigned num_blocks_required = 0;
1481 unsigned truncation_block_length = 0;
1482 char tarfilenamebuf[MAXPGPATH * 2];
1483 char *tarfilename = pathbuf + basepathlen + 1;
1485
1486 if (ib != NULL && isRelationFile)
1487 {
1488 Oid relspcoid;
1489 char *lookup_path;
1490
1491 if (OidIsValid(spcoid))
1492 {
1493 relspcoid = spcoid;
1495 tarfilename);
1496 }
1497 else
1498 {
1499 if (isGlobalDir)
1501 else
1503 lookup_path = pstrdup(tarfilename);
1504 }
1505
1506 method = GetFileBackupMethod(ib, lookup_path, dboid, relspcoid,
1507 relfilenumber, relForkNum,
1508 segno, statbuf.st_size,
1510 relative_block_numbers,
1511 &truncation_block_length);
1512 if (method == BACK_UP_FILE_INCREMENTALLY)
1513 {
1514 statbuf.st_size =
1517 "%s/INCREMENTAL.%s",
1518 path + basepathlen + 1,
1519 de->d_name);
1520 tarfilename = tarfilenamebuf;
1521 }
1522
1524 }
1525
1526 if (!sizeonly)
1527 sent = sendFile(sink, pathbuf, tarfilename, &statbuf,
1528 true, dboid, spcoid,
1529 relfilenumber, segno, manifest,
1531 method == BACK_UP_FILE_INCREMENTALLY ? relative_block_numbers : NULL,
1532 truncation_block_length);
1533
1534 if (sent || sizeonly)
1535 {
1536 /* Add size. */
1537 size += statbuf.st_size;
1538
1539 /* Pad to a multiple of the tar block size. */
1540 size += tarPaddingBytesRequired(statbuf.st_size);
1541
1542 /* Size of the header for the file. */
1543 size += TAR_BLOCK_SIZE;
1544 }
1545 }
1546 else
1548 (errmsg("skipping special file \"%s\"", pathbuf)));
1549 }
1550
1551 if (relative_block_numbers != NULL)
1552 pfree(relative_block_numbers);
1553
1554 FreeDir(dir);
1555 return size;
1556}
1557
1558/*
1559 * Given the member, write the TAR header & send the file.
1560 *
1561 * If 'missing_ok' is true, will not throw an error if the file is not found.
1562 *
1563 * If dboid is anything other than InvalidOid then any checksum failures
1564 * detected will get reported to the cumulative stats system.
1565 *
1566 * If the file is to be sent incrementally, then num_incremental_blocks
1567 * should be the number of blocks to be sent, and incremental_blocks
1568 * an array of block numbers relative to the start of the current segment.
1569 * If the whole file is to be sent, then incremental_blocks should be NULL,
1570 * and num_incremental_blocks can have any value, as it will be ignored.
1571 *
1572 * Returns true if the file was successfully sent, false if 'missing_ok',
1573 * and the file did not exist.
1574 */
1575static bool
1576sendFile(bbsink *sink, const char *readfilename, const char *tarfilename,
1577 struct stat *statbuf, bool missing_ok, Oid dboid, Oid spcoid,
1578 RelFileNumber relfilenumber, unsigned segno,
1580 BlockNumber *incremental_blocks, unsigned truncation_block_length)
1581{
1582 int fd;
1583 BlockNumber blkno = 0;
1584 int checksum_failures = 0;
1585 off_t cnt;
1586 pgoff_t bytes_done = 0;
1587 bool verify_checksum = false;
1588 pg_checksum_context checksum_ctx;
1589 int ibindex = 0;
1590
1591 if (pg_checksum_init(&checksum_ctx, manifest->checksum_type) < 0)
1592 elog(ERROR, "could not initialize checksum of file \"%s\"",
1593 readfilename);
1594
1596 if (fd < 0)
1597 {
1598 if (errno == ENOENT && missing_ok)
1599 return false;
1600 ereport(ERROR,
1602 errmsg("could not open file \"%s\": %m", readfilename)));
1603 }
1604
1605 _tarWriteHeader(sink, tarfilename, NULL, statbuf, false);
1606
1607 /*
1608 * Checksums are verified in multiples of BLCKSZ, so the buffer length
1609 * should be a multiple of the block size as well.
1610 */
1611 Assert((sink->bbs_buffer_length % BLCKSZ) == 0);
1612
1613 /*
1614 * If we weren't told not to verify checksums, and if checksums are
1615 * enabled for this cluster, and if this is a relation file, then verify
1616 * the checksum.
1617 */
1619 RelFileNumberIsValid(relfilenumber))
1620 verify_checksum = true;
1621
1622 /*
1623 * If we're sending an incremental file, write the file header.
1624 */
1625 if (incremental_blocks != NULL)
1626 {
1627 unsigned magic = INCREMENTAL_MAGIC;
1628 size_t header_bytes_done = 0;
1629 char padding[BLCKSZ];
1630 size_t paddinglen;
1631
1632 /* Emit header data. */
1633 push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1634 &magic, sizeof(magic));
1635 push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1637 push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1638 &truncation_block_length, sizeof(truncation_block_length));
1639 push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1642
1643 /*
1644 * Add padding to align header to a multiple of BLCKSZ, but only if
1645 * the incremental file has some blocks, and the alignment is actually
1646 * needed (i.e. header is not already a multiple of BLCKSZ). If there
1647 * are no blocks we don't want to make the file unnecessarily large,
1648 * as that might make some filesystem optimizations impossible.
1649 */
1650 if ((num_incremental_blocks > 0) && (header_bytes_done % BLCKSZ != 0))
1651 {
1653
1654 memset(padding, 0, paddinglen);
1655 bytes_done += paddinglen;
1656
1657 push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1658 padding, paddinglen);
1659 }
1660
1661 /* Flush out any data still in the buffer so it's again empty. */
1662 if (header_bytes_done > 0)
1663 {
1665 if (pg_checksum_update(&checksum_ctx,
1666 (uint8 *) sink->bbs_buffer,
1667 header_bytes_done) < 0)
1668 elog(ERROR, "could not update checksum of base backup");
1669 }
1670
1671 /* Update our notion of file position. */
1672 bytes_done += sizeof(magic);
1673 bytes_done += sizeof(num_incremental_blocks);
1674 bytes_done += sizeof(truncation_block_length);
1675 bytes_done += sizeof(BlockNumber) * num_incremental_blocks;
1676 }
1677
1678 /*
1679 * Loop until we read the amount of data the caller told us to expect. The
1680 * file could be longer, if it was extended while we were sending it, but
1681 * for a base backup we can ignore such extended data. It will be restored
1682 * from WAL.
1683 */
1684 while (1)
1685 {
1686 /*
1687 * Determine whether we've read all the data that we need, and if not,
1688 * read some more.
1689 */
1690 if (incremental_blocks == NULL)
1691 {
1692 size_t remaining = statbuf->st_size - bytes_done;
1693
1694 /*
1695 * If we've read the required number of bytes, then it's time to
1696 * stop.
1697 */
1698 if (bytes_done >= statbuf->st_size)
1699 break;
1700
1701 /*
1702 * Read as many bytes as will fit in the buffer, or however many
1703 * are left to read, whichever is less.
1704 */
1706 bytes_done, remaining,
1707 blkno + segno * RELSEG_SIZE,
1708 verify_checksum,
1709 &checksum_failures);
1710 }
1711 else
1712 {
1714
1715 /*
1716 * If we've read all the blocks, then it's time to stop.
1717 */
1719 break;
1720
1721 /*
1722 * Read just one block, whichever one is the next that we're
1723 * supposed to include.
1724 */
1728 BLCKSZ,
1729 relative_blkno + segno * RELSEG_SIZE,
1730 verify_checksum,
1731 &checksum_failures);
1732
1733 /*
1734 * If we get a partial read, that must mean that the relation is
1735 * being truncated. Ultimately, it should be truncated to a
1736 * multiple of BLCKSZ, since this path should only be reached for
1737 * relation files, but we might transiently observe an
1738 * intermediate value.
1739 *
1740 * It should be fine to treat this just as if the entire block had
1741 * been truncated away - i.e. fill this and all later blocks with
1742 * zeroes. WAL replay will fix things up.
1743 */
1744 if (cnt < BLCKSZ)
1745 break;
1746 }
1747
1748 /*
1749 * If the amount of data we were able to read was not a multiple of
1750 * BLCKSZ, we cannot verify checksums, which are block-level.
1751 */
1752 if (verify_checksum && (cnt % BLCKSZ != 0))
1753 {
1755 (errmsg("could not verify checksum in file \"%s\", block "
1756 "%u: read buffer size %d and page size %d "
1757 "differ",
1758 readfilename, blkno, (int) cnt, BLCKSZ)));
1759 verify_checksum = false;
1760 }
1761
1762 /*
1763 * If we hit end-of-file, a concurrent truncation must have occurred.
1764 * That's not an error condition, because WAL replay will fix things
1765 * up.
1766 */
1767 if (cnt == 0)
1768 break;
1769
1770 /* Update block number and # of bytes done for next loop iteration. */
1771 blkno += cnt / BLCKSZ;
1772 bytes_done += cnt;
1773
1774 /*
1775 * Make sure incremental files with block data are properly aligned
1776 * (header is a multiple of BLCKSZ, blocks are BLCKSZ too).
1777 */
1779 (bytes_done % BLCKSZ != 0)));
1780
1781 /* Archive the data we just read. */
1783
1784 /* Also feed it to the checksum machinery. */
1785 if (pg_checksum_update(&checksum_ctx,
1786 (uint8 *) sink->bbs_buffer, cnt) < 0)
1787 elog(ERROR, "could not update checksum of base backup");
1788 }
1789
1790 /* If the file was truncated while we were sending it, pad it with zeros */
1791 while (bytes_done < statbuf->st_size)
1792 {
1793 size_t remaining = statbuf->st_size - bytes_done;
1794 size_t nbytes = Min(sink->bbs_buffer_length, remaining);
1795
1796 MemSet(sink->bbs_buffer, 0, nbytes);
1797 if (pg_checksum_update(&checksum_ctx,
1798 (uint8 *) sink->bbs_buffer,
1799 nbytes) < 0)
1800 elog(ERROR, "could not update checksum of base backup");
1802 bytes_done += nbytes;
1803 }
1804
1805 /*
1806 * Pad to a block boundary, per tar format requirements. (This small piece
1807 * of data is probably not worth throttling, and is not checksummed
1808 * because it's not actually part of the file.)
1809 */
1810 _tarWritePadding(sink, bytes_done);
1811
1813
1814 if (checksum_failures > 1)
1815 {
1817 (errmsg_plural("file \"%s\" has a total of %d checksum verification failure",
1818 "file \"%s\" has a total of %d checksum verification failures",
1819 checksum_failures,
1820 readfilename, checksum_failures)));
1821
1823 pgstat_report_checksum_failures_in_db(dboid, checksum_failures);
1824 }
1825
1826 total_checksum_failures += checksum_failures;
1827
1828 AddFileToBackupManifest(manifest, spcoid, tarfilename, statbuf->st_size,
1829 (pg_time_t) statbuf->st_mtime, &checksum_ctx);
1830
1831 return true;
1832}
1833
1834/*
1835 * Read some more data from the file into the bbsink's buffer, verifying
1836 * checksums as required.
1837 *
1838 * 'offset' is the file offset from which we should begin to read, and
1839 * 'length' is the amount of data that should be read. The actual amount
1840 * of data read will be less than the requested amount if the bbsink's
1841 * buffer isn't big enough to hold it all, or if the underlying file has
1842 * been truncated. The return value is the number of bytes actually read.
1843 *
1844 * 'blkno' is the block number of the first page in the bbsink's buffer
1845 * relative to the start of the relation.
1846 *
1847 * 'verify_checksum' indicates whether we should try to verify checksums
1848 * for the blocks we read. If we do this, we'll update *checksum_failures
1849 * and issue warnings as appropriate.
1850 */
1851static off_t
1853 off_t offset, size_t length, BlockNumber blkno,
1854 bool verify_checksum, int *checksum_failures)
1855{
1856 off_t cnt;
1857 int i;
1858 char *page;
1859
1860 /* Try to read some more data. */
1861 cnt = basebackup_read_file(fd, sink->bbs_buffer,
1862 Min(sink->bbs_buffer_length, length),
1863 offset, readfilename, true);
1864
1865 /* Can't verify checksums if read length is not a multiple of BLCKSZ. */
1866 if (!verify_checksum || (cnt % BLCKSZ) != 0)
1867 return cnt;
1868
1869 /* Verify checksum for each block. */
1870 for (i = 0; i < cnt / BLCKSZ; i++)
1871 {
1872 int reread_cnt;
1874
1875 page = sink->bbs_buffer + BLCKSZ * i;
1876
1877 /* If the page is OK, go on to the next one. */
1878 if (verify_page_checksum(page, sink->bbs_state->startptr, blkno + i,
1880 continue;
1881
1882 /*
1883 * Retry the block on the first failure. It's possible that we read
1884 * the first 4K page of the block just before postgres updated the
1885 * entire block so it ends up looking torn to us. If, before we retry
1886 * the read, the concurrent write of the block finishes, the page LSN
1887 * will be updated and we'll realize that we should ignore this block.
1888 *
1889 * There's no guarantee that this will actually happen, though: the
1890 * torn write could take an arbitrarily long time to complete.
1891 * Retrying multiple times wouldn't fix this problem, either, though
1892 * it would reduce the chances of it happening in practice. The only
1893 * real fix here seems to be to have some kind of interlock that
1894 * allows us to wait until we can be certain that no write to the
1895 * block is in progress. Since we don't have any such thing right now,
1896 * we just do this and hope for the best.
1897 */
1898 reread_cnt =
1899 basebackup_read_file(fd, sink->bbs_buffer + BLCKSZ * i,
1900 BLCKSZ, offset + BLCKSZ * i,
1901 readfilename, false);
1902 if (reread_cnt == 0)
1903 {
1904 /*
1905 * If we hit end-of-file, a concurrent truncation must have
1906 * occurred, so reduce cnt to reflect only the blocks already
1907 * processed and break out of this loop.
1908 */
1909 cnt = BLCKSZ * i;
1910 break;
1911 }
1912
1913 /* If the page now looks OK, go on to the next one. */
1914 if (verify_page_checksum(page, sink->bbs_state->startptr, blkno + i,
1916 continue;
1917
1918 /* Handle checksum failure. */
1919 (*checksum_failures)++;
1920 if (*checksum_failures <= 5)
1922 (errmsg("checksum verification failed in "
1923 "file \"%s\", block %u: calculated "
1924 "%X but expected %X",
1926 ((PageHeader) page)->pd_checksum)));
1927 if (*checksum_failures == 5)
1929 (errmsg("further checksum verification "
1930 "failures in file \"%s\" will not "
1931 "be reported", readfilename)));
1932 }
1933
1934 return cnt;
1935}
1936
1937/*
1938 * Push data into a bbsink.
1939 *
1940 * It's better, when possible, to read data directly into the bbsink's buffer,
1941 * rather than using this function to copy it into the buffer; this function is
1942 * for cases where that approach is not practical.
1943 *
1944 * bytes_done should point to a count of the number of bytes that are
1945 * currently used in the bbsink's buffer. Upon return, the bytes identified by
1946 * data and length will have been copied into the bbsink's buffer, flushing
1947 * as required, and *bytes_done will have been updated accordingly. If the
1948 * buffer was flushed, the previous contents will also have been fed to
1949 * checksum_ctx.
1950 *
1951 * Note that after one or more calls to this function it is the caller's
1952 * responsibility to perform any required final flush.
1953 */
1954static void
1956 size_t *bytes_done, void *data, size_t length)
1957{
1958 while (length > 0)
1959 {
1960 size_t bytes_to_copy;
1961
1962 /*
1963 * We use < here rather than <= so that if the data exactly fills the
1964 * remaining buffer space, we trigger a flush now.
1965 */
1966 if (length < sink->bbs_buffer_length - *bytes_done)
1967 {
1968 /* Append remaining data to buffer. */
1969 memcpy(sink->bbs_buffer + *bytes_done, data, length);
1970 *bytes_done += length;
1971 return;
1972 }
1973
1974 /* Copy until buffer is full and flush it. */
1975 bytes_to_copy = sink->bbs_buffer_length - *bytes_done;
1976 memcpy(sink->bbs_buffer + *bytes_done, data, bytes_to_copy);
1977 data = ((char *) data) + bytes_to_copy;
1978 length -= bytes_to_copy;
1979 bbsink_archive_contents(sink, sink->bbs_buffer_length);
1980 if (pg_checksum_update(checksum_ctx, (uint8 *) sink->bbs_buffer,
1981 sink->bbs_buffer_length) < 0)
1982 elog(ERROR, "could not update checksum");
1983 *bytes_done = 0;
1984 }
1985}
1986
1987/*
1988 * Try to verify the checksum for the provided page, if it seems appropriate
1989 * to do so.
1990 *
1991 * Returns true if verification succeeds or if we decide not to check it,
1992 * and false if verification fails. When return false, it also sets
1993 * *expected_checksum to the computed value.
1994 */
1995static bool
1998{
1999 PageHeader phdr;
2000 uint16 checksum;
2001
2002 /*
2003 * Only check pages which have not been modified since the start of the
2004 * base backup. Otherwise, they might have been written only halfway and
2005 * the checksum would not be valid. However, replaying WAL would
2006 * reinstate the correct page in this case. We also skip completely new
2007 * pages, since they don't have a checksum yet.
2008 */
2009 if (PageIsNew(page) || PageGetLSN(page) >= start_lsn)
2010 return true;
2011
2012 /* Perform the actual checksum calculation. */
2013 checksum = pg_checksum_page(page, blkno);
2014
2015 /* See whether it matches the value from the page. */
2016 phdr = (PageHeader) page;
2017 if (phdr->pd_checksum == checksum)
2018 return true;
2019 *expected_checksum = checksum;
2020 return false;
2021}
2022
2023static int64
2024_tarWriteHeader(bbsink *sink, const char *filename, const char *linktarget,
2025 struct stat *statbuf, bool sizeonly)
2026{
2027 enum tarError rc;
2028
2029 if (!sizeonly)
2030 {
2031 /*
2032 * As of this writing, the smallest supported block size is 1kB, which
2033 * is twice TAR_BLOCK_SIZE. Since the buffer size is required to be a
2034 * multiple of BLCKSZ, it should be safe to assume that the buffer is
2035 * large enough to fit an entire tar block. We double-check by means
2036 * of these assertions.
2037 */
2039 "BLCKSZ too small for tar block");
2040 Assert(sink->bbs_buffer_length >= TAR_BLOCK_SIZE);
2041
2042 rc = tarCreateHeader(sink->bbs_buffer, filename, linktarget,
2043 statbuf->st_size, statbuf->st_mode,
2044 statbuf->st_uid, statbuf->st_gid,
2045 statbuf->st_mtime);
2046
2047 switch (rc)
2048 {
2049 case TAR_OK:
2050 break;
2051 case TAR_NAME_TOO_LONG:
2052 ereport(ERROR,
2054 errmsg("file name too long for tar format: \"%s\"",
2055 filename)));
2056 break;
2058 ereport(ERROR,
2060 errmsg("symbolic link target too long for tar format: "
2061 "file name \"%s\", target \"%s\"",
2062 filename, linktarget)));
2063 break;
2064 default:
2065 elog(ERROR, "unrecognized tar error: %d", rc);
2066 }
2067
2069 }
2070
2071 return TAR_BLOCK_SIZE;
2072}
2073
2074/*
2075 * Pad with zero bytes out to a multiple of TAR_BLOCK_SIZE.
2076 */
2077static void
2079{
2080 int pad = tarPaddingBytesRequired(len);
2081
2082 /*
2083 * As in _tarWriteHeader, it should be safe to assume that the buffer is
2084 * large enough that we don't need to do this in multiple chunks.
2085 */
2086 Assert(sink->bbs_buffer_length >= TAR_BLOCK_SIZE);
2087 Assert(pad <= TAR_BLOCK_SIZE);
2088
2089 if (pad > 0)
2090 {
2091 MemSet(sink->bbs_buffer, 0, pad);
2093 }
2094}
2095
2096/*
2097 * If the entry in statbuf is a link, then adjust statbuf to make it look like a
2098 * directory, so that it will be written that way.
2099 */
2100static void
2102{
2103 /* If symlink, write it as a directory anyway */
2104 if (S_ISLNK(statbuf->st_mode))
2105 statbuf->st_mode = S_IFDIR | pg_dir_create_mode;
2106}
2107
2108/*
2109 * Read some data from a file, setting a wait event and reporting any error
2110 * encountered.
2111 *
2112 * If partial_read_ok is false, also report an error if the number of bytes
2113 * read is not equal to the number of bytes requested.
2114 *
2115 * Returns the number of bytes read.
2116 */
2117static ssize_t
2118basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset,
2119 const char *filename, bool partial_read_ok)
2120{
2121 ssize_t rc;
2122
2124 rc = pg_pread(fd, buf, nbytes, offset);
2126
2127 if (rc < 0)
2128 ereport(ERROR,
2130 errmsg("could not read file \"%s\": %m", filename)));
2131 if (!partial_read_ok && rc > 0 && rc != nbytes)
2132 ereport(ERROR,
2134 errmsg("could not read file \"%s\": read %zd of %zu",
2135 filename, rc, nbytes)));
2136
2137 return rc;
2138}
void InitializeBackupManifest(backup_manifest_info *manifest, backup_manifest_option want_manifest, pg_checksum_type manifest_checksum_type)
void AddFileToBackupManifest(backup_manifest_info *manifest, Oid spcoid, const char *pathname, size_t size, pg_time_t mtime, pg_checksum_context *checksum_ctx)
void AddWALInfoToBackupManifest(backup_manifest_info *manifest, XLogRecPtr startptr, TimeLineID starttli, XLogRecPtr endptr, TimeLineID endtli)
void SendBackupManifest(backup_manifest_info *manifest, bbsink *sink)
void FreeBackupManifest(backup_manifest_info *manifest)
@ MANIFEST_OPTION_NO
@ MANIFEST_OPTION_FORCE_ENCODE
@ MANIFEST_OPTION_YES
enum manifest_option backup_manifest_option
static bool sendFile(bbsink *sink, const char *readfilename, const char *tarfilename, struct stat *statbuf, bool missing_ok, Oid dboid, Oid spcoid, RelFileNumber relfilenumber, unsigned segno, backup_manifest_info *manifest, unsigned num_incremental_blocks, BlockNumber *incremental_blocks, unsigned truncation_block_length)
static const struct exclude_list_item excludeFiles[]
Definition basebackup.c:197
static int compareWalFileNames(const ListCell *a, const ListCell *b)
Definition basebackup.c:688
static void sendFileWithContent(bbsink *sink, const char *filename, const char *content, int len, backup_manifest_info *manifest)
static off_t read_file_data_into_buffer(bbsink *sink, const char *readfilename, int fd, off_t offset, size_t length, BlockNumber blkno, bool verify_checksum, int *checksum_failures)
static void push_to_sink(bbsink *sink, pg_checksum_context *checksum_ctx, size_t *bytes_done, void *data, size_t length)
static int64 sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly, List *tablespaces, bool sendtblspclinks, backup_manifest_info *manifest, Oid spcoid, IncrementalBackupInfo *ib)
#define SINK_BUFFER_LENGTH
Definition basebackup.c:61
static void parse_basebackup_options(List *options, basebackup_options *opt)
Definition basebackup.c:700
static const char *const excludeDirContents[]
Definition basebackup.c:157
static void convert_link_to_directory(const char *pathbuf, struct stat *statbuf)
void SendBaseBackup(BaseBackupCmd *cmd, IncrementalBackupInfo *ib)
Definition basebackup.c:992
static bool backup_started_in_recovery
Definition basebackup.c:129
static int64 _tarWriteHeader(bbsink *sink, const char *filename, const char *linktarget, struct stat *statbuf, bool sizeonly)
static void perform_base_backup(basebackup_options *opt, bbsink *sink, IncrementalBackupInfo *ib)
Definition basebackup.c:240
static int64 sendTablespace(bbsink *sink, char *path, Oid spcoid, bool sizeonly, struct backup_manifest_info *manifest, IncrementalBackupInfo *ib)
static bool noverify_checksums
Definition basebackup.c:135
static void _tarWritePadding(bbsink *sink, int len)
static bool verify_page_checksum(Page page, XLogRecPtr start_lsn, BlockNumber blkno, uint16 *expected_checksum)
static ssize_t basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset, const char *filename, bool partial_read_ok)
#define TAR_NUM_TERMINATION_BLOCKS
Definition basebackup.c:82
static long long int total_checksum_failures
Definition basebackup.c:132
#define MAX_RATE_UPPER
Definition basebackup.h:21
#define MAX_RATE_LOWER
Definition basebackup.h:20
bbsink * bbsink_copystream_new(bool send_to_client)
bbsink * bbsink_gzip_new(bbsink *next, pg_compress_specification *compress)
size_t GetIncrementalFileSize(unsigned num_blocks_required)
FileBackupMethod GetFileBackupMethod(IncrementalBackupInfo *ib, const char *path, Oid dboid, Oid spcoid, RelFileNumber relfilenumber, ForkNumber forknum, unsigned segno, size_t size, unsigned *num_blocks_required, BlockNumber *relative_block_numbers, unsigned *truncation_block_length)
void PrepareForIncrementalBackup(IncrementalBackupInfo *ib, BackupState *backup_state)
#define INCREMENTAL_MAGIC
@ BACK_UP_FILE_INCREMENTALLY
@ BACK_UP_FILE_FULLY
bbsink * bbsink_lz4_new(bbsink *next, pg_compress_specification *compress)
void basebackup_progress_wait_checkpoint(void)
bbsink * bbsink_progress_new(bbsink *next, bool estimate_backup_size, bool incremental)
void basebackup_progress_wait_wal_archive(bbsink_state *state)
void basebackup_progress_done(void)
void basebackup_progress_transfer_wal(void)
void basebackup_progress_estimate_backup_size(void)
static void bbsink_begin_backup(bbsink *sink, bbsink_state *state, int buffer_length)
static void bbsink_begin_archive(bbsink *sink, const char *archive_name)
static void bbsink_end_archive(bbsink *sink)
static void bbsink_end_backup(bbsink *sink, XLogRecPtr endptr, TimeLineID endtli)
static void bbsink_cleanup(bbsink *sink)
static void bbsink_archive_contents(bbsink *sink, size_t len)
BaseBackupTargetHandle * BaseBackupGetTargetHandle(char *target, char *target_detail)
bbsink * BaseBackupGetSink(BaseBackupTargetHandle *handle, bbsink *next_sink)
bbsink * bbsink_throttle_new(bbsink *next, uint32 maxrate)
bbsink * bbsink_zstd_new(bbsink *next, pg_compress_specification *compress)
uint32 BlockNumber
Definition block.h:31
bool parse_bool(const char *value, bool *result)
Definition bool.c:31
PageHeaderData * PageHeader
Definition bufpage.h:199
static bool PageIsNew(const PageData *page)
Definition bufpage.h:259
PageData * Page
Definition bufpage.h:81
static XLogRecPtr PageGetLSN(const PageData *page)
Definition bufpage.h:411
#define Min(x, y)
Definition c.h:1093
uint8_t uint8
Definition c.h:616
#define Assert(condition)
Definition c.h:945
int64_t int64
Definition c.h:615
#define PG_BINARY
Definition c.h:1376
#define CppAsString2(x)
Definition c.h:500
uint16_t uint16
Definition c.h:617
uint32_t uint32
Definition c.h:618
#define MemSet(start, val, len)
Definition c.h:1109
#define StaticAssertDecl(condition, errmessage)
Definition c.h:1010
#define OidIsValid(objectId)
Definition c.h:860
uint16 pg_checksum_page(char *page, BlockNumber blkno)
bool pg_checksum_parse_type(char *name, pg_checksum_type *type)
int pg_checksum_update(pg_checksum_context *context, const uint8 *input, size_t len)
int pg_checksum_init(pg_checksum_context *context, pg_checksum_type type)
pg_checksum_type
@ CHECKSUM_TYPE_NONE
@ CHECKSUM_TYPE_CRC32C
char * validate_compress_specification(pg_compress_specification *spec)
bool parse_compress_algorithm(char *name, pg_compress_algorithm *algorithm)
Definition compression.c:79
void parse_compress_specification(pg_compress_algorithm algorithm, char *specification, pg_compress_specification *result)
pg_compress_algorithm
Definition compression.h:22
@ PG_COMPRESSION_GZIP
Definition compression.h:24
@ PG_COMPRESSION_LZ4
Definition compression.h:25
@ PG_COMPRESSION_NONE
Definition compression.h:23
@ PG_COMPRESSION_ZSTD
Definition compression.h:26
char * defGetString(DefElem *def)
Definition define.c:34
bool defGetBoolean(DefElem *def)
Definition define.c:93
int64 defGetInt64(DefElem *def)
Definition define.c:172
#define PG_DYNSHMEM_DIR
Definition dsm_impl.h:51
int errcode_for_file_access(void)
Definition elog.c:897
int errcode(int sqlerrcode)
Definition elog.c:874
int errhint(const char *fmt,...) pg_attribute_printf(1
#define PG_TRY(...)
Definition elog.h:372
#define WARNING
Definition elog.h:36
int int int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...) pg_attribute_printf(1
#define DEBUG2
Definition elog.h:29
#define PG_END_TRY(...)
Definition elog.h:397
#define DEBUG1
Definition elog.h:30
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define PG_FINALLY(...)
Definition elog.h:389
#define ereport(elevel,...)
Definition elog.h:150
int FreeDir(DIR *dir)
Definition fd.c:3009
int CloseTransientFile(int fd)
Definition fd.c:2855
DIR * AllocateDir(const char *dirname)
Definition fd.c:2891
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition fd.c:2957
bool looks_like_temp_rel_name(const char *name)
Definition fd.c:3499
int OpenTransientFile(const char *fileName, int fileFlags)
Definition fd.c:2678
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define palloc0_object(type)
Definition fe_memutils.h:75
int pg_file_create_mode
Definition file_perm.c:19
int pg_dir_create_mode
Definition file_perm.c:18
#define PG_TEMP_FILE_PREFIX
Definition file_utils.h:64
int remaining
Definition informix.c:692
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition ipc.h:47
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Definition ipc.h:52
int b
Definition isn.c:74
int a
Definition isn.c:73
int i
Definition isn.c:77
List * lappend(List *list, void *datum)
Definition list.c:339
void list_sort(List *list, list_sort_comparator cmp)
Definition list.c:1674
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
static char * errmsg
static bool manifest
static int32 maxrate
#define ERRCODE_DATA_CORRUPTED
#define MAXPGPATH
const void size_t len
const void * data
#define PG_AUTOCONF_FILENAME
static char * filename
Definition pg_dumpall.c:133
#define lfirst(lc)
Definition pg_list.h:172
#define NIL
Definition pg_list.h:68
#define linitial(l)
Definition pg_list.h:178
static ListCell * lnext(const List *l, const ListCell *c)
Definition pg_list.h:343
static char buf[DEFAULT_XLOG_SEG_SIZE]
#define PG_STAT_TMP_DIR
Definition pgstat.h:39
void pgstat_prepare_report_checksum_failure(Oid dboid)
void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount)
static size_t tarPaddingBytesRequired(size_t len)
Definition pgtar.h:79
enum tarError tarCreateHeader(char *h, const char *filename, const char *linktarget, pgoff_t size, mode_t mode, uid_t uid, gid_t gid, time_t mtime)
Definition tar.c:114
tarError
Definition pgtar.h:20
@ TAR_SYMLINK_TOO_LONG
Definition pgtar.h:23
@ TAR_OK
Definition pgtar.h:21
@ TAR_NAME_TOO_LONG
Definition pgtar.h:22
#define TAR_BLOCK_SIZE
Definition pgtar.h:17
int64 pg_time_t
Definition pgtime.h:23
char * last_dir_separator(const char *filename)
Definition path.c:145
int pg_strcasecmp(const char *s1, const char *s2)
#define pg_pread
Definition port.h:247
#define snprintf
Definition port.h:260
off_t pgoff_t
Definition port.h:421
static Datum BoolGetDatum(bool X)
Definition postgres.h:112
#define InvalidOid
unsigned int Oid
#define atooid(x)
static int fd(const char *x, int i)
static int fb(int x)
bool update_process_title
Definition ps_status.c:31
static void set_ps_display(const char *activity)
Definition ps_status.h:40
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
bool parse_filename_for_nontemp_relation(const char *name, RelFileNumber *relnumber, ForkNumber *fork, unsigned *segno)
Definition reinit.c:380
#define RELCACHE_INIT_FILENAME
Definition relcache.h:25
Oid RelFileNumber
Definition relpath.h:25
ForkNumber
Definition relpath.h:56
@ InvalidForkNumber
Definition relpath.h:57
@ INIT_FORKNUM
Definition relpath.h:61
#define InvalidRelFileNumber
Definition relpath.h:26
#define PG_TBLSPC_DIR
Definition relpath.h:41
#define TABLESPACE_VERSION_DIRECTORY
Definition relpath.h:33
#define RelFileNumberIsValid(relnumber)
Definition relpath.h:27
void ReleaseAuxProcessResources(bool isCommit)
Definition resowner.c:1016
ResourceOwner CurrentResourceOwner
Definition resowner.c:173
ResourceOwner AuxProcessResourceOwner
Definition resowner.c:176
#define PG_REPLSLOT_DIR
Definition slot.h:21
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
TimeLineID starttli
Definition xlogbackup.h:27
XLogRecPtr startpoint
Definition xlogbackup.h:26
TimeLineID stoptli
Definition xlogbackup.h:36
XLogRecPtr stoppoint
Definition xlogbackup.h:35
List * options
Definition replnodes.h:44
Definition dirent.c:26
Definition pg_list.h:54
uint16 pd_checksum
Definition bufpage.h:189
pg_compress_specification compression_specification
Definition basebackup.c:78
pg_checksum_type manifest_checksum_type
Definition basebackup.c:79
const char * label
Definition basebackup.c:65
pg_compress_algorithm compression
Definition basebackup.c:77
backup_manifest_option manifest
Definition basebackup.c:76
BaseBackupTargetHandle * target_handle
Definition basebackup.c:75
const char * name
Definition basebackup.c:145
pg_compress_algorithm algorithm
Definition compression.h:34
#define LOG_METAINFO_DATAFILE_TMP
Definition syslogger.h:103
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition wait_event.h:69
static void pgstat_report_wait_end(void)
Definition wait_event.h:85
const char * name
void WalSndSetState(WalSndState state)
Definition walsender.c:3965
@ WALSNDSTATE_BACKUP
bool summarize_wal
#define lstat(path, sb)
Definition win32_port.h:275
#define S_ISDIR(m)
Definition win32_port.h:315
#define S_ISLNK(m)
Definition win32_port.h:334
#define fstat
Definition win32_port.h:73
#define readlink(path, buf, size)
Definition win32_port.h:226
#define S_ISREG(m)
Definition win32_port.h:318
bool RecoveryInProgress(void)
Definition xlog.c:6444
void do_pg_abort_backup(int code, Datum arg)
Definition xlog.c:9573
SessionBackupState get_backup_status(void)
Definition xlog.c:9280
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition xlog.c:3748
int wal_segment_size
Definition xlog.c:147
bool DataChecksumsEnabled(void)
Definition xlog.c:4631
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition xlog.c:8977
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition xlog.c:9299
#define TABLESPACE_MAP
Definition xlog.h:324
#define BACKUP_LABEL_FILE
Definition xlog.h:321
SessionBackupState
Definition xlog.h:305
@ SESSION_BACKUP_RUNNING
Definition xlog.h:307
#define XLOG_CONTROL_FILE
static bool IsXLogFileName(const char *fname)
static void XLogFromFileName(const char *fname, TimeLineID *tli, XLogSegNo *logSegNo, int wal_segsz_bytes)
#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes)
static bool IsTLHistoryFileName(const char *fname)
#define MAXFNAMELEN
#define XLOGDIR
static void StatusFilePath(char *path, const char *xlog, const char *suffix)
#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes)
static void XLogFileName(char *fname, TimeLineID tli, XLogSegNo logSegNo, int wal_segsz_bytes)
char * build_backup_content(BackupState *state, bool ishistoryfile)
Definition xlogbackup.c:29
uint64 XLogRecPtr
Definition xlogdefs.h:21
uint32 TimeLineID
Definition xlogdefs.h:63
uint64 XLogSegNo
Definition xlogdefs.h:52
static BackupState * backup_state
Definition xlogfuncs.c:44
static StringInfo tablespace_map
Definition xlogfuncs.c:45