26#include "catalog/pg_tablespace_d.h"
61#define SINK_BUFFER_LENGTH Max(32768, BLCKSZ)
82#define TAR_NUM_TERMINATION_BLOCKS 2
101 unsigned truncation_block_length);
104 off_t offset,
size_t length,
106 bool verify_checksum,
107 int *checksum_failures);
109 size_t *bytes_done,
void *
data,
size_t length);
114 const char *content,
int len,
224 {
"backup_manifest",
false},
226 {
"postmaster.pid",
false},
227 {
"postmaster.opts",
false},
252 state.tablespace_num = 0;
253 state.bytes_done = 0;
254 state.bytes_total = 0;
255 state.bytes_total_is_valid =
false;
322 state.bytes_total_is_valid =
true;
363 errmsg(
"could not stat file \"%s\": %m",
371 char *archive_name =
psprintf(
"%u.tar",
ti->oid);
485 (
errmsg(
"could not find any WAL files")));
559 errmsg(
"could not stat file \"%s\": %m",
661 "%lld total checksum verification failures",
667 errmsg(
"checksum verification failure during base backup")));
722 MemSet(opt, 0,
sizeof(*opt));
737 errmsg(
"duplicate option \"%s\"",
defel->defname)));
746 errmsg(
"duplicate option \"%s\"",
defel->defname)));
750 else if (
strcmp(
defel->defname,
"checkpoint") == 0)
757 errmsg(
"duplicate option \"%s\"",
defel->defname)));
765 errmsg(
"unrecognized checkpoint type: \"%s\"",
774 errmsg(
"duplicate option \"%s\"",
defel->defname)));
783 errmsg(
"duplicate option \"%s\"",
defel->defname)));
787 else if (
strcmp(
defel->defname,
"incremental") == 0)
792 errmsg(
"duplicate option \"%s\"",
defel->defname)));
797 errmsg(
"incremental backups cannot be taken unless WAL summarization is enabled")));
807 errmsg(
"duplicate option \"%s\"",
defel->defname)));
813 errmsg(
"%" PRId64 " is outside the valid range for parameter \"%s\" (%d .. %d)",
819 else if (
strcmp(
defel->defname,
"tablespace_map") == 0)
824 errmsg(
"duplicate option \"%s\"",
defel->defname)));
828 else if (
strcmp(
defel->defname,
"verify_checksums") == 0)
833 errmsg(
"duplicate option \"%s\"",
defel->defname)));
845 errmsg(
"duplicate option \"%s\"",
defel->defname)));
858 errmsg(
"unrecognized manifest option: \"%s\"",
862 else if (
strcmp(
defel->defname,
"manifest_checksums") == 0)
869 errmsg(
"duplicate option \"%s\"",
defel->defname)));
874 errmsg(
"unrecognized checksum algorithm: \"%s\"",
883 errmsg(
"duplicate option \"%s\"",
defel->defname)));
887 else if (
strcmp(
defel->defname,
"target_detail") == 0)
894 errmsg(
"duplicate option \"%s\"",
defel->defname)));
898 else if (
strcmp(
defel->defname,
"compression") == 0)
905 errmsg(
"duplicate option \"%s\"",
defel->defname)));
909 errmsg(
"unrecognized compression algorithm: \"%s\"",
913 else if (
strcmp(
defel->defname,
"compression_detail") == 0)
918 errmsg(
"duplicate option \"%s\"",
defel->defname)));
925 errmsg(
"unrecognized base backup option: \"%s\"",
930 opt->
label =
"base backup";
936 errmsg(
"manifest checksums require a backup manifest")));
945 errmsg(
"target detail cannot be used without target")));
954 errmsg(
"target \"%s\" does not accept a target detail",
965 errmsg(
"compression detail cannot be specified unless compression is enabled")));
978 errmsg(
"invalid compression specification: %s",
1001 errmsg(
"a backup is already in progress in this session")));
1028 errmsg(
"must UPLOAD_MANIFEST before performing an incremental BASE_BACKUP")));
1085 elog(
ERROR,
"could not initialize checksum of file \"%s\"",
1110 elog(
ERROR,
"could not update checksum of file \"%s\"",
1113 while (bytes_done <
len)
1120 bytes_done += nbytes;
1161 errmsg(
"could not stat file or directory \"%s\": %m",
1243 else if (
strcmp(path,
"./global") == 0)
1270 if (
strcmp(
de->d_name,
".DS_Store") == 0)
1285 errmsg(
"the standby was promoted during online backup"),
1286 errhint(
"This means that the backup being taken is corrupt "
1287 "and should not be used. "
1288 "Try taking another online backup.")));
1300 elog(
DEBUG1,
"file \"%s\" excluded from backup",
de->d_name);
1329 path, relfilenumber);
1334 "unlogged relation file \"%s\" excluded from backup",
1345 "temporary relation file \"%s\" excluded from backup",
1362 errmsg(
"could not stat file or directory \"%s\": %m",
1375 elog(
DEBUG1,
"contents of directory \"%s\" excluded from backup",
de->d_name);
1421 errmsg(
"could not read symbolic link \"%s\": %m",
1426 errmsg(
"symbolic link \"%s\" target is too long",
1449 foreach(
lc, tablespaces)
1481 unsigned truncation_block_length = 0;
1510 relative_block_numbers,
1511 &truncation_block_length);
1517 "%s/INCREMENTAL.%s",
1532 truncation_block_length);
1551 if (relative_block_numbers !=
NULL)
1552 pfree(relative_block_numbers);
1584 int checksum_failures = 0;
1587 bool verify_checksum =
false;
1592 elog(
ERROR,
"could not initialize checksum of file \"%s\"",
1620 verify_checksum =
true;
1634 &magic,
sizeof(magic));
1638 &truncation_block_length,
sizeof(truncation_block_length));
1668 elog(
ERROR,
"could not update checksum of base backup");
1672 bytes_done +=
sizeof(magic);
1674 bytes_done +=
sizeof(truncation_block_length);
1698 if (bytes_done >=
statbuf->st_size)
1709 &checksum_failures);
1731 &checksum_failures);
1752 if (verify_checksum && (cnt %
BLCKSZ != 0))
1755 (
errmsg(
"could not verify checksum in file \"%s\", block "
1756 "%u: read buffer size %d and page size %d "
1759 verify_checksum =
false;
1779 (bytes_done %
BLCKSZ != 0)));
1787 elog(
ERROR,
"could not update checksum of base backup");
1800 elog(
ERROR,
"could not update checksum of base backup");
1802 bytes_done += nbytes;
1814 if (checksum_failures > 1)
1817 (
errmsg_plural(
"file \"%s\" has a total of %d checksum verification failure",
1818 "file \"%s\" has a total of %d checksum verification failures",
1854 bool verify_checksum,
int *checksum_failures)
1862 Min(
sink->bbs_buffer_length, length),
1866 if (!verify_checksum || (cnt %
BLCKSZ) != 0)
1919 (*checksum_failures)++;
1920 if (*checksum_failures <= 5)
1922 (
errmsg(
"checksum verification failed in "
1923 "file \"%s\", block %u: calculated "
1924 "%X but expected %X",
1927 if (*checksum_failures == 5)
1929 (
errmsg(
"further checksum verification "
1930 "failures in file \"%s\" will not "
1956 size_t *bytes_done,
void *
data,
size_t length)
1970 *bytes_done += length;
1981 sink->bbs_buffer_length) < 0)
1982 elog(
ERROR,
"could not update checksum");
2039 "BLCKSZ too small for tar block");
2054 errmsg(
"file name too long for tar format: \"%s\"",
2060 errmsg(
"symbolic link target too long for tar format: "
2061 "file name \"%s\", target \"%s\"",
2065 elog(
ERROR,
"unrecognized tar error: %d", rc);
2134 errmsg(
"could not read file \"%s\": read %zd of %zu",
void InitializeBackupManifest(backup_manifest_info *manifest, backup_manifest_option want_manifest, pg_checksum_type manifest_checksum_type)
void AddFileToBackupManifest(backup_manifest_info *manifest, Oid spcoid, const char *pathname, size_t size, pg_time_t mtime, pg_checksum_context *checksum_ctx)
void AddWALInfoToBackupManifest(backup_manifest_info *manifest, XLogRecPtr startptr, TimeLineID starttli, XLogRecPtr endptr, TimeLineID endtli)
void SendBackupManifest(backup_manifest_info *manifest, bbsink *sink)
void FreeBackupManifest(backup_manifest_info *manifest)
@ MANIFEST_OPTION_FORCE_ENCODE
enum manifest_option backup_manifest_option
static bool sendFile(bbsink *sink, const char *readfilename, const char *tarfilename, struct stat *statbuf, bool missing_ok, Oid dboid, Oid spcoid, RelFileNumber relfilenumber, unsigned segno, backup_manifest_info *manifest, unsigned num_incremental_blocks, BlockNumber *incremental_blocks, unsigned truncation_block_length)
static const struct exclude_list_item excludeFiles[]
static int compareWalFileNames(const ListCell *a, const ListCell *b)
static void sendFileWithContent(bbsink *sink, const char *filename, const char *content, int len, backup_manifest_info *manifest)
static off_t read_file_data_into_buffer(bbsink *sink, const char *readfilename, int fd, off_t offset, size_t length, BlockNumber blkno, bool verify_checksum, int *checksum_failures)
static void push_to_sink(bbsink *sink, pg_checksum_context *checksum_ctx, size_t *bytes_done, void *data, size_t length)
static int64 sendDir(bbsink *sink, const char *path, int basepathlen, bool sizeonly, List *tablespaces, bool sendtblspclinks, backup_manifest_info *manifest, Oid spcoid, IncrementalBackupInfo *ib)
#define SINK_BUFFER_LENGTH
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)
void SendBaseBackup(BaseBackupCmd *cmd, IncrementalBackupInfo *ib)
static bool backup_started_in_recovery
static int64 _tarWriteHeader(bbsink *sink, const char *filename, const char *linktarget, struct stat *statbuf, bool sizeonly)
static void perform_base_backup(basebackup_options *opt, bbsink *sink, IncrementalBackupInfo *ib)
static int64 sendTablespace(bbsink *sink, char *path, Oid spcoid, bool sizeonly, struct backup_manifest_info *manifest, IncrementalBackupInfo *ib)
static bool noverify_checksums
static void _tarWritePadding(bbsink *sink, int len)
static bool verify_page_checksum(Page page, XLogRecPtr start_lsn, BlockNumber blkno, uint16 *expected_checksum)
static ssize_t basebackup_read_file(int fd, char *buf, size_t nbytes, off_t offset, const char *filename, bool partial_read_ok)
#define TAR_NUM_TERMINATION_BLOCKS
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)
size_t GetIncrementalFileSize(unsigned num_blocks_required)
FileBackupMethod GetFileBackupMethod(IncrementalBackupInfo *ib, const char *path, Oid dboid, Oid spcoid, RelFileNumber relfilenumber, ForkNumber forknum, unsigned segno, size_t size, unsigned *num_blocks_required, BlockNumber *relative_block_numbers, unsigned *truncation_block_length)
void PrepareForIncrementalBackup(IncrementalBackupInfo *ib, BackupState *backup_state)
#define INCREMENTAL_MAGIC
@ BACK_UP_FILE_INCREMENTALLY
bbsink * bbsink_lz4_new(bbsink *next, pg_compress_specification *compress)
void basebackup_progress_wait_checkpoint(void)
bbsink * bbsink_progress_new(bbsink *next, bool estimate_backup_size, bool incremental)
void basebackup_progress_wait_wal_archive(bbsink_state *state)
void basebackup_progress_done(void)
void basebackup_progress_transfer_wal(void)
void basebackup_progress_estimate_backup_size(void)
static void bbsink_begin_backup(bbsink *sink, bbsink_state *state, int buffer_length)
static void bbsink_begin_archive(bbsink *sink, const char *archive_name)
static void bbsink_end_archive(bbsink *sink)
static void bbsink_end_backup(bbsink *sink, XLogRecPtr endptr, TimeLineID endtli)
static void bbsink_cleanup(bbsink *sink)
static void bbsink_archive_contents(bbsink *sink, size_t len)
BaseBackupTargetHandle * BaseBackupGetTargetHandle(char *target, char *target_detail)
bbsink * BaseBackupGetSink(BaseBackupTargetHandle *handle, bbsink *next_sink)
bbsink * bbsink_throttle_new(bbsink *next, uint32 maxrate)
bbsink * bbsink_zstd_new(bbsink *next, pg_compress_specification *compress)
bool parse_bool(const char *value, bool *result)
PageHeaderData * PageHeader
static bool PageIsNew(const PageData *page)
static XLogRecPtr PageGetLSN(const PageData *page)
#define Assert(condition)
#define MemSet(start, val, len)
#define StaticAssertDecl(condition, errmessage)
#define OidIsValid(objectId)
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)
char * validate_compress_specification(pg_compress_specification *spec)
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 * defGetString(DefElem *def)
bool defGetBoolean(DefElem *def)
int64 defGetInt64(DefElem *def)
int errcode_for_file_access(void)
int errcode(int sqlerrcode)
int errhint(const char *fmt,...) pg_attribute_printf(1
int int int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...) pg_attribute_printf(1
#define ereport(elevel,...)
int CloseTransientFile(int fd)
DIR * AllocateDir(const char *dirname)
struct dirent * ReadDir(DIR *dir, const char *dirname)
bool looks_like_temp_rel_name(const char *name)
int OpenTransientFile(const char *fileName, int fileFlags)
#define palloc_array(type, count)
#define palloc0_object(type)
#define PG_TEMP_FILE_PREFIX
#define PG_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
#define PG_END_ENSURE_ERROR_CLEANUP(cleanup_function, arg)
List * lappend(List *list, void *datum)
void list_sort(List *list, list_sort_comparator cmp)
char * pstrdup(const char *in)
void pfree(void *pointer)
#define CHECK_FOR_INTERRUPTS()
#define ERRCODE_DATA_CORRUPTED
#define PG_AUTOCONF_FILENAME
static ListCell * lnext(const List *l, const ListCell *c)
static char buf[DEFAULT_XLOG_SEG_SIZE]
void pgstat_prepare_report_checksum_failure(Oid dboid)
void pgstat_report_checksum_failures_in_db(Oid dboid, int failurecount)
static size_t tarPaddingBytesRequired(size_t len)
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, RelFileNumber *relnumber, ForkNumber *fork, unsigned *segno)
#define RELCACHE_INIT_FILENAME
#define InvalidRelFileNumber
#define TABLESPACE_VERSION_DIRECTORY
#define RelFileNumberIsValid(relnumber)
void ReleaseAuxProcessResources(bool isCommit)
ResourceOwner CurrentResourceOwner
ResourceOwner AuxProcessResourceOwner
void initStringInfo(StringInfo str)
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)
#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