Skip to content

Commit 4ee66be

Browse files
committed
[Issue #120] minor improvements
1 parent 2fdab7c commit 4ee66be

File tree

6 files changed

+64
-58
lines changed

6 files changed

+64
-58
lines changed

src/catalog.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,3 +2447,23 @@ get_backup_index_number(parray *backup_list, pgBackup *backup)
24472447
elog(WARNING, "Failed to find backup %s", base36enc(backup->start_time));
24482448
return -1;
24492449
}
2450+
2451+
/* On backup_list lookup children of target_backup and append them to append_list */
2452+
void
2453+
append_children(parray *backup_list, pgBackup *target_backup, parray *append_list)
2454+
{
2455+
int i;
2456+
2457+
for (i = 0; i < parray_num(backup_list); i++)
2458+
{
2459+
pgBackup *backup = (pgBackup *) parray_get(backup_list, i);
2460+
2461+
/* check if backup is descendant of target backup */
2462+
if (is_parent(target_backup->start_time, backup, false))
2463+
{
2464+
/* if backup is already in the list, then skip it */
2465+
if (!parray_contains(append_list, backup))
2466+
parray_append(append_list, backup);
2467+
}
2468+
}
2469+
}

src/delete.c

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,48 +1021,16 @@ do_delete_instance(void)
10211021
return 0;
10221022
}
10231023

1024-
/* checks that parray contains element */
1025-
bool parray_contains(parray *array, void *elem)
1026-
{
1027-
int i;
1028-
for (i = 0; i < parray_num(array); i++)
1029-
{
1030-
if (parray_get(array, i) == elem) return true;
1031-
}
1032-
return false;
1033-
}
1034-
1035-
1036-
void append_childs(parray *backup_list, pgBackup *target_backup, parray *delete_list)
1037-
{
1038-
int i;
1039-
pgBackup *backup;
1040-
for (i = 0; i < parray_num(backup_list); i++)
1041-
{
1042-
backup = (pgBackup *)parray_get(backup_list, i);
1043-
if (backup == target_backup) continue;
1044-
/* check if backup is descendant of delete target */
1045-
if (is_parent(target_backup->start_time, backup, false))
1046-
{
1047-
if (!parray_contains(delete_list, backup))
1048-
parray_append(delete_list, backup);
1049-
/* recursive call */
1050-
append_childs(backup_list, backup, delete_list);
1051-
}
1052-
}
1053-
1054-
}
1055-
10561024
/* Delete all backups of given status in instance */
10571025
void
10581026
do_delete_status(InstanceConfig *instance_config, const char *status)
10591027
{
1060-
parray *backup_list, *delete_list;;
1061-
int i;
1062-
const char *pretty_status;
1063-
int n_deleted = 0, n_found = 0;
1064-
size_t size_to_delete = 0;
1065-
char size_to_delete_pretty[20];
1028+
int i;
1029+
parray *backup_list, *delete_list;
1030+
const char *pretty_status;
1031+
int n_deleted = 0, n_found = 0;
1032+
size_t size_to_delete = 0;
1033+
char size_to_delete_pretty[20];
10661034
pgBackup *backup;
10671035

10681036
BackupStatus status_for_delete = str2status(status);
@@ -1086,44 +1054,49 @@ do_delete_status(InstanceConfig *instance_config, const char *status)
10861054
return;
10871055
}
10881056

1089-
elog(INFO, "Deleting all backups with status '%s'", pretty_status);
1057+
if (dry_run)
1058+
elog(INFO, "Deleting all backups with status '%s' in dry run mode", pretty_status);
1059+
else
1060+
elog(INFO, "Deleting all backups with status '%s'", pretty_status);
10901061

