PostgreSQL Source Code  git master
basebackup_incremental.h File Reference
#include "access/xlogbackup.h"
#include "common/relpath.h"
#include "storage/block.h"
#include "utils/palloc.h"
Include dependency graph for basebackup_incremental.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define INCREMENTAL_MAGIC   0xd3ae1f0d
 

Typedefs

typedef struct IncrementalBackupInfo IncrementalBackupInfo
 

Enumerations

enum  FileBackupMethod { BACK_UP_FILE_FULLY , BACK_UP_FILE_INCREMENTALLY }
 

Functions

IncrementalBackupInfoCreateIncrementalBackupInfo (MemoryContext)
 
void AppendIncrementalManifestData (IncrementalBackupInfo *ib, const char *data, int len)
 
void FinalizeIncrementalManifest (IncrementalBackupInfo *ib)
 
void PrepareForIncrementalBackup (IncrementalBackupInfo *ib, BackupState *backup_state)
 
char * GetIncrementalFilePath (Oid dboid, Oid spcoid, RelFileNumber relfilenumber, ForkNumber forknum, unsigned segno)
 
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)
 
size_t GetIncrementalFileSize (unsigned num_blocks_required)
 
size_t GetIncrementalHeaderSize (unsigned num_blocks_required)
 

Macro Definition Documentation

◆ INCREMENTAL_MAGIC

#define INCREMENTAL_MAGIC   0xd3ae1f0d

Definition at line 20 of file basebackup_incremental.h.

Typedef Documentation

◆ IncrementalBackupInfo

Definition at line 1 of file basebackup_incremental.h.

Enumeration Type Documentation

◆ FileBackupMethod

Enumerator
BACK_UP_FILE_FULLY 
BACK_UP_FILE_INCREMENTALLY 

Definition at line 22 of file basebackup_incremental.h.

Function Documentation

◆ AppendIncrementalManifestData()

void AppendIncrementalManifestData ( IncrementalBackupInfo ib,
const char *  data,
int  len 
)

Definition at line 196 of file basebackup_incremental.c.

198 {
199  MemoryContext oldcontext;
200 
201  /* Switch to our memory context. */
202  oldcontext = MemoryContextSwitchTo(ib->mcxt);
203 
204  if (ib->buf.len > MIN_CHUNK && ib->buf.len + len > MAX_CHUNK)
205  {
206  /*
207  * time for an incremental parse. We'll do all but the last MIN_CHUNK
208  * so that we have enough left for the final piece.
209  */
211  ib->inc_state, ib->buf.data, ib->buf.len - MIN_CHUNK, false);
212  /* now remove what we just parsed */
213  memmove(ib->buf.data, ib->buf.data + (ib->buf.len - MIN_CHUNK),
214  MIN_CHUNK + 1);
215  ib->buf.len = MIN_CHUNK;
216  }
217 
219 
220  /* Switch back to previous memory context. */
221  MemoryContextSwitchTo(oldcontext);
222 }
#define MAX_CHUNK
#define MIN_CHUNK
void json_parse_manifest_incremental_chunk(JsonManifestParseIncrementalState *incstate, const char *chunk, size_t size, bool is_last)
const void size_t len
const void * data
MemoryContextSwitchTo(old_ctx)
void appendBinaryStringInfo(StringInfo str, const void *data, int datalen)
Definition: stringinfo.c:233
JsonManifestParseIncrementalState * inc_state

References appendBinaryStringInfo(), IncrementalBackupInfo::buf, StringInfoData::data, data, IncrementalBackupInfo::inc_state, json_parse_manifest_incremental_chunk(), StringInfoData::len, len, MAX_CHUNK, IncrementalBackupInfo::mcxt, MemoryContextSwitchTo(), and MIN_CHUNK.

Referenced by HandleUploadManifestPacket().

◆ CreateIncrementalBackupInfo()

IncrementalBackupInfo* CreateIncrementalBackupInfo ( MemoryContext  mcxt)

Definition at line 154 of file basebackup_incremental.c.

155 {
157  MemoryContext oldcontext;
159 
160  oldcontext = MemoryContextSwitchTo(mcxt);
161 
162  ib = palloc0(sizeof(IncrementalBackupInfo));
163  ib->mcxt = mcxt;
164  initStringInfo(&ib->buf);
165 
166  /*
167  * It's hard to guess how many files a "typical" installation will have in
168  * the data directory, but a fresh initdb creates almost 1000 files as of
169  * this writing, so it seems to make sense for our estimate to
170  * substantially higher.
171  */
172  ib->manifest_files = backup_file_create(mcxt, 10000, NULL);
173 
175  /* Parse the manifest. */
176  context->private_data = ib;
177  context->version_cb = manifest_process_version;
178  context->system_identifier_cb = manifest_process_system_identifier;
179  context->per_file_cb = manifest_process_file;
180  context->per_wal_range_cb = manifest_process_wal_range;
181  context->error_cb = manifest_report_error;
182 
184 
185  MemoryContextSwitchTo(oldcontext);
186 
187  return ib;
188 }
static void manifest_process_version(JsonManifestParseContext *context, int manifest_version)
static void manifest_process_system_identifier(JsonManifestParseContext *context, uint64 manifest_system_identifier)
static void manifest_process_file(JsonManifestParseContext *context, const char *pathname, size_t size, pg_checksum_type checksum_type, int checksum_length, uint8 *checksum_payload)
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)
void * palloc0(Size size)
Definition: mcxt.c:1347
JsonManifestParseIncrementalState * json_parse_manifest_incremental_init(JsonManifestParseContext *context)
tree context
Definition: radixtree.h:1835
void initStringInfo(StringInfo str)
Definition: stringinfo.c:59
backup_file_hash * manifest_files

