diff --git a/src/merge.c b/src/merge.c index e3d6b9f8..13263c64 100644 --- a/src/merge.c +++ b/src/merge.c @@ -164,7 +164,7 @@ merge_backups(pgBackup *to_backup, pgBackup *from_backup) control_file[MAXPGPATH]; parray *files, *to_files; - pthread_t *threads; + pthread_t *threads = NULL; merge_files_arg *threads_args; int i; bool merge_isok = true; @@ -193,19 +193,6 @@ merge_backups(pgBackup *to_backup, pgBackup *from_backup) if (from_backup->status == BACKUP_STATUS_CORRUPT) elog(ERROR, "Interrupt merging"); - /* - * Previous merging was interrupted during deleting source backup. It is - * safe just to delete it again. - */ - if (from_backup->status == BACKUP_STATUS_DELETING) - goto delete_source_backup; - - to_backup->status = BACKUP_STATUS_MERGING; - write_backup_status(to_backup); - - from_backup->status = BACKUP_STATUS_MERGING; - write_backup_status(from_backup); - /* * Make backup paths. */ @@ -216,8 +203,6 @@ merge_backups(pgBackup *to_backup, pgBackup *from_backup) pgBackupGetPath(from_backup, from_database_path, lengthof(from_database_path), DATABASE_DIR); - create_data_directories(to_database_path, from_backup_path, false); - /* * Get list of files which will be modified or removed. */ @@ -238,6 +223,21 @@ merge_backups(pgBackup *to_backup, pgBackup *from_backup) /* sort by size for load balancing */ parray_qsort(files, pgFileCompareSize); + /* + * Previous merging was interrupted during deleting source backup. It is + * safe just to delete it again. + */ + if (from_backup->status == BACKUP_STATUS_DELETING) + goto delete_source_backup; + + to_backup->status = BACKUP_STATUS_MERGING; + write_backup_status(to_backup); + + from_backup->status = BACKUP_STATUS_MERGING; + write_backup_status(from_backup); + + create_data_directories(to_database_path, from_backup_path, false); + threads = (pthread_t *) palloc(sizeof(pthread_t) * num_threads); threads_args = (merge_files_arg *) palloc(sizeof(merge_files_arg) * num_threads); @@ -344,8 +344,11 @@ delete_source_backup: write_backup(to_backup); /* Cleanup */ - pfree(threads_args); - pfree(threads); + if (threads) + { + pfree(threads_args); + pfree(threads); + } parray_walk(to_files, pgFileFree); parray_free(to_files);