diff options
Diffstat (limited to 'src/bin/pg_basebackup/streamutil.c')
-rw-r--r-- | src/bin/pg_basebackup/streamutil.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/bin/pg_basebackup/streamutil.c b/src/bin/pg_basebackup/streamutil.c index 56d1b15951f..9ffd5a6ebb5 100644 --- a/src/bin/pg_basebackup/streamutil.c +++ b/src/bin/pg_basebackup/streamutil.c @@ -34,6 +34,7 @@ int WalSegSz; static bool RetrieveDataDirCreatePerm(PGconn *conn); +static void FindDbnameInConnParams(PQconninfoOption *conn_opts, char **dbname); /* SHOW command for replication connection was introduced in version 10 */ #define MINIMUM_VERSION_FOR_SHOW_CMD 100000 @@ -268,6 +269,75 @@ GetConnection(void) } /* + * FindDbnameInConnParams + * + * This is a helper function for GetDbnameFromConnectionOptions(). Extract + * the value of dbname from PQconninfoOption parameters. + */ +static void +FindDbnameInConnParams(PQconninfoOption *conn_opts, char **dbname) +{ + PQconninfoOption *conn_opt; + + Assert(dbname != NULL); + + for (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); + } +} + +/* + * GetDbnameFromConnectionOptions + * + * This is a special purpose function to retrieve the dbname from either the + * connection_string specified by the user or from the environment variables. + * + * We follow GetConnection() to fetch the dbname from various connection + * options. + * + * Returns NULL, if dbname is not specified by the user in the above + * mentioned connection options. + */ +char * +GetDbnameFromConnectionOptions(void) +{ + PQconninfoOption *conn_opts = NULL; + char *err_msg = NULL; + char *dbname = NULL; + + /* First try to get the dbname from connection string. */ + if (connection_string) + { + conn_opts = PQconninfoParse(connection_string, &err_msg); + if (conn_opts == NULL) + pg_fatal("%s", err_msg); + + FindDbnameInConnParams(conn_opts, &dbname); + if (dbname) + { + PQconninfoFree(conn_opts); + return dbname; + } + } + + /* + * Next try to get the dbname from default values that are available from + * the environment. + */ + conn_opts = PQconndefaults(); + if (conn_opts == NULL) + pg_fatal("out of memory"); + + FindDbnameInConnParams(conn_opts, &dbname); + + PQconninfoFree(conn_opts); + return dbname; +} + +/* * From version 10, explicitly set wal segment size using SHOW wal_segment_size * since ControlFile is not accessible here. */ |