Skip to content

Commit 927474c

Browse files
committed
pg_rewind: Allow writing recovery configuration
This is provided with a new switch --write-recovery-conf and reuses the pg_basebackup code. Author: Paul Guo, Jimmy Yih, Ashwin Agrawal Reviewed-by: Alexey Kondratov, Michaël Paquier, Álvaro Herrera Discussion: https://postgr.es/m/CAEET0ZEffUkXc48pg2iqARQgGRYDiiVxDu+yYek_bTwJF+q=Uw@mail.gmail.com
1 parent a12c75a commit 927474c

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

doc/src/sgml/ref/pg_rewind.sgml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ PostgreSQL documentation
180180
</listitem>
181181
</varlistentry>
182182

183+
<varlistentry>
184+
<term><option>-R</option></term>
185+
<term><option>--write-recovery-conf</option></term>
186+
<listitem>
187+
<para>
188+
Create <filename>standby.signal</filename> and append connection
189+
settings to <filename>postgresql.auto.conf</filename> in the output
190+
directory. <literal>--source-server</literal> is mandatory with
191+
this option.
192+
</para>
193+
</listitem>
194+
</varlistentry>
195+
183196
<varlistentry>
184197
<term><option>-n</option></term>
185198
<term><option>--dry-run</option></term>

src/bin/pg_rewind/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ top_builddir = ../../..
1616
include $(top_builddir)/src/Makefile.global
1717

1818
override CPPFLAGS := -I$(libpq_srcdir) -DFRONTEND $(CPPFLAGS)
19-
LDFLAGS_INTERNAL += $(libpq_pgport)
19+
LDFLAGS_INTERNAL += $(libpq_pgport) -L$(top_builddir)/src/fe_utils -lpgfeutils
2020

2121
OBJS = pg_rewind.o parsexlog.o xlogreader.o datapagemap.o timeline.o \
2222
fetch.o file_ops.o copy_fetch.o libpq_fetch.o filemap.o \

src/bin/pg_rewind/libpq_fetch.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@
2020
#include "file_ops.h"
2121
#include "filemap.h"
2222

23-
#include "libpq-fe.h"
2423
#include "catalog/pg_type_d.h"
2524
#include "fe_utils/connect.h"
2625
#include "port/pg_bswap.h"
2726

28-
static PGconn *conn = NULL;
27+
PGconn *conn = NULL;
2928

