PostgreSQL Source Code  git master
reconstruct.h File Reference
#include "common/checksum_helper.h"
#include "copy_file.h"
#include "load_manifest.h"
Include dependency graph for reconstruct.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void reconstruct_from_incremental_file (char *input_filename, char *output_filename, char *relative_path, char *bare_file_name, int n_prior_backups, char **prior_backup_dirs, manifest_data **manifests, char *manifest_path, pg_checksum_type checksum_type, int *checksum_length, uint8 **checksum_payload, CopyMethod copy_method, bool debug, bool dry_run)
 

Function Documentation

◆ reconstruct_from_incremental_file()

void reconstruct_from_incremental_file ( char *  input_filename,
char *  output_filename,
char *  relative_path,
char *  bare_file_name,
int  n_prior_backups,
char **  prior_backup_dirs,
manifest_data **  manifests,
char *  manifest_path,
pg_checksum_type  checksum_type,
int *  checksum_length,
uint8 **  checksum_payload,
CopyMethod  copy_method,
bool  debug,
bool  dry_run 
)

Definition at line 87 of file reconstruct.c.

101 {
102  rfile **source;
103  rfile *latest_source = NULL;
104  rfile **sourcemap;
105  off_t *offsetmap;
106  unsigned block_length;
107  unsigned i;
108  unsigned sidx = n_prior_backups;
109  bool full_copy_possible = true;
110  int copy_source_index = -1;
111  rfile *copy_source = NULL;
112  pg_checksum_context checksum_ctx;
113 
114  /*
115  * Every block must come either from the latest version of the file or
116  * from one of the prior backups.
117  */
118  source = pg_malloc0(sizeof(rfile *) * (1 + n_prior_backups));
119 
120  /*
121  * Use the information from the latest incremental file to figure out how
122  * long the reconstructed file should be.
123  */
124  latest_source = make_incremental_rfile(input_filename);
125  source[n_prior_backups] = latest_source;
126  block_length = find_reconstructed_block_length(latest_source);
127 
128  /*
129  * For each block in the output file, we need to know from which file we
130  * need to obtain it and at what offset in that file it's stored.
131  * sourcemap gives us the first of these things, and offsetmap the latter.
132  */
133  sourcemap = pg_malloc0(sizeof(rfile *) * block_length);
134  offsetmap = pg_malloc0(sizeof(off_t) * block_length);
135 
136  /*
137  * Every block that is present in the newest incremental file should be
138  * sourced from that file. If it precedes the truncation_block_length,
139  * it's a block that we would otherwise have had to find in an older
140  * backup and thus reduces the number of blocks remaining to be found by
141  * one; otherwise, it's an extra block that needs to be included in the
142  * output but would not have needed to be found in an older backup if it
143  * had not been present.
144  */
145  for (i = 0; i < latest_source->num_blocks; ++i)
146  {
147  BlockNumber b = latest_source->relative_block_numbers[i];
148 
149  Assert(b < block_length);
150  sourcemap[b] = latest_source;
151  offsetmap[b] = latest_source->header_length + (i * BLCKSZ);
152 
153  /*
154  * A full copy of a file from an earlier backup is only possible if no
155  * blocks are needed from any later incremental file.
156  */
157  full_copy_possible = false;
158  }
159 
160  while (1)
161  {
162  char source_filename[MAXPGPATH];
163  rfile *s;
164 
165  /*
166  * Move to the next backup in the chain. If there are no more, then
167  * we're done.
168  */
169  if (sidx == 0)
170  break;
171  --sidx;
172 
173  /*
174  * Look for the full file in the previous backup. If not found, then
175  * look for an incremental file instead.
176  */
177  snprintf(source_filename, MAXPGPATH, "%s/%s/%s",
178  prior_backup_dirs[sidx], relative_path, bare_file_name);
179  if ((s = make_rfile(source_filename, true)) == NULL)
180  {
181  snprintf(source_filename, MAXPGPATH, "%s/%s/INCREMENTAL.%s",
182  prior_backup_dirs[sidx], relative_path, bare_file_name);
183  s = make_incremental_rfile(source_filename);
184  }
185  source[sidx] = s;
186 
187  /*
188  * If s->header_length == 0, then this is a full file; otherwise, it's
189  * an incremental file.
190  */
191  if (s->header_length == 0)
192  {
193  struct stat sb;
194  BlockNumber b;
195  BlockNumber blocklength;
196 
197  /* We need to know the length of the file. */
198  if (fstat(s->fd, &sb) < 0)
199  pg_fatal("could not stat file \"%s\": %m", s->filename);
200 
201  /*
202  * Since we found a full file, source all blocks from it that
203  * exist in the file.
204  *
205  * Note that there may be blocks that don't exist either in this
206  * file or in any incremental file but that precede
207  * truncation_block_length. These are, presumably, zero-filled
208  * blocks that result from the server extending the file but
209  * taking no action on those blocks that generated any WAL.
210  *
211  * Sadly, we have no way of validating that this is really what
212  * happened, and neither does the server. From it's perspective,
213  * an unmodified block that contains data looks exactly the same
214  * as a zero-filled block that never had any data: either way,
215  * it's not mentioned in any WAL summary and the server has no
216  * reason to read it. From our perspective, all we know is that
217  * nobody had a reason to back up the block. That certainly means
218  * that the block didn't exist at the time of the full backup, but
219  * the supposition that it was all zeroes at the time of every
220  * later backup is one that we can't validate.
221  */
222  blocklength = sb.st_size / BLCKSZ;
223  for (b = 0; b < latest_source->truncation_block_length; ++b)
224  {
225  if (sourcemap[b] == NULL && b < blocklength)
226  {
227  sourcemap[b] = s;
228  offsetmap[b] = b * BLCKSZ;
229  }
230  }
231 
232  /*
233  * If a full copy looks possible, check whether the resulting file
234  * should be exactly as long as the source file is. If so, a full
235  * copy is acceptable, otherwise not.
236  */
237  if (full_copy_possible)
238  {
239  uint64 expected_length;
240 
241  expected_length =
242  (uint64) latest_source->truncation_block_length;
243  expected_length *= BLCKSZ;
244  if (expected_length == sb.st_size)
245  {
246  copy_source = s;
247  copy_source_index = sidx;
248  }
249  }
250 
251  /* We don't need to consider any further sources. */
252  break;
253  }
254 
255  /*
256  * Since we found another incremental file, source all blocks from it
257  * that we need but don't yet have.
258  */
259  for (i = 0; i < s->num_blocks; ++i)
260  {
262 
263  if (b < latest_source->truncation_block_length &&
264  sourcemap[b] == NULL)
265  {
266  sourcemap[b] = s;
267  offsetmap[b] = s->header_length + (i * BLCKSZ);
268 
269  /*
270  * A full copy of a file from an earlier backup is only
271  * possible if no blocks are needed from any later incremental
272  * file.
273  */
274  full_copy_possible = false;
275  }
276  }
277  }
278 
279  /*
280  * If a checksum of the required type already exists in the
281  * backup_manifest for the relevant input directory, we can save some work
282  * by reusing that checksum instead of computing a new one.
283  */
284  if (copy_source_index >= 0 && manifests[copy_source_index] != NULL &&
285  checksum_type != CHECKSUM_TYPE_NONE)
286  {
287  manifest_file *mfile;
288 
289  mfile = manifest_files_lookup(manifests[copy_source_index]->files,
290  manifest_path);
291  if (mfile == NULL)
292  {
293  char *path = psprintf("%s/backup_manifest",
294  prior_backup_dirs[copy_source_index]);
295 
296  /*
297  * The directory is out of sync with the backup_manifest, so emit
298  * a warning.
299  */
300  pg_log_warning("manifest file \"%s\" contains no entry for file \"%s\"",
301  path,
302  manifest_path);
303  pfree(path);
304  }
305  else if (mfile->checksum_type == checksum_type)
306  {
307  *checksum_length = mfile->checksum_length;
308  *checksum_payload = pg_malloc(*checksum_length);
309  memcpy(*checksum_payload, mfile->checksum_payload,
310  *checksum_length);
311  checksum_type = CHECKSUM_TYPE_NONE;
312  }
313  }
314 
315  /* Prepare for checksum calculation, if required. */
316  pg_checksum_init(&checksum_ctx, checksum_type);
317 
318  /*
319  * If the full file can be created by copying a file from an older backup
320  * in the chain without needing to overwrite any blocks or truncate the
321  * result, then forget about performing reconstruction and just copy that
322  * file in its entirety.
323  *
324  * Otherwise, reconstruct.
325  */
326  if (copy_source != NULL)
327  copy_file(copy_source->filename, output_filename,
328  &checksum_ctx, copy_method, dry_run);
329  else
330  {
332  block_length, sourcemap, offsetmap,
333  &checksum_ctx, copy_method,
334  debug, dry_run);
335  debug_reconstruction(n_prior_backups + 1, source, dry_run);
336  }
337 
338  /* Save results of checksum calculation. */
339  if (checksum_type != CHECKSUM_TYPE_NONE)
340  {
341  *checksum_payload = pg_malloc(PG_CHECKSUM_MAX_LENGTH);
342  *checksum_length = pg_checksum_final(&checksum_ctx,
343  *checksum_payload);
344  }
345 
346  /*
347  * Close files and release memory.
348  */
349  for (i = 0; i <= n_prior_backups; ++i)
350  {
351  rfile *s = source[i];
352 
353  if (s == NULL)
354  continue;
355  if (close(s->fd) != 0)
356  pg_fatal("could not close file \"%s\": %m", s->filename);
357  if (s->relative_block_numbers != NULL)
359  pg_free(s->filename);
360  }
361  pfree(sourcemap);
362  pfree(offsetmap);
363  pfree(source);
364 }
uint32 BlockNumber
Definition: block.h:31
#define Assert(condition)
Definition: c.h:849
int pg_checksum_final(pg_checksum_context *context, uint8 *output)
int pg_checksum_init(pg_checksum_context *context, pg_checksum_type type)
#define PG_CHECKSUM_MAX_LENGTH
@ CHECKSUM_TYPE_NONE
void copy_file(const char *fromfile, const char *tofile)
Definition: copydir.c:117
char * output_filename
Definition: ecpg.c:23
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static bool debug
Definition: initdb.c:161
#define close(a)
Definition: win32.h:12
int b
Definition: isn.c:70
int i
Definition: isn.c:73
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
void pfree(void *pointer)
Definition: mcxt.c:1521
#define pg_fatal(...)
#define MAXPGPATH
static bool dry_run
static rewind_source * source
Definition: pg_rewind.c:89
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define snprintf
Definition: port.h:238
char * input_filename
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static rfile * make_rfile(char *filename, bool missing_ok)
Definition: reconstruct.c:497
static rfile * make_incremental_rfile(char *filename)
Definition: reconstruct.c:442
static void debug_reconstruction(int n_source, rfile **sources, bool dry_run)
Definition: reconstruct.c:370
static void write_reconstructed_file(char *input_filename, char *output_filename, unsigned block_length, rfile **sourcemap, off_t *offsetmap, pg_checksum_context *checksum_ctx, CopyMethod copy_method, bool debug, bool dry_run)
Definition: reconstruct.c:538
static unsigned find_reconstructed_block_length(rfile *s)
Definition: reconstruct.c:425
uint8 * checksum_payload
Definition: load_manifest.h:29
pg_checksum_type checksum_type
Definition: load_manifest.h:27
BlockNumber * relative_block_numbers
Definition: reconstruct.c:43
int fd
Definition: reconstruct.c:40
unsigned num_blocks
Definition: reconstruct.c:42
size_t header_length
Definition: reconstruct.c:41
char * filename
Definition: reconstruct.c:39
unsigned truncation_block_length
Definition: reconstruct.c:44
#define fstat
Definition: win32_port.h:283

References Assert, b, manifest_file::checksum_length, manifest_file::checksum_payload, manifest_file::checksum_type, CHECKSUM_TYPE_NONE, close, copy_file(), debug, debug_reconstruction(), dry_run, rfile::fd, rfile::filename, find_reconstructed_block_length(), fstat, rfile::header_length, i, if(), input_filename, make_incremental_rfile(), make_rfile(), MAXPGPATH, rfile::num_blocks, output_filename, pfree(), pg_checksum_final(), pg_checksum_init(), PG_CHECKSUM_MAX_LENGTH, pg_fatal, pg_free(), pg_log_warning, pg_malloc(), pg_malloc0(), psprintf(), rfile::relative_block_numbers, snprintf, source, stat::st_size, rfile::truncation_block_length, and write_reconstructed_file().

Referenced by process_directory_recursively().