PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
reconstruct.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "backup/basebackup_incremental.h"
#include "common/file_perm.h"
#include "common/logging.h"
#include "copy_file.h"
#include "lib/stringinfo.h"
#include "reconstruct.h"
#include "storage/block.h"
Include dependency graph for reconstruct.c:

Go to the source code of this file.

Data Structures

struct  rfile
 

Typedefs

typedef struct rfile rfile
 

Functions

static void debug_reconstruction (int n_source, rfile **sources, bool dry_run)
 
static unsigned find_reconstructed_block_length (rfile *s)
 
static rfilemake_incremental_rfile (char *filename)
 
static rfilemake_rfile (char *filename, bool missing_ok)
 
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)
 
static void read_bytes (rfile *rf, void *buffer, unsigned length)
 
static void write_block (int fd, char *output_filename, uint8 *buffer, pg_checksum_context *checksum_ctx)
 
static void read_block (rfile *s, off_t off, uint8 *buffer)
 
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)
 

Typedef Documentation

◆ rfile

typedef struct rfile rfile

Function Documentation

◆ debug_reconstruction()

static void debug_reconstruction ( int  n_source,
rfile **  sources,
bool  dry_run 
)
static

Definition at line 383 of file reconstruct.c.

384{
385 unsigned i;
386
387 for (i = 0; i < n_source; ++i)
388 {
389 rfile *s = sources[i];
390
391 /* Ignore source if not used. */
392 if (s == NULL)
393 continue;
394
395 /* If no data is needed from this file, we can ignore it. */
396 if (s->num_blocks_read == 0)
397 continue;
398
399 /* Debug logging. */
400 if (dry_run)
401 pg_log_debug("would have read %u blocks from \"%s\"",
403 else
404 pg_log_debug("read %u blocks from \"%s\"",
406
407 /*
408 * In dry-run mode, we don't actually try to read data from the file,
409 * but we do try to verify that the file is long enough that we could
410 * have read the data if we'd tried.
411 *
412 * If this fails, then it means that a non-dry-run attempt would fail,
413 * complaining of not being able to read the required bytes from the
414 * file.
415 */
416 if (dry_run)
417 {
418 struct stat sb;
419
420 if (fstat(s->fd, &sb) < 0)
421 pg_fatal("could not stat file \"%s\": %m", s->filename);
422 if (sb.st_size < s->highest_offset_read)
423 pg_fatal("file \"%s\" is too short: expected %llu, found %llu",
424 s->filename,
425 (unsigned long long) s->highest_offset_read,
426 (unsigned long long) sb.st_size);
427 }
428 }
429}
int i
Definition: isn.c:72
#define pg_log_debug(...)
Definition: logging.h:133
#define pg_fatal(...)
static bool dry_run
off_t highest_offset_read
Definition: reconstruct.c:46
int fd
Definition: reconstruct.c:40
unsigned num_blocks_read
Definition: reconstruct.c:45
char * filename
Definition: reconstruct.c:39
#define fstat
Definition: win32_port.h:283

References dry_run, rfile::fd, rfile::filename, fstat, rfile::highest_offset_read, i, rfile::num_blocks_read, pg_fatal, pg_log_debug, and stat::st_size.

Referenced by reconstruct_from_incremental_file().

◆ find_reconstructed_block_length()

static unsigned find_reconstructed_block_length ( rfile s)
static

Definition at line 438 of file reconstruct.c.

439{
440 unsigned block_length = s->truncation_block_length;
441 unsigned i;
442
443 for (i = 0; i < s->num_blocks; ++i)
444 if (s->relative_block_numbers[i] >= block_length)
445 block_length = s->relative_block_numbers[i] + 1;
446
447 return block_length;
448}
BlockNumber * relative_block_numbers
Definition: reconstruct.c:43
unsigned num_blocks
Definition: reconstruct.c:42
unsigned truncation_block_length
Definition: reconstruct.c:44

References i, rfile::num_blocks, rfile::relative_block_numbers, and rfile::truncation_block_length.

Referenced by reconstruct_from_incremental_file().

◆ make_incremental_rfile()

static rfile * make_incremental_rfile ( char *  filename)
static

Definition at line 455 of file reconstruct.c.

456{
457 rfile *rf;
458 unsigned magic;
459
460 rf = make_rfile(filename, false);
461
462 /* Read and validate magic number. */
463 read_bytes(rf, &magic, sizeof(magic));
464 if (magic != INCREMENTAL_MAGIC)
465 pg_fatal("file \"%s\" has bad incremental magic number (0x%x, expected 0x%x)",
467
468 /* Read block count. */
469 read_bytes(rf, &rf->num_blocks, sizeof(rf->num_blocks));
470 if (rf->num_blocks > RELSEG_SIZE)
471 pg_fatal("file \"%s\" has block count %u in excess of segment size %u",
472 filename, rf->num_blocks, RELSEG_SIZE);
473
474 /* Read truncation block length. */
476 sizeof(rf->truncation_block_length));
477 if (rf->truncation_block_length > RELSEG_SIZE)
478 pg_fatal("file \"%s\" has truncation block length %u in excess of segment size %u",
479 filename, rf->truncation_block_length, RELSEG_SIZE);
480
481 /* Read block numbers if there are any. */
482 if (rf->num_blocks > 0)
483 {
485 pg_malloc0(sizeof(BlockNumber) * rf->num_blocks);
487 sizeof(BlockNumber) * rf->num_blocks);
488 }
489
490 /* Remember length of header. */
491 rf->header_length = sizeof(magic) + sizeof(rf->num_blocks) +
492 sizeof(rf->truncation_block_length) +
493 sizeof(BlockNumber) * rf->num_blocks;
494
495 /*
496 * Round header length to a multiple of BLCKSZ, so that blocks contents
497 * are properly aligned. Only do this when the file actually has data for
498 * some blocks.
499 */
500 if ((rf->num_blocks > 0) && ((rf->header_length % BLCKSZ) != 0))
501 rf->header_length += (BLCKSZ - (rf->header_length % BLCKSZ));
502
503 return rf;
504}
#define INCREMENTAL_MAGIC
uint32 BlockNumber
Definition: block.h:31
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
static char * filename
Definition: pg_dumpall.c:119
static rfile * make_rfile(char *filename, bool missing_ok)
Definition: reconstruct.c:510
static void read_bytes(rfile *rf, void *buffer, unsigned length)
Definition: reconstruct.c:533
size_t header_length
Definition: reconstruct.c:41

References filename, rfile::header_length, INCREMENTAL_MAGIC, make_rfile(), rfile::num_blocks, pg_fatal, pg_malloc0(), read_bytes(), rfile::relative_block_numbers, and rfile::truncation_block_length.

Referenced by reconstruct_from_incremental_file().

◆ make_rfile()

static rfile * make_rfile ( char *  filename,
bool  missing_ok 
)
static

Definition at line 510 of file reconstruct.c.

511{
512 rfile *rf;
513
514 rf = pg_malloc0(sizeof(rfile));
516 if ((rf->fd = open(filename, O_RDONLY | PG_BINARY, 0)) < 0)
517 {
518 if (missing_ok && errno == ENOENT)
519 {
520 pg_free(rf);
521 return NULL;
522 }
523 pg_fatal("could not open file \"%s\": %m", filename);
524 }
525
526 return rf;
527}
#define PG_BINARY
Definition: c.h:1227
void pg_free(void *ptr)
Definition: fe_memutils.c:105
char * pstrdup(const char *in)
Definition: mcxt.c:1696

References rfile::fd, rfile::filename, filename, PG_BINARY, pg_fatal, pg_free(), pg_malloc0(), and pstrdup().

Referenced by make_incremental_rfile(), and reconstruct_from_incremental_file().

◆ read_block()

static void read_block ( rfile s,
off_t  off,
uint8 buffer 
)
static

Definition at line 775 of file reconstruct.c.

776{
777 int rb;
778
779 /* Read the block from the correct source, except if dry-run. */
780 rb = pg_pread(s->fd, buffer, BLCKSZ, off);
781 if (rb != BLCKSZ)
782 {
783 if (rb < 0)
784 pg_fatal("could not read from file \"%s\": %m", s->filename);
785 else
786 pg_fatal("could not read from file \"%s\", offset %llu: read %d of %d",
787 s->filename, (unsigned long long) off, rb, BLCKSZ);
788 }
789}
#define pg_pread
Definition: port.h:225

References rfile::fd, rfile::filename, pg_fatal, and pg_pread.

Referenced by write_reconstructed_file().

◆ read_bytes()

static void read_bytes ( rfile rf,
void *  buffer,
unsigned  length 
)
static

Definition at line 533 of file reconstruct.c.

534{
535 int rb = read(rf->fd, buffer, length);
536
537 if (rb != length)
538 {
539 if (rb < 0)
540 pg_fatal("could not read file \"%s\": %m", rf->filename);
541 else
542 pg_fatal("could not read file \"%s\": read %d of %u",
543 rf->filename, rb, length);
544 }
545}
#define read(a, b, c)
Definition: win32.h:13

References rfile::fd, rfile::filename, pg_fatal, and read.

Referenced by make_incremental_rfile().

◆ 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 88 of file reconstruct.c.

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

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, 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().

◆ write_block()

static void write_block ( int  fd,
char *  output_filename,
uint8 buffer,
pg_checksum_context checksum_ctx 
)
static

Definition at line 751 of file reconstruct.c.

753{
754 int wb;
755
756 if ((wb = write(fd, buffer, BLCKSZ)) != BLCKSZ)
757 {
758 if (wb < 0)
759 pg_fatal("could not write file \"%s\": %m", output_filename);
760 else
761 pg_fatal("could not write file \"%s\": wrote %d of %d",
762 output_filename, wb, BLCKSZ);
763 }
764
765 /* Update the checksum computation. */
766 if (pg_checksum_update(checksum_ctx, buffer, BLCKSZ) < 0)
767 pg_fatal("could not update checksum of file \"%s\"",
769}
int pg_checksum_update(pg_checksum_context *context, const uint8 *input, size_t len)
#define write(a, b, c)
Definition: win32.h:14
static int fd(const char *x, int i)
Definition: preproc-init.c:105

References fd(), output_filename, pg_checksum_update(), pg_fatal, and write.

Referenced by write_reconstructed_file().

◆ write_reconstructed_file()

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 
)
static

Definition at line 551 of file reconstruct.c.

560{
561 int wfd = -1;
562 unsigned i;
563 unsigned zero_blocks = 0;
564
565 /* Debugging output. */
566 if (debug)
567 {
568 StringInfoData debug_buf;
569 unsigned start_of_range = 0;
570 unsigned current_block = 0;
571
572 /* Basic information about the output file to be produced. */
573 if (dry_run)
574 pg_log_debug("would reconstruct \"%s\" (%u blocks, checksum %s)",
575 output_filename, block_length,
576 pg_checksum_type_name(checksum_ctx->type));
577 else
578 pg_log_debug("reconstructing \"%s\" (%u blocks, checksum %s)",
579 output_filename, block_length,
580 pg_checksum_type_name(checksum_ctx->type));
581
582 /* Print out the plan for reconstructing this file. */
583 initStringInfo(&debug_buf);
584 while (current_block < block_length)
585 {
586 rfile *s = sourcemap[current_block];
587
588 /* Extend range, if possible. */
589 if (current_block + 1 < block_length &&
590 s == sourcemap[current_block + 1])
591 {
592 ++current_block;
593 continue;
594 }
595
596 /* Add details about this range. */
597 if (s == NULL)
598 {
599 if (current_block == start_of_range)
600 appendStringInfo(&debug_buf, " %u:zero", current_block);
601 else
602 appendStringInfo(&debug_buf, " %u-%u:zero",
603 start_of_range, current_block);
604 }
605 else
606 {
607 if (current_block == start_of_range)
608 appendStringInfo(&debug_buf, " %u:%s@" UINT64_FORMAT,
609 current_block, s->filename,
610 (uint64) offsetmap[current_block]);
611 else
612 appendStringInfo(&debug_buf, " %u-%u:%s@" UINT64_FORMAT,
613 start_of_range, current_block,
614 s->filename,
615 (uint64) offsetmap[current_block]);
616 }
617
618 /* Begin new range. */
619 start_of_range = ++current_block;
620
621 /* If the output is very long or we are done, dump it now. */
622 if (current_block == block_length || debug_buf.len > 1024)
623 {
624 pg_log_debug("reconstruction plan:%s", debug_buf.data);
625 resetStringInfo(&debug_buf);
626 }
627 }
628
629 /* Free memory. */
630 pfree(debug_buf.data);
631 }
632
633 /* Open the output file, except in dry_run mode. */
634 if (!dry_run &&
635 (wfd = open(output_filename,
636 O_RDWR | PG_BINARY | O_CREAT | O_EXCL,
638 pg_fatal("could not open file \"%s\": %m", output_filename);
639
640 /* Read and write the blocks as required. */
641 for (i = 0; i < block_length; ++i)
642 {
643 uint8 buffer[BLCKSZ];
644 rfile *s = sourcemap[i];
645
646 /* Update accounting information. */
647 if (s == NULL)
648 ++zero_blocks;
649 else
650 {
651 s->num_blocks_read++;
653 offsetmap[i] + BLCKSZ);
654 }
655
656 /* Skip the rest of this in dry-run mode. */
657 if (dry_run)
658 continue;
659
660 /* Read or zero-fill the block as appropriate. */
661 if (s == NULL)
662 {
663 /*
664 * New block not mentioned in the WAL summary. Should have been an
665 * uninitialized block, so just zero-fill it.
666 */
667 memset(buffer, 0, BLCKSZ);
668
669 /* Write out the block, update the checksum if needed. */
670 write_block(wfd, output_filename, buffer, checksum_ctx);
671
672 /* Nothing else to do for zero-filled blocks. */
673 continue;
674 }
675
676 /* Copy the block using the appropriate copy method. */
677 if (copy_method != COPY_METHOD_COPY_FILE_RANGE)
678 {
679 /*
680 * Read the block from the correct source file, and then write it
681 * out, possibly with a checksum update.
682 */
683 read_block(s, offsetmap[i], buffer);
684 write_block(wfd, output_filename, buffer, checksum_ctx);
685 }
686 else /* use copy_file_range */
687 {
688#if defined(HAVE_COPY_FILE_RANGE)
689 /* copy_file_range modifies the offset, so use a local copy */
690 off_t off = offsetmap[i];
691 size_t nwritten = 0;
692
693 /*
694 * Retry until we've written all the bytes (the offset is updated
695 * by copy_file_range, and so is the wfd file offset).
696 */
697 do
698 {
699 int wb;
700
701 wb = copy_file_range(s->fd, &off, wfd, NULL, BLCKSZ - nwritten, 0);
702
703 if (wb < 0)
704 pg_fatal("error while copying file range from \"%s\" to \"%s\": %m",
706
707 nwritten += wb;
708
709 } while (BLCKSZ > nwritten);
710
711 /*
712 * When checksum calculation not needed, we're done, otherwise
713 * read the block and pass it to the checksum calculation.
714 */
715 if (checksum_ctx->type == CHECKSUM_TYPE_NONE)
716 continue;
717
718 read_block(s, offsetmap[i], buffer);
719
720 if (pg_checksum_update(checksum_ctx, buffer, BLCKSZ) < 0)
721 pg_fatal("could not update checksum of file \"%s\"",
723#else
724 pg_fatal("copy_file_range not supported on this platform");
725#endif
726 }
727 }
728
729 /* Debugging output. */
730 if (zero_blocks > 0)
731 {
732 if (dry_run)
733 pg_log_debug("would have zero-filled %u blocks", zero_blocks);
734 else
735 pg_log_debug("zero-filled %u blocks", zero_blocks);
736 }
737
738 /* Close the output file. */
739 if (wfd >= 0 && close(wfd) != 0)
740 pg_fatal("could not close file \"%s\": %m", output_filename);
741}
uint8_t uint8
Definition: c.h:483
#define Max(x, y)
Definition: c.h:952
#define UINT64_FORMAT
Definition: c.h:504
char * pg_checksum_type_name(pg_checksum_type type)
@ COPY_METHOD_COPY_FILE_RANGE
Definition: copy_file.h:24
int pg_file_create_mode
Definition: file_perm.c:19
static void read_block(rfile *s, off_t off, uint8 *buffer)
Definition: reconstruct.c:775
static void write_block(int fd, char *output_filename, uint8 *buffer, pg_checksum_context *checksum_ctx)
Definition: reconstruct.c:751
void resetStringInfo(StringInfo str)
Definition: stringinfo.c:75
void appendStringInfo(StringInfo str, const char *fmt,...)
Definition: stringinfo.c:94
void initStringInfo(StringInfo str)
Definition: stringinfo.c:56
pg_checksum_type type

References appendStringInfo(), CHECKSUM_TYPE_NONE, close, COPY_METHOD_COPY_FILE_RANGE, StringInfoData::data, debug, dry_run, rfile::fd, rfile::filename, rfile::highest_offset_read, i, initStringInfo(), input_filename, StringInfoData::len, Max, rfile::num_blocks_read, output_filename, pfree(), PG_BINARY, pg_checksum_type_name(), pg_checksum_update(), pg_fatal, pg_file_create_mode, pg_log_debug, read_block(), resetStringInfo(), pg_checksum_context::type, UINT64_FORMAT, and write_block().

Referenced by reconstruct_from_incremental_file().