PostgreSQL Source Code  git master
basebackup.c File Reference
#include "postgres.h"
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include "access/xlog_internal.h"
#include "access/xlogbackup.h"
#include "backup/backup_manifest.h"
#include "backup/basebackup.h"
#include "backup/basebackup_incremental.h"
#include "backup/basebackup_sink.h"
#include "backup/basebackup_target.h"
#include "catalog/pg_tablespace_d.h"
#include "commands/defrem.h"
#include "common/compression.h"
#include "common/file_perm.h"
#include "common/file_utils.h"
#include "lib/stringinfo.h"
#include "miscadmin.h"
#include "nodes/pg_list.h"
#include "pgstat.h"
#include "pgtar.h"
#include "port.h"
#include "postmaster/syslogger.h"
#include "postmaster/walsummarizer.h"
#include "replication/slot.h"
#include "replication/walsender.h"
#include "replication/walsender_private.h"
#include "storage/bufpage.h"
#include "storage/checksum.h"
#include "storage/dsm_impl.h"
#include "storage/ipc.h"
#include "storage/reinit.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/ps_status.h"
#include "utils/relcache.h"
#include "utils/resowner.h"
Include dependency graph for basebackup.c:

Go to the source code of this file.

Data Structures

struct  basebackup_options
 
struct  exclude_list_item
 

Macros

#define SINK_BUFFER_LENGTH   Max(32768, BLCKSZ)
 

Functions

static int64 sendTablespace (bbsink *sink, char *path, Oid spcoid, bool sizeonly, struct backup_manifest_info *manifest, IncrementalBackupInfo *ib)
 
static int64 sendDir (bbsink *sink, const char *path, int basepathlen, bool sizeonly, List *tablespaces, bool sendtblspclinks, backup_manifest_info *manifest, Oid spcoid, IncrementalBackupInfo *ib)
 
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 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 bool verify_page_checksum (Page page, XLogRecPtr start_lsn, BlockNumber blkno, uint16 *expected_checksum)
 
static void sendFileWithContent (bbsink *sink, const char *filename, const char *content, int len, backup_manifest_info *manifest)
 
static int64 _tarWriteHeader (bbsink *sink, const char *filename, const char *linktarget, struct stat *statbuf, bool sizeonly)
 
static void _tarWritePadding (bbsink *sink, int len)
 
static void convert_link_to_directory (const char *pathbuf, struct stat *statbuf)
 
static void perform_base_backup (basebackup_options *opt, bbsink *sink, IncrementalBackupInfo *ib)
 
static void parse_basebackup_options (List *options, basebackup_options *opt)
 
static int compareWalFileNames (const ListCell *a, const ListCell *b)
 
static ssize_t basebackup_read_file (int fd, char *buf, size_t nbytes, off_t offset, const char *filename, bool partial_read_ok)
 
void SendBaseBackup (BaseBackupCmd *cmd, IncrementalBackupInfo *ib)
 

Variables

static bool backup_started_in_recovery = false
 
static long long int total_checksum_failures
 
static bool noverify_checksums = false
 
static const char *const excludeDirContents []
 
static const struct exclude_list_item excludeFiles []
 

Macro Definition Documentation

◆ SINK_BUFFER_LENGTH

#define SINK_BUFFER_LENGTH   Max(32768, BLCKSZ)

Definition at line 61 of file basebackup.c.

Function Documentation

◆ _tarWriteHeader()

static int64 _tarWriteHeader ( bbsink sink,
const char *  filename,
const char *  linktarget,
struct stat statbuf,
bool  sizeonly 
)
static

Definition at line 2020 of file basebackup.c.

2022 {
2023  enum tarError rc;
2024 
2025  if (!sizeonly)
2026  {
2027  /*
2028  * As of this writing, the smallest supported block size is 1kB, which
2029  * is twice TAR_BLOCK_SIZE. Since the buffer size is required to be a
2030  * multiple of BLCKSZ, it should be safe to assume that the buffer is
2031  * large enough to fit an entire tar block. We double-check by means
2032  * of these assertions.
2033  */
2034  StaticAssertDecl(TAR_BLOCK_SIZE <= BLCKSZ,
2035  "BLCKSZ too small for tar block");
2037 
2038  rc = tarCreateHeader(sink->bbs_buffer, filename, linktarget,
2039  statbuf->st_size, statbuf->st_mode,
2040  statbuf->st_uid, statbuf->st_gid,
2041  statbuf->st_mtime);
2042 
2043  switch (rc)
2044  {
2045  case TAR_OK:
2046  break;
2047  case TAR_NAME_TOO_LONG:
2048  ereport(ERROR,
2049  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2050  errmsg("file name too long for tar format: \"%s\"",
2051  filename)));
2052  break;
2053  case TAR_SYMLINK_TOO_LONG:
2054  ereport(ERROR,
2055  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
2056  errmsg("symbolic link target too long for tar format: "
2057  "file name \"%s\", target \"%s\"",
2058  filename, linktarget)));
2059  break;
2060  default:
2061  elog(ERROR, "unrecognized tar error: %d", rc);
2062  }
2063 
2065  }
2066 
2067  return TAR_BLOCK_SIZE;
2068 }
static void bbsink_archive_contents(bbsink *sink, size_t len)
#define Assert(condition)
Definition: c.h:858
#define StaticAssertDecl(condition, errmessage)
Definition: c.h:936
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
static char * filename
Definition: pg_dumpall.c:119
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
char * bbs_buffer
size_t bbs_buffer_length
__time64_t st_mtime
Definition: win32_port.h:275
__int64 st_size
Definition: win32_port.h:273
short st_gid
Definition: win32_port.h:271
unsigned short st_mode
Definition: win32_port.h:268
short st_uid
Definition: win32_port.h:270

References Assert, bbsink::bbs_buffer, bbsink::bbs_buffer_length, bbsink_archive_contents(), elog, ereport, errcode(), errmsg(), ERROR, filename, stat::st_gid, stat::st_mode, stat::st_mtime, stat::st_size, stat::st_uid, StaticAssertDecl, TAR_BLOCK_SIZE, TAR_NAME_TOO_LONG, TAR_OK, TAR_SYMLINK_TOO_LONG, and tarCreateHeader().

Referenced by perform_base_backup(), sendDir(), sendFile(), sendFileWithContent(), and sendTablespace().

◆ _tarWritePadding()

static void _tarWritePadding ( bbsink sink,
int  len 
)
static

Definition at line 2074 of file basebackup.c.

2075 {
2076  int pad = tarPaddingBytesRequired(len);
2077 
2078  /*
2079  * As in _tarWriteHeader, it should be safe to assume that the buffer is
2080  * large enough that we don't need to do this in multiple chunks.
2081  */
2083  Assert(pad <= TAR_BLOCK_SIZE);
2084 
2085  if (pad > 0)
2086  {
2087  MemSet(sink->bbs_buffer, 0, pad);
2088  bbsink_archive_contents(sink, pad);
2089  }
2090 }
#define MemSet(start, val, len)
Definition: c.h:1020
const void size_t len
static size_t tarPaddingBytesRequired(size_t len)
Definition: pgtar.h:79

References Assert, bbsink::bbs_buffer, bbsink::bbs_buffer_length, bbsink_archive_contents(), len, MemSet, TAR_BLOCK_SIZE, and tarPaddingBytesRequired().

Referenced by sendFile(), and sendFileWithContent().

◆ basebackup_read_file()

static ssize_t basebackup_read_file ( int  fd,
char *  buf,
size_t  nbytes,
off_t  offset,
const char *  filename,
bool  partial_read_ok 
)
static

Definition at line 2114 of file basebackup.c.

2116 {
2117  ssize_t rc;
2118 
2119  pgstat_report_wait_start(WAIT_EVENT_BASEBACKUP_READ);
2120  rc = pg_pread(fd, buf, nbytes, offset);
2122 
2123  if (rc < 0)
2124  ereport(ERROR,
2126  errmsg("could not read file \"%s\": %m", filename)));
2127  if (!partial_read_ok && rc > 0 && rc != nbytes)
2128  ereport(ERROR,
2130  errmsg("could not read file \"%s\": read %zd of %zu",
2131  filename, rc, nbytes)));
2132 
2133  return rc;
2134 }
int errcode_for_file_access(void)
Definition: elog.c:876
static char * buf
Definition: pg_test_fsync.c:73
#define pg_pread
Definition: port.h:225
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static void pgstat_report_wait_start(uint32 wait_event_info)
Definition: wait_event.h:85
static void pgstat_report_wait_end(void)
Definition: wait_event.h:101

References buf, ereport, errcode_for_file_access(), errmsg(), ERROR, fd(), filename, pg_pread, pgstat_report_wait_end(), and pgstat_report_wait_start().

Referenced by perform_base_backup(), and read_file_data_into_buffer().

◆ compareWalFileNames()

static int compareWalFileNames ( const ListCell a,
const ListCell b 
)
static

Definition at line 685 of file basebackup.c.

686 {
687  char *fna = (char *) lfirst(a);
688  char *fnb = (char *) lfirst(b);
689 
690  return strcmp(fna + 8, fnb + 8);
691 }
int b
Definition: isn.c:70
int a
Definition: isn.c:69
#define lfirst(lc)
Definition: pg_list.h:172

References a, b, and lfirst.

Referenced by perform_base_backup().

◆ convert_link_to_directory()

static void convert_link_to_directory ( const char *  pathbuf,
struct stat statbuf 
)
static

Definition at line 2097 of file basebackup.c.

2098 {
2099  /* If symlink, write it as a directory anyway */
2100  if (S_ISLNK(statbuf->st_mode))
2101  statbuf->st_mode = S_IFDIR | pg_dir_create_mode;
2102 }
int pg_dir_create_mode
Definition: file_perm.c:18
#define S_ISLNK(m)
Definition: win32_port.h:344

References pg_dir_create_mode, S_ISLNK, and stat::st_mode.

Referenced by sendDir().

◆ parse_basebackup_options()

static void parse_basebackup_options ( List options,
basebackup_options opt 
)
static

Definition at line 697 of file basebackup.c.

