Skip to content

Commit 23d1a26

Browse files
committed
Fix MERGE of uncompressed with compressed backups
1 parent 30cf458 commit 23d1a26

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

src/catalog.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -820,20 +820,6 @@ pgBackupInit(pgBackup *backup)
820820
backup->server_version[0] = '\0';
821821
}
822822

823-
/*
824-
* Copy backup metadata from **src** into **dst**.
825-
*/
826-
void
827-
pgBackupCopy(pgBackup *dst, pgBackup *src)
828-
{
829-
pfree(dst->primary_conninfo);
830-
831-
memcpy(dst, src, sizeof(pgBackup));
832-
833-
if (src->primary_conninfo)
834-
dst->primary_conninfo = pstrdup(src->primary_conninfo);
835-
}
836-
837823
/* free pgBackup object */
838824
void
839825
pgBackupFree(void *backup)

src/data.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,8 @@ restore_data_file(const char *to_path, pgFile *file, bool allow_truncate,
829829

830830
if (write_header)
831831
{
832+
/* We uncompressed the page, so its size is BLCKSZ */
833+
header.compressed_size = BLCKSZ;
832834
if (fwrite(&header, 1, sizeof(header), out) != sizeof(header))
833835
elog(ERROR, "cannot write header of block %u of \"%s\": %s",
834836
blknum, file->path, strerror(errno));
@@ -1592,19 +1594,23 @@ check_file_pages(pgFile *file, XLogRecPtr stop_lsn, uint32 checksum_version,
15921594
if (read_len == 0 && feof(in))
15931595
break; /* EOF found */
15941596
else if (read_len != 0 && feof(in))
1595-
elog(ERROR,
1597+
elog(WARNING,
15961598
"odd size page found at block %u of \"%s\"",
15971599
blknum, file->path);
15981600
else
1599-
elog(ERROR, "cannot read header of block %u of \"%s\": %s",
1601+
elog(WARNING, "cannot read header of block %u of \"%s\": %s",
16001602
blknum, file->path, strerror(errno_tmp));
1603+
return false;
16011604
}
16021605

16031606
COMP_FILE_CRC32(use_crc32c, crc, &header, read_len);
16041607

16051608
if (header.block < blknum)
1606-
elog(ERROR, "backup is broken at file->path %s block %u",
1609+
{
1610+
elog(WARNING, "backup is broken at file->path %s block %u",
16071611
file->path, blknum);
1612+
return false;
1613+
}
16081614

16091615
blknum = header.block;
16101616

@@ -1620,8 +1626,11 @@ check_file_pages(pgFile *file, XLogRecPtr stop_lsn, uint32 checksum_version,
16201626
read_len = fread(compressed_page.data, 1,
16211627
MAXALIGN(header.compressed_size), in);
16221628
if (read_len != MAXALIGN(header.compressed_size))
1623-
elog(ERROR, "cannot read block %u of \"%s\" read %zu of %d",
1629+
{
1630+
elog(WARNING, "cannot read block %u of \"%s\" read %zu of %d",
16241631
blknum, file->path, read_len, header.compressed_size);
1632+
return false;
1633+
}
16251634

16261635
COMP_FILE_CRC32(use_crc32c, crc, compressed_page.data, read_len);
16271636

@@ -1648,11 +1657,13 @@ check_file_pages(pgFile *file, XLogRecPtr stop_lsn, uint32 checksum_version,
16481657
is_valid = false;
16491658
continue;
16501659
}
1651-
elog(ERROR, "page of file \"%s\" uncompressed to %d bytes. != BLCKSZ",
1660+
elog(WARNING, "page of file \"%s\" uncompressed to %d bytes. != BLCKSZ",
16521661
file->path, uncompressed_size);
1662+
return false;
16531663
}
1664+
16541665
if (validate_one_page(page.data, file, blknum,
1655-
stop_lsn, checksum_version) == PAGE_IS_FOUND_AND_NOT_VALID)
1666+
stop_lsn, checksum_version) == PAGE_IS_FOUND_AND_NOT_VALID)
16561667
is_valid = false;
16571668
}
16581669
else

src/dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pgFileInit(const char *path)
211211
strcpy(file->path, path); /* enough buffer size guaranteed */
212212

213213
/* Get file name from the path */
214-
file_name = strrchr(file->path, '/');
214+
file_name = last_dir_separator(file->path);
215215
if (file_name == NULL)
216216
file->name = file->path;
217217
else

src/merge.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ do_merge(time_t backup_id)
9191
}
9292
else
9393
{
94-
Assert(dest_backup);
94+
if (dest_backup == NULL)
95+
elog(ERROR, "Target backup %s was not found", base36enc(backup_id));
9596

9697
if (backup->start_time != prev_parent)
9798
continue;
@@ -335,6 +336,20 @@ merge_backups(pgBackup *to_backup, pgBackup *from_backup)
335336
}
336337
}
337338

339+
/*
340+
* Rename FULL backup directory.
341+
*/
342+
elog(INFO, "Rename %s to %s", to_backup_id, from_backup_id);
343+
if (rename(to_backup_path, from_backup_path) == -1)
344+
elog(ERROR, "Could not rename directory \"%s\" to \"%s\": %s",
345+
to_backup_path, from_backup_path, strerror(errno));
346+
347+
/*
348+
* Merging finished, now we can safely update ID of the destination backup.
349+
*/
350+
to_backup->start_time = from_backup->start_time;
351+
write_backup(to_backup);
352+
338353
/* Cleanup */
339354
if (threads)
340355
{
@@ -526,6 +541,12 @@ merge_files(void *arg)
526541
else
527542
copy_file(argument->from_root, argument->to_root, file);
528543

544+
/*
545+
* We need to save compression algorithm type of the target backup to be
546+
* able to restore in the future.
547+
*/
548+
file->compress_alg = to_backup->compress_alg;
549+
529550
if (file->write_size != BYTES_INVALID)
530551
elog(LOG, "Moved file \"%s\": " INT64_FORMAT " bytes",
531552
file->path, file->write_size);

src/pg_probackup.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,6 @@ extern void pgBackupGetPath2(const pgBackup *backup, char *path, size_t len,
476476
const char *subdir1, const char *subdir2);
477477
extern int pgBackupCreateDir(pgBackup *backup);
478478
extern void pgBackupInit(pgBackup *backup);
479-
extern void pgBackupCopy(pgBackup *dst, pgBackup *src);
480479
extern void pgBackupFree(void *backup);
481480
extern int pgBackupCompareId(const void *f1, const void *f2);
482481
extern int pgBackupCompareIdDesc(const void *f1, const void *f2);

0 commit comments

Comments
 (0)