diff --git a/doc/src/sgml/ref/pg_basebackup.sgml b/doc/src/sgml/ref/pg_basebackup.sgml index 88c689e725..903da73df7 100644 --- a/doc/src/sgml/ref/pg_basebackup.sgml +++ b/doc/src/sgml/ref/pg_basebackup.sgml @@ -243,7 +243,10 @@ PostgreSQL documentation The postgresql.auto.conf file will record the connection settings and, if specified, the replication slot that pg_basebackup is using, so that - streaming replication will use the same settings later on. + streaming replication or + logical replication slot synchronization will use the same + settings later on. The dbname will be recorded only if the dbname was + specified explicitly in the connection string. @@ -807,7 +810,9 @@ PostgreSQL documentation client applications, but because pg_basebackup doesn't connect to any particular database in the cluster, any database name in the connection string will be ignored - by PostgreSQL. Middleware, or proxies, used in + by PostgreSQL. Middleware, proxies, or + + logical replication slot synchronization used in connecting to PostgreSQL might however utilize the value. diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c index 3a9940097c..2826af1252 100644 --- a/src/bin/pg_basebackup/pg_basebackup.c +++ b/src/bin/pg_basebackup/pg_basebackup.c @@ -1807,10 +1807,15 @@ BaseBackup(char *compression_algorithm, char *compression_detail, } /* - * Build contents of configuration file if requested + * Build contents of configuration file if requested. + * + * The dbname dbname is either of 1) the value written in the connection + * string, 2) specified as PGDATABASE or 3) the value written in the + * service file. */ if (writerecoveryconf) - recoveryconfcontents = GenerateRecoveryConfig(conn, replication_slot); + recoveryconfcontents = GenerateRecoveryConfig(conn, replication_slot, + GetDbnameFromConnectionStringOrDefaults()); /* * Run IDENTIFY_SYSTEM so we can get the timeline diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c index 56d1b15951..733eb89d49 100644 --- a/src/bin/pg_basebackup/streamutil.c +++ b/src/bin/pg_basebackup/streamutil.c @@ -34,6 +34,8 @@ int WalSegSz; static bool RetrieveDataDirCreatePerm(PGconn *conn); +static void FindDbnameFromPQconninfoOptions(PQconninfoOption *conn_opts, + char **dbname); /* SHOW command for replication connection was introduced in version 10 */ #define MINIMUM_VERSION_FOR_SHOW_CMD 100000 @@ -267,6 +269,71 @@ GetConnection(void) return tmpconn; } +/* + * FindDbnameFromPQconninfoOptions + * + * This is a helper function for GetDbnameFromConnectionStringOrDefaults(). + * Read all parameters in PQconninfoOption and extract the dbname to the + * argument. + */ +static void +FindDbnameFromPQconninfoOptions(PQconninfoOption *conn_opts, char **dbname) +{ + Assert(dbname != NULL); + + for (PQconninfoOption *conn_opt = conn_opts; conn_opt->keyword != NULL; + conn_opt++) + { + if ((strcmp(conn_opt->keyword, "dbname") == 0) && + conn_opt->val != NULL && conn_opt->val[0] != '\0') + *dbname = pg_strdup(conn_opt->val); + } +} + +/* + * GetDbnameFromConnectionStringOrDefaults + * + * If database is specified either in the connection string (higher priority) + * or enviroment variables, then return the last database name specified. + * If database is not specified, then return NULL. + */ +char * +GetDbnameFromConnectionStringOrDefaults(void) +{ + PQconninfoOption *conn_opts = NULL; + char *err_msg = NULL; + char *dbname = NULL; + + /* Parse the connection string first because this has higher priority */ + if (connection_string) + { + conn_opts = PQconninfoParse(connection_string, &err_msg); + if (conn_opts == NULL) + pg_fatal("%s", err_msg); + + FindDbnameFromPQconninfoOptions(conn_opts, &dbname); + } + + /* Skip till the cleanup phase when found */ + if (dbname) + goto cleanup; + + /* + * If we reach here, the connection string does not contain the dbname. + * Let's check default settings, especially environment variables and the + * service file. + */ + conn_opts = PQconndefaults(); + if (conn_opts == NULL) + pg_fatal("%s", err_msg); + + FindDbnameFromPQconninfoOptions(conn_opts, &dbname); + +cleanup: + PQconninfoFree(conn_opts); + return dbname; +} + /* * From version 10, explicitly set wal segment size using SHOW wal_segment_size * since ControlFile is not accessible here. diff --git a/src/bin/pg_basebackup/streamutil.h b/src/bin/pg_basebackup/streamutil.h index 7a3dd98da3..406c0070b2 100644 --- a/src/bin/pg_basebackup/streamutil.h +++ b/src/bin/pg_basebackup/streamutil.h @@ -31,6 +31,8 @@ extern PGconn *conn; extern PGconn *GetConnection(void); +extern char *GetDbnameFromConnectionStringOrDefaults(void); + /* Replication commands */ extern bool CreateReplicationSlot(PGconn *conn, const char *slot_name, const char *plugin, bool is_temporary, diff --git a/src/bin/pg_rewind/pg_rewind.c b/src/bin/pg_rewind/pg_rewind.c index bde90bf60b..8449ae78ef 100644 --- a/src/bin/pg_rewind/pg_rewind.c +++ b/src/bin/pg_rewind/pg_rewind.c @@ -451,7 +451,7 @@ main(int argc, char **argv) pg_log_info("no rewind required"); if (writerecoveryconf && !dry_run) WriteRecoveryConfig(conn, datadir_target, - GenerateRecoveryConfig(conn, NULL)); + GenerateRecoveryConfig(conn, NULL, NULL)); exit(0); } @@ -525,7 +525,7 @@ main(int argc, char **argv) /* Also update the standby configuration, if requested. */ if (writerecoveryconf && !dry_run) WriteRecoveryConfig(conn, datadir_target, - GenerateRecoveryConfig(conn, NULL)); + GenerateRecoveryConfig(conn, NULL, NULL)); /* don't need the source connection anymore */ source->destroy(source); diff --git a/src/fe_utils/recovery_gen.c b/src/fe_utils/recovery_gen.c index 63c78c986c..5904f0840a 100644 --- a/src/fe_utils/recovery_gen.c +++ b/src/fe_utils/recovery_gen.c @@ -18,9 +18,15 @@ static char *escape_quotes(const char *src); /* * Write recovery configuration contents into a fresh PQExpBuffer, and * return it. + * + * This can also accept the dbname, which should be written in the + * primary_conninfo. The walreciever process will ignore this value, but it is + * essential for the slotsync worker. It connects to the specified database on + * the primary server. */ PQExpBuffer -GenerateRecoveryConfig(PGconn *pgconn, const char *replication_slot) +GenerateRecoveryConfig(PGconn *pgconn, const char *replication_slot, + char *dbname) { PQconninfoOption *connOptions; PQExpBufferData conninfo_buf; @@ -66,6 +72,20 @@ GenerateRecoveryConfig(PGconn *pgconn, const char *replication_slot) appendPQExpBuffer(&conninfo_buf, "%s=", opt->keyword); appendConnStrVal(&conninfo_buf, opt->val); } + + if (dbname) + { + /* + * If dbname is specified in the connection, append the dbname. This + * will be used later for logical replication slot synchronization. + */ + if (conninfo_buf.len != 0) + appendPQExpBufferChar(&conninfo_buf, ' '); + + appendPQExpBuffer(&conninfo_buf, "%s=", "dbname"); + appendConnStrVal(&conninfo_buf, dbname); + } + if (PQExpBufferDataBroken(conninfo_buf)) pg_fatal("out of memory"); diff --git a/src/include/fe_utils/recovery_gen.h b/src/include/fe_utils/recovery_gen.h index f1b760604b..73c1aa8e59 100644 --- a/src/include/fe_utils/recovery_gen.h +++ b/src/include/fe_utils/recovery_gen.h @@ -21,7 +21,8 @@ #define MINIMUM_VERSION_FOR_RECOVERY_GUC 120000 extern PQExpBuffer GenerateRecoveryConfig(PGconn *pgconn, - const char *replication_slot); + const char *replication_slot, + char *dbname); extern void WriteRecoveryConfig(PGconn *pgconn, const char *target_dir, PQExpBuffer contents);