698 {
699  ListCell *lopt;
700  bool o_label = false;
701  bool o_progress = false;
702  bool o_checkpoint = false;
703  bool o_nowait = false;
704  bool o_wal = false;
705  bool o_incremental = false;
706  bool o_maxrate = false;
707  bool o_tablespace_map = false;
708  bool o_noverify_checksums = false;
709  bool o_manifest = false;
710  bool o_manifest_checksums = false;
711  bool o_target = false;
712  bool o_target_detail = false;
713  char *target_str = NULL;
714  char *target_detail_str = NULL;
715  bool o_compression = false;
716  bool o_compression_detail = false;
717  char *compression_detail_str = NULL;
718 
719  MemSet(opt, 0, sizeof(*opt));
724 
725  foreach(lopt, options)
726  {
727  DefElem *defel = (DefElem *) lfirst(lopt);
728 
729  if (strcmp(defel->defname, "label") == 0)
730  {
731  if (o_label)
732  ereport(ERROR,
733  (errcode(ERRCODE_SYNTAX_ERROR),
734  errmsg("duplicate option \"%s\"", defel->defname)));
735  opt->label = defGetString(defel);
736  o_label = true;
737  }
738  else if (strcmp(defel->defname, "progress") == 0)
739  {
740  if (o_progress)
741  ereport(ERROR,
742  (errcode(ERRCODE_SYNTAX_ERROR),
743  errmsg("duplicate option \"%s\"", defel->defname)));
744  opt->progress = defGetBoolean(defel);
745  o_progress = true;
746  }
747  else if (strcmp(defel->defname, "checkpoint") == 0)
748  {
749  char *optval = defGetString(defel);
750 
751  if (o_checkpoint)
752  ereport(ERROR,
753  (errcode(ERRCODE_SYNTAX_ERROR),
754  errmsg("duplicate option \"%s\"", defel->defname)));
755  if (pg_strcasecmp(optval, "fast") == 0)
756  opt->fastcheckpoint = true;
757  else if (pg_strcasecmp(optval, "spread") == 0)
758  opt->fastcheckpoint = false;
759  else
760  ereport(ERROR,
761  (errcode(ERRCODE_SYNTAX_ERROR),
762  errmsg("unrecognized checkpoint type: \"%s\"",
763  optval)));
764  o_checkpoint = true;
765  }
766  else if (strcmp(defel->defname, "wait") == 0)
767  {
768  if (o_nowait)
769  ereport(ERROR,
770  (errcode(ERRCODE_SYNTAX_ERROR),
771  errmsg("duplicate option \"%s\"", defel->defname)));
772  opt->nowait = !defGetBoolean(defel);
773  o_nowait = true;
774  }
775  else if (strcmp(defel->defname, "wal") == 0)
776  {
777  if (o_wal)
778  ereport(ERROR,
779  (errcode(ERRCODE_SYNTAX_ERROR),
780  errmsg("duplicate option \"%s\"", defel->defname)));
781  opt->includewal = defGetBoolean(defel);
782  o_wal = true;
783  }
784  else if (strcmp(defel->defname, "incremental") == 0)
785  {
786  if (o_incremental)
787  ereport(ERROR,
788  (errcode(ERRCODE_SYNTAX_ERROR),
789  errmsg("duplicate option \"%s\"", defel->defname)));
790  opt->incremental = defGetBoolean(defel);
791  if (opt->incremental && !summarize_wal)
792  ereport(ERROR,
793  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
794  errmsg("incremental backups cannot be taken unless WAL summarization is enabled")));
795  o_incremental = true;
796  }
797  else if (strcmp(defel->defname, "max_rate") == 0)
798  {
799  int64 maxrate;
800 
801  if (o_maxrate)
802  ereport(ERROR,
803  (errcode(ERRCODE_SYNTAX_ERROR),
804  errmsg("duplicate option \"%s\"", defel->defname)));
805 
806  maxrate = defGetInt64(defel);
807  if (maxrate < MAX_RATE_LOWER || maxrate > MAX_RATE_UPPER)
808  ereport(ERROR,
809  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
810  errmsg("%d is outside the valid range for parameter \"%s\" (%d .. %d)",
811  (int) maxrate, "MAX_RATE", MAX_RATE_LOWER, MAX_RATE_UPPER)));
812 
813  opt->maxrate = (uint32) maxrate;
814  o_maxrate = true;
815  }
816  else if (strcmp(defel->defname, "tablespace_map") == 0)
817  {
818  if (o_tablespace_map)
819  ereport(ERROR,
820  (errcode(ERRCODE_SYNTAX_ERROR),
821  errmsg("duplicate option \"%s\"", defel->defname)));
822  opt->sendtblspcmapfile = defGetBoolean(defel);
823  o_tablespace_map = true;
824  }
825  else if (strcmp(defel->defname, "verify_checksums") == 0)
826  {
827  if (o_noverify_checksums)
828  ereport(ERROR,
829  (errcode(ERRCODE_SYNTAX_ERROR),
830  errmsg("duplicate option \"%s\"", defel->defname)));
832  o_noverify_checksums = true;
833  }
834  else if (strcmp(defel->defname, "manifest") == 0)
835  {
836  char *optval = defGetString(defel);
837  bool manifest_bool;
838 
839  if (o_manifest)
840  ereport(ERROR,
841  (errcode(ERRCODE_SYNTAX_ERROR),
842  errmsg("duplicate option \"%s\"", defel->defname)));
843  if (parse_bool(optval, &manifest_bool))
844  {
845  if (manifest_bool)
847  else
849  }
850  else if (pg_strcasecmp(optval, "force-encode") == 0)
852  else
853  ereport(ERROR,
854  (errcode(ERRCODE_SYNTAX_ERROR),
855  errmsg("unrecognized manifest option: \"%s\"",
856  optval)));
857  o_manifest = true;
858  }
859  else if (strcmp(defel->defname, "manifest_checksums") == 0)
860  {
861  char *optval = defGetString(defel);
862 
863  if (o_manifest_checksums)
864  ereport(ERROR,
865  (errcode(ERRCODE_SYNTAX_ERROR),
866  errmsg("duplicate option \"%s\"", defel->defname)));
867  if (!pg_checksum_parse_type(optval,
868  &opt->manifest_checksum_type))
869  ereport(ERROR,
870  (errcode(ERRCODE_SYNTAX_ERROR),
871  errmsg("unrecognized checksum algorithm: \"%s\"",
872  optval)));
873  o_manifest_checksums = true;
874  }
875  else if (strcmp(defel->defname, "target") == 0)
876  {
877  if (o_target)
878  ereport(ERROR,
879  (errcode(ERRCODE_SYNTAX_ERROR),
880  errmsg("duplicate option \"%s\"", defel->defname)));
881  target_str = defGetString(defel);
882  o_target = true;
883  }
884  else if (strcmp(defel->defname, "target_detail") == 0)
885  {
886  char *optval = defGetString(defel);
887 
888  if (o_target_detail)
889  ereport(ERROR,
890  (errcode(ERRCODE_SYNTAX_ERROR),
891  errmsg("duplicate option \"%s\"", defel->defname)));
892  target_detail_str = optval;
893  o_target_detail = true;
894  }
895  else if (strcmp(defel->defname, "compression") == 0)
896  {
897  char *optval = defGetString(defel);
898 
899  if (o_compression)
900  ereport(ERROR,
901  (errcode(ERRCODE_SYNTAX_ERROR),
902  errmsg("duplicate option \"%s\"", defel->defname)));
903  if (!parse_compress_algorithm(optval, &opt->compression))
904  ereport(ERROR,
905  (errcode(ERRCODE_SYNTAX_ERROR),
906  errmsg("unrecognized compression algorithm: \"%s\"",
907  optval)));
908  o_compression = true;
909  }
910  else if (strcmp(defel->defname, "compression_detail") == 0)
911  {
912  if (o_compression_detail)
913  ereport(ERROR,
914  (errcode(ERRCODE_SYNTAX_ERROR),
915  errmsg("duplicate option \"%s\"", defel->defname)));
916  compression_detail_str = defGetString(defel);
917  o_compression_detail = true;
918  }
919  else
920  ereport(ERROR,
921  (errcode(ERRCODE_SYNTAX_ERROR),
922  errmsg("unrecognized base backup option: \"%s\"",
923  defel->defname)));
924  }
925 
926  if (opt->label == NULL)
927  opt->label = "base backup";
928  if (opt->manifest == MANIFEST_OPTION_NO)
929  {
930  if (o_manifest_checksums)
931  ereport(ERROR,
932  (errcode(ERRCODE_SYNTAX_ERROR),
933  errmsg("manifest checksums require a backup manifest")));
935  }
936 
937  if (target_str == NULL)
938  {
939  if (target_detail_str != NULL)
940  ereport(ERROR,
941  (errcode(ERRCODE_SYNTAX_ERROR),
942  errmsg("target detail cannot be used without target")));
943  opt->use_copytblspc = true;
944  opt->send_to_client = true;
945  }
946  else if (strcmp(target_str, "client") == 0)
947  {
948  if (target_detail_str != NULL)
949  ereport(ERROR,
950  (errcode(ERRCODE_SYNTAX_ERROR),
951  errmsg("target \"%s\" does not accept a target detail",
952  target_str)));
953  opt->send_to_client = true;
954  }
955  else
956  opt->target_handle =
957  BaseBackupGetTargetHandle(target_str, target_detail_str);
958 
959  if (o_compression_detail && !o_compression)
960  ereport(ERROR,
961  (errcode(ERRCODE_SYNTAX_ERROR),
962  errmsg("compression detail cannot be specified unless compression is enabled")));
963 
964  if (o_compression)
965  {
966  char *error_detail;
967 
968  parse_compress_specification(opt->compression, compression_detail_str,
970  error_detail =
972  if (error_detail != NULL)
973  ereport(ERROR,
974  errcode(ERRCODE_SYNTAX_ERROR),
975  errmsg("invalid compression specification: %s",
976  error_detail));
977  }
978 }
@ MANIFEST_OPTION_NO
@ MANIFEST_OPTION_FORCE_ENCODE
@ MANIFEST_OPTION_YES
static bool noverify_checksums
Definition: basebackup.c:130
#define MAX_RATE_UPPER
Definition: basebackup.h:21
#define MAX_RATE_LOWER
Definition: basebackup.h:20
BaseBackupTargetHandle * BaseBackupGetTargetHandle(char *target, char *target_detail)
bool parse_bool(const char *value, bool *result)
Definition: bool.c:31
unsigned int uint32
Definition: c.h:506
bool pg_checksum_parse_type(char *name, pg_checksum_type *type)
@ CHECKSUM_TYPE_NONE
@ CHECKSUM_TYPE_CRC32C
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)
Definition: compression.c:107
char * validate_compress_specification(pg_compress_specification *spec)
Definition: compression.c:344
@ PG_COMPRESSION_NONE
Definition: compression.h:23
bool defGetBoolean(DefElem *def)
Definition: define.c:107
char * defGetString(DefElem *def)
Definition: define.c:48
int64 defGetInt64(DefElem *def)
Definition: define.c:186
static int32 maxrate
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
char * defname
Definition: parsenodes.h:817
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
pg_compress_algorithm algorithm
Definition: compression.h:34
bool summarize_wal

References pg_compress_specification::algorithm, BaseBackupGetTargetHandle(), CHECKSUM_TYPE_CRC32C, CHECKSUM_TYPE_NONE, basebackup_options::compression, basebackup_options::compression_specification, defGetBoolean(), defGetInt64(), defGetString(), DefElem::defname, ereport, errcode(), errmsg(), ERROR, basebackup_options::fastcheckpoint, basebackup_options::includewal, basebackup_options::incremental, basebackup_options::label, lfirst, basebackup_options::manifest, basebackup_options::manifest_checksum_type, MANIFEST_OPTION_FORCE_ENCODE, MANIFEST_OPTION_NO, MANIFEST_OPTION_YES, MAX_RATE_LOWER, MAX_RATE_UPPER, basebackup_options::maxrate, maxrate, MemSet, noverify_checksums, basebackup_options::nowait, parse_bool(), parse_compress_algorithm(), parse_compress_specification(), pg_checksum_parse_type(), PG_COMPRESSION_NONE, pg_strcasecmp(), basebackup_options::progress, basebackup_options::send_to_client, basebackup_options::sendtblspcmapfile, summarize_wal, basebackup_options::target_handle, basebackup_options::use_copytblspc, and validate_compress_specification().

