aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagnus Hagander <magnus@hagander.net>2014-02-09 13:10:14 +0100
committerMagnus Hagander <magnus@hagander.net>2014-02-12 18:46:04 +0100
commitc90204c605b7e12858e52b6af6efdbf3cb1c1bc4 (patch)
treeb06a19a3b9db620105c4d6a1e6500f9893c7914a
parent8247236c639d08724ed13104c2acf8b322a735f0 (diff)
downloadpostgresql-c90204c605b7e12858e52b6af6efdbf3cb1c1bc4.tar.gz
postgresql-c90204c605b7e12858e52b6af6efdbf3cb1c1bc4.zip
Kill pg_basebackup background process when exiting
If an error occurs in the foreground (backup) process of pg_basebackup, and we exit in a controlled way, the background process (streaming xlog process) would stay around and keep streaming.
-rw-r--r--src/bin/pg_basebackup/pg_basebackup.c21
-rw-r--r--src/bin/pg_basebackup/pg_receivexlog.c7
-rw-r--r--src/bin/pg_basebackup/streamutil.h6
3 files changed, 28 insertions, 6 deletions
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c
index 34788122cca..12ed6c8640e 100644
--- a/src/bin/pg_basebackup/pg_basebackup.c
+++ b/src/bin/pg_basebackup/pg_basebackup.c
@@ -73,6 +73,7 @@ static PQExpBuffer recoveryconfcontents = NULL;
/* Function headers */
static void usage(void);
+static void disconnect_and_exit(int code);
static void verify_dir_is_empty_or_create(char *dirname);
static void progress_report(int tablespacenum, const char *filename);
@@ -85,6 +86,26 @@ static void BaseBackup(void);
static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
bool segment_finished);
+
+static void disconnect_and_exit(int code)
+{
+ if (conn != NULL)
+ PQfinish(conn);
+
+#ifndef WIN32
+ /*
+ * On windows, our background thread dies along with the process.
+ * But on Unix, if we have started a subprocess, we want to kill
+ * it off so it doesn't remain running trying to stream data.
+ */
+ if (bgchild> 0)
+ kill(bgchild, SIGTERM);
+#endif
+
+ exit(code);
+}
+
+
#ifdef HAVE_LIBZ
static const char *
get_gz_error(gzFile gzf)
diff --git a/src/bin/pg_basebackup/pg_receivexlog.c b/src/bin/pg_basebackup/pg_receivexlog.c
index 252a7e08d67..5738c0e6e6a 100644
--- a/src/bin/pg_basebackup/pg_receivexlog.c
+++ b/src/bin/pg_basebackup/pg_receivexlog.c
@@ -45,6 +45,13 @@ static void StreamLog();
static bool stop_streaming(XLogRecPtr segendpos, uint32 timeline,
bool segment_finished);
+#define disconnect_and_exit(code) \
+ { \
+ if (conn != NULL) PQfinish(conn); \
+ exit(code); \
+ }
+
+
static void
usage(void)
{
diff --git a/src/bin/pg_basebackup/streamutil.h b/src/bin/pg_basebackup/streamutil.h
index 77d6b86ced3..83c3087a0ba 100644
--- a/src/bin/pg_basebackup/streamutil.h
+++ b/src/bin/pg_basebackup/streamutil.h
@@ -10,10 +10,4 @@ extern int dbgetpassword;
/* Connection kept global so we can disconnect easily */
extern PGconn *conn;
-#define disconnect_and_exit(code) \
- { \
- if (conn != NULL) PQfinish(conn); \
- exit(code); \
- }
-
extern PGconn *GetConnection(void);