Skip to content

Commit eef374e

Browse files
committed
Merge remote-tracking branch 'origin/pgpro-1248'
2 parents 8d8a70f + d2c9fce commit eef374e

File tree

10 files changed

+69
-71
lines changed

10 files changed

+69
-71
lines changed

src/backup.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,23 +1541,18 @@ pg_stop_backup(pgBackup *backup)
15411541
{
15421542
const char *params[1];
15431543
char name[1024];
1544-
char *backup_id;
1545-
1546-
backup_id = base36enc(backup->start_time);
15471544

15481545
if (!from_replica)
15491546
snprintf(name, lengthof(name), "pg_probackup, backup_id %s",
1550-
backup_id);
1547+
base36enc(backup->start_time));
15511548
else
15521549
snprintf(name, lengthof(name), "pg_probackup, backup_id %s. Replica Backup",
1553-
backup_id);
1550+
base36enc(backup->start_time));
15541551
params[0] = name;
15551552

15561553
res = pgut_execute(conn, "SELECT pg_create_restore_point($1)",
15571554
1, params, true);
15581555
PQclear(res);
1559-
1560-
pfree(backup_id);
15611556
}
15621557

15631558
/*
@@ -1845,7 +1840,8 @@ backup_cleanup(bool fatal, void *userdata)
18451840
*/
18461841
if (current.status == BACKUP_STATUS_RUNNING && current.end_time == 0)
18471842
{
1848-
elog(INFO, "Backup %s is running, setting its status to ERROR", base36enc(current.start_time));
1843+
elog(INFO, "Backup %s is running, setting its status to ERROR",
1844+
base36enc(current.start_time));
18491845
current.end_time = time(NULL);
18501846
current.status = BACKUP_STATUS_ERROR;
18511847
pgBackupWriteBackupControlFile(&current);

src/catalog.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,7 @@ pgBackupWriteControl(FILE *out, pgBackup *backup)
436436

437437
/* 'parent_backup' is set if it is incremental backup */
438438
if (backup->parent_backup != 0)
439-
{
440-
char *parent_backup = base36enc(backup->parent_backup);
441-
442-
fprintf(out, "parent-backup-id = '%s'\n", parent_backup);
443-
free(parent_backup);
444-
}
439+
fprintf(out, "parent-backup-id = '%s'\n", base36enc(backup->parent_backup));
445440
}
446441

447442
/* create BACKUP_CONTROL_FILE */
@@ -669,21 +664,17 @@ void
669664
pgBackupGetPath2(const pgBackup *backup, char *path, size_t len,
670665
const char *subdir1, const char *subdir2)
671666
{
672-
char *datetime;
673-
674-
datetime = base36enc(backup->start_time);
675-
676667
/* If "subdir1" is NULL do not check "subdir2" */
677668
if (!subdir1)
678-
snprintf(path, len, "%s/%s", backup_instance_path, datetime);
669+
snprintf(path, len, "%s/%s", backup_instance_path,
670+
base36enc(backup->start_time));
679671
else if (!subdir2)
680-
snprintf(path, len, "%s/%s/%s", backup_instance_path, datetime, subdir1);
672+
snprintf(path, len, "%s/%s/%s", backup_instance_path,
673+
base36enc(backup->start_time), subdir1);
681674
/* "subdir1" and "subdir2" is not NULL */
682675
else
683676
snprintf(path, len, "%s/%s/%s/%s", backup_instance_path,
684-
datetime, subdir1, subdir2);
685-
686-
free(datetime);
677+
base36enc(backup->start_time), subdir1, subdir2);
687678

688679
make_native_path(path);
689680
}

src/delete.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,6 @@ static int
224224
pgBackupDeleteFiles(pgBackup *backup)
225225
{
226226
size_t i;
227-
char *backup_id;
228227
char path[MAXPGPATH];
229228
char timestamp[100];
230229
parray *files;
@@ -235,11 +234,9 @@ pgBackupDeleteFiles(pgBackup *backup)
235234
if (backup->status == BACKUP_STATUS_DELETED)
236235
return 0;
237236

238-
backup_id = base36enc(backup->start_time);
239237
time2iso(timestamp, lengthof(timestamp), backup->recovery_time);
240238

241-
elog(INFO, "delete: %s %s", backup_id, timestamp);
242-
free(backup_id);
239+
elog(INFO, "delete: %s %s", base36enc(backup->start_time), timestamp);
243240

244241
/*
245242
* Update STATUS to BACKUP_STATUS_DELETING in preparation for the case which

src/parsexlog.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ validate_wal(pgBackup *backup,
290290
TimeLineID tli)
291291
{
292292
XLogRecPtr startpoint = backup->start_lsn;
293-
char *backup_id;
293+
const char *backup_id;
294294
XLogRecord *record;
295295
XLogReaderState *xlogreader;
296296
char *errormsg;
@@ -329,11 +329,9 @@ validate_wal(pgBackup *backup,
329329
else
330330
validate_backup_wal_from_start_to_stop(backup, (char *) archivedir, tli);
331331

332-
free(backup_id);
333-
334332
if (backup->status == BACKUP_STATUS_CORRUPT)
335333
{
336-
elog(WARNING, "Backup %s WAL segments are corrupted", base36enc(backup->start_time));
334+
elog(WARNING, "Backup %s WAL segments are corrupted", backup_id);
337335
return;
338336
}
339337
/*
@@ -343,7 +341,7 @@ validate_wal(pgBackup *backup,
343341
if (!TransactionIdIsValid(target_xid) && target_time == 0)
344342
{
345343
/* Recovery target is not given so exit */
346-
elog(INFO, "Backup %s WAL segments are valid", base36enc(backup->start_time));
344+
elog(INFO, "Backup %s WAL segments are valid", backup_id);
347345
return;
348346
}
349347