Referenced by SendBaseBackup().

◆ perform_base_backup()

static void perform_base_backup ( basebackup_options opt,
bbsink sink,
IncrementalBackupInfo ib 
)
static

Definition at line 235 of file basebackup.c.

237 {
239  XLogRecPtr endptr;
240  TimeLineID endtli;
244 
245  /* Initial backup state, insofar as we know it now. */
246  state.tablespaces = NIL;
247  state.tablespace_num = 0;
248  state.bytes_done = 0;
249  state.bytes_total = 0;
250  state.bytes_total_is_valid = false;
251 
252  /* we're going to use a BufFile, so we need a ResourceOwner */
253  Assert(CurrentResourceOwner == NULL);
254  CurrentResourceOwner = ResourceOwnerCreate(NULL, "base backup");
255 
257 
260 
262 
263  /* Allocate backup related variables. */
266 
268  do_pg_backup_start(opt->label, opt->fastcheckpoint, &state.tablespaces,
270 
271  state.startptr = backup_state->startpoint;
272  state.starttli = backup_state->starttli;
273 
274  /*
275  * Once do_pg_backup_start has been called, ensure that any failure causes
276  * us to abort the backup so we don't "leak" a backup counter. For this
277  * reason, *all* functionality between do_pg_backup_start() and the end of
278  * do_pg_backup_stop() should be inside the error cleanup block!
279  */
280 
282  {
283  ListCell *lc;
284  tablespaceinfo *newti;
285 
286  /* If this is an incremental backup, execute preparatory steps. */
287  if (ib != NULL)
289 
290  /* Add a node for the base directory at the end */
291  newti = palloc0(sizeof(tablespaceinfo));
292  newti->size = -1;
293  state.tablespaces = lappend(state.tablespaces, newti);
294 
295  /*
296  * Calculate the total backup size by summing up the size of each
297  * tablespace
298  */
299  if (opt->progress)
300  {
302 
303  foreach(lc, state.tablespaces)
304  {
305  tablespaceinfo *tmp = (tablespaceinfo *) lfirst(lc);
306 
307  if (tmp->path == NULL)
308  tmp->size = sendDir(sink, ".", 1, true, state.tablespaces,
309  true, NULL, InvalidOid, NULL);
310  else
311  tmp->size = sendTablespace(sink, tmp->path, tmp->oid, true,
312  NULL, NULL);
313  state.bytes_total += tmp->size;
314  }
315  state.bytes_total_is_valid = true;
316  }
317 
318  /* notify basebackup sink about start of backup */
320 
321  /* Send off our tablespaces one by one */
322  foreach(lc, state.tablespaces)
323  {
324  tablespaceinfo *ti = (tablespaceinfo *) lfirst(lc);
325 
326  if (ti->path == NULL)
327  {
328  struct stat statbuf;
329  bool sendtblspclinks = true;
330  char *backup_label;
331 
332  bbsink_begin_archive(sink, "base.tar");
333 
334  /* In the main tar, include the backup_label first... */
335  backup_label = build_backup_content(backup_state, false);
337  backup_label, -1, &manifest);
338  pfree(backup_label);
339 
340  /* Then the tablespace_map file, if required... */
341  if (opt->sendtblspcmapfile)
342  {
344  tablespace_map->data, -1, &manifest);
345  sendtblspclinks = false;
346  }
347 
348  /* Then the bulk of the files... */
349  sendDir(sink, ".", 1, false, state.tablespaces,
350  sendtblspclinks, &manifest, InvalidOid, ib);
351 
352  /* ... and pg_control after everything else. */
353  if (lstat(XLOG_CONTROL_FILE, &statbuf) != 0)
354  ereport(ERROR,
356  errmsg("could not stat file \"%s\": %m",
359  false, InvalidOid, InvalidOid,
360  InvalidRelFileNumber, 0, &manifest, 0, NULL, 0);
361  }
362  else
363  {
364  char *archive_name = psprintf("%u.tar", ti->oid);
365 
366  bbsink_begin_archive(sink, archive_name);
367 
368  sendTablespace(sink, ti->path, ti->oid, false, &manifest, ib);
369  }
370 
371  /*
372  * If we're including WAL, and this is the main data directory we
373  * don't treat this as the end of the tablespace. Instead, we will
374  * include the xlog files below and stop afterwards. This is safe
375  * since the main data directory is always sent *last*.
376  */
377  if (opt->includewal && ti->path == NULL)
378  {
379  Assert(lnext(state.tablespaces, lc) == NULL);
380  }
381  else
382  {
383  /* Properly terminate the tarfile. */
384  StaticAssertDecl(2 * TAR_BLOCK_SIZE <= BLCKSZ,
385  "BLCKSZ too small for 2 tar blocks");
386  memset(sink->bbs_buffer, 0, 2 * TAR_BLOCK_SIZE);
388 
389  /* OK, that's the end of the archive. */
390  bbsink_end_archive(sink);
391  }
392  }
393 
396 
397  endptr = backup_state->stoppoint;
398  endtli = backup_state->stoptli;
399 
400  /* Deallocate backup-related variables. */
403  }
405 
406 
407  if (opt->includewal)
408  {
409  /*
410  * We've left the last tar file "open", so we can now append the
411  * required WAL files to it.
412  */
413  char pathbuf[MAXPGPATH];
414  XLogSegNo segno;
415  XLogSegNo startsegno;
416  XLogSegNo endsegno;
417  struct stat statbuf;
418  List *historyFileList = NIL;
419  List *walFileList = NIL;
420  char firstoff[MAXFNAMELEN];
421  char lastoff[MAXFNAMELEN];
422  DIR *dir;
423  struct dirent *de;
424  ListCell *lc;
425  TimeLineID tli;
426 
428 
429  /*
430  * I'd rather not worry about timelines here, so scan pg_wal and
431  * include all WAL files in the range between 'startptr' and 'endptr',
432  * regardless of the timeline the file is stamped with. If there are
433  * some spurious WAL files belonging to timelines that don't belong in
434  * this server's history, they will be included too. Normally there
435  * shouldn't be such files, but if there are, there's little harm in
436  * including them.
437  */
438  XLByteToSeg(state.startptr, startsegno, wal_segment_size);
439  XLogFileName(firstoff, state.starttli, startsegno, wal_segment_size);
440  XLByteToPrevSeg(endptr, endsegno, wal_segment_size);
441  XLogFileName(lastoff, endtli, endsegno, wal_segment_size);
442 
443  dir = AllocateDir("pg_wal");
444  while ((de = ReadDir(dir, "pg_wal")) != NULL)
445  {
446  /* Does it look like a WAL segment, and is it in the range? */
447  if (IsXLogFileName(de->d_name) &&
448  strcmp(de->d_name + 8, firstoff + 8) >= 0 &&
449  strcmp(de->d_name + 8, lastoff + 8) <= 0)
450  {
451  walFileList = lappend(walFileList, pstrdup(de->d_name));
452  }
453  /* Does it look like a timeline history file? */
454  else if (IsTLHistoryFileName(de->d_name))
455  {
456  historyFileList = lappend(historyFileList, pstrdup(de->d_name));
457  }
458  }
459  FreeDir(dir);
460 
461  /*
462  * Before we go any further, check that none of the WAL segments we
463  * need were removed.
464  */
465  CheckXLogRemoved(startsegno, state.starttli);
466 
467  /*
468  * Sort the WAL filenames. We want to send the files in order from
469  * oldest to newest, to reduce the chance that a file is recycled
470  * before we get a chance to send it over.
471  */
472  list_sort(walFileList, compareWalFileNames);
473 
474  /*
475  * There must be at least one xlog file in the pg_wal directory, since
476  * we are doing backup-including-xlog.
477  */
478  if (walFileList == NIL)
479  ereport(ERROR,
480  (errmsg("could not find any WAL files")));
481 
482  /*
483  * Sanity check: the first and last segment should cover startptr and
484  * endptr, with no gaps in between.
485  */
486  XLogFromFileName((char *) linitial(walFileList),
487  &tli, &segno, wal_segment_size);
488  if (segno != startsegno)
489  {
490  char startfname[MAXFNAMELEN];
491 
492  XLogFileName(startfname, state.starttli, startsegno,
494  ereport(ERROR,
495  (errmsg("could not find WAL file \"%s\"", startfname)));
496  }
497  foreach(lc, walFileList)
498  {
499  char *walFileName = (char *) lfirst(lc);
500  XLogSegNo currsegno = segno;
501  XLogSegNo nextsegno = segno + 1;
502 
503  XLogFromFileName(walFileName, &tli, &segno, wal_segment_size);
504  if (!(nextsegno == segno || currsegno == segno))
505  {
506  char nextfname[MAXFNAMELEN];
507 
508  XLogFileName(nextfname, tli, nextsegno, wal_segment_size);
509  ereport(ERROR,
510  (errmsg("could not find WAL file \"%s\"", nextfname)));
511  }
512  }
513  if (segno != endsegno)
514  {
515  char endfname[MAXFNAMELEN];
516 
517  XLogFileName(endfname, endtli, endsegno, wal_segment_size);
518  ereport(ERROR,
519  (errmsg("could not find WAL file \"%s\"", endfname)));
520  }
521 
522  /* Ok, we have everything we need. Send the WAL files. */
523  foreach(lc, walFileList)
524  {
525  char *walFileName = (char *) lfirst(lc);
526  int fd;
527  ssize_t cnt;
528  pgoff_t len = 0;
529 
530  snprintf(pathbuf, MAXPGPATH, XLOGDIR "/%s", walFileName);
531  XLogFromFileName(walFileName, &tli, &segno, wal_segment_size);
532 
533  fd = OpenTransientFile(pathbuf, O_RDONLY | PG_BINARY);
534  if (fd < 0)
535  {
536  int save_errno = errno;
537 
538  /*
539  * Most likely reason for this is that the file was already
540  * removed by a checkpoint, so check for that to get a better
541  * error message.
542  */
543  CheckXLogRemoved(segno, tli);
544 
545  errno = save_errno;
546  ereport(ERROR,
548  errmsg("could not open file \"%s\": %m", pathbuf)));
549  }
550 
551  if (fstat(fd, &statbuf) != 0)
552  ereport(ERROR,
554  errmsg("could not stat file \"%s\": %m",
555  pathbuf)));
556  if (statbuf.st_size != wal_segment_size)
557  {
558  CheckXLogRemoved(segno, tli);
559  ereport(ERROR,
561  errmsg("unexpected WAL file size \"%s\"", walFileName)));
562  }
563 
564  /* send the WAL file itself */
565  _tarWriteHeader(sink, pathbuf, NULL, &statbuf, false);
566 
567  while ((cnt = basebackup_read_file(fd, sink->bbs_buffer,
568  Min(sink->bbs_buffer_length,
570  len, pathbuf, true)) > 0)
571  {
572  CheckXLogRemoved(segno, tli);
573  bbsink_archive_contents(sink, cnt);
574 
575  len += cnt;
576 
577  if (len == wal_segment_size)
578  break;
579  }
580 
581  if (len != wal_segment_size)
582  {
583  CheckXLogRemoved(segno, tli);
584  ereport(ERROR,
586  errmsg("unexpected WAL file size \"%s\"", walFileName)));
587  }
588 
589  /*
590  * wal_segment_size is a multiple of TAR_BLOCK_SIZE, so no need
591  * for padding.
592  */
594 
596 
597  /*
598  * Mark file as archived, otherwise files can get archived again
599  * after promotion of a new node. This is in line with
600  * walreceiver.c always doing an XLogArchiveForceDone() after a
601  * complete segment.
602  */
603  StatusFilePath(pathbuf, walFileName, ".done");
604  sendFileWithContent(sink, pathbuf, "", -1, &manifest);
605  }
606 
607  /*
608  * Send timeline history files too. Only the latest timeline history
609  * file is required for recovery, and even that only if there happens
610  * to be a timeline switch in the first WAL segment that contains the
611  * checkpoint record, or if we're taking a base backup from a standby
612  * server and the target timeline changes while the backup is taken.
613  * But they are small and highly useful for debugging purposes, so
614  * better include them all, always.
615  */
616  foreach(lc, historyFileList)
617  {
618  char *fname = lfirst(lc);
619 
620  snprintf(pathbuf, MAXPGPATH, XLOGDIR "/%s", fname);
621 
622  if (lstat(pathbuf, &statbuf) != 0)
623  ereport(ERROR,
625  errmsg("could not stat file \"%s\": %m", pathbuf)));
626 
627  sendFile(sink, pathbuf, pathbuf, &statbuf, false,
629  &manifest, 0, NULL, 0);
630 
631  /* unconditionally mark file as archived */
632  StatusFilePath(pathbuf, fname, ".done");
633  sendFileWithContent(sink, pathbuf, "", -1, &manifest);
634  }
635 
636  /* Properly terminate the tar file. */
637  StaticAssertStmt(2 * TAR_BLOCK_SIZE <= BLCKSZ,
638  "BLCKSZ too small for 2 tar blocks");
639  memset(sink->bbs_buffer, 0, 2 * TAR_BLOCK_SIZE);
641 
642  /* OK, that's the end of the archive. */
643  bbsink_end_archive(sink);
644  }
645 
646  AddWALInfoToBackupManifest(&manifest, state.startptr, state.starttli,
647  endptr, endtli);
648 
650 
651  bbsink_end_backup(sink, endptr, endtli);
652 
654  {
655  if (total_checksum_failures > 1)
657  (errmsg_plural("%lld total checksum verification failure",
658  "%lld total checksum verification failures",
661 
662  ereport(ERROR,
664  errmsg("checksum verification failure during base backup")));
665  }
666 
667  /*
668  * Make sure to free the manifest before the resource owners as manifests
669  * use cryptohash contexts that may depend on resource owners (like
670  * OpenSSL).
671  */
673 
674  /* clean up the resource owner we created */
675  WalSndResourceCleanup(true);
676 
678 }
void InitializeBackupManifest(backup_manifest_info *manifest, backup_manifest_option want_manifest, pg_checksum_type manifest_checksum_type)
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)
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)
Definition: basebackup.c:1573
static int compareWalFileNames(const ListCell *a, const ListCell *b)
Definition: basebackup.c:685
static void sendFileWithContent(bbsink *sink, const char *filename, const char *content, int len, backup_manifest_info *manifest)
Definition: basebackup.c:1074
static int64 sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly, List *tablespaces, bool sendtblspclinks, backup_manifest_info *manifest, Oid spcoid, IncrementalBackupInfo *ib)
Definition: basebackup.c:1188
#define SINK_BUFFER_LENGTH
Definition: basebackup.c:61
static bool backup_started_in_recovery
Definition: basebackup.c:124
static int64 _tarWriteHeader(bbsink *sink, const char *filename, const char *linktarget, struct stat *statbuf, bool sizeonly)
Definition: basebackup.c:2020
static int64 sendTablespace(bbsink *sink, char *path, Oid spcoid, bool sizeonly, struct backup_manifest_info *manifest, IncrementalBackupInfo *ib)
Definition: basebackup.c:1135
static ssize_t basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset, const char *filename, bool partial_read_ok)
Definition: basebackup.c:2114
static long long int total_checksum_failures
Definition: basebackup.c:127
void PrepareForIncrementalBackup(IncrementalBackupInfo *ib, BackupState *backup_state)
void basebackup_progress_wait_checkpoint(void)
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)
#define Min(x, y)
Definition: c.h:1004
#define PG_BINARY
Definition: c.h:1273
#define StaticAssertStmt(condition, errmessage)
Definition: c.h:938
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
Definition: elog.c:1180
#define WARNING
Definition: elog.h:36
struct dirent * ReadDir(DIR *dir, const char *dirname)
Definition: fd.c:2932
int FreeDir(DIR *dir)
Definition: fd.c:2984
int CloseTransientFile(int fd)
Definition: fd.c:2832
int OpenTransientFile(const char *fileName, int fileFlags)
Definition: fd.c:2656
DIR * AllocateDir(const char *dirname)
Definition: fd.c:2866
#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
void list_sort(List *list, list_sort_comparator cmp)
Definition: list.c:1674
List * lappend(List *list, void *datum)
Definition: list.c:339
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
static bool manifest
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41
#define MAXPGPATH
#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
#define snprintf
Definition: port.h:238
static Datum BoolGetDatum(bool X)
Definition: postgres.h:102
#define InvalidOid
Definition: postgres_ext.h:36
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define InvalidRelFileNumber
Definition: relpath.h:26
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
Definition: resowner.c:413
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
void destroyStringInfo(StringInfo str)
Definition: stringinfo.c:361
StringInfo makeStringInfo(void)
Definition: stringinfo.c:41
TimeLineID starttli
Definition: xlogbackup.h:27
XLogRecPtr startpoint
Definition: xlogbackup.h:26
TimeLineID stoptli
Definition: xlogbackup.h:36
XLogRecPtr stoppoint
Definition: xlogbackup.h:35
Definition: dirent.c:26
Definition: pg_list.h:54
Definition: dirent.h:10
char d_name[MAX_PATH]
Definition: dirent.h:15
Definition: regguts.h:323
void WalSndResourceCleanup(bool isCommit)
Definition: walsender.c:362
#define lstat(path, sb)
Definition: win32_port.h:285
#define fstat
Definition: win32_port.h:283
#define pgoff_t
Definition: win32_port.h:207
bool RecoveryInProgress(void)
Definition: xlog.c:6333
void do_pg_abort_backup(int code, Datum arg)
Definition: xlog.c:9386
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
Definition: xlog.c:3720
int wal_segment_size
Definition: xlog.c:142
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
Definition: xlog.c:8784
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
Definition: xlog.c:9112
#define TABLESPACE_MAP
Definition: xlog.h:304
#define BACKUP_LABEL_FILE
Definition: xlog.h:301
#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:59
uint64 XLogSegNo
Definition: xlogdefs.h:48
static BackupState * backup_state
Definition: xlogfuncs.c:43
static StringInfo tablespace_map
Definition: xlogfuncs.c:44

