35#define DEFAULT_SUB_PORT "50432"
36#define OBJECTTYPE_PUBLICATIONS 0x0001
49#define PG_AUTOCONF_FILENAME "postgresql.auto.conf"
50#define INCLUDED_CONF_FILE "pg_createsubscriber.conf"
51#define INCLUDED_CONF_FILE_DISABLED INCLUDED_CONF_FILE ".disabled"
53#define SERVER_LOG_FILE_NAME "pg_createsubscriber_server.log"
54#define INTERNAL_LOG_FILE_NAME "pg_createsubscriber_internal.log"
102static void usage(
void);
126 const char *slotname);
131 const char *slot_name);
155#define WAIT_INTERVAL 1
233 "You must recreate the physical replica before continuing.");
262 pg_log_warning(
"publication \"%s\" created in database \"%s\" on primary was left behind",
269 pg_log_warning(
"replication slot \"%s\" created in database \"%s\" on primary was left behind",
285 printf(
_(
"%s creates a new logical replica from a standby server.\n\n"),
290 printf(
_(
" -a, --all create subscriptions for all databases except template\n"
291 " databases and databases that don't allow connections\n"));
292 printf(
_(
" -d, --database=DBNAME database in which to create a subscription\n"));
293 printf(
_(
" -D, --pgdata=DATADIR location for the subscriber data directory\n"));
294 printf(
_(
" -l, --logdir=LOGDIR location for the log directory\n"));
295 printf(
_(
" -n, --dry-run dry run, just show what would be done\n"));
297 printf(
_(
" -P, --publisher-server=CONNSTR publisher connection string\n"));
298 printf(
_(
" -s, --socketdir=DIR socket directory to use (default current dir.)\n"));
299 printf(
_(
" -t, --recovery-timeout=SECS seconds to wait for recovery to end\n"));
300 printf(
_(
" -T, --enable-two-phase enable two-phase commit for all subscriptions\n"));
301 printf(
_(
" -U, --subscriber-username=NAME user name for subscriber connection\n"));
302 printf(
_(
" -v, --verbose output verbose messages\n"));
303 printf(
_(
" --clean=OBJECTTYPE drop all objects of the specified type from specified\n"
304 " databases on the subscriber; accepts: \"%s\"\n"),
"publications");
305 printf(
_(
" --config-file=FILENAME use specified main server configuration\n"
306 " file when running target cluster\n"));
307 printf(
_(
" --publication=NAME publication name\n"));
308 printf(
_(
" --replication-slot=NAME replication slot name\n"));
309 printf(
_(
" --subscription=NAME subscription name\n"));
310 printf(
_(
" -V, --version output version information, then exit\n"));
311 printf(
_(
" -?, --help show this help, then exit\n"));
431 pg_fatal(
"program \"%s\" is needed by %s but was not found in the same directory as \"%s\"",
434 pg_fatal(
"program \"%s\" was found by \"%s\" but was not the same version as %s",
455 pg_log_info(
"checking if directory \"%s\" is a cluster data directory",
475 pg_log_error_detail(
"File \"%s\" contains \"%s\", which is not compatible with this program's version \"%s\".",
559 pg_log_debug(
"publisher(%d): publication: %s ; replication slot: %s ; connection string: %s",
i,
563 pg_log_debug(
"subscriber(%d): subscription: %s ; connection string: %s, two_phase: %s",
i,
647 pg_log_info(
"getting system identifier from publisher");
651 res =
PQexec(
conn,
"SELECT system_identifier FROM pg_catalog.pg_control_system()");
660 pg_log_error(
"could not get system identifier: got %d rows, expected %d row",
687 pg_log_info(
"getting system identifier from subscriber");
691 pg_fatal(
"control file appears to be corrupt");
717 pg_log_info(
"modifying system identifier of subscriber");
721 pg_fatal(
"control file appears to be corrupt");
729 cf->system_identifier = ((
uint64) tv.tv_sec) << 32;
730 cf->system_identifier |= ((
uint64) tv.tv_usec) << 12;
731 cf->system_identifier |=
getpid() & 0xFFF;
735 cf->system_identifier);
740 cf->system_identifier);
744 pg_log_info(
"dry-run: would run pg_resetwal on the subscriber");
746 pg_log_info(
"running pg_resetwal on the subscriber");
770 pg_log_info(
"successfully reset WAL on the subscriber");
793 "SELECT oid FROM pg_catalog.pg_database "
794 "WHERE datname = pg_catalog.current_database()");
804 pg_log_error(
"could not obtain database OID: got %d rows, expected %d row",
822 objname =
psprintf(
"pg_createsubscriber_%u_%x", oid,
rand);
839 "SELECT 1 FROM pg_catalog.pg_publication "
840 "WHERE pubname = %s",
845 pg_log_error(
"could not find publication \"%s\" in database \"%s\": %s",
899 pg_log_info(
"using existing publication \"%s\" in database \"%s\"",
900 dbinfo[
i].pubname, dbinfo[
i].
dbname);
935 res =
PQexec(
conn,
"SELECT pg_log_standby_snapshot()");
938 pg_log_error(
"could not write an additional WAL record: %s",
960 res =
PQexec(
conn,
"SELECT pg_catalog.pg_is_in_recovery()");
995 (
unsigned int) (
tval.tv_usec / 1000));
1019 bool failed =
false;
1055 "SELECT pg_catalog.current_setting('wal_level'),"
1056 " pg_catalog.current_setting('max_replication_slots'),"
1057 " (SELECT count(*) FROM pg_catalog.pg_replication_slots),"
1058 " pg_catalog.current_setting('max_wal_senders'),"
1059 " (SELECT count(*) FROM pg_catalog.pg_stat_activity WHERE backend_type = 'walsender'),"
1060 " pg_catalog.current_setting('max_prepared_transactions'),"
1061 " pg_catalog.current_setting('max_slot_wal_keep_size')");
1065 pg_log_error(
"could not obtain publisher settings: %s",
1085 pg_log_debug(
"publisher: max_prepared_transactions: %d",
1094 pg_log_error(
"publisher requires \"wal_level\" >= \"replica\"");
1100 pg_log_error(
"publisher requires %d replication slots, but only %d remain",
1102 pg_log_error_hint(
"Increase the configuration parameter \"%s\" to at least %d.",
1109 pg_log_error(
"publisher requires %d WAL sender processes, but only %d remain",
1111 pg_log_error_hint(
"Increase the configuration parameter \"%s\" to at least %d.",
1118 pg_log_warning(
"two_phase option will not be enabled for replication slots");
1120 "Prepared transactions will be replicated at COMMIT PREPARED.");
1121 pg_log_warning_hint(
"You can use the command-line option --enable-two-phase to enable two_phase.");
1131 pg_log_warning(
"required WAL could be removed from the publisher");
1132 pg_log_warning_hint(
"Set the configuration parameter \"%s\" to -1 to ensure that required WAL files are not prematurely removed.",
1133 "max_slot_wal_keep_size");
1158 bool failed =
false;
1186 "SELECT setting FROM pg_catalog.pg_settings WHERE name IN ("
1187 "'max_logical_replication_workers', "
1188 "'max_active_replication_origins', "
1189 "'max_worker_processes', "
1190 "'primary_slot_name') "
1195 pg_log_error(
"could not obtain subscriber settings: %s",
1206 pg_log_debug(
"subscriber: max_logical_replication_workers: %d",
1219 pg_log_error(
"subscriber requires %d active replication origins, but only %d remain",
1221 pg_log_error_hint(
"Increase the configuration parameter \"%s\" to at least %d.",
1222 "max_active_replication_origins",
num_dbs);
1228 pg_log_error(
"subscriber requires %d logical replication workers, but only %d remain",
1230 pg_log_error_hint(
"Increase the configuration parameter \"%s\" to at least %d.",
1231 "max_logical_replication_workers",
num_dbs);
1237 pg_log_error(
"subscriber requires %d worker processes, but only %d remain",
1239 pg_log_error_hint(
"Increase the configuration parameter \"%s\" to at least %d.",
1240 "max_worker_processes",
num_dbs + 1);
1278 pg_log_info(
"dry-run: would drop subscription \"%s\" in database \"%s\"",
1282 pg_log_info(
"dropping subscription \"%s\" in database \"%s\"",
1316 "SELECT s.subname FROM pg_catalog.pg_subscription s "
1317 "INNER JOIN pg_catalog.pg_database d ON (s.subdbid = d.oid) "
1318 "WHERE d.datname = %s",
1324 pg_log_error(
"could not obtain pre-existing subscriptions: %s",
1405 "recovery_target_timeline = 'latest'\n");
1416 "recovery_target_inclusive = false\n");
1418 "recovery_target_action = promote\n");
1427 "recovery_target_lsn = '%X/%08X'\n",
1490 pg_log_warning(
"could not drop replication slot \"%s\" on primary",
1514 "SELECT slot_name FROM pg_catalog.pg_replication_slots WHERE failover");
1524 pg_log_warning(
"could not obtain failover replication slot information: %s",
1526 pg_log_warning_hint(
"Drop the failover replication slots on subscriber soon to avoid retention of WAL files.");
1535 pg_log_warning_hint(
"Drop the failover replication slots on subscriber soon to avoid retention of WAL files.");
1557 pg_log_info(
"dry-run: would create the replication slot \"%s\" in database \"%s\" on publisher",
1558 slot_name, dbinfo->
dbname);
1560 pg_log_info(
"creating the replication slot \"%s\" in database \"%s\" on publisher",
1561 slot_name, dbinfo->
dbname);
1566 "SELECT lsn FROM pg_catalog.pg_create_logical_replication_slot(%s, 'pgoutput', false, %s, false)",
1579 pg_log_error(
"could not create replication slot \"%s\" in database \"%s\": %s",
1580 slot_name, dbinfo->
dbname,
1601 const char *slot_name)
1610 pg_log_info(
"dry-run: would drop the replication slot \"%s\" in database \"%s\"",
1611 slot_name, dbinfo->
dbname);
1613 pg_log_info(
"dropping the replication slot \"%s\" in database \"%s\"",
1614 slot_name, dbinfo->
dbname);
1629 pg_log_error(
"could not drop replication slot \"%s\" in database \"%s\": %s",
1654 pg_log_error(
"pg_ctl was terminated by exception 0x%X",
1656 pg_log_error_detail(
"See C include file \"ntstatus.h\" for a description of the hexadecimal value.");
1664 pg_log_error(
"pg_ctl exited with unrecognized status %d", rc);
1754 pg_log_info(
"waiting for the target server to reach the consistent state");
1784 pg_fatal(
"server did not end recovery");
1786 pg_log_info(
"target server reached the consistent state");
1787 pg_log_info_hint(
"If pg_createsubscriber fails after this point, you must recreate the physical replica before continuing.");
1808 "SELECT 1 FROM pg_catalog.pg_publication "
1809 "WHERE pubname = %s",
1814 pg_log_error(
"could not obtain publication information: %s",
1837 pg_log_info(
"dry-run: would create publication \"%s\" in database \"%s\"",
1840 pg_log_info(
"creating publication \"%s\" in database \"%s\"",
1853 pg_log_error(
"could not create publication \"%s\" in database \"%s\": %s",
1883 pg_log_info(
"dry-run: would drop publication \"%s\" in database \"%s\"",
1886 pg_log_info(
"dropping publication \"%s\" in database \"%s\"",
1900 pg_log_error(
"could not drop publication \"%s\" in database \"%s\": %s",
1935 pg_log_info(
"dropping all existing publications in database \"%s\"",
1939 res =
PQexec(
conn,
"SELECT pubname FROM pg_catalog.pg_publication;");
1942 pg_log_error(
"could not obtain publication information: %s",
1964 pg_log_info(
"dry-run: would preserve existing publication \"%s\" in database \"%s\"",
1967 pg_log_info(
"preserving existing publication \"%s\" in database \"%s\"",
2002 pg_log_info(
"dry-run: would create subscription \"%s\" in database \"%s\"",
2005 pg_log_info(
"creating subscription \"%s\" in database \"%s\"",
2009 "CREATE SUBSCRIPTION %s CONNECTION %s PUBLICATION %s "
2010 "WITH (create_slot = false, enabled = false, "
2011 "slot_name = %s, copy_data = false, two_phase = %s)",
2027 pg_log_error(
"could not create subscription \"%s\" in database \"%s\": %s",
2064 "SELECT s.oid FROM pg_catalog.pg_subscription s "
2065 "INNER JOIN pg_catalog.pg_database d ON (s.subdbid = d.oid) "
2066 "WHERE s.subname = %s AND d.datname = %s",
2079 pg_log_error(
"could not obtain subscription OID: got %d rows, expected %d row",
2104 pg_log_info(
"dry-run: would set the replication progress (node name \"%s\", LSN %s) in database \"%s\"",
2107 pg_log_info(
"setting the replication progress (node name \"%s\", LSN %s) in database \"%s\"",
2112 "SELECT pg_catalog.pg_replication_origin_advance('%s', '%s')",
2122 pg_log_error(
"could not set replication progress for subscription \"%s\": %s",
2154 pg_log_info(
"dry-run: would enable subscription \"%s\" in database \"%s\"",
2157 pg_log_info(
"enabling subscription \"%s\" in database \"%s\"",
2169 pg_log_error(
"could not enable subscription \"%s\": %s",
2212 res =
PQexec(
conn,
"SELECT datname FROM pg_database WHERE datistemplate = false AND datallowconn AND datconnlimit <> -2 ORDER BY 1");
2285 if (
strcmp(argv[1],
"--help") == 0 ||
strcmp(argv[1],
"-?") == 0)
2290 else if (
strcmp(argv[1],
"-V") == 0
2291 ||
strcmp(argv[1],
"--version") == 0)
2330 while ((
c =
getopt_long(argc, argv,
"ad:D:l:np:P:s:t:TU:v",
2345 pg_fatal(
"database \"%s\" specified more than once for -d/--database",
optarg);
2390 pg_fatal(
"publication \"%s\" specified more than once for --publication",
optarg);
2399 pg_fatal(
"replication slot \"%s\" specified more than once for --replication-slot",
optarg);
2408 pg_fatal(
"subscription \"%s\" specified more than once for --subscription",
optarg);
2414 pg_fatal(
"object type \"%s\" specified more than once for --clean",
optarg);
2439 pg_log_error(
"options %s and %s cannot be used together",
2449 pg_log_error(
"too many command-line arguments (first is \"%s\")",
2458 pg_log_error(
"no subscriber data directory specified");
2469 pg_fatal(
"could not determine current directory");
2486 pg_log_error(
"no publisher connection string specified");
2523 "The target directory will not be modified.");
2525 pg_log_info(
"validating publisher connection string");
2531 pg_log_info(
"validating subscriber connection string");
2559 pg_log_info(
"database name \"%s\" was extracted from the publisher connection string",
2574 pg_log_error(
"wrong number of publication names specified");
2575 pg_log_error_detail(
"The number of specified publication names (%d) must match the number of specified database names (%d).",
2581 pg_log_error(
"wrong number of subscription names specified");
2582 pg_log_error_detail(
"The number of specified subscription names (%d) must match the number of specified database names (%d).",
2588 pg_log_error(
"wrong number of replication slot names specified");
2589 pg_log_error_detail(
"The number of specified replication slot names (%d) must match the number of specified database names (%d).",
2601 pg_log_error(
"invalid object type \"%s\" specified for option %s",
2602 cell->val,
"--clean");
2634 pg_fatal(
"subscriber data directory is not a copy of the source database cluster");
2657 pg_log_info(
"starting the standby server with command-line options");
Datum now(PG_FUNCTION_ARGS)
#define Assert(condition)
#define PG_TEXTDOMAIN(domain)
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)
bool GetDataDirectoryCreatePerm(const char *dataDir)
#define PG_MODE_MASK_OWNER
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_set_logfile(FILE *logfile)
void pg_logging_init(const char *argv0)
void pg_logging_set_level(enum pg_log_level new_level)
#define pg_log_error(...)
#define pg_log_error_hint(...)
#define pg_log_warning_hint(...)
#define pg_log_info_hint(...)
#define pg_log_warning_detail(...)
#define pg_log_error_detail(...)
#define pg_log_debug(...)
static PQExpBuffer recoveryconfcontents
#define SERVER_LOG_FILE_NAME
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)
#define INTERNAL_LOG_FILE_NAME
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 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 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
static void make_output_dirs(const char *log_basedir)
static void drop_publication(PGconn *conn, const char *pubname, const char *dbname)
#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]
#define pg_log_warning(...)
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
uint32 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