32 #define BLOCKS_PER_READ 512
39 #define MIN_CHUNK 1024
40 #define MAX_CHUNK (128 * 1024)
63 #define SH_PREFIX backup_file
64 #define SH_ELEMENT_TYPE backup_file_entry
65 #define SH_KEY_TYPE const char *
67 #define SH_HASH_KEY(tb, key) hash_string_pointer(key)
68 #define SH_EQUAL(tb, a, b) (strcmp(a, b) == 0)
69 #define SH_SCOPE static inline
129 int manifest_version);
131 uint64 manifest_system_identifier);
133 const char *pathname,
137 uint8 *checksum_payload);
269 *required_wslist =
NIL;
274 bool found_backup_start_tli =
false;
289 if (num_wal_ranges == 0)
291 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
292 errmsg(
"manifest contains no required WAL ranges")));
315 for (
i = 0;
i < num_wal_ranges; ++
i)
318 bool saw_earliest_wal_range_tli =
false;
319 bool saw_latest_wal_range_tli =
false;
332 if (tle->
tli == earliest_wal_range_tli)
333 saw_earliest_wal_range_tli =
true;
334 if (tle->
tli == latest_wal_range_tli)
335 saw_latest_wal_range_tli =
true;
346 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
347 errmsg(
"timeline %u found in manifest, but not in this server's history",
363 if (!saw_latest_wal_range_tli)
364 latest_wal_range_tli =
range->tli;
365 if (earliest_wal_range_tli == 0 || saw_earliest_wal_range_tli)
367 earliest_wal_range_tli =
range->tli;
368 earliest_wal_range_start_lsn =
range->start_lsn;
403 for (
i = 0;
i < num_wal_ranges; ++
i)
407 if (
range->tli == earliest_wal_range_tli)
411 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
412 errmsg(
"manifest requires WAL from initial timeline %u starting at %X/%X, but that timeline begins at %X/%X",
421 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
422 errmsg(
"manifest requires WAL from continuation timeline %u starting at %X/%X, but that timeline begins at %X/%X",
428 if (
range->tli == latest_wal_range_tli)
432 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
433 errmsg(
"manifest requires WAL from final timeline %u ending at %X/%X, but this backup starts at %X/%X",
437 errhint(
"This can happen for incremental backups on a standby if there was little activity since the previous backup.")));
443 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
444 errmsg(
"manifest requires WAL from non-final timeline %u ending at %X/%X, but this server switched timelines at %X/%X",
493 found_backup_start_tli =
true;
496 else if (!found_backup_start_tli)
508 if (tle->
tli == earliest_wal_range_tli)
509 tli_start_lsn = earliest_wal_range_start_lsn;
511 tli_start_lsn, tli_end_lsn);
524 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
525 errmsg(
"WAL summaries are required on timeline %u from %X/%X to %X/%X, but no summaries for that timeline and LSN range exist",
531 (
errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
532 errmsg(
"WAL summaries are required on timeline %u from %X/%X to %X/%X, but the summaries for that timeline and LSN range are incomplete",
536 errdetail(
"The first unsummarized LSN in this range is %X/%X.",
554 required_wslist =
list_concat(required_wslist, tli_wslist);
560 if (tle->
tli == earliest_wal_range_tli)
572 foreach(lc, required_wslist)
594 forknum, limit_block);
606 for (
i = 0;
i < nblocks; ++
i)
635 lastslash = strrchr(path,
'/');
636 Assert(lastslash != NULL);
640 ipath =
psprintf(
"%s/INCREMENTAL.%s.%u", path, lastslash + 1, segno);
642 ipath =
psprintf(
"%s/INCREMENTAL.%s", path, lastslash + 1);
668 unsigned segno,
size_t size,
669 unsigned *num_blocks_required,
671 unsigned *truncation_block_length)
695 if ((
size % BLCKSZ) != 0 ||
size / BLCKSZ > RELSEG_SIZE)
742 rlocator.
dbOid = dboid;
745 &limit_block) != NULL)
770 if (brtentry == NULL)
774 *num_blocks_required = 0;
775 *truncation_block_length =
size / BLCKSZ;
783 if (limit_block <= segno * RELSEG_SIZE)
792 start_blkno = segno * RELSEG_SIZE;
793 stop_blkno = start_blkno + (
size / BLCKSZ);
794 if (start_blkno / RELSEG_SIZE != segno || stop_blkno < start_blkno)
796 errcode(ERRCODE_INTERNAL_ERROR),
797 errmsg_internal(
"overflow computing block number bounds for segment %u with size %zu",
805 relative_block_numbers, RELSEG_SIZE);
806 Assert(nblocks <= RELSEG_SIZE);
820 if (nblocks * BLCKSZ >
size * 0.9)
835 if (start_blkno != 0)
837 for (
i = 0;
i < nblocks; ++
i)
838 relative_block_numbers[
i] -= start_blkno;
840 *num_blocks_required = nblocks;
850 *truncation_block_length =
size / BLCKSZ;
853 unsigned relative_limit = limit_block - segno * RELSEG_SIZE;
855 if (*truncation_block_length < relative_limit)
856 *truncation_block_length = relative_limit;
874 Assert(num_blocks_required <= RELSEG_SIZE);
887 if ((num_blocks_required > 0) && (result % BLCKSZ != 0))
888 result += BLCKSZ - (result % BLCKSZ);
902 Assert(num_blocks_required <= RELSEG_SIZE);
910 result += BLCKSZ * num_blocks_required;
921 unsigned char *ss = (
unsigned char *) s;
931 int manifest_version)
934 if (manifest_version == 1)
936 "backup manifest version 1 does not support incremental backup");
945 uint64 manifest_system_identifier)
952 if (manifest_system_identifier != system_identifier)
954 "system identifier in backup manifest is %llu, but database system identifier is %llu",
955 (
unsigned long long) manifest_system_identifier,
956 (
unsigned long long) system_identifier);
970 uint8 *checksum_payload)
1001 range->start_lsn = start_lsn;
1002 range->end_lsn = end_lsn;
List * readTimeLineHistory(TimeLineID targetTLI)
void AppendIncrementalManifestData(IncrementalBackupInfo *ib, const char *data, int len)
static void manifest_process_version(JsonManifestParseContext *context, int manifest_version)
static int compare_block_numbers(const void *a, const void *b)
static uint32 hash_string_pointer(const char *s)
size_t GetIncrementalHeaderSize(unsigned num_blocks_required)
char * GetIncrementalFilePath(Oid dboid, Oid spcoid, RelFileNumber relfilenumber, ForkNumber forknum, unsigned segno)
static void manifest_process_system_identifier(JsonManifestParseContext *context, uint64 manifest_system_identifier)
size_t GetIncrementalFileSize(unsigned num_blocks_required)
static void manifest_process_file(JsonManifestParseContext *context, const char *pathname, uint64 size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
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)
static void pg_attribute_noreturn()
static void manifest_report_error(JsonManifestParseContext *context, const char *fmt,...) pg_attribute_printf(2
static void manifest_process_wal_range(JsonManifestParseContext *context, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
IncrementalBackupInfo * CreateIncrementalBackupInfo(MemoryContext mcxt)
void PrepareForIncrementalBackup(IncrementalBackupInfo *ib, BackupState *backup_state)
void FinalizeIncrementalManifest(IncrementalBackupInfo *ib)
@ BACK_UP_FILE_INCREMENTALLY
BlockRefTableReader * CreateBlockRefTableReader(io_callback_fn read_callback, void *read_callback_arg, char *error_filename, report_error_fn error_callback, void *error_callback_arg)
bool BlockRefTableReaderNextRelation(BlockRefTableReader *reader, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *limit_block)
int BlockRefTableEntryGetBlocks(BlockRefTableEntry *entry, BlockNumber start_blkno, BlockNumber stop_blkno, BlockNumber *blocks, int nblocks)
void BlockRefTableMarkBlockModified(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum)
BlockRefTableEntry * BlockRefTableGetEntry(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber *limit_block)
unsigned BlockRefTableReaderGetBlocks(BlockRefTableReader *reader, BlockNumber *blocks, int nblocks)
void BlockRefTableSetLimitBlock(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber limit_block)
void DestroyBlockRefTableReader(BlockRefTableReader *reader)
void(*) BlockRefTable CreateEmptyBlockRefTable)(void)
static bool BlockNumberIsValid(BlockNumber blockNumber)
#define Assert(condition)
#define pg_attribute_printf(f, a)
#define OidIsValid(objectId)
int errmsg_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
void FileClose(File file)
char * FilePathName(File file)
uint32 hash_bytes(const unsigned char *k, int keylen)
static int pg_cmp_u32(uint32 a, uint32 b)
static void const char * fmt
List * lappend(List *list, void *datum)
List * list_concat(List *list1, const List *list2)
void pfree(void *pointer)
void * palloc0(Size size)
char * MemoryContextStrdup(MemoryContext context, const char *string)
JsonManifestParseIncrementalState * json_parse_manifest_incremental_init(JsonManifestParseContext *context)
void json_parse_manifest_incremental_shutdown(JsonManifestParseIncrementalState *incstate)
void json_parse_manifest_incremental_chunk(JsonManifestParseIncrementalState *incstate, const char *chunk, size_t size, bool is_last)
static int list_length(const List *l)
static void * list_nth(const List *list, int n)
#define qsort(a, b, c, d)
#define INVALID_PROC_NUMBER
char * psprintf(const char *fmt,...)
MemoryContextSwitchTo(old_ctx)
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
char * GetRelationPath(Oid dbOid, Oid spcOid, RelFileNumber relNumber, int procNumber, ForkNumber forkNumber)
#define RelFileNumberIsValid(relnumber)
static pg_noinline void Size size
int appendStringInfoVA(StringInfo str, const char *fmt, va_list args)
void enlargeStringInfo(StringInfo str, int needed)
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
void initStringInfo(StringInfo str)
List * manifest_wal_ranges
backup_file_hash * manifest_files
JsonManifestParseIncrementalState * inc_state
void WaitForWalSummarization(XLogRecPtr lsn)
File OpenWalSummaryFile(WalSummaryFile *ws, bool missing_ok)
List * GetWalSummaries(TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
bool WalSummariesAreComplete(List *wslist, XLogRecPtr start_lsn, XLogRecPtr end_lsn, XLogRecPtr *missing_lsn)
int ReadWalSummary(void *wal_summary_io, void *data, int length)
List * FilterWalSummaries(List *wslist, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
void ReportWalSummaryError(void *callback_arg, char *fmt,...)
uint64 GetSystemIdentifier(void)
#define LSN_FORMAT_ARGS(lsn)
#define XLogRecPtrIsInvalid(r)
#define InvalidXLogRecPtr
static BackupState * backup_state
static List * expectedTLEs