Skip to content

Commit 5c76697

Browse files
author
Artur Zakirov
committed
Compare system_identifier of PGDATA with system_identifier of backup_conn
1 parent e3f9d59 commit 5c76697

File tree

7 files changed

+65
-36
lines changed

7 files changed

+65
-36
lines changed

backup.c

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ static volatile uint32 total_copy_files_increment;
4444
static uint32 total_files_num;
4545
static pthread_mutex_t check_stream_mut = PTHREAD_MUTEX_INITIALIZER;
4646

47+
static int is_ptrack_enable = false;
48+
4749
/* Backup connection */
4850
static PGconn *backup_conn = NULL;
4951

@@ -87,6 +89,7 @@ static char *pg_ptrack_get_and_clear(Oid tablespace_oid,
8789

8890
/* Check functions */
8991
static void check_server_version(void);
92+
static void check_system_identifier(void);
9093
static void confirm_block_size(const char *name, int blcksz);
9194

9295

@@ -114,7 +117,6 @@ do_backup_database(parray *backup_list, bool smooth_checkpoint)
114117
pthread_t backup_threads[num_threads];
115118
pthread_t stream_thread;
116119
backup_files_args *backup_threads_args[num_threads];
117-
bool is_ptrack_support;
118120

119121
/* repack the options */
120122
pgBackup *prev_backup = NULL;
@@ -131,15 +133,6 @@ do_backup_database(parray *backup_list, bool smooth_checkpoint)
131133
*/
132134
current.tli = get_current_timeline(false);
133135

134-
is_ptrack_support = pg_ptrack_support();
135-
if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK && !is_ptrack_support)
136-
elog(ERROR, "Current Postgres instance does not support ptrack");
137-
138-
if(current.backup_mode == BACKUP_MODE_DIFF_PTRACK && !pg_ptrack_enable())
139-
elog(ERROR, "ptrack is disabled");
140-
141-
if (is_ptrack_support)
142-
is_ptrack_support = pg_ptrack_enable();
143136
/*
144137
* In differential backup mode, check if there is an already-validated
145138
* full backup on current timeline.
@@ -153,8 +146,8 @@ do_backup_database(parray *backup_list, bool smooth_checkpoint)
153146
"Create new full backup before an incremental one.");
154147
}
155148

156-
/* clear ptrack files for FULL and DIFF backup */
157-
if (current.backup_mode != BACKUP_MODE_DIFF_PTRACK && is_ptrack_support)
149+
/* Clear ptrack files for FULL and DIFF backup */
150+
if (current.backup_mode != BACKUP_MODE_DIFF_PTRACK && is_ptrack_enable)
158151
pg_ptrack_clear();
159152

160153
/* notify start of backup to PostgreSQL server */
@@ -410,12 +403,12 @@ do_backup(bool smooth_checkpoint)
410403