3029
/*
3130
* Files are fetched max CHUNKSIZE bytes at a time.

src/bin/pg_rewind/pg_rewind.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "common/file_perm.h"
2828
#include "common/file_utils.h"
2929
#include "common/restricted_token.h"
30+
#include "fe_utils/recovery_gen.h"
3031
#include "getopt_long.h"
3132
#include "storage/bufpage.h"
3233

@@ -41,6 +42,7 @@ static void syncTargetDirectory(void);
4142
static void sanityChecks(void);
4243
static void findCommonAncestorTimeline(XLogRecPtr *recptr, int *tliIndex);
4344
static void ensureCleanShutdown(const char *argv0);
45+
static void disconnect_atexit(void);
4446

4547
static ControlFileData ControlFile_target;
4648
static ControlFileData ControlFile_source;
@@ -76,6 +78,8 @@ usage(const char *progname)
7678
printf(_(" -D, --target-pgdata=DIRECTORY existing data directory to modify\n"));
7779
printf(_(" --source-pgdata=DIRECTORY source data directory to synchronize with\n"));
7880
printf(_(" --source-server=CONNSTR source server to synchronize with\n"));
81+
printf(_(" -R, --write-recovery-conf write configuration for replication\n"
82+
" (requires --source-server)\n"));
7983
printf(_(" -n, --dry-run stop before modifying anything\n"));
8084
printf(_(" -N, --no-sync do not wait for changes to be written\n"
8185
" safely to disk\n"));
@@ -94,6 +98,7 @@ main(int argc, char **argv)
9498
static struct option long_options[] = {
9599
{"help", no_argument, NULL, '?'},
96100
{"target-pgdata", required_argument, NULL, 'D'},
101+
{"write-recovery-conf", no_argument, NULL, 'R'},
97102
{"source-pgdata", required_argument, NULL, 1},
98103
{"source-server", required_argument, NULL, 2},
99104
{"no-ensure-shutdown", no_argument, NULL, 44},
@@ -118,6 +123,7 @@ main(int argc, char **argv)
118123
XLogRecPtr endrec;
119124
TimeLineID endtli;
120125
ControlFileData ControlFile_new;
126+
bool writerecoveryconf = false;
121127

122128
pg_logging_init(argv[0]);
123129
set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_rewind"));
@@ -138,7 +144,7 @@ main(int argc, char **argv)
138144
}
139145
}
140146

141-
while ((c = getopt_long(argc, argv, "D:nNP", long_options, &option_index)) != -1)
147+
while ((c = getopt_long(argc, argv, "D:nNPR", long_options, &option_index)) != -1)
142148
{
143149
switch (c)
144150
{
@@ -158,6 +164,10 @@ main(int argc, char **argv)
158164
do_sync = false;
159165
break;
160166

167+
case 'R':
168+
writerecoveryconf = true;
169+
break;
170+
161171
case 3:
162172
debug = true;
163173
pg_logging_set_level(PG_LOG_DEBUG);
@@ -200,6 +210,13 @@ main(int argc, char **argv)
200210
exit(1);
201211
}
202212

213+
if (writerecoveryconf && connstr_source == NULL)
214+
{
215+
pg_log_error("no source server information (--source--server) specified for --write-recovery-conf");
216+
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
217+
exit(1);
218+
}
219+
203220
if (optind < argc)
204221
{
205222
pg_log_error("too many command-line arguments (first is \"%s\")",
@@ -236,6 +253,8 @@ main(int argc, char **argv)
236253

237254
umask(pg_mode_mask);
238255

256+
atexit(disconnect_atexit);
257+
239258
/* Connect to remote server */
240259
if (connstr_source)
241260
libpqConnect(connstr_source);
@@ -322,6 +341,9 @@ main(int argc, char **argv)
322341
if (!rewind_needed)
323342
{
324343
pg_log_info("no rewind required");
344+
if (writerecoveryconf)
345+
WriteRecoveryConfig(conn, datadir_target,
346+
GenerateRecoveryConfig(conn, NULL));
325347
exit(0);
326348
}
327349

@@ -419,6 +441,10 @@ main(int argc, char **argv)
419441
pg_log_info("syncing target data directory");
420442
syncTargetDirectory();
421443

444+
if (writerecoveryconf)
445+
WriteRecoveryConfig(conn, datadir_target,
446+
GenerateRecoveryConfig(conn, NULL));
447+
422448
pg_log_info("Done!");
423449

424450
return 0;
@@ -828,3 +854,10 @@ ensureCleanShutdown(const char *argv0)
828854
pg_fatal("Command was: %s", cmd);
829855
}
830856
}
857+
858+
static void
859+
disconnect_atexit(void)
860+
{
861+
if (conn != NULL)
862+
PQfinish(conn);
863+
}

src/bin/pg_rewind/pg_rewind.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
#include "datapagemap.h"
1515

1616
#include "access/timeline.h"
17+
#include "common/logging.h"
18+
#include "libpq-fe.h"
1719
#include "storage/block.h"
1820
#include "storage/relfilenode.h"
1921

20-
#include "common/logging.h"
2122

2223
/* Configuration options */
2324
extern char *datadir_target;
@@ -31,6 +32,9 @@ extern int WalSegSz;
3132
extern TimeLineHistoryEntry *targetHistory;
3233
extern int targetNentries;
3334

35+
/* general state */
36+
extern PGconn *conn;
37+
3438
/* Progress counters */
3539
extern uint64 fetch_size;
3640
extern uint64 fetch_done;
@@ -53,6 +57,7 @@ extern void progress_report(bool force);
5357

5458
/* in timeline.c */
5559
extern TimeLineHistoryEntry *rewind_parseTimeLineHistory(char *buffer,
56-
TimeLineID targetTLI, int *nentries);
60+
TimeLineID targetTLI,
61+
int *nentries);
5762

5863
#endif /* PG_REWIND_H */

0 commit comments

Comments
 (0)