References _tarWriteHeader(), AddWALInfoToBackupManifest(), AllocateDir(), Assert, BACKUP_LABEL_FILE, backup_started_in_recovery, backup_state, basebackup_progress_done(), basebackup_progress_estimate_backup_size(), basebackup_progress_transfer_wal(), basebackup_progress_wait_checkpoint(), basebackup_progress_wait_wal_archive(), basebackup_read_file(), bbsink::bbs_buffer, bbsink::bbs_buffer_length, bbsink_archive_contents(), bbsink_begin_archive(), bbsink_begin_backup(), bbsink_end_archive(), bbsink_end_backup(), BoolGetDatum(), build_backup_content(), CheckXLogRemoved(), CloseTransientFile(), compareWalFileNames(), CurrentResourceOwner, dirent::d_name, StringInfoData::data, destroyStringInfo(), do_pg_abort_backup(), do_pg_backup_start(), do_pg_backup_stop(), ereport, errcode(), ERRCODE_DATA_CORRUPTED, errcode_for_file_access(), errmsg(), errmsg_plural(), ERROR, basebackup_options::fastcheckpoint, fd(), FreeBackupManifest(), FreeDir(), fstat, basebackup_options::includewal, InitializeBackupManifest(), InvalidOid, InvalidRelFileNumber, IsTLHistoryFileName(), IsXLogFileName(), basebackup_options::label, lappend(), len, lfirst, linitial, list_sort(), lnext(), lstat, makeStringInfo(), basebackup_options::manifest, manifest, basebackup_options::manifest_checksum_type, MAXFNAMELEN, MAXPGPATH, Min, NIL, basebackup_options::nowait, tablespaceinfo::oid, OpenTransientFile(), palloc0(), tablespaceinfo::path, pfree(), PG_BINARY, PG_END_ENSURE_ERROR_CLEANUP, PG_ENSURE_ERROR_CLEANUP, pgoff_t, PrepareForIncrementalBackup(), basebackup_options::progress, psprintf(), pstrdup(), ReadDir(), RecoveryInProgress(), ResourceOwnerCreate(), SendBackupManifest(), sendDir(), sendFile(), sendFileWithContent(), sendTablespace(), basebackup_options::sendtblspcmapfile, SINK_BUFFER_LENGTH, tablespaceinfo::size, snprintf, stat::st_size, BackupState::startpoint, BackupState::starttli, StaticAssertDecl, StaticAssertStmt, StatusFilePath(), BackupState::stoppoint, BackupState::stoptli, tablespace_map, TABLESPACE_MAP, TAR_BLOCK_SIZE, total_checksum_failures, wal_segment_size, WalSndResourceCleanup(), WARNING, XLByteToPrevSeg, XLByteToSeg, XLOG_CONTROL_FILE, XLOGDIR, XLogFileName(), and XLogFromFileName().

Referenced by SendBaseBackup().

◆ push_to_sink()

static void push_to_sink ( bbsink sink,
pg_checksum_context checksum_ctx,
size_t *  bytes_done,
void *  data,
size_t  length 
)
static

Definition at line 1951 of file basebackup.c.

1953 {
1954  while (length > 0)
1955  {
1956  size_t bytes_to_copy;
1957 
1958  /*
1959  * We use < here rather than <= so that if the data exactly fills the
1960  * remaining buffer space, we trigger a flush now.
1961  */
1962  if (length < sink->bbs_buffer_length - *bytes_done)
1963  {
1964  /* Append remaining data to buffer. */
1965  memcpy(sink->bbs_buffer + *bytes_done, data, length);
1966  *bytes_done += length;
1967  return;
1968  }
1969 
1970  /* Copy until buffer is full and flush it. */
1971  bytes_to_copy = sink->bbs_buffer_length - *bytes_done;
1972  memcpy(sink->bbs_buffer + *bytes_done, data, bytes_to_copy);
1973  data = ((char *) data) + bytes_to_copy;
1974  length -= bytes_to_copy;
1976  if (pg_checksum_update(checksum_ctx, (uint8 *) sink->bbs_buffer,
1977  sink->bbs_buffer_length) < 0)
1978  elog(ERROR, "could not update checksum");
1979  *bytes_done = 0;
1980  }
1981 }
unsigned char uint8
Definition: c.h:504
int pg_checksum_update(pg_checksum_context *context, const uint8 *input, size_t len)
const void * data