411404
/* PGDATA and BACKUP_MODE are always required */
412405
if (pgdata == NULL)
413-
elog(ERROR, "Required parameter not specified: PGDATA "
406+
elog(ERROR, "required parameter not specified: PGDATA "
414407
"(-D, --pgdata)");
415408

416409
/* A backup mode is needed */
417410
if (current.backup_mode == BACKUP_MODE_INVALID)
418-
elog(ERROR, "Required parameter not specified: BACKUP_MODE "
411+
elog(ERROR, "required parameter not specified: BACKUP_MODE "
419412
"(-b, --backup-mode)");
420413

421414
/* Create connection for PostgreSQL */
@@ -433,18 +426,28 @@ do_backup(bool smooth_checkpoint)
433426
if (pg_is_standby() && !from_replica)
434427
elog(ERROR, "backup is not allowed for standby");
435428

436-
/* show configuration actually used */
429+
/* ptrack backup checks */
430+
if (current.backup_mode == BACKUP_MODE_DIFF_PTRACK && !pg_ptrack_support())
431+
elog(ERROR, "current Postgres instance does not support ptrack");
432+
433+
is_ptrack_enable = pg_ptrack_enable();
434+
if(current.backup_mode == BACKUP_MODE_DIFF_PTRACK && !is_ptrack_enable)
435+
elog(ERROR, "ptrack is disabled");
436+
437+
/* Get exclusive lock of backup catalog */
438+
catalog_lock(true);
439+
440+
check_system_identifier();
441+
442+
/* Show configuration actually used */
437443
elog(LOG, "========================================");
438444
elog(LOG, "backup start");
439445
elog(LOG, "----------------------------------------");
440446
if (verbose)
441447
pgBackupWriteConfigSection(stderr, &current);
442448
elog(LOG, "----------------------------------------");
443449

444-
/* get exclusive lock of backup catalog */
445-
catalog_lock(true);
446-
447-
/* initialize backup result */
450+
/* Initialize backup result */
448451
current.status = BACKUP_STATUS_RUNNING;
449452
current.tli = 0; /* get from result of pg_start_backup() */
450453
current.start_lsn = 0;
@@ -459,7 +462,7 @@ do_backup(bool smooth_checkpoint)
459462
current.checksum_version = get_data_checksum_version(true);
460463
current.stream = stream_wal;
461464

462-
/* create backup directory and backup.ini */
465+
/* Create backup directory and backup.ini */
463466
if (!check)
464467
{
465468
if (pgBackupCreateDir(&current))
@@ -544,6 +547,30 @@ check_server_version(void)
544547
confirm_block_size("wal_block_size", XLOG_BLCKSZ);
545548
}
546549

550+
/*
551+
* Compare system_identifier of PGDATA with system_identifier of backup_conn.
552+
*/
553+
static void
554+
check_system_identifier(void)
555+
{
556+
PGresult *res;
557+
uint64 id;
558+
char *val;
559+
560+
res = pgut_execute(backup_conn,
561+
"SELECT system_identifier FROM pg_control_system()",
562+
0, NULL);
563+
val = PQgetvalue(res, 0, 0);
564+
PQclear(res);
565+
566+
if (!parse_uint64(val, &id))
567+
elog(ERROR, "%s is not system_identifier", val);
568+
569+
if (id != system_identifier)
570+
elog(ERROR, "target data directory was initialized for system id %ld, but connected instance system id is %ld",
571+
system_identifier, id);
572+
}
573+
547574
static void
548575
confirm_block_size(const char *name, int blcksz)
549576
{

catalog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ catalog_lock(bool check_catalog)
215215
Assert(pgdata);
216216

217217
/* Check system-identifier */
218-
id = get_system_identifier(true);
218+
id = get_system_identifier(false);
219219
if (id != system_identifier)
220-
elog(ERROR, "Backup directory was initialized for system id = %ld, but target system id = %ld",
220+
elog(ERROR, "backup directory was initialized for system id %ld, but target system id is %ld",
221221
system_identifier, id);
222222
}
223223
}

init.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
static int selects(const struct dirent *dir)
2020
{
21-
return dir->d_name[0] != '.';
21+
return dir->d_name[0] != '.';
2222
}
2323

2424
/*
@@ -27,10 +27,10 @@ static int selects(const struct dirent *dir)
2727
int
2828
do_init(void)
2929
{
30-
char path[MAXPGPATH];
31-
char arclog_path_dir[MAXPGPATH];
32-
FILE *fp;
33-
uint64 _system_identifier;
30+
char path[MAXPGPATH];
31+
char arclog_path_dir[MAXPGPATH];
32+
FILE *fp;
33+
uint64 id;
3434

3535
struct dirent **dp;
3636
int results;
@@ -47,6 +47,9 @@ do_init(void)
4747
elog(ERROR, "backup catalog already exist and it's not empty");
4848
}
4949

50+
/* Read system_identifier from PGDATA */
51+
id = get_system_identifier(false);
52+
5053
/* create backup catalog root directory */
5154
dir_create_dir(backup_path, DIR_PERMISSION);
5255

@@ -58,15 +61,14 @@ do_init(void)
5861
join_path_components(arclog_path_dir, backup_path, "wal");
5962
dir_create_dir(arclog_path_dir, DIR_PERMISSION);
6063

61-
_system_identifier = get_system_identifier(false);
6264
/* create pg_probackup.conf */
6365
join_path_components(path, backup_path, BACKUP_CATALOG_CONF_FILE);
6466
fp = fopen(path, "wt");
6567
if (fp == NULL)
6668
elog(ERROR, "cannot create %s: %s",
6769
BACKUP_CATALOG_CONF_FILE, strerror(errno));
6870

69-
fprintf(fp, "system-identifier = %li\n", _system_identifier);
71+
fprintf(fp, "system-identifier = %li\n", id);
7072
fprintf(fp, "\n");
7173
fclose(fp);
7274

restore.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ restore_directories(const char *pg_data_dir, const char *backup_dir)
457457
* it again.
458458
*/
459459
if (!dir_is_empty(linked_path))
460-
elog(ERROR, "restore destination is not empty: \"%s\"",
460+
elog(ERROR, "restore tablespace destination is not empty: \"%s\"",
461461
linked_path);
462462

463463
if (link_sep)
@@ -560,7 +560,7 @@ check_tablespace_mapping(pgBackup *backup)
560560
linked_path);
561561

562562
if (!dir_is_empty(linked_path))
563-
elog(ERROR, "restore destination is not empty: \"%s\"",
563+
elog(ERROR, "restore tablespace destination is not empty: \"%s\"",
564564
linked_path);
565565
}
566566

@@ -1071,7 +1071,7 @@ get_tablespace_mapping(const char *dir)
10711071

10721072
/*
10731073
* Save create directory path into memory. We can use it in next page restore to
1074-
* not raise the error "restore destination is not empty" in
1074+
* not raise the error "restore tablespace destination is not empty" in
10751075
* restore_directories().
10761076
*/
10771077
static void

tests/option_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def test_options_4(self):
4848
# backup command failure without backup mode option
4949
self.assertEqual(
5050
self.run_pb(["backup", "-B", self.backup_dir(node), "-D", node.data_dir]),
51-
six.b("ERROR: Required parameter not specified: BACKUP_MODE (-b, --backup-mode)\n")
51+
six.b("ERROR: required parameter not specified: BACKUP_MODE (-b, --backup-mode)\n")
5252
)
5353

5454
# backup command failure with invalid backup mode option

tests/restore_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ def test_restore_with_tablespace_mapping_12(self):
524524

525525
# 2 - Try to restore to existing tablespace directory
526526
node.cleanup()
527-
self.assertIn(six.b("ERROR: restore destination is not empty"),
527+
self.assertIn(six.b("ERROR: restore tablespace destination is not empty"),
528528
self.restore_pb(node))
529529

530530
# 3 - Restore using tablespace-mapping

util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ uint64
114114
get_system_identifier(bool safe)
115115
{
116116
ControlFileData ControlFile;
117-
char *buffer;
118-
size_t size;
117+
char *buffer;
118+
size_t size;
119119

120120
/* First fetch file... */
121121
buffer = slurpFile(pgdata, "global/pg_control", &size, safe);

0 commit comments

Comments
 (0)