Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * pg_createsubscriber.c
4 : : * Create a new logical replica from a standby server
5 : : *
6 : : * Copyright (c) 2024-2025, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * src/bin/pg_basebackup/pg_createsubscriber.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : :
14 : : #include "postgres_fe.h"
15 : :
16 : : #include <sys/stat.h>
17 : : #include <sys/time.h>
18 : : #include <sys/wait.h>
19 : : #include <time.h>
20 : :
21 : : #include "common/connect.h"
22 : : #include "common/controldata_utils.h"
23 : : #include "common/logging.h"
24 : : #include "common/pg_prng.h"
25 : : #include "common/restricted_token.h"
26 : : #include "datatype/timestamp.h"
27 : : #include "fe_utils/recovery_gen.h"
28 : : #include "fe_utils/simple_list.h"
29 : : #include "fe_utils/string_utils.h"
30 : : #include "fe_utils/version.h"
31 : : #include "getopt_long.h"
32 : :
33 : : #define DEFAULT_SUB_PORT "50432"
34 : : #define OBJECTTYPE_PUBLICATIONS 0x0001
35 : :
36 : : /* Command-line options */
37 : : struct CreateSubscriberOptions
38 : : {
39 : : char *config_file; /* configuration file */
40 : : char *pub_conninfo_str; /* publisher connection string */
41 : : char *socket_dir; /* directory for Unix-domain socket, if any */
42 : : char *sub_port; /* subscriber port number */
43 : : const char *sub_username; /* subscriber username */
44 : : bool two_phase; /* enable-two-phase option */
45 : : SimpleStringList database_names; /* list of database names */
46 : : SimpleStringList pub_names; /* list of publication names */
47 : : SimpleStringList sub_names; /* list of subscription names */
48 : : SimpleStringList replslot_names; /* list of replication slot names */
49 : : int recovery_timeout; /* stop recovery after this time */
50 : : bool all_dbs; /* all option */
51 : : SimpleStringList objecttypes_to_clean; /* list of object types to cleanup */
52 : : };
53 : :
54 : : /* per-database publication/subscription info */
55 : : struct LogicalRepInfo
56 : : {
57 : : char *dbname; /* database name */
58 : : char *pubconninfo; /* publisher connection string */
59 : : char *subconninfo; /* subscriber connection string */
60 : : char *pubname; /* publication name */
61 : : char *subname; /* subscription name */
62 : : char *replslotname; /* replication slot name */
63 : :
64 : : bool made_replslot; /* replication slot was created */
65 : : bool made_publication; /* publication was created */
66 : : };
67 : :
68 : : /*
69 : : * Information shared across all the databases (or publications and
70 : : * subscriptions).
71 : : */
72 : : struct LogicalRepInfos
73 : : {
74 : : struct LogicalRepInfo *dbinfo;
75 : : bool two_phase; /* enable-two-phase option */
76 : : bits32 objecttypes_to_clean; /* flags indicating which object types
77 : : * to clean up on subscriber */
78 : : };
79 : :
80 : : static void cleanup_objects_atexit(void);
81 : : static void usage(void);
82 : : static char *get_base_conninfo(const char *conninfo, char **dbname);
83 : : static char *get_sub_conninfo(const struct CreateSubscriberOptions *opt);
84 : : static char *get_exec_path(const char *argv0, const char *progname);
85 : : static void check_data_directory(const char *datadir);
86 : : static char *concat_conninfo_dbname(const char *conninfo, const char *dbname);
87 : : static struct LogicalRepInfo *store_pub_sub_info(const struct CreateSubscriberOptions *opt,
88 : : const char *pub_base_conninfo,
89 : : const char *sub_base_conninfo);
90 : : static PGconn *connect_database(const char *conninfo, bool exit_on_error);
91 : : static void disconnect_database(PGconn *conn, bool exit_on_error);
92 : : static uint64 get_primary_sysid(const char *conninfo);
93 : : static uint64 get_standby_sysid(const char *datadir);
94 : : static void modify_subscriber_sysid(const struct CreateSubscriberOptions *opt);
95 : : static bool server_is_in_recovery(PGconn *conn);
96 : : static char *generate_object_name(PGconn *conn);
97 : : static void check_publisher(const struct LogicalRepInfo *dbinfo);
98 : : static char *setup_publisher(struct LogicalRepInfo *dbinfo);
99 : : static void check_subscriber(const struct LogicalRepInfo *dbinfo);
100 : : static void setup_subscriber(struct LogicalRepInfo *dbinfo,
101 : : const char *consistent_lsn);
102 : : static void setup_recovery(const struct LogicalRepInfo *dbinfo, const char *datadir,
103 : : const char *lsn);
104 : : static void drop_primary_replication_slot(struct LogicalRepInfo *dbinfo,
105 : : const char *slotname);
106 : : static void drop_failover_replication_slots(struct LogicalRepInfo *dbinfo);
107 : : static char *create_logical_replication_slot(PGconn *conn,
108 : : struct LogicalRepInfo *dbinfo);
109 : : static void drop_replication_slot(PGconn *conn, struct LogicalRepInfo *dbinfo,
110 : : const char *slot_name);
111 : : static void pg_ctl_status(const char *pg_ctl_cmd, int rc);
112 : : static void start_standby_server(const struct CreateSubscriberOptions *opt,
113 : : bool restricted_access,
114 : : bool restrict_logical_worker);
115 : : static void stop_standby_server(const char *datadir);
116 : : static void wait_for_end_recovery(const char *conninfo,
117 : : const struct CreateSubscriberOptions *opt);
118 : : static void create_publication(PGconn *conn, struct LogicalRepInfo *dbinfo);
119 : : static void drop_publication(PGconn *conn, const char *pubname,
120 : : const char *dbname, bool *made_publication);
121 : : static void check_and_drop_publications(PGconn *conn, struct LogicalRepInfo *dbinfo);
122 : : static void create_subscription(PGconn *conn, const struct LogicalRepInfo *dbinfo);
123 : : static void set_replication_progress(PGconn *conn, const struct LogicalRepInfo *dbinfo,
124 : : const char *lsn);
125 : : static void enable_subscription(PGconn *conn, const struct LogicalRepInfo *dbinfo);
126 : : static void check_and_drop_existing_subscriptions(PGconn *conn,
127 : : const struct LogicalRepInfo *dbinfo);
128 : : static void drop_existing_subscription(PGconn *conn, const char *subname,
129 : : const char *dbname);
130 : : static void get_publisher_databases(struct CreateSubscriberOptions *opt,
131 : : bool dbnamespecified);
132 : :
133 : : #define WAIT_INTERVAL 1 /* 1 second */
134 : :
135 : : static const char *progname;
136 : :
137 : : static char *primary_slot_name = NULL;
138 : : static bool dry_run = false;
139 : :
140 : : static bool success = false;
141 : :
142 : : static struct LogicalRepInfos dbinfos;
143 : : static int num_dbs = 0; /* number of specified databases */
144 : : static int num_pubs = 0; /* number of specified publications */
145 : : static int num_subs = 0; /* number of specified subscriptions */
146 : : static int num_replslots = 0; /* number of specified replication slots */
147 : :
148 : : static pg_prng_state prng_state;
149 : :
150 : : static char *pg_ctl_path = NULL;
151 : : static char *pg_resetwal_path = NULL;
152 : :
153 : : /* standby / subscriber data directory */
154 : : static char *subscriber_dir = NULL;
155 : :
156 : : static bool recovery_ended = false;
157 : : static bool standby_running = false;
158 : :
159 : :
160 : : /*
161 : : * Cleanup objects that were created by pg_createsubscriber if there is an
162 : : * error.
163 : : *
164 : : * Publications and replication slots are created on primary. Depending on the
165 : : * step it failed, it should remove the already created objects if it is
166 : : * possible (sometimes it won't work due to a connection issue).
167 : : * There is no cleanup on the target server. The steps on the target server are
168 : : * executed *after* promotion, hence, at this point, a failure means recreate
169 : : * the physical replica and start again.
170 : : */
171 : : static void
631 peter@eisentraut.org 172 :CBC 10 : cleanup_objects_atexit(void)
173 : : {
174 [ + + ]: 10 : if (success)
175 : 4 : return;
176 : :
177 : : /*
178 : : * If the server is promoted, there is no way to use the current setup
179 : : * again. Warn the user that a new replication setup should be done before
180 : : * trying again.
181 : : */
182 [ - + ]: 6 : if (recovery_ended)
183 : : {
631 peter@eisentraut.org 184 :UBC 0 : pg_log_warning("failed after the end of recovery");
185 : 0 : pg_log_warning_hint("The target server cannot be used as a physical replica anymore. "
186 : : "You must recreate the physical replica before continuing.");
187 : : }
188 : :
631 peter@eisentraut.org 189 [ + + ]:CBC 18 : for (int i = 0; i < num_dbs; i++)
190 : : {
275 michael@paquier.xyz 191 : 12 : struct LogicalRepInfo *dbinfo = &dbinfos.dbinfo[i];
192 : :
193 [ + - - + ]: 12 : if (dbinfo->made_publication || dbinfo->made_replslot)
194 : : {
195 : : PGconn *conn;
196 : :
275 michael@paquier.xyz 197 :UBC 0 : conn = connect_database(dbinfo->pubconninfo, false);
631 peter@eisentraut.org 198 [ # # ]: 0 : if (conn != NULL)
199 : : {
275 michael@paquier.xyz 200 [ # # ]: 0 : if (dbinfo->made_publication)
271 akapila@postgresql.o 201 : 0 : drop_publication(conn, dbinfo->pubname, dbinfo->dbname,
202 : : &dbinfo->made_publication);
275 michael@paquier.xyz 203 [ # # ]: 0 : if (dbinfo->made_replslot)
204 : 0 : drop_replication_slot(conn, dbinfo, dbinfo->replslotname);
631 peter@eisentraut.org 205 : 0 : disconnect_database(conn, false);
206 : : }
207 : : else
208 : : {
209 : : /*
210 : : * If a connection could not be established, inform the user
211 : : * that some objects were left on primary and should be
212 : : * removed before trying again.
213 : : */
275 michael@paquier.xyz 214 [ # # ]: 0 : if (dbinfo->made_publication)
215 : : {
479 peter@eisentraut.org 216 : 0 : pg_log_warning("publication \"%s\" created in database \"%s\" on primary was left behind",
217 : : dbinfo->pubname,
218 : : dbinfo->dbname);
219 : 0 : pg_log_warning_hint("Drop this publication before trying again.");
220 : : }
275 michael@paquier.xyz 221 [ # # ]: 0 : if (dbinfo->made_replslot)
222 : : {
479 peter@eisentraut.org 223 : 0 : pg_log_warning("replication slot \"%s\" created in database \"%s\" on primary was left behind",
224 : : dbinfo->replslotname,
225 : : dbinfo->dbname);
631 226 : 0 : pg_log_warning_hint("Drop this replication slot soon to avoid retention of WAL files.");
227 : : }
228 : : }
229 : : }
230 : : }
231 : :
631 peter@eisentraut.org 232 [ + + ]:CBC 6 : if (standby_running)
233 : 4 : stop_standby_server(subscriber_dir);
234 : : }
235 : :
236 : : static void
237 : 1 : usage(void)
238 : : {
239 : 1 : printf(_("%s creates a new logical replica from a standby server.\n\n"),
240 : : progname);
241 : 1 : printf(_("Usage:\n"));
242 : 1 : printf(_(" %s [OPTION]...\n"), progname);
243 : 1 : printf(_("\nOptions:\n"));
263 akapila@postgresql.o 244 : 1 : printf(_(" -a, --all create subscriptions for all databases except template\n"
245 : : " databases and databases that don't allow connections\n"));
479 peter@eisentraut.org 246 : 1 : printf(_(" -d, --database=DBNAME database in which to create a subscription\n"));
544 247 : 1 : printf(_(" -D, --pgdata=DATADIR location for the subscriber data directory\n"));
248 : 1 : printf(_(" -n, --dry-run dry run, just show what would be done\n"));
249 : 1 : printf(_(" -p, --subscriber-port=PORT subscriber port number (default %s)\n"), DEFAULT_SUB_PORT);
250 : 1 : printf(_(" -P, --publisher-server=CONNSTR publisher connection string\n"));
502 251 : 1 : printf(_(" -s, --socketdir=DIR socket directory to use (default current dir.)\n"));
544 252 : 1 : printf(_(" -t, --recovery-timeout=SECS seconds to wait for recovery to end\n"));
293 akapila@postgresql.o 253 : 1 : printf(_(" -T, --enable-two-phase enable two-phase commit for all subscriptions\n"));
479 peter@eisentraut.org 254 : 1 : printf(_(" -U, --subscriber-username=NAME user name for subscriber connection\n"));
544 255 : 1 : printf(_(" -v, --verbose output verbose messages\n"));
174 256 : 1 : printf(_(" --clean=OBJECTTYPE drop all objects of the specified type from specified\n"
257 : : " databases on the subscriber; accepts: \"%s\"\n"), "publications");
544 258 : 1 : printf(_(" --config-file=FILENAME use specified main server configuration\n"
259 : : " file when running target cluster\n"));
260 : 1 : printf(_(" --publication=NAME publication name\n"));
261 : 1 : printf(_(" --replication-slot=NAME replication slot name\n"));
262 : 1 : printf(_(" --subscription=NAME subscription name\n"));
263 : 1 : printf(_(" -V, --version output version information, then exit\n"));
264 : 1 : printf(_(" -?, --help show this help, then exit\n"));
631 265 : 1 : printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
266 : 1 : printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
267 : 1 : }
268 : :
269 : : /*
270 : : * Subroutine to append "keyword=value" to a connection string,
271 : : * with proper quoting of the value. (We assume keywords don't need that.)
272 : : */
273 : : static void
534 tgl@sss.pgh.pa.us 274 : 107 : appendConnStrItem(PQExpBuffer buf, const char *keyword, const char *val)
275 : : {
276 [ + + ]: 107 : if (buf->len > 0)
277 : 79 : appendPQExpBufferChar(buf, ' ');
278 : 107 : appendPQExpBufferStr(buf, keyword);
279 : 107 : appendPQExpBufferChar(buf, '=');
280 : 107 : appendConnStrVal(buf, val);
281 : 107 : }
282 : :
283 : : /*
284 : : * Validate a connection string. Returns a base connection string that is a
285 : : * connection string without a database name.
286 : : *
287 : : * Since we might process multiple databases, each database name will be
288 : : * appended to this base connection string to provide a final connection
289 : : * string. If the second argument (dbname) is not null, returns dbname if the
290 : : * provided connection string contains it.
291 : : *
292 : : * It is the caller's responsibility to free the returned connection string and
293 : : * dbname.
294 : : */
295 : : static char *
631 peter@eisentraut.org 296 : 14 : get_base_conninfo(const char *conninfo, char **dbname)
297 : : {
298 : : PQExpBuffer buf;
299 : : PQconninfoOption *conn_opts;
300 : : PQconninfoOption *conn_opt;
301 : 14 : char *errmsg = NULL;
302 : : char *ret;
303 : :
304 : 14 : conn_opts = PQconninfoParse(conninfo, &errmsg);
305 [ - + ]: 14 : if (conn_opts == NULL)
306 : : {
631 peter@eisentraut.org 307 :UBC 0 : pg_log_error("could not parse connection string: %s", errmsg);
624 tgl@sss.pgh.pa.us 308 : 0 : PQfreemem(errmsg);
631 peter@eisentraut.org 309 : 0 : return NULL;
310 : : }
311 : :
624 tgl@sss.pgh.pa.us 312 :CBC 14 : buf = createPQExpBuffer();
631 peter@eisentraut.org 313 [ + + ]: 728 : for (conn_opt = conn_opts; conn_opt->keyword != NULL; conn_opt++)
314 : : {
315 [ + + + - ]: 714 : if (conn_opt->val != NULL && conn_opt->val[0] != '\0')
316 : : {
534 tgl@sss.pgh.pa.us 317 [ + + ]: 33 : if (strcmp(conn_opt->keyword, "dbname") == 0)
318 : : {
319 [ + - ]: 9 : if (dbname)
320 : 9 : *dbname = pg_strdup(conn_opt->val);
321 : 9 : continue;
322 : : }
323 : 24 : appendConnStrItem(buf, conn_opt->keyword, conn_opt->val);
324 : : }
325 : : }
326 : :
631 peter@eisentraut.org 327 : 14 : ret = pg_strdup(buf->data);
328 : :
329 : 14 : destroyPQExpBuffer(buf);
330 : 14 : PQconninfoFree(conn_opts);
331 : :
332 : 14 : return ret;
333 : : }
334 : :
335 : : /*
336 : : * Build a subscriber connection string. Only a few parameters are supported
337 : : * since it starts a server with restricted access.
338 : : */
339 : : static char *
340 : 14 : get_sub_conninfo(const struct CreateSubscriberOptions *opt)
341 : : {
342 : 14 : PQExpBuffer buf = createPQExpBuffer();
343 : : char *ret;
344 : :
534 tgl@sss.pgh.pa.us 345 : 14 : appendConnStrItem(buf, "port", opt->sub_port);
346 : : #if !defined(WIN32)
347 : 14 : appendConnStrItem(buf, "host", opt->socket_dir);
348 : : #endif
631 peter@eisentraut.org 349 [ - + ]: 14 : if (opt->sub_username != NULL)
534 tgl@sss.pgh.pa.us 350 :UBC 0 : appendConnStrItem(buf, "user", opt->sub_username);
534 tgl@sss.pgh.pa.us 351 :CBC 14 : appendConnStrItem(buf, "fallback_application_name", progname);
352 : :
631 peter@eisentraut.org 353 : 14 : ret = pg_strdup(buf->data);
354 : :
355 : 14 : destroyPQExpBuffer(buf);
356 : :
357 : 14 : return ret;
358 : : }
359 : :
360 : : /*
361 : : * Verify if a PostgreSQL binary (progname) is available in the same directory as
362 : : * pg_createsubscriber and it has the same version. It returns the absolute
363 : : * path of the progname.
364 : : */
365 : : static char *
366 : 20 : get_exec_path(const char *argv0, const char *progname)
367 : : {
368 : : char *versionstr;
369 : : char *exec_path;
370 : : int ret;
371 : :
372 : 20 : versionstr = psprintf("%s (PostgreSQL) %s\n", progname, PG_VERSION);
373 : 20 : exec_path = pg_malloc(MAXPGPATH);
374 : 20 : ret = find_other_exec(argv0, progname, versionstr, exec_path);
375 : :
376 [ - + ]: 20 : if (ret < 0)
377 : : {
378 : : char full_path[MAXPGPATH];
379 : :
631 peter@eisentraut.org 380 [ # # ]:UBC 0 : if (find_my_exec(argv0, full_path) < 0)
381 : 0 : strlcpy(full_path, progname, sizeof(full_path));
382 : :
383 [ # # ]: 0 : if (ret == -1)
384 : 0 : pg_fatal("program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
385 : : progname, "pg_createsubscriber", full_path);
386 : : else
387 : 0 : pg_fatal("program \"%s\" was found by \"%s\" but was not the same version as %s",
388 : : progname, full_path, "pg_createsubscriber");
389 : : }
390 : :
631 peter@eisentraut.org 391 [ + + ]:CBC 20 : pg_log_debug("%s path is: %s", progname, exec_path);
392 : :
393 : 20 : return exec_path;
394 : : }
395 : :
396 : : /*
397 : : * Is it a cluster directory? These are preliminary checks. It is far from
398 : : * making an accurate check. If it is not a clone from the publisher, it will
399 : : * eventually fail in a future step.
400 : : */
401 : : static void
402 : 10 : check_data_directory(const char *datadir)
403 : : {
404 : : struct stat statbuf;
405 : : uint32 major_version;
406 : : char *version_str;
407 : :
408 : 10 : pg_log_info("checking if directory \"%s\" is a cluster data directory",
409 : : datadir);
410 : :
411 [ - + ]: 10 : if (stat(datadir, &statbuf) != 0)
412 : : {
631 peter@eisentraut.org 413 [ # # ]:UBC 0 : if (errno == ENOENT)
414 : 0 : pg_fatal("data directory \"%s\" does not exist", datadir);
415 : : else
630 416 : 0 : pg_fatal("could not access directory \"%s\": %m", datadir);
417 : : }
418 : :
419 : : /*
420 : : * Retrieve the contents of this cluster's PG_VERSION. We require
421 : : * compatibility with the same major version as the one this tool is
422 : : * compiled with.
423 : : */
62 michael@paquier.xyz 424 :GNC 10 : major_version = GET_PG_MAJORVERSION_NUM(get_pg_version(datadir, &version_str));
425 [ - + ]: 10 : if (major_version != PG_MAJORVERSION_NUM)
426 : : {
62 michael@paquier.xyz 427 :UNC 0 : pg_log_error("data directory is of wrong version");
428 : 0 : pg_log_error_detail("File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".",
429 : : "PG_VERSION", version_str, PG_MAJORVERSION);
430 : 0 : exit(1);
431 : : }
631 peter@eisentraut.org 432 :CBC 10 : }
433 : :
434 : : /*
435 : : * Append database name into a base connection string.
436 : : *
437 : : * dbname is the only parameter that changes so it is not included in the base
438 : : * connection string. This function concatenates dbname to build a "real"
439 : : * connection string.
440 : : */
441 : : static char *
442 : 41 : concat_conninfo_dbname(const char *conninfo, const char *dbname)
443 : : {
444 : 41 : PQExpBuffer buf = createPQExpBuffer();
445 : : char *ret;
446 : :
447 [ - + ]: 41 : Assert(conninfo != NULL);
448 : :
449 : 41 : appendPQExpBufferStr(buf, conninfo);
534 tgl@sss.pgh.pa.us 450 : 41 : appendConnStrItem(buf, "dbname", dbname);
451 : :
631 peter@eisentraut.org 452 : 41 : ret = pg_strdup(buf->data);
453 : 41 : destroyPQExpBuffer(buf);
454 : :
455 : 41 : return ret;
456 : : }
457 : :
458 : : /*
459 : : * Store publication and subscription information.
460 : : *
461 : : * If publication, replication slot and subscription names were specified,
462 : : * store it here. Otherwise, a generated name will be assigned to the object in
463 : : * setup_publisher().
464 : : */
465 : : static struct LogicalRepInfo *
466 : 10 : store_pub_sub_info(const struct CreateSubscriberOptions *opt,
467 : : const char *pub_base_conninfo,
468 : : const char *sub_base_conninfo)
469 : : {
470 : : struct LogicalRepInfo *dbinfo;
471 : 10 : SimpleStringListCell *pubcell = NULL;
472 : 10 : SimpleStringListCell *subcell = NULL;
473 : 10 : SimpleStringListCell *replslotcell = NULL;
474 : 10 : int i = 0;
475 : :
476 : 10 : dbinfo = pg_malloc_array(struct LogicalRepInfo, num_dbs);
477 : :
478 [ + + ]: 10 : if (num_pubs > 0)
479 : 2 : pubcell = opt->pub_names.head;
480 [ + + ]: 10 : if (num_subs > 0)
481 : 1 : subcell = opt->sub_names.head;
482 [ + + ]: 10 : if (num_replslots > 0)
483 : 2 : replslotcell = opt->replslot_names.head;
484 : :
485 [ + + ]: 30 : for (SimpleStringListCell *cell = opt->database_names.head; cell; cell = cell->next)
486 : : {
487 : : char *conninfo;
488 : :
489 : : /* Fill publisher attributes */
490 : 20 : conninfo = concat_conninfo_dbname(pub_base_conninfo, cell->val);
491 : 20 : dbinfo[i].pubconninfo = conninfo;
492 : 20 : dbinfo[i].dbname = cell->val;
493 [ + + ]: 20 : if (num_pubs > 0)
494 : 4 : dbinfo[i].pubname = pubcell->val;
495 : : else
496 : 16 : dbinfo[i].pubname = NULL;
497 [ + + ]: 20 : if (num_replslots > 0)
498 : 3 : dbinfo[i].replslotname = replslotcell->val;
499 : : else
500 : 17 : dbinfo[i].replslotname = NULL;
501 : 20 : dbinfo[i].made_replslot = false;
502 : 20 : dbinfo[i].made_publication = false;
503 : : /* Fill subscriber attributes */
504 : 20 : conninfo = concat_conninfo_dbname(sub_base_conninfo, cell->val);
505 : 20 : dbinfo[i].subconninfo = conninfo;
506 [ + + ]: 20 : if (num_subs > 0)
507 : 2 : dbinfo[i].subname = subcell->val;
508 : : else
509 : 18 : dbinfo[i].subname = NULL;
510 : : /* Other fields will be filled later */
511 : :
512 [ + + + - : 20 : pg_log_debug("publisher(%d): publication: %s ; replication slot: %s ; connection string: %s", i,
+ - ]
513 : : dbinfo[i].pubname ? dbinfo[i].pubname : "(auto)",
514 : : dbinfo[i].replslotname ? dbinfo[i].replslotname : "(auto)",
515 : : dbinfo[i].pubconninfo);
293 akapila@postgresql.o 516 [ + + + - : 20 : pg_log_debug("subscriber(%d): subscription: %s ; connection string: %s, two_phase: %s", i,
- + ]
517 : : dbinfo[i].subname ? dbinfo[i].subname : "(auto)",
518 : : dbinfo[i].subconninfo,
519 : : dbinfos.two_phase ? "true" : "false");
520 : :
631 peter@eisentraut.org 521 [ + + ]: 20 : if (num_pubs > 0)
522 : 4 : pubcell = pubcell->next;
523 [ + + ]: 20 : if (num_subs > 0)
524 : 2 : subcell = subcell->next;
525 [ + + ]: 20 : if (num_replslots > 0)
526 : 3 : replslotcell = replslotcell->next;
527 : :
528 : 20 : i++;
529 : : }
530 : :
531 : 10 : return dbinfo;
532 : : }
533 : :
534 : : /*
535 : : * Open a new connection. If exit_on_error is true, it has an undesired
536 : : * condition and it should exit immediately.
537 : : */
538 : : static PGconn *
539 : 57 : connect_database(const char *conninfo, bool exit_on_error)
540 : : {
541 : : PGconn *conn;
542 : : PGresult *res;
543 : :
544 : 57 : conn = PQconnectdb(conninfo);
545 [ - + ]: 57 : if (PQstatus(conn) != CONNECTION_OK)
546 : : {
631 peter@eisentraut.org 547 :UBC 0 : pg_log_error("connection to database failed: %s",
548 : : PQerrorMessage(conn));
624 tgl@sss.pgh.pa.us 549 : 0 : PQfinish(conn);
550 : :
631 peter@eisentraut.org 551 [ # # ]: 0 : if (exit_on_error)
552 : 0 : exit(1);
553 : 0 : return NULL;
554 : : }
555 : :
556 : : /* Secure search_path */
631 peter@eisentraut.org 557 :CBC 57 : res = PQexec(conn, ALWAYS_SECURE_SEARCH_PATH_SQL);
558 [ - + ]: 57 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
559 : : {
468 michael@paquier.xyz 560 :UBC 0 : pg_log_error("could not clear \"search_path\": %s",
561 : : PQresultErrorMessage(res));
624 tgl@sss.pgh.pa.us 562 : 0 : PQclear(res);
563 : 0 : PQfinish(conn);
564 : :
631 peter@eisentraut.org 565 [ # # ]: 0 : if (exit_on_error)
566 : 0 : exit(1);
567 : 0 : return NULL;
568 : : }
631 peter@eisentraut.org 569 :CBC 57 : PQclear(res);
570 : :
571 : 57 : return conn;
572 : : }
573 : :
574 : : /*
575 : : * Close the connection. If exit_on_error is true, it has an undesired
576 : : * condition and it should exit immediately.
577 : : */
578 : : static void
579 : 57 : disconnect_database(PGconn *conn, bool exit_on_error)
580 : : {
581 [ - + ]: 57 : Assert(conn != NULL);
582 : :
583 : 57 : PQfinish(conn);
584 : :
585 [ + + ]: 57 : if (exit_on_error)
586 : 2 : exit(1);
587 : 55 : }
588 : :
589 : : /*
590 : : * Obtain the system identifier using the provided connection. It will be used
591 : : * to compare if a data directory is a clone of another one.
592 : : */
593 : : static uint64
594 : 10 : get_primary_sysid(const char *conninfo)
595 : : {
596 : : PGconn *conn;
597 : : PGresult *res;
598 : : uint64 sysid;
599 : :
600 : 10 : pg_log_info("getting system identifier from publisher");
601 : :
602 : 10 : conn = connect_database(conninfo, true);
603 : :
604 : 10 : res = PQexec(conn, "SELECT system_identifier FROM pg_catalog.pg_control_system()");
605 [ - + ]: 10 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
606 : : {
631 peter@eisentraut.org 607 :UBC 0 : pg_log_error("could not get system identifier: %s",
608 : : PQresultErrorMessage(res));
609 : 0 : disconnect_database(conn, true);
610 : : }
631 peter@eisentraut.org 611 [ - + ]:CBC 10 : if (PQntuples(res) != 1)
612 : : {
631 peter@eisentraut.org 613 :UBC 0 : pg_log_error("could not get system identifier: got %d rows, expected %d row",
614 : : PQntuples(res), 1);
615 : 0 : disconnect_database(conn, true);
616 : : }
617 : :
631 peter@eisentraut.org 618 :CBC 10 : sysid = strtou64(PQgetvalue(res, 0, 0), NULL, 10);
619 : :
262 620 : 10 : pg_log_info("system identifier is %" PRIu64 " on publisher", sysid);
621 : :
631 622 : 10 : PQclear(res);
623 : 10 : disconnect_database(conn, false);
624 : :
625 : 10 : return sysid;
626 : : }
627 : :
628 : : /*
629 : : * Obtain the system identifier from control file. It will be used to compare
630 : : * if a data directory is a clone of another one. This routine is used locally
631 : : * and avoids a connection.
632 : : */
633 : : static uint64
634 : 10 : get_standby_sysid(const char *datadir)
635 : : {
636 : : ControlFileData *cf;
637 : : bool crc_ok;
638 : : uint64 sysid;
639 : :
640 : 10 : pg_log_info("getting system identifier from subscriber");
641 : :
642 : 10 : cf = get_controlfile(datadir, &crc_ok);
643 [ - + ]: 10 : if (!crc_ok)
631 peter@eisentraut.org 644 :UBC 0 : pg_fatal("control file appears to be corrupt");
645 : :
631 peter@eisentraut.org 646 :CBC 10 : sysid = cf->system_identifier;
647 : :
262 648 : 10 : pg_log_info("system identifier is %" PRIu64 " on subscriber", sysid);
649 : :
631 650 : 10 : pg_free(cf);
651 : :
652 : 10 : return sysid;
653 : : }
654 : :
655 : : /*
656 : : * Modify the system identifier. Since a standby server preserves the system
657 : : * identifier, it makes sense to change it to avoid situations in which WAL
658 : : * files from one of the systems might be used in the other one.
659 : : */
660 : : static void
661 : 4 : modify_subscriber_sysid(const struct CreateSubscriberOptions *opt)
662 : : {
663 : : ControlFileData *cf;
664 : : bool crc_ok;
665 : : struct timeval tv;
666 : :
667 : : char *cmd_str;
668 : :
669 : 4 : pg_log_info("modifying system identifier of subscriber");
670 : :
671 : 4 : cf = get_controlfile(subscriber_dir, &crc_ok);
672 [ - + ]: 4 : if (!crc_ok)
631 peter@eisentraut.org 673 :UBC 0 : pg_fatal("control file appears to be corrupt");
674 : :
675 : : /*
676 : : * Select a new system identifier.
677 : : *
678 : : * XXX this code was extracted from BootStrapXLOG().
679 : : */
631 peter@eisentraut.org 680 :CBC 4 : gettimeofday(&tv, NULL);
681 : 4 : cf->system_identifier = ((uint64) tv.tv_sec) << 32;
682 : 4 : cf->system_identifier |= ((uint64) tv.tv_usec) << 12;
683 : 4 : cf->system_identifier |= getpid() & 0xFFF;
684 : :
46 alvherre@kurilemu.de 685 [ + + ]: 4 : if (dry_run)
686 : 3 : pg_log_info("dry-run: would set system identifier to %" PRIu64 " on subscriber",
687 : : cf->system_identifier);
688 : : else
689 : : {
631 peter@eisentraut.org 690 : 1 : update_controlfile(subscriber_dir, cf, true);
46 alvherre@kurilemu.de 691 : 1 : pg_log_info("system identifier is %" PRIu64 " on subscriber",
692 : : cf->system_identifier);
693 : : }
694 : :
695 [ + + ]: 4 : if (dry_run)
696 : 3 : pg_log_info("dry-run: would run pg_resetwal on the subscriber");
697 : : else
698 : 1 : pg_log_info("running pg_resetwal on the subscriber");
699 : :
631 peter@eisentraut.org 700 : 4 : cmd_str = psprintf("\"%s\" -D \"%s\" > \"%s\"", pg_resetwal_path,
701 : : subscriber_dir, DEVNULL);
702 : :
703 [ + + ]: 4 : pg_log_debug("pg_resetwal command is: %s", cmd_str);
704 : :
705 [ + + ]: 4 : if (!dry_run)
706 : : {
707 : 1 : int rc = system(cmd_str);
708 : :
709 [ + - ]: 1 : if (rc == 0)
40 710 : 1 : pg_log_info("successfully reset WAL on the subscriber");
711 : : else
46 alvherre@kurilemu.de 712 :UBC 0 : pg_fatal("could not reset WAL on subscriber: %s", wait_result_to_str(rc));
713 : : }
714 : :
631 peter@eisentraut.org 715 :CBC 4 : pg_free(cf);
716 : 4 : }
717 : :
718 : : /*
719 : : * Generate an object name using a prefix, database oid and a random integer.
720 : : * It is used in case the user does not specify an object name (publication,
721 : : * subscription, replication slot).
722 : : */
723 : : static char *
724 : 8 : generate_object_name(PGconn *conn)
725 : : {
726 : : PGresult *res;
727 : : Oid oid;
728 : : uint32 rand;
729 : : char *objname;
730 : :
731 : 8 : res = PQexec(conn,
732 : : "SELECT oid FROM pg_catalog.pg_database "
733 : : "WHERE datname = pg_catalog.current_database()");
734 [ - + ]: 8 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
735 : : {
631 peter@eisentraut.org 736 :UBC 0 : pg_log_error("could not obtain database OID: %s",
737 : : PQresultErrorMessage(res));
738 : 0 : disconnect_database(conn, true);
739 : : }
740 : :
631 peter@eisentraut.org 741 [ - + ]:CBC 8 : if (PQntuples(res) != 1)
742 : : {
630 peter@eisentraut.org 743 :UBC 0 : pg_log_error("could not obtain database OID: got %d rows, expected %d row",
744 : : PQntuples(res), 1);
631 745 : 0 : disconnect_database(conn, true);
746 : : }
747 : :
748 : : /* Database OID */
631 peter@eisentraut.org 749 :CBC 8 : oid = strtoul(PQgetvalue(res, 0, 0), NULL, 10);
750 : :
751 : 8 : PQclear(res);
752 : :
753 : : /* Random unsigned integer */
754 : 8 : rand = pg_prng_uint32(&prng_state);
755 : :
756 : : /*
757 : : * Build the object name. The name must not exceed NAMEDATALEN - 1. This
758 : : * current schema uses a maximum of 40 characters (20 + 10 + 1 + 8 +
759 : : * '\0').
760 : : */
761 : 8 : objname = psprintf("pg_createsubscriber_%u_%x", oid, rand);
762 : :
763 : 8 : return objname;
764 : : }
765 : :
766 : : /*
767 : : * Create the publications and replication slots in preparation for logical
768 : : * replication. Returns the LSN from latest replication slot. It will be the
769 : : * replication start point that is used to adjust the subscriptions (see
770 : : * set_replication_progress).
771 : : */
772 : : static char *
773 : 4 : setup_publisher(struct LogicalRepInfo *dbinfo)
774 : : {
775 : 4 : char *lsn = NULL;
776 : :
777 : 4 : pg_prng_seed(&prng_state, (uint64) (getpid() ^ time(NULL)));
778 : :
779 [ + + ]: 12 : for (int i = 0; i < num_dbs; i++)
780 : : {
781 : : PGconn *conn;
782 : 8 : char *genname = NULL;
783 : :
784 : 8 : conn = connect_database(dbinfo[i].pubconninfo, true);
785 : :
786 : : /*
787 : : * If an object name was not specified as command-line options, assign
788 : : * a generated object name. The replication slot has a different rule.
789 : : * The subscription name is assigned to the replication slot name if
790 : : * no replication slot is specified. It follows the same rule as
791 : : * CREATE SUBSCRIPTION.
792 : : */
793 [ + + + + : 8 : if (num_pubs == 0 || num_subs == 0 || num_replslots == 0)
+ - ]
794 : 8 : genname = generate_object_name(conn);
795 [ + + ]: 8 : if (num_pubs == 0)
796 : 4 : dbinfo[i].pubname = pg_strdup(genname);
797 [ + + ]: 8 : if (num_subs == 0)
798 : 6 : dbinfo[i].subname = pg_strdup(genname);
799 [ + + ]: 8 : if (num_replslots == 0)
800 : 5 : dbinfo[i].replslotname = pg_strdup(dbinfo[i].subname);
801 : :
802 : : /*
803 : : * Create publication on publisher. This step should be executed
804 : : * *before* promoting the subscriber to avoid any transactions between
805 : : * consistent LSN and the new publication rows (such transactions
806 : : * wouldn't see the new publication rows resulting in an error).
807 : : */
808 : 8 : create_publication(conn, &dbinfo[i]);
809 : :
810 : : /* Create replication slot on publisher */
811 [ + + ]: 8 : if (lsn)
812 : 1 : pg_free(lsn);
813 : 8 : lsn = create_logical_replication_slot(conn, &dbinfo[i]);
68 michael@paquier.xyz 814 [ + + - + ]:GNC 8 : if (lsn == NULL && !dry_run)
631 peter@eisentraut.org 815 :UBC 0 : exit(1);
816 : :
817 : : /*
818 : : * Since we are using the LSN returned by the last replication slot as
819 : : * recovery_target_lsn, this LSN is ahead of the current WAL position
820 : : * and the recovery waits until the publisher writes a WAL record to
821 : : * reach the target and ends the recovery. On idle systems, this wait
822 : : * time is unpredictable and could lead to failure in promoting the
823 : : * subscriber. To avoid that, insert a harmless WAL record.
824 : : */
504 akapila@postgresql.o 825 [ + + + + ]:CBC 8 : if (i == num_dbs - 1 && !dry_run)
826 : : {
827 : : PGresult *res;
828 : :
829 : 1 : res = PQexec(conn, "SELECT pg_log_standby_snapshot()");
830 [ - + ]: 1 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
831 : : {
504 akapila@postgresql.o 832 :UBC 0 : pg_log_error("could not write an additional WAL record: %s",
833 : : PQresultErrorMessage(res));
834 : 0 : disconnect_database(conn, true);
835 : : }
504 akapila@postgresql.o 836 :CBC 1 : PQclear(res);
837 : : }
838 : :
631 peter@eisentraut.org 839 : 8 : disconnect_database(conn, false);
840 : : }
841 : :
842 : 4 : return lsn;
843 : : }
844 : :
845 : : /*
846 : : * Is recovery still in progress?
847 : : */
848 : : static bool
849 : 15 : server_is_in_recovery(PGconn *conn)
850 : : {
851 : : PGresult *res;
852 : : int ret;
853 : :
854 : 15 : res = PQexec(conn, "SELECT pg_catalog.pg_is_in_recovery()");
855 : :
856 [ - + ]: 15 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
857 : : {
631 peter@eisentraut.org 858 :UBC 0 : pg_log_error("could not obtain recovery progress: %s",
859 : : PQresultErrorMessage(res));
860 : 0 : disconnect_database(conn, true);
861 : : }
862 : :
863 : :
631 peter@eisentraut.org 864 :CBC 15 : ret = strcmp("t", PQgetvalue(res, 0, 0));
865 : :
866 : 15 : PQclear(res);
867 : :
868 : 15 : return ret == 0;
869 : : }
870 : :
871 : : /*
872 : : * Is the primary server ready for logical replication?
873 : : *
874 : : * XXX Does it not allow a synchronous replica?
875 : : */
876 : : static void
877 : 6 : check_publisher(const struct LogicalRepInfo *dbinfo)
878 : : {
879 : : PGconn *conn;
880 : : PGresult *res;
881 : 6 : bool failed = false;
882 : :
883 : : char *wal_level;
884 : : int max_repslots;
885 : : int cur_repslots;
886 : : int max_walsenders;
887 : : int cur_walsenders;
888 : : int max_prepared_transactions;
889 : : char *max_slot_wal_keep_size;
890 : :
891 : 6 : pg_log_info("checking settings on publisher");
892 : :
893 : 6 : conn = connect_database(dbinfo[0].pubconninfo, true);
894 : :
895 : : /*
896 : : * If the primary server is in recovery (i.e. cascading replication),
897 : : * objects (publication) cannot be created because it is read only.
898 : : */
899 [ + + ]: 6 : if (server_is_in_recovery(conn))
900 : : {
901 : 1 : pg_log_error("primary server cannot be in recovery");
902 : 1 : disconnect_database(conn, true);
903 : : }
904 : :
905 : : /*------------------------------------------------------------------------
906 : : * Logical replication requires a few parameters to be set on publisher.
907 : : * Since these parameters are not a requirement for physical replication,
908 : : * we should check it to make sure it won't fail.
909 : : *
910 : : * - wal_level = logical
911 : : * - max_replication_slots >= current + number of dbs to be converted
912 : : * - max_wal_senders >= current + number of dbs to be converted
913 : : * - max_slot_wal_keep_size = -1 (to prevent deletion of required WAL files)
914 : : * -----------------------------------------------------------------------
915 : : */
916 : 5 : res = PQexec(conn,
917 : : "SELECT pg_catalog.current_setting('wal_level'),"
918 : : " pg_catalog.current_setting('max_replication_slots'),"
919 : : " (SELECT count(*) FROM pg_catalog.pg_replication_slots),"
920 : : " pg_catalog.current_setting('max_wal_senders'),"
921 : : " (SELECT count(*) FROM pg_catalog.pg_stat_activity WHERE backend_type = 'walsender'),"
922 : : " pg_catalog.current_setting('max_prepared_transactions'),"
923 : : " pg_catalog.current_setting('max_slot_wal_keep_size')");
924 : :
925 [ - + ]: 5 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
926 : : {
631 peter@eisentraut.org 927 :UBC 0 : pg_log_error("could not obtain publisher settings: %s",
928 : : PQresultErrorMessage(res));
929 : 0 : disconnect_database(conn, true);
930 : : }
931 : :
631 peter@eisentraut.org 932 :CBC 5 : wal_level = pg_strdup(PQgetvalue(res, 0, 0));
933 : 5 : max_repslots = atoi(PQgetvalue(res, 0, 1));
934 : 5 : cur_repslots = atoi(PQgetvalue(res, 0, 2));
935 : 5 : max_walsenders = atoi(PQgetvalue(res, 0, 3));
936 : 5 : cur_walsenders = atoi(PQgetvalue(res, 0, 4));
534 tgl@sss.pgh.pa.us 937 : 5 : max_prepared_transactions = atoi(PQgetvalue(res, 0, 5));
301 akapila@postgresql.o 938 : 5 : max_slot_wal_keep_size = pg_strdup(PQgetvalue(res, 0, 6));
939 : :
631 peter@eisentraut.org 940 : 5 : PQclear(res);
941 : :
942 [ + + ]: 5 : pg_log_debug("publisher: wal_level: %s", wal_level);
943 [ + + ]: 5 : pg_log_debug("publisher: max_replication_slots: %d", max_repslots);
944 [ + + ]: 5 : pg_log_debug("publisher: current replication slots: %d", cur_repslots);
945 [ + + ]: 5 : pg_log_debug("publisher: max_wal_senders: %d", max_walsenders);
946 [ + + ]: 5 : pg_log_debug("publisher: current wal senders: %d", cur_walsenders);
534 tgl@sss.pgh.pa.us 947 [ + + ]: 5 : pg_log_debug("publisher: max_prepared_transactions: %d",
948 : : max_prepared_transactions);
301 akapila@postgresql.o 949 [ + + ]: 5 : pg_log_debug("publisher: max_slot_wal_keep_size: %s",
950 : : max_slot_wal_keep_size);
951 : :
631 peter@eisentraut.org 952 : 5 : disconnect_database(conn, false);
953 : :
954 [ + + ]: 5 : if (strcmp(wal_level, "logical") != 0)
955 : : {
468 michael@paquier.xyz 956 : 1 : pg_log_error("publisher requires \"wal_level\" >= \"logical\"");
631 peter@eisentraut.org 957 : 1 : failed = true;
958 : : }
959 : :
960 [ + + ]: 5 : if (max_repslots - cur_repslots < num_dbs)
961 : : {
962 : 1 : pg_log_error("publisher requires %d replication slots, but only %d remain",
963 : : num_dbs, max_repslots - cur_repslots);
508 964 : 1 : pg_log_error_hint("Increase the configuration parameter \"%s\" to at least %d.",
965 : : "max_replication_slots", cur_repslots + num_dbs);
631 966 : 1 : failed = true;
967 : : }
968 : :
969 [ + + ]: 5 : if (max_walsenders - cur_walsenders < num_dbs)
970 : : {
479 971 : 1 : pg_log_error("publisher requires %d WAL sender processes, but only %d remain",
972 : : num_dbs, max_walsenders - cur_walsenders);
508 973 : 1 : pg_log_error_hint("Increase the configuration parameter \"%s\" to at least %d.",
974 : : "max_wal_senders", cur_walsenders + num_dbs);
631 975 : 1 : failed = true;
976 : : }
977 : :
293 akapila@postgresql.o 978 [ - + - - ]: 5 : if (max_prepared_transactions != 0 && !dbinfos.two_phase)
979 : : {
479 peter@eisentraut.org 980 :UBC 0 : pg_log_warning("two_phase option will not be enabled for replication slots");
534 tgl@sss.pgh.pa.us 981 : 0 : pg_log_warning_detail("Subscriptions will be created with the two_phase option disabled. "
982 : : "Prepared transactions will be replicated at COMMIT PREPARED.");
183 peter@eisentraut.org 983 : 0 : pg_log_warning_hint("You can use the command-line option --enable-two-phase to enable two_phase.");
984 : : }
985 : :
986 : : /*
987 : : * In dry-run mode, validate 'max_slot_wal_keep_size'. If this parameter
988 : : * is set to a non-default value, it may cause replication failures due to
989 : : * required WAL files being prematurely removed.
990 : : */
301 akapila@postgresql.o 991 [ + + - + ]:CBC 5 : if (dry_run && (strcmp(max_slot_wal_keep_size, "-1") != 0))
992 : : {
301 akapila@postgresql.o 993 :UBC 0 : pg_log_warning("required WAL could be removed from the publisher");
994 : 0 : pg_log_warning_hint("Set the configuration parameter \"%s\" to -1 to ensure that required WAL files are not prematurely removed.",
995 : : "max_slot_wal_keep_size");
996 : : }
997 : :
624 tgl@sss.pgh.pa.us 998 :CBC 5 : pg_free(wal_level);
999 : :
631 peter@eisentraut.org 1000 [ + + ]: 5 : if (failed)
1001 : 1 : exit(1);
1002 : 4 : }
1003 : :
1004 : : /*
1005 : : * Is the standby server ready for logical replication?
1006 : : *
1007 : : * XXX Does it not allow a time-delayed replica?
1008 : : *
1009 : : * XXX In a cascaded replication scenario (P -> S -> C), if the target server
1010 : : * is S, it cannot detect there is a replica (server C) because server S starts
1011 : : * accepting only local connections and server C cannot connect to it. Hence,
1012 : : * there is not a reliable way to provide a suitable error saying the server C
1013 : : * will be broken at the end of this process (due to pg_resetwal).
1014 : : */
1015 : : static void
1016 : 8 : check_subscriber(const struct LogicalRepInfo *dbinfo)
1017 : : {
1018 : : PGconn *conn;
1019 : : PGresult *res;
1020 : 8 : bool failed = false;
1021 : :
1022 : : int max_lrworkers;
1023 : : int max_reporigins;
1024 : : int max_wprocs;
1025 : :
1026 : 8 : pg_log_info("checking settings on subscriber");
1027 : :
1028 : 8 : conn = connect_database(dbinfo[0].subconninfo, true);
1029 : :
1030 : : /* The target server must be a standby */
1031 [ + + ]: 8 : if (!server_is_in_recovery(conn))
1032 : : {
1033 : 1 : pg_log_error("target server must be a standby");
1034 : 1 : disconnect_database(conn, true);
1035 : : }
1036 : :
1037 : : /*------------------------------------------------------------------------
1038 : : * Logical replication requires a few parameters to be set on subscriber.
1039 : : * Since these parameters are not a requirement for physical replication,
1040 : : * we should check it to make sure it won't fail.
1041 : : *
1042 : : * - max_active_replication_origins >= number of dbs to be converted
1043 : : * - max_logical_replication_workers >= number of dbs to be converted
1044 : : * - max_worker_processes >= 1 + number of dbs to be converted
1045 : : *------------------------------------------------------------------------
1046 : : */
1047 : 7 : res = PQexec(conn,
1048 : : "SELECT setting FROM pg_catalog.pg_settings WHERE name IN ("
1049 : : "'max_logical_replication_workers', "
1050 : : "'max_active_replication_origins', "
1051 : : "'max_worker_processes', "
1052 : : "'primary_slot_name') "
1053 : : "ORDER BY name");
1054 : :
1055 [ - + ]: 7 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
1056 : : {
631 peter@eisentraut.org 1057 :UBC 0 : pg_log_error("could not obtain subscriber settings: %s",
1058 : : PQresultErrorMessage(res));
1059 : 0 : disconnect_database(conn, true);
1060 : : }
1061 : :
270 msawada@postgresql.o 1062 :CBC 7 : max_reporigins = atoi(PQgetvalue(res, 0, 0));
1063 : 7 : max_lrworkers = atoi(PQgetvalue(res, 1, 0));
631 peter@eisentraut.org 1064 : 7 : max_wprocs = atoi(PQgetvalue(res, 2, 0));
1065 [ + + ]: 7 : if (strcmp(PQgetvalue(res, 3, 0), "") != 0)
1066 : 6 : primary_slot_name = pg_strdup(PQgetvalue(res, 3, 0));
1067 : :
1068 [ + + ]: 7 : pg_log_debug("subscriber: max_logical_replication_workers: %d",
1069 : : max_lrworkers);
270 msawada@postgresql.o 1070 [ + + ]: 7 : pg_log_debug("subscriber: max_active_replication_origins: %d", max_reporigins);
631 peter@eisentraut.org 1071 [ + + ]: 7 : pg_log_debug("subscriber: max_worker_processes: %d", max_wprocs);
1072 [ + + ]: 7 : if (primary_slot_name)
1073 [ + + ]: 6 : pg_log_debug("subscriber: primary_slot_name: %s", primary_slot_name);
1074 : :
1075 : 7 : PQclear(res);
1076 : :
1077 : 7 : disconnect_database(conn, false);
1078 : :
270 msawada@postgresql.o 1079 [ + + ]: 7 : if (max_reporigins < num_dbs)
1080 : : {
1081 : 1 : pg_log_error("subscriber requires %d active replication origins, but only %d remain",
1082 : : num_dbs, max_reporigins);
508 peter@eisentraut.org 1083 : 1 : pg_log_error_hint("Increase the configuration parameter \"%s\" to at least %d.",
1084 : : "max_active_replication_origins", num_dbs);
631 1085 : 1 : failed = true;
1086 : : }
1087 : :
1088 [ + + ]: 7 : if (max_lrworkers < num_dbs)
1089 : : {
1090 : 1 : pg_log_error("subscriber requires %d logical replication workers, but only %d remain",
1091 : : num_dbs, max_lrworkers);
508 1092 : 1 : pg_log_error_hint("Increase the configuration parameter \"%s\" to at least %d.",
1093 : : "max_logical_replication_workers", num_dbs);
631 1094 : 1 : failed = true;
1095 : : }
1096 : :
1097 [ + + ]: 7 : if (max_wprocs < num_dbs + 1)
1098 : : {
1099 : 1 : pg_log_error("subscriber requires %d worker processes, but only %d remain",
1100 : : num_dbs + 1, max_wprocs);
508 1101 : 1 : pg_log_error_hint("Increase the configuration parameter \"%s\" to at least %d.",
1102 : : "max_worker_processes", num_dbs + 1);
631 1103 : 1 : failed = true;
1104 : : }
1105 : :
1106 [ + + ]: 7 : if (failed)
1107 : 1 : exit(1);
1108 : 6 : }
1109 : :
1110 : : /*
1111 : : * Drop a specified subscription. This is to avoid duplicate subscriptions on
1112 : : * the primary (publisher node) and the newly created subscriber. We
1113 : : * shouldn't drop the associated slot as that would be used by the publisher
1114 : : * node.
1115 : : */
1116 : : static void
46 alvherre@kurilemu.de 1117 :GNC 4 : drop_existing_subscription(PGconn *conn, const char *subname, const char *dbname)
1118 : : {
532 akapila@postgresql.o 1119 :CBC 4 : PQExpBuffer query = createPQExpBuffer();
1120 : : PGresult *res;
1121 : :
1122 [ - + ]: 4 : Assert(conn != NULL);
1123 : :
1124 : : /*
1125 : : * Construct a query string. These commands are allowed to be executed
1126 : : * within a transaction.
1127 : : */
1128 : 4 : appendPQExpBuffer(query, "ALTER SUBSCRIPTION %s DISABLE;",
1129 : : subname);
1130 : 4 : appendPQExpBuffer(query, " ALTER SUBSCRIPTION %s SET (slot_name = NONE);",
1131 : : subname);
1132 : 4 : appendPQExpBuffer(query, " DROP SUBSCRIPTION %s;", subname);
1133 : :
46 alvherre@kurilemu.de 1134 [ + + ]: 4 : if (dry_run)
1135 : 3 : pg_log_info("dry-run: would drop subscription \"%s\" in database \"%s\"",
1136 : : subname, dbname);
1137 : : else
1138 : : {
1139 : 1 : pg_log_info("dropping subscription \"%s\" in database \"%s\"",
1140 : : subname, dbname);
1141 : :
532 akapila@postgresql.o 1142 : 1 : res = PQexec(conn, query->data);
1143 : :
1144 [ - + ]: 1 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
1145 : : {
501 alvherre@alvh.no-ip. 1146 :UBC 0 : pg_log_error("could not drop subscription \"%s\": %s",
1147 : : subname, PQresultErrorMessage(res));
532 akapila@postgresql.o 1148 : 0 : disconnect_database(conn, true);
1149 : : }
1150 : :
532 akapila@postgresql.o 1151 :CBC 1 : PQclear(res);
1152 : : }
1153 : :
1154 : 4 : destroyPQExpBuffer(query);
1155 : 4 : }
1156 : :
1157 : : /*
1158 : : * Retrieve and drop the pre-existing subscriptions.
1159 : : */
1160 : : static void
1161 : 8 : check_and_drop_existing_subscriptions(PGconn *conn,
1162 : : const struct LogicalRepInfo *dbinfo)
1163 : : {
1164 : 8 : PQExpBuffer query = createPQExpBuffer();
1165 : : char *dbname;
1166 : : PGresult *res;
1167 : :
1168 [ - + ]: 8 : Assert(conn != NULL);
1169 : :
1170 : 8 : dbname = PQescapeLiteral(conn, dbinfo->dbname, strlen(dbinfo->dbname));
1171 : :
1172 : 8 : appendPQExpBuffer(query,
1173 : : "SELECT s.subname FROM pg_catalog.pg_subscription s "
1174 : : "INNER JOIN pg_catalog.pg_database d ON (s.subdbid = d.oid) "
1175 : : "WHERE d.datname = %s",
1176 : : dbname);
1177 : 8 : res = PQexec(conn, query->data);
1178 : :
1179 [ - + ]: 8 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
1180 : : {
532 akapila@postgresql.o 1181 :UBC 0 : pg_log_error("could not obtain pre-existing subscriptions: %s",
1182 : : PQresultErrorMessage(res));
1183 : 0 : disconnect_database(conn, true);
1184 : : }
1185 : :
532 akapila@postgresql.o 1186 [ + + ]:CBC 12 : for (int i = 0; i < PQntuples(res); i++)
46 alvherre@kurilemu.de 1187 :GNC 4 : drop_existing_subscription(conn, PQgetvalue(res, i, 0),
1188 : 4 : dbinfo->dbname);
1189 : :
532 akapila@postgresql.o 1190 :CBC 8 : PQclear(res);
1191 : 8 : destroyPQExpBuffer(query);
307 michael@paquier.xyz 1192 : 8 : PQfreemem(dbname);
532 akapila@postgresql.o 1193 : 8 : }
1194 : :
1195 : : /*
1196 : : * Create the subscriptions, adjust the initial location for logical
1197 : : * replication and enable the subscriptions. That's the last step for logical
1198 : : * replication setup.
1199 : : */
1200 : : static void
631 peter@eisentraut.org 1201 : 4 : setup_subscriber(struct LogicalRepInfo *dbinfo, const char *consistent_lsn)
1202 : : {
1203 [ + + ]: 12 : for (int i = 0; i < num_dbs; i++)
1204 : : {
1205 : : PGconn *conn;
1206 : :
1207 : : /* Connect to subscriber. */
1208 : 8 : conn = connect_database(dbinfo[i].subconninfo, true);
1209 : :
1210 : : /*
1211 : : * We don't need the pre-existing subscriptions on the newly formed
1212 : : * subscriber. They can connect to other publisher nodes and either
1213 : : * get some unwarranted data or can lead to ERRORs in connecting to
1214 : : * such nodes.
1215 : : */
532 akapila@postgresql.o 1216 : 8 : check_and_drop_existing_subscriptions(conn, &dbinfo[i]);
1217 : :
1218 : : /* Check and drop the required publications in the given database. */
271 1219 : 8 : check_and_drop_publications(conn, &dbinfo[i]);
1220 : :
631 peter@eisentraut.org 1221 : 8 : create_subscription(conn, &dbinfo[i]);
1222 : :
1223 : : /* Set the replication progress to the correct LSN */
1224 : 8 : set_replication_progress(conn, &dbinfo[i], consistent_lsn);
1225 : :
1226 : : /* Enable subscription */
1227 : 8 : enable_subscription(conn, &dbinfo[i]);
1228 : :
1229 : 8 : disconnect_database(conn, false);
1230 : : }
1231 : 4 : }
1232 : :
1233 : : /*
1234 : : * Write the required recovery parameters.
1235 : : */
1236 : : static void
1237 : 4 : setup_recovery(const struct LogicalRepInfo *dbinfo, const char *datadir, const char *lsn)
1238 : : {
1239 : : PGconn *conn;
1240 : : PQExpBuffer recoveryconfcontents;
1241 : :
1242 : : /*
1243 : : * Despite of the recovery parameters will be written to the subscriber,
1244 : : * use a publisher connection. The primary_conninfo is generated using the
1245 : : * connection settings.
1246 : : */
1247 : 4 : conn = connect_database(dbinfo[0].pubconninfo, true);
1248 : :
1249 : : /*
1250 : : * Write recovery parameters.
1251 : : *
1252 : : * The subscriber is not running yet. In dry run mode, the recovery
1253 : : * parameters *won't* be written. An invalid LSN is used for printing
1254 : : * purposes. Additional recovery parameters are added here. It avoids
1255 : : * unexpected behavior such as end of recovery as soon as a consistent
1256 : : * state is reached (recovery_target) and failure due to multiple recovery
1257 : : * targets (name, time, xid, LSN).
1258 : : */
1259 : 4 : recoveryconfcontents = GenerateRecoveryConfig(conn, NULL, NULL);
242 drowley@postgresql.o 1260 : 4 : appendPQExpBufferStr(recoveryconfcontents, "recovery_target = ''\n");
1261 : 4 : appendPQExpBufferStr(recoveryconfcontents,
1262 : : "recovery_target_timeline = 'latest'\n");
1263 : :
1264 : : /*
1265 : : * Set recovery_target_inclusive = false to avoid reapplying the
1266 : : * transaction committed at 'lsn' after subscription is enabled. This is
1267 : : * because the provided 'lsn' is also used as the replication start point
1268 : : * for the subscription. So, the server can send the transaction committed
1269 : : * at that 'lsn' after replication is started which can lead to applying
1270 : : * the same transaction twice if we keep recovery_target_inclusive = true.
1271 : : */
1272 : 4 : appendPQExpBufferStr(recoveryconfcontents,
1273 : : "recovery_target_inclusive = false\n");
1274 : 4 : appendPQExpBufferStr(recoveryconfcontents,
1275 : : "recovery_target_action = promote\n");
1276 : 4 : appendPQExpBufferStr(recoveryconfcontents, "recovery_target_name = ''\n");
1277 : 4 : appendPQExpBufferStr(recoveryconfcontents, "recovery_target_time = ''\n");
1278 : 4 : appendPQExpBufferStr(recoveryconfcontents, "recovery_target_xid = ''\n");
1279 : :
631 peter@eisentraut.org 1280 [ + + ]: 4 : if (dry_run)
1281 : : {
46 alvherre@kurilemu.de 1282 : 3 : appendPQExpBufferStr(recoveryconfcontents, "# dry run mode\n");
631 peter@eisentraut.org 1283 : 3 : appendPQExpBuffer(recoveryconfcontents,
1284 : : "recovery_target_lsn = '%X/%08X'\n",
631 peter@eisentraut.org 1285 :ECB (3) : LSN_FORMAT_ARGS((XLogRecPtr) InvalidXLogRecPtr));
1286 : : }
1287 : : else
1288 : : {
631 peter@eisentraut.org 1289 :CBC 1 : appendPQExpBuffer(recoveryconfcontents, "recovery_target_lsn = '%s'\n",
1290 : : lsn);
1291 : 1 : WriteRecoveryConfig(conn, datadir, recoveryconfcontents);
1292 : : }
1293 : 4 : disconnect_database(conn, false);
1294 : :
1295 [ + + ]: 4 : pg_log_debug("recovery parameters:\n%s", recoveryconfcontents->data);
1296 : 4 : }
1297 : :
1298 : : /*
1299 : : * Drop physical replication slot on primary if the standby was using it. After
1300 : : * the transformation, it has no use.
1301 : : *
1302 : : * XXX we might not fail here. Instead, we provide a warning so the user
1303 : : * eventually drops this replication slot later.
1304 : : */
1305 : : static void
1306 : 4 : drop_primary_replication_slot(struct LogicalRepInfo *dbinfo, const char *slotname)
1307 : : {
1308 : : PGconn *conn;
1309 : :
1310 : : /* Replication slot does not exist, do nothing */
1311 [ - + ]: 4 : if (!primary_slot_name)
631 peter@eisentraut.org 1312 :UBC 0 : return;
1313 : :
631 peter@eisentraut.org 1314 :CBC 4 : conn = connect_database(dbinfo[0].pubconninfo, false);
1315 [ + - ]: 4 : if (conn != NULL)
1316 : : {
1317 : 4 : drop_replication_slot(conn, &dbinfo[0], slotname);
1318 : 4 : disconnect_database(conn, false);
1319 : : }
1320 : : else
1321 : : {
631 peter@eisentraut.org 1322 :UBC 0 : pg_log_warning("could not drop replication slot \"%s\" on primary",
1323 : : slotname);
1324 : 0 : pg_log_warning_hint("Drop this replication slot soon to avoid retention of WAL files.");
1325 : : }
1326 : : }
1327 : :
1328 : : /*
1329 : : * Drop failover replication slots on subscriber. After the transformation,
1330 : : * they have no use.
1331 : : *
1332 : : * XXX We do not fail here. Instead, we provide a warning so the user can drop
1333 : : * them later.
1334 : : */
1335 : : static void
547 peter@eisentraut.org 1336 :CBC 4 : drop_failover_replication_slots(struct LogicalRepInfo *dbinfo)
1337 : : {
1338 : : PGconn *conn;
1339 : : PGresult *res;
1340 : :
1341 : 4 : conn = connect_database(dbinfo[0].subconninfo, false);
1342 [ + - ]: 4 : if (conn != NULL)
1343 : : {
1344 : : /* Get failover replication slot names */
1345 : 4 : res = PQexec(conn,
1346 : : "SELECT slot_name FROM pg_catalog.pg_replication_slots WHERE failover");
1347 : :
1348 [ + - ]: 4 : if (PQresultStatus(res) == PGRES_TUPLES_OK)
1349 : : {
1350 : : /* Remove failover replication slots from subscriber */
1351 [ + + ]: 8 : for (int i = 0; i < PQntuples(res); i++)
1352 : 4 : drop_replication_slot(conn, &dbinfo[0], PQgetvalue(res, i, 0));
1353 : : }
1354 : : else
1355 : : {
547 peter@eisentraut.org 1356 :UBC 0 : pg_log_warning("could not obtain failover replication slot information: %s",
1357 : : PQresultErrorMessage(res));
1358 : 0 : pg_log_warning_hint("Drop the failover replication slots on subscriber soon to avoid retention of WAL files.");
1359 : : }
1360 : :
547 peter@eisentraut.org 1361 :CBC 4 : PQclear(res);
1362 : 4 : disconnect_database(conn, false);
1363 : : }
1364 : : else
1365 : : {
547 peter@eisentraut.org 1366 :UBC 0 : pg_log_warning("could not drop failover replication slot");
1367 : 0 : pg_log_warning_hint("Drop the failover replication slots on subscriber soon to avoid retention of WAL files.");
1368 : : }
547 peter@eisentraut.org 1369 :CBC 4 : }
1370 : :
1371 : : /*
1372 : : * Create a logical replication slot and returns a LSN.
1373 : : *
1374 : : * CreateReplicationSlot() is not used because it does not provide the one-row
1375 : : * result set that contains the LSN.
1376 : : */
1377 : : static char *
631 1378 : 8 : create_logical_replication_slot(PGconn *conn, struct LogicalRepInfo *dbinfo)
1379 : : {
1380 : 8 : PQExpBuffer str = createPQExpBuffer();
1381 : 8 : PGresult *res = NULL;
1382 : 8 : const char *slot_name = dbinfo->replslotname;
1383 : : char *slot_name_esc;
1384 : 8 : char *lsn = NULL;
1385 : :
1386 [ - + ]: 8 : Assert(conn != NULL);
1387 : :
46 alvherre@kurilemu.de 1388 [ + + ]: 8 : if (dry_run)
1389 : 6 : pg_log_info("dry-run: would create the replication slot \"%s\" in database \"%s\" on publisher",
1390 : : slot_name, dbinfo->dbname);
1391 : : else
1392 : 2 : pg_log_info("creating the replication slot \"%s\" in database \"%s\" on publisher",
1393 : : slot_name, dbinfo->dbname);
1394 : :
631 peter@eisentraut.org 1395 : 8 : slot_name_esc = PQescapeLiteral(conn, slot_name, strlen(slot_name));
1396 : :
1397 : 8 : appendPQExpBuffer(str,
1398 : : "SELECT lsn FROM pg_catalog.pg_create_logical_replication_slot(%s, 'pgoutput', false, %s, false)",
1399 : : slot_name_esc,
293 akapila@postgresql.o 1400 [ + + ]: 8 : dbinfos.two_phase ? "true" : "false");
1401 : :
307 michael@paquier.xyz 1402 : 8 : PQfreemem(slot_name_esc);
1403 : :
631 peter@eisentraut.org 1404 [ + + ]: 8 : pg_log_debug("command is: %s", str->data);
1405 : :
1406 [ + + ]: 8 : if (!dry_run)
1407 : : {
1408 : 2 : res = PQexec(conn, str->data);
1409 [ - + ]: 2 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
1410 : : {
509 peter@eisentraut.org 1411 :UBC 0 : pg_log_error("could not create replication slot \"%s\" in database \"%s\": %s",
1412 : : slot_name, dbinfo->dbname,
1413 : : PQresultErrorMessage(res));
624 tgl@sss.pgh.pa.us 1414 : 0 : PQclear(res);
1415 : 0 : destroyPQExpBuffer(str);
631 peter@eisentraut.org 1416 : 0 : return NULL;
1417 : : }
1418 : :
631 peter@eisentraut.org 1419 :CBC 2 : lsn = pg_strdup(PQgetvalue(res, 0, 0));
1420 : 2 : PQclear(res);
1421 : : }
1422 : :
1423 : : /* For cleanup purposes */
1424 : 8 : dbinfo->made_replslot = true;
1425 : :
1426 : 8 : destroyPQExpBuffer(str);
1427 : :
1428 : 8 : return lsn;
1429 : : }
1430 : :
1431 : : static void
1432 : 8 : drop_replication_slot(PGconn *conn, struct LogicalRepInfo *dbinfo,
1433 : : const char *slot_name)
1434 : : {
1435 : 8 : PQExpBuffer str = createPQExpBuffer();
1436 : : char *slot_name_esc;
1437 : : PGresult *res;
1438 : :
1439 [ - + ]: 8 : Assert(conn != NULL);
1440 : :
46 alvherre@kurilemu.de 1441 [ + + ]: 8 : if (dry_run)
1442 : 6 : pg_log_info("dry-run: would drop the replication slot \"%s\" in database \"%s\"",
1443 : : slot_name, dbinfo->dbname);
1444 : : else
1445 : 2 : pg_log_info("dropping the replication slot \"%s\" in database \"%s\"",
1446 : : slot_name, dbinfo->dbname);
1447 : :
631 peter@eisentraut.org 1448 : 8 : slot_name_esc = PQescapeLiteral(conn, slot_name, strlen(slot_name));
1449 : :
1450 : 8 : appendPQExpBuffer(str, "SELECT pg_catalog.pg_drop_replication_slot(%s)", slot_name_esc);
1451 : :
307 michael@paquier.xyz 1452 : 8 : PQfreemem(slot_name_esc);
1453 : :
631 peter@eisentraut.org 1454 [ + + ]: 8 : pg_log_debug("command is: %s", str->data);
1455 : :
1456 [ + + ]: 8 : if (!dry_run)
1457 : : {
1458 : 2 : res = PQexec(conn, str->data);
1459 [ - + ]: 2 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
1460 : : {
509 peter@eisentraut.org 1461 :UBC 0 : pg_log_error("could not drop replication slot \"%s\" in database \"%s\": %s",
1462 : : slot_name, dbinfo->dbname, PQresultErrorMessage(res));
631 1463 : 0 : dbinfo->made_replslot = false; /* don't try again. */
1464 : : }
1465 : :
631 peter@eisentraut.org 1466 :CBC 2 : PQclear(res);
1467 : : }
1468 : :
1469 : 8 : destroyPQExpBuffer(str);
1470 : 8 : }
1471 : :
1472 : : /*
1473 : : * Reports a suitable message if pg_ctl fails.
1474 : : */
1475 : : static void
1476 : 24 : pg_ctl_status(const char *pg_ctl_cmd, int rc)
1477 : : {
1478 [ - + ]: 24 : if (rc != 0)
1479 : : {
631 peter@eisentraut.org 1480 [ # # ]:UBC 0 : if (WIFEXITED(rc))
1481 : : {
1482 : 0 : pg_log_error("pg_ctl failed with exit code %d", WEXITSTATUS(rc));
1483 : : }
1484 [ # # ]: 0 : else if (WIFSIGNALED(rc))
1485 : : {
1486 : : #if defined(WIN32)
1487 : : pg_log_error("pg_ctl was terminated by exception 0x%X",
1488 : : WTERMSIG(rc));
1489 : : pg_log_error_detail("See C include file \"ntstatus.h\" for a description of the hexadecimal value.");
1490 : : #else
1491 : 0 : pg_log_error("pg_ctl was terminated by signal %d: %s",
1492 : : WTERMSIG(rc), pg_strsignal(WTERMSIG(rc)));
1493 : : #endif
1494 : : }
1495 : : else
1496 : : {
1497 : 0 : pg_log_error("pg_ctl exited with unrecognized status %d", rc);
1498 : : }
1499 : :
1500 : 0 : pg_log_error_detail("The failed command was: %s", pg_ctl_cmd);
1501 : 0 : exit(1);
1502 : : }
631 peter@eisentraut.org 1503 :CBC 24 : }
1504 : :
1505 : : static void
532 akapila@postgresql.o 1506 : 12 : start_standby_server(const struct CreateSubscriberOptions *opt, bool restricted_access,
1507 : : bool restrict_logical_worker)
1508 : : {
631 peter@eisentraut.org 1509 : 12 : PQExpBuffer pg_ctl_cmd = createPQExpBuffer();
1510 : : int rc;
1511 : :
534 tgl@sss.pgh.pa.us 1512 : 12 : appendPQExpBuffer(pg_ctl_cmd, "\"%s\" start -D ", pg_ctl_path);
1513 : 12 : appendShellString(pg_ctl_cmd, subscriber_dir);
242 drowley@postgresql.o 1514 : 12 : appendPQExpBufferStr(pg_ctl_cmd, " -s -o \"-c sync_replication_slots=off\"");
1515 : :
1516 : : /* Prevent unintended slot invalidation */
243 1517 : 12 : appendPQExpBufferStr(pg_ctl_cmd, " -o \"-c idle_replication_slot_timeout=0\"");
1518 : :
631 peter@eisentraut.org 1519 [ + - ]: 12 : if (restricted_access)
1520 : : {
1521 : 12 : appendPQExpBuffer(pg_ctl_cmd, " -o \"-p %s\"", opt->sub_port);
1522 : : #if !defined(WIN32)
1523 : :
1524 : : /*
1525 : : * An empty listen_addresses list means the server does not listen on
1526 : : * any IP interfaces; only Unix-domain sockets can be used to connect
1527 : : * to the server. Prevent external connections to minimize the chance
1528 : : * of failure.
1529 : : */
1530 : 12 : appendPQExpBufferStr(pg_ctl_cmd, " -o \"-c listen_addresses='' -c unix_socket_permissions=0700");
1531 [ + - ]: 12 : if (opt->socket_dir)
1532 : 12 : appendPQExpBuffer(pg_ctl_cmd, " -c unix_socket_directories='%s'",
1533 : 12 : opt->socket_dir);
1534 : 12 : appendPQExpBufferChar(pg_ctl_cmd, '"');
1535 : : #endif
1536 : : }
1537 [ - + ]: 12 : if (opt->config_file != NULL)
631 peter@eisentraut.org 1538 :UBC 0 : appendPQExpBuffer(pg_ctl_cmd, " -o \"-c config_file=%s\"",
1539 : 0 : opt->config_file);
1540 : :
1541 : : /* Suppress to start logical replication if requested */
532 akapila@postgresql.o 1542 [ + + ]:CBC 12 : if (restrict_logical_worker)
243 drowley@postgresql.o 1543 : 4 : appendPQExpBufferStr(pg_ctl_cmd, " -o \"-c max_logical_replication_workers=0\"");
1544 : :
631 peter@eisentraut.org 1545 [ + + ]: 12 : pg_log_debug("pg_ctl command is: %s", pg_ctl_cmd->data);
1546 : 12 : rc = system(pg_ctl_cmd->data);
1547 : 12 : pg_ctl_status(pg_ctl_cmd->data, rc);
1548 : 12 : standby_running = true;
1549 : 12 : destroyPQExpBuffer(pg_ctl_cmd);
1550 : 12 : pg_log_info("server was started");
1551 : 12 : }
1552 : :
1553 : : static void
1554 : 12 : stop_standby_server(const char *datadir)
1555 : : {
1556 : : char *pg_ctl_cmd;
1557 : : int rc;
1558 : :
1559 : 12 : pg_ctl_cmd = psprintf("\"%s\" stop -D \"%s\" -s", pg_ctl_path,
1560 : : datadir);
1561 [ + + ]: 12 : pg_log_debug("pg_ctl command is: %s", pg_ctl_cmd);
1562 : 12 : rc = system(pg_ctl_cmd);
1563 : 12 : pg_ctl_status(pg_ctl_cmd, rc);
1564 : 12 : standby_running = false;
1565 : 12 : pg_log_info("server was stopped");
1566 : 12 : }
1567 : :
1568 : : /*
1569 : : * Returns after the server finishes the recovery process.
1570 : : *
1571 : : * If recovery_timeout option is set, terminate abnormally without finishing
1572 : : * the recovery process. By default, it waits forever.
1573 : : *
1574 : : * XXX Is the recovery process still in progress? When recovery process has a
1575 : : * better progress reporting mechanism, it should be added here.
1576 : : */
1577 : : static void
1578 : 4 : wait_for_end_recovery(const char *conninfo, const struct CreateSubscriberOptions *opt)
1579 : : {
1580 : : PGconn *conn;
43 alvherre@kurilemu.de 1581 :GNC 4 : bool ready = false;
631 peter@eisentraut.org 1582 :CBC 4 : int timer = 0;
1583 : :
1584 : 4 : pg_log_info("waiting for the target server to reach the consistent state");
1585 : :
1586 : 4 : conn = connect_database(conninfo, true);
1587 : :
1588 : : for (;;)
1589 : : {
1590 : : /* Did the recovery process finish? We're done if so. */
46 alvherre@kurilemu.de 1591 [ + + + - ]: 4 : if (dry_run || !server_is_in_recovery(conn))
1592 : : {
43 alvherre@kurilemu.de 1593 :GNC 4 : ready = true;
631 peter@eisentraut.org 1594 :CBC 4 : recovery_ended = true;
1595 : 4 : break;
1596 : : }
1597 : :
1598 : : /* Bail out after recovery_timeout seconds if this option is set */
631 peter@eisentraut.org 1599 [ # # # # ]:UBC 0 : if (opt->recovery_timeout > 0 && timer >= opt->recovery_timeout)
1600 : : {
1601 : 0 : stop_standby_server(subscriber_dir);
1602 : 0 : pg_log_error("recovery timed out");
1603 : 0 : disconnect_database(conn, true);
1604 : : }
1605 : :
1606 : : /* Keep waiting */
42 alvherre@kurilemu.de 1607 :UNC 0 : pg_usleep(WAIT_INTERVAL * USECS_PER_SEC);
631 peter@eisentraut.org 1608 :UBC 0 : timer += WAIT_INTERVAL;
1609 : : }
1610 : :
631 peter@eisentraut.org 1611 :CBC 4 : disconnect_database(conn, false);
1612 : :
43 alvherre@kurilemu.de 1613 [ - + ]:GNC 4 : if (!ready)
631 peter@eisentraut.org 1614 :UBC 0 : pg_fatal("server did not end recovery");
1615 : :
631 peter@eisentraut.org 1616 :CBC 4 : pg_log_info("target server reached the consistent state");
1617 : 4 : pg_log_info_hint("If pg_createsubscriber fails after this point, you must recreate the physical replica before continuing.");
1618 : 4 : }
1619 : :
1620 : : /*
1621 : : * Create a publication that includes all tables in the database.
1622 : : */
1623 : : static void
1624 : 8 : create_publication(PGconn *conn, struct LogicalRepInfo *dbinfo)
1625 : : {
1626 : 8 : PQExpBuffer str = createPQExpBuffer();
1627 : : PGresult *res;
1628 : : char *ipubname_esc;
1629 : : char *spubname_esc;
1630 : :
1631 [ - + ]: 8 : Assert(conn != NULL);
1632 : :
1633 : 8 : ipubname_esc = PQescapeIdentifier(conn, dbinfo->pubname, strlen(dbinfo->pubname));
1634 : 8 : spubname_esc = PQescapeLiteral(conn, dbinfo->pubname, strlen(dbinfo->pubname));
1635 : :
1636 : : /* Check if the publication already exists */
1637 : 8 : appendPQExpBuffer(str,
1638 : : "SELECT 1 FROM pg_catalog.pg_publication "
1639 : : "WHERE pubname = %s",
1640 : : spubname_esc);
1641 : 8 : res = PQexec(conn, str->data);
1642 [ - + ]: 8 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
1643 : : {
631 peter@eisentraut.org 1644 :UBC 0 : pg_log_error("could not obtain publication information: %s",
1645 : : PQresultErrorMessage(res));
1646 : 0 : disconnect_database(conn, true);
1647 : : }
1648 : :
631 peter@eisentraut.org 1649 [ - + ]:CBC 8 : if (PQntuples(res) == 1)
1650 : : {
1651 : : /*
1652 : : * Unfortunately, if it reaches this code path, it will always fail
1653 : : * (unless you decide to change the existing publication name). That's
1654 : : * bad but it is very unlikely that the user will choose a name with
1655 : : * pg_createsubscriber_ prefix followed by the exact database oid and
1656 : : * a random number.
1657 : : */
631 peter@eisentraut.org 1658 :UBC 0 : pg_log_error("publication \"%s\" already exists", dbinfo->pubname);
1659 : 0 : pg_log_error_hint("Consider renaming this publication before continuing.");
1660 : 0 : disconnect_database(conn, true);
1661 : : }
1662 : :
631 peter@eisentraut.org 1663 :CBC 8 : PQclear(res);
1664 : 8 : resetPQExpBuffer(str);
1665 : :
46 alvherre@kurilemu.de 1666 [ + + ]: 8 : if (dry_run)
1667 : 6 : pg_log_info("dry-run: would create publication \"%s\" in database \"%s\"",
1668 : : dbinfo->pubname, dbinfo->dbname);
1669 : : else
1670 : 2 : pg_log_info("creating publication \"%s\" in database \"%s\"",
1671 : : dbinfo->pubname, dbinfo->dbname);
1672 : :
631 peter@eisentraut.org 1673 : 8 : appendPQExpBuffer(str, "CREATE PUBLICATION %s FOR ALL TABLES",
1674 : : ipubname_esc);
1675 : :
1676 [ + + ]: 8 : pg_log_debug("command is: %s", str->data);
1677 : :
1678 [ + + ]: 8 : if (!dry_run)
1679 : : {
1680 : 2 : res = PQexec(conn, str->data);
1681 [ - + ]: 2 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
1682 : : {
509 peter@eisentraut.org 1683 :UBC 0 : pg_log_error("could not create publication \"%s\" in database \"%s\": %s",
1684 : : dbinfo->pubname, dbinfo->dbname, PQresultErrorMessage(res));
631 1685 : 0 : disconnect_database(conn, true);
1686 : : }
631 peter@eisentraut.org 1687 :CBC 2 : PQclear(res);
1688 : : }
1689 : :
1690 : : /* For cleanup purposes */
1691 : 8 : dbinfo->made_publication = true;
1692 : :
307 michael@paquier.xyz 1693 : 8 : PQfreemem(ipubname_esc);
1694 : 8 : PQfreemem(spubname_esc);
631 peter@eisentraut.org 1695 : 8 : destroyPQExpBuffer(str);
1696 : 8 : }
1697 : :
1698 : : /*
1699 : : * Drop the specified publication in the given database.
1700 : : */
1701 : : static void
271 akapila@postgresql.o 1702 : 10 : drop_publication(PGconn *conn, const char *pubname, const char *dbname,
1703 : : bool *made_publication)
1704 : : {
631 peter@eisentraut.org 1705 : 10 : PQExpBuffer str = createPQExpBuffer();
1706 : : PGresult *res;
1707 : : char *pubname_esc;
1708 : :
1709 [ - + ]: 10 : Assert(conn != NULL);
1710 : :
271 akapila@postgresql.o 1711 : 10 : pubname_esc = PQescapeIdentifier(conn, pubname, strlen(pubname));
1712 : :
46 alvherre@kurilemu.de 1713 [ + + ]: 10 : if (dry_run)
1714 : 6 : pg_log_info("dry-run: would drop publication \"%s\" in database \"%s\"",
1715 : : pubname, dbname);
1716 : : else
1717 : 4 : pg_log_info("dropping publication \"%s\" in database \"%s\"",
1718 : : pubname, dbname);
1719 : :
631 peter@eisentraut.org 1720 : 10 : appendPQExpBuffer(str, "DROP PUBLICATION %s", pubname_esc);
1721 : :
307 michael@paquier.xyz 1722 : 10 : PQfreemem(pubname_esc);
1723 : :
631 peter@eisentraut.org 1724 [ + + ]: 10 : pg_log_debug("command is: %s", str->data);
1725 : :
1726 [ + + ]: 10 : if (!dry_run)
1727 : : {
1728 : 4 : res = PQexec(conn, str->data);
1729 [ - + ]: 4 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
1730 : : {
509 peter@eisentraut.org 1731 :UBC 0 : pg_log_error("could not drop publication \"%s\" in database \"%s\": %s",
1732 : : pubname, dbname, PQresultErrorMessage(res));
271 akapila@postgresql.o 1733 : 0 : *made_publication = false; /* don't try again. */
1734 : :
1735 : : /*
1736 : : * Don't disconnect and exit here. This routine is used by primary
1737 : : * (cleanup publication / replication slot due to an error) and
1738 : : * subscriber (remove the replicated publications). In both cases,
1739 : : * it can continue and provide instructions for the user to remove
1740 : : * it later if cleanup fails.
1741 : : */
1742 : : }
631 peter@eisentraut.org 1743 :CBC 4 : PQclear(res);
1744 : : }
1745 : :
1746 : 10 : destroyPQExpBuffer(str);
1747 : 10 : }
1748 : :
1749 : : /*
1750 : : * Retrieve and drop the publications.
1751 : : *
1752 : : * Since the publications were created before the consistent LSN, they
1753 : : * remain on the subscriber even after the physical replica is
1754 : : * promoted. Remove these publications from the subscriber because
1755 : : * they have no use. Additionally, if requested, drop all pre-existing
1756 : : * publications.
1757 : : */
1758 : : static void
271 akapila@postgresql.o 1759 : 8 : check_and_drop_publications(PGconn *conn, struct LogicalRepInfo *dbinfo)
1760 : : {
1761 : : PGresult *res;
174 peter@eisentraut.org 1762 : 8 : bool drop_all_pubs = dbinfos.objecttypes_to_clean & OBJECTTYPE_PUBLICATIONS;
1763 : :
271 akapila@postgresql.o 1764 [ - + ]: 8 : Assert(conn != NULL);
1765 : :
1766 [ + + ]: 8 : if (drop_all_pubs)
1767 : : {
1768 : 2 : pg_log_info("dropping all existing publications in database \"%s\"",
1769 : : dbinfo->dbname);
1770 : :
1771 : : /* Fetch all publication names */
1772 : 2 : res = PQexec(conn, "SELECT pubname FROM pg_catalog.pg_publication;");
1773 [ - + ]: 2 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
1774 : : {
271 akapila@postgresql.o 1775 :UBC 0 : pg_log_error("could not obtain publication information: %s",
1776 : : PQresultErrorMessage(res));
1777 : 0 : PQclear(res);
1778 : 0 : disconnect_database(conn, true);
1779 : : }
1780 : :
1781 : : /* Drop each publication */
63 msawada@postgresql.o 1782 [ + + ]:CBC 6 : for (int i = 0; i < PQntuples(res); i++)
1783 : 4 : drop_publication(conn, PQgetvalue(res, i, 0), dbinfo->dbname,
1784 : : &dbinfo->made_publication);
1785 : :
271 akapila@postgresql.o 1786 : 2 : PQclear(res);
1787 : : }
1788 : :
1789 : : /*
1790 : : * In dry-run mode, we don't create publications, but we still try to drop
1791 : : * those to provide necessary information to the user.
1792 : : */
1793 [ + + - + ]: 8 : if (!drop_all_pubs || dry_run)
1794 : 6 : drop_publication(conn, dbinfo->pubname, dbinfo->dbname,
1795 : : &dbinfo->made_publication);
1796 : 8 : }
1797 : :
1798 : : /*
1799 : : * Create a subscription with some predefined options.
1800 : : *
1801 : : * A replication slot was already created in a previous step. Let's use it. It
1802 : : * is not required to copy data. The subscription will be created but it will
1803 : : * not be enabled now. That's because the replication progress must be set and
1804 : : * the replication origin name (one of the function arguments) contains the
1805 : : * subscription OID in its name. Once the subscription is created,
1806 : : * set_replication_progress() can obtain the chosen origin name and set up its
1807 : : * initial location.
1808 : : */
1809 : : static void
631 peter@eisentraut.org 1810 : 8 : create_subscription(PGconn *conn, const struct LogicalRepInfo *dbinfo)
1811 : : {
1812 : 8 : PQExpBuffer str = createPQExpBuffer();
1813 : : PGresult *res;
1814 : : char *pubname_esc;
1815 : : char *subname_esc;
1816 : : char *pubconninfo_esc;
1817 : : char *replslotname_esc;
1818 : :
1819 [ - + ]: 8 : Assert(conn != NULL);
1820 : :
1821 : 8 : pubname_esc = PQescapeIdentifier(conn, dbinfo->pubname, strlen(dbinfo->pubname));
1822 : 8 : subname_esc = PQescapeIdentifier(conn, dbinfo->subname, strlen(dbinfo->subname));
1823 : 8 : pubconninfo_esc = PQescapeLiteral(conn, dbinfo->pubconninfo, strlen(dbinfo->pubconninfo));
1824 : 8 : replslotname_esc = PQescapeLiteral(conn, dbinfo->replslotname, strlen(dbinfo->replslotname));
1825 : :
46 alvherre@kurilemu.de 1826 [ + + ]: 8 : if (dry_run)
1827 : 6 : pg_log_info("dry-run: would create subscription \"%s\" in database \"%s\"",
1828 : : dbinfo->subname, dbinfo->dbname);
1829 : : else
1830 : 2 : pg_log_info("creating subscription \"%s\" in database \"%s\"",
1831 : : dbinfo->subname, dbinfo->dbname);
1832 : :
631 peter@eisentraut.org 1833 : 8 : appendPQExpBuffer(str,
1834 : : "CREATE SUBSCRIPTION %s CONNECTION %s PUBLICATION %s "
1835 : : "WITH (create_slot = false, enabled = false, "
1836 : : "slot_name = %s, copy_data = false, two_phase = %s)",
1837 : : subname_esc, pubconninfo_esc, pubname_esc, replslotname_esc,
293 akapila@postgresql.o 1838 [ + + ]: 8 : dbinfos.two_phase ? "true" : "false");
1839 : :
307 michael@paquier.xyz 1840 : 8 : PQfreemem(pubname_esc);
1841 : 8 : PQfreemem(subname_esc);
1842 : 8 : PQfreemem(pubconninfo_esc);
1843 : 8 : PQfreemem(replslotname_esc);
1844 : :
631 peter@eisentraut.org 1845 [ + + ]: 8 : pg_log_debug("command is: %s", str->data);
1846 : :
1847 [ + + ]: 8 : if (!dry_run)
1848 : : {
1849 : 2 : res = PQexec(conn, str->data);
1850 [ - + ]: 2 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
1851 : : {
509 peter@eisentraut.org 1852 :UBC 0 : pg_log_error("could not create subscription \"%s\" in database \"%s\": %s",
1853 : : dbinfo->subname, dbinfo->dbname, PQresultErrorMessage(res));
631 1854 : 0 : disconnect_database(conn, true);
1855 : : }
631 peter@eisentraut.org 1856 :CBC 2 : PQclear(res);
1857 : : }
1858 : :
1859 : 8 : destroyPQExpBuffer(str);
1860 : 8 : }
1861 : :
1862 : : /*
1863 : : * Sets the replication progress to the consistent LSN.
1864 : : *
1865 : : * The subscriber caught up to the consistent LSN provided by the last
1866 : : * replication slot that was created. The goal is to set up the initial
1867 : : * location for the logical replication that is the exact LSN that the
1868 : : * subscriber was promoted. Once the subscription is enabled it will start
1869 : : * streaming from that location onwards. In dry run mode, the subscription OID
1870 : : * and LSN are set to invalid values for printing purposes.
1871 : : */
1872 : : static void
1873 : 8 : set_replication_progress(PGconn *conn, const struct LogicalRepInfo *dbinfo, const char *lsn)
1874 : : {
1875 : 8 : PQExpBuffer str = createPQExpBuffer();
1876 : : PGresult *res;
1877 : : Oid suboid;
1878 : : char *subname;
1879 : : char *dbname;
1880 : : char *originname;
1881 : : char *lsnstr;
1882 : :
1883 [ - + ]: 8 : Assert(conn != NULL);
1884 : :
1885 : 8 : subname = PQescapeLiteral(conn, dbinfo->subname, strlen(dbinfo->subname));
1886 : 8 : dbname = PQescapeLiteral(conn, dbinfo->dbname, strlen(dbinfo->dbname));
1887 : :
1888 : 8 : appendPQExpBuffer(str,
1889 : : "SELECT s.oid FROM pg_catalog.pg_subscription s "
1890 : : "INNER JOIN pg_catalog.pg_database d ON (s.subdbid = d.oid) "
1891 : : "WHERE s.subname = %s AND d.datname = %s",
1892 : : subname, dbname);
1893 : :
1894 : 8 : res = PQexec(conn, str->data);
1895 [ - + ]: 8 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
1896 : : {
631 peter@eisentraut.org 1897 :UBC 0 : pg_log_error("could not obtain subscription OID: %s",
1898 : : PQresultErrorMessage(res));
1899 : 0 : disconnect_database(conn, true);
1900 : : }
1901 : :
631 peter@eisentraut.org 1902 [ + + - + ]:CBC 8 : if (PQntuples(res) != 1 && !dry_run)
1903 : : {
630 peter@eisentraut.org 1904 :UBC 0 : pg_log_error("could not obtain subscription OID: got %d rows, expected %d row",
1905 : : PQntuples(res), 1);
631 1906 : 0 : disconnect_database(conn, true);
1907 : : }
1908 : :
631 peter@eisentraut.org 1909 [ + + ]:CBC 8 : if (dry_run)
1910 : : {
1911 : 6 : suboid = InvalidOid;
162 alvherre@kurilemu.de 1912 :GNC 6 : lsnstr = psprintf("%X/%08X", LSN_FORMAT_ARGS((XLogRecPtr) InvalidXLogRecPtr));
1913 : : }
1914 : : else
1915 : : {
631 peter@eisentraut.org 1916 :CBC 2 : suboid = strtoul(PQgetvalue(res, 0, 0), NULL, 10);
1917 : 2 : lsnstr = psprintf("%s", lsn);
1918 : : }
1919 : :
1920 : 8 : PQclear(res);
1921 : :
1922 : : /*
1923 : : * The origin name is defined as pg_%u. %u is the subscription OID. See
1924 : : * ApplyWorkerMain().
1925 : : */
1926 : 8 : originname = psprintf("pg_%u", suboid);
1927 : :
46 alvherre@kurilemu.de 1928 [ + + ]: 8 : if (dry_run)
1929 : 6 : pg_log_info("dry-run: would set the replication progress (node name \"%s\", LSN %s) in database \"%s\"",
1930 : : originname, lsnstr, dbinfo->dbname);
1931 : : else
1932 : 2 : pg_log_info("setting the replication progress (node name \"%s\", LSN %s) in database \"%s\"",
1933 : : originname, lsnstr, dbinfo->dbname);
1934 : :
631 peter@eisentraut.org 1935 : 8 : resetPQExpBuffer(str);
1936 : 8 : appendPQExpBuffer(str,
1937 : : "SELECT pg_catalog.pg_replication_origin_advance('%s', '%s')",
1938 : : originname, lsnstr);
1939 : :
1940 [ + + ]: 8 : pg_log_debug("command is: %s", str->data);
1941 : :
1942 [ + + ]: 8 : if (!dry_run)
1943 : : {
1944 : 2 : res = PQexec(conn, str->data);
1945 [ - + ]: 2 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
1946 : : {
479 peter@eisentraut.org 1947 :UBC 0 : pg_log_error("could not set replication progress for subscription \"%s\": %s",
1948 : : dbinfo->subname, PQresultErrorMessage(res));
631 1949 : 0 : disconnect_database(conn, true);
1950 : : }
631 peter@eisentraut.org 1951 :CBC 2 : PQclear(res);
1952 : : }
1953 : :
307 michael@paquier.xyz 1954 : 8 : PQfreemem(subname);
1955 : 8 : PQfreemem(dbname);
631 peter@eisentraut.org 1956 : 8 : pg_free(originname);
1957 : 8 : pg_free(lsnstr);
1958 : 8 : destroyPQExpBuffer(str);
1959 : 8 : }
1960 : :
1961 : : /*
1962 : : * Enables the subscription.
1963 : : *
1964 : : * The subscription was created in a previous step but it was disabled. After
1965 : : * adjusting the initial logical replication location, enable the subscription.
1966 : : */
1967 : : static void
1968 : 8 : enable_subscription(PGconn *conn, const struct LogicalRepInfo *dbinfo)
1969 : : {
1970 : 8 : PQExpBuffer str = createPQExpBuffer();
1971 : : PGresult *res;
1972 : : char *subname;
1973 : :
1974 [ - + ]: 8 : Assert(conn != NULL);
1975 : :
1976 : 8 : subname = PQescapeIdentifier(conn, dbinfo->subname, strlen(dbinfo->subname));
1977 : :
46 alvherre@kurilemu.de 1978 [ + + ]: 8 : if (dry_run)
1979 : 6 : pg_log_info("dry-run: would enable subscription \"%s\" in database \"%s\"",
1980 : : dbinfo->subname, dbinfo->dbname);
1981 : : else
1982 : 2 : pg_log_info("enabling subscription \"%s\" in database \"%s\"",
1983 : : dbinfo->subname, dbinfo->dbname);
1984 : :
631 peter@eisentraut.org 1985 : 8 : appendPQExpBuffer(str, "ALTER SUBSCRIPTION %s ENABLE", subname);
1986 : :
1987 [ + + ]: 8 : pg_log_debug("command is: %s", str->data);
1988 : :
1989 [ + + ]: 8 : if (!dry_run)
1990 : : {
1991 : 2 : res = PQexec(conn, str->data);
1992 [ - + ]: 2 : if (PQresultStatus(res) != PGRES_COMMAND_OK)
1993 : : {
631 peter@eisentraut.org 1994 :UBC 0 : pg_log_error("could not enable subscription \"%s\": %s",
1995 : : dbinfo->subname, PQresultErrorMessage(res));
1996 : 0 : disconnect_database(conn, true);
1997 : : }
1998 : :
631 peter@eisentraut.org 1999 :CBC 2 : PQclear(res);
2000 : : }
2001 : :
307 michael@paquier.xyz 2002 : 8 : PQfreemem(subname);
631 peter@eisentraut.org 2003 : 8 : destroyPQExpBuffer(str);
2004 : 8 : }
2005 : :
2006 : : /*
2007 : : * Fetch a list of all connectable non-template databases from the source server
2008 : : * and form a list such that they appear as if the user has specified multiple
2009 : : * --database options, one for each source database.
2010 : : */
2011 : : static void
263 akapila@postgresql.o 2012 : 1 : get_publisher_databases(struct CreateSubscriberOptions *opt,
2013 : : bool dbnamespecified)
2014 : : {
2015 : : PGconn *conn;
2016 : : PGresult *res;
2017 : :
2018 : : /* If a database name was specified, just connect to it. */
2019 [ - + ]: 1 : if (dbnamespecified)
263 akapila@postgresql.o 2020 :UBC 0 : conn = connect_database(opt->pub_conninfo_str, true);
2021 : : else
2022 : : {
2023 : : /* Otherwise, try postgres first and then template1. */
2024 : : char *conninfo;
2025 : :
263 akapila@postgresql.o 2026 :CBC 1 : conninfo = concat_conninfo_dbname(opt->pub_conninfo_str, "postgres");
2027 : 1 : conn = connect_database(conninfo, false);
2028 : 1 : pg_free(conninfo);
2029 [ - + ]: 1 : if (!conn)
2030 : : {
263 akapila@postgresql.o 2031 :UBC 0 : conninfo = concat_conninfo_dbname(opt->pub_conninfo_str, "template1");
2032 : 0 : conn = connect_database(conninfo, true);
2033 : 0 : pg_free(conninfo);
2034 : : }
2035 : : }
2036 : :
263 akapila@postgresql.o 2037 :CBC 1 : res = PQexec(conn, "SELECT datname FROM pg_database WHERE datistemplate = false AND datallowconn AND datconnlimit <> -2 ORDER BY 1");
2038 [ - + ]: 1 : if (PQresultStatus(res) != PGRES_TUPLES_OK)
2039 : : {
263 akapila@postgresql.o 2040 :UBC 0 : pg_log_error("could not obtain a list of databases: %s", PQresultErrorMessage(res));
2041 : 0 : PQclear(res);
2042 : 0 : disconnect_database(conn, true);
2043 : : }
2044 : :
263 akapila@postgresql.o 2045 [ + + ]:CBC 4 : for (int i = 0; i < PQntuples(res); i++)
2046 : : {
2047 : 3 : const char *dbname = PQgetvalue(res, i, 0);
2048 : :
2049 : 3 : simple_string_list_append(&opt->database_names, dbname);
2050 : :
2051 : : /* Increment num_dbs to reflect multiple --database options */
2052 : 3 : num_dbs++;
2053 : : }
2054 : :
2055 : 1 : PQclear(res);
2056 : 1 : disconnect_database(conn, false);
2057 : 1 : }
2058 : :
2059 : : int
631 peter@eisentraut.org 2060 : 23 : main(int argc, char **argv)
2061 : : {
2062 : : static struct option long_options[] =
2063 : : {
2064 : : {"all", no_argument, NULL, 'a'},
2065 : : {"database", required_argument, NULL, 'd'},
2066 : : {"pgdata", required_argument, NULL, 'D'},
2067 : : {"dry-run", no_argument, NULL, 'n'},
2068 : : {"subscriber-port", required_argument, NULL, 'p'},
2069 : : {"publisher-server", required_argument, NULL, 'P'},
2070 : : {"socketdir", required_argument, NULL, 's'},
2071 : : {"recovery-timeout", required_argument, NULL, 't'},
2072 : : {"enable-two-phase", no_argument, NULL, 'T'},
2073 : : {"subscriber-username", required_argument, NULL, 'U'},
2074 : : {"verbose", no_argument, NULL, 'v'},
2075 : : {"version", no_argument, NULL, 'V'},
2076 : : {"help", no_argument, NULL, '?'},
2077 : : {"config-file", required_argument, NULL, 1},
2078 : : {"publication", required_argument, NULL, 2},
2079 : : {"replication-slot", required_argument, NULL, 3},
2080 : : {"subscription", required_argument, NULL, 4},
2081 : : {"clean", required_argument, NULL, 5},
2082 : : {NULL, 0, NULL, 0}
2083 : : };
2084 : :
2085 : 23 : struct CreateSubscriberOptions opt = {0};
2086 : :
2087 : : int c;
2088 : : int option_index;
2089 : :
2090 : : char *pub_base_conninfo;
2091 : : char *sub_base_conninfo;
2092 : 23 : char *dbname_conninfo = NULL;
2093 : :
2094 : : uint64 pub_sysid;
2095 : : uint64 sub_sysid;
2096 : : struct stat statbuf;
2097 : :
2098 : : char *consistent_lsn;
2099 : :
2100 : : char pidfile[MAXPGPATH];
2101 : :
2102 : 23 : pg_logging_init(argv[0]);
2103 : 23 : pg_logging_set_level(PG_LOG_WARNING);
2104 : 23 : progname = get_progname(argv[0]);
501 alvherre@alvh.no-ip. 2105 : 23 : set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_basebackup"));
2106 : :
631 peter@eisentraut.org 2107 [ + + ]: 23 : if (argc > 1)
2108 : : {
2109 [ + + - + ]: 22 : if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
2110 : : {
2111 : 1 : usage();
2112 : 1 : exit(0);
2113 : : }
2114 [ + - ]: 21 : else if (strcmp(argv[1], "-V") == 0
2115 [ + + ]: 21 : || strcmp(argv[1], "--version") == 0)
2116 : : {
2117 : 1 : puts("pg_createsubscriber (PostgreSQL) " PG_VERSION);
2118 : 1 : exit(0);
2119 : : }
2120 : : }
2121 : :
2122 : : /* Default settings */
2123 : 21 : subscriber_dir = NULL;
2124 : 21 : opt.config_file = NULL;
2125 : 21 : opt.pub_conninfo_str = NULL;
2126 : 21 : opt.socket_dir = NULL;
2127 : 21 : opt.sub_port = DEFAULT_SUB_PORT;
2128 : 21 : opt.sub_username = NULL;
293 akapila@postgresql.o 2129 : 21 : opt.two_phase = false;
631 peter@eisentraut.org 2130 : 21 : opt.database_names = (SimpleStringList)
2131 : : {
2132 : : 0
2133 : : };
2134 : 21 : opt.recovery_timeout = 0;
263 akapila@postgresql.o 2135 : 21 : opt.all_dbs = false;
2136 : :
2137 : : /*
2138 : : * Don't allow it to be run as root. It uses pg_ctl which does not allow
2139 : : * it either.
2140 : : */
2141 : : #ifndef WIN32
631 peter@eisentraut.org 2142 [ - + ]: 21 : if (geteuid() == 0)
2143 : : {
631 peter@eisentraut.org 2144 :UBC 0 : pg_log_error("cannot be executed by \"root\"");
2145 : 0 : pg_log_error_hint("You must run %s as the PostgreSQL superuser.",
2146 : : progname);
2147 : 0 : exit(1);
2148 : : }
2149 : : #endif
2150 : :
631 peter@eisentraut.org 2151 :CBC 21 : get_restricted_token();
2152 : :
174 2153 : 162 : while ((c = getopt_long(argc, argv, "ad:D:np:P:s:t:TU:v",
631 2154 [ + + ]: 162 : long_options, &option_index)) != -1)
2155 : : {
2156 [ + + + + : 144 : switch (c)
+ + + + +
- + - + +
+ + + ]
2157 : : {
263 akapila@postgresql.o 2158 : 3 : case 'a':
2159 : 3 : opt.all_dbs = true;
2160 : 3 : break;
631 peter@eisentraut.org 2161 : 25 : case 'd':
2162 [ + + ]: 25 : if (!simple_string_list_member(&opt.database_names, optarg))
2163 : : {
2164 : 24 : simple_string_list_append(&opt.database_names, optarg);
2165 : 24 : num_dbs++;
2166 : : }
2167 : : else
256 akapila@postgresql.o 2168 : 1 : pg_fatal("database \"%s\" specified more than once for -d/--database", optarg);
631 peter@eisentraut.org 2169 : 24 : break;
2170 : 19 : case 'D':
2171 : 19 : subscriber_dir = pg_strdup(optarg);
2172 : 19 : canonicalize_path(subscriber_dir);
2173 : 19 : break;
2174 : 9 : case 'n':
2175 : 9 : dry_run = true;
2176 : 9 : break;
2177 : 12 : case 'p':
2178 : 12 : opt.sub_port = pg_strdup(optarg);
2179 : 12 : break;
2180 : 18 : case 'P':
2181 : 18 : opt.pub_conninfo_str = pg_strdup(optarg);
2182 : 18 : break;
2183 : 12 : case 's':
2184 : 12 : opt.socket_dir = pg_strdup(optarg);
2185 : 12 : canonicalize_path(opt.socket_dir);
2186 : 12 : break;
2187 : 3 : case 't':
2188 : 3 : opt.recovery_timeout = atoi(optarg);
2189 : 3 : break;
293 akapila@postgresql.o 2190 : 1 : case 'T':
2191 : 1 : opt.two_phase = true;
2192 : 1 : break;
631 peter@eisentraut.org 2193 :UBC 0 : case 'U':
2194 : 0 : opt.sub_username = pg_strdup(optarg);
2195 : 0 : break;
631 peter@eisentraut.org 2196 :CBC 19 : case 'v':
2197 : 19 : pg_logging_increase_verbosity();
2198 : 19 : break;
631 peter@eisentraut.org 2199 :UBC 0 : case 1:
2200 : 0 : opt.config_file = pg_strdup(optarg);
2201 : 0 : break;
631 peter@eisentraut.org 2202 :CBC 12 : case 2:
2203 [ + + ]: 12 : if (!simple_string_list_member(&opt.pub_names, optarg))
2204 : : {
2205 : 11 : simple_string_list_append(&opt.pub_names, optarg);
2206 : 11 : num_pubs++;
2207 : : }
2208 : : else
256 akapila@postgresql.o 2209 : 1 : pg_fatal("publication \"%s\" specified more than once for --publication", optarg);
631 peter@eisentraut.org 2210 : 11 : break;
2211 : 4 : case 3:
2212 [ + - ]: 4 : if (!simple_string_list_member(&opt.replslot_names, optarg))
2213 : : {
2214 : 4 : simple_string_list_append(&opt.replslot_names, optarg);
2215 : 4 : num_replslots++;
2216 : : }
2217 : : else
256 akapila@postgresql.o 2218 :UBC 0 : pg_fatal("replication slot \"%s\" specified more than once for --replication-slot", optarg);
631 peter@eisentraut.org 2219 :CBC 4 : break;
2220 : 5 : case 4:
2221 [ + - ]: 5 : if (!simple_string_list_member(&opt.sub_names, optarg))
2222 : : {
2223 : 5 : simple_string_list_append(&opt.sub_names, optarg);
2224 : 5 : num_subs++;
2225 : : }
2226 : : else
256 akapila@postgresql.o 2227 :UBC 0 : pg_fatal("subscription \"%s\" specified more than once for --subscription", optarg);
631 peter@eisentraut.org 2228 :CBC 5 : break;
174 2229 : 1 : case 5:
2230 [ + - ]: 1 : if (!simple_string_list_member(&opt.objecttypes_to_clean, optarg))
2231 : 1 : simple_string_list_append(&opt.objecttypes_to_clean, optarg);
2232 : : else
174 peter@eisentraut.org 2233 :UBC 0 : pg_fatal("object type \"%s\" specified more than once for --clean", optarg);
174 peter@eisentraut.org 2234 :CBC 1 : break;
631 2235 : 1 : default:
2236 : : /* getopt_long already emitted a complaint */
2237 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2238 : 1 : exit(1);
2239 : : }
2240 : : }
2241 : :
2242 : : /* Validate that --all is not used with incompatible options */
263 akapila@postgresql.o 2243 [ + + ]: 18 : if (opt.all_dbs)
2244 : : {
2245 : 3 : char *bad_switch = NULL;
2246 : :
2247 [ + + ]: 3 : if (num_dbs > 0)
2248 : 1 : bad_switch = "--database";
2249 [ + + ]: 2 : else if (num_pubs > 0)
2250 : 1 : bad_switch = "--publication";
2251 [ - + ]: 1 : else if (num_replslots > 0)
263 akapila@postgresql.o 2252 :UBC 0 : bad_switch = "--replication-slot";
263 akapila@postgresql.o 2253 [ - + ]:CBC 1 : else if (num_subs > 0)
263 akapila@postgresql.o 2254 :UBC 0 : bad_switch = "--subscription";
2255 : :
263 akapila@postgresql.o 2256 [ + + ]:CBC 3 : if (bad_switch)
2257 : : {
8 alvherre@kurilemu.de 2258 : 2 : pg_log_error("options %s and %s cannot be used together",
2259 : : bad_switch, "-a/--all");
263 akapila@postgresql.o 2260 : 2 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2261 : 2 : exit(1);
2262 : : }
2263 : : }
2264 : :
2265 : : /* Any non-option arguments? */
631 peter@eisentraut.org 2266 [ - + ]: 16 : if (optind < argc)
2267 : : {
631 peter@eisentraut.org 2268 :UBC 0 : pg_log_error("too many command-line arguments (first is \"%s\")",
2269 : : argv[optind]);
2270 : 0 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2271 : 0 : exit(1);
2272 : : }
2273 : :
2274 : : /* Required arguments */
631 peter@eisentraut.org 2275 [ + + ]:CBC 16 : if (subscriber_dir == NULL)
2276 : : {
2277 : 1 : pg_log_error("no subscriber data directory specified");
2278 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2279 : 1 : exit(1);
2280 : : }
2281 : :
2282 : : /* If socket directory is not provided, use the current directory */
2283 [ + + ]: 15 : if (opt.socket_dir == NULL)
2284 : : {
2285 : : char cwd[MAXPGPATH];
2286 : :
2287 [ - + ]: 5 : if (!getcwd(cwd, MAXPGPATH))
631 peter@eisentraut.org 2288 :UBC 0 : pg_fatal("could not determine current directory");
631 peter@eisentraut.org 2289 :CBC 5 : opt.socket_dir = pg_strdup(cwd);
2290 : 5 : canonicalize_path(opt.socket_dir);
2291 : : }
2292 : :
2293 : : /*
2294 : : * Parse connection string. Build a base connection string that might be
2295 : : * reused by multiple databases.
2296 : : */
2297 [ + + ]: 15 : if (opt.pub_conninfo_str == NULL)
2298 : : {
2299 : : /*
2300 : : * TODO use primary_conninfo (if available) from subscriber and
2301 : : * extract publisher connection string. Assume that there are
2302 : : * identical entries for physical and logical replication. If there is
2303 : : * not, we would fail anyway.
2304 : : */
2305 : 1 : pg_log_error("no publisher connection string specified");
2306 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.", progname);
2307 : 1 : exit(1);
2308 : : }
2309 : :
28 alvherre@kurilemu.de 2310 [ + + ]:GNC 14 : if (dry_run)
2311 : 8 : pg_log_info("Executing in dry-run mode.\n"
2312 : : "The target directory will not be modified.");
2313 : :
501 alvherre@alvh.no-ip. 2314 :CBC 14 : pg_log_info("validating publisher connection string");
631 peter@eisentraut.org 2315 : 14 : pub_base_conninfo = get_base_conninfo(opt.pub_conninfo_str,
2316 : : &dbname_conninfo);
2317 [ - + ]: 14 : if (pub_base_conninfo == NULL)
631 peter@eisentraut.org 2318 :UBC 0 : exit(1);
2319 : :
501 alvherre@alvh.no-ip. 2320 :CBC 14 : pg_log_info("validating subscriber connection string");
631 peter@eisentraut.org 2321 : 14 : sub_base_conninfo = get_sub_conninfo(&opt);
2322 : :
2323 : : /*
2324 : : * Fetch all databases from the source (publisher) and treat them as if
2325 : : * the user specified has multiple --database options, one for each source
2326 : : * database.
2327 : : */
263 akapila@postgresql.o 2328 [ + + ]: 14 : if (opt.all_dbs)
2329 : : {
2330 : 1 : bool dbnamespecified = (dbname_conninfo != NULL);
2331 : :
2332 : 1 : get_publisher_databases(&opt, dbnamespecified);
2333 : : }
2334 : :
631 peter@eisentraut.org 2335 [ + + ]: 14 : if (opt.database_names.head == NULL)
2336 : : {
2337 : 2 : pg_log_info("no database was specified");
2338 : :
2339 : : /*
2340 : : * Try to obtain the dbname from the publisher conninfo. If dbname
2341 : : * parameter is not available, error out.
2342 : : */
2343 [ + + ]: 2 : if (dbname_conninfo)
2344 : : {
2345 : 1 : simple_string_list_append(&opt.database_names, dbname_conninfo);
2346 : 1 : num_dbs++;
2347 : :
479 2348 : 1 : pg_log_info("database name \"%s\" was extracted from the publisher connection string",
2349 : : dbname_conninfo);
2350 : : }
2351 : : else
2352 : : {
631 2353 : 1 : pg_log_error("no database name specified");
2354 : 1 : pg_log_error_hint("Try \"%s --help\" for more information.",
2355 : : progname);
2356 : 1 : exit(1);
2357 : : }
2358 : : }
2359 : :
2360 : : /* Number of object names must match number of databases */
2361 [ + + + + ]: 13 : if (num_pubs > 0 && num_pubs != num_dbs)
2362 : : {
479 2363 : 1 : pg_log_error("wrong number of publication names specified");
2364 : 1 : pg_log_error_detail("The number of specified publication names (%d) must match the number of specified database names (%d).",
2365 : : num_pubs, num_dbs);
631 2366 : 1 : exit(1);
2367 : : }
2368 [ + + + + ]: 12 : if (num_subs > 0 && num_subs != num_dbs)
2369 : : {
479 2370 : 1 : pg_log_error("wrong number of subscription names specified");
2371 : 1 : pg_log_error_detail("The number of specified subscription names (%d) must match the number of specified database names (%d).",
2372 : : num_subs, num_dbs);
631 2373 : 1 : exit(1);
2374 : : }
2375 [ + + + + ]: 11 : if (num_replslots > 0 && num_replslots != num_dbs)
2376 : : {
479 2377 : 1 : pg_log_error("wrong number of replication slot names specified");
2378 : 1 : pg_log_error_detail("The number of specified replication slot names (%d) must match the number of specified database names (%d).",
2379 : : num_replslots, num_dbs);
631 2380 : 1 : exit(1);
2381 : : }
2382 : :
2383 : : /* Verify the object types specified for removal from the subscriber */
174 2384 [ + + ]: 11 : for (SimpleStringListCell *cell = opt.objecttypes_to_clean.head; cell; cell = cell->next)
2385 : : {
271 akapila@postgresql.o 2386 [ + - ]: 1 : if (pg_strcasecmp(cell->val, "publications") == 0)
174 peter@eisentraut.org 2387 : 1 : dbinfos.objecttypes_to_clean |= OBJECTTYPE_PUBLICATIONS;
2388 : : else
2389 : : {
8 alvherre@kurilemu.de 2390 :UBC 0 : pg_log_error("invalid object type \"%s\" specified for %s",
2391 : : cell->val, "--clean");
183 peter@eisentraut.org 2392 : 0 : pg_log_error_hint("The valid value is: \"%s\"", "publications");
271 akapila@postgresql.o 2393 : 0 : exit(1);
2394 : : }
2395 : : }
2396 : :
2397 : : /* Get the absolute path of pg_ctl and pg_resetwal on the subscriber */
631 peter@eisentraut.org 2398 :CBC 10 : pg_ctl_path = get_exec_path(argv[0], "pg_ctl");
2399 : 10 : pg_resetwal_path = get_exec_path(argv[0], "pg_resetwal");
2400 : :
2401 : : /* Rudimentary check for a data directory */
2402 : 10 : check_data_directory(subscriber_dir);
2403 : :
293 akapila@postgresql.o 2404 : 10 : dbinfos.two_phase = opt.two_phase;
2405 : :
2406 : : /*
2407 : : * Store database information for publisher and subscriber. It should be
2408 : : * called before atexit() because its return is used in the
2409 : : * cleanup_objects_atexit().
2410 : : */
2411 : 10 : dbinfos.dbinfo = store_pub_sub_info(&opt, pub_base_conninfo, sub_base_conninfo);
2412 : :
2413 : : /* Register a function to clean up objects in case of failure */
631 peter@eisentraut.org 2414 : 10 : atexit(cleanup_objects_atexit);
2415 : :
2416 : : /*
2417 : : * Check if the subscriber data directory has the same system identifier
2418 : : * than the publisher data directory.
2419 : : */
293 akapila@postgresql.o 2420 : 10 : pub_sysid = get_primary_sysid(dbinfos.dbinfo[0].pubconninfo);
631 peter@eisentraut.org 2421 : 10 : sub_sysid = get_standby_sysid(subscriber_dir);
2422 [ + + ]: 10 : if (pub_sysid != sub_sysid)
2423 : 1 : pg_fatal("subscriber data directory is not a copy of the source database cluster");
2424 : :
2425 : : /* Subscriber PID file */
2426 : 9 : snprintf(pidfile, MAXPGPATH, "%s/postmaster.pid", subscriber_dir);
2427 : :
2428 : : /*
2429 : : * The standby server must not be running. If the server is started under
2430 : : * service manager and pg_createsubscriber stops it, the service manager
2431 : : * might react to this action and start the server again. Therefore,
2432 : : * refuse to proceed if the server is running to avoid possible failures.
2433 : : */
2434 [ + + ]: 9 : if (stat(pidfile, &statbuf) == 0)
2435 : : {
479 2436 : 1 : pg_log_error("standby server is running");
2437 : 1 : pg_log_error_hint("Stop the standby server and try again.");
631 2438 : 1 : exit(1);
2439 : : }
2440 : :
2441 : : /*
2442 : : * Start a short-lived standby server with temporary parameters (provided
2443 : : * by command-line options). The goal is to avoid connections during the
2444 : : * transformation steps.
2445 : : */
479 2446 : 8 : pg_log_info("starting the standby server with command-line options");
532 akapila@postgresql.o 2447 : 8 : start_standby_server(&opt, true, false);
2448 : :
2449 : : /* Check if the standby server is ready for logical replication */
293 2450 : 8 : check_subscriber(dbinfos.dbinfo);
2451 : :
2452 : : /* Check if the primary server is ready for logical replication */
2453 : 6 : check_publisher(dbinfos.dbinfo);
2454 : :
2455 : : /*
2456 : : * Stop the target server. The recovery process requires that the server
2457 : : * reaches a consistent state before targeting the recovery stop point.
2458 : : * Make sure a consistent state is reached (stop the target server
2459 : : * guarantees it) *before* creating the replication slots in
2460 : : * setup_publisher().
2461 : : */
631 peter@eisentraut.org 2462 : 4 : pg_log_info("stopping the subscriber");
2463 : 4 : stop_standby_server(subscriber_dir);
2464 : :
2465 : : /* Create the required objects for each database on publisher */
293 akapila@postgresql.o 2466 : 4 : consistent_lsn = setup_publisher(dbinfos.dbinfo);
2467 : :
2468 : : /* Write the required recovery parameters */
2469 : 4 : setup_recovery(dbinfos.dbinfo, subscriber_dir, consistent_lsn);
2470 : :
2471 : : /*
2472 : : * Start subscriber so the recovery parameters will take effect. Wait
2473 : : * until accepting connections. We don't want to start logical replication
2474 : : * during setup.
2475 : : */
631 peter@eisentraut.org 2476 : 4 : pg_log_info("starting the subscriber");
532 akapila@postgresql.o 2477 : 4 : start_standby_server(&opt, true, true);
2478 : :
2479 : : /* Waiting the subscriber to be promoted */
293 2480 : 4 : wait_for_end_recovery(dbinfos.dbinfo[0].subconninfo, &opt);
2481 : :
2482 : : /*
2483 : : * Create the subscription for each database on subscriber. It does not
2484 : : * enable it immediately because it needs to adjust the replication start
2485 : : * point to the LSN reported by setup_publisher(). It also cleans up
2486 : : * publications created by this tool and replication to the standby.
2487 : : */
2488 : 4 : setup_subscriber(dbinfos.dbinfo, consistent_lsn);
2489 : :
2490 : : /* Remove primary_slot_name if it exists on primary */
2491 : 4 : drop_primary_replication_slot(dbinfos.dbinfo, primary_slot_name);
2492 : :
2493 : : /* Remove failover replication slots if they exist on subscriber */
2494 : 4 : drop_failover_replication_slots(dbinfos.dbinfo);
2495 : :
2496 : : /* Stop the subscriber */
631 peter@eisentraut.org 2497 : 4 : pg_log_info("stopping the subscriber");
2498 : 4 : stop_standby_server(subscriber_dir);
2499 : :
2500 : : /* Change system identifier from subscriber */
2501 : 4 : modify_subscriber_sysid(&opt);
2502 : :
2503 : 4 : success = true;
2504 : :
2505 : 4 : pg_log_info("Done!");
2506 : :
2507 : 4 : return 0;
2508 : : }
|