From 8d947baf848f6f8d81155b9354e9ad11cd6a3c86 Mon Sep 17 00:00:00 2001
From: Paul Guo <paulguo@gmail.com>
Date: Thu, 18 Apr 2019 19:37:36 +0800
Subject: [PATCH v2 2/2] Add option to write recovery configuration information
 in pg_rewind.

---
 src/bin/pg_rewind/pg_rewind.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c
index 7337bdd833..65d5a5d26d 100644
--- a/src/bin/pg_rewind/pg_rewind.c
+++ b/src/bin/pg_rewind/pg_rewind.c
@@ -73,6 +73,7 @@ usage(const char *progname)
 	printf(_("  -D, --target-pgdata=DIRECTORY  existing data directory to modify\n"));
 	printf(_("      --source-pgdata=DIRECTORY  source data directory to synchronize with\n"));
 	printf(_("      --source-server=CONNSTR    source server to synchronize with\n"));
+	printf(_("  -R, --write-recovery-conf      write configuration for replication\n"));
 	printf(_("  -n, --dry-run                  stop before modifying anything\n"));
 	printf(_("  -N, --no-sync                  do not wait for changes to be written\n"));
 	printf(_("                                 safely to disk\n"));
@@ -90,6 +91,7 @@ main(int argc, char **argv)
 	static struct option long_options[] = {
 		{"help", no_argument, NULL, '?'},
 		{"target-pgdata", required_argument, NULL, 'D'},
+		{"write-recovery-conf", no_argument, NULL, 'R'},
 		{"source-pgdata", required_argument, NULL, 1},
 		{"source-server", required_argument, NULL, 2},
 		{"version", no_argument, NULL, 'V'},
@@ -132,7 +134,7 @@ main(int argc, char **argv)
 		}
 	}
 
-	while ((c = getopt_long(argc, argv, "D:nNP", long_options, &option_index)) != -1)
+	while ((c = getopt_long(argc, argv, "D:nNPR", long_options, &option_index)) != -1)
 	{
 		switch (c)
 		{
@@ -152,6 +154,10 @@ main(int argc, char **argv)
 				do_sync = false;
 				break;
 
+			case 'R':
+				writerecoveryconf = true;
+				break;
+
 			case 3:
 				debug = true;
 				pg_logging_set_level(PG_LOG_DEBUG);
@@ -298,6 +304,11 @@ main(int argc, char **argv)
 	if (!rewind_needed)
 	{
 		pg_log_info("no rewind required");
+		if (writerecoveryconf && connstr_source)
+		{
+			GenerateRecoveryConf();
+			WriteRecoveryConf(datadir_target);
+		}
 		exit(0);
 	}
 
@@ -395,6 +406,12 @@ main(int argc, char **argv)
 		pg_log_info("syncing target data directory");
 	syncTargetDirectory();
 
+	if (writerecoveryconf && connstr_source)
+	{
+		GenerateRecoveryConf();
+		WriteRecoveryConf(datadir_target);
+	}
+
 	pg_log_info("Done!");
 
 	return 0;
-- 
2.17.2