References bbsink::bbs_buffer, bbsink::bbs_buffer_length, bbsink_archive_contents(), data, elog, ERROR, and pg_checksum_update().

Referenced by sendFile().

◆ read_file_data_into_buffer()

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

Definition at line 1848 of file basebackup.c.

1851 {
1852  off_t cnt;
1853  int i;
1854  char *page;
1855 
1856  /* Try to read some more data. */
1857  cnt = basebackup_read_file(fd, sink->bbs_buffer,
1858  Min(sink->bbs_buffer_length, length),
1859  offset, readfilename, true);
1860 
1861  /* Can't verify checksums if read length is not a multiple of BLCKSZ. */
1862  if (!verify_checksum || (cnt % BLCKSZ) != 0)
1863  return cnt;
1864 
1865  /* Verify checksum for each block. */
1866  for (i = 0; i < cnt / BLCKSZ; i++)
1867  {
1868  int reread_cnt;
1869  uint16 expected_checksum;
1870 
1871  page = sink->bbs_buffer + BLCKSZ * i;
1872 
1873  /* If the page is OK, go on to the next one. */
1874  if (verify_page_checksum(page, sink->bbs_state->startptr, blkno + i,
1875  &expected_checksum))
1876  continue;
1877 
1878  /*
1879  * Retry the block on the first failure. It's possible that we read
1880  * the first 4K page of the block just before postgres updated the
1881  * entire block so it ends up looking torn to us. If, before we retry
1882  * the read, the concurrent write of the block finishes, the page LSN
1883  * will be updated and we'll realize that we should ignore this block.
1884  *
1885  * There's no guarantee that this will actually happen, though: the
1886  * torn write could take an arbitrarily long time to complete.
1887  * Retrying multiple times wouldn't fix this problem, either, though
1888  * it would reduce the chances of it happening in practice. The only
1889  * real fix here seems to be to have some kind of interlock that
1890  * allows us to wait until we can be certain that no write to the
1891  * block is in progress. Since we don't have any such thing right now,
1892  * we just do this and hope for the best.
1893  */
1894  reread_cnt =
1895  basebackup_read_file(fd, sink->bbs_buffer + BLCKSZ * i,
1896  BLCKSZ, offset + BLCKSZ * i,
1897  readfilename, false);
1898  if (reread_cnt == 0)
1899  {
1900  /*
1901  * If we hit end-of-file, a concurrent truncation must have
1902  * occurred, so reduce cnt to reflect only the blocks already
1903  * processed and break out of this loop.
1904  */
1905  cnt = BLCKSZ * i;
1906  break;
1907  }
1908 
1909  /* If the page now looks OK, go on to the next one. */
1910  if (verify_page_checksum(page, sink->bbs_state->startptr, blkno + i,
1911  &expected_checksum))
1912  continue;
1913 
1914  /* Handle checksum failure. */
1915  (*checksum_failures)++;
1916  if (*checksum_failures <= 5)
1917  ereport(WARNING,
1918  (errmsg("checksum verification failed in "
1919  "file \"%s\", block %u: calculated "
1920  "%X but expected %X",
1921  readfilename, blkno + i, expected_checksum,
1922  ((PageHeader) page)->pd_checksum)));
1923  if (*checksum_failures == 5)
1924  ereport(WARNING,
1925  (errmsg("further checksum verification "
1926  "failures in file \"%s\" will not "
1927  "be reported", readfilename)));
1928  }
1929 
1930  return cnt;
1931 }
static bool verify_page_checksum(Page page, XLogRecPtr start_lsn, BlockNumber blkno, uint16 *expected_checksum)
Definition: basebackup.c:1992
unsigned short uint16
Definition: c.h:505
int i
Definition: isn.c:73
XLogRecPtr startptr
bbsink_state * bbs_state

References basebackup_read_file(), bbsink::bbs_buffer, bbsink::bbs_buffer_length, bbsink::bbs_state, ereport, errmsg(), fd(), i, Min, bbsink_state::startptr, verify_page_checksum(), and WARNING.

Referenced by sendFile().

◆ SendBaseBackup()

void SendBaseBackup ( BaseBackupCmd cmd,
IncrementalBackupInfo ib 
)

Definition at line 989 of file basebackup.c.

990 {
991  basebackup_options opt;
992  bbsink *sink;
994 
995  if (status == SESSION_BACKUP_RUNNING)
996  ereport(ERROR,
997  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
998  errmsg("a backup is already in progress in this session")));
999 
1000  parse_basebackup_options(cmd->options, &opt);
1001 
1003 
1005  {
1006  char activitymsg[50];
1007 
1008  snprintf(activitymsg, sizeof(activitymsg), "sending backup \"%s\"",
1009  opt.label);
1010  set_ps_display(activitymsg);
1011  }
1012 
1013  /*
1014  * If we're asked to perform an incremental backup and the user has not
1015  * supplied a manifest, that's an ERROR.
1016  *
1017  * If we're asked to perform a full backup and the user did supply a
1018  * manifest, just ignore it.
1019  */
1020  if (!opt.incremental)
1021  ib = NULL;
1022  else if (ib == NULL)
1023  ereport(ERROR,
1024  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1025  errmsg("must UPLOAD_MANIFEST before performing an incremental BASE_BACKUP")));
1026 
1027  /*
1028  * If the target is specifically 'client' then set up to stream the backup
1029  * to the client; otherwise, it's being sent someplace else and should not
1030  * be sent to the client. BaseBackupGetSink has the job of setting up a
1031  * sink to send the backup data wherever it needs to go.
1032  */
1034  if (opt.target_handle != NULL)
1035  sink = BaseBackupGetSink(opt.target_handle, sink);
1036 
1037  /* Set up network throttling, if client requested it */
1038  if (opt.maxrate > 0)
1039  sink = bbsink_throttle_new(sink, opt.maxrate);
1040 
1041  /* Set up server-side compression, if client requested it */
1042  if (opt.compression == PG_COMPRESSION_GZIP)
1043  sink = bbsink_gzip_new(sink, &opt.compression_specification);
1044  else if (opt.compression == PG_COMPRESSION_LZ4)
1045  sink = bbsink_lz4_new(sink, &opt.compression_specification);
1046  else if (opt.compression == PG_COMPRESSION_ZSTD)
1047  sink = bbsink_zstd_new(sink, &opt.compression_specification);
1048 
1049  /* Set up progress reporting. */
1050  sink = bbsink_progress_new(sink, opt.progress);
1051 
1052  /*
1053  * Perform the base backup, but make sure we clean up the bbsink even if
1054  * an error occurs.
1055  */
1056  PG_TRY();
1057  {
1058  perform_base_backup(&opt, sink, ib);
1059  }
1060  PG_FINALLY();
1061  {
1062  bbsink_cleanup(sink);
1063  }
1064  PG_END_TRY();
1065 }
static void parse_basebackup_options(List *options, basebackup_options *opt)
Definition: basebackup.c:697
static void perform_base_backup(basebackup_options *opt, bbsink *sink, IncrementalBackupInfo *ib)
Definition: basebackup.c:235
bbsink * bbsink_copystream_new(bool send_to_client)
bbsink * bbsink_gzip_new(bbsink *next, pg_compress_specification *compress)
bbsink * bbsink_lz4_new(bbsink *next, pg_compress_specification *compress)
bbsink * bbsink_progress_new(bbsink *next, bool estimate_backup_size)
static void bbsink_cleanup(bbsink *sink)
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)
@ PG_COMPRESSION_GZIP
Definition: compression.h:24
@ PG_COMPRESSION_LZ4
Definition: compression.h:25
@ PG_COMPRESSION_ZSTD
Definition: compression.h:26
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define PG_FINALLY(...)
Definition: elog.h:388
bool update_process_title
Definition: ps_status.c:29
static void set_ps_display(const char *activity)
Definition: ps_status.h:40
List * options
Definition: replnodes.h:44
void WalSndSetState(WalSndState state)
Definition: walsender.c:3812
@ WALSNDSTATE_BACKUP
SessionBackupState get_backup_status(void)
Definition: xlog.c:9093
SessionBackupState
Definition: xlog.h:285
@ SESSION_BACKUP_RUNNING
Definition: xlog.h:287

References BaseBackupGetSink(), bbsink_cleanup(), bbsink_copystream_new(), bbsink_gzip_new(), bbsink_lz4_new(), bbsink_progress_new(), bbsink_throttle_new(), bbsink_zstd_new(), basebackup_options::compression, basebackup_options::compression_specification, ereport, errcode(), errmsg(), ERROR, get_backup_status(), basebackup_options::incremental, basebackup_options::label, basebackup_options::maxrate, BaseBackupCmd::options, parse_basebackup_options(), perform_base_backup(), PG_COMPRESSION_GZIP, PG_COMPRESSION_LZ4, PG_COMPRESSION_ZSTD, PG_END_TRY, PG_FINALLY, PG_TRY, basebackup_options::progress, basebackup_options::send_to_client, SESSION_BACKUP_RUNNING, set_ps_display(), snprintf, basebackup_options::target_handle, update_process_title, WalSndSetState(), and WALSNDSTATE_BACKUP.

Referenced by exec_replication_command().

◆ sendDir()

static int64 sendDir ( bbsink sink,
const char *  path,
int  basepathlen,
bool  sizeonly,
List tablespaces,
bool  sendtblspclinks,
backup_manifest_info manifest,
Oid  spcoid,
IncrementalBackupInfo ib 
)
static

Definition at line 1188 of file basebackup.c.

