Skip to content

Commit

Permalink
refactoring to use common logic when getting standby.signal file (#172)
Browse files Browse the repository at this point in the history
* refactoring to use common logic when getting standby.signal file

* move common functions for backup.c and restore.c to data.c
  • Loading branch information
mikecaat committed May 10, 2021
1 parent dbf9458 commit 0882db4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
20 changes: 1 addition & 19 deletions backup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*/
Expand Down
28 changes: 28 additions & 0 deletions data.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions expected/backup_from_standby.out
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@
streaming
(1 row)

###### BACKUP COMMAND FROM STANDBY SERVER TEST-0005 ######
ERROR: please specify both standby host and port
3 changes: 2 additions & 1 deletion pg_rman.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions restore.c
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,6 @@ create_recovery_signal(void)
static void
remove_standby_signal(void)
{
struct stat stat_buf;
char path[MAXPGPATH];

if (verbose && !check)
Expand All @@ -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))
{
Expand Down
7 changes: 7 additions & 0 deletions sql/backup_from_standby.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0882db4

Please sign in to comment.