src/pg_probackup.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,21 +423,17 @@ main(int argc, char *argv[])
423423
return do_init();
424424
case BACKUP:
425425
{
426-
char *backup_id;
427426
const char *backup_mode;
428427
time_t start_time;
429428

430429
start_time = time(NULL);
431-
backup_id = base36enc(start_time);
432430
backup_mode = deparse_backup_mode(current.backup_mode);
433431

434432
elog(INFO, "Backup start, pg_probackup version: %s, backup ID: %s, backup mode: %s, instance: %s, stream: %s, remote: %s",
435-
PROGRAM_VERSION, backup_id, backup_mode, instance_name,
433+
PROGRAM_VERSION, base36enc(start_time), backup_mode, instance_name,
436434
current.stream ? "true" : "false", is_remote_backup ? "true" : "false");
437435
elog_file(INFO, "command: %s", command);
438436

439-
pfree(backup_id);
440-
441437
return do_backup(start_time);
442438
}
443439
case RESTORE:

src/pg_probackup.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,8 @@ extern const char *status2str(BackupStatus status);
461461
extern void remove_trailing_space(char *buf, int comment_mark);
462462
extern void remove_not_digit(char *buf, size_t len, const char *str);
463463
extern uint32 get_data_checksum_version(bool safe);
464-
extern char *base36enc(long unsigned int value);
464+
extern const char *base36enc(long unsigned int value);
465+
extern char *base36enc_dup(long unsigned int value);
465466
extern long unsigned int base36dec(const char *text);
466467
extern uint64 get_system_identifier(char *pgdata);
467468
extern uint64 get_remote_system_identifier(PGconn *conn);

src/restore.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ do_restore_or_validate(time_t target_backup_id,
195195
{
196196
if (current_backup->status != BACKUP_STATUS_OK)
197197
elog(ERROR, "base backup %s for given backup %s is in %s status",
198-
base36enc(current_backup->start_time),
199-
base36enc(dest_backup->start_time),
198+
base36enc_dup(current_backup->start_time),
199+
base36enc_dup(dest_backup->start_time),
200200
status2str(current_backup->status));
201201
else
202202
{
@@ -264,10 +264,20 @@ do_restore_or_validate(time_t target_backup_id,
264264
continue;
265265
else
266266
{
267+
char *backup_id,
268+
*corrupted_backup_id;
269+
267270
backup->status = BACKUP_STATUS_ORPHAN;
268271
pgBackupWriteBackupControlFile(backup);
272+
273+
backup_id = base36enc_dup(backup->start_time);
274+
corrupted_backup_id = base36enc_dup(corrupted_backup->start_time);
275+
269276
elog(WARNING, "Backup %s is orphaned because his parent %s is corrupted",
270-
base36enc(backup->start_time), base36enc(corrupted_backup->start_time));
277+
backup_id, corrupted_backup_id);
278+
279+
free(backup_id);
280+
free(corrupted_backup_id);
271281
}
272282
}
273283
}
@@ -409,13 +419,7 @@ restore_backup(pgBackup *backup)
409419
parray_free(files);
410420

411421
if (LOG_LEVEL_CONSOLE <= LOG || LOG_LEVEL_FILE <= LOG)
412-
{
413-
char *backup_id;
414-
415-
backup_id = base36enc(backup->start_time);
416-
elog(LOG, "restore %s backup completed", backup_id);
417-
free(backup_id);
418-
}
422+
elog(LOG, "restore %s backup completed", base36enc(backup->start_time));
419423
}
420424

421425
/*
@@ -635,13 +639,8 @@ check_tablespace_mapping(pgBackup *backup)
635639
read_tablespace_map(links, this_backup_path);
636640

637641
if (LOG_LEVEL_CONSOLE <= LOG || LOG_LEVEL_FILE <= LOG)
638-
{
639-
char *backup_id;
640-
641-
backup_id = base36enc(backup->start_time);
642-
elog(LOG, "check tablespace directories of backup %s", backup_id);
643-
pfree(backup_id);
644-
}
642+
elog(LOG, "check tablespace directories of backup %s",
643+
base36enc(backup->start_time));
645644

646645
/* 1 - each OLDDIR must have an entry in tablespace_map file (links) */
647646
for (cell = tablespace_dirs.head; cell; cell = cell->next)

