34#define DEFAULT_SUB_PORT "50432"
35#define OBJECTTYPE_PUBLICATIONS 0x0001
48#define PG_AUTOCONF_FILENAME "postgresql.auto.conf"
49#define INCLUDED_CONF_FILE "pg_createsubscriber.conf"
50#define INCLUDED_CONF_FILE_DISABLED INCLUDED_CONF_FILE ".disabled"
97static void usage(
void);
121 const char *slotname);
126 const char *slot_name);
155#define WAIT_INTERVAL 1
246 "A manual removal of the recovery parameters may be required.");
261 "failed after the end of recovery");
263 "The target server cannot be used as a physical replica anymore. "
264 "You must recreate the physical replica before continuing.");
295 "publication \"%s\" created in database \"%s\" on primary was left behind",
299 "Drop this publication before trying again.");
304 "replication slot \"%s\" created in database \"%s\" on primary was left behind",
308 "Drop this replication slot soon to avoid retention of WAL files.");
321 printf(
_(
"%s creates a new logical replica from a standby server.\n\n"),
326 printf(
_(
" -a, --all create subscriptions for all databases except template\n"
327 " databases and databases that don't allow connections\n"));
328 printf(
_(
" -d, --database=DBNAME database in which to create a subscription\n"));
329 printf(
_(
" -D, --pgdata=DATADIR location for the subscriber data directory\n"));
330 printf(
_(
" -n, --dry-run dry run, just show what would be done\n"));
332 printf(
_(
" -P, --publisher-server=CONNSTR publisher connection string\n"));
333 printf(
_(
" -s, --socketdir=DIR socket directory to use (default current dir.)\n"));
334 printf(
_(
" -t, --recovery-timeout=SECS seconds to wait for recovery to end\n"));
335 printf(
_(
" -T, --enable-two-phase enable two-phase commit for all subscriptions\n"));
336 printf(
_(
" -U, --subscriber-username=NAME user name for subscriber connection\n"));
337 printf(
_(
" -v, --verbose output verbose messages\n"));
338 printf(
_(
" --clean=OBJECTTYPE drop all objects of the specified type from specified\n"
339 " databases on the subscriber; accepts: \"%s\"\n"),
"publications");
340 printf(
_(
" --config-file=FILENAME use specified main server configuration\n"
341 " file when running target cluster\n"));
342 printf(
_(
" --publication=NAME publication name\n"));
343 printf(
_(
" --replication-slot=NAME replication slot name\n"));
344 printf(
_(
" --subscription=NAME subscription name\n"));
345 printf(
_(
" -V, --version output version information, then exit\n"));
346 printf(
_(
" -?, --help show this help, then exit\n"));
390 "could not parse connection string: %s",
errmsg);
467 report_createsub_fatal(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
493 "checking if directory \"%s\" is a cluster data directory",
513 "data directory is of wrong version");
515 "File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".",
600 "publisher(%d): publication: %s ; replication slot: %s ; connection string: %s",
i,
605 "subscriber(%d): subscription: %s ; connection string: %s, two_phase: %s",
i,
637 "connection to database failed: %s",
651 "could not clear \"search_path\": %s",
692 "getting system identifier from publisher");
696 res =
PQexec(
conn,
"SELECT system_identifier FROM pg_catalog.pg_control_system()");
700 "could not get system identifier: %s",
707 "could not get system identifier: got %d rows, expected %d row",
715 "system identifier is %" PRIu64 " on publisher",
sysid);
736 "getting system identifier from subscriber");
745 "system identifier is %" PRIu64 " on subscriber",
sysid);
767 "modifying system identifier of subscriber");
779 cf->system_identifier = ((
uint64) tv.tv_sec) << 32;
780 cf->system_identifier |= ((
uint64) tv.tv_usec) << 12;
781 cf->system_identifier |=
getpid() & 0xFFF;
785 "dry-run: would set system identifier to %" PRIu64 " on subscriber",
786 cf->system_identifier);
791 "system identifier is %" PRIu64 " on subscriber",
792 cf->system_identifier);
797 "dry-run: would run pg_resetwal on the subscriber");
800 "running pg_resetwal on the subscriber");
806 "pg_resetwal command is: %s",
cmd_str);
814 "successfully reset WAL on the subscriber");
836 "SELECT oid FROM pg_catalog.pg_database "
837 "WHERE datname = pg_catalog.current_database()");
841 "could not obtain database OID: %s",
849 "could not obtain database OID: got %d rows, expected %d row",
867 objname =
psprintf(
"pg_createsubscriber_%u_%x", oid,
rand);
884 "SELECT 1 FROM pg_catalog.pg_publication "
885 "WHERE pubname = %s",
891 "could not find publication \"%s\" in database \"%s\": %s",
946 "use existing publication \"%s\" in database \"%s\"",
947 dbinfo[
i].pubname, dbinfo[
i].
dbname);
982 res =
PQexec(
conn,
"SELECT pg_log_standby_snapshot()");
986 "could not write an additional WAL record: %s",
1008 res =
PQexec(
conn,
"SELECT pg_catalog.pg_is_in_recovery()");
1013 "could not obtain recovery progress: %s",
1036 bool failed =
false;
1047 "checking settings on publisher");
1058 "primary server cannot be in recovery");
1074 "SELECT pg_catalog.current_setting('wal_level'),"
1075 " pg_catalog.current_setting('max_replication_slots'),"
1076 " (SELECT count(*) FROM pg_catalog.pg_replication_slots),"
1077 " pg_catalog.current_setting('max_wal_senders'),"
1078 " (SELECT count(*) FROM pg_catalog.pg_stat_activity WHERE backend_type = 'walsender'),"
1079 " pg_catalog.current_setting('max_prepared_transactions'),"
1080 " pg_catalog.current_setting('max_slot_wal_keep_size')");
1085 "could not obtain publisher settings: %s",
1105 "publisher: current replication slots: %d",
cur_repslots);
1111 "publisher: max_prepared_transactions: %d",
1114 "publisher: max_slot_wal_keep_size: %s",
1122 "publisher requires \"wal_level\" >= \"replica\"");
1129 "publisher requires %d replication slots, but only %d remain",
1132 "Increase the configuration parameter \"%s\" to at least %d.",
1140 "publisher requires %d WAL sender processes, but only %d remain",
1143 "Increase the configuration parameter \"%s\" to at least %d.",
1151 "two_phase option will not be enabled for replication slots");
1153 "Subscriptions will be created with the two_phase option disabled. "
1154 "Prepared transactions will be replicated at COMMIT PREPARED.");
1156 "You can use the command-line option --enable-two-phase to enable two_phase.");
1167 "required WAL could be removed from the publisher");
1169 "Set the configuration parameter \"%s\" to -1 to ensure that required WAL files are not prematurely removed.",
1170 "max_slot_wal_keep_size");
1195 bool failed =
false;
1202 "checking settings on subscriber");
1210 "target server must be a standby");
1225 "SELECT setting FROM pg_catalog.pg_settings WHERE name IN ("
1226 "'max_logical_replication_workers', "
1227 "'max_active_replication_origins', "
1228 "'max_worker_processes', "
1229 "'primary_slot_name') "
1235 "could not obtain subscriber settings: %s",
1247 "subscriber: max_logical_replication_workers: %d",
1252 "subscriber: max_worker_processes: %d",
max_wprocs);
1264 "subscriber requires %d active replication origins, but only %d remain",
1267 "Increase the configuration parameter \"%s\" to at least %d.",
1268 "max_active_replication_origins",
num_dbs);
1275 "subscriber requires %d logical replication workers, but only %d remain",
1278 "Increase the configuration parameter \"%s\" to at least %d.",
1279 "max_logical_replication_workers",
num_dbs);
1286 "subscriber requires %d worker processes, but only %d remain",
1289 "Increase the configuration parameter \"%s\" to at least %d.",
1290 "max_worker_processes",
num_dbs + 1);
1324 "dry-run: would drop subscription \"%s\" in database \"%s\"",
1329 "dropping subscription \"%s\" in database \"%s\"",
1337 "could not drop subscription \"%s\": %s",
1364 "SELECT s.subname FROM pg_catalog.pg_subscription s "
1365 "INNER JOIN pg_catalog.pg_database d ON (s.subdbid = d.oid) "
1366 "WHERE d.datname = %s",
1373 "could not obtain pre-existing subscriptions: %s",
1454 "recovery_target_timeline = 'latest'\n");
1465 "recovery_target_inclusive = false\n");
1467 "recovery_target_action = promote\n");
1476 "recovery_target_lsn = '%X/%08X'\n",
1541 "could not drop replication slot \"%s\" on primary",
1544 "Drop this replication slot soon to avoid retention of WAL files.");
1566 "SELECT slot_name FROM pg_catalog.pg_replication_slots WHERE failover");
1577 "could not obtain failover replication slot information: %s",
1580 "Drop the failover replication slots on subscriber soon to avoid retention of WAL files.");
1589 "could not drop failover replication slot");
1591 "Drop the failover replication slots on subscriber soon to avoid retention of WAL files.");
1614 "dry-run: would create the replication slot \"%s\" in database \"%s\" on publisher",
1615 slot_name, dbinfo->
dbname);
1618 "creating the replication slot \"%s\" in database \"%s\" on publisher",
1619 slot_name, dbinfo->
dbname);
1624 "SELECT lsn FROM pg_catalog.pg_create_logical_replication_slot(%s, 'pgoutput', false, %s, false)",
1631 "command is: %s",
str->data);
1639 "could not create replication slot \"%s\" in database \"%s\": %s",
1640 slot_name, dbinfo->
dbname,
1661 const char *slot_name)
1671 "dry-run: would drop the replication slot \"%s\" in database \"%s\"",
1672 slot_name, dbinfo->
dbname);
1675 "dropping the replication slot \"%s\" in database \"%s\"",
1676 slot_name, dbinfo->
dbname);
1685 "command is: %s",
str->data);
1693 "could not drop replication slot \"%s\" in database \"%s\": %s",
1715 "pg_ctl failed with exit code %d",
1722 "pg_ctl was terminated by exception 0x%X",
1725 "See C include file \"ntstatus.h\" for a description of the hexadecimal value.");
1728 "pg_ctl was terminated by signal %d: %s",
1735 "pg_ctl exited with unrecognized status %d", rc);
1791 "server was started");
1808 "server was stopped");
1828 "waiting for the target server to reach the consistent state");
1847 "recovery timed out");
1862 "target server reached the consistent state");
1864 "If pg_createsubscriber fails after this point, you must recreate the physical replica before continuing.");
1885 "SELECT 1 FROM pg_catalog.pg_publication "
1886 "WHERE pubname = %s",
1892 "could not obtain publication information: %s",
1907 "publication \"%s\" already exists", dbinfo->
pubname);
1909 "Consider renaming this publication before continuing.");
1918 "dry-run: would create publication \"%s\" in database \"%s\"",
1922 "creating publication \"%s\" in database \"%s\"",
1929 "command is: %s",
str->data);
1937 "could not create publication \"%s\" in database \"%s\": %s",
1957 bool *made_publication)
1969 "dry-run: would drop publication \"%s\" in database \"%s\"",
1973 "dropping publication \"%s\" in database \"%s\"",
1981 "command is: %s",
str->data);
1989 "could not drop publication \"%s\" in database \"%s\": %s",
1991 *made_publication =
false;
2026 "dropping all existing publications in database \"%s\"",
2030 res =
PQexec(
conn,
"SELECT pubname FROM pg_catalog.pg_publication;");
2034 "could not obtain publication information: %s",
2059 "dry-run: would preserve existing publication \"%s\" in database \"%s\"",
2063 "preserve existing publication \"%s\" in database \"%s\"",
2099 "dry-run: would create subscription \"%s\" in database \"%s\"",
2103 "creating subscription \"%s\" in database \"%s\"",
2107 "CREATE SUBSCRIPTION %s CONNECTION %s PUBLICATION %s "
2108 "WITH (create_slot = false, enabled = false, "
2109 "slot_name = %s, copy_data = false, two_phase = %s)",
2119 "command is: %s",
str->data);
2127 "could not create subscription \"%s\" in database \"%s\": %s",
2164 "SELECT s.oid FROM pg_catalog.pg_subscription s "
2165 "INNER JOIN pg_catalog.pg_database d ON (s.subdbid = d.oid) "
2166 "WHERE s.subname = %s AND d.datname = %s",
2173 "could not obtain subscription OID: %s",
2181 "could not obtain subscription OID: got %d rows, expected %d row",
2207 "dry-run: would set the replication progress (node name \"%s\", LSN %s) in database \"%s\"",
2211 "setting the replication progress (node name \"%s\", LSN %s) in database \"%s\"",
2216 "SELECT pg_catalog.pg_replication_origin_advance('%s', '%s')",
2220 "command is: %s",
str->data);
2228 "could not set replication progress for subscription \"%s\": %s",
2261 "dry-run: would enable subscription \"%s\" in database \"%s\"",
2265 "enabling subscription \"%s\" in database \"%s\"",
2271 "command is: %s",
str->data);
2279 "could not enable subscription \"%s\": %s",
2322 res =
PQexec(
conn,
"SELECT datname FROM pg_database WHERE datistemplate = false AND datallowconn AND datconnlimit <> -2 ORDER BY 1");
2326 "could not obtain a list of databases: %s",
2396 if (
strcmp(argv[1],
"--help") == 0 ||
strcmp(argv[1],
"-?") == 0)
2401 else if (
strcmp(argv[1],
"-V") == 0
2402 ||
strcmp(argv[1],
"--version") == 0)
2432 "cannot be executed by \"root\"");
2434 "You must run %s as the PostgreSQL superuser.",
2442 while ((
c =
getopt_long(argc, argv,
"ad:D:np:P:s:t:TU:v",
2527 "Try \"%s --help\" for more information.",
2550 "options %s and %s cannot be used together",
2553 "Try \"%s --help\" for more information.",
2563 "too many command-line arguments (first is \"%s\")",
2566 "Try \"%s --help\" for more information.",
progname);
2574 "no subscriber data directory specified");
2576 "Try \"%s --help\" for more information.",
progname);
2604 "no publisher connection string specified");
2606 "Try \"%s --help\" for more information.",
progname);
2612 "Executing in dry-run mode.\n"
2613 "The target directory will not be modified.");
2616 "validating publisher connection string");
2623 "validating subscriber connection string");
2641 "no database was specified");
2653 "database name \"%s\" was extracted from the publisher connection string",
2659 "no database name specified");
2661 "Try \"%s --help\" for more information.",
2671 "wrong number of publication names specified");
2673 "The number of specified publication names (%d) must match the number of specified database names (%d).",
2680 "wrong number of subscription names specified");
2682 "The number of specified subscription names (%d) must match the number of specified database names (%d).",
2689 "wrong number of replication slot names specified");
2691 "The number of specified replication slot names (%d) must match the number of specified database names (%d).",
2704 "invalid object type \"%s\" specified for %s",
2705 cell->val,
"--clean");
2707 "The valid value is: \"%s\"",
"publications");
2752 "standby server is running");
2754 "Stop the standby server and try again.");
2764 "starting the standby server with command-line options");
2781 "stopping the subscriber");
2796 "starting the subscriber");
2818 "stopping the subscriber");
#define Assert(condition)
#define PG_TEXTDOMAIN(domain)
#define pg_attribute_printf(f, a)
int find_my_exec(const char *argv0, char *retpath)
void set_pglocale_pgservice(const char *argv0, const char *app)
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
#define ALWAYS_SECURE_SEARCH_PATH_SQL
void update_controlfile(const char *DataDir, ControlFileData *ControlFile, bool do_sync)
ControlFileData * get_controlfile(const char *DataDir, bool *crc_ok_p)
int durable_rename(const char *oldfile, const char *newfile, int elevel)
PGconn * PQconnectdb(const char *conninfo)
void PQconninfoFree(PQconninfoOption *connOptions)
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
ConnStatusType PQstatus(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
void PQfreemem(void *ptr)
char * PQescapeLiteral(PGconn *conn, const char *str, size_t len)
PGresult * PQexec(PGconn *conn, const char *query)
char * PQescapeIdentifier(PGconn *conn, const char *str, size_t len)
void * pg_malloc(size_t size)
char * pg_strdup(const char *in)
#define pg_malloc_array(type, count)
uint32 get_pg_version(const char *datadir, char **version_str)
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
#define required_argument
#define PQresultErrorMessage
void pg_logging_increase_verbosity(void)
void pg_logging_init(const char *argv0)
void pg_logging_set_level(enum pg_log_level new_level)
void pg_log_generic_v(enum pg_log_level level, enum pg_log_part part, const char *pg_restrict fmt, va_list ap)
static PQExpBuffer recoveryconfcontents
static void static pg_noreturn void report_createsub_fatal(const char *pg_restrict fmt,...) pg_attribute_printf(1
static void pg_ctl_status(const char *pg_ctl_cmd, int rc)
static void create_subscription(PGconn *conn, const struct LogicalRepInfo *dbinfo)
static struct LogicalRepInfo * store_pub_sub_info(const struct CreateSubscriberOptions *opt, const char *pub_base_conninfo, const char *sub_base_conninfo)
static void modify_subscriber_sysid(const struct CreateSubscriberOptions *opt)
static void stop_standby_server(const char *datadir)
static char * pg_ctl_path
static bool server_is_in_recovery(PGconn *conn)
static char * get_exec_path(const char *argv0, const char *progname)
static void appendConnStrItem(PQExpBuffer buf, const char *keyword, const char *val)
static void check_publisher(const struct LogicalRepInfo *dbinfo)
static char * subscriber_dir
static char * create_logical_replication_slot(PGconn *conn, struct LogicalRepInfo *dbinfo)
static char * primary_slot_name
static void cleanup_objects_atexit(void)
static void check_subscriber(const struct LogicalRepInfo *dbinfo)
static pg_prng_state prng_state
static char * concat_conninfo_dbname(const char *conninfo, const char *dbname)
static void check_data_directory(const char *datadir)
static char * setup_publisher(struct LogicalRepInfo *dbinfo)
#define INCLUDED_CONF_FILE
static void setup_subscriber(struct LogicalRepInfo *dbinfo, const char *consistent_lsn)
static char * get_base_conninfo(const char *conninfo, char **dbname)
static void report_createsub_log(enum pg_log_level, enum pg_log_part, const char *pg_restrict fmt,...) pg_attribute_printf(3
static struct LogicalRepInfos dbinfos
static void check_and_drop_publications(PGconn *conn, struct LogicalRepInfo *dbinfo)
static uint64 get_standby_sysid(const char *datadir)
static bool recovery_ended
static void start_standby_server(const struct CreateSubscriberOptions *opt, bool restricted_access, bool restrict_logical_worker)
static void drop_existing_subscription(PGconn *conn, const char *subname, const char *dbname)
static char * get_sub_conninfo(const struct CreateSubscriberOptions *opt)
static void disconnect_database(PGconn *conn, bool exit_on_error)
static void drop_publication(PGconn *conn, const char *pubname, const char *dbname, bool *made_publication)
static void check_and_drop_existing_subscriptions(PGconn *conn, const struct LogicalRepInfo *dbinfo)
static void drop_primary_replication_slot(struct LogicalRepInfo *dbinfo, const char *slotname)
static bool find_publication(PGconn *conn, const char *pubname, const char *dbname)
static void drop_failover_replication_slots(struct LogicalRepInfo *dbinfo)
static void wait_for_end_recovery(const char *conninfo, const struct CreateSubscriberOptions *opt)
static void get_publisher_databases(struct CreateSubscriberOptions *opt, bool dbnamespecified)
static void create_publication(PGconn *conn, struct LogicalRepInfo *dbinfo)
#define OBJECTTYPE_PUBLICATIONS
static void set_replication_progress(PGconn *conn, const struct LogicalRepInfo *dbinfo, const char *lsn)
static char * pg_resetwal_path
static char * generate_object_name(PGconn *conn)
static const char * progname
#define INCLUDED_CONF_FILE_DISABLED
static void drop_replication_slot(PGconn *conn, struct LogicalRepInfo *dbinfo, const char *slot_name)
static bool standby_running
static uint64 get_primary_sysid(const char *conninfo)
static void setup_recovery(const struct LogicalRepInfo *dbinfo, const char *datadir, const char *lsn)
static bool recovery_params_set
static void enable_subscription(PGconn *conn, const struct LogicalRepInfo *dbinfo)
static PGconn * connect_database(const char *conninfo, bool exit_on_error)
PGDLLIMPORT char * optarg
uint32 pg_prng_uint32(pg_prng_state *state)
void pg_prng_seed(pg_prng_state *state, uint64 seed)
static char buf[DEFAULT_XLOG_SEG_SIZE]
int pg_strcasecmp(const char *s1, const char *s2)
const char * pg_strsignal(int signum)
void canonicalize_path(char *path)
const char * get_progname(const char *argv0)
size_t strlcpy(char *dst, const char *src, size_t siz)
PQExpBuffer createPQExpBuffer(void)
void resetPQExpBuffer(PQExpBuffer str)
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
void destroyPQExpBuffer(PQExpBuffer str)
void appendPQExpBufferChar(PQExpBuffer str, char ch)
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
static int fd(const char *x, int i)
char * psprintf(const char *fmt,...)
void WriteRecoveryConfig(PGconn *pgconn, const char *target_dir, PQExpBuffer contents)
PQExpBuffer GenerateRecoveryConfig(PGconn *pgconn, const char *replication_slot, char *dbname)
void get_restricted_token(void)
void pg_usleep(long microsec)
bool simple_string_list_member(SimpleStringList *list, const char *val)
void simple_string_list_append(SimpleStringList *list, const char *val)
void appendShellString(PQExpBuffer buf, const char *str)
void appendConnStrVal(PQExpBuffer buf, const char *str)
const char * sub_username
SimpleStringList database_names
SimpleStringList objecttypes_to_clean
SimpleStringList sub_names
SimpleStringList replslot_names
SimpleStringList pub_names
struct LogicalRepInfo * dbinfo
bits32 objecttypes_to_clean
SimpleStringListCell * head
#define GET_PG_MAJORVERSION_NUM(v)
char * wait_result_to_str(int exitstatus)
int gettimeofday(struct timeval *tp, void *tzp)
#define LSN_FORMAT_ARGS(lsn)
#define InvalidXLogRecPtr