1091-
/* Selects backups for deleting to delete_list array. Will delete all backups with specified status and childs*/
1062+
/* Selects backups with specified status and their children into delete_list array. */
10921063
for (i = 0; i < parray_num(backup_list); i++)
10931064
{
10941065
backup = (pgBackup *) parray_get(backup_list, i);
10951066

10961067
if (backup->status == status_for_delete)
10971068
{
10981069
n_found++;
1099-
if (parray_contains(delete_list, backup)) continue;
1070+
1071+
/* incremental backup can be already in delete_list due to append_children() */
1072+
if (parray_contains(delete_list, backup))
1073+
continue;
11001074
parray_append(delete_list, backup);
1101-
append_childs(backup_list, backup, delete_list);
1075+
1076+
append_children(backup_list, backup, delete_list);
11021077
}
11031078
}
1079+
1080+
parray_qsort(delete_list, pgBackupCompareIdDesc);
1081+
11041082
/* delete and calculate free size from delete_list */
11051083
for (i = 0; i < parray_num(delete_list); i++)
11061084
{
11071085
backup = (pgBackup *)parray_get(delete_list, i);
1108-
elog(dry_run ? INFO : LOG, "Backup %s with status %s %s be deleted",
1086+
1087+
elog(INFO, "Backup %s with status %s %s be deleted",
11091088
base36enc(backup->start_time), status2str(backup->status), dry_run ? "can" : "will");
11101089

11111090
size_to_delete += backup->data_bytes;
11121091
if (backup->stream)
11131092
size_to_delete += backup->wal_bytes;
11141093

1115-
11161094
if (!dry_run && lock_backup(backup))
1117-
{
1118-
if (interrupted)
1119-
elog(ERROR, "interrupted during delete backup");
1120-
11211095
delete_backup_files(backup);
1122-
n_deleted++;
1123-
1124-
}
11251096

1097+
n_deleted++;
11261098
}
1099+
11271100
/* Inform about data size to free */
11281101
if (size_to_delete >= 0)
11291102
{
@@ -1133,7 +1106,7 @@ do_delete_status(InstanceConfig *instance_config, const char *status)
11331106
}
11341107

11351108
/* delete selected backups */
1136-
if (!dry_run)
1109+
if (!dry_run && n_deleted > 0)
11371110
elog(INFO, "Successfully deleted %i %s from instance '%s'",
11381111
n_deleted, n_deleted == 1 ? "backup" : "backups",
11391112
instance_config->name);
@@ -1143,9 +1116,8 @@ do_delete_status(InstanceConfig *instance_config, const char *status)
11431116
elog(WARNING, "Instance '%s' has no backups with status '%s'",
11441117
instance_config->name, pretty_status);
11451118

1146-
/* Clean WAL segments */
1147-
if (delete_wal)
1148-
do_retention_wal(dry_run);
1119+
// we don`t do WAL purge here, because it is impossible to correctly handle
1120+
// dry-run case.
11491121

11501122
/* Cleanup */
11511123
parray_free(delete_list);

src/help.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,8 @@ help_delete(void)
638638
printf(_(" --wal-depth=wal-depth number of latest valid backups per timeline that must\n"));
639639
printf(_(" retain the ability to perform PITR; 0 disables; (default: 0)\n"));
640640
printf(_(" --dry-run perform a trial run without any changes\n"));
641-
printf(_(" --status=backups_status delete all backups with specific status\n"));
642-
641+
printf(_(" --status=backup_status delete all backups with specified status\n"));
642+
643643
printf(_("\n Logging options:\n"));
644644
printf(_(" --log-level-console=log-level-console\n"));
645645
printf(_(" level for console logging (default: info)\n"));

src/pg_probackup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,8 @@ extern int scan_parent_chain(pgBackup *current_backup, pgBackup **result_backup)
808808

809809
extern bool is_parent(time_t parent_backup_time, pgBackup *child_backup, bool inclusive);
810810
extern bool is_prolific(parray *backup_list, pgBackup *target_backup);
811-
extern bool in_backup_list(parray *backup_list, pgBackup *target_backup);
812811
extern int get_backup_index_number(parray *backup_list, pgBackup *backup);
812+
extern void append_children(parray *backup_list, pgBackup *target_backup, parray *append_list);
813813
extern bool launch_agent(void);
814814
extern void launch_ssh(char* argv[]);
815815
extern void wait_ssh(void);

src/utils/parray.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,16 @@ parray_bsearch(parray *array, const void *key, int(*compare)(const void *, const
197197
{
198198
return bsearch(&key, array->data, array->used, sizeof(void *), compare);
199199
}
200+
201+
/* checks that parray contains element */
202+
bool parray_contains(parray *array, void *elem)
203+
{
204+
int i;
205+
206+
for (i = 0; i < parray_num(array); i++)
207+
{
208+
if (parray_get(array, i) == elem)
209+
return true;
210+
}
211+
return false;
212+
}

src/utils/parray.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern size_t parray_num(const parray *array);
3030
extern void parray_qsort(parray *array, int(*compare)(const void *, const void *));
3131
extern void *parray_bsearch(parray *array, const void *key, int(*compare)(const void *, const void *));
3232
extern void parray_walk(parray *array, void (*action)(void *));
33+
extern bool parray_contains(parray *array, void *elem);
3334

3435
#endif /* PARRAY_H */
3536

0 commit comments

Comments
 (0)