References IncrementalBackupInfo::buf, context, IncrementalBackupInfo::inc_state, initStringInfo(), json_parse_manifest_incremental_init(), IncrementalBackupInfo::manifest_files, manifest_process_file(), manifest_process_system_identifier(), manifest_process_version(), manifest_process_wal_range(), manifest_report_error(), IncrementalBackupInfo::mcxt, MemoryContextSwitchTo(), and palloc0().

Referenced by UploadManifest().

◆ FinalizeIncrementalManifest()

void FinalizeIncrementalManifest ( IncrementalBackupInfo ib)

Definition at line 229 of file basebackup_incremental.c.

230 {
231  MemoryContext oldcontext;
232 
233  /* Switch to our memory context. */
234  oldcontext = MemoryContextSwitchTo(ib->mcxt);
235 
236  /* Parse the last chunk of the manifest */
238  ib->inc_state, ib->buf.data, ib->buf.len, true);
239 
240  /* Done with the buffer, so release memory. */
241  pfree(ib->buf.data);
242  ib->buf.data = NULL;
243 
244  /* Done with inc_state, so release that memory too */
246 
247  /* Switch back to previous memory context. */
248  MemoryContextSwitchTo(oldcontext);
249 }
void pfree(void *pointer)
Definition: mcxt.c:1521
void json_parse_manifest_incremental_shutdown(JsonManifestParseIncrementalState *incstate)

References IncrementalBackupInfo::buf, StringInfoData::data, IncrementalBackupInfo::inc_state, json_parse_manifest_incremental_chunk(), json_parse_manifest_incremental_shutdown(), StringInfoData::len, IncrementalBackupInfo::mcxt, MemoryContextSwitchTo(), and pfree().

Referenced by UploadManifest().

◆ GetFileBackupMethod()

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 
)

Definition at line 667 of file basebackup_incremental.c.

