PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 
)
extern

Definition at line 88 of file reconstruct.c.

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

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, fb(), rfile::fd, rfile::filename, filename, find_reconstructed_block_length(), fstat, rfile::header_length, i, input_filename, make_incremental_rfile(), make_rfile(), MAXPGPATH, memcpy(), 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_array, psprintf(), rfile::relative_block_numbers, snprintf, source, rfile::truncation_block_length, and write_reconstructed_file().

Referenced by process_directory_recursively().