58 #define SINK_BUFFER_LENGTH Max(32768, BLCKSZ)
80 static int64
sendDir(
bbsink *sink,
const char *path,
int basepathlen,
bool sizeonly,
81 List *tablespaces,
bool sendtblspclinks,
83 static bool sendFile(
bbsink *sink,
const char *readfilename,
const char *tarfilename,
84 struct stat *statbuf,
bool missing_ok,
Oid dboid,
90 const char *linktarget,
struct stat *statbuf,
99 const char *
filename,
bool partial_read_ok);
197 {
"backup_manifest",
false},
199 {
"postmaster.pid",
false},
200 {
"postmaster.opts",
false},
213 {
"pg_control",
false},
214 {
"pg_filenode.map",
false},
215 {
"pg_internal.init",
true},
216 {
"PG_VERSION",
false},
218 {
"config_exec_params",
true},
241 state.tablespace_num = 0;
242 state.bytes_done = 0;
243 state.bytes_total = 0;
244 state.bytes_total_is_valid =
false;
293 foreach(lc,
state.tablespaces)
297 if (tmp->
path == NULL)
305 state.bytes_total_is_valid =
true;
312 foreach(lc,
state.tablespaces)
316 if (ti->
path == NULL)
319 bool sendtblspclinks =
true;
335 sendtblspclinks =
false;
346 errmsg(
"could not stat file \"%s\": %m",
374 "BLCKSZ too small for 2 tar blocks");
434 while ((de =
ReadDir(dir,
"pg_wal")) != NULL)
438 strcmp(de->
d_name + 8, firstoff + 8) >= 0 &&
439 strcmp(de->
d_name + 8, lastoff + 8) <= 0)
468 if (walFileList ==
NIL)
470 (
errmsg(
"could not find any WAL files")));
478 if (segno != startsegno)
485 (
errmsg(
"could not find WAL file \"%s\"", startfname)));
487 foreach(lc, walFileList)
489 char *walFileName = (
char *)
lfirst(lc);
494 if (!(nextsegno == segno || currsegno == segno))
500 (
errmsg(
"could not find WAL file \"%s\"", nextfname)));
503 if (segno != endsegno)
509 (
errmsg(
"could not find WAL file \"%s\"", endfname)));
513 foreach(lc, walFileList)
515 char *walFileName = (
char *)
lfirst(lc);
526 int save_errno = errno;
538 errmsg(
"could not open file \"%s\": %m", pathbuf)));
544 errmsg(
"could not stat file \"%s\": %m",
551 errmsg(
"unexpected WAL file size \"%s\"", walFileName)));
560 len, pathbuf,
true)) > 0)
576 errmsg(
"unexpected WAL file size \"%s\"", walFileName)));
606 foreach(lc, historyFileList)
612 if (
lstat(pathbuf, &statbuf) != 0)
615 errmsg(
"could not stat file \"%s\": %m", pathbuf)));
627 "BLCKSZ too small for 2 tar blocks");
647 "%lld total checksum verification failures",
653 errmsg(
"checksum verification failure during base backup")));
676 char *fna = (
char *)
lfirst(
a);
677 char *fnb = (
char *)
lfirst(
b);
679 return strcmp(fna + 8, fnb + 8);
689 bool o_label =
false;
690 bool o_progress =
false;
691 bool o_checkpoint =
false;
692 bool o_nowait =
false;
694 bool o_maxrate =
false;
695 bool o_tablespace_map =
false;
696 bool o_noverify_checksums =
false;
697 bool o_manifest =
false;
698 bool o_manifest_checksums =
false;
699 bool o_target =
false;
700 bool o_target_detail =
false;
701 char *target_str = NULL;
702 char *target_detail_str = NULL;
703 bool o_compression =
false;
704 bool o_compression_detail =
false;
705 char *compression_detail_str = NULL;
707 MemSet(opt, 0,
sizeof(*opt));
717 if (strcmp(defel->
defname,
"label") == 0)
721 (
errcode(ERRCODE_SYNTAX_ERROR),
726 else if (strcmp(defel->
defname,
"progress") == 0)
730 (
errcode(ERRCODE_SYNTAX_ERROR),
735 else if (strcmp(defel->
defname,
"checkpoint") == 0)
741 (
errcode(ERRCODE_SYNTAX_ERROR),
749 (
errcode(ERRCODE_SYNTAX_ERROR),
750 errmsg(
"unrecognized checkpoint type: \"%s\"",
754 else if (strcmp(defel->
defname,
"wait") == 0)
758 (
errcode(ERRCODE_SYNTAX_ERROR),
763 else if (strcmp(defel->
defname,
"wal") == 0)
767 (
errcode(ERRCODE_SYNTAX_ERROR),
772 else if (strcmp(defel->
defname,
"max_rate") == 0)
778 (
errcode(ERRCODE_SYNTAX_ERROR),
784 (
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
785 errmsg(
"%d is outside the valid range for parameter \"%s\" (%d .. %d)",
791 else if (strcmp(defel->
defname,
"tablespace_map") == 0)
793 if (o_tablespace_map)
795 (
errcode(ERRCODE_SYNTAX_ERROR),
798 o_tablespace_map =
true;
800 else if (strcmp(defel->
defname,
"verify_checksums") == 0)
802 if (o_noverify_checksums)
804 (
errcode(ERRCODE_SYNTAX_ERROR),
807 o_noverify_checksums =
true;
809 else if (strcmp(defel->
defname,
"manifest") == 0)
816 (
errcode(ERRCODE_SYNTAX_ERROR),
829 (
errcode(ERRCODE_SYNTAX_ERROR),
830 errmsg(
"unrecognized manifest option: \"%s\"",
834 else if (strcmp(defel->
defname,
"manifest_checksums") == 0)
838 if (o_manifest_checksums)
840 (
errcode(ERRCODE_SYNTAX_ERROR),
845 (
errcode(ERRCODE_SYNTAX_ERROR),
846 errmsg(
"unrecognized checksum algorithm: \"%s\"",
848 o_manifest_checksums =
true;
850 else if (strcmp(defel->
defname,
"target") == 0)
854 (
errcode(ERRCODE_SYNTAX_ERROR),
859 else if (strcmp(defel->
defname,
"target_detail") == 0)
865 (
errcode(ERRCODE_SYNTAX_ERROR),
867 target_detail_str = optval;
868 o_target_detail =
true;
870 else if (strcmp(defel->
defname,
"compression") == 0)
876 (
errcode(ERRCODE_SYNTAX_ERROR),
880 (
errcode(ERRCODE_SYNTAX_ERROR),
881 errmsg(
"unrecognized compression algorithm: \"%s\"",
883 o_compression =
true;
885 else if (strcmp(defel->
defname,
"compression_detail") == 0)
887 if (o_compression_detail)
889 (
errcode(ERRCODE_SYNTAX_ERROR),
892 o_compression_detail =
true;
896 (
errcode(ERRCODE_SYNTAX_ERROR),
897 errmsg(
"unrecognized base backup option: \"%s\"",
901 if (opt->
label == NULL)
902 opt->
label =
"base backup";
905 if (o_manifest_checksums)
907 (
errcode(ERRCODE_SYNTAX_ERROR),
908 errmsg(
"manifest checksums require a backup manifest")));
912 if (target_str == NULL)
914 if (target_detail_str != NULL)
916 (
errcode(ERRCODE_SYNTAX_ERROR),
917 errmsg(
"target detail cannot be used without target")));
921 else if (strcmp(target_str,
"client") == 0)
923 if (target_detail_str != NULL)
925 (
errcode(ERRCODE_SYNTAX_ERROR),
926 errmsg(
"target \"%s\" does not accept a target detail",
934 if (o_compression_detail && !o_compression)
936 (
errcode(ERRCODE_SYNTAX_ERROR),
937 errmsg(
"compression detail cannot be specified unless compression is enabled")));
947 if (error_detail != NULL)
950 errmsg(
"invalid compression specification: %s",
972 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
973 errmsg(
"a backup is already in progress in this session")));
981 char activitymsg[50];
983 snprintf(activitymsg,
sizeof(activitymsg),
"sending backup \"%s\"",
1035 struct stat statbuf;
1041 elog(
ERROR,
"could not initialize checksum of file \"%s\"",
1044 len = strlen(content);
1055 statbuf.
st_uid = geteuid();
1056 statbuf.
st_gid = getegid();
1065 elog(
ERROR,
"could not update checksum of file \"%s\"",
1068 while (bytes_done <
len)
1075 bytes_done += nbytes;
1098 struct stat statbuf;
1104 snprintf(pathbuf,
sizeof(pathbuf),
"%s/%s", path,
1111 if (
lstat(pathbuf, &statbuf) != 0)
1113 if (errno != ENOENT)
1116 errmsg(
"could not stat file or directory \"%s\": %m",
1153 struct stat statbuf;
1155 const char *lastDir;
1156 bool isDbDir =
false;
1168 if (lastDir != NULL &&
1169 strspn(lastDir + 1,
"0123456789") == strlen(lastDir + 1))
1172 int parentPathLen = lastDir - path;
1178 if (strncmp(path,
"./base", parentPathLen) == 0 ||
1187 while ((de =
ReadDir(dir, path)) != NULL)
1196 if (strcmp(de->
d_name,
".") == 0 || strcmp(de->
d_name,
"..") == 0)
1216 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
1217 errmsg(
"the standby was promoted during online backup"),
1218 errhint(
"This means that the backup being taken is corrupt "
1219 "and should not be used. "
1220 "Try taking another online backup.")));
1223 excludeFound =
false;
1233 excludeFound =
true;
1257 memcpy(relNumber, de->
d_name, relnumchars);
1258 relNumber[relnumchars] =
'\0';
1259 snprintf(initForkFile,
sizeof(initForkFile),
"%s/%s_init",
1262 if (
lstat(initForkFile, &statbuf) == 0)
1265 "unlogged relation file \"%s\" excluded from backup",
1277 "temporary relation file \"%s\" excluded from backup",
1286 if (strcmp(pathbuf,
"./global/pg_control") == 0)
1289 if (
lstat(pathbuf, &statbuf) != 0)
1291 if (errno != ENOENT)
1294 errmsg(
"could not stat file or directory \"%s\": %m",
1302 excludeFound =
false;
1307 elog(
DEBUG1,
"contents of directory \"%s\" excluded from backup", de->
d_name);
1310 &statbuf, sizeonly);
1311 excludeFound =
true;
1324 if (strcmp(pathbuf,
"./pg_wal") == 0)
1329 &statbuf, sizeonly);
1336 &statbuf, sizeonly);
1342 if (strcmp(path,
"./pg_tblspc") == 0 &&
S_ISLNK(statbuf.
st_mode))
1347 rllen =
readlink(pathbuf, linkpath,
sizeof(linkpath));
1351 errmsg(
"could not read symbolic link \"%s\": %m",
1353 if (rllen >=
sizeof(linkpath))
1355 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1356 errmsg(
"symbolic link \"%s\" target is too long",
1358 linkpath[rllen] =
'\0';
1361 &statbuf, sizeonly);
1365 bool skip_this_dir =
false;
1372 size +=
_tarWriteHeader(sink, pathbuf + basepathlen + 1, NULL, &statbuf,
1379 foreach(lc, tablespaces)
1390 if (ti->
rpath && strcmp(ti->
rpath, pathbuf + 2) == 0)
1392 skip_this_dir =
true;
1400 if (strcmp(pathbuf,
"./pg_tblspc") == 0 && !sendtblspclinks)
1401 skip_this_dir =
true;
1404 size +=
sendDir(sink, pathbuf, basepathlen, sizeonly, tablespaces,
1405 sendtblspclinks,
manifest, spcoid);
1412 sent =
sendFile(sink, pathbuf, pathbuf + basepathlen + 1, &statbuf,
1416 if (sent || sizeonly)
1430 (
errmsg(
"skipping special file \"%s\"", pathbuf)));
1446 if (strncmp(fullpath,
"./global/", 9) == 0 ||
1447 strncmp(fullpath,
"./base/", 7) == 0 ||
1448 strncmp(fullpath,
"/", 1) == 0)
1483 struct stat *statbuf,
bool missing_ok,
Oid dboid,
1488 bool block_retry =
false;
1490 int checksum_failures = 0;
1498 bool verify_checksum =
false;
1502 elog(
ERROR,
"could not initialize checksum of file \"%s\"",
1508 if (errno == ENOENT && missing_ok)
1512 errmsg(
"could not open file \"%s\": %m", readfilename)));
1530 verify_checksum =
true;
1536 segmentpath = strstr(
filename,
".");
1537 if (segmentpath != NULL)
1539 segmentno = atoi(segmentpath + 1);
1542 (
errmsg(
"invalid segment number %d in file \"%s\"",
1554 while (len < statbuf->
st_size)
1561 len, readfilename,
true);
1571 if (verify_checksum && (cnt % BLCKSZ != 0))
1574 (
errmsg(
"could not verify checksum in file \"%s\", block "
1575 "%u: read buffer size %d and page size %d "
1577 readfilename, blkno, (
int) cnt, BLCKSZ)));
1578 verify_checksum =
false;
1581 if (verify_checksum)
1583 for (
i = 0;
i < cnt / BLCKSZ;
i++)
1597 checksum =
pg_checksum_page((
char *) page, blkno + segmentno * RELSEG_SIZE);
1621 if (block_retry ==
false)
1629 BLCKSZ,
len + BLCKSZ *
i,
1632 if (reread_cnt == 0)
1654 checksum_failures++;
1656 if (checksum_failures <= 5)
1658 (
errmsg(
"checksum verification failed in "
1659 "file \"%s\", block %u: calculated "
1660 "%X but expected %X",
1661 readfilename, blkno, checksum,
1663 if (checksum_failures == 5)
1665 (
errmsg(
"further checksum verification "
1666 "failures in file \"%s\" will not "
1667 "be reported", readfilename)));
1670 block_retry =
false;
1689 elog(
ERROR,
"could not update checksum of base backup");
1695 while (len < statbuf->
st_size)
1704 elog(
ERROR,
"could not update checksum of base backup");
1718 if (checksum_failures > 1)
1721 (
errmsg_plural(
"file \"%s\" has a total of %d checksum verification failure",
1722 "file \"%s\" has a total of %d checksum verification failures",
1724 readfilename, checksum_failures)));
1739 struct stat *statbuf,
bool sizeonly)
1753 "BLCKSZ too small for tar block");
1767 (
errmsg(
"file name too long for tar format: \"%s\"",
1772 (
errmsg(
"symbolic link target too long for tar format: "
1773 "file name \"%s\", target \"%s\"",
1777 elog(
ERROR,
"unrecognized tar error: %d", rc);
1831 const char *
filename,
bool partial_read_ok)
1843 if (!partial_read_ok && rc > 0 && rc != nbytes)
1846 errmsg(
"could not read file \"%s\": read %d of %zu",
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)
void AddFileToBackupManifest(backup_manifest_info *manifest, const char *spcoid, const char *pathname, size_t size, pg_time_t mtime, pg_checksum_context *checksum_ctx)
@ MANIFEST_OPTION_FORCE_ENCODE
enum manifest_option backup_manifest_option
static const struct exclude_list_item excludeFiles[]
static const struct exclude_list_item noChecksumFiles[]
void SendBaseBackup(BaseBackupCmd *cmd)
static void sendFileWithContent(bbsink *sink, const char *filename, const char *content, backup_manifest_info *manifest)
static int compareWalFileNames(const ListCell *a, const ListCell *b)
#define SINK_BUFFER_LENGTH
static void parse_basebackup_options(List *options, basebackup_options *opt)
static const char *const excludeDirContents[]
static void convert_link_to_directory(const char *pathbuf, struct stat *statbuf)
static void perform_base_backup(basebackup_options *opt, bbsink *sink)
static bool is_checksummed_file(const char *fullpath, const char *filename)
static bool backup_started_in_recovery
static int64 _tarWriteHeader(bbsink *sink, const char *filename, const char *linktarget, struct stat *statbuf, bool sizeonly)
static bool sendFile(bbsink *sink, const char *readfilename, const char *tarfilename, struct stat *statbuf, bool missing_ok, Oid dboid, backup_manifest_info *manifest, const char *spcoid)
static int64 sendTablespace(bbsink *sink, char *path, char *spcoid, bool sizeonly, struct backup_manifest_info *manifest)
static int basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset, const char *filename, bool partial_read_ok)
static bool noverify_checksums
static int64 sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly, List *tablespaces, bool sendtblspclinks, backup_manifest_info *manifest, const char *spcoid)
static void _tarWritePadding(bbsink *sink, int len)
static long long int total_checksum_failures
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)
void basebackup_progress_wait_checkpoint(void)
void basebackup_progress_wait_wal_archive(bbsink_state *state)
bbsink * bbsink_progress_new(bbsink *next, bool estimate_backup_size)
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)
bbsink * BaseBackupGetSink(BaseBackupTargetHandle *handle, bbsink *next_sink)
BaseBackupTargetHandle * BaseBackupGetTargetHandle(char *target, char *target_detail)
bbsink * bbsink_throttle_new(bbsink *next, uint32 maxrate)
bbsink * bbsink_zstd_new(bbsink *next, pg_compress_specification *compress)
bool parse_bool(const char *value, bool *result)
PageHeaderData * PageHeader
static bool PageIsNew(Page page)
static XLogRecPtr PageGetLSN(Page page)
#define MemSet(start, val, len)
#define StaticAssertDecl(condition, errmessage)
#define StaticAssertStmt(condition, errmessage)
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)
bool parse_compress_algorithm(char *name, pg_compress_algorithm *algorithm)
void parse_compress_specification(pg_compress_algorithm algorithm, char *specification, pg_compress_specification *result)
char * validate_compress_specification(pg_compress_specification *spec)
elog(ERROR, "%s: %s", p2, msg)
bool defGetBoolean(DefElem *def)
char * defGetString(DefElem *def)
int64 defGetInt64(DefElem *def)
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
int errcode_for_file_access(void)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
struct dirent * ReadDir(DIR *dir, const char *dirname)
int CloseTransientFile(int fd)
bool looks_like_temp_rel_name(const char *name)
int OpenTransientFile(const char *fileName, int fileFlags)
DIR * AllocateDir(const char *dirname)
#define PG_TEMP_FILE_PREFIX
#define PG_AUTOCONF_FILENAME
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
Assert(fmt[strlen(fmt) - 1] !='\n')
void list_sort(List *list, list_sort_comparator cmp)
List * lappend(List *list, void *datum)
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
#define CHECK_FOR_INTERRUPTS()
#define ERRCODE_DATA_CORRUPTED
static ListCell * lnext(const List *l, const ListCell *c)
void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount)
static size_t tarPaddingBytesRequired(size_t len)
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)
char * last_dir_separator(const char *filename)
int pg_strcasecmp(const char *s1, const char *s2)
static Datum BoolGetDatum(bool X)
static int fd(const char *x, int i)
bool update_process_title
static void set_ps_display(const char *activity)
char * psprintf(const char *fmt,...)
bool parse_filename_for_nontemp_relation(const char *name, int *relnumchars, ForkNumber *fork)
#define RELCACHE_INIT_FILENAME
#define TABLESPACE_VERSION_DIRECTORY
ResourceOwner ResourceOwnerCreate(ResourceOwner parent, const char *name)
ResourceOwner CurrentResourceOwner
StringInfo makeStringInfo(void)
pg_compress_specification compression_specification
pg_checksum_type manifest_checksum_type
pg_compress_algorithm compression
backup_manifest_option manifest
BaseBackupTargetHandle * target_handle
pg_compress_algorithm algorithm
#define LOG_METAINFO_DATAFILE_TMP
static void pgstat_report_wait_start(uint32 wait_event_info)
static void pgstat_report_wait_end(void)
void WalSndSetState(WalSndState state)
void WalSndResourceCleanup(bool isCommit)
#define readlink(path, buf, size)
bool RecoveryInProgress(void)
void do_pg_abort_backup(int code, Datum arg)
SessionBackupState get_backup_status(void)
void CheckXLogRemoved(XLogSegNo segno, TimeLineID tli)
bool DataChecksumsEnabled(void)
void do_pg_backup_start(const char *backupidstr, bool fast, List **tablespaces, BackupState *state, StringInfo tblspcmapfile)
void do_pg_backup_stop(BackupState *state, bool waitforarchive)
#define BACKUP_LABEL_FILE
#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)
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)
static BackupState * backup_state
static StringInfo tablespace_map