674 {
675  BlockNumber limit_block;
676  BlockNumber start_blkno;
677  BlockNumber stop_blkno;
678  RelFileLocator rlocator;
679  BlockRefTableEntry *brtentry;
680  unsigned i;
681  unsigned nblocks;
682 
683  /* Should only be called after PrepareForIncrementalBackup. */
684  Assert(ib->buf.data == NULL);
685 
686  /*
687  * dboid could be InvalidOid if shared rel, but spcoid and relfilenumber
688  * should have legal values.
689  */
690  Assert(OidIsValid(spcoid));
691  Assert(RelFileNumberIsValid(relfilenumber));
692 
693  /*
694  * If the file size is too large or not a multiple of BLCKSZ, then
695  * something weird is happening, so give up and send the whole file.
696  */
697  if ((size % BLCKSZ) != 0 || size / BLCKSZ > RELSEG_SIZE)
698  return BACK_UP_FILE_FULLY;
699 
700  /*
701  * The free-space map fork is not properly WAL-logged, so we need to
702  * backup the entire file every time.
703  */
704  if (forknum == FSM_FORKNUM)
705  return BACK_UP_FILE_FULLY;
706 
707  /*
708  * If this file was not part of the prior backup, back it up fully.
709  *
710  * If this file was created after the prior backup and before the start of
711  * the current backup, then the WAL summary information will tell us to
712  * back up the whole file. However, if this file was created after the
713  * start of the current backup, then the WAL summary won't know anything
714  * about it. Without this logic, we would erroneously conclude that it was
715  * OK to send it incrementally.
716  *
717  * Note that the file could have existed at the time of the prior backup,
718  * gotten deleted, and then a new file with the same name could have been
719  * created. In that case, this logic won't prevent the file from being
720  * backed up incrementally. But, if the deletion happened before the start
721  * of the current backup, the limit block will be 0, inducing a full
722  * backup. If the deletion happened after the start of the current backup,
723  * reconstruction will erroneously combine blocks from the current
724  * lifespan of the file with blocks from the previous lifespan -- but in
725  * this type of case, WAL replay to reach backup consistency should remove
726  * and recreate the file anyway, so the initial bogus contents should not
727  * matter.
728  */
729  if (backup_file_lookup(ib->manifest_files, path) == NULL)
730  {
731  char *ipath;
732 
733  ipath = GetIncrementalFilePath(dboid, spcoid, relfilenumber,
734  forknum, segno);
735  if (backup_file_lookup(ib->manifest_files, ipath) == NULL)
736  return BACK_UP_FILE_FULLY;
737  }
738 
739  /*
740  * Look up the special block reference table entry for the database as a
741  * whole.
742  */
743  rlocator.spcOid = spcoid;
744  rlocator.dbOid = dboid;
745  rlocator.relNumber = 0;
746  if (BlockRefTableGetEntry(ib->brtab, &rlocator, MAIN_FORKNUM,
747  &limit_block) != NULL)
748  {
749  /*
750  * According to the WAL summary, this database OID/tablespace OID
751  * pairing has been created since the previous backup. So, everything
752  * in it must be backed up fully.
753  */
754  return BACK_UP_FILE_FULLY;
755  }
756 
757  /* Look up the block reference table entry for this relfilenode. */
758  rlocator.relNumber = relfilenumber;
759  brtentry = BlockRefTableGetEntry(ib->brtab, &rlocator, forknum,
760  &limit_block);
761 
762  /*
763  * If there is no entry, then there have been no WAL-logged changes to the
764  * relation since the predecessor backup was taken, so we can back it up
765  * incrementally and need not include any modified blocks.
766  *
767  * However, if the file is zero-length, we should do a full backup,
768  * because an incremental file is always more than zero length, and it's
769  * silly to take an incremental backup when a full backup would be
770  * smaller.
771  */
772  if (brtentry == NULL)
773  {
774  if (size == 0)
775  return BACK_UP_FILE_FULLY;
776  *num_blocks_required = 0;
777  *truncation_block_length = size / BLCKSZ;
779  }
780 
781  /*
782  * If the limit_block is less than or equal to the point where this
783  * segment starts, send the whole file.
784  */
785  if (limit_block <= segno * RELSEG_SIZE)
786  return BACK_UP_FILE_FULLY;
787 
788  /*
789  * Get relevant entries from the block reference table entry.
790  *
791  * We shouldn't overflow computing the start or stop block numbers, but if
792  * it manages to happen somehow, detect it and throw an error.
793  */
794  start_blkno = segno * RELSEG_SIZE;
795  stop_blkno = start_blkno + (size / BLCKSZ);
796  if (start_blkno / RELSEG_SIZE != segno || stop_blkno < start_blkno)
797  ereport(ERROR,
798  errcode(ERRCODE_INTERNAL_ERROR),
799  errmsg_internal("overflow computing block number bounds for segment %u with size %zu",
800  segno, size));
801 
802  /*
803  * This will write *absolute* block numbers into the output array, but
804  * we'll transpose them below.
805  */
806  nblocks = BlockRefTableEntryGetBlocks(brtentry, start_blkno, stop_blkno,
807  relative_block_numbers, RELSEG_SIZE);
808  Assert(nblocks <= RELSEG_SIZE);
809 
810  /*
811  * If we're going to have to send nearly all of the blocks, then just send
812  * the whole file, because that won't require much extra storage or
813  * transfer and will speed up and simplify backup restoration. It's not
814  * clear what threshold is most appropriate here and perhaps it ought to
815  * be configurable, but for now we're just going to say that if we'd need
816  * to send 90% of the blocks anyway, give up and send the whole file.
817  *
818  * NB: If you change the threshold here, at least make sure to back up the
819  * file fully when every single block must be sent, because there's
820  * nothing good about sending an incremental file in that case.
821  */
822  if (nblocks * BLCKSZ > size * 0.9)
823  return BACK_UP_FILE_FULLY;
824 
825  /*
826  * Looks like we can send an incremental file, so sort the block numbers
827  * and then transpose them from absolute block numbers to relative block
828  * numbers if necessary.
829  *
830  * NB: If the block reference table was using the bitmap representation
831  * for a given chunk, the block numbers in that chunk will already be
832  * sorted, but when the array-of-offsets representation is used, we can
833  * receive block numbers here out of order.
834  */
835  qsort(relative_block_numbers, nblocks, sizeof(BlockNumber),
837  if (start_blkno != 0)
838  {
839  for (i = 0; i < nblocks; ++i)
840  relative_block_numbers[i] -= start_blkno;
841  }
842  *num_blocks_required = nblocks;
843 
844  /*
845  * The truncation block length is the minimum length of the reconstructed
846  * file. Any block numbers below this threshold that are not present in
847  * the backup need to be fetched from the prior backup. At or above this
848  * threshold, blocks should only be included in the result if they are
849  * present in the backup. (This may require inserting zero blocks if the
850  * blocks included in the backup are non-consecutive.)
851  */
852  *truncation_block_length = size / BLCKSZ;
853  if (BlockNumberIsValid(limit_block))
854  {
855  unsigned relative_limit = limit_block - segno * RELSEG_SIZE;
856 
857  if (*truncation_block_length < relative_limit)
858  *truncation_block_length = relative_limit;
859  }
860 
861  /* Send it incrementally. */
863 }
static int compare_block_numbers(const void *a, const void *b)
char * GetIncrementalFilePath(Oid dboid, Oid spcoid, RelFileNumber relfilenumber, ForkNumber forknum, unsigned segno)
int BlockRefTableEntryGetBlocks(BlockRefTableEntry *entry, BlockNumber start_blkno, BlockNumber stop_blkno, BlockNumber *blocks, int nblocks)
Definition: blkreftable.c:369
BlockRefTableEntry * BlockRefTableGetEntry(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber *limit_block)
Definition: blkreftable.c:340
uint32 BlockNumber
Definition: block.h:31
static bool BlockNumberIsValid(BlockNumber blockNumber)
Definition: block.h:71
#define Assert(condition)
Definition: c.h:858
#define OidIsValid(objectId)
Definition: c.h:775
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
int errcode(int sqlerrcode)
Definition: elog.c:853
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
int i
Definition: isn.c:73
#define qsort(a, b, c, d)
Definition: port.h:453
@ FSM_FORKNUM
Definition: relpath.h:51
@ MAIN_FORKNUM
Definition: relpath.h:50
#define RelFileNumberIsValid(relnumber)
Definition: relpath.h:27
static pg_noinline void Size size
Definition: slab.c:607
RelFileNumber relNumber

References Assert, BACK_UP_FILE_FULLY, BACK_UP_FILE_INCREMENTALLY, BlockNumberIsValid(), BlockRefTableEntryGetBlocks(), BlockRefTableGetEntry(), IncrementalBackupInfo::brtab, IncrementalBackupInfo::buf, compare_block_numbers(), StringInfoData::data, RelFileLocator::dbOid, ereport, errcode(), errmsg_internal(), ERROR, FSM_FORKNUM, GetIncrementalFilePath(), i, MAIN_FORKNUM, IncrementalBackupInfo::manifest_files, OidIsValid, qsort, RelFileNumberIsValid, RelFileLocator::relNumber, size, and RelFileLocator::spcOid.

Referenced by sendDir().

◆ GetIncrementalFilePath()

char* GetIncrementalFilePath ( Oid  dboid,
Oid  spcoid,
RelFileNumber  relfilenumber,
ForkNumber  forknum,
unsigned  segno 
)

Definition at line 627 of file basebackup_incremental.c.

629 {
630  char *path;
631  char *lastslash;
632  char *ipath;
633 
634  path = GetRelationPath(dboid, spcoid, relfilenumber, INVALID_PROC_NUMBER,
635  forknum);
636 
637  lastslash = strrchr(path, '/');
638  Assert(lastslash != NULL);
639  *lastslash = '\0';
640 
641  if (segno > 0)
642  ipath = psprintf("%s/INCREMENTAL.%s.%u", path, lastslash + 1, segno);
643  else
644  ipath = psprintf("%s/INCREMENTAL.%s", path, lastslash + 1);
645 
646  pfree(path);
647 
648  return ipath;
649 }
#define INVALID_PROC_NUMBER
Definition: procnumber.h:26
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
char * GetRelationPath(Oid dbOid, Oid spcOid, RelFileNumber relNumber, int procNumber, ForkNumber forkNumber)
Definition: relpath.c:141

References Assert, GetRelationPath(), INVALID_PROC_NUMBER, pfree(), and psprintf().

Referenced by GetFileBackupMethod().

◆ GetIncrementalFileSize()

size_t GetIncrementalFileSize ( unsigned  num_blocks_required)

Definition at line 899 of file basebackup_incremental.c.

900 {
901  size_t result;
902 
903  /* Make sure we're not going to overflow. */
904  Assert(num_blocks_required <= RELSEG_SIZE);
905 
906  /*
907  * Header with three four byte quantities (magic number, truncation block
908  * length, block count) followed by block numbers, rounded to a multiple
909  * of BLCKSZ (for files with block data), followed by block contents.
910  */
911  result = GetIncrementalHeaderSize(num_blocks_required);
912  result += BLCKSZ * num_blocks_required;
913 
914  return result;
915 }
size_t GetIncrementalHeaderSize(unsigned num_blocks_required)

References Assert, and GetIncrementalHeaderSize().

Referenced by sendDir().

◆ GetIncrementalHeaderSize()

size_t GetIncrementalHeaderSize ( unsigned  num_blocks_required)

Definition at line 871 of file basebackup_incremental.c.

872 {
873  size_t result;
874 
875  /* Make sure we're not going to overflow. */
876  Assert(num_blocks_required <= RELSEG_SIZE);
877 
878  /*
879  * Three four byte quantities (magic number, truncation block length,
880  * block count) followed by block numbers.
881  */
882  result = 3 * sizeof(uint32) + (sizeof(BlockNumber) * num_blocks_required);
883 
884  /*
885  * Round the header size to a multiple of BLCKSZ - when not a multiple of
886  * BLCKSZ, add the missing fraction of a block. But do this only if the
887  * file will store data for some blocks, otherwise keep it small.
888  */
889  if ((num_blocks_required > 0) && (result % BLCKSZ != 0))
890  result += BLCKSZ - (result % BLCKSZ);
891 
892  return result;
893 }
unsigned int uint32
Definition: c.h:506

References Assert.

Referenced by GetIncrementalFileSize().

◆ PrepareForIncrementalBackup()

void PrepareForIncrementalBackup ( IncrementalBackupInfo ib,
BackupState backup_state 
)

Definition at line 265 of file basebackup_incremental.c.

267 {
268  MemoryContext oldcontext;
270  List *all_wslist,
271  *required_wslist = NIL;
272  ListCell *lc;
273  TimeLineHistoryEntry **tlep;
274  int num_wal_ranges;
275  int i;
276  bool found_backup_start_tli = false;
277  TimeLineID earliest_wal_range_tli = 0;
278  XLogRecPtr earliest_wal_range_start_lsn = InvalidXLogRecPtr;
279  TimeLineID latest_wal_range_tli = 0;
280 
281  Assert(ib->buf.data == NULL);
282 
283  /* Switch to our memory context. */
284  oldcontext = MemoryContextSwitchTo(ib->mcxt);
285 
286  /*
287  * A valid backup manifest must always contain at least one WAL range
288  * (usually exactly one, unless the backup spanned a timeline switch).
289  */
290  num_wal_ranges = list_length(ib->manifest_wal_ranges);
291  if (num_wal_ranges == 0)
292  ereport(ERROR,
293  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
294  errmsg("manifest contains no required WAL ranges")));
295 
296  /*
297  * Match up the TLIs that appear in the WAL ranges of the backup manifest
298  * with those that appear in this server's timeline history. We expect
299  * every backup_wal_range to match to a TimeLineHistoryEntry; if it does
300  * not, that's an error.
301  *
302  * This loop also decides which of the WAL ranges is the manifest is most
303  * ancient and which one is the newest, according to the timeline history
304  * of this server, and stores TLIs of those WAL ranges into
305  * earliest_wal_range_tli and latest_wal_range_tli. It also updates
306  * earliest_wal_range_start_lsn to the start LSN of the WAL range for
307  * earliest_wal_range_tli.
308  *
309  * Note that the return value of readTimeLineHistory puts the latest
310  * timeline at the beginning of the list, not the end. Hence, the earliest
311  * TLI is the one that occurs nearest the end of the list returned by
312  * readTimeLineHistory, and the latest TLI is the one that occurs closest
313  * to the beginning.
314  */
316  tlep = palloc0(num_wal_ranges * sizeof(TimeLineHistoryEntry *));
317  for (i = 0; i < num_wal_ranges; ++i)
318  {
320  bool saw_earliest_wal_range_tli = false;
321  bool saw_latest_wal_range_tli = false;
322 
323  /* Search this server's history for this WAL range's TLI. */
324  foreach(lc, expectedTLEs)
325  {
326  TimeLineHistoryEntry *tle = lfirst(lc);
327 
328  if (tle->tli == range->tli)
329  {
330  tlep[i] = tle;
331  break;
332  }
333 
334  if (tle->tli == earliest_wal_range_tli)
335  saw_earliest_wal_range_tli = true;
336  if (tle->tli == latest_wal_range_tli)
337  saw_latest_wal_range_tli = true;
338  }
339 
340  /*
341  * An incremental backup can only be taken relative to a backup that
342  * represents a previous state of this server. If the backup requires
343  * WAL from a timeline that's not in our history, that definitely
344  * isn't the case.
345  */
346  if (tlep[i] == NULL)
347  ereport(ERROR,
348  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
349  errmsg("timeline %u found in manifest, but not in this server's history",
350  range->tli)));
351 
352  /*
353  * If we found this TLI in the server's history before encountering
354  * the latest TLI seen so far in the server's history, then this TLI
355  * is the latest one seen so far.
356  *
357  * If on the other hand we saw the earliest TLI seen so far before
358  * finding this TLI, this TLI is earlier than the earliest one seen so
359  * far. And if this is the first TLI for which we've searched, it's
360  * also the earliest one seen so far.
361  *
362  * On the first loop iteration, both things should necessarily be
363  * true.
364  */
365  if (!saw_latest_wal_range_tli)
366  latest_wal_range_tli = range->tli;
367  if (earliest_wal_range_tli == 0 || saw_earliest_wal_range_tli)
368  {
369  earliest_wal_range_tli = range->tli;
370  earliest_wal_range_start_lsn = range->start_lsn;
371  }
372  }
373 
374  /*
375  * Propagate information about the prior backup into the backup_label that
376  * will be generated for this backup.
377  */
378  backup_state->istartpoint = earliest_wal_range_start_lsn;
379  backup_state->istarttli = earliest_wal_range_tli;
380 
381  /*
382  * Sanity check start and end LSNs for the WAL ranges in the manifest.
383  *
384  * Commonly, there won't be any timeline switches during the prior backup
385  * at all, but if there are, they should happen at the same LSNs that this
386  * server switched timelines.
387  *
388  * Whether there are any timeline switches during the prior backup or not,
389  * the prior backup shouldn't require any WAL from a timeline prior to the
390  * start of that timeline. It also shouldn't require any WAL from later
391  * than the start of this backup.
392  *
393  * If any of these sanity checks fail, one possible explanation is that
394  * the user has generated WAL on the same timeline with the same LSNs more
395  * than once. For instance, if two standbys running on timeline 1 were
396  * both promoted and (due to a broken archiving setup) both selected new
397  * timeline ID 2, then it's possible that one of these checks might trip.
398  *
399  * Note that there are lots of ways for the user to do something very bad
400  * without tripping any of these checks, and they are not intended to be
401  * comprehensive. It's pretty hard to see how we could be certain of
402  * anything here. However, if there's a problem staring us right in the
403  * face, it's best to report it, so we do.
404  */
405  for (i = 0; i < num_wal_ranges; ++i)
406  {
408 
409  if (range->tli == earliest_wal_range_tli)
410  {
411  if (range->start_lsn < tlep[i]->begin)
412  ereport(ERROR,
413  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
414  errmsg("manifest requires WAL from initial timeline %u starting at %X/%X, but that timeline begins at %X/%X",
415  range->tli,
416  LSN_FORMAT_ARGS(range->start_lsn),
417  LSN_FORMAT_ARGS(tlep[i]->begin))));
418  }
419  else
420  {
421  if (range->start_lsn != tlep[i]->begin)
422  ereport(ERROR,
423  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
424  errmsg("manifest requires WAL from continuation timeline %u starting at %X/%X, but that timeline begins at %X/%X",
425  range->tli,
426  LSN_FORMAT_ARGS(range->start_lsn),
427  LSN_FORMAT_ARGS(tlep[i]->begin))));
428  }
429 
430  if (range->tli == latest_wal_range_tli)
431  {
432  if (range->end_lsn > backup_state->startpoint)
433  ereport(ERROR,
434  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
435  errmsg("manifest requires WAL from final timeline %u ending at %X/%X, but this backup starts at %X/%X",
436  range->tli,
437  LSN_FORMAT_ARGS(range->end_lsn),
439  errhint("This can happen for incremental backups on a standby if there was little activity since the previous backup.")));
440  }
441  else
442  {
443  if (range->end_lsn != tlep[i]->end)
444  ereport(ERROR,
445  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
446  errmsg("manifest requires WAL from non-final timeline %u ending at %X/%X, but this server switched timelines at %X/%X",
447  range->tli,
448  LSN_FORMAT_ARGS(range->end_lsn),
449  LSN_FORMAT_ARGS(tlep[i]->end))));
450  }
451 
452  }
453 
454  /*
455  * Wait for WAL summarization to catch up to the backup start LSN. This
456  * will throw an error if the WAL summarizer appears to be stuck. If WAL
457  * summarization gets disabled while we're waiting, this will return
458  * immediately, and we'll error out further down if the WAL summaries are
459  * incomplete.
460  */
462 
463  /*
464  * Retrieve a list of all WAL summaries on any timeline that overlap with
465  * the LSN range of interest. We could instead call GetWalSummaries() once
466  * per timeline in the loop that follows, but that would involve reading
467  * the directory multiple times. It should be mildly faster - and perhaps
468  * a bit safer - to do it just once.
469  */
470  all_wslist = GetWalSummaries(0, earliest_wal_range_start_lsn,
472 
473  /*
474  * We need WAL summaries for everything that happened during the prior
475  * backup and everything that happened afterward up until the point where
476  * the current backup started.
477  */
478  foreach(lc, expectedTLEs)
479  {
480  TimeLineHistoryEntry *tle = lfirst(lc);
481  XLogRecPtr tli_start_lsn = tle->begin;
482  XLogRecPtr tli_end_lsn = tle->end;
483  XLogRecPtr tli_missing_lsn = InvalidXLogRecPtr;
484  List *tli_wslist;
485 
486  /*
487  * Working through the history of this server from the current
488  * timeline backwards, we skip everything until we find the timeline
489  * where this backup started. Most of the time, this means we won't
490  * skip anything at all, as it's unlikely that the timeline has
491  * changed since the beginning of the backup moments ago.
492  */
493  if (tle->tli == backup_state->starttli)
494  {
495  found_backup_start_tli = true;
496  tli_end_lsn = backup_state->startpoint;
497  }
498  else if (!found_backup_start_tli)
499  continue;
500 
501  /*
502  * Find the summaries that overlap the LSN range of interest for this
503  * timeline. If this is the earliest timeline involved, the range of
504  * interest begins with the start LSN of the prior backup; otherwise,
505  * it begins at the LSN at which this timeline came into existence. If
506  * this is the latest TLI involved, the range of interest ends at the
507  * start LSN of the current backup; otherwise, it ends at the point
508  * where we switched from this timeline to the next one.
509  */
510  if (tle->tli == earliest_wal_range_tli)
511  tli_start_lsn = earliest_wal_range_start_lsn;
512  tli_wslist = FilterWalSummaries(all_wslist, tle->tli,
513  tli_start_lsn, tli_end_lsn);
514 
515  /*
516  * There is no guarantee that the WAL summaries we found cover the
517  * entire range of LSNs for which summaries are required, or indeed
518  * that we found any WAL summaries at all. Check whether we have a
519  * problem of that sort.
520  */
521  if (!WalSummariesAreComplete(tli_wslist, tli_start_lsn, tli_end_lsn,
522  &tli_missing_lsn))
523  {
524  if (XLogRecPtrIsInvalid(tli_missing_lsn))
525  ereport(ERROR,
526  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
527  errmsg("WAL summaries are required on timeline %u from %X/%X to %X/%X, but no summaries for that timeline and LSN range exist",
528  tle->tli,
529  LSN_FORMAT_ARGS(tli_start_lsn),
530  LSN_FORMAT_ARGS(tli_end_lsn))));
531  else
532  ereport(ERROR,
533  (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
534  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",
535  tle->tli,
536  LSN_FORMAT_ARGS(tli_start_lsn),
537  LSN_FORMAT_ARGS(tli_end_lsn)),
538  errdetail("The first unsummarized LSN in this range is %X/%X.",
539  LSN_FORMAT_ARGS(tli_missing_lsn))));
540  }
541 
542  /*
543  * Remember that we need to read these summaries.
544  *
545  * Technically, it's possible that this could read more files than
546  * required, since tli_wslist in theory could contain redundant
547  * summaries. For instance, if we have a summary from 0/10000000 to
548  * 0/20000000 and also one from 0/00000000 to 0/30000000, then the
549  * latter subsumes the former and the former could be ignored.
550  *
551  * We ignore this possibility because the WAL summarizer only tries to
552  * generate summaries that do not overlap. If somehow they exist,
553  * we'll do a bit of extra work but the results should still be
554  * correct.
555  */
556  required_wslist = list_concat(required_wslist, tli_wslist);
557 
558  /*
559  * Timelines earlier than the one in which the prior backup began are
560  * not relevant.
561  */
562  if (tle->tli == earliest_wal_range_tli)
563  break;
564  }
565 
566  /*
567  * Read all of the required block reference table files and merge all of
568  * the data into a single in-memory block reference table.
569  *
570  * See the comments for struct IncrementalBackupInfo for some thoughts on
571  * memory usage.
572  */
574  foreach(lc, required_wslist)
575  {
576  WalSummaryFile *ws = lfirst(lc);
577  WalSummaryIO wsio;
578  BlockRefTableReader *reader;
579  RelFileLocator rlocator;
580  ForkNumber forknum;
581  BlockNumber limit_block;
583 
584  wsio.file = OpenWalSummaryFile(ws, false);
585  wsio.filepos = 0;
586  ereport(DEBUG1,
587  (errmsg_internal("reading WAL summary file \"%s\"",
588  FilePathName(wsio.file))));
590  FilePathName(wsio.file),
591  ReportWalSummaryError, NULL);
592  while (BlockRefTableReaderNextRelation(reader, &rlocator, &forknum,
593  &limit_block))
594  {
595  BlockRefTableSetLimitBlock(ib->brtab, &rlocator,
596  forknum, limit_block);
597 
598  while (1)
599  {
600  unsigned nblocks;
601  unsigned i;
602 
603  nblocks = BlockRefTableReaderGetBlocks(reader, blocks,
605  if (nblocks == 0)
606  break;
607 
608  for (i = 0; i < nblocks; ++i)
609  BlockRefTableMarkBlockModified(ib->brtab, &rlocator,
610  forknum, blocks[i]);
611  }
612  }
614  FileClose(wsio.file);
615  }
616 
617  /* Switch back to previous memory context. */
618  MemoryContextSwitchTo(oldcontext);
619 }
List * readTimeLineHistory(TimeLineID targetTLI)
Definition: timeline.c:76
#define BLOCKS_PER_READ
BlockRefTableReader * CreateBlockRefTableReader(io_callback_fn read_callback, void *read_callback_arg, char *error_filename, report_error_fn error_callback, void *error_callback_arg)
Definition: blkreftable.c:577
bool BlockRefTableReaderNextRelation(BlockRefTableReader *reader, RelFileLocator *rlocator, ForkNumber *forknum, BlockNumber *limit_block)
Definition: blkreftable.c:613
void BlockRefTableMarkBlockModified(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber blknum)
Definition: blkreftable.c:297
unsigned BlockRefTableReaderGetBlocks(BlockRefTableReader *reader, BlockNumber *blocks, int nblocks)
Definition: blkreftable.c:689
void BlockRefTableSetLimitBlock(BlockRefTable *brtab, const RelFileLocator *rlocator, ForkNumber forknum, BlockNumber limit_block)
Definition: blkreftable.c:262
void DestroyBlockRefTableReader(BlockRefTableReader *reader)
Definition: blkreftable.c:773
void(*) BlockRefTable CreateEmptyBlockRefTable)(void)
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errhint(const char *fmt,...)
Definition: elog.c:1317
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define DEBUG1
Definition: elog.h:30
void FileClose(File file)
Definition: fd.c:1978
char * FilePathName(File file)
Definition: fd.c:2461
List * list_concat(List *list1, const List *list2)
Definition: list.c:561
#define lfirst(lc)
Definition: pg_list.h:172
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
static void * list_nth(const List *list, int n)
Definition: pg_list.h:299
static struct cvec * range(struct vars *v, chr a, chr b, int cases)
Definition: regc_locale.c:412
ForkNumber
Definition: relpath.h:48
TimeLineID istarttli
Definition: xlogbackup.h:32
TimeLineID starttli
Definition: xlogbackup.h:27
XLogRecPtr startpoint
Definition: xlogbackup.h:26
XLogRecPtr istartpoint
Definition: xlogbackup.h:31
Definition: pg_list.h:54
XLogRecPtr begin
Definition: timeline.h:28
TimeLineID tli
Definition: timeline.h:27
XLogRecPtr end
Definition: timeline.h:29
off_t filepos
Definition: walsummary.h:24
void WaitForWalSummarization(XLogRecPtr lsn)
File OpenWalSummaryFile(WalSummaryFile *ws, bool missing_ok)
Definition: walsummary.c:205
List * GetWalSummaries(TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
Definition: walsummary.c:43
bool WalSummariesAreComplete(List *wslist, XLogRecPtr start_lsn, XLogRecPtr end_lsn, XLogRecPtr *missing_lsn)
Definition: walsummary.c:138
int ReadWalSummary(void *wal_summary_io, void *data, int length)
Definition: walsummary.c:273
List * FilterWalSummaries(List *wslist, TimeLineID tli, XLogRecPtr start_lsn, XLogRecPtr end_lsn)
Definition: walsummary.c:100
void ReportWalSummaryError(void *callback_arg, char *fmt,...)
Definition: walsummary.c:322
#define LSN_FORMAT_ARGS(lsn)
Definition: xlogdefs.h:43
#define XLogRecPtrIsInvalid(r)
Definition: xlogdefs.h:29
uint64 XLogRecPtr
Definition: xlogdefs.h:21
#define InvalidXLogRecPtr
Definition: xlogdefs.h:28
uint32 TimeLineID
Definition: xlogdefs.h:59
static BackupState * backup_state
Definition: xlogfuncs.c:40
static List * expectedTLEs
Definition: xlogrecovery.c:123

References Assert, backup_state, TimeLineHistoryEntry::begin, BlockRefTableMarkBlockModified(), BlockRefTableReaderGetBlocks(), BlockRefTableReaderNextRelation(), BlockRefTableSetLimitBlock(), BLOCKS_PER_READ, IncrementalBackupInfo::brtab, IncrementalBackupInfo::buf, CreateBlockRefTableReader(), StringInfoData::data, DEBUG1, DestroyBlockRefTableReader(), TimeLineHistoryEntry::end, ereport, errcode(), errdetail(), errhint(), errmsg(), errmsg_internal(), ERROR, expectedTLEs, WalSummaryIO::file, FileClose(), FilePathName(), WalSummaryIO::filepos, FilterWalSummaries(), GetWalSummaries(), i, InvalidXLogRecPtr, BackupState::istartpoint, BackupState::istarttli, lfirst, list_concat(), list_length(), list_nth(), LSN_FORMAT_ARGS, IncrementalBackupInfo::manifest_wal_ranges, IncrementalBackupInfo::mcxt, MemoryContextSwitchTo(), NIL, OpenWalSummaryFile(), palloc0(), range(), readTimeLineHistory(), ReadWalSummary(), ReportWalSummaryError(), BackupState::startpoint, BackupState::starttli, TimeLineHistoryEntry::tli, WaitForWalSummarization(), WalSummariesAreComplete(), and XLogRecPtrIsInvalid.

Referenced by perform_base_backup().