1191 {
1192  DIR *dir;
1193  struct dirent *de;
1194  char pathbuf[MAXPGPATH * 2];
1195  struct stat statbuf;
1196  int64 size = 0;
1197  const char *lastDir; /* Split last dir from parent path. */
1198  bool isRelationDir = false; /* Does directory contain relations? */
1199  bool isGlobalDir = false;
1200  Oid dboid = InvalidOid;
1201  BlockNumber *relative_block_numbers = NULL;
1202 
1203  /*
1204  * Since this array is relatively large, avoid putting it on the stack.
1205  * But we don't need it at all if this is not an incremental backup.
1206  */
1207  if (ib != NULL)
1208  relative_block_numbers = palloc(sizeof(BlockNumber) * RELSEG_SIZE);
1209 
1210  /*
1211  * Determine if the current path is a database directory that can contain
1212  * relations.
1213  *
1214  * Start by finding the location of the delimiter between the parent path
1215  * and the current path.
1216  */
1217  lastDir = last_dir_separator(path);
1218 
1219  /* Does this path look like a database path (i.e. all digits)? */
1220  if (lastDir != NULL &&
1221  strspn(lastDir + 1, "0123456789") == strlen(lastDir + 1))
1222  {
1223  /* Part of path that contains the parent directory. */
1224  int parentPathLen = lastDir - path;
1225 
1226  /*
1227  * Mark path as a database directory if the parent path is either
1228  * $PGDATA/base or a tablespace version path.
1229  */
1230  if (strncmp(path, "./base", parentPathLen) == 0 ||
1231  (parentPathLen >= (sizeof(TABLESPACE_VERSION_DIRECTORY) - 1) &&
1232  strncmp(lastDir - (sizeof(TABLESPACE_VERSION_DIRECTORY) - 1),
1234  sizeof(TABLESPACE_VERSION_DIRECTORY) - 1) == 0))
1235  {
1236  isRelationDir = true;
1237  dboid = atooid(lastDir + 1);
1238  }
1239  }
1240  else if (strcmp(path, "./global") == 0)
1241  {
1242  isRelationDir = true;
1243  isGlobalDir = true;
1244  }
1245 
1246  dir = AllocateDir(path);
1247  while ((de = ReadDir(dir, path)) != NULL)
1248  {
1249  int excludeIdx;
1250  bool excludeFound;
1251  RelFileNumber relfilenumber = InvalidRelFileNumber;
1252  ForkNumber relForkNum = InvalidForkNumber;
1253  unsigned segno = 0;
1254  bool isRelationFile = false;
1255 
1256  /* Skip special stuff */
1257  if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1258  continue;
1259 
1260  /* Skip temporary files */
1261  if (strncmp(de->d_name,
1263  strlen(PG_TEMP_FILE_PREFIX)) == 0)
1264  continue;
1265 
1266  /* Skip macOS system files */
1267  if (strcmp(de->d_name, ".DS_Store") == 0)
1268  continue;
1269 
1270  /*
1271  * Check if the postmaster has signaled us to exit, and abort with an
1272  * error in that case. The error handler further up will call
1273  * do_pg_abort_backup() for us. Also check that if the backup was
1274  * started while still in recovery, the server wasn't promoted.
1275  * do_pg_backup_stop() will check that too, but it's better to stop
1276  * the backup early than continue to the end and fail there.
1277  */
1280  ereport(ERROR,
1281  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1282  errmsg("the standby was promoted during online backup"),
1283  errhint("This means that the backup being taken is corrupt "
1284  "and should not be used. "
1285  "Try taking another online backup.")));
1286 
1287  /* Scan for files that should be excluded */
1288  excludeFound = false;
1289  for (excludeIdx = 0; excludeFiles[excludeIdx].name != NULL; excludeIdx++)
1290  {
1291  int cmplen = strlen(excludeFiles[excludeIdx].name);
1292 
1293  if (!excludeFiles[excludeIdx].match_prefix)
1294  cmplen++;
1295  if (strncmp(de->d_name, excludeFiles[excludeIdx].name, cmplen) == 0)
1296  {
1297  elog(DEBUG1, "file \"%s\" excluded from backup", de->d_name);
1298  excludeFound = true;
1299  break;
1300  }
1301  }
1302 
1303  if (excludeFound)
1304  continue;
1305 
1306  /*
1307  * If there could be non-temporary relation files in this directory,
1308  * try to parse the filename.
1309  */
1310  if (isRelationDir)
1311  isRelationFile =
1313  &relfilenumber,
1314  &relForkNum, &segno);
1315 
1316  /* Exclude all forks for unlogged tables except the init fork */
1317  if (isRelationFile && relForkNum != INIT_FORKNUM)
1318  {
1319  char initForkFile[MAXPGPATH];
1320 
1321  /*
1322  * If any other type of fork, check if there is an init fork with
1323  * the same RelFileNumber. If so, the file can be excluded.
1324  */
1325  snprintf(initForkFile, sizeof(initForkFile), "%s/%u_init",
1326  path, relfilenumber);
1327 
1328  if (lstat(initForkFile, &statbuf) == 0)
1329  {
1330  elog(DEBUG2,
1331  "unlogged relation file \"%s\" excluded from backup",
1332  de->d_name);
1333 
1334  continue;
1335  }
1336  }
1337 
1338  /* Exclude temporary relations */
1339  if (OidIsValid(dboid) && looks_like_temp_rel_name(de->d_name))
1340  {
1341  elog(DEBUG2,
1342  "temporary relation file \"%s\" excluded from backup",
1343  de->d_name);
1344 
1345  continue;
1346  }
1347 
1348  snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path, de->d_name);
1349 
1350  /* Skip pg_control here to back up it last */
1351  if (strcmp(pathbuf, "./global/pg_control") == 0)
1352  continue;
1353 
1354  if (lstat(pathbuf, &statbuf) != 0)
1355  {
1356  if (errno != ENOENT)
1357  ereport(ERROR,
1359  errmsg("could not stat file or directory \"%s\": %m",
1360  pathbuf)));
1361 
1362  /* If the file went away while scanning, it's not an error. */
1363  continue;
1364  }
1365 
1366  /* Scan for directories whose contents should be excluded */
1367  excludeFound = false;
1368  for (excludeIdx = 0; excludeDirContents[excludeIdx] != NULL; excludeIdx++)
1369  {
1370  if (strcmp(de->d_name, excludeDirContents[excludeIdx]) == 0)
1371  {
1372  elog(DEBUG1, "contents of directory \"%s\" excluded from backup", de->d_name);
1373  convert_link_to_directory(pathbuf, &statbuf);
1374  size += _tarWriteHeader(sink, pathbuf + basepathlen + 1, NULL,
1375  &statbuf, sizeonly);
1376  excludeFound = true;
1377  break;
1378  }
1379  }
1380 
1381  if (excludeFound)
1382  continue;
1383 
1384  /*
1385  * We can skip pg_wal, the WAL segments need to be fetched from the
1386  * WAL archive anyway. But include it as an empty directory anyway, so
1387  * we get permissions right.
1388  */
1389  if (strcmp(pathbuf, "./pg_wal") == 0)
1390  {
1391  /* If pg_wal is a symlink, write it as a directory anyway */
1392  convert_link_to_directory(pathbuf, &statbuf);
1393  size += _tarWriteHeader(sink, pathbuf + basepathlen + 1, NULL,
1394  &statbuf, sizeonly);
1395 
1396  /*
1397  * Also send archive_status and summaries directories (by
1398  * hackishly reusing statbuf from above ...).
1399  */
1400  size += _tarWriteHeader(sink, "./pg_wal/archive_status", NULL,
1401  &statbuf, sizeonly);
1402  size += _tarWriteHeader(sink, "./pg_wal/summaries", NULL,
1403  &statbuf, sizeonly);
1404 
1405  continue; /* don't recurse into pg_wal */
1406  }
1407 
1408  /* Allow symbolic links in pg_tblspc only */
1409  if (strcmp(path, "./pg_tblspc") == 0 && S_ISLNK(statbuf.st_mode))
1410  {
1411  char linkpath[MAXPGPATH];
1412  int rllen;
1413 
1414  rllen = readlink(pathbuf, linkpath, sizeof(linkpath));
1415  if (rllen < 0)
1416  ereport(ERROR,
1418  errmsg("could not read symbolic link \"%s\": %m",
1419  pathbuf)));
1420  if (rllen >= sizeof(linkpath))
1421  ereport(ERROR,
1422  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1423  errmsg("symbolic link \"%s\" target is too long",
1424  pathbuf)));
1425  linkpath[rllen] = '\0';
1426 
1427  size += _tarWriteHeader(sink, pathbuf + basepathlen + 1, linkpath,
1428  &statbuf, sizeonly);
1429  }
1430  else if (S_ISDIR(statbuf.st_mode))
1431  {
1432  bool skip_this_dir = false;
1433  ListCell *lc;
1434 
1435  /*
1436  * Store a directory entry in the tar file so we can get the
1437  * permissions right.
1438  */
1439  size += _tarWriteHeader(sink, pathbuf + basepathlen + 1, NULL, &statbuf,
1440  sizeonly);
1441 
1442  /*
1443  * Call ourselves recursively for a directory, unless it happens
1444  * to be a separate tablespace located within PGDATA.
1445  */
1446  foreach(lc, tablespaces)
1447  {
1448  tablespaceinfo *ti = (tablespaceinfo *) lfirst(lc);
1449 
1450  /*
1451  * ti->rpath is the tablespace relative path within PGDATA, or
1452  * NULL if the tablespace has been properly located somewhere
1453  * else.
1454  *
1455  * Skip past the leading "./" in pathbuf when comparing.
1456  */
1457  if (ti->rpath && strcmp(ti->rpath, pathbuf + 2) == 0)
1458  {
1459  skip_this_dir = true;
1460  break;
1461  }
1462  }
1463 
1464  /*
1465  * skip sending directories inside pg_tblspc, if not required.
1466  */
1467  if (strcmp(pathbuf, "./pg_tblspc") == 0 && !sendtblspclinks)
1468  skip_this_dir = true;
1469 
1470  if (!skip_this_dir)
1471  size += sendDir(sink, pathbuf, basepathlen, sizeonly, tablespaces,
1472  sendtblspclinks, manifest, spcoid, ib);
1473  }
1474  else if (S_ISREG(statbuf.st_mode))
1475  {
1476  bool sent = false;
1477  unsigned num_blocks_required = 0;
1478  unsigned truncation_block_length = 0;
1479  char tarfilenamebuf[MAXPGPATH * 2];
1480  char *tarfilename = pathbuf + basepathlen + 1;
1482 
1483  if (ib != NULL && isRelationFile)
1484  {
1485  Oid relspcoid;
1486  char *lookup_path;
1487 
1488  if (OidIsValid(spcoid))
1489  {
1490  relspcoid = spcoid;
1491  lookup_path = psprintf("%s/%u/%s", PG_TBLSPC_DIR, spcoid,
1492  tarfilename);
1493  }
1494  else
1495  {
1496  if (isGlobalDir)
1497  relspcoid = GLOBALTABLESPACE_OID;
1498  else
1499  relspcoid = DEFAULTTABLESPACE_OID;
1500  lookup_path = pstrdup(tarfilename);
1501  }
1502 
1503  method = GetFileBackupMethod(ib, lookup_path, dboid, relspcoid,
1504  relfilenumber, relForkNum,
1505  segno, statbuf.st_size,
1506  &num_blocks_required,
1507  relative_block_numbers,
1508  &truncation_block_length);
1509  if (method == BACK_UP_FILE_INCREMENTALLY)
1510  {
1511  statbuf.st_size =
1512  GetIncrementalFileSize(num_blocks_required);
1513  snprintf(tarfilenamebuf, sizeof(tarfilenamebuf),
1514  "%s/INCREMENTAL.%s",
1515  path + basepathlen + 1,
1516  de->d_name);
1517  tarfilename = tarfilenamebuf;
1518  }
1519 
1520  pfree(lookup_path);
1521  }
1522 
1523  if (!sizeonly)
1524  sent = sendFile(sink, pathbuf, tarfilename, &statbuf,
1525  true, dboid, spcoid,
1526  relfilenumber, segno, manifest,
1527  num_blocks_required,
1528  method == BACK_UP_FILE_INCREMENTALLY ? relative_block_numbers : NULL,
1529  truncation_block_length);
1530 
1531  if (sent || sizeonly)
1532  {
1533  /* Add size. */
1534  size += statbuf.st_size;
1535 
1536  /* Pad to a multiple of the tar block size. */
1537  size += tarPaddingBytesRequired(statbuf.st_size);
1538 
1539  /* Size of the header for the file. */
1540  size += TAR_BLOCK_SIZE;
1541  }
1542  }
1543  else
1544  ereport(WARNING,
1545  (errmsg("skipping special file \"%s\"", pathbuf)));
1546  }
1547 
1548  if (relative_block_numbers != NULL)
1549  pfree(relative_block_numbers);
1550 
1551  FreeDir(dir);
1552  return size;
1553 }
static const struct exclude_list_item excludeFiles[]
Definition: basebackup.c:192
static const char *const excludeDirContents[]
Definition: basebackup.c:152
static void convert_link_to_directory(const char *pathbuf, struct stat *statbuf)
Definition: basebackup.c:2097
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)
@ BACK_UP_FILE_INCREMENTALLY
@ BACK_UP_FILE_FULLY
uint32 BlockNumber
Definition: block.h:31
#define OidIsValid(objectId)
Definition: c.h:775
int errhint(const char *fmt,...)
Definition: elog.c:1317
#define DEBUG2
Definition: elog.h:29
#define DEBUG1
Definition: elog.h:30
bool looks_like_temp_rel_name(const char *name)
Definition: fd.c:3473
#define PG_TEMP_FILE_PREFIX
Definition: file_utils.h:63
void * palloc(Size size)
Definition: mcxt.c:1317
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:122
char * last_dir_separator(const char *filename)
Definition: path.c:140
unsigned int Oid
Definition: postgres_ext.h:31
#define atooid(x)
Definition: postgres_ext.h:42
bool parse_filename_for_nontemp_relation(const char *name, RelFileNumber *relnumber, ForkNumber *fork, unsigned *segno)
Definition: reinit.c:380
Oid RelFileNumber
Definition: relpath.h:25
ForkNumber
Definition: relpath.h:56
@ InvalidForkNumber
Definition: relpath.h:57
@ INIT_FORKNUM
Definition: relpath.h:61
#define PG_TBLSPC_DIR
Definition: relpath.h:41
#define TABLESPACE_VERSION_DIRECTORY
Definition: relpath.h:33
static pg_noinline void Size size
Definition: slab.c:607
const char * name
Definition: basebackup.c:140
char * rpath
Definition: basebackup.h:32
const char * name
#define S_ISDIR(m)
Definition: win32_port.h:325
#define readlink(path, buf, size)
Definition: win32_port.h:236
#define S_ISREG(m)
Definition: win32_port.h:328

