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