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
52/*
53 * How much data do we want to send in one CopyData message? Note that
54 * this may also result in reading the underlying files in chunks of this
55 * size.
56 *
57 * NB: The buffer size is required to be a multiple of the system block
58 * size, so use that value instead if it's bigger than our preference.
59 */
60#define SINK_BUFFER_LENGTH Max(32768, BLCKSZ)
61
80
81#define TAR_NUM_TERMINATION_BLOCKS 2
82
84 "BLCKSZ too small for " CppAsString2(TAR_NUM_TERMINATION_BLOCKS) " tar termination blocks");
85
86static int64 sendTablespace(bbsink *sink, char *path, Oid spcoid, bool sizeonly,
89static int64 sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly,
90 List *tablespaces, bool sendtblspclinks,
93static bool sendFile(bbsink *sink, const char *readfilename, const char *tarfilename,
94 struct stat *statbuf, bool missing_ok,
95 Oid dboid, Oid spcoid, RelFileNumber relfilenumber,
96 unsigned segno,
100 unsigned truncation_block_length);
102 const char *readfilename, int fd,
103 off_t offset, size_t length,
104 BlockNumber blkno,
105 bool verify_checksum,
106 int *checksum_failures);
107static void push_to_sink(bbsink *sink, pg_checksum_context *checksum_ctx,
108 size_t *bytes_done, void *data, size_t length);
109static bool verify_page_checksum(Page page, XLogRecPtr start_lsn,
110 BlockNumber blkno,
112static void sendFileWithContent(bbsink *sink, const char *filename,
113 const char *content, int len,
115static int64 _tarWriteHeader(bbsink *sink, const char *filename,
116 const char *linktarget, struct stat *statbuf,
117 bool sizeonly);
118static void _tarWritePadding(bbsink *sink, int len);
119static void convert_link_to_directory(const char *pathbuf, struct stat *statbuf);
123static int compareWalFileNames(const ListCell *a, const ListCell *b);
124static ssize_t basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset,
125 const char *filename, bool partial_read_ok);
126
127/* Was the backup currently in-progress initiated in recovery mode? */
128static bool backup_started_in_recovery = false;
129
130/* Total number of checksum failures during base backup. */
131static long long int total_checksum_failures;
132
133/* Do not verify checksums. */
134static bool noverify_checksums = false;
135
136/*
137 * Definition of one element part of an exclusion list, used for paths part
138 * of checksum validation or base backups. "name" is the name of the file
139 * or path to check for exclusion. If "match_prefix" is true, any items
140 * matching the name as prefix are excluded.
141 */
143{
144 const char *name;
146};
147
148/*
149 * The contents of these directories are removed or recreated during server
150 * start so they are not included in backups. The directories themselves are
151 * kept and included as empty to preserve access permissions.
152 *
153 * Note: this list should be kept in sync with the filter lists in pg_rewind's
154 * filemap.c.
155 */
156static const char *const excludeDirContents[] =
157{
158 /*
159 * Skip temporary statistics files. PG_STAT_TMP_DIR must be skipped
160 * because extensions like pg_stat_statements store data there.
161 */
163
164 /*
165 * It is generally not useful to backup the contents of this directory
166 * even if the intention is to restore to another primary. See backup.sgml
167 * for a more detailed description.
168 */
170
171 /* Contents removed on startup, see dsm_cleanup_for_mmap(). */
173
174 /* Contents removed on startup, see AsyncShmemInit(). */
175 "pg_notify",
176
177 /*
178 * Old contents are loaded for possible debugging but are not required for
179 * normal operation, see SerialInit().
180 */
181 "pg_serial",
182
183 /* Contents removed on startup, see DeleteAllExportedSnapshotFiles(). */
184 "pg_snapshots",
185
186 /* Contents zeroed on startup, see StartupSUBTRANS(). */
187 "pg_subtrans",
188
189 /* end of list */
190 NULL
191};
192
193/*
194 * List of files excluded from backups.
195 */
196static const struct exclude_list_item excludeFiles[] =
197{
198 /* Skip auto conf temporary file. */
199 {PG_AUTOCONF_FILENAME ".tmp", false},
200
201 /* Skip current log file temporary file */
203
204 /*
205 * Skip relation cache because it is rebuilt on startup. This includes
206 * temporary files.
207 */
209
210 /*
211 * backup_label and tablespace_map should not exist in a running cluster
212 * capable of doing an online backup, but exclude them just in case.
213 */
214 {BACKUP_LABEL_FILE, false},
215 {TABLESPACE_MAP, false},
216
217 /*
218 * If there's a backup_manifest, it belongs to a backup that was used to
219 * start this server. It is *not* correct for this backup. Our
220 * backup_manifest is injected into the backup separately if users want
221 * it.
222 */
223 {"backup_manifest", false},
224
225 {"postmaster.pid", false},
226 {"postmaster.opts", false},
227
228 /* end of list */
229 {NULL, false}
230};
231
232/*
233 * Actually do a base backup for the specified tablespaces.
234 *
235 * This is split out mainly to avoid complaints about "variable might be
236 * clobbered by longjmp" from stupider versions of gcc.
237 */
238static void
241{
243 XLogRecPtr endptr;
248
249 /* Initial backup state, insofar as we know it now. */
250 state.tablespaces = NIL;
251 state.tablespace_num = 0;
252 state.bytes_done = 0;
253 state.bytes_total = 0;
254 state.bytes_total_is_valid = false;
255
256 /* we're going to use a BufFile, so we need a ResourceOwner */
261
263
266
268
269 /* Allocate backup related variables. */
272
274 do_pg_backup_start(opt->label, opt->fastcheckpoint, &state.tablespaces,
276
277 state.startptr = backup_state->startpoint;
278 state.starttli = backup_state->starttli;
279
280 /*
281 * Once do_pg_backup_start has been called, ensure that any failure causes
282 * us to abort the backup so we don't "leak" a backup counter. For this
283 * reason, *all* functionality between do_pg_backup_start() and the end of
284 * do_pg_backup_stop() should be inside the error cleanup block!
285 */
286
288 {
289 ListCell *lc;
291
292 /* If this is an incremental backup, execute preparatory steps. */
293 if (ib != NULL)
295
296 /* Add a node for the base directory at the end */
298 newti->size = -1;
299 state.tablespaces = lappend(state.tablespaces, newti);
300
301 /*
302 * Calculate the total backup size by summing up the size of each
303 * tablespace
304 */
305 if (opt->progress)
306 {
308
309 foreach(lc, state.tablespaces)
310 {
312
313 if (tmp->path == NULL)
314 tmp->size = sendDir(sink, ".", 1, true, state.tablespaces,
315 true, NULL, InvalidOid, NULL);
316 else
317 tmp->size = sendTablespace(sink, tmp->path, tmp->oid, true,
318 NULL, NULL);
319 state.bytes_total += tmp->size;
320 }
321 state.bytes_total_is_valid = true;
322 }
323
324 /* notify basebackup sink about start of backup */
326
327 /* Send off our tablespaces one by one */
328 foreach(lc, state.tablespaces)
329 {
331
332 if (ti->path == NULL)
333 {
334 struct stat statbuf;
335 bool sendtblspclinks = true;
336 char *backup_label;
337
338 bbsink_begin_archive(sink, "base.tar");
339
340 /* In the main tar, include the backup_label first... */
343 backup_label, -1, &manifest);
345
346 /* Then the tablespace_map file, if required... */
347 if (opt->sendtblspcmapfile)
348 {
351 sendtblspclinks = false;
352 }
353
354 /* Then the bulk of the files... */
355 sendDir(sink, ".", 1, false, state.tablespaces,
357
358 /* ... and pg_control after everything else. */
359 if (lstat(XLOG_CONTROL_FILE, &statbuf) != 0)
362 errmsg("could not stat file \"%s\": %m",
365 false, InvalidOid, InvalidOid,
367 }
368 else
369 {
370 char *archive_name = psprintf("%u.tar", ti->oid);
371
372 bbsink_begin_archive(sink, archive_name);
373
374 sendTablespace(sink, ti->path, ti->oid, false, &manifest, ib);
375 }
376
377 /*
378 * If we're including WAL, and this is the main data directory we
379 * don't treat this as the end of the tablespace. Instead, we will
380 * include the xlog files below and stop afterwards. This is safe
381 * since the main data directory is always sent *last*.
382 */
383 if (opt->includewal && ti->path == NULL)
384 {
385 Assert(lnext(state.tablespaces, lc) == NULL);
386 }
387 else
388 {
389 /* Properly terminate the tarfile. */
392
393 /* OK, that's the end of the archive. */
395 }
396 }
397
400
401 endptr = backup_state->stoppoint;
403
404 /* Deallocate backup-related variables. */
407 }
409
410
411 if (opt->includewal)
412 {
413 /*
414 * We've left the last tar file "open", so we can now append the
415 * required WAL files to it.
416 */
417 char pathbuf[MAXPGPATH];
418 XLogSegNo segno;
421 struct stat statbuf;
424 char firstoff[MAXFNAMELEN];
425 char lastoff[MAXFNAMELEN];
426 DIR *dir;
427 struct dirent *de;
428 ListCell *lc;
429 TimeLineID tli;
430
432
433 /*
434 * I'd rather not worry about timelines here, so scan pg_wal and
435 * include all WAL files in the range between 'startptr' and 'endptr',
436 * regardless of the timeline the file is stamped with. If there are
437 * some spurious WAL files belonging to timelines that don't belong in
438 * this server's history, they will be included too. Normally there
439 * shouldn't be such files, but if there are, there's little harm in
440 * including them.
441 */
446
447 dir = AllocateDir("pg_wal");
448 while ((de = ReadDir(dir, "pg_wal")) != NULL)
449 {
450 /* Does it look like a WAL segment, and is it in the range? */
451 if (IsXLogFileName(de->d_name) &&
452 strcmp(de->d_name + 8, firstoff + 8) >= 0 &&
453 strcmp(de->d_name + 8, lastoff + 8) <= 0)
454 {
456 }
457 /* Does it look like a timeline history file? */
458 else if (IsTLHistoryFileName(de->d_name))
459 {
461 }
462 }
463 FreeDir(dir);
464
465 /*
466 * Before we go any further, check that none of the WAL segments we
467 * need were removed.
468 */
470
471 /*
472 * Sort the WAL filenames. We want to send the files in order from
473 * oldest to newest, to reduce the chance that a file is recycled
474 * before we get a chance to send it over.
475 */
477
478 /*
479 * There must be at least one xlog file in the pg_wal directory, since
480 * we are doing backup-including-xlog.
481 */
482 if (walFileList == NIL)
484 (errmsg("could not find any WAL files")));
485
486 /*
487 * Sanity check: the first and last segment should cover startptr and
488 * endptr, with no gaps in between.
489 */
491 &tli, &segno, wal_segment_size);
492 if (segno != startsegno)
493 {
495
499 (errmsg("could not find WAL file \"%s\"", startfname)));
500 }
501 foreach(lc, walFileList)
502 {
503 char *walFileName = (char *) lfirst(lc);
504 XLogSegNo currsegno = segno;
505 XLogSegNo nextsegno = segno + 1;
506
508 if (!(nextsegno == segno || currsegno == segno))
509 {
511
514 (errmsg("could not find WAL file \"%s\"", nextfname)));
515 }
516 }
517 if (segno != endsegno)
518 {
519 char endfname[MAXFNAMELEN];
520
523 (errmsg("could not find WAL file \"%s\"", endfname)));
524 }
525
526 /* Ok, we have everything we need. Send the WAL files. */
527 foreach(lc, walFileList)
528 {
529 char *walFileName = (char *) lfirst(lc);
530 int fd;
531 ssize_t cnt;
532 pgoff_t len = 0;
533
536
538 if (fd < 0)
539 {
540 int save_errno = errno;
541
542 /*
543 * Most likely reason for this is that the file was already
544 * removed by a checkpoint, so check for that to get a better
545 * error message.
546 */
547 CheckXLogRemoved(segno, tli);
548
552 errmsg("could not open file \"%s\": %m", pathbuf)));
553 }
554
555 if (fstat(fd, &statbuf) != 0)
558 errmsg("could not stat file \"%s\": %m",
559 pathbuf)));
560 if (statbuf.st_size != wal_segment_size)
561 {
562 CheckXLogRemoved(segno, tli);
565 errmsg("unexpected WAL file size \"%s\"", walFileName)));
566 }
567
568 /* send the WAL file itself */
570
571 while ((cnt = basebackup_read_file(fd, sink->bbs_buffer,
572 Min(sink->bbs_buffer_length,
574 len, pathbuf, true)) > 0)
575 {
576 CheckXLogRemoved(segno, tli);
578
579 len += cnt;
580
581 if (len == wal_segment_size)
582 break;
583 }
584
585 if (len != wal_segment_size)
586 {
587 CheckXLogRemoved(segno, tli);
590 errmsg("unexpected WAL file size \"%s\"", walFileName)));
591 }
592
593 /*
594 * wal_segment_size is a multiple of TAR_BLOCK_SIZE, so no need
595 * for padding.
596 */
598
600
601 /*
602 * Mark file as archived, otherwise files can get archived again
603 * after promotion of a new node. This is in line with
604 * walreceiver.c always doing an XLogArchiveForceDone() after a
605 * complete segment.
606 */
609 }
610
611 /*
612 * Send timeline history files too. Only the latest timeline history
613 * file is required for recovery, and even that only if there happens
614 * to be a timeline switch in the first WAL segment that contains the
615 * checkpoint record, or if we're taking a base backup from a standby
616 * server and the target timeline changes while the backup is taken.
617 * But they are small and highly useful for debugging purposes, so
618 * better include them all, always.
619 */
620 foreach(lc, historyFileList)
621 {
622 char *fname = lfirst(lc);
623
624 snprintf(pathbuf, MAXPGPATH, XLOGDIR "/%s", fname);
625
626 if (lstat(pathbuf, &statbuf) != 0)
629 errmsg("could not stat file \"%s\": %m", pathbuf)));
630
633 &manifest, 0, NULL, 0);
634
635 /* unconditionally mark file as archived */
636 StatusFilePath(pathbuf, fname, ".done");
638 }
639
640 /* Properly terminate the tar file. */
643
644 /* OK, that's the end of the archive. */
646 }
647
648 AddWALInfoToBackupManifest(&manifest, state.startptr, state.starttli,
649 endptr, endtli);
650
652
653 bbsink_end_backup(sink, endptr, endtli);
654
656 {
659 (errmsg_plural("%lld total checksum verification failure",
660 "%lld total checksum verification failures",
663
666 errmsg("checksum verification failure during base backup")));
667 }
668
669 /*
670 * Make sure to free the manifest before the resource owners as manifests
671 * use cryptohash contexts that may depend on resource owners (like
672 * OpenSSL).
673 */
675
676 /* clean up the resource owner we created */
678
680}
681
682/*
683 * list_sort comparison function, to compare log/seg portion of WAL segment
684 * filenames, ignoring the timeline portion.
685 */
686static int
688{
689 char *fna = (char *) lfirst(a);
690 char *fnb = (char *) lfirst(b);
691
692 return strcmp(fna + 8, fnb + 8);
693}
694
695/*
696 * Parse the base backup options passed down by the parser
697 */
698static void
700{
701 ListCell *lopt;
702 bool o_label = false;
703 bool o_progress = false;
704 bool o_checkpoint = false;
705 bool o_nowait = false;
706 bool o_wal = false;
707 bool o_incremental = false;
708 bool o_maxrate = false;
709 bool o_tablespace_map = false;
710 bool o_noverify_checksums = false;
711 bool o_manifest = false;
712 bool o_manifest_checksums = false;
713 bool o_target = false;
714 bool o_target_detail = false;
715 char *target_str = NULL;
716 char *target_detail_str = NULL;
717 bool o_compression = false;
718 bool o_compression_detail = false;
720
721 MemSet(opt, 0, sizeof(*opt));
726
727 foreach(lopt, options)
728 {
730
731 if (strcmp(defel->defname, "label") == 0)
732 {
733 if (o_label)
736 errmsg("duplicate option \"%s\"", defel->defname)));
737 opt->label = defGetString(defel);
738 o_label = true;
739 }
740 else if (strcmp(defel->defname, "progress") == 0)
741 {
742 if (o_progress)
745 errmsg("duplicate option \"%s\"", defel->defname)));
747 o_progress = true;
748 }
749 else if (strcmp(defel->defname, "checkpoint") == 0)
750 {
751 char *optval = defGetString(defel);
752
753 if (o_checkpoint)
756 errmsg("duplicate option \"%s\"", defel->defname)));
757 if (pg_strcasecmp(optval, "fast") == 0)
758 opt->fastcheckpoint = true;
759 else if (pg_strcasecmp(optval, "spread") == 0)
760 opt->fastcheckpoint = false;
761 else
764 errmsg("unrecognized checkpoint type: \"%s\"",
765 optval)));
766 o_checkpoint = true;
767 }
768 else if (strcmp(defel->defname, "wait") == 0)
769 {
770 if (o_nowait)
773 errmsg("duplicate option \"%s\"", defel->defname)));
774 opt->nowait = !defGetBoolean(defel);
775 o_nowait = true;
776 }
777 else if (strcmp(defel->defname, "wal") == 0)
778 {
779 if (o_wal)
782 errmsg("duplicate option \"%s\"", defel->defname)));
784 o_wal = true;
785 }
786 else if (strcmp(defel->defname, "incremental") == 0)
787 {
788 if (o_incremental)
791 errmsg("duplicate option \"%s\"", defel->defname)));
793 if (opt->incremental && !summarize_wal)
796 errmsg("incremental backups cannot be taken unless WAL summarization is enabled")));
797 o_incremental = true;
798 }
799 else if (strcmp(defel->defname, "max_rate") == 0)
800 {
802
803 if (o_maxrate)
806 errmsg("duplicate option \"%s\"", defel->defname)));
807
812 errmsg("%" PRId64 " is outside the valid range for parameter \"%s\" (%d .. %d)",
813 maxrate, "MAX_RATE", MAX_RATE_LOWER, MAX_RATE_UPPER)));
814
815 opt->maxrate = (uint32) maxrate;
816 o_maxrate = true;
817 }
818 else if (strcmp(defel->defname, "tablespace_map") == 0)
819 {
823 errmsg("duplicate option \"%s\"", defel->defname)));
825 o_tablespace_map = true;
826 }
827 else if (strcmp(defel->defname, "verify_checksums") == 0)
828 {
832 errmsg("duplicate option \"%s\"", defel->defname)));
835 }
836 else if (strcmp(defel->defname, "manifest") == 0)
837 {
838 char *optval = defGetString(defel);
839 bool manifest_bool;
840
841 if (o_manifest)
844 errmsg("duplicate option \"%s\"", defel->defname)));
846 {
847 if (manifest_bool)
849 else
851 }
852 else if (pg_strcasecmp(optval, "force-encode") == 0)
854 else
857 errmsg("unrecognized manifest option: \"%s\"",
858 optval)));
859 o_manifest = true;
860 }
861 else if (strcmp(defel->defname, "manifest_checksums") == 0)
862 {
863 char *optval = defGetString(defel);
864
868 errmsg("duplicate option \"%s\"", defel->defname)));
873 errmsg("unrecognized checksum algorithm: \"%s\"",
874 optval)));
876 }
877 else if (strcmp(defel->defname, "target") == 0)
878 {
879 if (o_target)
882 errmsg("duplicate option \"%s\"", defel->defname)));
884 o_target = true;
885 }
886 else if (strcmp(defel->defname, "target_detail") == 0)
887 {
888 char *optval = defGetString(defel);
889
890 if (o_target_detail)
893 errmsg("duplicate option \"%s\"", defel->defname)));
895 o_target_detail = true;
896 }
897 else if (strcmp(defel->defname, "compression") == 0)
898 {
899 char *optval = defGetString(defel);
900
901 if (o_compression)
904 errmsg("duplicate option \"%s\"", defel->defname)));
908 errmsg("unrecognized compression algorithm: \"%s\"",
909 optval)));
910 o_compression = true;
911 }
912 else if (strcmp(defel->defname, "compression_detail") == 0)
913 {
917 errmsg("duplicate option \"%s\"", defel->defname)));
920 }
921 else
924 errmsg("unrecognized base backup option: \"%s\"",
925 defel->defname)));
926 }
927
928 if (opt->label == NULL)
929 opt->label = "base backup";
930 if (opt->manifest == MANIFEST_OPTION_NO)
931 {
935 errmsg("manifest checksums require a backup manifest")));
937 }
938
939 if (target_str == NULL)
940 {
941 if (target_detail_str != NULL)
944 errmsg("target detail cannot be used without target")));
945 opt->use_copytblspc = true;
946 opt->send_to_client = true;
947 }
948 else if (strcmp(target_str, "client") == 0)
949 {
950 if (target_detail_str != NULL)
953 errmsg("target \"%s\" does not accept a target detail",
954 target_str)));
955 opt->send_to_client = true;
956 }
957 else
958 opt->target_handle =
960
964 errmsg("compression detail cannot be specified unless compression is enabled")));
965
966 if (o_compression)
967 {
968 char *error_detail;
969
974 if (error_detail != NULL)
977 errmsg("invalid compression specification: %s",
978 error_detail));
979 }
980}
981
982
983/*
984 * SendBaseBackup() - send a complete base backup.
985 *
986 * The function will put the system into backup mode like pg_backup_start()
987 * does, so that the backup is consistent even though we read directly from
988 * the filesystem, bypassing the buffer cache.
989 */
990void
992{
994 bbsink *sink;
996
997 if (status == SESSION_BACKUP_RUNNING)
1000 errmsg("a backup is already in progress in this session")));
1001
1003
1005
1007 {
1008 char activitymsg[50];
1009
1010 snprintf(activitymsg, sizeof(activitymsg), "sending backup \"%s\"",
1011 opt.label);
1013 }
1014
1015 /*
1016 * If we're asked to perform an incremental backup and the user has not
1017 * supplied a manifest, that's an ERROR.
1018 *
1019 * If we're asked to perform a full backup and the user did supply a
1020 * manifest, just ignore it.
1021 */
1022 if (!opt.incremental)
1023 ib = NULL;
1024 else if (ib == NULL)
1025 ereport(ERROR,
1027 errmsg("must UPLOAD_MANIFEST before performing an incremental BASE_BACKUP")));
1028
1029 /*
1030 * If the target is specifically 'client' then set up to stream the backup
1031 * to the client; otherwise, it's being sent someplace else and should not
1032 * be sent to the client. BaseBackupGetSink has the job of setting up a
1033 * sink to send the backup data wherever it needs to go.
1034 */
1036 if (opt.target_handle != NULL)
1038
1039 /* Set up network throttling, if client requested it */
1040 if (opt.maxrate > 0)
1042
1043 /* Set up server-side compression, if client requested it */
1046 else if (opt.compression == PG_COMPRESSION_LZ4)
1048 else if (opt.compression == PG_COMPRESSION_ZSTD)
1050
1051 /* Set up progress reporting. */
1053
1054 /*
1055 * Perform the base backup, but make sure we clean up the bbsink even if
1056 * an error occurs.
1057 */
1058 PG_TRY();
1059 {
1060 perform_base_backup(&opt, sink, ib);
1061 }
1062 PG_FINALLY();
1063 {
1065 }
1066 PG_END_TRY();
1067}
1068
1069/*
1070 * Inject a file with given name and content in the output tar stream.
1071 *
1072 * "len" can optionally be set to an arbitrary length of data sent. If set
1073 * to -1, the content sent is treated as a string with strlen() as length.
1074 */
1075static void
1076sendFileWithContent(bbsink *sink, const char *filename, const char *content,
1078{
1079 struct stat statbuf;
1080 int bytes_done = 0;
1081 pg_checksum_context checksum_ctx;
1082
1083 if (pg_checksum_init(&checksum_ctx, manifest->checksum_type) < 0)
1084 elog(ERROR, "could not initialize checksum of file \"%s\"",
1085 filename);
1086
1087 if (len < 0)
1088 len = strlen(content);
1089
1090 /*
1091 * Construct a stat struct for the file we're injecting in the tar.
1092 */
1093
1094 /* Windows doesn't have the concept of uid and gid */
1095#ifdef WIN32
1096 statbuf.st_uid = 0;
1097 statbuf.st_gid = 0;
1098#else
1099 statbuf.st_uid = geteuid();
1100 statbuf.st_gid = getegid();
1101#endif
1102 statbuf.st_mtime = time(NULL);
1103 statbuf.st_mode = pg_file_create_mode;
1104 statbuf.st_size = len;
1105
1107
1108 if (pg_checksum_update(&checksum_ctx, (const uint8 *) content, len) < 0)
1109 elog(ERROR, "could not update checksum of file \"%s\"",
1110 filename);
1111
1112 while (bytes_done < len)
1113 {
1114 size_t remaining = len - bytes_done;
1115 size_t nbytes = Min(sink->bbs_buffer_length, remaining);
1116
1117 memcpy(sink->bbs_buffer, content, nbytes);
1119 bytes_done += nbytes;
1120 content += nbytes;
1121 }
1122
1124
1126 (pg_time_t) statbuf.st_mtime, &checksum_ctx);
1127}
1128
1129/*
1130 * Include the tablespace directory pointed to by 'path' in the output tar
1131 * stream. If 'sizeonly' is true, we just calculate a total length and return
1132 * it, without actually sending anything.
1133 *
1134 * Only used to send auxiliary tablespaces, not PGDATA.
1135 */
1136static int64
1139{
1140 int64 size;
1141 char pathbuf[MAXPGPATH];
1142 struct stat statbuf;
1143
1144 /*
1145 * 'path' points to the tablespace location, but we only want to include
1146 * the version directory in it that belongs to us.
1147 */
1148 snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path,
1150
1151 /*
1152 * Store a directory entry in the tar file so we get the permissions
1153 * right.
1154 */
1155 if (lstat(pathbuf, &statbuf) != 0)
1156 {
1157 if (errno != ENOENT)
1158 ereport(ERROR,
1160 errmsg("could not stat file or directory \"%s\": %m",
1161 pathbuf)));
1162
1163 /* If the tablespace went away while scanning, it's no error. */
1164 return 0;
1165 }
1166
1168 sizeonly);
1169
1170 /* Send all the files in the tablespace version directory */
1171 size += sendDir(sink, pathbuf, strlen(path), sizeonly, NIL, true, manifest,
1172 spcoid, ib);
1173
1174 return size;
1175}
1176
1177/*
1178 * Include all files from the given directory in the output tar stream. If
1179 * 'sizeonly' is true, we just calculate a total length and return it, without
1180 * actually sending anything.
1181 *
1182 * Omit any directory in the tablespaces list, to avoid backing up
1183 * tablespaces twice when they were created inside PGDATA.
1184 *
1185 * If sendtblspclinks is true, we need to include symlink
1186 * information in the tar file. If not, we can skip that
1187 * as it will be sent separately in the tablespace_map file.
1188 */
1189static int64
1190sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly,
1193{
1194 DIR *dir;
1195 struct dirent *de;
1196 char pathbuf[MAXPGPATH * 2];
1197 struct stat statbuf;
1198 int64 size = 0;
1199 const char *lastDir; /* Split last dir from parent path. */
1200 bool isRelationDir = false; /* Does directory contain relations? */
1201 bool isGlobalDir = false;
1202 Oid dboid = InvalidOid;
1203 BlockNumber *relative_block_numbers = NULL;
1204
1205 /*
1206 * Since this array is relatively large, avoid putting it on the stack.
1207 * But we don't need it at all if this is not an incremental backup.
1208 */
1209 if (ib != NULL)
1210 relative_block_numbers = palloc_array(BlockNumber, RELSEG_SIZE);
1211
1212 /*
1213 * Determine if the current path is a database directory that can contain
1214 * relations.
1215 *
1216 * Start by finding the location of the delimiter between the parent path
1217 * and the current path.
1218 */
1220
1221 /* Does this path look like a database path (i.e. all digits)? */
1222 if (lastDir != NULL &&
1223 strspn(lastDir + 1, "0123456789") == strlen(lastDir + 1))
1224 {
1225 /* Part of path that contains the parent directory. */
1226 int parentPathLen = lastDir - path;
1227
1228 /*
1229 * Mark path as a database directory if the parent path is either
1230 * $PGDATA/base or a tablespace version path.
1231 */
1232 if (strncmp(path, "./base", parentPathLen) == 0 ||
1233 (parentPathLen >= (sizeof(TABLESPACE_VERSION_DIRECTORY) - 1) &&
1236 sizeof(TABLESPACE_VERSION_DIRECTORY) - 1) == 0))
1237 {
1238 isRelationDir = true;
1239 dboid = atooid(lastDir + 1);
1240 }
1241 }
1242 else if (strcmp(path, "./global") == 0)
1243 {
1244 isRelationDir = true;
1245 isGlobalDir = true;
1246 }
1247
1248 dir = AllocateDir(path);
1249 while ((de = ReadDir(dir, path)) != NULL)
1250 {
1251 int excludeIdx;
1252 bool excludeFound;
1253 RelFileNumber relfilenumber = InvalidRelFileNumber;
1255 unsigned segno = 0;
1256 bool isRelationFile = false;
1257
1258 /* Skip special stuff */
1259 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1260 continue;
1261
1262 /* Skip temporary files */
1263 if (strncmp(de->d_name,
1266 continue;
1267
1268 /* Skip macOS system files */
1269 if (strcmp(de->d_name, ".DS_Store") == 0)
1270 continue;
1271
1272 /*
1273 * Check if the postmaster has signaled us to exit, and abort with an
1274 * error in that case. The error handler further up will call
1275 * do_pg_abort_backup() for us. Also check that if the backup was
1276 * started while still in recovery, the server wasn't promoted.
1277 * do_pg_backup_stop() will check that too, but it's better to stop
1278 * the backup early than continue to the end and fail there.
1279 */
1282 ereport(ERROR,
1284 errmsg("the standby was promoted during online backup"),
1285 errhint("This means that the backup being taken is corrupt "
1286 "and should not be used. "
1287 "Try taking another online backup.")));
1288
1289 /* Scan for files that should be excluded */
1290 excludeFound = false;
1292 {
1294
1295 if (!excludeFiles[excludeIdx].match_prefix)
1296 cmplen++;
1297 if (strncmp(de->d_name, excludeFiles[excludeIdx].name, cmplen) == 0)
1298 {
1299 elog(DEBUG1, "file \"%s\" excluded from backup", de->d_name);
1300 excludeFound = true;
1301 break;
1302 }
1303 }
1304
1305 if (excludeFound)
1306 continue;
1307
1308 /*
1309 * If there could be non-temporary relation files in this directory,
1310 * try to parse the filename.
1311 */
1312 if (isRelationDir)
1315 &relfilenumber,
1316 &relForkNum, &segno);
1317
1318 /* Exclude all forks for unlogged tables except the init fork */
1320 {
1321 char initForkFile[MAXPGPATH];
1322
1323 /*
1324 * If any other type of fork, check if there is an init fork with
1325 * the same RelFileNumber. If so, the file can be excluded.
1326 */
1327 snprintf(initForkFile, sizeof(initForkFile), "%s/%u_init",
1328 path, relfilenumber);
1329
1330 if (lstat(initForkFile, &statbuf) == 0)
1331 {
1332 elog(DEBUG2,
1333 "unlogged relation file \"%s\" excluded from backup",
1334 de->d_name);
1335
1336 continue;
1337 }
1338 }
1339
1340 /* Exclude temporary relations */
1341 if (OidIsValid(dboid) && looks_like_temp_rel_name(de->d_name))
1342 {
1343 elog(DEBUG2,
1344 "temporary relation file \"%s\" excluded from backup",
1345 de->d_name);
1346
1347 continue;
1348 }
1349
1350 snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path, de->d_name);
1351
1352 /* Skip pg_control here to back up it last */
1353 if (strcmp(pathbuf, "./" XLOG_CONTROL_FILE) == 0)
1354 continue;
1355
1356 if (lstat(pathbuf, &statbuf) != 0)
1357 {
1358 if (errno != ENOENT)
1359 ereport(ERROR,
1361 errmsg("could not stat file or directory \"%s\": %m",
1362 pathbuf)));
1363
1364 /* If the file went away while scanning, it's not an error. */
1365 continue;
1366 }
1367
1368 /* Scan for directories whose contents should be excluded */
1369 excludeFound = false;
1371 {
1372 if (strcmp(de->d_name, excludeDirContents[excludeIdx]) == 0)
1373 {
1374 elog(DEBUG1, "contents of directory \"%s\" excluded from backup", de->d_name);
1377 &statbuf, sizeonly);
1378 excludeFound = true;
1379 break;
1380 }
1381 }
1382
1383 if (excludeFound)
1384 continue;
1385
1386 /*
1387 * We can skip pg_wal, the WAL segments need to be fetched from the
1388 * WAL archive anyway. But include it as an empty directory anyway, so
1389 * we get permissions right.
1390 */
1391 if (strcmp(pathbuf, "./pg_wal") == 0)
1392 {
1393 /* If pg_wal is a symlink, write it as a directory anyway */
1396 &statbuf, sizeonly);
1397
1398 /*
1399 * Also send archive_status and summaries directories (by
1400 * hackishly reusing statbuf from above ...).
1401 */
1402 size += _tarWriteHeader(sink, "./pg_wal/archive_status", NULL,
1403 &statbuf, sizeonly);
1404 size += _tarWriteHeader(sink, "./pg_wal/summaries", NULL,
1405 &statbuf, sizeonly);
1406
1407 continue; /* don't recurse into pg_wal */
1408 }
1409
1410 /* Allow symbolic links in pg_tblspc only */
1411 if (strcmp(path, "./pg_tblspc") == 0 && S_ISLNK(statbuf.st_mode))
1412 {
1413 char linkpath[MAXPGPATH];
1414 int rllen;
1415
1416 rllen = readlink(pathbuf, linkpath, sizeof(linkpath));
1417 if (rllen < 0)
1418 ereport(ERROR,
1420 errmsg("could not read symbolic link \"%s\": %m",
1421 pathbuf)));
1422 if (rllen >= sizeof(linkpath))
1423 ereport(ERROR,
1425 errmsg("symbolic link \"%s\" target is too long",
1426 pathbuf)));
1427 linkpath[rllen] = '\0';
1428
1430 &statbuf, sizeonly);
1431 }
1432 else if (S_ISDIR(statbuf.st_mode))
1433 {
1434 bool skip_this_dir = false;
1435 ListCell *lc;
1436
1437 /*
1438 * Store a directory entry in the tar file so we can get the
1439 * permissions right.
1440 */
1442 sizeonly);
1443
1444 /*
1445 * Call ourselves recursively for a directory, unless it happens
1446 * to be a separate tablespace located within PGDATA.
1447 */
1448 foreach(lc, tablespaces)
1449 {
1451
1452 /*
1453 * ti->rpath is the tablespace relative path within PGDATA, or
1454 * NULL if the tablespace has been properly located somewhere
1455 * else.
1456 *
1457 * Skip past the leading "./" in pathbuf when comparing.
1458 */
1459 if (ti->rpath && strcmp(ti->rpath, pathbuf + 2) == 0)
1460 {
1461 skip_this_dir = true;
1462 break;
1463 }
1464 }
1465
1466 /*
1467 * skip sending directories inside pg_tblspc, if not required.
1468 */
1469 if (strcmp(pathbuf, "./pg_tblspc") == 0 && !sendtblspclinks)
1470 skip_this_dir = true;
1471
1472 if (!skip_this_dir)
1473 size += sendDir(sink, pathbuf, basepathlen, sizeonly, tablespaces,
1475 }
1476 else if (S_ISREG(statbuf.st_mode))
1477 {
1478 bool sent = false;
1479 unsigned num_blocks_required = 0;
1480 unsigned truncation_block_length = 0;
1481 char tarfilenamebuf[MAXPGPATH * 2];
1482 char *tarfilename = pathbuf + basepathlen + 1;
1484
1485 if (ib != NULL && isRelationFile)
1486 {
1487 Oid relspcoid;
1488 char *lookup_path;
1489
1490 if (OidIsValid(spcoid))
1491 {
1492 relspcoid = spcoid;
1494 tarfilename);
1495 }
1496 else
1497 {
1498 if (isGlobalDir)
1500 else
1502 lookup_path = pstrdup(tarfilename);
1503 }
1504
1505 method = GetFileBackupMethod(ib, lookup_path, dboid, relspcoid,
1506 relfilenumber, relForkNum,
1507 segno, statbuf.st_size,
1509 relative_block_numbers,
1510 &truncation_block_length);
1511 if (method == BACK_UP_FILE_INCREMENTALLY)
1512 {
1513 statbuf.st_size =
1516 "%s/INCREMENTAL.%s",
1517 path + basepathlen + 1,
1518 de->d_name);
1519 tarfilename = tarfilenamebuf;
1520 }
1521
1523 }
1524
1525 if (!sizeonly)
1526 sent = sendFile(sink, pathbuf, tarfilename, &statbuf,
1527 true, dboid, spcoid,
1528 relfilenumber, segno, manifest,
1530 method == BACK_UP_FILE_INCREMENTALLY ? relative_block_numbers : NULL,
1531 truncation_block_length);
1532
1533 if (sent || sizeonly)
1534 {
1535 /* Add size. */
1536 size += statbuf.st_size;
1537
1538 /* Pad to a multiple of the tar block size. */
1539 size += tarPaddingBytesRequired(statbuf.st_size);
1540
1541 /* Size of the header for the file. */
1542 size += TAR_BLOCK_SIZE;
1543 }
1544 }
1545 else
1547 (errmsg("skipping special file \"%s\"", pathbuf)));
1548 }
1549
1550 if (relative_block_numbers != NULL)
1551 pfree(relative_block_numbers);
1552
1553 FreeDir(dir);
1554 return size;
1555}
1556
1557/*
1558 * Given the member, write the TAR header & send the file.
1559 *
1560 * If 'missing_ok' is true, will not throw an error if the file is not found.
1561 *
1562 * If dboid is anything other than InvalidOid then any checksum failures
1563 * detected will get reported to the cumulative stats system.
1564 *
1565 * If the file is to be sent incrementally, then num_incremental_blocks
1566 * should be the number of blocks to be sent, and incremental_blocks
1567 * an array of block numbers relative to the start of the current segment.
1568 * If the whole file is to be sent, then incremental_blocks should be NULL,
1569 * and num_incremental_blocks can have any value, as it will be ignored.
1570 *
1571 * Returns true if the file was successfully sent, false if 'missing_ok',
1572 * and the file did not exist.
1573 */
1574static bool
1575sendFile(bbsink *sink, const char *readfilename, const char *tarfilename,
1576 struct stat *statbuf, bool missing_ok, Oid dboid, Oid spcoid,
1577 RelFileNumber relfilenumber, unsigned segno,
1579 BlockNumber *incremental_blocks, unsigned truncation_block_length)
1580{
1581 int fd;
1582 BlockNumber blkno = 0;
1583 int checksum_failures = 0;
1584 off_t cnt;
1585 pgoff_t bytes_done = 0;
1586 bool verify_checksum = false;
1587 pg_checksum_context checksum_ctx;
1588 int ibindex = 0;
1589
1590 if (pg_checksum_init(&checksum_ctx, manifest->checksum_type) < 0)
1591 elog(ERROR, "could not initialize checksum of file \"%s\"",
1592 readfilename);
1593
1595 if (fd < 0)
1596 {
1597 if (errno == ENOENT && missing_ok)
1598 return false;
1599 ereport(ERROR,
1601 errmsg("could not open file \"%s\": %m", readfilename)));
1602 }
1603
1604 _tarWriteHeader(sink, tarfilename, NULL, statbuf, false);
1605
1606 /*
1607 * Checksums are verified in multiples of BLCKSZ, so the buffer length
1608 * should be a multiple of the block size as well.
1609 */
1610 Assert((sink->bbs_buffer_length % BLCKSZ) == 0);
1611
1612 /*
1613 * If we weren't told not to verify checksums, and if checksums are
1614 * enabled for this cluster, and if this is a relation file, then verify
1615 * the checksum.
1616 */
1618 RelFileNumberIsValid(relfilenumber))
1619 verify_checksum = true;
1620
1621 /*
1622 * If we're sending an incremental file, write the file header.
1623 */
1624 if (incremental_blocks != NULL)
1625 {
1626 unsigned magic = INCREMENTAL_MAGIC;
1627 size_t header_bytes_done = 0;
1628 char padding[BLCKSZ];
1629 size_t paddinglen;
1630
1631 /* Emit header data. */
1632 push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1633 &magic, sizeof(magic));
1634 push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1636 push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1637 &truncation_block_length, sizeof(truncation_block_length));
1638 push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1641
1642 /*
1643 * Add padding to align header to a multiple of BLCKSZ, but only if
1644 * the incremental file has some blocks, and the alignment is actually
1645 * needed (i.e. header is not already a multiple of BLCKSZ). If there
1646 * are no blocks we don't want to make the file unnecessarily large,
1647 * as that might make some filesystem optimizations impossible.
1648 */
1649 if ((num_incremental_blocks > 0) && (header_bytes_done % BLCKSZ != 0))
1650 {
1652
1653 memset(padding, 0, paddinglen);
1654 bytes_done += paddinglen;
1655
1656 push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1657 padding, paddinglen);
1658 }
1659
1660 /* Flush out any data still in the buffer so it's again empty. */
1661 if (header_bytes_done > 0)
1662 {
1664 if (pg_checksum_update(&checksum_ctx,
1665 (uint8 *) sink->bbs_buffer,
1666 header_bytes_done) < 0)
1667 elog(ERROR, "could not update checksum of base backup");
1668 }
1669
1670 /* Update our notion of file position. */
1671 bytes_done += sizeof(magic);
1672 bytes_done += sizeof(num_incremental_blocks);
1673 bytes_done += sizeof(truncation_block_length);
1674 bytes_done += sizeof(BlockNumber) * num_incremental_blocks;
1675 }
1676
1677 /*
1678 * Loop until we read the amount of data the caller told us to expect. The
1679 * file could be longer, if it was extended while we were sending it, but
1680 * for a base backup we can ignore such extended data. It will be restored
1681 * from WAL.
1682 */
1683 while (1)
1684 {
1685 /*
1686 * Determine whether we've read all the data that we need, and if not,
1687 * read some more.
1688 */
1689 if (incremental_blocks == NULL)
1690 {
1691 size_t remaining = statbuf->st_size - bytes_done;
1692
1693 /*
1694 * If we've read the required number of bytes, then it's time to
1695 * stop.
1696 */
1697 if (bytes_done >= statbuf->st_size)
1698 break;
1699
1700 /*
1701 * Read as many bytes as will fit in the buffer, or however many
1702 * are left to read, whichever is less.
1703 */
1705 bytes_done, remaining,
1706 blkno + segno * RELSEG_SIZE,
1707 verify_checksum,
1708 &checksum_failures);
1709 }
1710 else
1711 {
1713
1714 /*
1715 * If we've read all the blocks, then it's time to stop.
1716 */
1718 break;
1719
1720 /*
1721 * Read just one block, whichever one is the next that we're
1722 * supposed to include.
1723 */
1727 BLCKSZ,
1728 relative_blkno + segno * RELSEG_SIZE,
1729 verify_checksum,
1730 &checksum_failures);
1731
1732 /*
1733 * If we get a partial read, that must mean that the relation is
1734 * being truncated. Ultimately, it should be truncated to a
1735 * multiple of BLCKSZ, since this path should only be reached for
1736 * relation files, but we might transiently observe an
1737 * intermediate value.
1738 *
1739 * It should be fine to treat this just as if the entire block had
1740 * been truncated away - i.e. fill this and all later blocks with
1741 * zeroes. WAL replay will fix things up.
1742 */
1743 if (cnt < BLCKSZ)
1744 break;
1745 }
1746
1747 /*
1748 * If the amount of data we were able to read was not a multiple of
1749 * BLCKSZ, we cannot verify checksums, which are block-level.
1750 */
1751 if (verify_checksum && (cnt % BLCKSZ != 0))
1752 {
1754 (errmsg("could not verify checksum in file \"%s\", block "
1755 "%u: read buffer size %d and page size %d "
1756 "differ",
1757 readfilename, blkno, (int) cnt, BLCKSZ)));
1758 verify_checksum = false;
1759 }
1760
1761 /*
1762 * If we hit end-of-file, a concurrent truncation must have occurred.
1763 * That's not an error condition, because WAL replay will fix things
1764 * up.
1765 */
1766 if (cnt == 0)
1767 break;
1768
1769 /* Update block number and # of bytes done for next loop iteration. */
1770 blkno += cnt / BLCKSZ;
1771 bytes_done += cnt;
1772
1773 /*
1774 * Make sure incremental files with block data are properly aligned
1775 * (header is a multiple of BLCKSZ, blocks are BLCKSZ too).
1776 */
1778 (bytes_done % BLCKSZ != 0)));
1779
1780 /* Archive the data we just read. */
1782
1783 /* Also feed it to the checksum machinery. */
1784 if (pg_checksum_update(&checksum_ctx,
1785 (uint8 *) sink->bbs_buffer, cnt) < 0)
1786 elog(ERROR, "could not update checksum of base backup");
1787 }
1788
1789 /* If the file was truncated while we were sending it, pad it with zeros */
1790 while (bytes_done < statbuf->st_size)
1791 {
1792 size_t remaining = statbuf->st_size - bytes_done;
1793 size_t nbytes = Min(sink->bbs_buffer_length, remaining);
1794
1795 MemSet(sink->bbs_buffer, 0, nbytes);
1796 if (pg_checksum_update(&checksum_ctx,
1797 (uint8 *) sink->bbs_buffer,
1798 nbytes) < 0)
1799 elog(ERROR, "could not update checksum of base backup");
1801 bytes_done += nbytes;
1802 }
1803
1804 /*
1805 * Pad to a block boundary, per tar format requirements. (This small piece
1806 * of data is probably not worth throttling, and is not checksummed
1807 * because it's not actually part of the file.)
1808 */
1809 _tarWritePadding(sink, bytes_done);
1810
1812
1813 if (checksum_failures > 1)
1814 {
1816 (errmsg_plural("file \"%s\" has a total of %d checksum verification failure",
1817 "file \"%s\" has a total of %d checksum verification failures",
1818 checksum_failures,
1819 readfilename, checksum_failures)));
1820
1822 pgstat_report_checksum_failures_in_db(dboid, checksum_failures);
1823 }
1824
1825 total_checksum_failures += checksum_failures;
1826
1827 AddFileToBackupManifest(manifest, spcoid, tarfilename, statbuf->st_size,
1828 (pg_time_t) statbuf->st_mtime, &checksum_ctx);
1829
1830 return true;
1831}
1832
1833/*
1834 * Read some more data from the file into the bbsink's buffer, verifying
1835 * checksums as required.
1836 *
1837 * 'offset' is the file offset from which we should begin to read, and
1838 * 'length' is the amount of data that should be read. The actual amount
1839 * of data read will be less than the requested amount if the bbsink's
1840 * buffer isn't big enough to hold it all, or if the underlying file has
1841 * been truncated. The return value is the number of bytes actually read.
1842 *
1843 * 'blkno' is the block number of the first page in the bbsink's buffer
1844 * relative to the start of the relation.
1845 *
1846 * 'verify_checksum' indicates whether we should try to verify checksums
1847 * for the blocks we read. If we do this, we'll update *checksum_failures
1848 * and issue warnings as appropriate.
1849 */
1850static off_t
1852 off_t offset, size_t length, BlockNumber blkno,
1853 bool verify_checksum, int *checksum_failures)
1854{
1855 off_t cnt;
1856 int i;
1857 char *page;
1858
1859 /* Try to read some more data. */
1860 cnt = basebackup_read_file(fd, sink->bbs_buffer,
1861 Min(sink->bbs_buffer_length, length),
1862 offset, readfilename, true);
1863
1864 /* Can't verify checksums if read length is not a multiple of BLCKSZ. */
1865 if (!verify_checksum || (cnt % BLCKSZ) != 0)
1866 return cnt;
1867
1868 /* Verify checksum for each block. */
1869 for (i = 0; i < cnt / BLCKSZ; i++)
1870 {
1871 int reread_cnt;
1873
1874 page = sink->bbs_buffer + BLCKSZ * i;
1875
1876 /* If the page is OK, go on to the next one. */
1877 if (verify_page_checksum(page, sink->bbs_state->startptr, blkno + i,
1879 continue;
1880
1881 /*
1882 * Retry the block on the first failure. It's possible that we read
1883 * the first 4K page of the block just before postgres updated the
1884 * entire block so it ends up looking torn to us. If, before we retry
1885 * the read, the concurrent write of the block finishes, the page LSN
1886 * will be updated and we'll realize that we should ignore this block.
1887 *
1888 * There's no guarantee that this will actually happen, though: the
1889 * torn write could take an arbitrarily long time to complete.
1890 * Retrying multiple times wouldn't fix this problem, either, though
1891 * it would reduce the chances of it happening in practice. The only
1892 * real fix here seems to be to have some kind of interlock that
1893 * allows us to wait until we can be certain that no write to the
1894 * block is in progress. Since we don't have any such thing right now,
1895 * we just do this and hope for the best.
1896 */
1897 reread_cnt =
1898 basebackup_read_file(fd, sink->bbs_buffer + BLCKSZ * i,
1899 BLCKSZ, offset + BLCKSZ * i,
1900 readfilename, false);
1901 if (reread_cnt == 0)
1902 {
1903 /*
1904 * If we hit end-of-file, a concurrent truncation must have
1905 * occurred, so reduce cnt to reflect only the blocks already
1906 * processed and break out of this loop.
1907 */
1908 cnt = BLCKSZ * i;
1909 break;
1910 }
1911
1912 /* If the page now looks OK, go on to the next one. */
1913 if (verify_page_checksum(page, sink->bbs_state->startptr, blkno + i,
1915 continue;
1916
1917 /* Handle checksum failure. */
1918 (*checksum_failures)++;
1919 if (*checksum_failures <= 5)
1921 (errmsg("checksum verification failed in "
1922 "file \"%s\", block %u: calculated "
1923 "%X but expected %X",
1925 ((PageHeader) page)->pd_checksum)));
1926 if (*checksum_failures == 5)
1928 (errmsg("further checksum verification "
1929 "failures in file \"%s\" will not "
1930 "be reported", readfilename)));
1931 }
1932
1933 return cnt;
1934}
1935
1936/*
1937 * Push data into a bbsink.
1938 *
1939 * It's better, when possible, to read data directly into the bbsink's buffer,
1940 * rather than using this function to copy it into the buffer; this function is
1941 * for cases where that approach is not practical.
1942 *
1943 * bytes_done should point to a count of the number of bytes that are
1944 * currently used in the bbsink's buffer. Upon return, the bytes identified by
1945 * data and length will have been copied into the bbsink's buffer, flushing
1946 * as required, and *bytes_done will have been updated accordingly. If the
1947 * buffer was flushed, the previous contents will also have been fed to
1948 * checksum_ctx.
1949 *
1950 * Note that after one or more calls to this function it is the caller's
1951 * responsibility to perform any required final flush.
1952 */
1953static void
1955 size_t *bytes_done, void *data, size_t length)
1956{
1957 while (length > 0)
1958 {
1959 size_t bytes_to_copy;
1960
1961 /*
1962 * We use < here rather than <= so that if the data exactly fills the
1963 * remaining buffer space, we trigger a flush now.
1964 */
1965 if (length < sink->bbs_buffer_length - *bytes_done)
1966 {
1967 /* Append remaining data to buffer. */
1968 memcpy(sink->bbs_buffer + *bytes_done, data, length);
1969 *bytes_done += length;
1970 return;
1971 }
1972
1973 /* Copy until buffer is full and flush it. */
1974 bytes_to_copy = sink->bbs_buffer_length - *bytes_done;
1975 memcpy(sink->bbs_buffer + *bytes_done, data, bytes_to_copy);
1976 data = ((char *) data) + bytes_to_copy;
1977 length -= bytes_to_copy;
1978 bbsink_archive_contents(sink, sink->bbs_buffer_length);
1979 if (pg_checksum_update(checksum_ctx, (uint8 *) sink->bbs_buffer,
1980 sink->bbs_buffer_length) < 0)
1981 elog(ERROR, "could not update checksum");
1982 *bytes_done = 0;
1983 }
1984}
1985
1986/*
1987 * Try to verify the checksum for the provided page, if it seems appropriate
1988 * to do so.
1989 *
1990 * Returns true if verification succeeds or if we decide not to check it,
1991 * and false if verification fails. When return false, it also sets
1992 * *expected_checksum to the computed value.
1993 */
1994static bool
1997{
1998 PageHeader phdr;
1999 uint16 checksum;
2000
2001 /*
2002 * Only check pages which have not been modified since the start of the
2003 * base backup. Otherwise, they might have been written only halfway and
2004 * the checksum would not be valid. However, replaying WAL would
2005 * reinstate the correct page in this case. We also skip completely new
2006 * pages, since they don't have a checksum yet.
2007 */
2008 if (PageIsNew(page) || PageGetLSN(page) >= start_lsn)
2009 return true;
2010
2011 /* Perform the actual checksum calculation. */
2012 checksum = pg_checksum_page(page, blkno);
2013
2014 /* See whether it matches the value from the page. */
2015 phdr = (PageHeader) page;
2016 if (phdr->pd_checksum == checksum)
2017 return true;
2018 *expected_checksum = checksum;
2019 return false;
2020}
2021
2022static int64
2023_tarWriteHeader(bbsink *sink, const char *filename, const char *linktarget,
2024 struct stat *statbuf, bool sizeonly)
2025{
2026 enum tarError rc;
2027
2028 if (!sizeonly)
2029 {
2030 /*
2031 * As of this writing, the smallest supported block size is 1kB, which
2032 * is twice TAR_BLOCK_SIZE. Since the buffer size is required to be a
2033 * multiple of BLCKSZ, it should be safe to assume that the buffer is
2034 * large enough to fit an entire tar block. We double-check by means
2035 * of these assertions.
2036 */
2038 "BLCKSZ too small for tar block");
2039 Assert(sink->bbs_buffer_length >= TAR_BLOCK_SIZE);
2040
2041 rc = tarCreateHeader(sink->bbs_buffer, filename, linktarget,
2042 statbuf->st_size, statbuf->st_mode,
2043 statbuf->st_uid, statbuf->st_gid,
2044 statbuf->st_mtime);
2045
2046 switch (rc)
2047 {
2048 case TAR_OK:
2049 break;
2050 case TAR_NAME_TOO_LONG:
2051 ereport(ERROR,
2053 errmsg("file name too long for tar format: \"%s\"",
2054 filename)));
2055 break;
2057 ereport(ERROR,
2059 errmsg("symbolic link target too long for tar format: "
2060 "file name \"%s\", target \"%s\"",
2061 filename, linktarget)));
2062 break;
2063 default:
2064 elog(ERROR, "unrecognized tar error: %d", rc);
2065 }
2066
2068 }
2069
2070 return TAR_BLOCK_SIZE;
2071}
2072
2073/*
2074 * Pad with zero bytes out to a multiple of TAR_BLOCK_SIZE.
2075 */
2076static void
2078{
2079 int pad = tarPaddingBytesRequired(len);
2080
2081 /*
2082 * As in _tarWriteHeader, it should be safe to assume that the buffer is
2083 * large enough that we don't need to do this in multiple chunks.
2084 */
2085 Assert(sink->bbs_buffer_length >= TAR_BLOCK_SIZE);
2086 Assert(pad <= TAR_BLOCK_SIZE);
2087
2088 if (pad > 0)
2089 {
2090 MemSet(sink->bbs_buffer, 0, pad);
2092 }
2093}
2094
2095/*
2096 * If the entry in statbuf is a link, then adjust statbuf to make it look like a
2097 * directory, so that it will be written that way.
2098 */
2099static void
2101{
2102 /* If symlink, write it as a directory anyway */
2103 if (S_ISLNK(statbuf->st_mode))
2104 statbuf->st_mode = S_IFDIR | pg_dir_create_mode;
2105}
2106
2107/*
2108 * Read some data from a file, setting a wait event and reporting any error
2109 * encountered.
2110 *
2111 * If partial_read_ok is false, also report an error if the number of bytes
2112 * read is not equal to the number of bytes requested.
2113 *
2114 * Returns the number of bytes read.
2115 */
2116static ssize_t
2117basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset,
2118 const char *filename, bool partial_read_ok)
2119{
2120 ssize_t rc;
2121
2123 rc = pg_pread(fd, buf, nbytes, offset);
2125
2126 if (rc < 0)
2127 ereport(ERROR,
2129 errmsg("could not read file \"%s\": %m", filename)));
2130 if (!partial_read_ok && rc > 0 && rc != nbytes)
2131 ereport(ERROR,
2133 errmsg("could not read file \"%s\": read %zd of %zu",
2134 filename, rc, nbytes)));
2135
2136 return rc;
2137}
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:196
static int compareWalFileNames(const ListCell *a, const ListCell *b)
Definition basebackup.c:687
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:60
static void parse_basebackup_options(List *options, basebackup_options *opt)
Definition basebackup.c:699
static const char *const excludeDirContents[]
Definition basebackup.c:156
static void convert_link_to_directory(const char *pathbuf, struct stat *statbuf)
void SendBaseBackup(BaseBackupCmd *cmd, IncrementalBackupInfo *ib)
Definition basebackup.c:991
static bool backup_started_in_recovery
Definition basebackup.c:128
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:239
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:134
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:81
static long long int total_checksum_failures
Definition basebackup.c:131
#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:173
static bool PageIsNew(const PageData *page)
Definition bufpage.h:233
PageData * Page
Definition bufpage.h:81
static XLogRecPtr PageGetLSN(const PageData *page)
Definition bufpage.h:385
#define Min(x, y)
Definition c.h:1019
uint8_t uint8
Definition c.h:556
#define Assert(condition)
Definition c.h:885
int64_t int64
Definition c.h:555
#define PG_BINARY
Definition c.h:1309
#define CppAsString2(x)
Definition c.h:440
uint16_t uint16
Definition c.h:557
uint32_t uint32
Definition c.h:558
#define MemSet(start, val, len)
Definition c.h:1035
#define StaticAssertDecl(condition, errmessage)
Definition c.h:950
#define OidIsValid(objectId)
Definition c.h:800
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:49
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 errmsg(const char *fmt,...)
Definition elog.c:1093
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:3008
int CloseTransientFile(int fd)
Definition fd.c:2854
DIR * AllocateDir(const char *dirname)
Definition fd.c:2890
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition fd.c:2956
bool looks_like_temp_rel_name(const char *name)
Definition fd.c:3498
int OpenTransientFile(const char *fileName, int fileFlags)
Definition fd.c:2677
#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 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:132
#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:40
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:163
pg_compress_specification compression_specification
Definition basebackup.c:77
pg_checksum_type manifest_checksum_type
Definition basebackup.c:78
const char * label
Definition basebackup.c:64
pg_compress_algorithm compression
Definition basebackup.c:76
backup_manifest_option manifest
Definition basebackup.c:75
BaseBackupTargetHandle * target_handle
Definition basebackup.c:74
const char * name
Definition basebackup.c:144
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:3953
@ 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:6460
void do_pg_abort_backup(int code, Datum arg)
Definition xlog.c:9567
SessionBackupState get_backup_status(void)
Definition xlog.c:9274
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition xlog.c:3764
int wal_segment_size
Definition xlog.c:146
bool DataChecksumsEnabled(void)
Definition xlog.c:4647
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition xlog.c:8965
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition xlog.c:9293
#define TABLESPACE_MAP
Definition xlog.h:322
#define BACKUP_LABEL_FILE
Definition xlog.h:319
SessionBackupState
Definition xlog.h:303
@ SESSION_BACKUP_RUNNING
Definition xlog.h:305
#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:41
static StringInfo tablespace_map
Definition xlogfuncs.c:42