References _tarWriteHeader(), AllocateDir(), atooid, BACK_UP_FILE_FULLY, BACK_UP_FILE_INCREMENTALLY, backup_started_in_recovery, CHECK_FOR_INTERRUPTS, convert_link_to_directory(), dirent::d_name, DEBUG1, DEBUG2, elog, ereport, errcode(), errcode_for_file_access(), errhint(), errmsg(), ERROR, excludeDirContents, excludeFiles, FreeDir(), GetFileBackupMethod(), GetIncrementalFileSize(), INIT_FORKNUM, InvalidForkNumber, InvalidOid, InvalidRelFileNumber, last_dir_separator(), lfirst, looks_like_temp_rel_name(), lstat, manifest, MAXPGPATH, exclude_list_item::name, name, OidIsValid, palloc(), parse_filename_for_nontemp_relation(), pfree(), PG_TBLSPC_DIR, PG_TEMP_FILE_PREFIX, psprintf(), pstrdup(), ReadDir(), readlink, RecoveryInProgress(), tablespaceinfo::rpath, S_ISDIR, S_ISLNK, S_ISREG, sendFile(), size, snprintf, stat::st_mode, stat::st_size, TABLESPACE_VERSION_DIRECTORY, TAR_BLOCK_SIZE, tarPaddingBytesRequired(), and WARNING.

Referenced by perform_base_backup(), and sendTablespace().

◆ sendFile()

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

Definition at line 1573 of file basebackup.c.

1578 {
1579  int fd;
1580  BlockNumber blkno = 0;
1581  int checksum_failures = 0;
1582  off_t cnt;
1583  pgoff_t bytes_done = 0;
1584  bool verify_checksum = false;
1585  pg_checksum_context checksum_ctx;
1586  int ibindex = 0;
1587 
1588  if (pg_checksum_init(&checksum_ctx, manifest->checksum_type) < 0)
1589  elog(ERROR, "could not initialize checksum of file \"%s\"",
1590  readfilename);
1591 
1592  fd = OpenTransientFile(readfilename, O_RDONLY | PG_BINARY);
1593  if (fd < 0)
1594  {
1595  if (errno == ENOENT && missing_ok)
1596  return false;
1597  ereport(ERROR,
1599  errmsg("could not open file \"%s\": %m", readfilename)));
1600  }
1601 
1602  _tarWriteHeader(sink, tarfilename, NULL, statbuf, false);
1603 
1604  /*
1605  * Checksums are verified in multiples of BLCKSZ, so the buffer length
1606  * should be a multiple of the block size as well.
1607  */
1608  Assert((sink->bbs_buffer_length % BLCKSZ) == 0);
1609 
1610  /*
1611  * If we weren't told not to verify checksums, and if checksums are
1612  * enabled for this cluster, and if this is a relation file, then verify
1613  * the checksum.
1614  */
1616  RelFileNumberIsValid(relfilenumber))
1617  verify_checksum = true;
1618 
1619  /*
1620  * If we're sending an incremental file, write the file header.
1621  */
1622  if (incremental_blocks != NULL)
1623  {
1624  unsigned magic = INCREMENTAL_MAGIC;
1625  size_t header_bytes_done = 0;
1626  char padding[BLCKSZ];
1627  size_t paddinglen;
1628 
1629  /* Emit header data. */
1630  push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1631  &magic, sizeof(magic));
1632  push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1633  &num_incremental_blocks, sizeof(num_incremental_blocks));
1634  push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1635  &truncation_block_length, sizeof(truncation_block_length));
1636  push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1637  incremental_blocks,
1638  sizeof(BlockNumber) * num_incremental_blocks);
1639 
1640  /*
1641  * Add padding to align header to a multiple of BLCKSZ, but only if
1642  * the incremental file has some blocks, and the alignment is actually
1643  * needed (i.e. header is not already a multiple of BLCKSZ). If there
1644  * are no blocks we don't want to make the file unnecessarily large,
1645  * as that might make some filesystem optimizations impossible.
1646  */
1647  if ((num_incremental_blocks > 0) && (header_bytes_done % BLCKSZ != 0))
1648  {
1649  paddinglen = (BLCKSZ - (header_bytes_done % BLCKSZ));
1650 
1651  memset(padding, 0, paddinglen);
1652  bytes_done += paddinglen;
1653 
1654  push_to_sink(sink, &checksum_ctx, &header_bytes_done,
1655  padding, paddinglen);
1656  }
1657 
1658  /* Flush out any data still in the buffer so it's again empty. */
1659  if (header_bytes_done > 0)
1660  {
1661  bbsink_archive_contents(sink, header_bytes_done);
1662  if (pg_checksum_update(&checksum_ctx,
1663  (uint8 *) sink->bbs_buffer,
1664  header_bytes_done) < 0)
1665  elog(ERROR, "could not update checksum of base backup");
1666  }
1667 
1668  /* Update our notion of file position. */
1669  bytes_done += sizeof(magic);
1670  bytes_done += sizeof(num_incremental_blocks);
1671  bytes_done += sizeof(truncation_block_length);
1672  bytes_done += sizeof(BlockNumber) * num_incremental_blocks;
1673  }
1674 
1675  /*
1676  * Loop until we read the amount of data the caller told us to expect. The
1677  * file could be longer, if it was extended while we were sending it, but
1678  * for a base backup we can ignore such extended data. It will be restored
1679  * from WAL.
1680  */
1681  while (1)
1682  {
1683  /*
1684  * Determine whether we've read all the data that we need, and if not,
1685  * read some more.
1686  */
1687  if (incremental_blocks == NULL)
1688  {
1689  size_t remaining = statbuf->st_size - bytes_done;
1690 
1691  /*
1692  * If we've read the required number of bytes, then it's time to
1693  * stop.
1694  */
1695  if (bytes_done >= statbuf->st_size)
1696  break;
1697 
1698  /*
1699  * Read as many bytes as will fit in the buffer, or however many
1700  * are left to read, whichever is less.
1701  */
1702  cnt = read_file_data_into_buffer(sink, readfilename, fd,
1703  bytes_done, remaining,
1704  blkno + segno * RELSEG_SIZE,
1705  verify_checksum,
1706  &checksum_failures);
1707  }
1708  else
1709  {
1710  BlockNumber relative_blkno;
1711 
1712  /*
1713  * If we've read all the blocks, then it's time to stop.
1714  */
1715  if (ibindex >= num_incremental_blocks)
1716  break;
1717 
1718  /*
1719  * Read just one block, whichever one is the next that we're
1720  * supposed to include.
1721  */
1722  relative_blkno = incremental_blocks[ibindex++];
1723  cnt = read_file_data_into_buffer(sink, readfilename, fd,
1724  relative_blkno * BLCKSZ,
1725  BLCKSZ,
1726  relative_blkno + segno * RELSEG_SIZE,
1727  verify_checksum,
1728  &checksum_failures);
1729 
1730  /*
1731  * If we get a partial read, that must mean that the relation is
1732  * being truncated. Ultimately, it should be truncated to a
1733  * multiple of BLCKSZ, since this path should only be reached for
1734  * relation files, but we might transiently observe an
1735  * intermediate value.
1736  *
1737  * It should be fine to treat this just as if the entire block had
1738  * been truncated away - i.e. fill this and all later blocks with
1739  * zeroes. WAL replay will fix things up.
1740  */
1741  if (cnt < BLCKSZ)
1742  break;
1743  }
1744 
1745  /*
1746  * If the amount of data we were able to read was not a multiple of
1747  * BLCKSZ, we cannot verify checksums, which are block-level.
1748  */
1749  if (verify_checksum && (cnt % BLCKSZ != 0))
1750  {
1751  ereport(WARNING,
1752  (errmsg("could not verify checksum in file \"%s\", block "
1753  "%u: read buffer size %d and page size %d "
1754  "differ",
1755  readfilename, blkno, (int) cnt, BLCKSZ)));
1756  verify_checksum = false;
1757  }
1758 
1759  /*
1760  * If we hit end-of-file, a concurrent truncation must have occurred.
1761  * That's not an error condition, because WAL replay will fix things
1762  * up.
1763  */
1764  if (cnt == 0)
1765  break;
1766 
1767  /* Update block number and # of bytes done for next loop iteration. */
1768  blkno += cnt / BLCKSZ;
1769  bytes_done += cnt;
1770 
1771  /*
1772  * Make sure incremental files with block data are properly aligned
1773  * (header is a multiple of BLCKSZ, blocks are BLCKSZ too).
1774  */
1775  Assert(!((incremental_blocks != NULL && num_incremental_blocks > 0) &&
1776  (bytes_done % BLCKSZ != 0)));
1777 
1778  /* Archive the data we just read. */
1779  bbsink_archive_contents(sink, cnt);
1780 
1781  /* Also feed it to the checksum machinery. */
1782  if (pg_checksum_update(&checksum_ctx,
1783  (uint8 *) sink->bbs_buffer, cnt) < 0)
1784  elog(ERROR, "could not update checksum of base backup");
1785  }
1786 
1787  /* If the file was truncated while we were sending it, pad it with zeros */
1788  while (bytes_done < statbuf->st_size)
1789  {
1790  size_t remaining = statbuf->st_size - bytes_done;
1791  size_t nbytes = Min(sink->bbs_buffer_length, remaining);
1792 
1793  MemSet(sink->bbs_buffer, 0, nbytes);
1794  if (pg_checksum_update(&checksum_ctx,
1795  (uint8 *) sink->bbs_buffer,
1796  nbytes) < 0)
1797  elog(ERROR, "could not update checksum of base backup");
1798  bbsink_archive_contents(sink, nbytes);
1799  bytes_done += nbytes;
1800  }
1801 
1802  /*
1803  * Pad to a block boundary, per tar format requirements. (This small piece
1804  * of data is probably not worth throttling, and is not checksummed
1805  * because it's not actually part of the file.)
1806  */
1807  _tarWritePadding(sink, bytes_done);
1808 
1810 
1811  if (checksum_failures > 1)
1812  {
1813  ereport(WARNING,
1814  (errmsg_plural("file \"%s\" has a total of %d checksum verification failure",
1815  "file \"%s\" has a total of %d checksum verification failures",
1816  checksum_failures,
1817  readfilename, checksum_failures)));
1818 
1819  pgstat_report_checksum_failures_in_db(dboid, checksum_failures);
1820  }
1821 
1822  total_checksum_failures += checksum_failures;
1823 
1824  AddFileToBackupManifest(manifest, spcoid, tarfilename, statbuf->st_size,
1825  (pg_time_t) statbuf->st_mtime, &checksum_ctx);
1826 
1827  return true;
1828 }
void AddFileToBackupManifest(backup_manifest_info *manifest, Oid spcoid, const char *pathname, size_t size, pg_time_t mtime, pg_checksum_context *checksum_ctx)
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)
Definition: basebackup.c:1848
static void push_to_sink(bbsink *sink, pg_checksum_context *checksum_ctx, size_t *bytes_done, void *data, size_t length)
Definition: basebackup.c:1951
static void _tarWritePadding(bbsink *sink, int len)
Definition: basebackup.c:2074
#define INCREMENTAL_MAGIC
int pg_checksum_init(pg_checksum_context *context, pg_checksum_type type)
int remaining
Definition: informix.c:692
void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount)
int64 pg_time_t
Definition: pgtime.h:23
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27
bool DataChecksumsEnabled(void)
Definition: xlog.c:4581