src/show.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ show_backup_list(FILE *out, parray *backup_list)
234234
{
235235
pgBackup *backup = parray_get(backup_list, i);
236236
TimeLineID parent_tli;
237-
char *backup_id;
238237
char timestamp[100] = "----";
239238
char duration[20] = "----";
240239
char data_bytes_str[10] = "----";
@@ -255,12 +254,11 @@ show_backup_list(FILE *out, parray *backup_list)
255254

256255
/* Get parent timeline before printing */
257256
parent_tli = get_parent_tli(backup->tli);
258-
backup_id = base36enc(backup->start_time);
259257

260258
fprintf(out, " %-11s %-8s %-6s %-22s %-6s %-7s %3d / %-3d %5s %6s %2X/%-8X %2X/%-8X %-8s\n",
261259
instance_name,
262260
(backup->server_version[0] ? backup->server_version : "----"),
263-
backup_id,
261+
base36enc(backup->start_time),
264262
timestamp,
265263
pgBackupGetBackupMode(backup),
266264
backup->stream ? "STREAM": "ARCHIVE",
@@ -273,8 +271,6 @@ show_backup_list(FILE *out, parray *backup_list)
273271
(uint32) (backup->stop_lsn >> 32),
274272
(uint32) backup->stop_lsn,
275273
status2str(backup->status));
276-
277-
free(backup_id);
278274
}
279275
}
280276

src/util.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,29 @@
1414

1515
#include "storage/bufpage.h"
1616

17-
char *
17+
const char *
1818
base36enc(long unsigned int value)
1919
{
20-
char base36[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
20+
const char base36[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
21+
/* log(2**64) / log(36) = 12.38 => max 13 char + '\0' */
22+
static char buffer[14];
23+
unsigned int offset = sizeof(buffer);
24+
25+
buffer[--offset] = '\0';
26+
do {
27+
buffer[--offset] = base36[value % 36];
28+
} while (value /= 36);
29+
30+
return buffer;
31+
}
32+
33+
/*
34+
* Same as base36enc(), but the result must be released by the user.
35+
*/
36+
char *
37+
base36enc_dup(long unsigned int value)
38+
{
39+
const char base36[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
2140
/* log(2**64) / log(36) = 12.38 => max 13 char + '\0' */
2241
char buffer[14];
2342
unsigned int offset = sizeof(buffer);
@@ -27,7 +46,7 @@ base36enc(long unsigned int value)
2746
buffer[--offset] = base36[value % 36];
2847
} while (value /= 36);
2948

30-
return strdup(&buffer[offset]); /* warning: this must be free-d by the user */
49+
return strdup(&buffer[offset]);
3150
}
3251

3352
long unsigned int

src/validate.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ typedef struct
3131
void
3232
pgBackupValidate(pgBackup *backup)
3333
{
34-
char *backup_id_string;
34+
const char *backup_id_string;
3535
char base_path[MAXPGPATH];
3636
char path[MAXPGPATH];
3737
parray *files;
@@ -100,7 +100,6 @@ pgBackupValidate(pgBackup *backup)
100100
elog(WARNING, "Backup %s data files are corrupted", backup_id_string);
101101
else
102102
elog(INFO, "Backup %s data files are valid", backup_id_string);
103-
free(backup_id_string);
104103
}
105104

106105
/*
@@ -263,11 +262,9 @@ do_validate_instance(void)
263262
/* Valiate each backup along with its xlog files. */
264263
for (i = 0; i < parray_num(backups); i++)
265264
{
266-
char *backup_id;
267265
pgBackup *base_full_backup = NULL;
268266

269267
current_backup = (pgBackup *) parray_get(backups, i);
270-
backup_id = base36enc(current_backup->start_time);
271268

272269
if (current_backup->backup_mode != BACKUP_MODE_FULL)
273270
{
@@ -294,7 +291,7 @@ do_validate_instance(void)
294291
{
295292
if (base_full_backup == NULL)
296293
elog(ERROR, "Valid full backup for backup %s is not found.",
297-
backup_id);
294+
base36enc(current_backup->start_time));
298295
/* Validate corresponding WAL files */
299296
validate_wal(current_backup, arclog_path, 0,
300297
0, base_full_backup->tli);
@@ -313,15 +310,23 @@ do_validate_instance(void)
313310
continue;
314311
else
315312
{
313+
char *backup_id,
314+
*current_backup_id;
315+
316316
backup->status = BACKUP_STATUS_ORPHAN;
317317
pgBackupWriteBackupControlFile(backup);
318+
319+
backup_id = base36enc_dup(backup->start_time);
320+
current_backup_id = base36enc_dup(current_backup->start_time);
321+
318322
elog(WARNING, "Backup %s is orphaned because his parent %s is corrupted",
319-
base36enc(backup->start_time), base36enc(current_backup->start_time));
323+
backup_id, current_backup_id);
324+
325+
free(backup_id);
326+
free(current_backup_id);
320327
}
321328
}
322329
}
323-
324-
free(backup_id);
325330
}
326331

327332
/* cleanup */

0 commit comments

Comments
 (0)