From 0882db44ca7de9790107efaf85ac11ae0692948f Mon Sep 17 00:00:00 2001 From: mikecaat <35882227+mikecaat@users.noreply.github.com> Date: Mon, 10 May 2021 17:07:39 +0900 Subject: [PATCH] refactoring to use common logic when getting standby.signal file (#172) * refactoring to use common logic when getting standby.signal file * move common functions for backup.c and restore.c to data.c --- backup.c | 20 +------------------- data.c | 28 ++++++++++++++++++++++++++++ expected/backup_from_standby.out | 2 ++ pg_rman.h | 3 ++- restore.c | 5 +---- sql/backup_from_standby.sh | 7 +++++++ 6 files changed, 41 insertions(+), 24 deletions(-) diff --git a/backup.c b/backup.c index 0f172269..e69111e4 100644 --- a/backup.c +++ b/backup.c @@ -860,9 +860,7 @@ do_backup(pgBackupOption bkupopt) * (ie, $PGDATA has recovery.conf or standby.signal), * check required parameters (ie, standby connection info). */ - snprintf(path, lengthof(path), "%s/standby.signal", pgdata); - make_native_path(path); - if (fileExists(path)) + if (get_standby_signal_filepath(path, sizeof(path))) { if (!bkupopt.standby_host || !bkupopt.standby_port) ereport(ERROR, @@ -1351,22 +1349,6 @@ get_xid(PGresult *res, uint32 *xid) elog(DEBUG, "current XID is %s", PQgetvalue(res, 0, 0)); } -/* - * Return true if the path is a existing regular file. - */ -bool -fileExists(const char *path) -{ - struct stat buf; - - if (stat(path, &buf) == -1 && errno == ENOENT) - return false; - else if (!S_ISREG(buf.st_mode)) - return false; - else - return true; -} - /* * Return true if the path is a existing directory. */ diff --git a/data.c b/data.c index 6f89f117..e02fd229 100644 --- a/data.c +++ b/data.c @@ -1276,3 +1276,31 @@ figure_out_segno(char *filepath) return segno; } + +/* + * Return true if the path is a existing regular file. + */ +bool +fileExists(const char *path) +{ + struct stat buf; + + if (stat(path, &buf) == -1 && errno == ENOENT) + return false; + else if (!S_ISREG(buf.st_mode)) + return false; + else + return true; +} + +/* + * Return true if standby.signal file exists and + * store the file path to the "path" + */ +bool +get_standby_signal_filepath(char *path, size_t size) +{ + snprintf(path, size, "%s/standby.signal", pgdata); + make_native_path(path); + return fileExists(path); +} diff --git a/expected/backup_from_standby.out b/expected/backup_from_standby.out index 8914b6bb..b3b13327 100644 --- a/expected/backup_from_standby.out +++ b/expected/backup_from_standby.out @@ -35,3 +35,5 @@ streaming (1 row) +###### BACKUP COMMAND FROM STANDBY SERVER TEST-0005 ###### +ERROR: please specify both standby host and port diff --git a/pg_rman.h b/pg_rman.h index 28f9c12d..1394f634 100644 --- a/pg_rman.h +++ b/pg_rman.h @@ -258,7 +258,6 @@ extern bool data_checksum_enabled; /* in backup.c */ extern int do_backup(pgBackupOption bkupopt); extern BackupMode parse_backup_mode(const char *value, int elevel); -extern bool fileExists(const char *path); /* in restore.c */ extern int do_restore(const char *target_time, @@ -340,6 +339,8 @@ extern void restore_data_file(const char *from_root, const char *to_root, extern bool copy_file(const char *from_root, const char *to_root, pgFile *file, CompressionMode compress); extern pgFile *write_stop_backup_file(pgBackup *backup, const char *buf, int len, const char *file_name); +extern bool fileExists(const char *path); +extern bool get_standby_signal_filepath(char *path, size_t size); /* in util.c */ extern void time2iso(char *buf, size_t len, time_t time); diff --git a/restore.c b/restore.c index deb13769..efc838ea 100644 --- a/restore.c +++ b/restore.c @@ -809,7 +809,6 @@ create_recovery_signal(void) static void remove_standby_signal(void) { - struct stat stat_buf; char path[MAXPGPATH]; if (verbose && !check) @@ -819,9 +818,7 @@ remove_standby_signal(void) if (!check) { - snprintf(path, lengthof(path), "%s/standby.signal", pgdata); - - if (stat(path, &stat_buf) == 0) + if (get_standby_signal_filepath(path, sizeof(path))) { if (remove(path)) { diff --git a/sql/backup_from_standby.sh b/sql/backup_from_standby.sh index 53712fcb..ceb4d016 100644 --- a/sql/backup_from_standby.sh +++ b/sql/backup_from_standby.sh @@ -273,6 +273,13 @@ psql -p ${TEST_SBYPGPORT} --no-psqlrc -d pgbench -c "SELECT status FROM pg_stat_ get_database_data_from_standby > ${TEST_BASE}/TEST-0004-after.out diff ${TEST_BASE}/TEST-0004-before.out ${TEST_BASE}/TEST-0004-after.out +# check to fail if "--standby-host" and "--standby-port" are not specified +init_backup +setup_standby +init_catalog +echo '###### BACKUP COMMAND FROM STANDBY SERVER TEST-0005 ######' +pg_rman backup -B ${BACKUP_PATH} -b full -Z -p ${TEST_PGPORT} -d postgres -D ${SBYDATA_PATH} + # clean up the temporal test data pg_ctl stop -m immediate -D ${PGDATA_PATH} > /dev/null 2>&1 pg_ctl stop -m immediate -D ${SBYDATA_PATH} > /dev/null 2>&1