References _tarWriteHeader(), _tarWritePadding(), AddFileToBackupManifest(), Assert, bbsink::bbs_buffer, bbsink::bbs_buffer_length, bbsink_archive_contents(), CloseTransientFile(), DataChecksumsEnabled(), elog, ereport, errcode_for_file_access(), errmsg(), errmsg_plural(), ERROR, fd(), INCREMENTAL_MAGIC, manifest, MemSet, Min, noverify_checksums, OpenTransientFile(), PG_BINARY, pg_checksum_init(), pg_checksum_update(), pgoff_t, pgstat_report_checksum_failures_in_db(), push_to_sink(), read_file_data_into_buffer(), RelFileNumberIsValid, remaining, stat::st_mtime, stat::st_size, total_checksum_failures, and WARNING.

Referenced by perform_base_backup(), and sendDir().

◆ sendFileWithContent()

static void sendFileWithContent ( bbsink sink,
const char *  filename,
const char *  content,
int  len,
backup_manifest_info manifest 
)
static

Definition at line 1074 of file basebackup.c.

1076 {
1077  struct stat statbuf;
1078  int bytes_done = 0;
1079  pg_checksum_context checksum_ctx;
1080 
1081  if (pg_checksum_init(&checksum_ctx, manifest->checksum_type) < 0)
1082  elog(ERROR, "could not initialize checksum of file \"%s\"",
1083  filename);
1084 
1085  if (len < 0)
1086  len = strlen(content);
1087 
1088  /*
1089  * Construct a stat struct for the file we're injecting in the tar.
1090  */
1091 
1092  /* Windows doesn't have the concept of uid and gid */
1093 #ifdef WIN32
1094  statbuf.st_uid = 0;
1095  statbuf.st_gid = 0;
1096 #else
1097  statbuf.st_uid = geteuid();
1098  statbuf.st_gid = getegid();
1099 #endif
1100  statbuf.st_mtime = time(NULL);
1101  statbuf.st_mode = pg_file_create_mode;
1102  statbuf.st_size = len;
1103 
1104  _tarWriteHeader(sink, filename, NULL, &statbuf, false);
1105 
1106  if (pg_checksum_update(&checksum_ctx, (uint8 *) content, len) < 0)
1107  elog(ERROR, "could not update checksum of file \"%s\"",
1108  filename);
1109 
1110  while (bytes_done < len)
1111  {
1112  size_t remaining = len - bytes_done;
1113  size_t nbytes = Min(sink->bbs_buffer_length, remaining);
1114 
1115  memcpy(sink->bbs_buffer, content, nbytes);
1116  bbsink_archive_contents(sink, nbytes);
1117  bytes_done += nbytes;
1118  content += nbytes;
1119  }
1120 
1121  _tarWritePadding(sink, len);
1122 
1124  (pg_time_t) statbuf.st_mtime, &checksum_ctx);
1125 }
int pg_file_create_mode
Definition: file_perm.c:19

References _tarWriteHeader(), _tarWritePadding(), AddFileToBackupManifest(), bbsink::bbs_buffer, bbsink::bbs_buffer_length, bbsink_archive_contents(), elog, ERROR, filename, InvalidOid, len, manifest, Min, pg_checksum_init(), pg_checksum_update(), pg_file_create_mode, remaining, stat::st_gid, stat::st_mode, stat::st_mtime, stat::st_size, and stat::st_uid.

Referenced by perform_base_backup().

◆ sendTablespace()

static int64 sendTablespace ( bbsink sink,
char *  path,
Oid  spcoid,
bool  sizeonly,
struct backup_manifest_info manifest,
IncrementalBackupInfo ib 
)
static

Definition at line 1135 of file basebackup.c.

1137 {
1138  int64 size;
1139  char pathbuf[MAXPGPATH];
1140  struct stat statbuf;
1141 
1142  /*
1143  * 'path' points to the tablespace location, but we only want to include
1144  * the version directory in it that belongs to us.
1145  */
1146  snprintf(pathbuf, sizeof(pathbuf), "%s/%s", path,
1148 
1149  /*
1150  * Store a directory entry in the tar file so we get the permissions
1151  * right.
1152  */
1153  if (lstat(pathbuf, &statbuf) != 0)
1154  {
1155  if (errno != ENOENT)
1156  ereport(ERROR,
1158  errmsg("could not stat file or directory \"%s\": %m",
1159  pathbuf)));
1160 
1161  /* If the tablespace went away while scanning, it's no error. */
1162  return 0;
1163  }
1164 
1165  size = _tarWriteHeader(sink, TABLESPACE_VERSION_DIRECTORY, NULL, &statbuf,
1166  sizeonly);
1167 
1168  /* Send all the files in the tablespace version directory */
1169  size += sendDir(sink, pathbuf, strlen(path), sizeonly, NIL, true, manifest,
1170  spcoid, ib);
1171 
1172  return size;
1173 }

References _tarWriteHeader(), ereport, errcode_for_file_access(), errmsg(), ERROR, lstat, manifest, MAXPGPATH, NIL, sendDir(), size, snprintf, and TABLESPACE_VERSION_DIRECTORY.

Referenced by perform_base_backup().

◆ verify_page_checksum()

static bool verify_page_checksum ( Page  page,
XLogRecPtr  start_lsn,
BlockNumber  blkno,
uint16 expected_checksum 
)
static

Definition at line 1992 of file basebackup.c.

1994 {
1995  PageHeader phdr;
1996  uint16 checksum;
1997 
1998  /*
1999  * Only check pages which have not been modified since the start of the
2000  * base backup. Otherwise, they might have been written only halfway and
2001  * the checksum would not be valid. However, replaying WAL would
2002  * reinstate the correct page in this case. We also skip completely new
2003  * pages, since they don't have a checksum yet.
2004  */
2005  if (PageIsNew(page) || PageGetLSN(page) >= start_lsn)
2006  return true;
2007 
2008  /* Perform the actual checksum calculation. */
2009  checksum = pg_checksum_page(page, blkno);
2010 
2011  /* See whether it matches the value from the page. */
2012  phdr = (PageHeader) page;
2013  if (phdr->pd_checksum == checksum)
2014  return true;
2015  *expected_checksum = checksum;
2016  return false;
2017 }
PageHeaderData * PageHeader
Definition: bufpage.h:173
static bool PageIsNew(Page page)
Definition: bufpage.h:233
static XLogRecPtr PageGetLSN(const char *page)
Definition: bufpage.h:386
uint16 pg_checksum_page(char *page, BlockNumber blkno)
uint16 pd_checksum
Definition: bufpage.h:163

References PageGetLSN(), PageIsNew(), PageHeaderData::pd_checksum, and pg_checksum_page().

Referenced by read_file_data_into_buffer().

Variable Documentation

◆ backup_started_in_recovery

bool backup_started_in_recovery = false
static

Definition at line 124 of file basebackup.c.

Referenced by do_pg_backup_start(), perform_base_backup(), and sendDir().

◆ excludeDirContents

const char* const excludeDirContents[]
static
Initial value:
=
{
"pg_notify",
"pg_serial",
"pg_snapshots",
"pg_subtrans",
NULL
}
#define PG_DYNSHMEM_DIR
Definition: dsm_impl.h:51
#define PG_STAT_TMP_DIR
Definition: pgstat.h:34
#define PG_REPLSLOT_DIR
Definition: slot.h:21

Definition at line 152 of file basebackup.c.

Referenced by sendDir().

◆ excludeFiles

const struct exclude_list_item excludeFiles[]
static
Initial value:
=
{
{PG_AUTOCONF_FILENAME ".tmp", false},
{TABLESPACE_MAP, false},
{"backup_manifest", false},
{"postmaster.pid", false},
{"postmaster.opts", false},
{NULL, false}
}
#define PG_AUTOCONF_FILENAME
Definition: guc.h:33
#define RELCACHE_INIT_FILENAME
Definition: relcache.h:25
#define LOG_METAINFO_DATAFILE_TMP
Definition: syslogger.h:103

Definition at line 152 of file basebackup.c.

Referenced by sendDir().

◆ noverify_checksums

bool noverify_checksums = false
static

Definition at line 130 of file basebackup.c.

Referenced by parse_basebackup_options(), and sendFile().

◆ total_checksum_failures

long long int total_checksum_failures
static

Definition at line 127 of file basebackup.c.

Referenced by perform_base_backup(), and sendFile().